diff --git a/ipc/native/src/core/source/ipc_object_proxy.cpp b/ipc/native/src/core/source/ipc_object_proxy.cpp index 93800835abc2c3b7292817a9372899ddb36cb261..98fc77392d4fc0cf84e9b1aea84a4bd8a69469ee 100644 --- a/ipc/native/src/core/source/ipc_object_proxy.cpp +++ b/ipc/native/src/core/source/ipc_object_proxy.cpp @@ -530,6 +530,9 @@ int IPCObjectProxy::GetProtoInfo() switch (reply.ReadUint32()) { case IRemoteObject::IF_PROT_BINDER: { + remoteDescriptor_ = reply.ReadString16(); + ZLOGD(LABEL, "it is normal binder, handle:%{public}u desc:%{public}s", + handle_, Str16ToStr8(remoteDescriptor_).c_str()); break; } case IRemoteObject::IF_PROT_DATABUS: { diff --git a/ipc/native/src/core/source/ipc_object_stub.cpp b/ipc/native/src/core/source/ipc_object_stub.cpp index 7dc9ce4062752b68825a7a7cc320ce2fecc4c6a7..9cf81aa47fd94f570678cf17b86313fceff62d7e 100644 --- a/ipc/native/src/core/source/ipc_object_stub.cpp +++ b/ipc/native/src/core/source/ipc_object_stub.cpp @@ -371,8 +371,8 @@ int IPCObjectStub::GetObjectType() const int32_t IPCObjectStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { int result = ERR_NONE; - ZLOGD(LABEL, "IPCObjectStub::ProcessProto called, type = 0, normal stub object"); - if (!reply.WriteUint32(IRemoteObject::IF_PROT_BINDER)) { + ZLOGD(LABEL, "IPCObjectStub::ProcessProto called, type = 0, normal stub object,descriptor_=%{public}s",Str16ToStr8(descriptor_).c_str()); + if (!reply.WriteUint32(IRemoteObject::IF_PROT_BINDER) || !reply.WriteString16(descriptor_)) { ZLOGE(LABEL, "write to parcel fail"); result = IPC_STUB_WRITE_PARCEL_ERR; } 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 25e7e6f01eda9c588fbe0e3626f6f41c2d04c20a..ab544ddcb19b4e4520486c1eb27cbf5e83f91a87 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 @@ -26,13 +26,13 @@ namespace OHOS { class NAPIRemoteObjectHolder : public RefBase { public: - explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor); + explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar); ~NAPIRemoteObjectHolder(); - sptr Get(napi_value object); + sptr Get(); void Set(sptr object); void attachLocalInterface(napi_value localInterface, std::string &descriptor); napi_value queryLocalInterface(std::string &descriptor); - /*void Lock() + void Lock() { mutex_.lock(); }; @@ -58,7 +58,7 @@ public: std::u16string GetDescriptor() { return descriptor_; - };*/ + }; private: std::mutex mutex_; @@ -67,7 +67,8 @@ private: sptr sptrCachedObject_; wptr wptrCachedObject_; napi_ref localInterfaceRef_; - //int32_t attachCount_; + int32_t attachCount_; + napi_ref jsObjectRef_ = nullptr; }; } // namespace OHOS #endif // NAPI_REMOTE_OBJECT_HOLDER_H \ No newline at end of file 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 b9a1f74f72206af8efc04d1a9035cb92f1c24072..f1ceae52f58e84f04f6b253b79a9c7421cdef015 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_value thisVar, const std::u16string &descriptor); + NAPIRemoteObject(napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor); ~NAPIRemoteObject() override; @@ -38,6 +38,7 @@ public: napi_ref GetJsObjectRef() const; private: + std::u16string desc_; napi_env env_ = nullptr; napi_value thisVar_ = nullptr; static napi_value ThenCallback(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 839e99205b17f2893530adead0d89d13d79245ad..dea1f6c89b76a738aebee6e254327009222b0345 100644 --- a/ipc/native/src/napi_common/source/napi_remote_object.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_object.cpp @@ -31,7 +31,7 @@ #include "napi_rpc_error.h" #include "napi/native_api.h" #include "napi/native_node_api.h" -//#include "native_engine/native_value.h" +#include "native_engine/native_value.h" namespace OHOS { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_remoteObject" }; @@ -45,7 +45,7 @@ static const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. static std::atomic bytraceId = 1000; static NapiError napiErr; -/*template +template inline T *ConvertNativeValueTo(NativeValue *value) { return (value != nullptr) ? static_cast(value->GetInterface(T::INTERFACE_ID)) : nullptr; @@ -60,6 +60,7 @@ static void RemoteObjectHolderFinalizeCb(napi_env env, void *data, void *hint) } holder->Lock(); int32_t curAttachCount = holder->DecAttachCount(); + ZLOGW(LOG_LABEL, "NAPIRemoteObjectHolder destructed by js callback, curAttachCount:%{public}d", curAttachCount); if (curAttachCount == 0) { delete holder; } @@ -111,7 +112,7 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void holder->IncAttachCount(); holder->Unlock(); return reinterpret_cast(jsRemoteObject); -}*/ +} napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) { @@ -134,35 +135,28 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) napi_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength); NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong"); std::string descriptor = stringValue; - auto holder = new NAPIRemoteObjectHolder(env, Str8ToStr16(descriptor)); - /*auto nativeObj = ConvertNativeValueTo(reinterpret_cast(thisVar)); + auto holder = new NAPIRemoteObjectHolder(env, Str8ToStr16(descriptor), thisVar); + auto nativeObj = ConvertNativeValueTo(reinterpret_cast(thisVar)); if (nativeObj == nullptr) { ZLOGE(LOG_LABEL, "Failed to get RemoteObject native object"); delete holder; return nullptr; } - nativeObj->ConvertToNativeBindingObject(env, RemoteObjectDetachCb, RemoteObjectAttachCb, holder, nullptr);*/ + nativeObj->ConvertToNativeBindingObject(env, RemoteObjectDetachCb, RemoteObjectAttachCb, holder, nullptr); // connect native object to js thisVar - napi_status status = napi_wrap( - env, thisVar, holder, - [](napi_env env, void *data, void *hint) { - ZLOGI(LOG_LABEL, "NAPIRemoteObjectHolder destructed by js callback"); - delete (reinterpret_cast(data)); - }, - nullptr, nullptr); - //napi_status status = napi_wrap(env, thisVar, holder, RemoteObjectHolderFinalizeCb, nullptr, nullptr); + napi_status status = napi_wrap(env, thisVar, holder, RemoteObjectHolderFinalizeCb, nullptr, nullptr); NAPI_ASSERT(env, status == napi_ok, "wrap js RemoteObject and native holder failed"); - /*if (NAPI_ohos_rpc_getNativeRemoteObject(env, thisVar) == nullptr) { - ZLOGE(LOG_LABEL, "RemoteObject_JS_Constructor create native object failed"); - return nullptr; - }*/ return thisVar; } -NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_value thisVar, const std::u16string &descriptor) +NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor) : IPCObjectStub(descriptor) { env_ = env; + desc_ = descriptor; + napi_value thisVar = nullptr; + napi_get_reference_value(env, jsObjectRef, &thisVar); + NAPI_ASSERT_RETURN_VOID(env, thisVar != nullptr, "failed to get value from js RemoteObject ref"); thisVar_ = thisVar; napi_create_reference(env, thisVar_, 1, &thisVarRef_); NAPI_ASSERT_RETURN_VOID(env, thisVarRef_ != nullptr, "failed to create ref to js RemoteObject"); @@ -170,7 +164,7 @@ NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_value thisVar, const std:: NAPIRemoteObject::~NAPIRemoteObject() { - ZLOGI(LOG_LABEL, "NAPIRemoteObject Destructor"); + ZLOGI(LOG_LABEL, "NAPIRemoteObject destroyed, desc:%{public}s", Str16ToStr8(desc_).c_str()); if (thisVarRef_ != nullptr) { napi_status status = napi_delete_reference(env_, thisVarRef_); NAPI_ASSERT_RETURN_VOID(env_, status == napi_ok, "failed to delete ref to js RemoteObject"); @@ -603,9 +597,8 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptrCheckObjectLegality()) { IPCObjectStub *tmp = static_cast(target.GetRefPtr()); uint32_t objectType = tmp->GetObjectType(); - ZLOGI(LOG_LABEL, "[mgttest]object type:%{public}d", objectType); + ZLOGI(LOG_LABEL, "object type:%{public}d", objectType); if (objectType == IPCObjectStub::OBJECT_TYPE_JAVASCRIPT || objectType == IPCObjectStub::OBJECT_TYPE_NATIVE) { - //ZLOGI(LOG_LABEL, "napi create js remote object"); sptr object = static_cast(target.GetRefPtr()); // retrieve js remote object constructor napi_value global = nullptr; @@ -630,7 +623,6 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptrSet(object); return jsRemoteObject; } @@ -668,7 +660,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(object) : nullptr; + return holder != nullptr ? holder->Get() : 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 327b82b4a92575fc12b646cbba9f89f0bb7041f2..c3bd720c23aed866f227e3de24089e9b461e7017 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 @@ -18,10 +18,17 @@ #include namespace OHOS { -NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor) - : env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr) -// : env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr), attachCount_(1) -{} +NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar) +{ + ZLOGE(LOG_LABEL, "[memtest]NAPIRemoteObjectHolder created"); + env_ = env; + descriptor_ = descriptor; + sptrCachedObject_ = nullptr; + wptrCachedObject_ = nullptr; + localInterfaceRef_ = nullptr; + attachCount_ = 1; + napi_create_reference(env, thisVar, 0, &jsObjectRef_); +} NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder() { @@ -33,7 +40,7 @@ NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder() } } -sptr NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject) +sptr NAPIRemoteObjectHolder::Get() { std::lock_guard lockGuard(mutex_); // grab an strong reference to the object, @@ -43,7 +50,7 @@ sptr NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject) } sptr tmp = wptrCachedObject_.promote(); if (tmp == nullptr) { - tmp = new NAPIRemoteObject(env_, jsRemoteObject, descriptor_); + tmp = new NAPIRemoteObject(env_, jsObjectRef_, descriptor_); wptrCachedObject_ = tmp; } return tmp;