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