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