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