1 /*
2 * Copyright (C) 2024 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 "location_switch_callback_napi.h"
17 #include "common_utils.h"
18 #include "ipc_skeleton.h"
19 #include "location_log.h"
20
21 namespace OHOS {
22 namespace Location {
23 static std::mutex g_regCallbackMutex;
24 static std::vector<napi_ref> g_registerCallbacks;
LocationSwitchCallbackNapi()25 LocationSwitchCallbackNapi::LocationSwitchCallbackNapi()
26 {
27 env_ = nullptr;
28 handlerCb_ = nullptr;
29 remoteDied_ = false;
30 }
31
~LocationSwitchCallbackNapi()32 LocationSwitchCallbackNapi::~LocationSwitchCallbackNapi()
33 {
34 LBSLOGW(SWITCH_CALLBACK, "~LocationSwitchCallbackNapi()");
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int LocationSwitchCallbackNapi::OnRemoteRequest(
38 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
39 {
40 LBSLOGD(SWITCH_CALLBACK, "LocatorCallbackNapi::OnRemoteRequest!");
41 if (data.ReadInterfaceToken() != GetDescriptor()) {
42 LBSLOGE(SWITCH_CALLBACK, "invalid token.");
43 return -1;
44 }
45 if (remoteDied_) {
46 LBSLOGD(SWITCH_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
47 return -1;
48 }
49
50 switch (code) {
51 case RECEIVE_SWITCH_STATE_EVENT: {
52 OnSwitchChange(data.ReadInt32());
53 break;
54 }
55 default: {
56 IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57 break;
58 }
59 }
60 return 0;
61 }
62
GetEnv()63 napi_env LocationSwitchCallbackNapi::GetEnv()
64 {
65 std::unique_lock<std::mutex> guard(mutex_);
66 return env_;
67 }
68
SetEnv(const napi_env & env)69 void LocationSwitchCallbackNapi::SetEnv(const napi_env& env)
70 {
71 std::unique_lock<std::mutex> guard(mutex_);
72 env_ = env;
73 }
74
GetHandleCb()75 napi_ref LocationSwitchCallbackNapi::GetHandleCb()
76 {
77 std::unique_lock<std::mutex> guard(mutex_);
78 return handlerCb_;
79 }
80
SetHandleCb(const napi_ref & handlerCb)81 void LocationSwitchCallbackNapi::SetHandleCb(const napi_ref& handlerCb)
82 {
83 {
84 std::unique_lock<std::mutex> guard(mutex_);
85 handlerCb_ = handlerCb;
86 }
87 std::unique_lock<std::mutex> guard(g_regCallbackMutex);
88 g_registerCallbacks.emplace_back(handlerCb);
89 }
90
FindSwitchCallback(napi_ref cb)91 bool FindSwitchCallback(napi_ref cb)
92 {
93 std::unique_lock<std::mutex> guard(g_regCallbackMutex);
94 auto iter = std::find(g_registerCallbacks.begin(), g_registerCallbacks.end(), cb);
95 if (iter == g_registerCallbacks.end()) {
96 return false;
97 }
98 return true;
99 }
100
DeleteSwitchCallback(napi_ref cb)101 void DeleteSwitchCallback(napi_ref cb)
102 {
103 std::unique_lock<std::mutex> guard(g_regCallbackMutex);
104 for (auto iter = g_registerCallbacks.begin(); iter != g_registerCallbacks.end(); iter++) {
105 if (*iter == cb) {
106 iter = g_registerCallbacks.erase(iter);
107 break;
108 }
109 }
110 LBSLOGW(SWITCH_CALLBACK, "after DeleteSwitchCallback, callback size %{public}s",
111 std::to_string(g_registerCallbacks.size()).c_str());
112 }
113
IsRemoteDied()114 bool LocationSwitchCallbackNapi::IsRemoteDied()
115 {
116 return remoteDied_;
117 }
118
PackResult(bool switchState)119 napi_value LocationSwitchCallbackNapi::PackResult(bool switchState)
120 {
121 napi_value result;
122 NAPI_CALL(env_, napi_get_boolean(env_, switchState, &result));
123 return result;
124 }
125
Send(int switchState)126 bool LocationSwitchCallbackNapi::Send(int switchState)
127 {
128 std::unique_lock<std::mutex> guard(mutex_);
129 uv_loop_s *loop = nullptr;
130 NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
131 if (loop == nullptr) {
132 LBSLOGE(SWITCH_CALLBACK, "loop == nullptr.");
133 return false;
134 }
135 if (handlerCb_ == nullptr) {
136 LBSLOGE(SWITCH_CALLBACK, "handler is nullptr.");
137 return false;
138 }
139 uv_work_t *work = new (std::nothrow) uv_work_t;
140 if (work == nullptr) {
141 LBSLOGE(SWITCH_CALLBACK, "work == nullptr.");
142 return false;
143 }
144 SwitchAsyncContext *context = new (std::nothrow) SwitchAsyncContext(env_);
145 if (context == nullptr) {
146 LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
147 delete work;
148 return false;
149 }
150 if (!InitContext(context)) {
151 LBSLOGE(LOCATOR_CALLBACK, "InitContext fail");
152 delete work;
153 delete context;
154 return false;
155 }
156 context->enable = (switchState == 1 ? true : false);
157 work->data = context;
158 UvQueueWork(loop, work);
159 return true;
160 }
161
UvQueueWork(uv_loop_s * loop,uv_work_t * work)162 void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
163 {
164 uv_queue_work(
165 loop,
166 work,
167 [](uv_work_t *work) {},
168 [](uv_work_t *work, int status) {
169 SwitchAsyncContext *context = nullptr;
170 napi_handle_scope scope = nullptr;
171 if (work == nullptr) {
172 LBSLOGE(SWITCH_CALLBACK, "work is nullptr!");
173 return;
174 }
175 context = static_cast<SwitchAsyncContext *>(work->data);
176 if (context == nullptr || context->env == nullptr) {
177 LBSLOGE(SWITCH_CALLBACK, "context is nullptr!");
178 delete work;
179 return;
180 }
181 NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
182 napi_value jsEvent;
183 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_boolean(context->env, context->enable, &jsEvent),
184 scope, context, work);
185 if (scope == nullptr) {
186 LBSLOGE(SWITCH_CALLBACK, "scope is nullptr");
187 delete context;
188 delete work;
189 return;
190 }
191 if (context->callback[0] != nullptr) {
192 napi_value undefine;
193 napi_value handler = nullptr;
194 napi_status ret = napi_ok;
195 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
196 scope, context, work);
197 if (FindSwitchCallback(context->callback[0])) {
198 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
199 napi_get_reference_value(context->env, context->callback[0], &handler), scope, context, work);
200 ret = napi_call_function(context->env, nullptr, handler, 1, &jsEvent, &undefine);
201 } else {
202 LBSLOGE(SWITCH_CALLBACK, "no valid callback");
203 }
204 if (ret != napi_ok) {
205 LBSLOGE(SWITCH_CALLBACK, "Report event failed");
206 }
207 }
208 NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
209 delete context;
210 delete work;
211 });
212 }
213
OnSwitchChange(int switchState)214 void LocationSwitchCallbackNapi::OnSwitchChange(int switchState)
215 {
216 LBSLOGD(SWITCH_CALLBACK, "LocatorCallbackNapi::OnSwitchChange");
217 Send(switchState);
218 }
219
DeleteHandler()220 void LocationSwitchCallbackNapi::DeleteHandler()
221 {
222 std::unique_lock<std::mutex> guard(mutex_);
223 if (handlerCb_ == nullptr || env_ == nullptr) {
224 LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr.");
225 return;
226 }
227 DeleteSwitchCallback(handlerCb_);
228 NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
229 handlerCb_ = nullptr;
230 }
231 } // namespace Location
232 } // namespace OHOS
233