From 3d541f83d8f2f7faf94b28934f753f6125a119c5 Mon Sep 17 00:00:00 2001 From: hanlin15 Date: Fri, 12 Sep 2025 11:16:10 +0800 Subject: [PATCH] rpc Transfer Signed-off-by: hanlin15 --- ipc/native/src/napi/src/napi_remote_proxy.cpp | 8 - .../napi_common/source/napi_remote_object.cpp | 8 - ipc/native/src/taihe/BUILD.gn | 6 + ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe | 63 +++ ipc/native/src/taihe/inc/taihe_ashmem.h | 4 + .../src/taihe/inc/taihe_message_option.h | 4 + .../src/taihe/inc/taihe_remote_object.h | 3 + ipc/native/src/taihe/inc/taihe_remote_proxy.h | 2 + .../src/taihe/src/ohos.rpc.rpc.impl.cpp | 368 ++++++++++++++++++ 9 files changed, 450 insertions(+), 16 deletions(-) diff --git a/ipc/native/src/napi/src/napi_remote_proxy.cpp b/ipc/native/src/napi/src/napi_remote_proxy.cpp index 644a298c..1b3b17f1 100644 --- a/ipc/native/src/napi/src/napi_remote_proxy.cpp +++ b/ipc/native/src/napi/src/napi_remote_proxy.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #include "ipc_object_proxy.h" @@ -33,8 +32,6 @@ static std::atomic bytraceId = 1000; namespace OHOS { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_NAPI, "napi_remoteObject" }; -static const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. - static NapiError napiErr; static const size_t ARGV_INDEX_0 = 0; @@ -51,9 +48,6 @@ void ExecuteSendRequest(napi_env env, void *data) param->errCode = param->target->SendRequest(param->code, *(param->data.get()), *(param->reply.get()), param->option); ZLOGD(LOG_LABEL, "sendRequest done, errCode:%{public}d", param->errCode); - if (param->traceId != 0) { - FinishAsyncTrace(HITRACE_TAG_RPC, (param->traceValue).c_str(), param->traceId); - } } // This method runs on the main thread after 'ExecuteSendRequest' exits @@ -121,7 +115,6 @@ napi_value SendRequestAsync(napi_env env, sptr target, uint32_t c if (!remoteDescriptor.empty()) { sendRequestParam->traceValue = remoteDescriptor + std::to_string(code); sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); - StartAsyncTrace(HITRACE_TAG_RPC, (sendRequestParam->traceValue).c_str(), sendRequestParam->traceId); } napi_create_reference(env, argv[ARGV_INDEX_0], 1, &sendRequestParam->jsCodeRef); @@ -167,7 +160,6 @@ napi_value SendRequestPromise(napi_env env, sptr target, uint32_t if (!remoteDescriptor.empty()) { sendRequestParam->traceValue = remoteDescriptor + std::to_string(code); sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); - StartAsyncTrace(HITRACE_TAG_RPC, (sendRequestParam->traceValue).c_str(), sendRequestParam->traceId); } napi_create_reference(env, argv[ARGV_INDEX_0], 1, &sendRequestParam->jsCodeRef); diff --git a/ipc/native/src/napi_common/source/napi_remote_object.cpp b/ipc/native/src/napi_common/source/napi_remote_object.cpp index 18526d18..6c1666a1 100644 --- a/ipc/native/src/napi_common/source/napi_remote_object.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_object.cpp @@ -16,7 +16,6 @@ #include "napi_remote_object_holder.h" #include "napi_remote_object_internal.h" -#include #include #include @@ -50,8 +49,6 @@ static const size_t ARGV_LENGTH_5 = 5; static const size_t DEVICE_ID_LENGTH = 64; -static const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. - static std::atomic bytraceId = 1000; static NapiError napiErr; @@ -1148,9 +1145,6 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) uint64_t curTime = static_cast(std::chrono::duration_cast( std::chrono::steady_clock::now().time_since_epoch()).count()); ZLOGI(LOG_LABEL, "sendRequest done, errCode:%{public}d time:%{public}" PRIu64, param->errCode, curTime); - if (param->traceId != 0) { - FinishAsyncTrace(HITRACE_TAG_RPC, (param->traceValue).c_str(), param->traceId); - } auto task = [param]() { AfterWorkCallback(param); @@ -1190,7 +1184,6 @@ napi_value StubSendRequestAsync(napi_env env, sptr target, uint32 if (!remoteDescriptor.empty()) { sendRequestParam->traceValue = remoteDescriptor + std::to_string(code); sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); - StartAsyncTrace(HITRACE_TAG_RPC, (sendRequestParam->traceValue).c_str(), sendRequestParam->traceId); } } napi_create_reference(env, argv[ARGV_INDEX_0], 1, &sendRequestParam->jsCodeRef); @@ -1234,7 +1227,6 @@ napi_value StubSendRequestPromise(napi_env env, sptr target, uint if (!remoteDescriptor.empty()) { sendRequestParam->traceValue = remoteDescriptor + std::to_string(code); sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); - StartAsyncTrace(HITRACE_TAG_RPC, (sendRequestParam->traceValue).c_str(), sendRequestParam->traceId); } } napi_create_reference(env, argv[ARGV_INDEX_0], 1, &sendRequestParam->jsCodeRef); diff --git a/ipc/native/src/taihe/BUILD.gn b/ipc/native/src/taihe/BUILD.gn index eebdead5..39885cb3 100644 --- a/ipc/native/src/taihe/BUILD.gn +++ b/ipc/native/src/taihe/BUILD.gn @@ -54,6 +54,12 @@ config("taihe_inc_config") { "src/ohos.rpc.rpc.impl.cpp", "src/rpc_taihe_error.cpp", "src/remote_object_taihe_ani.cpp", + "$SUBSYSTEM_DIR/ipc/native/src/napi_common/source/napi_ashmem.cpp", + "$SUBSYSTEM_DIR/ipc/native/src/napi_common/source/napi_remote_object_holder.cpp", + "$SUBSYSTEM_DIR/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp", + "$SUBSYSTEM_DIR/ipc/native/src/napi_common/source/napi_remote_object.cpp", + "$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_remote_proxy.cpp", + "$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_message_option.cpp", ] deps = [ ":rpc_taihe_generate", diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe index 2e433a6c..2a37ec3e 100644 --- a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -48,11 +48,27 @@ struct RequestResult { @class interface Ashmem { + @!sts_inject_into_class(""" + static TransferStatic(input: Any): Object { + return Ashmem.rpcTransferStaticAshmem(ESValue.wrap(input)); + } + + static TransferDynamic(input: Object): Any { + return Ashmem.rpcTransferDynamicAshmem(input as Ashmem); + } + """) + MapReadWriteAshmem(): void; GetNativePtr(): i64; GetAshmemSize(): i32; } +@static("Ashmem") +function RpcTransferStaticAshmem(input: @sts_type("ESValue") Opaque): Ashmem; + +@static("Ashmem") +function RpcTransferDynamicAshmem(obj: Ashmem): @sts_type("Any") Opaque; + @static("Ashmem") @overload("create") function CreateAshmem_WithTwoParam(name: String, size: i32): Ashmem; @@ -201,6 +217,15 @@ interface IRemoteObject { @class interface RemoteObject : IRemoteObject { +@!sts_inject_into_class(""" + static TransferStatic(input: Any): Object { + return RemoteObject.rpcTransferStaticObject(ESValue.wrap(input)); + } + + static TransferDynamic(input: Object): Any { + return RemoteObject.rpcTransferDynamicObject(input as RemoteObject); + } + """) @!sts_inject_into_class(""" constructor(descriptor: String) { @@ -220,6 +245,12 @@ interface RemoteObject : IRemoteObject { AddJsObjWeakRef(obj: RemoteObject, isNative: bool): void; } +@static("RemoteObject") +function RpcTransferStaticObject(input: @sts_type("ESValue") Opaque): RemoteObject; + +@static("RemoteObject") +function RpcTransferDynamicObject(obj: RemoteObject): @sts_type("Any") Opaque; + @ctor("RemoteObject") function CreateRemoteObject(self: RemoteObject, descriptor: String): RemoteObject; @@ -228,20 +259,52 @@ function CreateRemoteObjectFromNative(nativePtr: i64): RemoteObject; @class interface RemoteProxy : IRemoteObject { + @!sts_inject_into_class(""" + static TransferStatic(input: Any): Object { + return RemoteProxy.rpcTransferStaticProxy(ESValue.wrap(input)); + } + + static TransferDynamic(input: Object): Any { + return RemoteProxy.rpcTransferDynamicProxy(input as RemoteProxy); + } + """) + GetNativePtr(): i64; AddJsObjWeakRef(obj: RemoteProxy): void; } +@static("RemoteProxy") +function RpcTransferStaticProxy(input: @sts_type("ESValue") Opaque): RemoteProxy; + +@static("RemoteProxy") +function RpcTransferDynamicProxy(obj: RemoteProxy): @sts_type("Any") Opaque; + @ctor("RemoteProxy") function CreateRemoteProxyFromNative(nativePtr: i64): RemoteProxy; @class interface MessageOption { + @!sts_inject_into_class(""" + static TransferStatic(input: Any): Object { + return MessageOption.rpcTransferStaticOption(ESValue.wrap(input)); + } + + static TransferDynamic(input: Object): Any { + return MessageOption.rpcTransferDynamicOption(input as MessageOption); + } + """) + IsAsync(): bool; SetAsync(isAsync: bool): void; GetNativePtr(): i64; } +@static("MessageOption") +function RpcTransferStaticOption(input: @sts_type("ESValue") Opaque): MessageOption; + +@static("MessageOption") +function RpcTransferDynamicOption(obj: MessageOption): @sts_type("Any") Opaque; + @ctor("MessageOption") function CreateMessageOption_WithTwoParam(syncFlags: i32, waitTime: i32): MessageOption; diff --git a/ipc/native/src/taihe/inc/taihe_ashmem.h b/ipc/native/src/taihe/inc/taihe_ashmem.h index 851f3645..8db7881e 100644 --- a/ipc/native/src/taihe/inc/taihe_ashmem.h +++ b/ipc/native/src/taihe/inc/taihe_ashmem.h @@ -26,6 +26,7 @@ #include #include #include +#include "napi/native_api.h" #include "ashmem.h" @@ -51,6 +52,9 @@ public: static ::ohos::rpc::rpc::Ashmem CreateAshmem_WithTwoParam(::taihe::string_view name, int32_t size); static ::ohos::rpc::rpc::Ashmem CreateAshmem_WithOneParam(::ohos::rpc::rpc::weak::Ashmem ashmem); + static ::ohos::rpc::rpc::Ashmem RpcTransferStaticAshmem(uintptr_t input); + static uintptr_t RpcTransferDynamicAshmem(::ohos::rpc::rpc::Ashmem obj); + static uintptr_t TransferDynamicAshmem(Ashmem* ashmem, napi_env jsenv, napi_value constructor); private: OHOS::sptr ashmem_ = nullptr; }; diff --git a/ipc/native/src/taihe/inc/taihe_message_option.h b/ipc/native/src/taihe/inc/taihe_message_option.h index 0d204266..ff67208f 100644 --- a/ipc/native/src/taihe/inc/taihe_message_option.h +++ b/ipc/native/src/taihe/inc/taihe_message_option.h @@ -26,6 +26,7 @@ #include #include #include +#include "napi/native_api.h" #include "message_option.h" @@ -47,6 +48,9 @@ public: static ::ohos::rpc::rpc::MessageOption CreateMessageOption_WithOneParam(bool isAsync); static ::ohos::rpc::rpc::MessageOption CreateMessageOption(); + static ::ohos::rpc::rpc::MessageOption RpcTransferStaticOption(uintptr_t input); + static uintptr_t RpcTransferDynamicOption(::ohos::rpc::rpc::MessageOption obj); + static uintptr_t TransferDynamicOption(MessageOption* messageOption, napi_env jsenv, napi_value jsMessageOption); private: std::shared_ptr messageOption_ = nullptr; std::optional<::ohos::rpc::rpc::weak::MessageOption> jsObjRef_; diff --git a/ipc/native/src/taihe/inc/taihe_remote_object.h b/ipc/native/src/taihe/inc/taihe_remote_object.h index f3fbad6c..b1757a41 100644 --- a/ipc/native/src/taihe/inc/taihe_remote_object.h +++ b/ipc/native/src/taihe/inc/taihe_remote_object.h @@ -72,6 +72,9 @@ public: ::taihe::string_view descriptor); static ::ohos::rpc::rpc::RemoteObject CreateRemoteObjectFromNative(uintptr_t nativePtr); + static ::ohos::rpc::rpc::RemoteObject RpcTransferStaticObject(uintptr_t input); + static uintptr_t RpcTransferDynamicObject(::ohos::rpc::rpc::RemoteObject obj); + static uintptr_t TransferDynamicObject(IRemoteObject* remoteObject, napi_env jsenv, napi_value constructor); private: std::mutex mutex_; OHOS::wptr wptrCachedObject_; diff --git a/ipc/native/src/taihe/inc/taihe_remote_proxy.h b/ipc/native/src/taihe/inc/taihe_remote_proxy.h index 290a661e..9fb4a762 100644 --- a/ipc/native/src/taihe/inc/taihe_remote_proxy.h +++ b/ipc/native/src/taihe/inc/taihe_remote_proxy.h @@ -54,6 +54,8 @@ public: void AddJsObjWeakRef(::ohos::rpc::rpc::weak::RemoteProxy obj); static ::ohos::rpc::rpc::RemoteProxy CreateRemoteProxyFromNative(uintptr_t nativePtr); + static ::ohos::rpc::rpc::RemoteProxy RpcTransferStaticProxy(uintptr_t input); + static uintptr_t RpcTransferDynamicProxy(::ohos::rpc::rpc::RemoteProxy obj); private: OHOS::sptr remoteObject_; OHOS::sptr cachedObject_; diff --git a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp index f03f9753..8710dd6b 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -46,6 +46,10 @@ #include "napi/native_api.h" #include "napi_message_sequence.h" +#include "napi_ashmem.h" +#include "napi_remote_object_holder.h" +#include "napi_remote_proxy_holder.h" +#include "message_option.h" namespace OHOS { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_OTHER, "RpcTaiheImpl" }; @@ -286,6 +290,81 @@ int64_t RemoteProxyImpl::GetNativePtr() return reinterpret_cast(cachedObject_.GetRefPtr()); } +::ohos::rpc::rpc::RemoteProxy RemoteProxyImpl::RpcTransferStaticProxy(uintptr_t input) +{ + void* nativePtr = nullptr; + if (!arkts_esvalue_unwrap(taihe::get_env(), reinterpret_cast(input), &nativePtr) || + !nativePtr) { + ZLOGE(LOG_LABEL, "arkts_esvalue_unwrap failed"); + return taihe::make_holder(0); + } + auto* napiRemoteProxy = reinterpret_cast(nativePtr); + if (!napiRemoteProxy) { + ZLOGE(LOG_LABEL, "napiRemoteProxy is nullptr"); + return taihe::make_holder(0); + } + auto remoteProxyptr = napiRemoteProxy->Get(); + auto jsref = taihe::make_holder(reinterpret_cast(remoteProxyptr.GetRefPtr())); + jsref->AddJsObjWeakRef(jsref); + return jsref; +} + +uintptr_t RemoteProxyImpl::RpcTransferDynamicProxy(::ohos::rpc::rpc::RemoteProxy obj) +{ + int64_t impRawPtr = obj->GetNativePtr(); + auto* proxy = reinterpret_cast(impRawPtr); + if (!proxy) { + ZLOGE(LOG_LABEL, "impl or objectProxy is nullptr"); + return 0; + } + napi_env jsenv; + if (!arkts_napi_scope_open(taihe::get_env(), &jsenv)) { + ZLOGE(LOG_LABEL, "arkts_napi_scope_open failed"); + return 0; + } + napi_value global = nullptr; + napi_status status = napi_get_global(jsenv, &global); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_get_global failed"); + return 0; + } + napi_value constructor = nullptr; + status = napi_get_named_property(jsenv, global, "IPCProxyConstructor_", &constructor); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "Get constructor failed"); + return 0; + } + + napi_value jsRemoteProxy = nullptr; + status = napi_new_instance(jsenv, constructor, 0, nullptr, &jsRemoteProxy); + + auto proxyHolder = new (std::nothrow) NAPIRemoteProxyHolder(); + if (!proxyHolder) { + return 0; + } + proxyHolder->object_ = proxy; + // connect native object to js thisVar + status = napi_wrap( + jsenv, + jsRemoteProxy, + proxyHolder, + [](napi_env env, void *data, void *hint) { + NAPIRemoteProxyHolder *remoteproxy = reinterpret_cast(data); + delete remoteproxy; + }, + nullptr, + nullptr); + uintptr_t result = 0; + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_wrap js RemoteProxy and native option failed"); + delete proxyHolder; + } else { + arkts_napi_scope_close_n(jsenv, 1, &jsRemoteProxy, reinterpret_cast(&result)); + } + return result; +} + void RemoteProxyImpl::AddJsObjWeakRef(::ohos::rpc::rpc::weak::RemoteProxy obj) { jsObjRef_ = std::optional<::ohos::rpc::rpc::weak::RemoteProxy>(std::in_place, obj); @@ -346,6 +425,106 @@ int64_t AshmemImpl::GetNativePtr() return reinterpret_cast(ashmem_.GetRefPtr()); } +::ohos::rpc::rpc::Ashmem AshmemImpl::RpcTransferStaticAshmem(uintptr_t input) +{ + ZLOGE(LOG_LABEL, "RpcTransferStaticImpl start"); + void* nativePtr = nullptr; + if (!arkts_esvalue_unwrap(taihe::get_env(), reinterpret_cast(input), &nativePtr) || + !nativePtr) { + ZLOGE(LOG_LABEL, "arkts_esvalue_unwrap failed"); + return taihe::make_holder(); + } + + auto* napiAshmem = reinterpret_cast(nativePtr); + if (!napiAshmem) { + ZLOGE(LOG_LABEL, "napiAshmem is nullptr"); + return taihe::make_holder(); + } + + OHOS::sptr ashmemImpl = napiAshmem->GetAshmem(); + if (!ashmemImpl) { + ZLOGE(LOG_LABEL, "ashmemImpl is nullptr"); + return taihe::make_holder(); + } + + auto jsref = taihe::make_holder(ashmemImpl); + return jsref; +} + +uintptr_t AshmemImpl::RpcTransferDynamicAshmem(::ohos::rpc::rpc::Ashmem obj) +{ + int64_t impRawPtr = obj->GetNativePtr(); + auto* ashmem = reinterpret_cast(impRawPtr); + if (!ashmem) { + ZLOGE(LOG_LABEL, "ashmem is nullptr"); + return 0; + } + napi_env jsenv; + if (!arkts_napi_scope_open(taihe::get_env(), &jsenv)) { + ZLOGE(LOG_LABEL, "arkts_napi_scope_open failed"); + return 0; + } + napi_value global = nullptr; + napi_status status = napi_get_global(jsenv, &global); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_get_global failed"); + return 0; + } + napi_value constructor = nullptr; + status = napi_get_named_property(jsenv, global, "AshmemConstructor_", &constructor); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "Get Ashmem constructor failed"); + return 0; + } + return TransferDynamicAshmem(ashmem, jsenv, constructor); +} + +uintptr_t AshmemImpl::TransferDynamicAshmem(Ashmem* ashmem, napi_env jsenv, napi_value constructor) +{ + if (!ashmem) { + ZLOGE(LOG_LABEL, "ashmem is nullptr"); + return 0; + } + int fd = ashmem->GetAshmemFd(); + int32_t size = ashmem->GetAshmemSize(); + if (fd <= 0 || size == 0) { + ZLOGE(LOG_LABEL, "fd <= 0 or size == 0"); + return 0; + } + int dupFd = dup(fd); + if (dupFd < 0) { + ZLOGE(LOG_LABEL, "Fail to dup fd:%{public}d", dupFd); + return 0; + } + napi_value jsAshmem = nullptr; + napi_status status = napi_new_instance(jsenv, constructor, 0, nullptr, &jsAshmem); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "Failed to construct js Ashmem"); + return 0; + } + auto newAshmem = new (std::nothrow) Ashmem(dupFd, size); + if (newAshmem == nullptr) { + close(dupFd); + ZLOGE(LOG_LABEL, "newAshmem is null"); + return 0; + } + status = napi_wrap( + jsenv, jsAshmem, newAshmem, + [](napi_env env, void *data, void *hint) { + Ashmem *ashmem = reinterpret_cast(data); + delete ashmem; + }, + nullptr, nullptr); + uintptr_t result = 0; + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "wrap js AshmemImpl and native option failed"); + delete newAshmem; + } else { + arkts_napi_scope_close_n(jsenv, 1, &jsAshmem, reinterpret_cast(&result)); + } + return result; +} + void AshmemImpl::MapReadWriteAshmem() { CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); @@ -483,6 +662,102 @@ OHOS::sptr RemoteObjectImpl::GetNativeObject() return tmp; } +::ohos::rpc::rpc::RemoteObject RemoteObjectImpl::RpcTransferStaticObject(uintptr_t input) +{ + ZLOGE(LOG_LABEL, "RpcTransferStaticImpl start"); + void* nativePtr = nullptr; + if (!arkts_esvalue_unwrap(taihe::get_env(), reinterpret_cast(input), &nativePtr) || + !nativePtr) { + ZLOGE(LOG_LABEL, "arkts_esvalue_unwrap failed"); + return taihe::make_holder(0); + } + + auto* napiRemoteObjectHolder = reinterpret_cast(nativePtr); + + if (!napiRemoteObjectHolder) { + ZLOGE(LOG_LABEL, "napiRemoteObjectHolder is nullptr"); + return taihe::make_holder(0); + } + + auto remoteObjectptr = napiRemoteObjectHolder->Get(); + auto jsref = taihe::make_holder(reinterpret_cast(remoteObjectptr.GetRefPtr())); + return jsref; +} + +uintptr_t RemoteObjectImpl::RpcTransferDynamicObject(::ohos::rpc::rpc::RemoteObject obj) +{ + int64_t impRawPtr = obj->GetNativePtr(); + auto *remoteObject = reinterpret_cast(impRawPtr); + if (!remoteObject) { + ZLOGE(LOG_LABEL, "remoteObject is nullptr"); + return 0; + } + + napi_env jsenv; + if (!arkts_napi_scope_open(taihe::get_env(), &jsenv)) { + ZLOGE(LOG_LABEL, "arkts_napi_scope_open failed"); + return 0; + } + napi_value global = nullptr; + napi_status status = napi_get_global(jsenv, &global); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_get_global failed"); + return 0; + } + napi_value constructor = nullptr; + status = napi_get_named_property(jsenv, global, "IPCStubConstructor_", &constructor); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "Get constructor failed"); + return 0; + } + return TransferDynamicObject(remoteObject, jsenv, constructor); +} + +uintptr_t RemoteObjectImpl::TransferDynamicObject(IRemoteObject* remoteObject, napi_env jsenv, napi_value constructor) +{ + if (!remoteObject) { + ZLOGE(LOG_LABEL, "remoteObject is nullptr"); + return 0; + } + auto descriptor = remoteObject->GetInterfaceDescriptor(); + std::u16string descStr16(descriptor.begin(), descriptor.end()); + const std::string descStr8 = Str16ToStr8(descStr16); + + napi_value jsRemoteObject = nullptr; + napi_value jsDesc = nullptr; + napi_create_string_utf8(jsenv, descStr8.c_str(), descStr8.length(), &jsDesc); + napi_value argv[1] = {jsDesc}; + napi_status status = napi_new_instance(jsenv, constructor, 1, argv, &jsRemoteObject); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_new_instance failed"); + return 0; + } + + auto holder = new (std::nothrow) NAPIRemoteObjectHolder(jsenv, OHOS::Str8ToStr16(descStr8), jsRemoteObject); + if (holder == nullptr) { + ZLOGE(LOG_LABEL, "new NAPIRemoteObjectHolder failed"); + return 0; + } + + status = napi_wrap( + jsenv, jsRemoteObject, holder, + [](napi_env env, void *data, void *hint) { + NAPIRemoteObjectHolder *remoteObject = reinterpret_cast(data); + delete remoteObject; + }, + nullptr, nullptr); + uintptr_t result = 0; + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_wrap failed"); + delete holder; + return 0; + } else { + arkts_napi_scope_close_n(jsenv, 1, &jsRemoteObject, reinterpret_cast(&result)); + } + return result; +} + int64_t RemoteObjectImpl::GetNativePtr() { return reinterpret_cast(sptrCachedObject_ != nullptr ? @@ -1311,6 +1586,91 @@ int64_t MessageOptionImpl::GetNativePtr() return reinterpret_cast(messageOption_.get()); } +::ohos::rpc::rpc::MessageOption MessageOptionImpl::RpcTransferStaticOption(uintptr_t input) +{ + ZLOGE(LOG_LABEL, "RpcTransferStaticImpl start"); + void* nativePtr = nullptr; + if (!arkts_esvalue_unwrap(taihe::get_env(), reinterpret_cast(input), &nativePtr) || + !nativePtr) { + ZLOGE(LOG_LABEL, "arkts_esvalue_unwrap failed"); + return taihe::make_holder(OHOS::MessageOption::TF_SYNC, + OHOS::MessageOption::TF_WAIT_TIME); + } + + auto* tempMessageOption = reinterpret_cast(nativePtr); + if (!tempMessageOption) { + ZLOGE(LOG_LABEL, "tempMessageOption is nullptr"); + return taihe::make_holder(OHOS::MessageOption::TF_SYNC, + OHOS::MessageOption::TF_WAIT_TIME); + } + auto jsref = taihe::make_holder(tempMessageOption->GetFlags(), + tempMessageOption->GetWaitTime()); + jsref->AddJsObjWeakRef(jsref); + return jsref; +} + +uintptr_t MessageOptionImpl::RpcTransferDynamicOption(::ohos::rpc::rpc::MessageOption obj) +{ + int64_t impRawPtr = obj->GetNativePtr(); + auto* messageOption = reinterpret_cast(impRawPtr); + if (!messageOption) { + ZLOGE(LOG_LABEL, "messageOptionTemp is nullptr"); + return 0; + } + + napi_env jsenv; + if (!arkts_napi_scope_open(taihe::get_env(), &jsenv)) { + ZLOGE(LOG_LABEL, "arkts_napi_scope_open failed"); + return 0; + } + napi_value global = nullptr; + napi_status status = napi_get_global(jsenv, &global); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_get_global failed"); + return 0; + } + napi_value constructor = nullptr; + status = napi_get_named_property(jsenv, global, "IPCOptionConstructor_", &constructor); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "get constructor failed"); + return 0; + } + napi_value jsMessageOption = nullptr; + status = napi_new_instance(jsenv, constructor, 0, nullptr, &jsMessageOption); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_new_instance failed"); + return 0; + } + return TransferDynamicOption(messageOption, jsenv, jsMessageOption); +} + +uintptr_t MessageOptionImpl::TransferDynamicOption(MessageOption* messageOpt, napi_env jsenv, + napi_value jsMessageOption) +{ + if (!messageOpt) { + ZLOGE(LOG_LABEL, "messageOpt is nullptr"); + return 0; + } + int flag = messageOpt->GetFlags(); + int waitTime = messageOpt->GetWaitTime(); + auto messageOption = new (std::nothrow) MessageOption(flag, waitTime); + napi_status status = napi_wrap( + jsenv, jsMessageOption, messageOption, + [](napi_env env, void *data, void *hint) { + ZLOGD(LOG_LABEL, "NAPIMessageOption destructed by js callback"); + delete (reinterpret_cast(data)); + }, + nullptr, nullptr); + uintptr_t result = 0; + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "wrap js MessageOption and native option failed"); + delete messageOption; + } else { + arkts_napi_scope_close_n(jsenv, 1, &jsMessageOption, reinterpret_cast(&result)); + } + return result; +} + void MessageOptionImpl::AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageOption obj) { jsObjRef_ = std::optional<::ohos::rpc::rpc::MessageOption>(std::in_place, obj); @@ -1365,7 +1725,11 @@ int64_t IPCSkeletonImpl::GetCallingTokenId() // NOLINTBEGIN TH_EXPORT_CPP_API_CreateRemoteObject(OHOS::RemoteObjectImpl::CreateRemoteObject); TH_EXPORT_CPP_API_CreateRemoteObjectFromNative(OHOS::RemoteObjectImpl::CreateRemoteObjectFromNative); +TH_EXPORT_CPP_API_RpcTransferStaticObject(OHOS::RemoteObjectImpl::RpcTransferStaticObject); +TH_EXPORT_CPP_API_RpcTransferDynamicObject(OHOS::RemoteObjectImpl::RpcTransferDynamicObject); TH_EXPORT_CPP_API_CreateRemoteProxyFromNative(OHOS::RemoteProxyImpl::CreateRemoteProxyFromNative); +TH_EXPORT_CPP_API_RpcTransferStaticProxy(OHOS::RemoteProxyImpl::RpcTransferStaticProxy); +TH_EXPORT_CPP_API_RpcTransferDynamicProxy(OHOS::RemoteProxyImpl::RpcTransferDynamicProxy); TH_EXPORT_CPP_API_CreateMessageSequence(OHOS::MessageSequenceImpl::CreateMessageSequence); TH_EXPORT_CPP_API_CloseFileDescriptor(OHOS::MessageSequenceImpl::CloseFileDescriptor); TH_EXPORT_CPP_API_RpcTransferStaicImpl(OHOS::MessageSequenceImpl::RpcTransferStaicImpl); @@ -1373,8 +1737,12 @@ TH_EXPORT_CPP_API_RpcTransferDynamicImpl(OHOS::MessageSequenceImpl::RpcTransferD TH_EXPORT_CPP_API_CreateMessageOption_WithTwoParam(OHOS::MessageOptionImpl::CreateMessageOption_WithTwoParam); TH_EXPORT_CPP_API_CreateMessageOption_WithOneParam(OHOS::MessageOptionImpl::CreateMessageOption_WithOneParam); TH_EXPORT_CPP_API_CreateMessageOption(OHOS::MessageOptionImpl::CreateMessageOption); +TH_EXPORT_CPP_API_RpcTransferStaticOption(OHOS::MessageOptionImpl::RpcTransferStaticOption); +TH_EXPORT_CPP_API_RpcTransferDynamicOption(OHOS::MessageOptionImpl::RpcTransferDynamicOption); TH_EXPORT_CPP_API_CreateAshmem_WithTwoParam(OHOS::AshmemImpl::CreateAshmem_WithTwoParam); TH_EXPORT_CPP_API_CreateAshmem_WithOneParam(OHOS::AshmemImpl::CreateAshmem_WithOneParam); +TH_EXPORT_CPP_API_RpcTransferStaticAshmem(OHOS::AshmemImpl::RpcTransferStaticAshmem); +TH_EXPORT_CPP_API_RpcTransferDynamicAshmem(OHOS::AshmemImpl::RpcTransferDynamicAshmem); TH_EXPORT_CPP_API_GetCallingPid(OHOS::IPCSkeletonImpl::GetCallingPid); TH_EXPORT_CPP_API_GetCallingUid(OHOS::IPCSkeletonImpl::GetCallingUid); TH_EXPORT_CPP_API_GetCallingTokenId(OHOS::IPCSkeletonImpl::GetCallingTokenId); -- Gitee