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 "napi_geolocation_permission.h"
17
18 #include <cstdint>
19 #include <vector>
20
21 #include "business_error.h"
22 #include "nweb_napi_scope.h"
23 #include "napi/native_common.h"
24 #include "nweb_data_base.h"
25 #include "nweb_helper.h"
26 #include "web_errors.h"
27 #include "securec.h"
28
29 namespace {
30 constexpr int32_t PARAMZERO = 0;
31 constexpr int32_t PARAMONE = 1;
32 constexpr int32_t PARAMTWO = 2;
33 constexpr int32_t PARAMTHREE = 3;
34 constexpr int32_t RESULT_COUNT = 2;
35 constexpr int32_t INTERFACE_OK = 0;
36 constexpr int32_t INTERFACE_ERROR = -1;
37 constexpr int32_t ALLOW_PERMISSION_OPERATION = 1;
38 constexpr int32_t DELETE_PERMISSION_OPERATION = 2;
39
40 struct GetPermissionOriginsParam {
41 std::vector<std::string> origins;
42 napi_env env;
43 napi_async_work asyncWork;
44 napi_deferred deferred;
45 napi_ref callbackRef;
46 napi_status status;
47 int errCode;
48 bool incognitoMode;
49 };
50
51 struct GetOriginPermissionStateParam {
52 bool retValue;
53 bool incognitoMode;
54 std::string origin;
55 napi_env env;
56 napi_async_work asyncWork;
57 napi_deferred deferred;
58 napi_ref jsStringRef;
59 napi_ref callbackRef;
60 napi_status status;
61 int errCode;
62 };
63 } // namespace
64
65 namespace OHOS {
66 namespace NWeb {
Init(napi_env env,napi_value exports)67 napi_value NapiGeolocationPermission::Init(napi_env env, napi_value exports)
68 {
69 const std::string GEOLOCATION_PERMISSION_CLASS_NAME = "GeolocationPermissions";
70 napi_property_descriptor properties[] = {
71 DECLARE_NAPI_STATIC_FUNCTION("allowGeolocation", NapiGeolocationPermission::JsAllowGeolocation),
72 DECLARE_NAPI_STATIC_FUNCTION("deleteGeolocation", NapiGeolocationPermission::JsDeleteGeolocation),
73 DECLARE_NAPI_STATIC_FUNCTION("deleteAllGeolocation", NapiGeolocationPermission::JsDeleteAllGeolocation),
74 DECLARE_NAPI_STATIC_FUNCTION("getStoredGeolocation", NapiGeolocationPermission::JsGetStoredGeolocation),
75 DECLARE_NAPI_STATIC_FUNCTION("getAccessibleGeolocation",
76 NapiGeolocationPermission::JsGetAccessibleGeolocation),
77 };
78 napi_value constructor = nullptr;
79 napi_define_class(env, GEOLOCATION_PERMISSION_CLASS_NAME.c_str(), GEOLOCATION_PERMISSION_CLASS_NAME.length(),
80 JsConstructor, nullptr, sizeof(properties) / sizeof(properties[0]), properties, &constructor);
81 NAPI_ASSERT(env, constructor != nullptr, "NapiGeolocationPermission define js class failed");
82 napi_status status = napi_set_named_property(env, exports, "GeolocationPermissions", constructor);
83 NAPI_ASSERT(env, status == napi_ok, "NapiGeolocationPermission set property failed");
84 return exports;
85 }
86
GetStringPara(napi_env env,napi_value argv,std::string & outValue)87 bool NapiGeolocationPermission::GetStringPara(napi_env env, napi_value argv, std::string& outValue)
88 {
89 constexpr int32_t MAX_STRING_LENGTH = 40960;
90 size_t bufferSize = 0;
91 napi_valuetype valueType = napi_null;
92
93 napi_typeof(env, argv, &valueType);
94 if (valueType != napi_string) {
95 return false;
96 }
97 napi_get_value_string_utf8(env, argv, nullptr, 0, &bufferSize);
98 if (bufferSize > MAX_STRING_LENGTH) {
99 return false;
100 }
101 char stringValue[bufferSize + 1];
102 size_t jsStringLength = 0;
103 napi_get_value_string_utf8(env, argv, stringValue, bufferSize + 1, &jsStringLength);
104 if (jsStringLength != bufferSize) {
105 return false;
106 }
107 outValue = stringValue;
108 return true;
109 }
110
GetBooleanPara(napi_env env,napi_value argv,bool & outValue)111 bool NapiGeolocationPermission::GetBooleanPara(napi_env env, napi_value argv, bool& outValue)
112 {
113 napi_valuetype valueType = napi_null;
114
115 napi_typeof(env, argv, &valueType);
116 if (valueType != napi_boolean) {
117 return false;
118 }
119
120 bool boolValue;
121 napi_get_value_bool(env, argv, &boolValue);
122 outValue = boolValue;
123 return true;
124 }
125
ProcessActionByType(napi_env env,napi_callback_info info,int32_t operationType)126 napi_value NapiGeolocationPermission::ProcessActionByType(napi_env env, napi_callback_info info,
127 int32_t operationType)
128 {
129 napi_value retValue = nullptr;
130 size_t argc = PARAMTWO;
131 size_t argcForOld = PARAMONE;
132 napi_value argv[PARAMTWO] = { 0 };
133 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
134 if (argc != PARAMTWO && argc != argcForOld) {
135 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
136 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "one", "two"));
137 return nullptr;
138 }
139
140 std::string origin;
141 if (!GetStringPara(env, argv[PARAMZERO], origin)) {
142 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
143 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "origin", "string"));
144 return nullptr;
145 }
146 bool incognitoMode = false;
147 if (argc == PARAMTWO) {
148 napi_get_value_bool(env, argv[PARAMONE], &incognitoMode);
149 }
150
151 napi_value result = nullptr;
152 napi_get_undefined(env, &result);
153 std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
154 if (!dataBase) {
155 return result;
156 }
157 if (operationType == ALLOW_PERMISSION_OPERATION) {
158 if (dataBase->SetPermissionByOrigin(origin, OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, true,
159 incognitoMode) == NWebError::INVALID_ORIGIN) {
160 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::INVALID_ORIGIN);
161 return result;
162 }
163 } else if (operationType == DELETE_PERMISSION_OPERATION) {
164 if (dataBase->ClearPermissionByOrigin(origin, OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE,
165 incognitoMode) == NWebError::INVALID_ORIGIN) {
166 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::INVALID_ORIGIN);
167 return result;
168 }
169 }
170 return result;
171 }
172
JsAllowGeolocation(napi_env env,napi_callback_info info)173 napi_value NapiGeolocationPermission::JsAllowGeolocation(napi_env env, napi_callback_info info)
174 {
175 return ProcessActionByType(env, info, ALLOW_PERMISSION_OPERATION);
176 }
177
JsDeleteGeolocation(napi_env env,napi_callback_info info)178 napi_value NapiGeolocationPermission::JsDeleteGeolocation(napi_env env, napi_callback_info info)
179 {
180 return ProcessActionByType(env, info, DELETE_PERMISSION_OPERATION);
181 }
182
JsDeleteAllGeolocation(napi_env env,napi_callback_info info)183 napi_value NapiGeolocationPermission::JsDeleteAllGeolocation(napi_env env, napi_callback_info info)
184 {
185 napi_value retValue = nullptr;
186 size_t argc = PARAMONE;
187 size_t argcForOld = 0;
188 napi_value argv[PARAMONE] = { 0 };
189 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
190 if (argc != PARAMONE && argc != argcForOld) {
191 return nullptr;
192 }
193 bool incognitoMode = false;
194 if (argc == PARAMONE) {
195 napi_get_value_bool(env, argv[PARAMZERO], &incognitoMode);
196 }
197
198 std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
199 if (dataBase != nullptr) {
200 dataBase->ClearAllPermission(OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, incognitoMode);
201 }
202
203 napi_value result = nullptr;
204 napi_get_undefined(env, &result);
205 return result;
206 }
207
GetPermissionStateComplete(napi_env env,napi_status status,void * data)208 void NapiGeolocationPermission::GetPermissionStateComplete(napi_env env, napi_status status, void *data)
209 {
210 GetOriginPermissionStateParam *param = static_cast<GetOriginPermissionStateParam *>(data);
211 NApiScope scope(env);
212 if (!scope.IsVaild()) {
213 return;
214 }
215 napi_value setResult[RESULT_COUNT] = {0};
216 if (param->status) {
217 setResult[PARAMZERO] = NWebError::BusinessError::CreateError(env, param->errCode);
218 napi_get_undefined(env, &setResult[PARAMONE]);
219 } else {
220 napi_get_undefined(env, &setResult[PARAMZERO]);
221 napi_get_boolean(env, param->retValue, &setResult[PARAMONE]);
222 }
223 napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
224 napi_value callback = nullptr;
225 napi_get_reference_value(env, param->callbackRef, &callback);
226 napi_value returnVal = nullptr;
227 napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &returnVal);
228 napi_delete_reference(env, param->jsStringRef);
229 napi_delete_reference(env, param->callbackRef);
230 napi_delete_async_work(env, param->asyncWork);
231 delete param;
232 param = nullptr;
233 }
234
GetPermissionStatePromiseComplete(napi_env env,napi_status status,void * data)235 void NapiGeolocationPermission::GetPermissionStatePromiseComplete(napi_env env, napi_status status, void *data)
236 {
237 GetOriginPermissionStateParam *param = static_cast<GetOriginPermissionStateParam *>(data);
238 NApiScope scope(env);
239 if (!scope.IsVaild()) {
240 return;
241 }
242 napi_value setResult[RESULT_COUNT] = {0};
243 setResult[PARAMZERO] = NWebError::BusinessError::CreateError(env, param->errCode);
244 napi_get_boolean(env, param->retValue, &setResult[PARAMONE]);
245 napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
246 if (param->status == napi_ok) {
247 napi_resolve_deferred(env, param->deferred, args[1]);
248 } else {
249 napi_reject_deferred(env, param->deferred, args[0]);
250 }
251 napi_delete_reference(env, param->jsStringRef);
252 napi_delete_async_work(env, param->asyncWork);
253 delete param;
254 param = nullptr;
255 }
256
ExecuteGetPermissionState(napi_env env,void * data)257 void NapiGeolocationPermission::ExecuteGetPermissionState(napi_env env, void *data)
258 {
259 GetOriginPermissionStateParam *param = static_cast<GetOriginPermissionStateParam *>(data);
260 std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
261 if (!dataBase) {
262 param->errCode = INTERFACE_ERROR;
263 param->status = napi_generic_failure;
264 return;
265 }
266 if (dataBase->GetPermissionResultByOrigin(param->origin,
267 OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, param->retValue, param->incognitoMode)) {
268 param->errCode = INTERFACE_OK;
269 param->status = napi_ok;
270 } else {
271 param->errCode = NWebError::INVALID_ORIGIN;
272 param->status = napi_generic_failure;
273 }
274 }
275
GetPermissionStateAsync(napi_env env,napi_value * argv,const std::string & origin,bool incognitoMode)276 napi_value NapiGeolocationPermission::GetPermissionStateAsync(napi_env env,
277 napi_value *argv, const std::string& origin, bool incognitoMode)
278 {
279 napi_value result = nullptr;
280 napi_value resourceName = nullptr;
281
282 GetOriginPermissionStateParam *param = new (std::nothrow) GetOriginPermissionStateParam {
283 .retValue = false,
284 .incognitoMode = incognitoMode,
285 .origin = origin,
286 .env = env,
287 .asyncWork = nullptr,
288 .deferred = nullptr,
289 .jsStringRef = nullptr,
290 .callbackRef = nullptr,
291 };
292 if (param == nullptr) {
293 return nullptr;
294 }
295 napi_create_reference(env, argv[0], 1, ¶m->jsStringRef);
296 napi_create_reference(env, argv[1], 1, ¶m->callbackRef);
297 NAPI_CALL(env, napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
298 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteGetPermissionState,
299 GetPermissionStateComplete, static_cast<void *>(param), ¶m->asyncWork));
300 NAPI_CALL(env, napi_queue_async_work_with_qos(env, param->asyncWork, napi_qos_user_initiated));
301 napi_get_undefined(env, &result);
302 return result;
303 }
304
GetPermissionStatePromise(napi_env env,napi_value * argv,const std::string & origin,bool incognitoMode)305 napi_value NapiGeolocationPermission::GetPermissionStatePromise(napi_env env, napi_value *argv,
306 const std::string& origin, bool incognitoMode)
307 {
308 napi_deferred deferred = nullptr;
309 napi_value promise = nullptr;
310 napi_create_promise(env, &deferred, &promise);
311
312 GetOriginPermissionStateParam *param = new (std::nothrow) GetOriginPermissionStateParam {
313 .retValue = false,
314 .incognitoMode = incognitoMode,
315 .origin = origin,
316 .env = env,
317 .asyncWork = nullptr,
318 .deferred = deferred,
319 .jsStringRef = nullptr,
320 .callbackRef = nullptr,
321 };
322 if (param == nullptr) {
323 return nullptr;
324 }
325 napi_create_reference(env, argv[0], 1, ¶m->jsStringRef);
326 napi_value resourceName = nullptr;
327 NAPI_CALL(env, napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
328 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteGetPermissionState,
329 GetPermissionStatePromiseComplete, static_cast<void *>(param), ¶m->asyncWork));
330 NAPI_CALL(env, napi_queue_async_work_with_qos(env, param->asyncWork, napi_qos_user_initiated));
331 return promise;
332 }
333
JsGetAccessibleGeolocation(napi_env env,napi_callback_info info)334 napi_value NapiGeolocationPermission::JsGetAccessibleGeolocation(napi_env env, napi_callback_info info)
335 {
336 napi_value retValue = nullptr;
337 size_t argc = PARAMTHREE;
338 size_t argcPromiseForOld = PARAMONE;
339 size_t argcPromise = PARAMTWO;
340 size_t argcCallback = PARAMTHREE;
341 napi_value argv[PARAMTHREE] = { 0 };
342 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
343 if (argc != argcPromise && argc != argcCallback && argc != argcPromiseForOld) {
344 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
345 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_THREE, "one", "two", "three"));
346 return nullptr;
347 }
348 std::string origin;
349 if (!GetStringPara(env, argv[PARAMZERO], origin)) {
350 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
351 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "origin", "string"));
352 return nullptr;
353 }
354 bool incognitoMode = false;
355 napi_value result = nullptr;
356 if (argc == argcCallback) {
357 napi_valuetype valueType = napi_undefined;
358 napi_typeof(env, argv[PARAMONE], &valueType);
359 if (valueType != napi_function) {
360 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
361 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function"));
362 return nullptr;
363 }
364 if (!GetBooleanPara(env, argv[PARAMTWO], incognitoMode)) {
365 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
366 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "incognito", "boolean"));
367 return nullptr;
368 }
369 GetPermissionStateAsync(env, argv, origin, incognitoMode);
370 napi_get_undefined(env, &result);
371 return result;
372 }
373 if (argc == PARAMTWO) {
374 napi_valuetype valueType = napi_undefined;
375 napi_typeof(env, argv[PARAMONE], &valueType);
376 if (valueType != napi_function) {
377 if (!GetBooleanPara(env, argv[PARAMONE], incognitoMode)) {
378 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
379 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "incognito", "boolean"));
380 return nullptr;
381 }
382 return GetPermissionStatePromise(env, argv, origin, incognitoMode);
383 }
384 GetPermissionStateAsync(env, argv, origin, incognitoMode);
385 napi_get_undefined(env, &result);
386 return result;
387 }
388 return GetPermissionStatePromise(env, argv, origin, incognitoMode);
389 }
390
GetOriginComplete(napi_env env,napi_status status,void * data)391 void NapiGeolocationPermission::GetOriginComplete(napi_env env, napi_status status, void *data)
392 {
393 GetPermissionOriginsParam *param = static_cast<GetPermissionOriginsParam *>(data);
394 NApiScope scope(env);
395 if (!scope.IsVaild()) {
396 return;
397 }
398 napi_value setResult[RESULT_COUNT] = {0};
399 if (param->status) {
400 napi_get_undefined(env, &setResult[PARAMZERO]);
401 napi_get_undefined(env, &setResult[PARAMONE]);
402 } else {
403 napi_get_undefined(env, &setResult[PARAMZERO]);
404 napi_create_array(env, &setResult[PARAMONE]);
405 for (uint32_t i = 0; i < param->origins.size(); i++) {
406 std::string str = param->origins[i];
407 napi_value val = nullptr;
408 napi_create_string_utf8(env, str.c_str(), str.length(), &val);
409 napi_set_element(env, setResult[PARAMONE], i, val);
410 }
411 }
412 napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
413 napi_value callback = nullptr;
414 napi_get_reference_value(env, param->callbackRef, &callback);
415 napi_value returnVal = nullptr;
416 napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &returnVal);
417 napi_delete_reference(env, param->callbackRef);
418 napi_delete_async_work(env, param->asyncWork);
419 delete param;
420 param = nullptr;
421 }
422
GetOriginsPromiseComplete(napi_env env,napi_status status,void * data)423 void NapiGeolocationPermission::GetOriginsPromiseComplete(napi_env env, napi_status status, void *data)
424 {
425 GetPermissionOriginsParam *param = static_cast<GetPermissionOriginsParam *>(data);
426 NApiScope scope(env);
427 if (!scope.IsVaild()) {
428 delete param;
429 return;
430 }
431 napi_value setResult[RESULT_COUNT] = {0};
432 napi_get_undefined(env, &setResult[PARAMZERO]);
433 napi_create_array(env, &setResult[PARAMONE]);
434 for (uint32_t i = 0; i < param->origins.size(); i++) {
435 std::string str = param->origins[i];
436 napi_value val = nullptr;
437 napi_create_string_utf8(env, str.c_str(), str.length(), &val);
438 napi_set_element(env, setResult[PARAMONE], i, val);
439 }
440 napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
441 if (param->status == napi_ok) {
442 napi_resolve_deferred(env, param->deferred, args[1]);
443 } else {
444 napi_reject_deferred(env, param->deferred, args[0]);
445 }
446 napi_delete_async_work(env, param->asyncWork);
447 delete param;
448 param = nullptr;
449 }
450
ExecuteGetOrigins(napi_env env,void * data)451 void NapiGeolocationPermission::ExecuteGetOrigins(napi_env env, void *data)
452 {
453 GetPermissionOriginsParam *param = static_cast<GetPermissionOriginsParam *>(data);
454 std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
455 if (!dataBase) {
456 param->errCode = INTERFACE_ERROR;
457 param->status = napi_generic_failure;
458 return;
459 }
460 param->origins = dataBase->GetOriginsByPermission(
461 OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, param->incognitoMode);
462 param->errCode = INTERFACE_OK;
463 param->status = napi_ok;
464 }
465
GetOriginsAsync(napi_env env,napi_value * argv,bool incognitoMode)466 napi_value NapiGeolocationPermission::GetOriginsAsync(napi_env env,
467 napi_value *argv,
468 bool incognitoMode)
469 {
470 napi_value result = nullptr;
471 napi_value resourceName = nullptr;
472
473 GetPermissionOriginsParam *param = new (std::nothrow) GetPermissionOriginsParam {
474 .env = env,
475 .asyncWork = nullptr,
476 .deferred = nullptr,
477 .callbackRef = nullptr,
478 .incognitoMode = incognitoMode,
479 };
480 if (param == nullptr) {
481 return nullptr;
482 }
483 napi_create_reference(env, *argv, 1, ¶m->callbackRef);
484 NAPI_CALL(env, napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
485 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteGetOrigins,
486 GetOriginComplete, static_cast<void *>(param), ¶m->asyncWork));
487 NAPI_CALL(env, napi_queue_async_work_with_qos(env, param->asyncWork, napi_qos_user_initiated));
488 napi_get_undefined(env, &result);
489 return result;
490 }
491
GetOriginsPromise(napi_env env,bool incognitoMode)492 napi_value NapiGeolocationPermission::GetOriginsPromise(napi_env env,
493 bool incognitoMode)
494 {
495 napi_deferred deferred = nullptr;
496 napi_value promise = nullptr;
497 napi_create_promise(env, &deferred, &promise);
498
499 napi_value resourceName = nullptr;
500 NAPI_CALL(env, napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
501 GetPermissionOriginsParam *param = new (std::nothrow) GetPermissionOriginsParam {
502 .env = env,
503 .asyncWork = nullptr,
504 .deferred = deferred,
505 .callbackRef = nullptr,
506 .incognitoMode = incognitoMode,
507 };
508 if (param == nullptr) {
509 return nullptr;
510 }
511 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteGetOrigins,
512 GetOriginsPromiseComplete, static_cast<void *>(param), ¶m->asyncWork));
513 NAPI_CALL(env, napi_queue_async_work_with_qos(env, param->asyncWork, napi_qos_user_initiated));
514 return promise;
515 }
516
JsGetStoredGeolocation(napi_env env,napi_callback_info info)517 napi_value NapiGeolocationPermission::JsGetStoredGeolocation(napi_env env, napi_callback_info info)
518 {
519 napi_value retValue = nullptr;
520 size_t argc = PARAMTWO;
521 size_t argcForZero = PARAMZERO;
522 size_t argcPromise = PARAMONE;
523 size_t argcCallback = PARAMTWO;
524 napi_value argv[PARAMTWO] = { 0 };
525 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
526 if (argc != argcPromise && argc != argcCallback && argc != argcForZero) {
527 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
528 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_THREE, "one", "two", "three"));
529 return nullptr;
530 }
531
532 bool incognitoMode = false;
533 if (argc == argcCallback) {
534 napi_valuetype valueType = napi_undefined;
535 napi_typeof(env, argv[PARAMZERO], &valueType);
536 if (valueType != napi_function) {
537 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
538 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function"));
539 return nullptr;
540 }
541
542 if (!GetBooleanPara(env, argv[PARAMONE], incognitoMode)) {
543 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
544 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "incognito", "boolean"));
545 return nullptr;
546 }
547 GetOriginsAsync(env, argv, incognitoMode);
548 napi_value result = nullptr;
549 napi_get_undefined(env, &result);
550 return result;
551 }
552
553 if (argc == PARAMONE) {
554 napi_valuetype valueType = napi_undefined;
555 napi_typeof(env, argv[PARAMZERO], &valueType);
556 if (valueType != napi_function) {
557 if (!GetBooleanPara(env, argv[PARAMZERO], incognitoMode)) {
558 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
559 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "incognito", "boolean"));
560 return nullptr;
561 }
562 return GetOriginsPromise(env, incognitoMode);
563 }
564 GetOriginsAsync(env, argv, incognitoMode);
565 napi_value result = nullptr;
566 napi_get_undefined(env, &result);
567 return result;
568 }
569
570 return GetOriginsPromise(env, incognitoMode);
571 }
572
JsConstructor(napi_env env,napi_callback_info info)573 napi_value NapiGeolocationPermission::JsConstructor(napi_env env, napi_callback_info info)
574 {
575 napi_value thisVar = nullptr;
576 size_t argc = PARAMTWO;
577 napi_value argv[PARAMTWO] = { 0 };
578 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
579 return thisVar;
580 }
581 } // namespace NWeb
582 } // namespace OHOS
583