1 /*
2 * Copyright (C) 2022 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_napi_adapter.h"
17 #include "location_log.h"
18 #include "location_napi_errcode.h"
19 #include "constant_definition.h"
20
21 namespace OHOS {
22 namespace Location {
23 auto g_locatorClient = Locator::GetInstance();
24
GetLastLocation(napi_env env,napi_callback_info info)25 napi_value GetLastLocation(napi_env env, napi_callback_info info)
26 {
27 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
28 size_t argc = MAXIMUM_JS_PARAMS;
29 napi_value argv[MAXIMUM_JS_PARAMS];
30 napi_value thisVar = nullptr;
31 void* data = nullptr;
32
33 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
34 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
35
36 #ifdef ENABLE_NAPI_MANAGER
37 return HandleGetCachedLocation(env);
38 #else
39 auto asyncContext = new (std::nothrow) LocationAsyncContext(env);
40 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
41 NAPI_CALL(env, napi_create_string_latin1(env, "getLastLocation", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
42 asyncContext->executeFunc = [&](void* data) -> void {
43 auto context = static_cast<LocationAsyncContext*>(data);
44 context->loc = g_locatorClient->IsLocationEnabled() ? g_locatorClient->GetCachedLocation() : nullptr;
45 if (context->loc != nullptr) {
46 context->errCode = SUCCESS;
47 } else {
48 context->errCode = LAST_KNOWN_LOCATION_ERROR;
49 }
50 };
51
52 asyncContext->completeFunc = [&](void* data) -> void {
53 auto context = static_cast<LocationAsyncContext*>(data);
54 NAPI_CALL_RETURN_VOID(context->env, napi_create_object(context->env, &context->result[PARAM1]));
55 if (context->loc != nullptr) {
56 LocationToJs(context->env, context->loc, context->result[PARAM1]);
57 } else {
58 LBSLOGE(LOCATOR_STANDARD, "loc is nullptr!");
59 }
60 LBSLOGI(LOCATOR_STANDARD, "Push last location result to client");
61 };
62
63 size_t objectArgsNum = 0;
64 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
65 #endif
66 }
67
68 #ifdef ENABLE_NAPI_MANAGER
HandleGetCachedLocation(napi_env env)69 napi_value HandleGetCachedLocation(napi_env env)
70 {
71 napi_value res;
72 NAPI_CALL(env, napi_create_object(env, &res));
73 LocationErrCode errorCode = CheckLocationSwitchState();
74 if (errorCode != ERRCODE_SUCCESS) {
75 HandleSyncErrCode(env, errorCode);
76 return UndefinedNapiValue(env);
77 }
78 std::unique_ptr<Location> loc;
79 errorCode = g_locatorClient->GetCachedLocationV9(loc);
80 if (loc != nullptr) {
81 LocationToJs(env, loc, res);
82 return res;
83 } else {
84 HandleSyncErrCode(env, errorCode);
85 }
86 return UndefinedNapiValue(env);
87 }
88 #endif
89
IsLocationEnabled(napi_env env,napi_callback_info info)90 napi_value IsLocationEnabled(napi_env env, napi_callback_info info)
91 {
92 size_t argc = MAXIMUM_JS_PARAMS;
93 napi_value argv[MAXIMUM_JS_PARAMS];
94 napi_value thisVar = nullptr;
95 void* data = nullptr;
96 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
97 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
98 #ifdef ENABLE_NAPI_MANAGER
99 napi_value res;
100 bool isEnabled = false;
101 LocationErrCode errorCode = g_locatorClient->IsLocationEnabledV9(isEnabled);
102 if (errorCode != ERRCODE_SUCCESS) {
103 HandleSyncErrCode(env, errorCode);
104 return UndefinedNapiValue(env);
105 }
106 NAPI_CALL(env, napi_get_boolean(env, isEnabled, &res));
107 return res;
108 #else
109 auto asyncContext = new (std::nothrow) SwitchAsyncContext(env);
110 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
111 if (napi_create_string_latin1(env, "isLocationEnabled", NAPI_AUTO_LENGTH,
112 &asyncContext->resourceName) != napi_ok) {
113 LBSLOGE(LOCATOR_STANDARD, "copy string failed");
114 }
115 asyncContext->executeFunc = [&](void* data) -> void {
116 auto context = static_cast<SwitchAsyncContext*>(data);
117 context->enable = g_locatorClient->IsLocationEnabled();
118 context->errCode = SUCCESS;
119 };
120 asyncContext->completeFunc = [&](void* data) -> void {
121 auto context = static_cast<SwitchAsyncContext*>(data);
122 NAPI_CALL_RETURN_VOID(context->env, napi_get_boolean(context->env, context->enable, &context->result[PARAM1]));
123 LBSLOGI(LOCATOR_STANDARD, "Push IsLocationEnabled result to client");
124 };
125
126 size_t objectArgsNum = 0;
127 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
128 #endif
129 }
130
EnableLocation(napi_env env,napi_callback_info info)131 napi_value EnableLocation(napi_env env, napi_callback_info info)
132 {
133 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
134 size_t argc = MAXIMUM_JS_PARAMS;
135 napi_value argv[MAXIMUM_JS_PARAMS];
136 napi_value thisVar = nullptr;
137 void* data = nullptr;
138 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
139 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
140 #ifdef ENABLE_NAPI_MANAGER
141 if (argc > PARAM1 || (argc == PARAM1 && !CheckIfParamIsFunctionType(env, argv[PARAM0]))) {
142 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
143 return UndefinedNapiValue(env);
144 }
145 #endif
146 auto asyncContext = new (std::nothrow) SwitchAsyncContext(env);
147 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
148 NAPI_CALL(env, napi_create_string_latin1(env, "enableLocation", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
149
150 asyncContext->executeFunc = [&](void* data) -> void {
151 auto context = static_cast<SwitchAsyncContext*>(data);
152 #ifdef ENABLE_NAPI_MANAGER
153 context->errCode = g_locatorClient->EnableAbilityV9(true);
154 #else
155 g_locatorClient->EnableAbility(true);
156 context->errCode = SUCCESS;
157 #endif
158 };
159
160 asyncContext->completeFunc = [&](void* data) -> void {
161 auto context = static_cast<SwitchAsyncContext*>(data);
162 #ifdef ENABLE_NAPI_MANAGER
163 NAPI_CALL_RETURN_VOID(context->env, napi_get_undefined(context->env, &context->result[PARAM1]));
164 #else
165 NAPI_CALL_RETURN_VOID(context->env,
166 napi_get_boolean(context->env, context->errCode == SUCCESS, &context->result[PARAM1]));
167 #endif
168 LBSLOGI(LOCATOR_STANDARD, "Push EnableLocation result to client");
169 };
170
171 size_t objectArgsNum = 0;
172 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
173 }
174
DisableLocation(napi_env env,napi_callback_info info)175 napi_value DisableLocation(napi_env env, napi_callback_info info)
176 {
177 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
178 size_t argc = MAXIMUM_JS_PARAMS;
179 napi_value argv[MAXIMUM_JS_PARAMS];
180 napi_value thisVar = nullptr;
181 void* data = nullptr;
182 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
183 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
184
185 #ifdef ENABLE_NAPI_MANAGER
186 LocationErrCode errorCode = g_locatorClient->EnableAbilityV9(false);
187 if (errorCode != ERRCODE_SUCCESS) {
188 HandleSyncErrCode(env, errorCode);
189 }
190 return UndefinedNapiValue(env);
191 #else
192 auto asyncContext = new (std::nothrow) SwitchAsyncContext(env);
193 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
194 NAPI_CALL(env, napi_create_string_latin1(env, "disableLocation", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
195 asyncContext->executeFunc = [&](void* data) -> void {
196 auto context = static_cast<SwitchAsyncContext*>(data);
197 g_locatorClient->EnableAbility(false);
198 context->errCode = SUCCESS;
199 };
200 asyncContext->completeFunc = [&](void* data) -> void {
201 auto context = static_cast<SwitchAsyncContext*>(data);
202 NAPI_CALL_RETURN_VOID(context->env,
203 napi_get_boolean(context->env, context->errCode == SUCCESS, &context->result[PARAM1]));
204 LBSLOGI(LOCATOR_STANDARD, "Push DisableLocation result to client");
205 };
206 size_t objectArgsNum = 0;
207 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
208 #endif
209 }
210
RequestEnableLocation(napi_env env,napi_callback_info info)211 napi_value RequestEnableLocation(napi_env env, napi_callback_info info)
212 {
213 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
214 size_t argc = MAXIMUM_JS_PARAMS;
215 napi_value argv[MAXIMUM_JS_PARAMS];
216 napi_value thisVar = nullptr;
217 void* data = nullptr;
218 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
219 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
220
221 auto asyncContext = new (std::nothrow) SwitchAsyncContext(env);
222 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
223 NAPI_CALL(env, napi_create_string_latin1(env, "enableLocation", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
224
225 asyncContext->executeFunc = [&](void* data) -> void {
226 auto context = static_cast<SwitchAsyncContext*>(data);
227 if (!g_locatorClient->IsLocationEnabled()) {
228 g_locatorClient->ShowNotification();
229 }
230 g_locatorClient->EnableAbility(true);
231 context->errCode = SUCCESS;
232 };
233
234 asyncContext->completeFunc = [&](void* data) -> void {
235 auto context = static_cast<SwitchAsyncContext*>(data);
236 NAPI_CALL_RETURN_VOID(context->env,
237 napi_get_boolean(context->env, context->errCode == SUCCESS, &context->result[PARAM1]));
238 LBSLOGI(LOCATOR_STANDARD, "Push RequestEnableLocation result to client");
239 };
240
241 size_t objectArgsNum = 0;
242 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
243 }
244
IsGeoServiceAvailable(napi_env env,napi_callback_info info)245 napi_value IsGeoServiceAvailable(napi_env env, napi_callback_info info)
246 {
247 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
248 size_t argc = MAXIMUM_JS_PARAMS;
249 napi_value argv[MAXIMUM_JS_PARAMS];
250 napi_value thisVar = nullptr;
251 void* data = nullptr;
252 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
253 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
254 #ifdef ENABLE_NAPI_MANAGER
255 napi_value res;
256 bool isAvailable = false;
257 LocationErrCode errorCode = g_locatorClient->IsGeoServiceAvailableV9(isAvailable);
258 if (errorCode != ERRCODE_SUCCESS) {
259 HandleSyncErrCode(env, errorCode);
260 return UndefinedNapiValue(env);
261 }
262 NAPI_CALL(env, napi_get_boolean(env, isAvailable, &res));
263 return res;
264 #else
265 auto asyncContext = new (std::nothrow) SwitchAsyncContext(env);
266 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
267 NAPI_CALL(env,
268 napi_create_string_latin1(env, "isGeoServiceAvailable", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
269 asyncContext->executeFunc = [&](void* data) -> void {
270 auto context = static_cast<SwitchAsyncContext*>(data);
271 bool isAvailable = g_locatorClient->IsGeoServiceAvailable();
272 context->enable = isAvailable;
273 context->errCode = SUCCESS;
274 };
275 asyncContext->completeFunc = [&](void* data) -> void {
276 auto context = static_cast<SwitchAsyncContext*>(data);
277 NAPI_CALL_RETURN_VOID(context->env, napi_get_boolean(context->env, context->enable, &context->result[PARAM1]));
278 LBSLOGI(LOCATOR_STANDARD, "Push isGeoServiceAvailable result to client");
279 };
280 size_t objectArgsNum = 0;
281 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
282 #endif
283 }
284
CreateReverseGeocodeAsyncContext(ReverseGeoCodeAsyncContext * asyncContext)285 void CreateReverseGeocodeAsyncContext(ReverseGeoCodeAsyncContext* asyncContext)
286 {
287 asyncContext->executeFunc = [&](void* data) -> void {
288 auto context = static_cast<ReverseGeoCodeAsyncContext*>(data);
289 #ifdef ENABLE_NAPI_MANAGER
290 if (context->errCode != ERRCODE_SUCCESS) {
291 #else
292 if (context->errCode != SUCCESS) {
293 #endif
294 return;
295 }
296 #ifdef ENABLE_NAPI_MANAGER
297 bool isAvailable = false;
298 LocationErrCode errorCode = g_locatorClient->IsGeoServiceAvailableV9(isAvailable);
299 if (errorCode != ERRCODE_SUCCESS) {
300 context->errCode = errorCode;
301 return;
302 }
303 if (!isAvailable) {
304 context->errCode = ERRCODE_REVERSE_GEOCODING_FAIL;
305 return;
306 }
307 errorCode = g_locatorClient->GetAddressByCoordinateV9(context->reverseGeoCodeRequest, context->replyList);
308 if (context->replyList.empty() || errorCode != ERRCODE_SUCCESS) {
309 context->errCode = errorCode;
310 }
311 #else
312 if (!g_locatorClient->IsGeoServiceAvailable()) {
313 context->errCode = REVERSE_GEOCODE_ERROR;
314 return;
315 }
316 g_locatorClient->GetAddressByCoordinate(context->reverseGeoCodeRequest, context->replyList);
317 if (context->replyList.empty()) {
318 context->errCode = REVERSE_GEOCODE_ERROR;
319 }
320 #endif
321 };
322 asyncContext->completeFunc = [&](void* data) -> void {
323 auto context = static_cast<ReverseGeoCodeAsyncContext*>(data);
324 NAPI_CALL_RETURN_VOID(context->env,
325 napi_create_array_with_length(context->env, context->replyList.size(), &context->result[PARAM1]));
326 GeoAddressesToJsObj(context->env, context->replyList, context->result[PARAM1]);
327 };
328 }
329
330 void CreateGeocodeAsyncContext(GeoCodeAsyncContext* asyncContext)
331 {
332 asyncContext->executeFunc = [&](void* data) -> void {
333 auto context = static_cast<GeoCodeAsyncContext*>(data);
334 if (context->errCode != SUCCESS) {
335 return;
336 }
337 #ifdef ENABLE_NAPI_MANAGER
338 bool isAvailable = false;
339 LocationErrCode errorCode = g_locatorClient->IsGeoServiceAvailableV9(isAvailable);
340 if (errorCode != ERRCODE_SUCCESS) {
341 context->errCode = errorCode;
342 return;
343 }
344 if (!isAvailable) {
345 context->errCode = ERRCODE_GEOCODING_FAIL;
346 return;
347 }
348 errorCode = g_locatorClient->GetAddressByLocationNameV9(context->geoCodeRequest, context->replyList);
349 if (context->replyList.empty() || errorCode != ERRCODE_SUCCESS) {
350 context->errCode = errorCode;
351 }
352 #else
353 if (!g_locatorClient->IsGeoServiceAvailable()) {
354 context->errCode = GEOCODE_ERROR;
355 return;
356 }
357 g_locatorClient->GetAddressByLocationName(context->geoCodeRequest, context->replyList);
358 if (context->replyList.empty()) {
359 context->errCode = GEOCODE_ERROR;
360 }
361 #endif
362 };
363 asyncContext->completeFunc = [&](void* data) -> void {
364 auto context = static_cast<GeoCodeAsyncContext*>(data);
365 NAPI_CALL_RETURN_VOID(context->env,
366 napi_create_array_with_length(context->env, context->replyList.size(), &context->result[PARAM1]));
367 GeoAddressesToJsObj(context->env, context->replyList, context->result[PARAM1]);
368 LBSLOGI(LOCATOR_STANDARD, "Push GetAddressesFromLocationName result to client");
369 };
370 }
371
372 napi_value GetAddressesFromLocation(napi_env env, napi_callback_info info)
373 {
374 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
375 size_t argc = MAXIMUM_JS_PARAMS;
376 napi_value argv[MAXIMUM_JS_PARAMS];
377 napi_value thisVar = nullptr;
378 void* data = nullptr;
379 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
380 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
381 #ifdef ENABLE_NAPI_MANAGER
382 if (argc < PARAM1 || argc > PARAM2 || (argc == PARAM2 && !CheckIfParamIsFunctionType(env, argv[1]))) {
383 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
384 return UndefinedNapiValue(env);
385 }
386 #else
387 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
388 #endif
389
390 napi_valuetype valueType;
391 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
392 #ifdef ENABLE_NAPI_MANAGER
393 if (valueType != napi_object) {
394 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
395 return UndefinedNapiValue(env);
396 }
397 #else
398 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1.");
399 #endif
400 auto asyncContext = new (std::nothrow) ReverseGeoCodeAsyncContext(env);
401 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
402 NAPI_CALL(env, napi_create_string_latin1(env, "getAddressesFromLocation",
403 NAPI_AUTO_LENGTH, &asyncContext->resourceName));
404 bool ret = JsObjToReverseGeoCodeRequest(env, argv[0], asyncContext->reverseGeoCodeRequest);
405 #ifdef ENABLE_NAPI_MANAGER
406 asyncContext->errCode = ret ? ERRCODE_SUCCESS : ERRCODE_INVALID_PARAM;
407 #else
408 asyncContext->errCode = ret ? SUCCESS : INPUT_PARAMS_ERROR;
409 #endif
410 #ifdef ENABLE_NAPI_MANAGER
411 if (asyncContext->errCode != SUCCESS) {
412 int code = asyncContext->errCode;
413 delete asyncContext;
414 asyncContext = nullptr;
415 HandleSyncErrCode(env, code);
416 return UndefinedNapiValue(env);
417 }
418 #endif
419 CreateReverseGeocodeAsyncContext(asyncContext);
420
421 size_t objectArgsNum = 1;
422 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
423 }
424
425 napi_value GetAddressesFromLocationName(napi_env env, napi_callback_info info)
426 {
427 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
428 size_t argc = MAXIMUM_JS_PARAMS;
429 napi_value argv[MAXIMUM_JS_PARAMS];
430 napi_value thisVar = nullptr;
431 void* data = nullptr;
432 NAPI_ASSERT(env, g_locatorClient != nullptr, "get locator SA failed");
433 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
434 #ifdef ENABLE_NAPI_MANAGER
435 if (argc < PARAM1 || argc > PARAM2 || (argc == PARAM2 && !CheckIfParamIsFunctionType(env, argv[1]))) {
436 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
437 return UndefinedNapiValue(env);
438 }
439 #else
440 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
441 #endif
442
443 napi_valuetype valueType;
444 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
445 #ifdef ENABLE_NAPI_MANAGER
446 if (valueType != napi_object) {
447 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
448 return UndefinedNapiValue(env);
449 }
450 #else
451 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1.");
452 #endif
453 auto asyncContext = new (std::nothrow) GeoCodeAsyncContext(env);
454 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
455 NAPI_CALL(env,
456 napi_create_string_latin1(env, "GetAddressesFromLocationName", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
457 asyncContext->errCode = JsObjToGeoCodeRequest(env, argv[0], asyncContext->geoCodeRequest);
458 #ifdef ENABLE_NAPI_MANAGER
459 if (asyncContext->errCode == INPUT_PARAMS_ERROR) {
460 delete asyncContext;
461 asyncContext = nullptr;
462 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
463 return UndefinedNapiValue(env);
464 }
465 #endif
466 CreateGeocodeAsyncContext(asyncContext);
467 size_t objectArgsNum = 1;
468 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
469 }
470
471 #ifdef ENABLE_NAPI_MANAGER
472 napi_value IsLocationPrivacyConfirmed(napi_env env, napi_callback_info info)
473 {
474 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
475 size_t argc = MAXIMUM_JS_PARAMS;
476 napi_value argv[MAXIMUM_JS_PARAMS];
477 napi_value thisVar = nullptr;
478 void* data = nullptr;
479 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
480 if (g_locatorClient == nullptr) {
481 HandleSyncErrCode(env, ERRCODE_SERVICE_UNAVAILABLE);
482 return UndefinedNapiValue(env);
483 }
484 // 1 arguement is necessary
485 if (argc != PARAM1) {
486 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
487 return UndefinedNapiValue(env);
488 }
489 napi_valuetype valueType;
490 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
491 if (valueType != napi_number) {
492 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
493 return UndefinedNapiValue(env);
494 }
495 int type;
496 NAPI_CALL(env, napi_get_value_int32(env, argv[0], &type));
497 napi_value res;
498 bool isEnabled = false;
499 LocationErrCode errorCode = g_locatorClient->IsLocationPrivacyConfirmedV9(type, isEnabled);
500 if (errorCode != ERRCODE_SUCCESS) {
501 HandleSyncErrCode(env, errorCode);
502 return UndefinedNapiValue(env);
503 }
504 NAPI_CALL(env, napi_get_boolean(env, isEnabled, &res));
505 return res;
506 }
507
508 napi_value SetLocationPrivacyConfirmStatus(napi_env env, napi_callback_info info)
509 {
510 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
511 size_t argc = MAXIMUM_JS_PARAMS;
512 napi_value argv[MAXIMUM_JS_PARAMS];
513 napi_value thisVar = nullptr;
514 void* data = nullptr;
515 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
516 if (g_locatorClient == nullptr) {
517 HandleSyncErrCode(env, ERRCODE_SERVICE_UNAVAILABLE);
518 return UndefinedNapiValue(env);
519 }
520 // 2 arguement is necessary
521 if (argc != PARAM2) {
522 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
523 return UndefinedNapiValue(env);
524 }
525 napi_valuetype valueType1;
526 napi_valuetype valueType2;
527 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType1));
528 NAPI_CALL(env, napi_typeof(env, argv[1], &valueType2));
529 if (valueType1 != napi_number || valueType2 != napi_boolean) {
530 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
531 return UndefinedNapiValue(env);
532 }
533 int type;
534 NAPI_CALL(env, napi_get_value_int32(env, argv[0], &type));
535 bool isConfirmed;
536 NAPI_CALL(env, napi_get_value_bool(env, argv[1], &isConfirmed));
537 LocationErrCode errorCode = g_locatorClient->SetLocationPrivacyConfirmStatusV9(type, isConfirmed);
538 if (errorCode != ERRCODE_SUCCESS) {
539 HandleSyncErrCode(env, errorCode);
540 }
541 return UndefinedNapiValue(env);
542 }
543 #endif
544
545 napi_value GetCachedGnssLocationsSize(napi_env env, napi_callback_info info)
546 {
547 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
548 size_t argc = MAXIMUM_JS_PARAMS;
549 napi_value argv[MAXIMUM_JS_PARAMS];
550 napi_value thisVar = nullptr;
551 void* data = nullptr;
552 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
553 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
554 #ifdef ENABLE_NAPI_MANAGER
555 if (argc > PARAM1 || (argc == PARAM1 && !CheckIfParamIsFunctionType(env, argv[PARAM0]))) {
556 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
557 return UndefinedNapiValue(env);
558 }
559 #endif
560 auto asyncContext = new (std::nothrow) CachedAsyncContext(env);
561 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
562 NAPI_CALL(env,
563 napi_create_string_latin1(env, "GetCachedGnssLocationsSize", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
564
565 asyncContext->executeFunc = [&](void* data) -> void {
566 auto context = static_cast<CachedAsyncContext*>(data);
567 #ifdef ENABLE_NAPI_MANAGER
568 LocationErrCode errorCode = CheckLocationSwitchState();
569 if (errorCode != ERRCODE_SUCCESS) {
570 context->errCode = errorCode;
571 return;
572 }
573 #endif
574
575 #ifdef ENABLE_NAPI_MANAGER
576 int size = -1;
577 g_locatorClient->GetCachedGnssLocationsSizeV9(size);
578 context->errCode = ERRCODE_NOT_SUPPORTED;
579 context->locationSize = size;
580 #else
581 context->locationSize = g_locatorClient->GetCachedGnssLocationsSize();
582 context->errCode = (context->locationSize >= 0) ? SUCCESS : NOT_SUPPORTED;
583 #endif
584 };
585 asyncContext->completeFunc = [&](void* data) -> void {
586 auto context = static_cast<CachedAsyncContext*>(data);
587 NAPI_CALL_RETURN_VOID(context->env,
588 napi_create_int32(context->env, context->locationSize, &context->result[PARAM1]));
589 LBSLOGI(LOCATOR_STANDARD, "Push GetCachedGnssLocationsSize result to client");
590 };
591
592 size_t objectArgsNum = 0;
593 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
594 }
595
596 napi_value FlushCachedGnssLocations(napi_env env, napi_callback_info info)
597 {
598 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
599 size_t argc = MAXIMUM_JS_PARAMS;
600 napi_value argv[MAXIMUM_JS_PARAMS];
601 napi_value thisVar = nullptr;
602 void* data = nullptr;
603 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
604 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
605 #ifdef ENABLE_NAPI_MANAGER
606 if (argc > PARAM1 || (argc == PARAM1 && !CheckIfParamIsFunctionType(env, argv[PARAM0]))) {
607 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
608 return UndefinedNapiValue(env);
609 }
610 #endif
611 auto asyncContext = new (std::nothrow) CachedAsyncContext(env);
612 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
613 NAPI_CALL(env,
614 napi_create_string_latin1(env, "FlushCachedGnssLocations", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
615
616 asyncContext->executeFunc = [&](void* data) -> void {
617 auto context = static_cast<CachedAsyncContext*>(data);
618 #ifdef ENABLE_NAPI_MANAGER
619 LocationErrCode errorCode = CheckLocationSwitchState();
620 if (errorCode != ERRCODE_SUCCESS) {
621 context->errCode = errorCode;
622 return;
623 }
624 g_locatorClient->FlushCachedGnssLocationsV9();
625 context->errCode = ERRCODE_NOT_SUPPORTED;
626 #else
627 if (g_locatorClient->IsLocationEnabled()) {
628 g_locatorClient->FlushCachedGnssLocations();
629 }
630 context->errCode = NOT_SUPPORTED;
631 #endif
632 };
633
634 asyncContext->completeFunc = [&](void* data) -> void {
635 auto context = static_cast<CachedAsyncContext*>(data);
636 #ifdef ENABLE_NAPI_MANAGER
637 NAPI_CALL_RETURN_VOID(context->env, napi_get_undefined(context->env, &context->result[PARAM1]));
638 #else
639 NAPI_CALL_RETURN_VOID(context->env,
640 napi_get_boolean(context->env, context->errCode == SUCCESS, &context->result[PARAM1]));
641 #endif
642 LBSLOGI(LOCATOR_STANDARD, "Push FlushCachedGnssLocations result to client");
643 };
644
645 size_t objectArgsNum = 0;
646 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
647 }
648
649 void CreateCommandAsyncContext(CommandAsyncContext* asyncContext)
650 {
651 asyncContext->executeFunc = [&](void* data) -> void {
652 auto context = static_cast<CommandAsyncContext*>(data);
653 #ifdef ENABLE_NAPI_MANAGER
654 if (context->command != nullptr) {
655 context->errCode = g_locatorClient->SendCommandV9(context->command);
656 }
657 #else
658 if (context->command != nullptr) {
659 g_locatorClient->SendCommand(context->command);
660 }
661 context->errCode = NOT_SUPPORTED;
662 #endif
663 };
664 asyncContext->completeFunc = [&](void* data) -> void {
665 auto context = static_cast<CommandAsyncContext*>(data);
666 #ifdef ENABLE_NAPI_MANAGER
667 NAPI_CALL_RETURN_VOID(context->env, napi_get_undefined(context->env, &context->result[PARAM1]));
668 #else
669 NAPI_CALL_RETURN_VOID(context->env, napi_get_boolean(context->env,
670 context->enable, &context->result[PARAM1]));
671 #endif
672 LBSLOGI(LOCATOR_STANDARD, "Push SendCommand result to client");
673 };
674 }
675
676 napi_value SendCommand(napi_env env, napi_callback_info info)
677 {
678 size_t argc = MAXIMUM_JS_PARAMS;
679 napi_value argv[MAXIMUM_JS_PARAMS];
680 napi_value thisVar = nullptr;
681 void* data = nullptr;
682 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
683 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
684 #ifdef ENABLE_NAPI_MANAGER
685 if (argc < PARAM1 || argc > PARAM2 || (argc == PARAM2 && !CheckIfParamIsFunctionType(env, argv[1]))) {
686 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
687 return UndefinedNapiValue(env);
688 }
689 #else
690 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
691 #endif
692
693 napi_valuetype valueType;
694 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
695 #ifdef ENABLE_NAPI_MANAGER
696 if (valueType != napi_object) {
697 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
698 return UndefinedNapiValue(env);
699 }
700 #else
701 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1.");
702 #endif
703
704 #ifdef ENABLE_NAPI_MANAGER
705 if (argc == PARAM2 && !CheckIfParamIsFunctionType(env, argv[PARAM1])) {
706 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
707 return UndefinedNapiValue(env);
708 }
709 #endif
710 auto asyncContext = new (std::nothrow) CommandAsyncContext(env);
711 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
712 asyncContext->command = std::make_unique<LocationCommand>();
713 NAPI_CALL(env, napi_create_string_latin1(env, "SendCommand", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
714
715 int errCode = JsObjToCommand(env, argv[0], asyncContext->command);
716 #ifdef ENABLE_NAPI_MANAGER
717 if (errCode == INPUT_PARAMS_ERROR) {
718 delete asyncContext;
719 asyncContext = nullptr;
720 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
721 return UndefinedNapiValue(env);
722 }
723 #else
724 NAPI_ASSERT(env, errCode != INPUT_PARAMS_ERROR, "The input params should be checked first.");
725 #endif
726 CreateCommandAsyncContext(asyncContext);
727 size_t objectArgsNum = 1;
728 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
729 }
730
731 #ifdef ENABLE_NAPI_MANAGER
732 napi_value GetIsoCountryCode(napi_env env, napi_callback_info info)
733 {
734 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
735 size_t argc = MAXIMUM_JS_PARAMS;
736 napi_value argv[MAXIMUM_JS_PARAMS];
737 napi_value thisVar = nullptr;
738 void *data = nullptr;
739 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
740 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
741 if (argc > PARAM1 || (argc == PARAM1 && !CheckIfParamIsFunctionType(env, argv[PARAM0]))) {
742 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
743 return UndefinedNapiValue(env);
744 }
745 CountryCodeContext *asyncContext = new (std::nothrow) CountryCodeContext(env);
746 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
747 if (napi_create_string_latin1(env, "CountryCodeContext", NAPI_AUTO_LENGTH,
748 &asyncContext->resourceName) != napi_ok) {
749 LBSLOGE(LOCATOR_STANDARD, "copy string failed");
750 }
751 asyncContext->executeFunc = [&](void *data) -> void {
752 if (data == nullptr) {
753 LBSLOGE(LOCATOR_STANDARD, "GetIsoCountryCode data == nullptr");
754 return;
755 }
756 CountryCodeContext *context = static_cast<CountryCodeContext*>(data);
757 std::shared_ptr<CountryCode> country = std::make_shared<CountryCode>();
758 LocationErrCode errorCode = g_locatorClient->GetIsoCountryCodeV9(country);
759 context->errCode = errorCode;
760 if (errorCode == ERRCODE_SUCCESS) {
761 context->country = country;
762 }
763 };
764 asyncContext->completeFunc = [&](void *data) -> void {
765 if (data == nullptr) {
766 LBSLOGE(LOCATOR_STANDARD, "GetIsoCountryCode data == nullptr");
767 return;
768 }
769 CountryCodeContext *context = static_cast<CountryCodeContext *>(data);
770 NAPI_CALL_RETURN_VOID(context->env, napi_create_object(context->env, &context->result[PARAM1]));
771 if (context->country) {
772 CountryCodeToJs(context->env, context->country, context->result[PARAM1]);
773 } else {
774 LBSLOGE(LOCATOR_STANDARD, "country is nullptr!");
775 }
776 LBSLOGI(LOCATOR_STANDARD, "Push GetIsoCountryCode result to client");
777 };
778
779 size_t objectArgsNum = 0;
780 return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum);
781 }
782
783 int ParseLocationMockParams(napi_env env, LocationMockAsyncContext *asyncContext, napi_value object)
784 {
785 CHK_ERROR_CODE("timeInterval", JsObjectToInt(env, object, "timeInterval", asyncContext->timeInterval), true);
786 bool result = false;
787 napi_value value = nullptr;
788 NAPI_CALL_BASE(env, napi_has_named_property(env, object, "locations", &result), false);
789 if (result) {
790 NAPI_CALL_BASE(env, napi_get_named_property(env, object, "locations", &value), false);
791 bool isArray = false;
792 NAPI_CALL_BASE(env, napi_is_array(env, value, &isArray), false);
793 if (!isArray) {
794 LBSLOGE(LOCATOR_STANDARD, "not an array!");
795 return INPUT_PARAMS_ERROR;
796 }
797 GetLocationArray(env, asyncContext, value);
798 }
799 return SUCCESS;
800 }
801
802 napi_value EnableLocationMock(napi_env env, napi_callback_info info)
803 {
804 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
805 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
806 LocationErrCode errorCode = CheckLocationSwitchState();
807 if (errorCode != ERRCODE_SUCCESS) {
808 HandleSyncErrCode(env, errorCode);
809 return UndefinedNapiValue(env);
810 }
811 errorCode = g_locatorClient->EnableLocationMockV9();
812 if (errorCode != ERRCODE_SUCCESS) {
813 HandleSyncErrCode(env, errorCode);
814 }
815 return UndefinedNapiValue(env);
816 }
817
818 napi_value DisableLocationMock(napi_env env, napi_callback_info info)
819 {
820 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
821 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
822 LocationErrCode errorCode = CheckLocationSwitchState();
823 if (errorCode != ERRCODE_SUCCESS) {
824 HandleSyncErrCode(env, errorCode);
825 return UndefinedNapiValue(env);
826 }
827 errorCode = g_locatorClient->DisableLocationMockV9();
828 if (errorCode != ERRCODE_SUCCESS) {
829 HandleSyncErrCode(env, errorCode);
830 }
831 return UndefinedNapiValue(env);
832 }
833
834 napi_value SetMockedLocations(napi_env env, napi_callback_info info)
835 {
836 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
837 size_t argc = MAXIMUM_JS_PARAMS;
838 napi_value argv[MAXIMUM_JS_PARAMS];
839 napi_value thisVar = nullptr;
840 void *data = nullptr;
841 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
842 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
843 if (argc != PARAM1) {
844 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
845 return UndefinedNapiValue(env);
846 }
847 napi_valuetype valueType;
848 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
849 if (valueType != napi_object) {
850 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
851 return UndefinedNapiValue(env);
852 }
853
854 LocationMockAsyncContext *asyncContext = new (std::nothrow) LocationMockAsyncContext(env);
855 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
856 NAPI_CALL(env, napi_create_string_latin1(env,
857 "SetMockedLocations", NAPI_AUTO_LENGTH, &asyncContext->resourceName));
858 asyncContext->errCode = ParseLocationMockParams(env, asyncContext, argv[0]);
859 if (asyncContext->errCode == INPUT_PARAMS_ERROR) {
860 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
861 return UndefinedNapiValue(env);
862 }
863 LocationErrCode errorCode = CheckLocationSwitchState();
864 if (errorCode != ERRCODE_SUCCESS) {
865 HandleSyncErrCode(env, errorCode);
866 return UndefinedNapiValue(env);
867 }
868 errorCode = g_locatorClient->SetMockedLocationsV9(asyncContext->timeInterval, asyncContext->LocationNapi);
869 if (errorCode != ERRCODE_SUCCESS) {
870 HandleSyncErrCode(env, errorCode);
871 }
872 return UndefinedNapiValue(env);
873 }
874
875 napi_value EnableReverseGeocodingMock(napi_env env, napi_callback_info info)
876 {
877 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
878 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
879 LocationErrCode errorCode = g_locatorClient->EnableReverseGeocodingMockV9();
880 if (errorCode != ERRCODE_SUCCESS) {
881 HandleSyncErrCode(env, errorCode);
882 }
883 return UndefinedNapiValue(env);
884 }
885
886 napi_value DisableReverseGeocodingMock(napi_env env, napi_callback_info info)
887 {
888 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
889 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
890 LocationErrCode errorCode = g_locatorClient->DisableReverseGeocodingMockV9();
891 if (errorCode != ERRCODE_SUCCESS) {
892 HandleSyncErrCode(env, errorCode);
893 }
894 return UndefinedNapiValue(env);
895 }
896
897 napi_value SetReverseGeocodingMockInfo(napi_env env, napi_callback_info info)
898 {
899 LBSLOGI(LOCATOR_STANDARD, "%{public}s called.", __func__);
900 size_t argc = MAXIMUM_JS_PARAMS;
901 napi_value argv[MAXIMUM_JS_PARAMS];
902 napi_value thisVar = nullptr;
903 void *data = nullptr;
904 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
905 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
906 if (argc != PARAM1) {
907 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
908 return UndefinedNapiValue(env);
909 }
910
911 bool isArray = false;
912 NAPI_CALL(env, napi_is_array(env, argv[0], &isArray));
913 if (!isArray) {
914 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
915 return UndefinedNapiValue(env);
916 }
917 std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
918 JsObjToRevGeocodeMock(env, argv[0], mockInfo);
919 LocationErrCode errorCode = g_locatorClient->SetReverseGeocodingMockInfoV9(mockInfo);
920 if (errorCode != ERRCODE_SUCCESS) {
921 HandleSyncErrCode(env, errorCode);
922 }
923 return UndefinedNapiValue(env);
924 }
925 #endif
926
927 #ifdef ENABLE_NAPI_MANAGER
928 LocationErrCode CheckLocationSwitchState()
929 {
930 bool isEnabled = false;
931 LocationErrCode errorCode = g_locatorClient->IsLocationEnabledV9(isEnabled);
932 if (errorCode != ERRCODE_SUCCESS) {
933 return errorCode;
934 }
935 if (!isEnabled) {
936 return ERRCODE_SWITCH_OFF;
937 }
938 return ERRCODE_SUCCESS;
939 }
940
941 sptr<LocatingRequiredDataCallbackHost> CreateSingleCallbackHost()
942 {
943 auto callbackHost =
944 sptr<LocatingRequiredDataCallbackHost>(new (std::nothrow) LocatingRequiredDataCallbackHost());
945 if (callbackHost) {
946 callbackHost->SetFixNumber(1);
947 }
948 return callbackHost;
949 }
950
951 SingleScanAsyncContext* CreateSingleScanAsyncContext(const napi_env& env,
952 std::unique_ptr<LocatingRequiredDataConfig>& config, sptr<LocatingRequiredDataCallbackHost> callback)
953 {
954 auto asyncContext = new (std::nothrow) SingleScanAsyncContext(env);
955 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
956 NAPI_CALL(env, napi_create_string_latin1(env, "getLocatingRequiredDataOnce",
957 NAPI_AUTO_LENGTH, &asyncContext->resourceName));
958 asyncContext->timeout_ = config->GetScanTimeoutMs();
959 asyncContext->callbackHost_ = callback;
960 asyncContext->executeFunc = [&](void* data) -> void {
961 if (data == nullptr) {
962 LBSLOGE(LOCATOR_STANDARD, "data is nullptr!");
963 return;
964 }
965 auto context = static_cast<SingleScanAsyncContext*>(data);
966 auto callbackHost = context->callbackHost_;
967 if (callbackHost != nullptr) {
968 callbackHost->Wait(context->timeout_);
969 auto callbackPtr = sptr<ILocatingRequiredDataCallback>(callbackHost);
970 g_locatorClient->UnRegisterLocatingRequiredDataCallback(callbackPtr);
971 if (callbackHost->GetCount() != 0) {
972 context->errCode = ERRCODE_SCAN_FAIL;
973 }
974 callbackHost->SetCount(1);
975 }
976 };
977 asyncContext->completeFunc = [&](void* data) -> void {
978 if (data == nullptr) {
979 LBSLOGE(LOCATOR_STANDARD, "data is nullptr!");
980 return;
981 }
982 auto context = static_cast<SingleScanAsyncContext*>(data);
983
984 auto callbackHost = context->callbackHost_;
985 if (callbackHost != nullptr) {
986 std::vector<std::shared_ptr<LocatingRequiredData>> res = callbackHost->GetSingleResult();
987 napi_create_array_with_length(context->env, res.size(), &context->result[PARAM1]);
988 LocatingRequiredDataToJsObj(context->env, res, context->result[PARAM1]);
989 callbackHost->ClearSingleResult();
990 } else {
991 LBSLOGE(LOCATOR_STANDARD, "m_singleLocation is nullptr!");
992 }
993 if (context->callbackHost_) {
994 context->callbackHost_ = nullptr;
995 }
996 LBSLOGI(LOCATOR_STANDARD, "Push scan info to client");
997 };
998 return asyncContext;
999 }
1000
1001 napi_value GetLocatingRequiredData(napi_env env, napi_callback_info info)
1002 {
1003 LBSLOGD(LOCATOR_STANDARD, "%{public}s called.", __func__);
1004 size_t argc = MAXIMUM_JS_PARAMS;
1005 napi_value argv[MAXIMUM_JS_PARAMS];
1006 napi_value thisVar = nullptr;
1007 void* data = nullptr;
1008 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
1009 NAPI_ASSERT(env, g_locatorClient != nullptr, "locator instance is null.");
1010 if (argc > PARAM1 || (argc == PARAM1 && !CheckIfParamIsObjectType(env, argv[PARAM0]))) {
1011 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
1012 return UndefinedNapiValue(env);
1013 }
1014 if (argc == PARAM1) {
1015 napi_valuetype valueType;
1016 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
1017 if (valueType != napi_object) {
1018 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
1019 return UndefinedNapiValue(env);
1020 }
1021 }
1022
1023 auto singleCallbackHost = CreateSingleCallbackHost();
1024 if (singleCallbackHost == nullptr) {
1025 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
1026 return UndefinedNapiValue(env);
1027 }
1028 std::unique_ptr<LocatingRequiredDataConfig> requestConfig = std::make_unique<LocatingRequiredDataConfig>();
1029 JsObjToLocatingRequiredDataConfig(env, argv[0], requestConfig);
1030 auto callbackPtr = sptr<ILocatingRequiredDataCallback>(singleCallbackHost);
1031 LocationErrCode errorCode = g_locatorClient->RegisterLocatingRequiredDataCallback(requestConfig, callbackPtr);
1032 if (errorCode != ERRCODE_SUCCESS) {
1033 HandleSyncErrCode(env, errorCode);
1034 return UndefinedNapiValue(env);
1035 }
1036
1037 auto asyncContext = CreateSingleScanAsyncContext(env, requestConfig, singleCallbackHost);
1038 if (asyncContext == nullptr) {
1039 HandleSyncErrCode(env, ERRCODE_INVALID_PARAM);
1040 return UndefinedNapiValue(env);
1041 }
1042 return DoAsyncWork(env, asyncContext, argc, argv, 1);
1043 }
1044 #endif
1045 } // namespace Location
1046 } // namespace OHOS
1047