From 7cc89d44f86ee8683388764ab6df78cab47c5ebb Mon Sep 17 00:00:00 2001 From: liangshenglin1 Date: Fri, 22 Oct 2021 21:29:51 +0800 Subject: [PATCH] remove rpc.js Signed-off-by: liangshenglin1 --- interfaces/kits/js/napi/BUILD.gn | 44 --- interfaces/kits/js/napi/rpc.js | 252 ------------------ .../src/napi/include/napi_message_parcel.h | 3 +- .../src/napi/include/napi_remote_object.h | 25 +- .../src/napi/src/napi_message_parcel.cpp | 105 +++----- .../src/napi/src/napi_remote_object.cpp | 240 +++++++++++------ .../src/napi/src/napi_rpc_native_module.cpp | 27 -- 7 files changed, 204 insertions(+), 492 deletions(-) delete mode 100644 interfaces/kits/js/napi/rpc.js diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn index ffcc2a4..1b8a6c4 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -11,52 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//ark/ts2abc/ts2panda/ts2abc_config.gni") import("//build/ohos.gni") -import("//foundation/ace/ace_engine/ace_config.gni") SUBSYSTEM_DIR = "//foundation/communication/ipc" -# compile .js to .abc. -action("gen_rpc_abc") { - visibility = [ ":*" ] - script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" - - args = [ - "--src-js", - rebase_path( - "//foundation/communication/ipc/interfaces/kits/js/napi/rpc.js"), - "--dst-file", - rebase_path(target_out_dir + "/rpc.abc"), - "--node", - rebase_path("${node_path}"), - "--frontend-tool-path", - rebase_path("${ts2abc_build_path}"), - "--node-modules", - rebase_path("${node_modules}"), - ] - deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build" ] - - inputs = [ "//foundation/communication/ipc/interfaces/kits/js/napi/rpc.js" ] - outputs = [ target_out_dir + "/rpc.abc" ] -} - -base_output_path = get_label_info(":rpc_js", "target_out_dir") -ipc_js_obj_path = base_output_path + "/rpc.o" - -gen_js_obj("rpc_js") { - input = "//foundation/communication/ipc/interfaces/kits/js/napi/rpc.js" - output = ipc_js_obj_path -} - -abc_output_path = get_label_info(":rpc_abc", "target_out_dir") -rpc_abc_obj_path = abc_output_path + "/rpc_abc.o" -gen_js_obj("rpc_abc") { - input = "$target_out_dir/rpc.abc" - output = rpc_abc_obj_path - dep = ":gen_rpc_abc" -} - config("rpc_public_config") { visibility = [ ":*" ] include_dirs = [ "$SUBSYSTEM_DIR/ipc/native/src/napi/include" ] @@ -78,8 +36,6 @@ ohos_shared_library("rpc") { ] deps = [ - ":rpc_abc", - ":rpc_js", "//foundation/ace/napi:ace_napi", "//third_party/libuv:uv_static", "//utils/native/base:utils", diff --git a/interfaces/kits/js/napi/rpc.js b/interfaces/kits/js/napi/rpc.js deleted file mode 100644 index 563a5ac..0000000 --- a/interfaces/kits/js/napi/rpc.js +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const rpcSo = requireInternal("rpc"); - -let NAPIMessageParcel = rpcSo.MessageParcel; -let NAPIIPCSkeleton = rpcSo.IPCSkeleton; -let NAPIRemoteObject = rpcSo.RemoteObject; -let RemoteProxy = rpcSo.RemoteProxy; -let MessageOption = rpcSo.MessageOption; - -class RemoteObject extends NAPIRemoteObject { - constructor(descriptor) { - if (typeof descriptor === 'string' && descriptor.length > 0) { - super(descriptor, descriptor.length); - this.descriptor = descriptor; - } else { - throw new NullPointerException("invalid descriptor"); - } - } - - addDeathRecipient(recipient, flags) { - return false; - } - - removeDeathRecipient(recipient, flags) { - return false; - } - - isObjectDead() { - return false; - } - - attachLocalInterface(localInterface, descriptor) { - this.descriptor = descriptor; - this.interface = localInterface; - } - - queryLocalInterface(descriptor) { - if (this.descriptor === descriptor) { - return this.interface; - } - return null; - } -} - -class IPCSkeleton extends NAPIIPCSkeleton { - static setCallingIdentity(identity) { - if (typeof identity === 'string') { - return NAPIIPCSkeleton.setCallingIdentity(identity, identity.length); - } - return false; - } -} - -class Exception { - constructor(msg) { - this.message = msg; - } - - getMessage() { - return this.message; - } -} - -class NullPointerException extends Exception {} -class SecurityException extends Exception {} -class IllegalArgumentException extends Exception {} -class IllegalStateException extends Exception {} -class UnsupportedOperationException extends Exception {} -class IndexOutOfBoundsException extends Exception {} -class NegativeArraySizeException extends Exception {} -class ArrayStoreException extends Exception {} -class ClassCastException extends Exception {} -class RemoteException extends Exception {} -class ParcelException extends Exception {} -class RuntimeException extends Exception {} - -class MessageParcel extends NAPIMessageParcel { - static EXC_INSECURITY = -1; - static EXC_ILLEGAL_ARGUMENT = -3; - static EXC_NULL_POINTER = -4; - static EXC_ILLEGAL_STATE = -5; - static EXC_UNSUPPORTED_OPERATION = -7; - static EXC_INDEX_OUTOF_BOUNDS = -10; - static EXC_NEGATIVE_ARRAY_SIZE = -11; - static EXC_ARRAY_STORE = -12; - static EXC_CLASS_CAST = -13; - static EXC_PARCEL_CAPACITY_ERROR = -14; - static EXC_REMOTE_TRANSACTION_FAILED = -200; - - createException(code, msg) { - switch (code) { - case MessageParcel.EXC_INSECURITY: { - return new SecurityException(msg); - } - case MessageParcel.EXC_ILLEGAL_ARGUMENT: { - return new IllegalArgumentException(msg); - } - case MessageParcel.EXC_NULL_POINTER: { - return new NullPointerException(msg); - } - case MessageParcel.EXC_ILLEGAL_STATE: { - return new IllegalStateException(msg); - } - case MessageParcel.EXC_UNSUPPORTED_OPERATION: { - return new UnsupportedOperationException(msg); - } - case MessageParcel.EXC_INDEX_OUTOF_BOUNDS: { - return new IndexOutOfBoundsException(msg); - } - case MessageParcel.EXC_NEGATIVE_ARRAY_SIZE: { - return new NegativeArraySizeException(msg); - } - case MessageParcel.EXC_ARRAY_STORE: { - return new ArrayStoreException(msg); - } - case MessageParcel.EXC_CLASS_CAST: { - return new ClassCastException(msg); - } - case MessageParcel.EXC_REMOTE_TRANSACTION_FAILED: { - return new RemoteException(msg); - } - case MessageParcel.EXC_PARCEL_CAPACITY_ERROR: { - return new ParcelException(msg); - } - default: { - return new RuntimeException("Unknown exception code: " + code + " msg " + msg); - } - } - } - - writeException(exception) { - let code = 0; - if (exception instanceof SecurityException) { - code = MessageParcel.EXC_INSECURITY; - } else if (exception instanceof IllegalArgumentException) { - code = MessageParcel.EXC_ILLEGAL_ARGUMENT; - } else if (exception instanceof NullPointerException) { - code = MessageParcel.EXC_NULL_POINTER; - } else if (exception instanceof IllegalStateException) { - code = MessageParcel.EXC_ILLEGAL_STATE; - } else if (exception instanceof UnsupportedOperationException) { - code = MessageParcel.EXC_UNSUPPORTED_OPERATION; - } else if (exception instanceof IndexOutOfBoundsException) { - code = MessageParcel.EXC_INDEX_OUTOF_BOUNDS; - } else if (exception instanceof NegativeArraySizeException) { - code = MessageParcel.EXC_NEGATIVE_ARRAY_SIZE; - } else if (exception instanceof ArrayStoreException) { - code = MessageParcel.EXC_ARRAY_STORE; - } else if (exception instanceof ClassCastException) { - code = MessageParcel.EXC_CLASS_CAST; - } else if (exception instanceof RemoteException) { - code = MessageParcel.EXC_REMOTE_TRANSACTION_FAILED; - } else if (exception instanceof ParcelException) { - code = MessageParcel.EXC_PARCEL_CAPACITY_ERROR; - } else { - code = 0; - } - this.writeInt(code); - if (code === 0) { - throw new RuntimeException(exception.getMessage()); - } - this.writeString(exception.getMessage()); - } - - writeNoException() { - this.writeInt(0); - } - - readException() { - let code = this.readInt(); - if (code === 0) { - return; - } - let msg = this.readString(); - let exception = this.createException(code, msg); - throw exception; - } - - createRemoteObjectArray() { - let num = this.readInt(); - if (num <= 0) { - return null; - } - let list = new Array(num); - for (let i = 0; i < num; i++) { - list[i] = this.readRemoteObject(); - } - return list; - } - - writeRemoteObjectArray(objects) { - if (objects === null || objects.length <= 0) { - this.writeInt(-1); - return false; - } - - let num = objects.length; - this.writeInt(num); - for (let i = 0; i < num; i++) { - this.writeRemoteObject(objects[i]); - } - return true; - } - - readRemoteObjectArray(objects) { - if (objects === null) { - return; - } - - let num = this.readInt(); - if (num !== objects.length) { - return; - } - for (let i = 0; i < num; i++) { - objects[i] = this.readRemoteObject(); - } - } -} - -export default { - MessageParcel: MessageParcel, - IPCSkeleton: IPCSkeleton, - RemoteObject: RemoteObject, - RemoteProxy: RemoteProxy, - MessageOption: MessageOption, - NullPointerException: NullPointerException, - SecurityException: SecurityException, - IllegalArgumentException: IllegalArgumentException, - IllegalStateException: IllegalStateException, - UnsupportedOperationException: UnsupportedOperationException, - IndexOutOfBoundsException: IndexOutOfBoundsException, - NegativeArraySizeException: NegativeArraySizeException, - ArrayStoreException: ArrayStoreException, - ClassCastException: ClassCastException, - RemoteException: RemoteException, - ParcelException: ParcelException, - RuntimeException: RuntimeException -} \ No newline at end of file diff --git a/ipc/native/src/napi/include/napi_message_parcel.h b/ipc/native/src/napi/include/napi_message_parcel.h index 453eecf..3af1502 100644 --- a/ipc/native/src/napi/include/napi_message_parcel.h +++ b/ipc/native/src/napi/include/napi_message_parcel.h @@ -32,7 +32,6 @@ public: static napi_value Export(napi_env env, napi_value exports); private: // Napi methods and properties - static napi_value JS_constructor(napi_env env, napi_callback_info cbinfo); static napi_value JS_create(napi_env env, napi_callback_info cbinfo); static napi_value JS_reclaim(napi_env env, napi_callback_info cbinfo); static napi_value JS_writeRemoteObject(napi_env env, napi_callback_info cbinfo); @@ -58,7 +57,6 @@ private: static napi_value JS_writeDouble(napi_env env, napi_callback_info cbinfo); static napi_value JS_writeBoolean(napi_env env, napi_callback_info cbinfo); static napi_value JS_writeChar(napi_env env, napi_callback_info cbinfo); - static napi_value JS_writeStringWithLength(napi_env env, napi_callback_info cbinfo); static napi_value JS_writeString(napi_env env, napi_callback_info cbinfo); static napi_value JS_writeSequenceable(napi_env env, napi_callback_info cbinfo); static napi_value JS_writeByteArray(napi_env env, napi_callback_info cbinfo); @@ -92,6 +90,7 @@ private: static napi_value JS_readCharArray(napi_env env, napi_callback_info cbinfo); static napi_value JS_readStringArray(napi_env env, napi_callback_info cbinfo); + static napi_value JS_constructor(napi_env env, napi_callback_info cbinfo); static void release(MessageParcel *parcel); napi_env env_ = nullptr; diff --git a/ipc/native/src/napi/include/napi_remote_object.h b/ipc/native/src/napi/include/napi_remote_object.h index 86a55ea..76d6231 100644 --- a/ipc/native/src/napi/include/napi_remote_object.h +++ b/ipc/native/src/napi/include/napi_remote_object.h @@ -32,10 +32,6 @@ EXTERN_C_START EXTERN_C_END // IPCSkeleton napi methods - napi_value NAPI_IPCSkeleton_getSystemAbility(napi_env env, napi_callback_info info); - - napi_value NAPI_IPCSkeleton_addSystemAbility(napi_env env, napi_callback_info info); - napi_value NAPI_IPCSkeleton_getContextObject(napi_env env, napi_callback_info info); napi_value NAPI_IPCSkeleton_getCallingPid(napi_env env, napi_callback_info info); @@ -55,18 +51,25 @@ EXTERN_C_END napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info info); // RemoteObject napi methods - napi_value NAPI_RemoteObject_getInterfaceDescriptor(napi_env env, napi_callback_info info); + napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info); napi_value NAPI_RemoteObject_getCallingPid(napi_env env, napi_callback_info info); napi_value NAPI_RemoteObject_getCallingUid(napi_env env, napi_callback_info info); - napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info); + napi_value NAPI_RemoteObject_getInterfaceDescriptor(napi_env env, napi_callback_info info); - // RemoteProxy napi methods - napi_value SendRequestPromise(napi_env env, sptr target, uint32_t code, - std::shared_ptr data, std::shared_ptr reply, MessageOption &option); + napi_value NAPI_RemoteObject_attachLocalInterface(napi_env env, napi_callback_info info); + napi_value NAPI_RemoteObject_queryLocalInterface(napi_env env, napi_callback_info info); + + napi_value NAPI_RemoteObject_addDeathRecipient(napi_env env, napi_callback_info info); + + napi_value NAPI_RemoteObject_removeDeathRecipient(napi_env env, napi_callback_info info); + + napi_value NAPI_RemoteObject_isObjectDead(napi_env env, napi_callback_info info); + + // RemoteProxy napi methods napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info); napi_value NAPI_RemoteProxy_queryLocalInterface(napi_env env, napi_callback_info info); @@ -79,8 +82,6 @@ EXTERN_C_END napi_value NAPI_RemoteProxy_isObjectDead(napi_env env, napi_callback_info info); - napi_value NAPI_RemoteProxy_getHandle(napi_env env, napi_callback_info info); - sptr NAPI_ohos_rpc_getNativeRemoteObject(napi_env env, napi_value object); napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr target); @@ -90,7 +91,7 @@ EXTERN_C_END uint32_t code; std::shared_ptr data; std::shared_ptr reply; - MessageOption& option; + MessageOption &option; napi_async_work asyncWork; napi_deferred deferred; int errCode; diff --git a/ipc/native/src/napi/src/napi_message_parcel.cpp b/ipc/native/src/napi/src/napi_message_parcel.cpp index 9b73dea..714c90c 100644 --- a/ipc/native/src/napi/src/napi_message_parcel.cpp +++ b/ipc/native/src/napi/src/napi_message_parcel.cpp @@ -296,45 +296,6 @@ napi_value NAPI_MessageParcel::JS_writeChar(napi_env env, napi_callback_info inf return napiValue; } -napi_value NAPI_MessageParcel::JS_writeStringWithLength(napi_env env, napi_callback_info info) -{ - size_t argc = 2; - size_t expectedArgc = 2; - napi_value argv[2] = {0}; - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter"); - - NAPI_MessageParcel *napiParcel = nullptr; - napi_unwrap(env, thisVar, (void **)&napiParcel); - NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null"); - - napi_valuetype valueType = napi_null; - napi_typeof(env, argv[0], &valueType); - NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1"); - - napi_typeof(env, argv[1], &valueType); - NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 2"); - - uint32_t maxStrLen = 40960; - uint32_t stringLength = 0; - napi_get_value_uint32(env, argv[1], &stringLength); - NAPI_ASSERT(env, stringLength < maxStrLen, "string length too large"); - - char stringValue[stringLength + 1]; - size_t jsStringLength = 0; - napi_get_value_string_utf8(env, argv[0], stringValue, stringLength + 1, &jsStringLength); - NAPI_ASSERT(env, jsStringLength == stringLength, "string length wrong"); - - CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * stringLength, napiParcel); - std::string parcelString = stringValue; - bool result = napiParcel->nativeParcel_->WriteString16(to_utf16(parcelString)); - - napi_value napiValue = nullptr; - NAPI_CALL(env, napi_get_boolean(env, result, &napiValue)); - return napiValue; -} - napi_value NAPI_MessageParcel::JS_writeByteArray(napi_env env, napi_callback_info info) { size_t argc = 1; @@ -716,7 +677,7 @@ napi_value NAPI_MessageParcel::JS_writeString(napi_env env, napi_callback_info i napi_value thisVar = nullptr; void *data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - NAPI_ASSERT(env, argc == 1, "requires 2 parameter"); + NAPI_ASSERT(env, argc == 1, "requires 1 parameter"); NAPI_MessageParcel *napiParcel = nullptr; napi_unwrap(env, thisVar, (void **)&napiParcel); @@ -823,46 +784,44 @@ napi_value NAPI_MessageParcel::JS_writeSequenceable(napi_env env, napi_callback_ napi_value funcArg[1] = { thisVar }; napi_value callResult = nullptr; - napi_status status = napi_call_function(env, argv[0], prop, 1, funcArg, &callResult); - bool result = (status == napi_ok); - bool isExceptionPending = false; - napi_is_exception_pending(env, &isExceptionPending); - if (isExceptionPending) { - napiParcel->nativeParcel_->RewindWrite(pos); + napi_call_function(env, argv[0], prop, 1, funcArg, &callResult); + if (callResult != nullptr) { + return callResult; } - - napi_value napiValue = nullptr; - NAPI_CALL(env, napi_get_boolean(env, result, &napiValue)); - return napiValue; + DBINDER_LOGE("call mashalling failed"); + napiParcel->nativeParcel_->RewindWrite(pos); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_boolean(env, false, &result)); + return result; } napi_value NAPI_MessageParcel::JS_writeSequenceableArray(napi_env env, napi_callback_info info) { + napi_value retValue = nullptr; + napi_get_boolean(env, false, &retValue); + size_t argc = 1; napi_value argv[1] = { 0 }; napi_value thisVar = nullptr; - void *data = nullptr; - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - NAPI_ASSERT(env, argc == 1, "requires 1 parameter"); + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + NAPI_ASSERT_BASE(env, argc == 1 && argv[0] != nullptr, "requires 1 parameter", retValue); bool isArray = false; napi_is_array(env, argv[0], &isArray); - NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1"); - + NAPI_ASSERT_BASE(env, isArray == true, "type mismatch for parameter 1", retValue); uint32_t arrayLength = 0; napi_get_array_length(env, argv[0], &arrayLength); NAPI_MessageParcel *napiParcel = nullptr; napi_unwrap(env, thisVar, (void **)&napiParcel); - NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null"); + NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", retValue); size_t pos = napiParcel->nativeParcel_->GetWritePosition(); - napiParcel->nativeParcel_->WriteUint32(arrayLength); - bool result = false; + bool result = napiParcel->nativeParcel_->WriteUint32(arrayLength); for (size_t i = 0; i < arrayLength; i++) { bool hasElement = false; napi_has_element(env, argv[0], i, &hasElement); - NAPI_ASSERT(env, hasElement == true, "parameter check error"); + NAPI_ASSERT_BASE(env, hasElement == true, "parameter check error", retValue); napi_value element = nullptr; napi_get_element(env, argv[0], i, &element); @@ -875,20 +834,15 @@ napi_value NAPI_MessageParcel::JS_writeSequenceableArray(napi_env env, napi_call napi_value funcArg[1] = { thisVar }; napi_value callResult = nullptr; - napi_status status = napi_call_function(env, element, prop, 1, funcArg, &callResult); - result = (status == napi_ok); - - bool isExceptionPending = false; - napi_is_exception_pending(env, &isExceptionPending); - if (!result || isExceptionPending) { + napi_call_function(env, element, prop, 1, funcArg, &callResult); + if (callResult == nullptr) { + DBINDER_LOGE("call mashalling failed, element index: %{public}zu", i); napiParcel->nativeParcel_->RewindWrite(pos); - break; + return retValue; } } - - napi_value napiValue = nullptr; - NAPI_CALL(env, napi_get_boolean(env, result, &napiValue)); - return napiValue; + napi_get_boolean(env, result, &retValue); + return retValue; } napi_value NAPI_MessageParcel::JS_readByte(napi_env env, napi_callback_info info) @@ -1719,7 +1673,6 @@ napi_value NAPI_MessageParcel::JS_readSequenceable(napi_env env, napi_callback_i NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0); int32_t len = napiParcel->nativeParcel_->ReadInt32(); - bool result = false; if (len > 0) { napi_value propKey = nullptr; const char *propKeyStr = "unmarshalling"; @@ -1729,12 +1682,15 @@ napi_value NAPI_MessageParcel::JS_readSequenceable(napi_env env, napi_callback_i napi_value funcArg[1] = {thisVar}; napi_value callResult = nullptr; - napi_status status = napi_call_function(env, argv[0], prop, 1, funcArg, &callResult); - result = (status == napi_ok); + napi_call_function(env, argv[0], prop, 1, funcArg, &callResult); + if (callResult != nullptr) { + return callResult; + } + DBINDER_LOGI("call unmarshalling failed"); } napi_value napiValue = nullptr; - NAPI_CALL(env, napi_get_boolean(env, result, &napiValue)); + NAPI_CALL(env, napi_get_boolean(env, false, &napiValue)); return napiValue; } @@ -1899,7 +1855,6 @@ napi_value NAPI_MessageParcel::Export(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("writeCharArray", NAPI_MessageParcel::JS_writeCharArray), DECLARE_NAPI_FUNCTION("writeStringArray", NAPI_MessageParcel::JS_writeStringArray), DECLARE_NAPI_FUNCTION("writeSequenceableArray", NAPI_MessageParcel::JS_writeSequenceableArray), - DECLARE_NAPI_FUNCTION("writeStringWithLength", NAPI_MessageParcel::JS_writeStringWithLength), DECLARE_NAPI_FUNCTION("readByte", NAPI_MessageParcel::JS_readByte), DECLARE_NAPI_FUNCTION("readShort", NAPI_MessageParcel::JS_readShort), DECLARE_NAPI_FUNCTION("readInt", NAPI_MessageParcel::JS_readInt), diff --git a/ipc/native/src/napi/src/napi_remote_object.cpp b/ipc/native/src/napi/src/napi_remote_object.cpp index 117722b..222a3d6 100644 --- a/ipc/native/src/napi/src/napi_remote_object.cpp +++ b/ipc/native/src/napi/src/napi_remote_object.cpp @@ -306,21 +306,27 @@ public: ~NAPIRemoteObjectHolder(); sptr Get(napi_value object); void Set(sptr object); + void attachLocalInterface(napi_value localInterface, std::string &descriptor); + napi_value queryLocalInterface(std::string &descriptor); private: std::mutex mutex_; napi_env env_ = nullptr; std::u16string descriptor_; sptr cachedObject_; + napi_ref localInterfaceRef_; }; NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor) - : env_(env), descriptor_(descriptor), cachedObject_(nullptr) + : env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr) {} NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder() { // free the reference of object. cachedObject_ = nullptr; + if (localInterfaceRef_ != nullptr) { + napi_delete_reference(env_, localInterfaceRef_); + } } sptr NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject) @@ -346,34 +352,48 @@ void NAPIRemoteObjectHolder::Set(sptr object) cachedObject_ = object; } +void NAPIRemoteObjectHolder::attachLocalInterface(napi_value localInterface, std::string &descriptor) +{ + if (localInterfaceRef_ != nullptr) { + napi_delete_reference(env_, localInterfaceRef_); + } + napi_create_reference(env_, localInterface, 1, &localInterfaceRef_); + descriptor_ = Str8ToStr16(descriptor); +} + +napi_value NAPIRemoteObjectHolder::queryLocalInterface(std::string &descriptor) +{ + if (!descriptor_.empty() && strcmp(Str16ToStr8(descriptor_).c_str(), descriptor.c_str()) == 0) { + napi_value ret = nullptr; + napi_get_reference_value(env_, localInterfaceRef_, &ret); + return ret; + } + napi_value result = nullptr; + napi_get_null(env_, &result); + return result; +} + napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) { // new napi remote object - size_t argc = 2; - size_t expectedArgc = 2; - napi_value argv[2] = { 0 }; + size_t argc = 1; + size_t expectedArgc = 1; + napi_value argv[1] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter"); - - napi_valuetype valueType = napi_null; + NAPI_ASSERT(env, argc == expectedArgc, "requires 1 parameters"); + napi_valuetype valueType; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1"); - - napi_typeof(env, argv[1], &valueType); - NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 2"); - - uint32_t stringLength = 0; - uint32_t maxStrLen = 40960; - napi_get_value_uint32(env, argv[1], &stringLength); - NAPI_ASSERT(env, stringLength < maxStrLen, "string length too large"); - - char stringValue[stringLength + 1]; + size_t bufferSize = 0; + size_t maxLen = 40960; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize); + NAPI_ASSERT(env, bufferSize < maxLen, "string length too large"); + char stringValue[bufferSize + 1]; size_t jsStringLength = 0; - napi_get_value_string_utf8(env, argv[0], stringValue, stringLength + 1, &jsStringLength); - NAPI_ASSERT(env, jsStringLength == stringLength, "string length wrong"); + napi_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength); + NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong"); std::string descriptor = stringValue; - auto holder = new NAPIRemoteObjectHolder(env, Str8ToStr16(descriptor)); // connect native object to js thisVar napi_status status = napi_wrap( @@ -395,10 +415,15 @@ napi_value NAPIRemoteObjectExport(napi_env env, napi_value exports) { const std::string className = "RemoteObject"; napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("getInterfaceDescriptor", NAPI_RemoteObject_getInterfaceDescriptor), DECLARE_NAPI_FUNCTION("sendRequest", NAPI_RemoteObject_sendRequest), DECLARE_NAPI_FUNCTION("getCallingPid", NAPI_RemoteObject_getCallingPid), DECLARE_NAPI_FUNCTION("getCallingUid", NAPI_RemoteObject_getCallingUid), + DECLARE_NAPI_FUNCTION("getInterfaceDescriptor", NAPI_RemoteObject_getInterfaceDescriptor), + DECLARE_NAPI_FUNCTION("attachLocalInterface", NAPI_RemoteObject_attachLocalInterface), + DECLARE_NAPI_FUNCTION("queryLocalInterface", NAPI_RemoteObject_queryLocalInterface), + DECLARE_NAPI_FUNCTION("addDeathRecipient", NAPI_RemoteObject_addDeathRecipient), + DECLARE_NAPI_FUNCTION("removeDeathRecipient", NAPI_RemoteObject_removeDeathRecipient), + DECLARE_NAPI_FUNCTION("isObjectDead", NAPI_RemoteObject_isObjectDead), }; napi_value constructor = nullptr; napi_define_class(env, className.c_str(), className.length(), RemoteObject_JS_Constructor, nullptr, @@ -1004,32 +1029,29 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info napi_get_boolean(env, true, &result); return result; } - size_t argc = 2; - size_t expectedArgc = 2; - napi_value argv[2] = {0}; - napi_value thisVar = nullptr; - void *data = nullptr; - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter"); - napi_valuetype valueType = napi_null; - napi_typeof(env, argv[0], &valueType); - NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1"); + napi_value retValue = nullptr; + napi_get_boolean(env, false, &retValue); - napi_typeof(env, argv[1], &valueType); - NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 2"); - - uint32_t stringLength = 0; - uint32_t maxStrLen = 40960; - napi_get_value_uint32(env, argv[1], &stringLength); - NAPI_ASSERT(env, stringLength < maxStrLen, "string length too large"); - - char stringValue[stringLength + 1]; + size_t argc = 1; + size_t expectedArgc = 1; + napi_value argv[1] = { 0 }; + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + NAPI_ASSERT_BASE(env, argc == expectedArgc, "requires 1 parameters", retValue); + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT_BASE(env, valueType == napi_string, "type mismatch for parameter 1", retValue); + size_t bufferSize = 0; + size_t maxLen = 40960; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize); + NAPI_ASSERT_BASE(env, bufferSize < maxLen, "string length too large", retValue); + char stringValue[bufferSize + 1]; size_t jsStringLength = 0; - napi_get_value_string_utf8(env, argv[0], stringValue, stringLength + 1, &jsStringLength); - NAPI_ASSERT(env, jsStringLength == stringLength, "string length wrong"); - std::string identity = stringValue; + napi_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength); + NAPI_ASSERT_BASE(env, jsStringLength == bufferSize, "string length wrong", retValue); + std::string identity = stringValue; napi_value napiIsLocalCalling = nullptr; napi_get_named_property(env, global, "isLocalCalling_", &napiIsLocalCalling); bool isLocalCalling = true; @@ -1076,6 +1098,33 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info } } +napi_value NAPI_RemoteObject_queryLocalInterface(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + size_t expectedArgc = 1; + napi_value argv[1] = { 0 }; + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + NAPI_ASSERT(env, argc == expectedArgc, "requires 1 parameters"); + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1"); + size_t bufferSize = 0; + size_t maxLen = 40960; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize); + NAPI_ASSERT(env, bufferSize < maxLen, "string length too large"); + char stringValue[bufferSize + 1]; + size_t jsStringLength = 0; + napi_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength); + NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong"); + std::string descriptor = stringValue; + NAPIRemoteObjectHolder *holder = nullptr; + napi_unwrap(env, thisVar, (void **)&holder); + NAPI_ASSERT(env, holder != nullptr, "failed to get napi remote object holder"); + napi_value ret = holder->queryLocalInterface(descriptor); + return ret; +} + napi_value NAPI_RemoteObject_getInterfaceDescriptor(napi_env env, napi_callback_info info) { napi_value result = nullptr; @@ -1173,11 +1222,11 @@ napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info) { DBINDER_LOGI("remote object send request starts"); size_t argc = 4; - size_t expectedArgc = 4; + size_t argcPromise = 4; napi_value argv[4] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - NAPI_ASSERT(env, argc == expectedArgc, "requires 4 parameter"); + NAPI_ASSERT(env, argc == argcPromise, "requires 4 parameters"); napi_valuetype valueType = napi_null; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1"); @@ -1197,7 +1246,6 @@ napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info) MessageOption *option = nullptr; status = napi_unwrap(env, argv[3], (void **)&option); NAPI_ASSERT(env, option != nullptr, "failed to get message option"); - int32_t code = 0; napi_get_value_int32(env, argv[0], &code); @@ -1205,6 +1253,60 @@ napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info) return StubSendRequestPromise(env, target, code, data->GetMessageParcel(), reply->GetMessageParcel(), *option); } +napi_value NAPI_RemoteObject_attachLocalInterface(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + size_t expectedArgc = 2; + napi_value argv[2] = { 0 }; + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameters"); + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_object, "type mismatch for parameter 1"); + napi_typeof(env, argv[1], &valueType); + NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 2"); + size_t bufferSize = 0; + size_t maxLen = 40960; + napi_get_value_string_utf8(env, argv[1], nullptr, 0, &bufferSize); + NAPI_ASSERT(env, bufferSize < maxLen, "string length too large"); + char stringValue[bufferSize + 1]; + size_t jsStringLength = 0; + napi_get_value_string_utf8(env, argv[1], stringValue, bufferSize + 1, &jsStringLength); + NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong"); + std::string descriptor = stringValue; + + NAPIRemoteObjectHolder *holder = nullptr; + napi_unwrap(env, thisVar, (void* *)&holder); + NAPI_ASSERT(env, holder != nullptr, "failed to get napi remote object holder"); + holder->attachLocalInterface(argv[0], descriptor); + + napi_value result = nullptr; + napi_get_undefined(env, &result); + return result; +} + +napi_value NAPI_RemoteObject_addDeathRecipient(napi_env env, napi_callback_info info) +{ + napi_value result = nullptr; + napi_get_boolean(env, false, &result); + return result; +} + +napi_value NAPI_RemoteObject_removeDeathRecipient(napi_env env, napi_callback_info info) +{ + napi_value result = nullptr; + napi_get_boolean(env, false, &result); + return result; +} + +napi_value NAPI_RemoteObject_isObjectDead(napi_env env, napi_callback_info info) +{ + napi_value result = nullptr; + napi_get_boolean(env, false, &result); + return result; +} + // This method runs on a worker thread, no access to the JavaScript void ExecuteSendRequest(napi_env env, void *data) { @@ -1215,7 +1317,7 @@ void ExecuteSendRequest(napi_env env, void *data) } // This method runs on the main thread after 'ExecuteSendRequest' exits -void SendRequestComplete(napi_env env, napi_status status, void *data) +void SendRequestPromiseComplete(napi_env env, napi_status status, void *data) { SendRequestParam *param = reinterpret_cast(data); napi_value result = nullptr; @@ -1236,7 +1338,7 @@ napi_value SendRequestPromise(napi_env env, sptr target, uint32_t napi_deferred deferred = nullptr; napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - SendRequestParam *sendRquestParam = new SendRequestParam { + SendRequestParam *sendRequestParam = new SendRequestParam { .target = target, .code = code, .data = data, @@ -1249,9 +1351,9 @@ napi_value SendRequestPromise(napi_env env, sptr target, uint32_t }; napi_value resourceName = nullptr; NAPI_CALL(env, napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteSendRequest, SendRequestComplete, - (void *)sendRquestParam, &sendRquestParam->asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, sendRquestParam->asyncWork)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteSendRequest, + SendRequestPromiseComplete, (void *)sendRequestParam, &sendRequestParam->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, sendRequestParam->asyncWork)); DBINDER_LOGI("sendRequest and returns promise"); return promise; } @@ -1260,11 +1362,11 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info) { DBINDER_LOGI("send request starts"); size_t argc = 4; - size_t expectedArgc = 4; + size_t argcPromise = 4; napi_value argv[4] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - NAPI_ASSERT(env, argc == expectedArgc, "requires 4 parameter"); + NAPI_ASSERT(env, argc == argcPromise, "requires 4 parameters"); napi_valuetype valueType = napi_null; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1"); @@ -1284,7 +1386,6 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info) MessageOption *option = nullptr; status = napi_unwrap(env, argv[3], (void **)&option); NAPI_ASSERT(env, option != nullptr, "failed to get message option"); - int32_t code = 0; napi_get_value_int32(env, argv[0], &code); @@ -1305,12 +1406,15 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); return undefined; } - return SendRequestPromise(env, target, code, data->GetMessageParcel(), reply->GetMessageParcel(), *option); + return SendRequestPromise(env, target, code, data->GetMessageParcel(), + reply->GetMessageParcel(), *option); } napi_value NAPI_RemoteProxy_queryLocalInterface(napi_env env, napi_callback_info info) { - return nullptr; + napi_value result = nullptr; + napi_get_null(env, &result); + return result; } napi_value NAPI_RemoteProxy_addDeathRecipient(napi_env env, napi_callback_info info) @@ -1469,30 +1573,6 @@ napi_value NAPI_RemoteProxy_isObjectDead(napi_env env, napi_callback_info info) } } -napi_value NAPI_RemoteProxy_getHandle(napi_env env, napi_callback_info info) -{ - DBINDER_LOGI("call get handle"); - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, 0, 0, &thisVar, nullptr); - NAPIRemoteProxyHolder *holder = nullptr; - napi_status status = napi_unwrap(env, thisVar, (void **)&holder); - NAPI_ASSERT(env, status == napi_ok, "failed to get proxy holder"); - napi_value result; - if (holder == nullptr) { - napi_create_uint32(env, 0, &result); - return result; - } - IPCObjectProxy *target = reinterpret_cast(holder->object_.GetRefPtr()); - if (target == nullptr) { - DBINDER_LOGE("Invalid proxy object"); - napi_create_uint32(env, 0, &result); - return result; - } - uint32_t handle = target->GetHandle(); - napi_create_uint32(env, handle, &result); - return result; -} - napi_value NAPIIPCSkeleton_JS_Constructor(napi_env env, napi_callback_info info) { napi_value thisArg = nullptr; diff --git a/ipc/native/src/napi/src/napi_rpc_native_module.cpp b/ipc/native/src/napi/src/napi_rpc_native_module.cpp index 1cbed70..019ce55 100644 --- a/ipc/native/src/napi/src/napi_rpc_native_module.cpp +++ b/ipc/native/src/napi/src/napi_rpc_native_module.cpp @@ -21,11 +21,6 @@ #include "napi/native_node_api.h" #include "napi_remote_object.h" -extern const char _binary_rpc_js_start[]; -extern const char _binary_rpc_js_end[]; -extern const char _binary_rpc_abc_start[]; -extern const char _binary_rpc_abc_end[]; - namespace OHOS { EXTERN_C_START static napi_value rpcExport(napi_env env, napi_value exports) @@ -39,28 +34,6 @@ static napi_value rpcExport(napi_env env, napi_value exports) } EXTERN_C_END -extern "C" __attribute__((visibility("default"))) void NAPI_rpc_GetJSCode(const char **buf, int *bufLen) -{ - if (buf != nullptr) { - *buf = _binary_rpc_js_start; - } - - if (bufLen != nullptr) { - *bufLen = _binary_rpc_js_end - _binary_rpc_js_start; - } -} - -// rpc JS register -extern "C" __attribute__((visibility("default"))) void NAPI_rpc_GetABCCode(const char **buf, int *buflen) -{ - if (buf != nullptr) { - *buf = _binary_rpc_abc_start; - } - if (buflen != nullptr) { - *buflen = _binary_rpc_abc_end - _binary_rpc_abc_start; - } -} - static napi_module RPCModule_ = { .nm_version = 1, .nm_flags = 0, -- GitLab