diff --git a/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets b/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets index 28303be34264621426faa24cc50363092b194931..9d7ac45333e5a6402b214fee2905ce9e51d7a289 100644 --- a/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets +++ b/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets @@ -109,5 +109,7 @@ export namespace rpc { export class RemoteProxy extends IRemoteObject { native getDescriptor(): string; + + private nativePtr: long = 0; } } diff --git a/ipc/native/src/ani/rpc/include/ani_remote_object.h b/ipc/native/src/ani/rpc/include/ani_remote_object.h index 2cc153f852d77ecde5688b9e3e327b857dd88338..b5a66b57e0ba8a52c0b9b214457aad2dcddc18ea 100644 --- a/ipc/native/src/ani/rpc/include/ani_remote_object.h +++ b/ipc/native/src/ani/rpc/include/ani_remote_object.h @@ -24,5 +24,6 @@ namespace OHOS { } OHOS::sptr AniGetNativeRemoteObject(ani_env *env, ani_object obj); +ani_object ANI_ohos_rpc_CreateJsRemoteObject(ani_env *env, OHOS::sptr remoteObject); #endif //ANI_REMOTE_OBJECT_H \ No newline at end of file diff --git a/ipc/native/src/ani/rpc/include/ani_utils.h b/ipc/native/src/ani/rpc/include/ani_utils.h index 72f605a6d35905263fc2e10fac4782f90fd48ec5..d9c1e4144b8c287f6d8d5691e19ea0283a337376 100644 --- a/ipc/native/src/ani/rpc/include/ani_utils.h +++ b/ipc/native/src/ani/rpc/include/ani_utils.h @@ -446,4 +446,33 @@ std::optional OptionalAccessor::Convert() return content; } +class NativeObject { +public: + virtual ~NativeObject() = default; +}; + +template +class SharedPtrHolder : public NativeObject { +public: + SharedPtrHolder(const OHOS::sptr &sptr) : sptr_(sptr) + { + } + + OHOS::sptr Get() + { + return sptr_; + } + + OHOS::sptr GetOrDefault() + { + if (!sptr_) { + sptr_ = std::make_shared(); + } + return sptr_; + } + +private: + OHOS::sptr sptr_; +}; + #endif diff --git a/ipc/native/src/ani/rpc/src/rpc_ani.cpp b/ipc/native/src/ani/rpc/src/rpc_ani.cpp index 46e42bcefbd3b0ff99ab578c03197eae2b4d9ed4..de13b6fb34305edddd4d09edfda85ff42b8b7be7 100644 --- a/ipc/native/src/ani/rpc/src/rpc_ani.cpp +++ b/ipc/native/src/ani/rpc/src/rpc_ani.cpp @@ -259,6 +259,32 @@ sptr AniGetNativeRemoteObject(ani_env *env, ani_object obj) return holder->Get(); } +ani_object CreateJsProxyRemoteObject(ani_env *env, const sptr target) +{ + auto holder = new SharedPtrHolder(target); + if (holder == nullptr) { + ZLOGE(LOG_LABEL, "[ANI] SharedPtrHolder constructor failed"); + return nullptr; + } + ani_object jsRemoteProxy = AniObjectUtils::Create(env, "L@ohos/rpc/rpc;", "LRemoteProxy;"); + if (jsRemoteProxy == nullptr) { + ZLOGE(LOG_LABEL, "[ANI] Create jsRemoteProxy failed"); + return nullptr; + } + AniObjectUtils::Wrap>(env, jsRemoteProxy, holder); + return jsRemoteProxy; +} + +ani_object ANI_ohos_rpc_CreateJsRemoteObject(ani_env *env, sptr remoteObject) +{ + if (remoteObject == nullptr) { + ZLOGE(LOG_LABEL, "[ANI] RemoteObject is nullptr"); + return nullptr; + } + + return CreateJsProxyRemoteObject(env, remoteObject); +} + static ani_string MessageSequenceReadString([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) { ZLOGI(LOG_LABEL, "[ANI] enter MessageSequenceReadString func");