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 }
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 
GetEnv()62 napi_env LocationSwitchCallbackNapi::GetEnv()
63 {
64     std::unique_lock<std::mutex> guard(mutex_);
65     return env_;
66 }
67 
SetEnv(const napi_env & env)68 void LocationSwitchCallbackNapi::SetEnv(const napi_env& env)
69 {
70     std::unique_lock<std::mutex> guard(mutex_);
71     env_ = env;
72 }
73 
GetHandleCb()74 napi_ref LocationSwitchCallbackNapi::GetHandleCb()
75 {
76     std::unique_lock<std::mutex> guard(mutex_);
77     return handlerCb_;
78 }
79 
SetHandleCb(const napi_ref & handlerCb)80 void LocationSwitchCallbackNapi::SetHandleCb(const napi_ref& handlerCb)
81 {
82     {
83         std::unique_lock<std::mutex> guard(mutex_);
84         handlerCb_ = handlerCb;
85     }
86     std::unique_lock<std::mutex> guard(g_regCallbackMutex);
87     g_registerCallbacks.emplace_back(handlerCb);
88 }
89 
FindSwitchCallback(napi_ref cb)90 bool FindSwitchCallback(napi_ref cb)
91 {
92     std::unique_lock<std::mutex> guard(g_regCallbackMutex);
93     auto iter = std::find(g_registerCallbacks.begin(), g_registerCallbacks.end(), cb);
94     if (iter == g_registerCallbacks.end()) {
95         return false;
96     }
97     return true;
98 }
99 
DeleteSwitchCallback(napi_ref cb)100 void DeleteSwitchCallback(napi_ref cb)
101 {
102     std::unique_lock<std::mutex> guard(g_regCallbackMutex);
103     for (auto iter = g_registerCallbacks.begin(); iter != g_registerCallbacks.end(); iter++) {
104         if (*iter == cb) {
105             iter = g_registerCallbacks.erase(iter);
106             break;
107         }
108     }
109 }
110 
IsRemoteDied()111 bool LocationSwitchCallbackNapi::IsRemoteDied()
112 {
113     return remoteDied_;
114 }
115 
PackResult(bool switchState)116 napi_value LocationSwitchCallbackNapi::PackResult(bool switchState)
117 {
118     napi_value result;
119     NAPI_CALL(env_, napi_get_boolean(env_, switchState, &result));
120     return result;
121 }
122 
Send(int switchState)123 bool LocationSwitchCallbackNapi::Send(int switchState)
124 {
125     std::unique_lock<std::mutex> guard(mutex_);
126     uv_loop_s *loop = nullptr;
127     NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
128     if (loop == nullptr) {
129         LBSLOGE(SWITCH_CALLBACK, "loop == nullptr.");
130         return false;
131     }
132     if (handlerCb_ == nullptr) {
133         LBSLOGE(SWITCH_CALLBACK, "handler is nullptr.");
134         return false;
135     }
136     uv_work_t *work = new (std::nothrow) uv_work_t;
137     if (work == nullptr) {
138         LBSLOGE(SWITCH_CALLBACK, "work == nullptr.");
139         return false;
140     }
141     SwitchAsyncContext *context = new (std::nothrow) SwitchAsyncContext(env_);
142     if (context == nullptr) {
143         LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
144         delete work;
145         return false;
146     }
147     if (!InitContext(context)) {
148         LBSLOGE(LOCATOR_CALLBACK, "InitContext fail");
149         return false;
150     }
151     context->enable = (switchState == 1 ? true : false);
152     work->data = context;
153     UvQueueWork(loop, work);
154     return true;
155 }
156 
UvQueueWork(uv_loop_s * loop,uv_work_t * work)157 void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
158 {
159     uv_queue_work(
160         loop,
161         work,
162         [](uv_work_t *work) {},
163         [](uv_work_t *work, int status) {
164             SwitchAsyncContext *context = nullptr;
165             napi_handle_scope scope = nullptr;
166             if (work == nullptr) {
167                 LBSLOGE(SWITCH_CALLBACK, "work is nullptr!");
168                 return;
169             }
170             context = static_cast<SwitchAsyncContext *>(work->data);
171             if (context == nullptr || context->env == nullptr) {
172                 LBSLOGE(SWITCH_CALLBACK, "context is nullptr!");
173                 delete work;
174                 return;
175             }
176             if (!FindSwitchCallback(context->callback[0])) {
177                 LBSLOGE(SWITCH_CALLBACK, "no valid callback");
178                 delete context;
179                 delete work;
180                 return;
181             }
182             NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
183             napi_value jsEvent;
184             CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_boolean(context->env, context->enable, &jsEvent),
185                 scope, context, work);
186             if (scope == nullptr) {
187                 LBSLOGE(SWITCH_CALLBACK, "scope is nullptr");
188                 delete context;
189                 delete work;
190                 return;
191             }
192             if (context->callback[0] != nullptr) {
193                 napi_value undefine;
194                 napi_value handler = nullptr;
195                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
196                     scope, context, work);
197                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
198                     napi_get_reference_value(context->env, context->callback[0], &handler), scope, context, work);
199                 if (napi_call_function(context->env, nullptr, handler, 1,
200                     &jsEvent, &undefine) != napi_ok) {
201                     LBSLOGE(SWITCH_CALLBACK, "Report event failed");
202                 }
203             }
204             NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
205             delete context;
206             delete work;
207     });
208 }
209 
OnSwitchChange(int switchState)210 void LocationSwitchCallbackNapi::OnSwitchChange(int switchState)
211 {
212     LBSLOGD(SWITCH_CALLBACK, "LocatorCallbackNapi::OnSwitchChange");
213     Send(switchState);
214 }
215 
DeleteHandler()216 void LocationSwitchCallbackNapi::DeleteHandler()
217 {
218     std::unique_lock<std::mutex> guard(mutex_);
219     if (handlerCb_ == nullptr || env_ == nullptr) {
220         LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr.");
221         return;
222     }
223     DeleteSwitchCallback(handlerCb_);
224     NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
225     handlerCb_ = nullptr;
226 }
227 }  // namespace Location
228 }  // namespace OHOS
229