From 59e6cd0b2f8ca26e471bd6cbdd10cac87aa9ea95 Mon Sep 17 00:00:00 2001 From: Dragon51 Date: Mon, 11 Aug 2025 18:11:39 +0800 Subject: [PATCH] transfer Signed-off-by: Dragon51 --- ipc/native/src/napi/src/napi_remote_proxy.cpp | 10 +- .../napi_common/source/napi_remote_object.cpp | 10 +- ipc/native/src/taihe/BUILD.gn | 13 +- ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe | 67 ++ .../src/taihe/inc/remote_object_taihe.h | 22 + .../src/taihe/inc/remote_object_taihe_ani.h | 33 + ipc/native/src/taihe/inc/rpc_taihe_error.h | 56 +- ipc/native/src/taihe/inc/taihe_ashmem.h | 3 + .../src/taihe/inc/taihe_message_option.h | 7 + .../src/taihe/inc/taihe_message_sequence.h | 5 + .../src/taihe/inc/taihe_remote_object.h | 18 + ipc/native/src/taihe/inc/taihe_remote_proxy.h | 9 + .../src/taihe/src/ohos.rpc.rpc.impl.cpp | 613 +++++++++++++++--- .../src/taihe/src/remote_object_taihe_ani.cpp | 77 +++ 14 files changed, 807 insertions(+), 136 deletions(-) create mode 100644 ipc/native/src/taihe/inc/remote_object_taihe.h create mode 100644 ipc/native/src/taihe/inc/remote_object_taihe_ani.h create mode 100644 ipc/native/src/taihe/src/remote_object_taihe_ani.cpp diff --git a/ipc/native/src/napi/src/napi_remote_proxy.cpp b/ipc/native/src/napi/src/napi_remote_proxy.cpp index 644a298c..35ee5be4 100644 --- a/ipc/native/src/napi/src/napi_remote_proxy.cpp +++ b/ipc/native/src/napi/src/napi_remote_proxy.cpp @@ -14,7 +14,7 @@ */ #include -#include +//#include #include #include "ipc_object_proxy.h" @@ -33,7 +33,7 @@ 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 const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. static NapiError napiErr; @@ -52,7 +52,7 @@ void ExecuteSendRequest(napi_env env, void *data) *(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); + //FinishAsyncTrace(HITRACE_TAG_RPC, (param->traceValue).c_str(), param->traceId); } } @@ -121,7 +121,7 @@ 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); + //StartAsyncTrace(HITRACE_TAG_RPC, (sendRequestParam->traceValue).c_str(), sendRequestParam->traceId); } napi_create_reference(env, argv[ARGV_INDEX_0], 1, &sendRequestParam->jsCodeRef); @@ -167,7 +167,7 @@ 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); + //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..640d373f 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,7 @@ #include "napi_remote_object_holder.h" #include "napi_remote_object_internal.h" -#include +//#include #include #include @@ -50,7 +50,7 @@ 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 const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. static std::atomic bytraceId = 1000; static NapiError napiErr; @@ -1149,7 +1149,7 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) 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); + //FinishAsyncTrace(HITRACE_TAG_RPC, (param->traceValue).c_str(), param->traceId); } auto task = [param]() { @@ -1190,7 +1190,7 @@ 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); + //StartAsyncTrace(HITRACE_TAG_RPC, (sendRequestParam->traceValue).c_str(), sendRequestParam->traceId); } } napi_create_reference(env, argv[ARGV_INDEX_0], 1, &sendRequestParam->jsCodeRef); @@ -1234,7 +1234,7 @@ 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); + //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 c196bf71..4b77f0ef 100644 --- a/ipc/native/src/taihe/BUILD.gn +++ b/ipc/native/src/taihe/BUILD.gn @@ -43,19 +43,30 @@ if (support_jsapi) { "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core/include", "$SUBSYSTEM_DIR/ipc/native/src/taihe/inc", "$SUBSYSTEM_DIR/utils/include", + "$SUBSYSTEM_DIR/ipc/native/src/napi_common/include", ] sources += [ "src/ani_constructor.cpp", "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", - "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core:ipc_core" + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core:ipc_core", + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_napi_common:ipc_napi", ] external_deps = [ "c_utils:utils", "hilog:libhilog", + "napi:ace_napi", + "runtime_core:ani_helpers", ] } diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe index 9a1c7e52..3e82f443 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; @@ -163,6 +179,10 @@ function CreateMessageSequence(): MessageSequence; @static("MessageSequence") function CloseFileDescriptor(fd: i32): void; +function unwrapRemoteObject(obj: IRemoteObjectUnion): i64; + +function wrapRemoteObject(nativePtr: i64): IRemoteObjectUnion; + interface IRemoteObject { GetLocalInterface(descriptor: String): IRemoteBroker; @@ -180,6 +200,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) { @@ -199,6 +228,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; @@ -207,20 +242,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/remote_object_taihe.h b/ipc/native/src/taihe/inc/remote_object_taihe.h new file mode 100644 index 00000000..c5775917 --- /dev/null +++ b/ipc/native/src/taihe/inc/remote_object_taihe.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef TAIHE_REMOTE_OBJECT_H +#define TAIHE_REMOTE_OBJECT_H + +void WriteRemoteObjecttaihe(::ohos::rpc::rpc::weak::MessageSequence obj, + obj::ohos::rpc::rpc::IRemoteObjectUnion const& object); +::ohos::rpc::rpc::IRemoteObjectUnion ReadRemoteObjecttaihe(::ohos::rpc::rpc::weak::MessageSequence object); +#endif // TAIHE_REMOTE_OBJECT_HSSSS \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/remote_object_taihe_ani.h b/ipc/native/src/taihe/inc/remote_object_taihe_ani.h new file mode 100644 index 00000000..ca403ac8 --- /dev/null +++ b/ipc/native/src/taihe/inc/remote_object_taihe_ani.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef REMOTE_OBJECT_TAIHE_ANI_H +#define REMOTE_OBJECT_TAIHE_ANI_H + +#include +#include +#include "iremote_object.h" + +namespace OHOS { + +class RemoteObjectTaiheAni { +public: + static ani_object ANI_ohos_rpc_CreateJsRemoteObject(ani_env *env, OHOS::sptr remoteObject); + static OHOS::sptr AniGetNativeRemoteObject(ani_env *env, ani_object obj); + OHOS::sptr nativeRemoteObject_; +}; +} // namespace OHOS + +#endif // REMOTE_OBJECT_TAIHE_ANI_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/rpc_taihe_error.h b/ipc/native/src/taihe/inc/rpc_taihe_error.h index f1f248d9..0a025e54 100644 --- a/ipc/native/src/taihe/inc/rpc_taihe_error.h +++ b/ipc/native/src/taihe/inc/rpc_taihe_error.h @@ -21,20 +21,20 @@ namespace OHOS { enum RpcTaiheErrorCode { - CHECK_PARAM_ERROR, - OS_MMAP_ERROR, - OS_IOCTL_ERROR, - WRITE_TO_ASHMEM_ERROR, - READ_FROM_ASHMEM_ERROR, - ONLY_PROXY_OBJECT_PERMITTED_ERROR, - ONLY_REMOTE_OBJECT_PERMITTED_ERROR, - COMMUNICATION_ERROR, - PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, - WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, - READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, - PARCEL_MEMORY_ALLOC_ERROR, - CALL_JS_METHOD_ERROR, - OS_DUP_ERROR + RPC_ERR_CHECK_PARAM_ERROR, + RPC_ERR_OS_MMAP_ERROR, + RPC_ERR_OS_IOCTL_ERROR, + RPC_ERR_WRITE_TO_ASHMEM_ERROR, + RPC_ERR_READ_FROM_ASHMEM_ERROR, + RPC_ERR_ONLY_PROXY_OBJECT_PERMITTED_ERROR, + RPC_ERR_ONLY_REMOTE_OBJECT_PERMITTED_ERROR, + RPC_ERR_COMMUNICATION_ERROR, + RPC_ERR_PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, + RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, + RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_ERR_PARCEL_MEMORY_ALLOC_ERROR, + RPC_ERR_CALL_JS_METHOD_ERROR, + RPC_ERR_OS_DUP_ERROR }; struct RpcTaiheErrorInfo { @@ -59,20 +59,20 @@ struct RpcTaiheErrorInfo { } while (0) const std::map RPC_TAIHE_ERR_MAP { - { CHECK_PARAM_ERROR, { 401, "check param error" } }, - { OS_MMAP_ERROR, { 1900001, "os mmap function failed" } }, - { OS_IOCTL_ERROR, { 1900002, "os ioctl function failed" } }, - { WRITE_TO_ASHMEM_ERROR, { 1900003, "write to ashmem failed" } }, - { READ_FROM_ASHMEM_ERROR, { 1900004, "read from ashmem failed" } }, - { ONLY_PROXY_OBJECT_PERMITTED_ERROR, { 1900005, "only proxy object permitted" } }, - { ONLY_REMOTE_OBJECT_PERMITTED_ERROR, { 1900006, "only remote object permitted" } }, - { COMMUNICATION_ERROR, { 1900007, "communication failed" } }, - { PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, { 1900008, "proxy or remote object is invalid" } }, - { WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, { 1900009, "write data to message sequence failed" } }, - { READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, { 1900010, "read data from message sequence failed" } }, - { PARCEL_MEMORY_ALLOC_ERROR, { 1900011, "parcel memory alloc failed" } }, - { CALL_JS_METHOD_ERROR, { 1900012, "call js method failed" } }, - { OS_DUP_ERROR, { 1900013, "os dup function failed" } } + { RPC_ERR_CHECK_PARAM_ERROR, { 401, "check param error" } }, + { RPC_ERR_OS_MMAP_ERROR, { 1900001, "os mmap function failed" } }, + { RPC_ERR_OS_IOCTL_ERROR, { 1900002, "os ioctl function failed" } }, + { RPC_ERR_WRITE_TO_ASHMEM_ERROR, { 1900003, "write to ashmem failed" } }, + { RPC_ERR_READ_FROM_ASHMEM_ERROR, { 1900004, "read from ashmem failed" } }, + { RPC_ERR_ONLY_PROXY_OBJECT_PERMITTED_ERROR, { 1900005, "only proxy object permitted" } }, + { RPC_ERR_ONLY_REMOTE_OBJECT_PERMITTED_ERROR, { 1900006, "only remote object permitted" } }, + { RPC_ERR_COMMUNICATION_ERROR, { 1900007, "communication failed" } }, + { RPC_ERR_PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, { 1900008, "proxy or remote object is invalid" } }, + { RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, { 1900009, "write data to message sequence failed" } }, + { RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, { 1900010, "read data from message sequence failed" } }, + { RPC_ERR_PARCEL_MEMORY_ALLOC_ERROR, { 1900011, "parcel memory alloc failed" } }, + { RPC_ERR_CALL_JS_METHOD_ERROR, { 1900012, "call js method failed" } }, + { RPC_ERR_OS_DUP_ERROR, { 1900013, "os dup function failed" } } }; class RpcTaiheError { diff --git a/ipc/native/src/taihe/inc/taihe_ashmem.h b/ipc/native/src/taihe/inc/taihe_ashmem.h index 851f3645..caba4315 100644 --- a/ipc/native/src/taihe/inc/taihe_ashmem.h +++ b/ipc/native/src/taihe/inc/taihe_ashmem.h @@ -50,6 +50,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); 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..bd802d52 100644 --- a/ipc/native/src/taihe/inc/taihe_message_option.h +++ b/ipc/native/src/taihe/inc/taihe_message_option.h @@ -47,6 +47,13 @@ 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); + + std::shared_ptr GetMessageOption() const + { + return messageOption_; + } private: std::shared_ptr messageOption_ = nullptr; std::optional<::ohos::rpc::rpc::weak::MessageOption> jsObjRef_; diff --git a/ipc/native/src/taihe/inc/taihe_message_sequence.h b/ipc/native/src/taihe/inc/taihe_message_sequence.h index 5866e363..85871598 100644 --- a/ipc/native/src/taihe/inc/taihe_message_sequence.h +++ b/ipc/native/src/taihe/inc/taihe_message_sequence.h @@ -118,6 +118,11 @@ public: static ::ohos::rpc::rpc::MessageSequence CreateMessageSequence(); static void CloseFileDescriptor(int32_t fd); + static void WriteRemoteObjecttaihe(::ohos::rpc::rpc::weak::MessageSequence obj, + ::ohos::rpc::rpc::IRemoteObjectUnion const& object); + + static ::ohos::rpc::rpc::IRemoteObjectUnion ReadRemoteObjecttaihe(::ohos::rpc::rpc::weak::MessageSequence object); + private: OHOS::MessageParcel* nativeParcel_ = nullptr; std::optional<::ohos::rpc::rpc::weak::MessageSequence> 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..2cf6312d 100644 --- a/ipc/native/src/taihe/inc/taihe_remote_object.h +++ b/ipc/native/src/taihe/inc/taihe_remote_object.h @@ -72,6 +72,24 @@ 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); + + OHOS::wptr GetWptrCachedObject() const + { + return wptrCachedObject_; + } + + OHOS::sptr GetSptrCachedObject() const + { + return sptrCachedObject_; + } + + ::taihe::string Getdesc() const + { + return desc_; + } + 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 9ec46e09..736d5260 100644 --- a/ipc/native/src/taihe/inc/taihe_remote_proxy.h +++ b/ipc/native/src/taihe/inc/taihe_remote_proxy.h @@ -54,6 +54,15 @@ public: static ::ohos::rpc::rpc::RemoteProxy CreateRemoteProxyFromNative(uintptr_t nativePtr); + int64_t GetRemoteProxyImpl(); + static ::ohos::rpc::rpc::RemoteProxy RpcTransferStaticProxy(uintptr_t input); + static uintptr_t RpcTransferDynamicProxy(::ohos::rpc::rpc::RemoteProxy obj); + + sptr GetObjectProxy() const + { + return cachedObject_; + } + private: OHOS::sptr cachedObject_; std::optional<::ohos::rpc::rpc::RemoteProxy> jsObjRef_; 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 8d3ea8e5..097e61f6 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -40,7 +40,13 @@ #include "taihe_parcelable.h" #include "taihe_remote_object.h" #include "taihe_remote_proxy.h" - +#include "interop_js/arkts_esvalue.h" +#include "interop_js/arkts_interop_js_api.h" +#include "napi/native_api.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" }; @@ -54,7 +60,7 @@ constexpr size_t BYTE_SIZE_8 = 1; if ((maxCapacityToWrite) < (nativeParcel)->GetWritePosition()) { \ ZLOGE(LOG_LABEL, "invalid write position, maxCapacityToWrite:%{public}zu, GetWritePosition:%{public}zu", \ (maxCapacityToWrite), (nativeParcel)->GetWritePosition()); \ - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); \ + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); \ } \ } while (0) @@ -74,7 +80,7 @@ constexpr size_t BYTE_SIZE_8 = 1; if ((nativeParcel)->GetDataSize() < (nativeParcel)->GetReadPosition()) { \ ZLOGE(LOG_LABEL, "invalid read position, GetDataSize:%{public}zu, GetReadPosition:%{public}zu", \ (nativeParcel)->GetDataSize(), (nativeParcel)->GetReadPosition()); \ - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); \ + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); \ } \ } while (0) @@ -83,7 +89,8 @@ constexpr size_t BYTE_SIZE_8 = 1; if ((nativeParcel)->GetDataSize() < (nativeParcel)->GetReadPosition()) { \ ZLOGE(LOG_LABEL, "invalid read position, GetDataSize:%{public}zu, GetReadPosition:%{public}zu", \ (nativeParcel)->GetDataSize(), (nativeParcel)->GetReadPosition()); \ - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, retVal); \ + RPC_TAIHE_ERROR_WITH_RETVAL( \ + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, retVal); \ } \ } while (0) @@ -151,7 +158,7 @@ void DeathRecipientImpl::OnRemoteDied(const OHOS::wptr &obj jsObjRef_->OnRemoteDied(); if (taihe::has_error()) { ZLOGE(LOG_LABEL, "call onRemoteDied failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CALL_JS_METHOD_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CALL_JS_METHOD_ERROR); } } @@ -215,7 +222,7 @@ RemoteProxyImpl::RemoteProxyImpl(uintptr_t nativePtr) { ZLOGE(LOG_LABEL, "only RemoteObject permitted"); auto jsBroker = taihe::make_holder(); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::ONLY_REMOTE_OBJECT_PERMITTED_ERROR, jsBroker); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_ONLY_REMOTE_OBJECT_PERMITTED_ERROR, jsBroker); } ::ohos::rpc::rpc::RequestResult RemoteProxyImpl::SendMessageRequestSync( @@ -271,6 +278,82 @@ int64_t RemoteProxyImpl::GetNativePtr() return reinterpret_cast(cachedObject_.GetRefPtr()); } +::ohos::rpc::rpc::RemoteProxy RemoteProxyImpl::RpcTransferStaticProxy(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* napiRemoteProxy = reinterpret_cast(nativePtr); + if (!napiRemoteProxy) { + ZLOGE(LOG_LABEL, "napiRemoteProxy is nullptr"); + return taihe::make_holder(0); + } + sptr remoteObject = napiRemoteProxy->object_; + auto jsref = taihe::make_holder(reinterpret_cast(remoteObject.GetRefPtr())); + jsref->AddJsObjWeakRef(jsref); + return jsref; +} + +uintptr_t RemoteProxyImpl::RpcTransferDynamicProxy(::ohos::rpc::rpc::RemoteProxy obj) +{ + ZLOGE(LOG_LABEL, "RpcTransferDynamicImpl start"); + int64_t impRawPtr = obj->GetNativePtr(); + auto* impl = reinterpret_cast(impRawPtr); + if (!impl || !impl->GetObjectProxy()) { + 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(); + // 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); @@ -311,17 +394,17 @@ AshmemImpl::AshmemImpl(OHOS::sptr ashmem) int32_t size = ashmem->GetAshmemSize(); if (fd < 0 || size == 0) { ZLOGE(LOG_LABEL, "fd < 0 or size == 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } int dupFd = dup(fd); if (dupFd < 0) { ZLOGE(LOG_LABEL, "fail to dup fd:%{public}d", dupFd); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::OS_DUP_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_OS_DUP_ERROR); } OHOS::sptr newAshmem(new (std::nothrow) OHOS::Ashmem(dupFd, size)); if (newAshmem == nullptr) { ZLOGE(LOG_LABEL, "fail to create new Ashmem"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::PARCEL_MEMORY_ALLOC_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_PARCEL_MEMORY_ALLOC_ERROR); } ashmem_ = newAshmem; } @@ -331,15 +414,118 @@ 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) +{ + ZLOGE(LOG_LABEL, "RpcTransferDynamicImpl start"); + int64_t impRawPtr = obj->GetNativePtr(); + auto* impl = reinterpret_cast(impRawPtr); + if (!impl || !impl->GetAshmem()) { + ZLOGE(LOG_LABEL, "impl or 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; + } + + int32_t fd = impl->GetAshmem()->GetAshmemFd(); + uint32_t size = (uint32_t)(impl->GetAshmem()->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; + status = napi_new_instance(jsenv, constructor, 0, nullptr, &jsAshmem); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "Failed to construct js Ashmem"); + return 0; + } + + sptr newAshmem(new 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) { + AshmemImpl *ashmemImpl = reinterpret_cast(data); + delete ashmemImpl; + }, + 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::OS_MMAP_ERROR); + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::RPC_ERR_OS_MMAP_ERROR); ashmem_->MapReadAndWriteAshmem(); } int32_t AshmemImpl::GetAshmemSize() { - CHECK_NATIVE_OBJECT_WITH_RETVAL(ashmem_, OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR, 0); + CHECK_NATIVE_OBJECT_WITH_RETVAL(ashmem_, OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR, 0); return ashmem_->GetAshmemSize(); } @@ -468,6 +654,95 @@ 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) +{ + ZLOGE(LOG_LABEL, "RpcTransferDynamicImpl start"); + int64_t impRawPtr = obj->GetNativePtr(); + auto* impl = reinterpret_cast(impRawPtr); + if (!impl || !impl->GetSptrCachedObject()) { + ZLOGE(LOG_LABEL, "impl or 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; + } + + auto descriptor = impl->GetDescriptor(); + + napi_value jsRemoteObject = nullptr; + status = napi_new_instance(jsenv, constructor, 0, nullptr, &jsRemoteObject); + if (status != napi_ok) { + ZLOGE(LOG_LABEL, "napi_new_instance failed"); + return 0; + } + std::u16string descStr16(descriptor.begin(), descriptor.end()); + const std::string descStr8 = Str16ToStr8(descStr16); + + auto holder = new (std::nothrow) NAPIRemoteObjectHolder(jsenv, OHOS::Str8ToStr16(descStr8), jsRemoteObject); + if(holder == nullptr) { + ZLOGE(LOG_LABEL, "new NAPIRemoteObjectHolder failed"); + } + + // connect native object to js thisVar + 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 ? @@ -534,19 +809,19 @@ void MessageSequenceImpl::Reclaim() void MessageSequenceImpl::WriteRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion const& object) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); if (object.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteObject) { auto &remoteStub = object.get_remoteObject_ref(); OHOS::sptr nativeStub = reinterpret_cast(remoteStub->GetNativePtr()); if (nativeStub == nullptr) { ZLOGE(LOG_LABEL, "reinterpret_cast to IRemoteObject failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } bool result = nativeParcel_->WriteRemoteObject(nativeStub); if (!result) { ZLOGE(LOG_LABEL, "write RemoteObject failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } return; } else if (object.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteProxy) { @@ -555,7 +830,7 @@ void MessageSequenceImpl::WriteRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion bool result = nativeParcel_->WriteRemoteObject(nativeProxy); if (!result) { ZLOGE(LOG_LABEL, "write RemoteProxy failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } return; } else { @@ -566,11 +841,12 @@ void MessageSequenceImpl::WriteRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion ::ohos::rpc::rpc::IRemoteObjectUnion MessageSequenceImpl::ReadRemoteObject() { - CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::ohos::rpc::rpc::IRemoteObjectUnion::make_errRet()); OHOS::sptr obj = nativeParcel_->ReadRemoteObject(); if (obj == nullptr) { - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::ohos::rpc::rpc::IRemoteObjectUnion::make_errRet()); } if (obj->IsProxyObject()) { @@ -592,23 +868,74 @@ void MessageSequenceImpl::WriteRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion void MessageSequenceImpl::WriteInterfaceToken(::taihe::string_view token) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); if (token.size() > MAX_BYTES_LENGTH) { ZLOGE(LOG_LABEL, "token is too large"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } std::u16string tokenStr(token.begin(), token.end()); bool result = nativeParcel_->WriteInterfaceToken(tokenStr); if (!result) { ZLOGE(LOG_LABEL, "write interface token failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + +int64_t unwrapRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion const& obj) +{ + if (obj.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteObject) { + auto &remoteStub = obj.get_remoteObject_ref(); + OHOS::sptr nativeStub = + reinterpret_cast(remoteStub->GetNativePtr()); + if (nativeStub == nullptr) { + ZLOGE(LOG_LABEL, "reinterpret_cast to IRemoteObject failed"); + return 0; + } + int64_t addr = reinterpret_cast(nativeStub.GetRefPtr()); + return addr; + } + if (obj.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteProxy) { + auto &remoteProxy = obj.get_remoteProxy_ref(); + auto nativeProxy = reinterpret_cast(remoteProxy->GetNativePtr()); + if (nativeProxy == nullptr) { + ZLOGE(LOG_LABEL, "reinterpret_cast to IPCObjectProxy failed"); + return 0; + } + int64_t addr = reinterpret_cast(nativeProxy); + return addr; + } + ZLOGE(LOG_LABEL, "unknown tag: %{public}d", obj.get_tag()); + return 0; +} + +::ohos::rpc::rpc::IRemoteObjectUnion wrapRemoteObject(int64_t nativePtr) +{ + if (reinterpret_cast(nativePtr) == nullptr) { + TH_THROW(std::runtime_error, "nativePtr is null"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::ohos::rpc::rpc::IRemoteObjectUnion::make_errRet()); + } + auto stub = reinterpret_cast(nativePtr); + if (stub->IsProxyObject()) { + ZLOGE(LOG_LABEL, "reinterpret_cast to ProxyObject"); + auto jsProxy = RemoteProxyImpl::CreateRemoteProxyFromNative(nativePtr); + return ::ohos::rpc::rpc::IRemoteObjectUnion::make_remoteProxy(jsProxy); + } + auto objstub = reinterpret_cast(nativePtr); + if (objstub->GetObjectType() == OHOS::IPCObjectStub::OBJECT_TYPE_JAVASCRIPT) { + ZLOGE(LOG_LABEL, "reinterpret_cast to ANIRemoteObject"); + auto aniStub = reinterpret_cast(nativePtr); + return ::ohos::rpc::rpc::IRemoteObjectUnion::make_remoteObject(aniStub->GetJsObject()); } + ZLOGE(LOG_LABEL, "reinterpret_cast to RemoteObject"); + auto jsStub = RemoteObjectImpl::CreateRemoteObjectFromNative(nativePtr); + return ::ohos::rpc::rpc::IRemoteObjectUnion::make_remoteObject(jsStub); } ::taihe::string MessageSequenceImpl::ReadInterfaceToken() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ""); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ""); std::u16string result = nativeParcel_->ReadInterfaceToken(); return OHOS::Str16ToStr8(result); } @@ -616,38 +943,38 @@ void MessageSequenceImpl::WriteInterfaceToken(::taihe::string_view token) int32_t MessageSequenceImpl::GetCapacity() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); int32_t result = nativeParcel_->GetDataCapacity(); return result; } void MessageSequenceImpl::SetCapacity(int32_t size) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); bool result = nativeParcel_->SetDataCapacity(size); if (!result) { ZLOGE(LOG_LABEL, "set data capacity failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } void MessageSequenceImpl::WriteNoException() { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); bool result = nativeParcel_->WriteInt32(0); if (!result) { ZLOGE(LOG_LABEL, "write int32 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } void MessageSequenceImpl::ReadException() { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); int32_t code = nativeParcel_->ReadInt32(); if (code == 0) { ZLOGE(LOG_LABEL, "ReadException failed, no exception"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); } std::u16string result = nativeParcel_->ReadString16(); taihe::set_business_error(code, OHOS::Str16ToStr8(result)); @@ -655,31 +982,31 @@ void MessageSequenceImpl::ReadException() void MessageSequenceImpl::WriteInt(int32_t val) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); bool result = nativeParcel_->WriteInt32(val); if (!result) { ZLOGE(LOG_LABEL, "write int32 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } void MessageSequenceImpl::WriteLong(int64_t val) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); bool result = nativeParcel_->WriteInt64(val); if (!result) { ZLOGE(LOG_LABEL, "write int64 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } void MessageSequenceImpl::WriteBoolean(bool val) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); bool result = nativeParcel_->WriteInt8(val); if (!result) { ZLOGE(LOG_LABEL, "write int8 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } @@ -687,38 +1014,38 @@ void MessageSequenceImpl::WriteString(::taihe::string_view val) { if (val.size() > MAX_BYTES_LENGTH) { ZLOGE(LOG_LABEL, "write string failed, string size:%{public}zu is too large", val.size()); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); CHECK_WRITE_CAPACITY(BYTE_SIZE_32 * val.size(), nativeParcel_, (nativeParcel_->GetMaxCapacity())); std::u16string str(val.begin(), val.end()); bool result = nativeParcel_->WriteString16(str); if (!result) { ZLOGE(LOG_LABEL, "write string16 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } void MessageSequenceImpl::WriteParcelable(::ohos::rpc::rpc::weak::Parcelable val) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); size_t pos = nativeParcel_->GetWritePosition(); nativeParcel_->WriteInt32(1); val->Marshalling(*jsObjRef_); if (taihe::has_error()) { ZLOGE(LOG_LABEL, "call marshalling failed"); nativeParcel_->RewindWrite(pos); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CALL_JS_METHOD_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CALL_JS_METHOD_ERROR); } } void MessageSequenceImpl::WriteByteArray(::taihe::array_view byteArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = byteArray.size(); if (arrayLength == 0) { ZLOGE(LOG_LABEL, "arrayLength is 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + (BYTE_SIZE_8 * arrayLength), nativeParcel_, nativeParcel_->GetMaxCapacity()); size_t pos = nativeParcel_->GetWritePosition(); @@ -729,18 +1056,18 @@ void MessageSequenceImpl::WriteByteArray(::taihe::array_view byteArray) if (!result) { nativeParcel_->RewindWrite(pos); ZLOGE(LOG_LABEL, "write int8 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } } void MessageSequenceImpl::WriteIntArray(::taihe::array_view intArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = intArray.size(); if (arrayLength == 0) { ZLOGE(LOG_LABEL, "arrayLength is 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } CHECK_WRITE_CAPACITY(BYTE_SIZE_32 * (arrayLength + 1), nativeParcel_, nativeParcel_->GetMaxCapacity()); size_t pos = nativeParcel_->GetWritePosition(); @@ -751,18 +1078,18 @@ void MessageSequenceImpl::WriteIntArray(::taihe::array_view intArray) if (!result) { nativeParcel_->RewindWrite(pos); ZLOGE(LOG_LABEL, "write int32 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } } void MessageSequenceImpl::WriteDoubleArray(::taihe::array_view doubleArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = doubleArray.size(); if (arrayLength == 0) { ZLOGE(LOG_LABEL, "arrayLength is 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + sizeof(double) * arrayLength, nativeParcel_, nativeParcel_->GetMaxCapacity()); size_t pos = nativeParcel_->GetWritePosition(); @@ -773,18 +1100,18 @@ void MessageSequenceImpl::WriteDoubleArray(::taihe::array_view doubleArr if (!result) { nativeParcel_->RewindWrite(pos); ZLOGE(LOG_LABEL, "write double failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } } void MessageSequenceImpl::WriteBooleanArray(::taihe::array_view booleanArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = booleanArray.size(); if (arrayLength == 0) { ZLOGE(LOG_LABEL, "arrayLength is 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + (BYTE_SIZE_8 * arrayLength), nativeParcel_, nativeParcel_->GetMaxCapacity()); size_t pos = nativeParcel_->GetWritePosition(); @@ -795,18 +1122,18 @@ void MessageSequenceImpl::WriteBooleanArray(::taihe::array_view booleanArr if (!result) { nativeParcel_->RewindWrite(pos); ZLOGE(LOG_LABEL, "write int8 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } } void MessageSequenceImpl::WriteStringArray(::taihe::array_view<::taihe::string> stringArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = stringArray.size(); if (arrayLength == 0) { ZLOGE(LOG_LABEL, "arrayLength is 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } size_t pos = nativeParcel_->GetWritePosition(); nativeParcel_->WriteUint32(arrayLength); @@ -815,7 +1142,7 @@ void MessageSequenceImpl::WriteStringArray(::taihe::array_view<::taihe::string> if (stringArray[i].size() > MAX_BYTES_LENGTH) { ZLOGE(LOG_LABEL, "string length is too long, index:%{public}zu, size:%{public}zu", i, stringArray[i].size()); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } REWIND_IF_WRITE_CHECK_FAIL(BYTE_SIZE_32 + (BYTE_SIZE_16 * stringArray[i].size()), pos, nativeParcel_, (nativeParcel_->GetMaxCapacity())); @@ -824,23 +1151,23 @@ void MessageSequenceImpl::WriteStringArray(::taihe::array_view<::taihe::string> if (!result) { nativeParcel_->RewindWrite(pos); ZLOGE(LOG_LABEL, "write string16 failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } } void MessageSequenceImpl::WriteParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = parcelableArray.size(); if (arrayLength == 0) { ZLOGE(LOG_LABEL, "arrayLength is 0"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } size_t pos = nativeParcel_->GetWritePosition(); if (!(nativeParcel_->WriteUint32(arrayLength))) { ZLOGE(LOG_LABEL, "write array length failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } for (size_t i = 0; i < arrayLength; i++) { nativeParcel_->WriteInt32(1); @@ -848,7 +1175,7 @@ void MessageSequenceImpl::WriteParcelableArray(::taihe::array_view<::ohos::rpc:: if (taihe::has_error()) { nativeParcel_->RewindWrite(pos); ZLOGE(LOG_LABEL, "call marshalling failed, element index:%{public}zu", i); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CALL_JS_METHOD_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CALL_JS_METHOD_ERROR); } } } @@ -856,54 +1183,54 @@ void MessageSequenceImpl::WriteParcelableArray(::taihe::array_view<::ohos::rpc:: int32_t MessageSequenceImpl::ReadInt() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); return nativeParcel_->ReadInt32(); } int64_t MessageSequenceImpl::ReadLong() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); return nativeParcel_->ReadInt64(); } bool MessageSequenceImpl::ReadBoolean() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, false); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, false); return static_cast(nativeParcel_->ReadInt8()); } ::taihe::string MessageSequenceImpl::ReadString() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ""); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ""); return OHOS::Str16ToStr8(nativeParcel_->ReadString16()); } void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable dataIn) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); int32_t flags = nativeParcel_->ReadInt32(); if (flags != 1) { ZLOGE(LOG_LABEL, "read parcelable failed, flags:%{public}d", flags); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); } dataIn->Unmarshalling(*jsObjRef_); if (taihe::has_error()) { ZLOGE(LOG_LABEL, "call marshalling failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CALL_JS_METHOD_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CALL_JS_METHOD_ERROR); } } ::taihe::array MessageSequenceImpl::ReadIntArrayImpl() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); int32_t arrayLength = nativeParcel_->ReadInt32(); if (arrayLength <= 0) { ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR, (::taihe::array(nullptr, 0))); } CHECK_READ_LENGTH_RETVAL(static_cast(arrayLength), BYTE_SIZE_32, @@ -912,7 +1239,7 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data for (uint32_t i = 0; i < static_cast(arrayLength); i++) { if (!nativeParcel_->ReadInt32(res[i])) { ZLOGE(LOG_LABEL, "read int32 failed"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, (::taihe::array(nullptr, 0))); } } @@ -922,11 +1249,11 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data ::taihe::array MessageSequenceImpl::ReadDoubleArrayImpl() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); int32_t arrayLength = nativeParcel_->ReadInt32(); if (arrayLength <= 0) { ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR, (::taihe::array(nullptr, 0))); } CHECK_READ_LENGTH_RETVAL(static_cast(arrayLength), sizeof(double), @@ -935,7 +1262,7 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data for (uint32_t i = 0; i < static_cast(arrayLength); i++) { if (!nativeParcel_->ReadDouble(res[i])) { ZLOGE(LOG_LABEL, "read double failed"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, (::taihe::array(nullptr, 0))); } } @@ -945,11 +1272,11 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data ::taihe::array MessageSequenceImpl::ReadBooleanArrayImpl() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); int32_t arrayLength = nativeParcel_->ReadInt32(); if (arrayLength <= 0) { ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR, (::taihe::array(nullptr, 0))); } CHECK_READ_LENGTH_RETVAL(static_cast(arrayLength), BYTE_SIZE_8, @@ -959,7 +1286,7 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data for (uint32_t i = 0; i < static_cast(arrayLength); i++) { if (!nativeParcel_->ReadInt8(val)) { ZLOGE(LOG_LABEL, "read bool failed"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, (::taihe::array(nullptr, 0))); } res[i] = (val != 0) ? true : false; @@ -969,12 +1296,13 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data ::taihe::array<::taihe::string> MessageSequenceImpl::ReadStringArrayImpl() { - CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array<::taihe::string>(nullptr, 0)); int32_t arrayLength = nativeParcel_->ReadInt32(); if (arrayLength <= 0) { ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR, (::taihe::array<::taihe::string>(nullptr, 0))); } std::vector res; @@ -985,7 +1313,7 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data } if (!nativeParcel_->ReadString16(val)) { ZLOGE(LOG_LABEL, "read string16 failed"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, (::taihe::array<::taihe::string>(nullptr, 0))); } res.push_back(OHOS::Str16ToStr8(val)); @@ -995,67 +1323,67 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data void MessageSequenceImpl::ReadParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); int32_t arrayLength = nativeParcel_->ReadInt32(); if (arrayLength <= 0) { ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } int32_t flags; for (uint32_t i = 0; i < static_cast(arrayLength); i++) { flags = nativeParcel_->ReadInt32(); if (flags != 1) { ZLOGE(LOG_LABEL, "read parcelable failed, flags:%{public}d", flags); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); } parcelableArray[i]->Unmarshalling(*jsObjRef_); if (taihe::has_error()) { ZLOGE(LOG_LABEL, "call unmarshalling failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CALL_JS_METHOD_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CALL_JS_METHOD_ERROR); } } } void MessageSequenceImpl::WriteFileDescriptor(int32_t fd) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); bool result = nativeParcel_->WriteFileDescriptor(fd); if (!result) { ZLOGE(LOG_LABEL, "write file descriptor failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } int32_t MessageSequenceImpl::ReadFileDescriptor() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); int32_t result = nativeParcel_->ReadFileDescriptor(); if (result < 0) { ZLOGE(LOG_LABEL, "read file descriptor failed"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); } return result; } void MessageSequenceImpl::WriteAshmem(::ohos::rpc::rpc::weak::Ashmem ashmem) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); if (!nativeParcel_->WriteAshmem(reinterpret_cast(ashmem->GetNativePtr()))) { ZLOGE(LOG_LABEL, "write ashmem failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } ::ohos::rpc::rpc::Ashmem MessageSequenceImpl::ReadAshmem() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, (taihe::make_holder())); OHOS::sptr nativeAshmem = nativeParcel_->ReadAshmem(); if (nativeAshmem == nullptr) { ZLOGE(LOG_LABEL, "nativeAshmem is null"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, (taihe::make_holder())); } return taihe::make_holder(nativeAshmem); @@ -1063,26 +1391,26 @@ void MessageSequenceImpl::WriteAshmem(::ohos::rpc::rpc::weak::Ashmem ashmem) void MessageSequenceImpl::WriteRawDataBuffer(::taihe::array_view rawData, int32_t size) { - CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); if (!nativeParcel_->WriteRawData(static_cast(rawData.data()), size)) { ZLOGE(LOG_LABEL, "write raw data failed"); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); } } ::taihe::array MessageSequenceImpl::ReadRawDataBuffer(int32_t size) { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, - OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); + OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); if (size <= 0) { ZLOGE(LOG_LABEL, "invalid param size:%{public}d", size); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR, (::taihe::array(nullptr, 0))); } const void *rawData = nativeParcel_->ReadRawData(size); if (rawData == nullptr) { ZLOGE(LOG_LABEL, "rawData is null"); - RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::RPC_ERR_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); } const uint8_t* bytePtr = static_cast(rawData); @@ -1113,7 +1441,7 @@ void MessageSequenceImpl::CloseFileDescriptor(int32_t fd) { if (fd < 0) { ZLOGE(LOG_LABEL, "invalid fd:%{public}d", fd); - RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::CHECK_PARAM_ERROR); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::RPC_ERR_CHECK_PARAM_ERROR); } close(fd); } @@ -1150,12 +1478,93 @@ 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) +{ + ZLOGE(LOG_LABEL, "RpcTransferDynamicImpl start"); + int64_t impRawPtr = obj->GetNativePtr(); + auto* impl = reinterpret_cast(impRawPtr); + if (!impl || !impl->GetMessageOption()) { + ZLOGE(LOG_LABEL, "impl or parcel 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; + } + auto messageOptionTemp = impl ->GetMessageOption(); + auto messageOption = new (std::nothrow) MessageOption(messageOptionTemp->GetFlags(), + messageOptionTemp->GetWaitTime()); + 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); } -::ohos::rpc::rpc::MessageOption MessageOptionImpl::CreateMessageOption_WithTwoParam(int32_t syncFlags, int32_t waitTime) +::ohos::rpc::rpc::MessageOption MessageOptionImpl::CreateMessageOption_WithTwoParam(int32_t syncFlags, + int32_t waitTime) { return taihe::make_holder(syncFlags, waitTime); } @@ -1203,16 +1612,26 @@ 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_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); TH_EXPORT_CPP_API_GetContextObject(OHOS::IPCSkeletonImpl::GetContextObject); +TH_EXPORT_CPP_API_unwrapRemoteObject(OHOS::unwrapRemoteObject); +TH_EXPORT_CPP_API_wrapRemoteObject(OHOS::wrapRemoteObject); // NOLINTEND \ No newline at end of file diff --git a/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp b/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp new file mode 100644 index 00000000..2b2f8a59 --- /dev/null +++ b/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2025 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. + */ + +#include "remote_object_taihe_ani.h" + +#include +#include "taihe_remote_object.h" + +// This file is for legacy ANI backward compatibility + +namespace OHOS { +using namespace arkts; + +ani_object RemoteObjectTaiheAni::ANI_ohos_rpc_CreateJsRemoteObject(ani_env* env, OHOS::sptr remoteObject) +{ + std::unique_ptr remoteobj = std::make_unique(); + remoteobj->nativeRemoteObject_ = remoteObject; + + ani_signature::Namespace ns = ani_signature::Builder::BuildNamespace("@ohos.rpc.rpc"); + ani_namespace imageNamespace; + if (env->FindNamespace(ns.Descriptor().c_str(), &imageNamespace) != ANI_OK) { + return nullptr; + } + ani_function createFunc; + if (env->Namespace_FindFunction(imageNamespace, "wrapRemoteObject", nullptr, &createFunc) != ANI_OK) { + return nullptr; + } + ani_ref Obj; + if (env->Function_Call_Ref(createFunc, &Obj, reinterpret_cast(remoteobj.get())) == ANI_OK) { + remoteobj.release(); + } else { + return nullptr; + } + + return reinterpret_cast(Obj); +} + +OHOS::sptr RemoteObjectTaiheAni::AniGetNativeRemoteObject(ani_env* env, ani_object obj) +{ + ani_signature::Type cls = ani_signature::Builder::BuildClass("@ohos.rpc.rpc.RemoteObject"); + ani_class remoteObjectCls; + if (env->FindClass(cls.Descriptor().c_str(), &remoteObjectCls) != ANI_OK) { + return nullptr; + } + ani_signature::SignatureBuilder sb{}; + ani_method getMethod; + sb.SetReturnLong(); + if (env->Class_FindMethod(remoteObjectCls, "unwrapRemoteObject", + sb.BuildSignatureDescriptor().c_str(), &getMethod) != ANI_OK) { + return nullptr; + } + ani_long implPtr; + if (env->Object_CallMethod_Long(obj, getMethod, &implPtr) != ANI_OK) { + return nullptr; + } + + RemoteObjectImpl* remoteObjectImpl = reinterpret_cast(implPtr); + if (remoteObjectImpl == nullptr) { + return nullptr; + } + OHOS::sptr nativeStub = + reinterpret_cast(remoteObjectImpl->GetNativePtr()); + return nativeStub; +} +} // namespace OHOS \ No newline at end of file -- Gitee