From 6038d383ef156b8be8ed114127faa53aa1a49693 Mon Sep 17 00:00:00 2001 From: 18392170496 Date: Sun, 2 Jul 2023 16:51:49 +0800 Subject: [PATCH] fix ablity acts fails Signed-off-by: 18392170496 --- .../include/napi_remote_object_holder.h | 2 +- .../include/napi_remote_object_internal.h | 5 ++++- .../src/napi_common/source/napi_remote_object.cpp | 14 +++++++++++--- .../source/napi_remote_object_holder.cpp | 5 +++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ipc/native/src/napi_common/include/napi_remote_object_holder.h b/ipc/native/src/napi_common/include/napi_remote_object_holder.h index ab544dd..6ccb1cd 100644 --- a/ipc/native/src/napi_common/include/napi_remote_object_holder.h +++ b/ipc/native/src/napi_common/include/napi_remote_object_holder.h @@ -28,7 +28,7 @@ class NAPIRemoteObjectHolder : public RefBase { public: explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar); ~NAPIRemoteObjectHolder(); - sptr Get(); + sptr Get(napi_env envNew); void Set(sptr object); void attachLocalInterface(napi_value localInterface, std::string &descriptor); napi_value queryLocalInterface(std::string &descriptor); diff --git a/ipc/native/src/napi_common/include/napi_remote_object_internal.h b/ipc/native/src/napi_common/include/napi_remote_object_internal.h index f1ceae5..0663851 100644 --- a/ipc/native/src/napi_common/include/napi_remote_object_internal.h +++ b/ipc/native/src/napi_common/include/napi_remote_object_internal.h @@ -26,7 +26,7 @@ namespace OHOS { class NAPIRemoteObject : public IPCObjectStub { public: - NAPIRemoteObject(napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor); + NAPIRemoteObject(napi_env envNew, napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor); ~NAPIRemoteObject() override; @@ -37,9 +37,12 @@ public: int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; napi_ref GetJsObjectRef() const; + + void SetNewEnv(napi_env envNew); private: std::u16string desc_; napi_env env_ = nullptr; + napi_env envNew_ = nullptr; napi_value thisVar_ = nullptr; static napi_value ThenCallback(napi_env env, napi_callback_info info); static napi_value CatchCallback(napi_env env, napi_callback_info info); 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 066b179..4dcea77 100644 --- a/ipc/native/src/napi_common/source/napi_remote_object.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_object.cpp @@ -149,9 +149,10 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) return thisVar; } -NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor) +NAPIRemoteObject::NAPIRemoteObject(napi_env envNew, napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor) : IPCObjectStub(descriptor) { + envNew_ = envNew; env_ = env; desc_ = descriptor; napi_value thisVar = nullptr; @@ -166,7 +167,7 @@ NAPIRemoteObject::~NAPIRemoteObject() { ZLOGI(LOG_LABEL, "NAPIRemoteObject destroyed, desc:%{public}s", Str16ToStr8(desc_).c_str()); if (thisVarRef_ != nullptr) { - napi_status status = napi_delete_reference(env_, thisVarRef_); + napi_status status = napi_delete_reference(envNew_, thisVarRef_); NAPI_ASSERT_RETURN_VOID(env_, status == napi_ok, "failed to delete ref to js RemoteObject"); thisVarRef_ = nullptr; } @@ -187,6 +188,13 @@ napi_ref NAPIRemoteObject::GetJsObjectRef() const return thisVarRef_; } +void NAPIRemoteObject::SetNewEnv(napi_env env) +{ + if (envNew_ != env) { + ZLOGI(LOG_LABEL, "env updated"); + } +} + void NAPI_RemoteObject_getCallingInfo(CallingInfo &newCallingInfoParam) { newCallingInfoParam.callingPid = IPCSkeleton::GetCallingPid(); @@ -660,7 +668,7 @@ sptr NAPI_ohos_rpc_getNativeRemoteObject(napi_env env, napi_value NAPIRemoteObjectHolder *holder = nullptr; napi_unwrap(env, object, (void **)&holder); NAPI_ASSERT(env, holder != nullptr, "failed to get napi remote object holder"); - return holder != nullptr ? holder->Get() : nullptr; + return holder != nullptr ? holder->Get(env) : nullptr; } napi_value proxyConstructor = nullptr; diff --git a/ipc/native/src/napi_common/source/napi_remote_object_holder.cpp b/ipc/native/src/napi_common/source/napi_remote_object_holder.cpp index 3eb79ba..96d485e 100644 --- a/ipc/native/src/napi_common/source/napi_remote_object_holder.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_object_holder.cpp @@ -42,7 +42,7 @@ NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder() } } -sptr NAPIRemoteObjectHolder::Get() +sptr NAPIRemoteObjectHolder::Get(napi_env envNew) { std::lock_guard lockGuard(mutex_); // grab an strong reference to the object, @@ -52,9 +52,10 @@ sptr NAPIRemoteObjectHolder::Get() } sptr tmp = wptrCachedObject_.promote(); if (tmp == nullptr) { - tmp = new NAPIRemoteObject(env_, jsObjectRef_, descriptor_); + tmp = new NAPIRemoteObject(envNew, env_, jsObjectRef_, descriptor_); wptrCachedObject_ = tmp; } + tmp->SetNewEnv(envNew); return tmp; } -- GitLab