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(uv_work_t * work,int status)34 void NAPIDeathRecipient::AfterWorkCallback(uv_work_t *work, int status)
35 {
36 if (work == nullptr || work->data == nullptr) {
37 ZLOGE(LOG_LABEL, "work or work->data is nullptr");
38 return;
39 }
40 ZLOGD(LOG_LABEL, "start to call onRemoteDied");
41 OnRemoteDiedParam *param = reinterpret_cast<OnRemoteDiedParam *>(work->data);
42 napi_handle_scope scope = nullptr;
43 napi_open_handle_scope(param->env, &scope);
44
45 auto CleanUp = [&]() {
46 napi_close_handle_scope(param->env, scope);
47 delete param;
48 delete work;
49 };
50
51 napi_value jsDeathRecipient = nullptr;
52 napi_get_reference_value(param->env, param->deathRecipientRef, &jsDeathRecipient);
53 if (jsDeathRecipient == nullptr) {
54 ZLOGE(LOG_LABEL, "failed to get js death recipient");
55 CleanUp();
56 return;
57 }
58
59 napi_value onRemoteDied = nullptr;
60 napi_get_named_property(param->env, jsDeathRecipient, "onRemoteDied", &onRemoteDied);
61 if (onRemoteDied == nullptr) {
62 ZLOGE(LOG_LABEL, "failed to get property onRemoteDied");
63 CleanUp();
64 return;
65 }
66
67 napi_value returnVal = nullptr;
68 napi_call_function(param->env, jsDeathRecipient, onRemoteDied, 0, nullptr, &returnVal);
69 if (returnVal == nullptr) {
70 ZLOGE(LOG_LABEL, "failed to call function onRemoteDied");
71 }
72
73 napi_status napiStatus = napi_delete_reference(param->env, param->deathRecipientRef);
74 if (napiStatus != napi_ok) {
75 ZLOGE(LOG_LABEL, "failed to delete ref to js death recipient");
76 }
77
78 CleanUp();
79 }
80
OnRemoteDied(const wptr<IRemoteObject> & object)81 void NAPIDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
82 {
83 if (deathRecipientRef_ == nullptr) {
84 ZLOGE(LOG_LABEL, "js death recipient has already removed");
85 return;
86 }
87
88 if (env_ == nullptr) {
89 ZLOGE(LOG_LABEL, "js env has been destructed");
90 return;
91 }
92
93 uv_loop_s *loop = nullptr;
94 napi_get_uv_event_loop(env_, &loop);
95 if (loop == nullptr) {
96 ZLOGE(LOG_LABEL, "loop is nullptr");
97 return;
98 }
99
100 uv_work_t *work = new (std::nothrow) uv_work_t;
101 if (work == nullptr) {
102 ZLOGE(LOG_LABEL, "failed to new uv_work_t");
103 return;
104 }
105 OnRemoteDiedParam *param = new (std::nothrow) OnRemoteDiedParam {
106 .env = env_,
107 .deathRecipientRef = deathRecipientRef_
108 };
109 if (param == nullptr) {
110 ZLOGE(LOG_LABEL, "new OnRemoteDiedParam failed");
111 delete work;
112 return;
113 }
114 work->data = reinterpret_cast<void *>(param);
115 ZLOGI(LOG_LABEL, "start to queue");
116 int uvRet = uv_queue_work(loop, work, [](uv_work_t *work) {
117 ZLOGD(LOG_LABEL, "enter work pool.");
118 }, AfterWorkCallback);
119 if (uvRet != 0) {
120 ZLOGE(LOG_LABEL, "uv_queue_work failed, ret %{public}d", uvRet);
121 delete param;
122 delete work;
123 }
124 }
125
Matches(napi_value object)126 bool NAPIDeathRecipient::Matches(napi_value object)
127 {
128 bool result = false;
129 if (object != nullptr && deathRecipientRef_ != nullptr) {
130 napi_value jsDeathRecipient = nullptr;
131 napi_get_reference_value(env_, deathRecipientRef_, &jsDeathRecipient);
132 napi_status status = napi_strict_equals(env_, object, jsDeathRecipient, &result);
133 if (status != napi_ok) {
134 ZLOGI(LOG_LABEL, "compares death recipients failed");
135 }
136 }
137 return result;
138 }
139
NAPIDeathRecipientList()140 NAPIDeathRecipientList::NAPIDeathRecipientList() {}
141
~NAPIDeathRecipientList()142 NAPIDeathRecipientList::~NAPIDeathRecipientList()
143 {
144 std::lock_guard<std::mutex> lockGuard(mutex_);
145 set_.clear();
146 }
147
Add(const sptr<NAPIDeathRecipient> & recipient)148 bool NAPIDeathRecipientList::Add(const sptr<NAPIDeathRecipient> &recipient)
149 {
150 std::lock_guard<std::mutex> lockGuard(mutex_);
151 auto ret = set_.insert(recipient);
152 return ret.second;
153 }
154
Remove(const sptr<NAPIDeathRecipient> & recipient)155 bool NAPIDeathRecipientList::Remove(const sptr<NAPIDeathRecipient> &recipient)
156 {
157 std::lock_guard<std::mutex> lockGuard(mutex_);
158 return (set_.erase(recipient) > 0);
159 }
160
Find(napi_value jsRecipient)161 sptr<NAPIDeathRecipient> NAPIDeathRecipientList::Find(napi_value jsRecipient)
162 {
163 std::lock_guard<std::mutex> lockGuard(mutex_);
164 for (auto it = set_.begin(); it != set_.end(); it++) {
165 if ((*it)->Matches(jsRecipient)) {
166 return *it;
167 }
168 }
169 return nullptr;
170 }
171
NAPIRemoteProxyHolder()172 NAPIRemoteProxyHolder::NAPIRemoteProxyHolder() : list_(nullptr), object_(nullptr) {}
173
~NAPIRemoteProxyHolder()174 NAPIRemoteProxyHolder::~NAPIRemoteProxyHolder()
175 {
176 list_ = nullptr;
177 object_ = nullptr;
178 }
179
NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env,napi_value jsRemoteProxy)180 NAPIRemoteProxyHolder *NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env, napi_value jsRemoteProxy)
181 {
182 NAPIRemoteProxyHolder *proxyHolder = nullptr;
183 napi_unwrap(env, jsRemoteProxy, (void **)&proxyHolder);
184 NAPI_ASSERT(env, proxyHolder != nullptr, "failed to get napi remote proxy holder");
185 return proxyHolder;
186 }
187 } // namesapce OHOS