• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_remote_proxy_holder.h"
17 
18 #include "ipc_debug.h"
19 #include "log_tags.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "native_engine/native_value.h"
23 
24 namespace OHOS {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_NAPI, "NapiRemoteProxyHolder" };
26 
NAPIDeathRecipient(napi_env env,napi_value jsDeathRecipient)27 NAPIDeathRecipient::NAPIDeathRecipient(napi_env env, napi_value jsDeathRecipient)
28 {
29     env_ = env;
30     napi_status status = napi_create_reference(env_, jsDeathRecipient, 1, &deathRecipientRef_);
31     NAPI_ASSERT_RETURN_VOID(env_, status == napi_ok, "failed to create ref to js death recipient");
32 }
33 
AfterWorkCallback(OnRemoteDiedParam * param)34 void NAPIDeathRecipient::AfterWorkCallback(OnRemoteDiedParam *param)
35 {
36     ZLOGD(LOG_LABEL, "start to call onRemoteDied");
37     napi_handle_scope scope = nullptr;
38     napi_open_handle_scope(param->env, &scope);
39 
40     auto CleanUp = [&param, &scope]() {
41         napi_close_handle_scope(param->env, scope);
42         delete param;
43     };
44 
45     napi_value jsDeathRecipient = nullptr;
46     napi_get_reference_value(param->env, param->deathRecipient->GetDeathRecipientRef(), &jsDeathRecipient);
47     if (jsDeathRecipient == nullptr) {
48         ZLOGE(LOG_LABEL, "failed to get js death recipient");
49         CleanUp();
50         return;
51     }
52 
53     napi_value onRemoteDied = nullptr;
54     napi_get_named_property(param->env, jsDeathRecipient, "onRemoteDied", &onRemoteDied);
55     if (onRemoteDied == nullptr) {
56         ZLOGE(LOG_LABEL, "failed to get property onRemoteDied");
57         CleanUp();
58         return;
59     }
60 
61     napi_value returnVal = nullptr;
62     napi_call_function(param->env, jsDeathRecipient, onRemoteDied, 0, nullptr, &returnVal);
63     if (returnVal == nullptr) {
64         ZLOGE(LOG_LABEL, "failed to call function onRemoteDied");
65     }
66 
67     napi_status napiStatus = napi_delete_reference(param->env, param->deathRecipient->GetDeathRecipientRef());
68     if (napiStatus != napi_ok) {
69         ZLOGE(LOG_LABEL, "failed to delete ref to js death recipient");
70     }
71     param->deathRecipient->CleanDeathRecipientRef();
72 
73     CleanUp();
74 }
75 
OnRemoteDied(const wptr<IRemoteObject> & object)76 void NAPIDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
77 {
78     if (deathRecipientRef_ == nullptr) {
79         ZLOGE(LOG_LABEL, "js death recipient has already removed");
80         return;
81     }
82 
83     if (env_ == nullptr) {
84         ZLOGE(LOG_LABEL, "js env has been destructed");
85         return;
86     }
87 
88     OnRemoteDiedParam *param = new (std::nothrow) OnRemoteDiedParam {
89         .env = env_,
90         .deathRecipient = this
91     };
92     NAPI_ASSERT_RETURN_VOID(env_, param != nullptr, "new OperateJsRefParam failed");
93 
94     auto task = [param]() {
95         AfterWorkCallback(param);
96     };
97     napi_status sendRet = napi_send_event(env_, task, napi_eprio_high);
98     if (sendRet != napi_ok) {
99         ZLOGE(LOG_LABEL, "napi_send_event failed, ret:%{public}d", sendRet);
100         delete param;
101     }
102 }
103 
Matches(napi_value object)104 bool NAPIDeathRecipient::Matches(napi_value object)
105 {
106     bool result = false;
107     if (object != nullptr && deathRecipientRef_ != nullptr) {
108         napi_value jsDeathRecipient = nullptr;
109         napi_get_reference_value(env_, deathRecipientRef_, &jsDeathRecipient);
110         napi_status status = napi_strict_equals(env_, object, jsDeathRecipient, &result);
111         if (status != napi_ok) {
112             ZLOGI(LOG_LABEL, "compares death recipients failed");
113         }
114     }
115     return result;
116 }
117 
GetDeathRecipientRef() const118 napi_ref NAPIDeathRecipient::GetDeathRecipientRef() const
119 {
120     return deathRecipientRef_;
121 }
122 
CleanDeathRecipientRef()123 void NAPIDeathRecipient::CleanDeathRecipientRef()
124 {
125     deathRecipientRef_ = nullptr;
126 }
127 
NAPIDeathRecipientList()128 NAPIDeathRecipientList::NAPIDeathRecipientList() {}
129 
~NAPIDeathRecipientList()130 NAPIDeathRecipientList::~NAPIDeathRecipientList()
131 {
132     std::lock_guard<std::mutex> lockGuard(mutex_);
133     set_.clear();
134 }
135 
Add(const sptr<NAPIDeathRecipient> & recipient)136 bool NAPIDeathRecipientList::Add(const sptr<NAPIDeathRecipient> &recipient)
137 {
138     std::lock_guard<std::mutex> lockGuard(mutex_);
139     auto ret = set_.insert(recipient);
140     return ret.second;
141 }
142 
Remove(const sptr<NAPIDeathRecipient> & recipient)143 bool NAPIDeathRecipientList::Remove(const sptr<NAPIDeathRecipient> &recipient)
144 {
145     std::lock_guard<std::mutex> lockGuard(mutex_);
146     return (set_.erase(recipient) > 0);
147 }
148 
Find(napi_value jsRecipient)149 sptr<NAPIDeathRecipient> NAPIDeathRecipientList::Find(napi_value jsRecipient)
150 {
151     std::lock_guard<std::mutex> lockGuard(mutex_);
152     for (auto it = set_.begin(); it != set_.end(); it++) {
153         if ((*it)->Matches(jsRecipient)) {
154             return *it;
155         }
156     }
157     return nullptr;
158 }
159 
NAPIRemoteProxyHolder()160 NAPIRemoteProxyHolder::NAPIRemoteProxyHolder() : list_(nullptr), object_(nullptr) {}
161 
~NAPIRemoteProxyHolder()162 NAPIRemoteProxyHolder::~NAPIRemoteProxyHolder()
163 {
164     list_ = nullptr;
165     object_ = nullptr;
166 }
167 
NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env,napi_value jsRemoteProxy)168 NAPIRemoteProxyHolder *NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env, napi_value jsRemoteProxy)
169 {
170     NAPIRemoteProxyHolder *proxyHolder = nullptr;
171     napi_unwrap(env, jsRemoteProxy, (void **)&proxyHolder);
172     NAPI_ASSERT(env, proxyHolder != nullptr, "failed to get napi remote proxy holder");
173     return proxyHolder;
174 }
175 } // namespace OHOS