• 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(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 OnRemoteDiedParam {
106         .env = env_,
107         .deathRecipientRef = deathRecipientRef_
108     };
109     work->data = reinterpret_cast<void *>(param);
110     ZLOGI(LOG_LABEL, "start to queue");
111     int uvRet = uv_queue_work(loop, work, [](uv_work_t *work) {
112         ZLOGD(LOG_LABEL, "enter work pool.");
113     }, AfterWorkCallback);
114     if (uvRet != 0) {
115         ZLOGE(LOG_LABEL, "uv_queue_work failed, ret %{public}d", uvRet);
116     }
117 }
118 
Matches(napi_value object)119 bool NAPIDeathRecipient::Matches(napi_value object)
120 {
121     bool result = false;
122     if (object != nullptr && deathRecipientRef_ != nullptr) {
123         napi_value jsDeathRecipient = nullptr;
124         napi_get_reference_value(env_, deathRecipientRef_, &jsDeathRecipient);
125         napi_status status = napi_strict_equals(env_, object, jsDeathRecipient, &result);
126         if (status != napi_ok) {
127             ZLOGI(LOG_LABEL, "compares death recipients failed");
128         }
129     }
130     return result;
131 }
132 
NAPIDeathRecipientList()133 NAPIDeathRecipientList::NAPIDeathRecipientList() {}
134 
~NAPIDeathRecipientList()135 NAPIDeathRecipientList::~NAPIDeathRecipientList()
136 {
137     std::lock_guard<std::mutex> lockGuard(mutex_);
138     set_.clear();
139 }
140 
Add(const sptr<NAPIDeathRecipient> & recipient)141 bool NAPIDeathRecipientList::Add(const sptr<NAPIDeathRecipient> &recipient)
142 {
143     std::lock_guard<std::mutex> lockGuard(mutex_);
144     auto ret = set_.insert(recipient);
145     return ret.second;
146 }
147 
Remove(const sptr<NAPIDeathRecipient> & recipient)148 bool NAPIDeathRecipientList::Remove(const sptr<NAPIDeathRecipient> &recipient)
149 {
150     std::lock_guard<std::mutex> lockGuard(mutex_);
151     return (set_.erase(recipient) > 0);
152 }
153 
Find(napi_value jsRecipient)154 sptr<NAPIDeathRecipient> NAPIDeathRecipientList::Find(napi_value jsRecipient)
155 {
156     std::lock_guard<std::mutex> lockGuard(mutex_);
157     for (auto it = set_.begin(); it != set_.end(); it++) {
158         if ((*it)->Matches(jsRecipient)) {
159             return *it;
160         }
161     }
162     return nullptr;
163 }
164 
NAPIRemoteProxyHolder()165 NAPIRemoteProxyHolder::NAPIRemoteProxyHolder() : list_(nullptr), object_(nullptr) {}
166 
~NAPIRemoteProxyHolder()167 NAPIRemoteProxyHolder::~NAPIRemoteProxyHolder()
168 {
169     list_ = nullptr;
170     object_ = nullptr;
171 }
172 
NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env,napi_value jsRemoteProxy)173 NAPIRemoteProxyHolder *NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env, napi_value jsRemoteProxy)
174 {
175     NAPIRemoteProxyHolder *proxyHolder = nullptr;
176     napi_unwrap(env, jsRemoteProxy, (void **)&proxyHolder);
177     NAPI_ASSERT(env, proxyHolder != nullptr, "failed to get napi remote proxy holder");
178     return proxyHolder;
179 }
180 } // namesapce OHOS