!727 fix ablity acts fails

Merge pull request !727 from 马根堂/master
This commit is contained in:
openharmony_ci 2023-07-02 12:35:38 +00:00 committed by Gitee
commit 85b1926121
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 20 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,11 @@ 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;
@ -165,7 +167,7 @@ NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_ref jsObjectRef, const std
NAPIRemoteObject::~NAPIRemoteObject()
{
ZLOGI(LOG_LABEL, "NAPIRemoteObject destroyed, desc:%{public}s", Str16ToStr8(desc_).c_str());
if (thisVarRef_ != nullptr) {
if (thisVarRef_ != nullptr && envNew_ == env_) {
napi_status status = napi_delete_reference(env_, thisVarRef_);
NAPI_ASSERT_RETURN_VOID(env_, status == napi_ok, "failed to delete ref to js RemoteObject");
thisVarRef_ = nullptr;
@ -187,6 +189,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 +669,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;
}