fix ablity acts fails

Signed-off-by: 18392170496 <magentang4@huawei.com>
This commit is contained in:
18392170496 2023-07-02 16:51:49 +08:00
parent 4857bbfa4a
commit 6038d383ef
4 changed files with 19 additions and 7 deletions

View File

@ -28,7 +28,7 @@ class NAPIRemoteObjectHolder : public RefBase {
public:
explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar);
~NAPIRemoteObjectHolder();
sptr<NAPIRemoteObject> Get();
sptr<NAPIRemoteObject> Get(napi_env envNew);
void Set(sptr<NAPIRemoteObject> object);
void attachLocalInterface(napi_value localInterface, std::string &descriptor);
napi_value queryLocalInterface(std::string &descriptor);

View File

@ -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);

View File

@ -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<IRemoteObject> 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;

View File

@ -42,7 +42,7 @@ NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
}
}
sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get()
sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_env envNew)
{
std::lock_guard<std::mutex> lockGuard(mutex_);
// grab an strong reference to the object,
@ -52,9 +52,10 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get()
}
sptr<NAPIRemoteObject> 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;
}