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 <uv.h>
19
20 #include "ipc_debug.h"
21 #include "log_tags.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "native_engine/native_value.h"
25
26 namespace OHOS {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "NapiRemoteProxyHolder" };
28
NAPIDeathRecipient(napi_env env,napi_value jsDeathRecipient)29 NAPIDeathRecipient::NAPIDeathRecipient(napi_env env, napi_value jsDeathRecipient)
30 {
31 env_ = env;
32 napi_status status = napi_create_reference(env_, jsDeathRecipient, 1, &deathRecipientRef_);
33 NAPI_ASSERT_RETURN_VOID(env, status == napi_ok, "failed to create ref to js death recipient");
34 }
35
OnRemoteDied(const wptr<IRemoteObject> & object)36 void NAPIDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
37 {
38 if (deathRecipientRef_ == nullptr) {
39 ZLOGE(LOG_LABEL, "js death recipient has already removed");
40 return;
41 }
42
43 uv_loop_s *loop = nullptr;
44 napi_get_uv_event_loop(env_, &loop);
45 uv_work_t *work = new(std::nothrow) uv_work_t;
46 if (work == nullptr) {
47 ZLOGE(LOG_LABEL, "failed to new uv_work_t");
48 return;
49 }
50 OnRemoteDiedParam *param = new OnRemoteDiedParam {
51 .env = env_,
52 .deathRecipientRef = deathRecipientRef_
53 };
54 work->data = reinterpret_cast<void *>(param);
55 ZLOGI(LOG_LABEL, "start to queue");
56 uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) {
57 ZLOGI(LOG_LABEL, "start to call onRmeoteDied");
58 OnRemoteDiedParam *param = reinterpret_cast<OnRemoteDiedParam *>(work->data);
59 napi_handle_scope scope = nullptr;
60 napi_open_handle_scope(param->env, &scope);
61 napi_value jsDeathRecipient = nullptr;
62 napi_get_reference_value(param->env, param->deathRecipientRef, &jsDeathRecipient);
63 NAPI_ASSERT_RETURN_VOID(param->env, jsDeathRecipient != nullptr, "failed to get js death recipient");
64 napi_value onRemoteDied = nullptr;
65 napi_get_named_property(param->env, jsDeathRecipient, "onRemoteDied", &onRemoteDied);
66 NAPI_ASSERT_RETURN_VOID(param->env, onRemoteDied != nullptr, "failed to get property onRemoteDied");
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 napi_close_handle_scope(param->env, scope);
75 NAPI_ASSERT_RETURN_VOID(param->env, napiStatus == napi_ok, "failed to delete ref to js death recipient");
76 delete param;
77 delete work;
78 });
79 }
80
Matches(napi_value object)81 bool NAPIDeathRecipient::Matches(napi_value object)
82 {
83 bool result = false;
84 if (object != nullptr) {
85 if (deathRecipientRef_ != nullptr) {
86 napi_value jsDeathRecipient = nullptr;
87 napi_get_reference_value(env_, deathRecipientRef_, &jsDeathRecipient);
88 napi_status status = napi_strict_equals(env_, object, jsDeathRecipient, &result);
89 if (status != napi_ok) {
90 ZLOGI(LOG_LABEL, "compares death recipients failed");
91 }
92 }
93 }
94 return result;
95 }
96
NAPIDeathRecipientList()97 NAPIDeathRecipientList::NAPIDeathRecipientList() {}
98
~NAPIDeathRecipientList()99 NAPIDeathRecipientList::~NAPIDeathRecipientList()
100 {
101 std::lock_guard<std::mutex> lockGuard(mutex_);
102 set_.clear();
103 }
104
Add(const sptr<NAPIDeathRecipient> & recipient)105 bool NAPIDeathRecipientList::Add(const sptr<NAPIDeathRecipient> &recipient)
106 {
107 std::lock_guard<std::mutex> lockGuard(mutex_);
108 auto ret = set_.insert(recipient);
109 return ret.second;
110 }
111
Remove(const sptr<NAPIDeathRecipient> & recipient)112 bool NAPIDeathRecipientList::Remove(const sptr<NAPIDeathRecipient> &recipient)
113 {
114 std::lock_guard<std::mutex> lockGuard(mutex_);
115 return (set_.erase(recipient) > 0);
116 }
117
Find(napi_value jsRecipient)118 sptr<NAPIDeathRecipient> NAPIDeathRecipientList::Find(napi_value jsRecipient)
119 {
120 std::lock_guard<std::mutex> lockGuard(mutex_);
121 for (auto it = set_.begin(); it != set_.end(); it++) {
122 if ((*it)->Matches(jsRecipient)) {
123 return *it;
124 }
125 }
126 return nullptr;
127 }
128
NAPIRemoteProxyHolder()129 NAPIRemoteProxyHolder::NAPIRemoteProxyHolder() : list_(nullptr), object_(nullptr) {}
130
~NAPIRemoteProxyHolder()131 NAPIRemoteProxyHolder::~NAPIRemoteProxyHolder()
132 {
133 list_ = nullptr;
134 object_ = nullptr;
135 }
136
NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env,napi_value jsRemoteProxy)137 NAPIRemoteProxyHolder *NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env, napi_value jsRemoteProxy)
138 {
139 NAPIRemoteProxyHolder *proxyHolder = nullptr;
140 napi_unwrap(env, jsRemoteProxy, (void **)&proxyHolder);
141 NAPI_ASSERT(env, proxyHolder != nullptr, "failed to get napi remote proxy holder");
142 return proxyHolder;
143 }
144 } // namesapce OHOS