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 #include "locating_required_data_callback_host.h"
16
17 #include "ipc_skeleton.h"
18 #include "napi/native_common.h"
19
20 #include "common_utils.h"
21 #include "location_log.h"
22 #include "napi_util.h"
23
24 namespace OHOS {
25 namespace Location {
LocatingRequiredDataCallbackHost()26 LocatingRequiredDataCallbackHost::LocatingRequiredDataCallbackHost()
27 {
28 env_ = nullptr;
29 handlerCb_ = nullptr;
30 remoteDied_ = false;
31 fixNumber_ = 0;
32 InitLatch();
33 }
34
~LocatingRequiredDataCallbackHost()35 LocatingRequiredDataCallbackHost::~LocatingRequiredDataCallbackHost()
36 {
37 if (latch_ != nullptr) {
38 delete latch_;
39 latch_ = nullptr;
40 }
41 }
42
InitLatch()43 void LocatingRequiredDataCallbackHost::InitLatch()
44 {
45 latch_ = new CountDownLatch();
46 latch_->SetCount(1);
47 }
48
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int LocatingRequiredDataCallbackHost::OnRemoteRequest(
50 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
51 {
52 LBSLOGD(LOCATING_DATA_CALLBACK, "LocatingRequiredDataCallbackHost::OnRemoteRequest!");
53 if (data.ReadInterfaceToken() != GetDescriptor()) {
54 LBSLOGE(LOCATING_DATA_CALLBACK, "invalid token.");
55 return -1;
56 }
57 if (remoteDied_) {
58 LBSLOGD(LOCATING_DATA_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
59 return -1;
60 }
61
62 switch (code) {
63 case RECEIVE_INFO_EVENT: {
64 int cnt = data.ReadInt32();
65 if (cnt > 0 && cnt <= MAXIMUM_LOCATING_REQUIRED_DATAS) {
66 std::vector<std::shared_ptr<LocatingRequiredData>> res;
67 for (int i = 0; i < cnt; i++) {
68 res.push_back(LocatingRequiredData::Unmarshalling(data));
69 }
70 // update wifi info
71 if (res[0]->GetType() == LocatingRequiredDataType::WIFI) {
72 SetSingleResult(res);
73 }
74 OnLocatingDataChange(res);
75 CountDown();
76 }
77 break;
78 }
79 default: {
80 IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81 break;
82 }
83 }
84 return 0;
85 }
86
IsRemoteDied()87 bool LocatingRequiredDataCallbackHost::IsRemoteDied()
88 {
89 return remoteDied_;
90 }
91
Send(const std::vector<std::shared_ptr<LocatingRequiredData>> & data)92 bool LocatingRequiredDataCallbackHost::Send(const std::vector<std::shared_ptr<LocatingRequiredData>>& data)
93 {
94 if (IsSingleLocationRequest()) {
95 return false;
96 }
97 std::unique_lock<std::mutex> guard(mutex_);
98 uv_loop_s *loop = nullptr;
99 NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
100 if (loop == nullptr) {
101 LBSLOGE(LOCATING_DATA_CALLBACK, "loop == nullptr.");
102 return false;
103 }
104 uv_work_t *work = new (std::nothrow) uv_work_t;
105 if (work == nullptr) {
106 LBSLOGE(LOCATING_DATA_CALLBACK, "work == nullptr.");
107 return false;
108 }
109 LocatingRequiredDataAsyncContext *context = new (std::nothrow) LocatingRequiredDataAsyncContext(env_);
110 if (context == nullptr) {
111 LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr.");
112 return false;
113 }
114 context->env = env_;
115 context->callback[SUCCESS_CALLBACK] = handlerCb_;
116 context->locatingRequiredDataList_ = data;
117 work->data = context;
118 UvQueueWork(loop, work);
119 return true;
120 }
121
UvQueueWork(uv_loop_s * loop,uv_work_t * work)122 void LocatingRequiredDataCallbackHost::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
123 {
124 uv_queue_work(
125 loop,
126 work,
127 [](uv_work_t *work) {},
128 [](uv_work_t *work, int status) {
129 LocatingRequiredDataAsyncContext *context = nullptr;
130 napi_handle_scope scope = nullptr;
131 if (work == nullptr) {
132 LBSLOGE(LOCATING_DATA_CALLBACK, "work is nullptr");
133 return;
134 }
135 context = static_cast<LocatingRequiredDataAsyncContext *>(work->data);
136 if (context == nullptr || context->env == nullptr) {
137 LBSLOGE(LOCATING_DATA_CALLBACK, "context is nullptr");
138 delete work;
139 return;
140 }
141 NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
142 if (scope == nullptr) {
143 LBSLOGE(LOCATING_DATA_CALLBACK, "scope is nullptr");
144 delete context;
145 delete work;
146 return;
147 }
148 napi_value jsEvent = nullptr;
149 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
150 napi_create_array_with_length(context->env, context->locatingRequiredDataList_.size(), &jsEvent),
151 scope, context, work);
152 LocatingRequiredDataToJsObj(context->env, context->locatingRequiredDataList_, jsEvent);
153 if (context->callback[0] != nullptr) {
154 napi_value undefine;
155 napi_value handler = nullptr;
156 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
157 scope, context, work);
158 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
159 napi_get_reference_value(context->env, context->callback[0], &handler), scope, context, work);
160 if (napi_call_function(context->env, nullptr, handler, 1,
161 &jsEvent, &undefine) != napi_ok) {
162 LBSLOGE(LOCATING_DATA_CALLBACK, "Report event failed");
163 }
164 }
165 NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
166 delete context;
167 delete work;
168 });
169 }
170
OnLocatingDataChange(const std::vector<std::shared_ptr<LocatingRequiredData>> & data)171 void LocatingRequiredDataCallbackHost::OnLocatingDataChange(
172 const std::vector<std::shared_ptr<LocatingRequiredData>>& data)
173 {
174 LBSLOGD(LOCATING_DATA_CALLBACK, "LocatingRequiredDataCallbackHost::OnLocatingDataChange");
175 Send(data);
176 }
177
DeleteHandler()178 void LocatingRequiredDataCallbackHost::DeleteHandler()
179 {
180 std::unique_lock<std::mutex> guard(mutex_);
181 if (handlerCb_ == nullptr || env_ == nullptr) {
182 LBSLOGE(LOCATING_DATA_CALLBACK, "handler or env is nullptr.");
183 return;
184 }
185 auto context = new (std::nothrow) AsyncContext(env_);
186 if (context == nullptr) {
187 LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr.");
188 return;
189 }
190 context->env = env_;
191 context->callback[SUCCESS_CALLBACK] = handlerCb_;
192 DeleteQueueWork(context);
193 handlerCb_ = nullptr;
194 }
195
IsSingleLocationRequest()196 bool LocatingRequiredDataCallbackHost::IsSingleLocationRequest()
197 {
198 return (fixNumber_ == 1);
199 }
200
CountDown()201 void LocatingRequiredDataCallbackHost::CountDown()
202 {
203 if (IsSingleLocationRequest() && latch_ != nullptr) {
204 latch_->CountDown();
205 }
206 }
207
Wait(int time)208 void LocatingRequiredDataCallbackHost::Wait(int time)
209 {
210 if (IsSingleLocationRequest() && latch_ != nullptr) {
211 latch_->Wait(time);
212 }
213 }
214
GetCount()215 int LocatingRequiredDataCallbackHost::GetCount()
216 {
217 if (IsSingleLocationRequest() && latch_ != nullptr) {
218 return latch_->GetCount();
219 }
220 return 0;
221 }
222
SetCount(int count)223 void LocatingRequiredDataCallbackHost::SetCount(int count)
224 {
225 if (IsSingleLocationRequest() && latch_ != nullptr) {
226 return latch_->SetCount(count);
227 }
228 }
229
ClearSingleResult()230 void LocatingRequiredDataCallbackHost::ClearSingleResult()
231 {
232 std::unique_lock<std::mutex> guard(singleResultMutex_);
233 singleResult_.clear();
234 }
235
SetSingleResult(std::vector<std::shared_ptr<LocatingRequiredData>> singleResult)236 void LocatingRequiredDataCallbackHost::SetSingleResult(
237 std::vector<std::shared_ptr<LocatingRequiredData>> singleResult)
238 {
239 std::unique_lock<std::mutex> guard(singleResultMutex_);
240 singleResult_.assign(singleResult.begin(), singleResult.end());
241 }
242 } // namespace Location
243 } // namespace OHOS
244