1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "napi_app_account_common.h"
16 #include "account_error_no.h"
17 #include "account_log_wrapper.h"
18 #include "app_account_constants.h"
19 #include "app_account_manager.h"
20 #include "napi_common.h"
21
22 namespace OHOS {
23 namespace AccountJsKit {
24 using namespace OHOS::AccountSA;
25
26 std::mutex g_lockForAppAccountSubscribers;
27 std::map<AppAccountManager *, std::vector<AsyncContextForSubscribe *>> g_AppAccountSubscribers;
28
SubscriberPtr(const AppAccountSubscribeInfo & subscribeInfo)29 SubscriberPtr::SubscriberPtr(const AppAccountSubscribeInfo &subscribeInfo) : AppAccountSubscriber(subscribeInfo)
30 {}
31
~SubscriberPtr()32 SubscriberPtr::~SubscriberPtr()
33 {}
34
UvQueueWorkOnAppAccountsChanged(uv_work_t * work,int status)35 void UvQueueWorkOnAppAccountsChanged(uv_work_t *work, int status)
36 {
37 std::unique_ptr<uv_work_t> workPtr(work);
38 napi_handle_scope scope = nullptr;
39 if (!InitUvWorkCallbackEnv(work, scope)) {
40 return;
41 }
42 std::unique_ptr<SubscriberAccountsWorker> data(reinterpret_cast<SubscriberAccountsWorker *>(work->data));
43 bool isFound = false;
44 {
45 std::lock_guard<std::mutex> lock(g_lockForAppAccountSubscribers);
46 SubscriberPtr *subscriber = data->subscriber;
47 for (auto objectInfoTmp : g_AppAccountSubscribers) {
48 isFound = std::any_of(objectInfoTmp.second.begin(), objectInfoTmp.second.end(),
49 [subscriber](const AsyncContextForSubscribe *item) {
50 return item->subscriber.get() == subscriber;
51 });
52 if (isFound) {
53 ACCOUNT_LOGD("app account subscriber has been found.");
54 break;
55 }
56 }
57 }
58 if (isFound) {
59 napi_value results[ARGS_SIZE_ONE] = {nullptr};
60 GetAppAccountInfoForResult(data->env, data->accounts, results[0]);
61 NapiCallVoidFunction(data->env, results, ARGS_SIZE_ONE, data->ref);
62 }
63 napi_close_handle_scope(data->env, scope);
64 }
65
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts_)66 void SubscriberPtr::OnAccountsChanged(const std::vector<AppAccountInfo> &accounts_)
67 {
68 uv_loop_s *loop = nullptr;
69 napi_get_uv_event_loop(env_, &loop);
70 if (loop == nullptr) {
71 ACCOUNT_LOGE("loop instance is nullptr");
72 return;
73 }
74 uv_work_t *work = new (std::nothrow) uv_work_t;
75 if (work == nullptr) {
76 ACCOUNT_LOGE("work is null");
77 return;
78 }
79
80 SubscriberAccountsWorker *subscriberAccountsWorker = new (std::nothrow) SubscriberAccountsWorker(env_);
81
82 if (subscriberAccountsWorker == nullptr) {
83 ACCOUNT_LOGE("SubscriberAccountsWorker is null");
84 delete work;
85 return;
86 }
87
88 subscriberAccountsWorker->accounts = accounts_;
89 subscriberAccountsWorker->ref = ref_;
90 subscriberAccountsWorker->subscriber = this;
91
92 work->data = reinterpret_cast<void *>(subscriberAccountsWorker);
93
94 uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAppAccountsChanged);
95 }
96
SetEnv(const napi_env & env)97 void SubscriberPtr::SetEnv(const napi_env &env)
98 {
99 env_ = env;
100 }
101
SetCallbackRef(const napi_ref & ref)102 void SubscriberPtr::SetCallbackRef(const napi_ref &ref)
103 {
104 ref_ = ref;
105 }
106
CheckAccountLabelsOnResultWork(uv_work_t * work,int status)107 void CheckAccountLabelsOnResultWork(uv_work_t *work, int status)
108 {
109 std::unique_ptr<uv_work_t> workPtr(work);
110 napi_handle_scope scope = nullptr;
111 if (!InitUvWorkCallbackEnv(work, scope)) {
112 return;
113 }
114 std::unique_ptr<AuthenticatorCallbackParam> data(reinterpret_cast<AuthenticatorCallbackParam *>(work->data));
115 napi_value checkResult[RESULT_COUNT] = {NapiGetNull(data->context.env)};
116 if (data->context.errCode == ERR_JS_SUCCESS) {
117 bool hasLabels = data->result.GetBoolParam(Constants::KEY_BOOLEAN_RESULT, false);
118 napi_get_boolean(data->context.env, hasLabels, &checkResult[PARAMONE]);
119 } else {
120 checkResult[PARAMZERO] = GetErrorCodeValue(data->context.env, data->context.errCode);
121 }
122 ProcessCallbackOrPromise(data->context.env, &(data->context), checkResult[PARAMZERO], checkResult[PARAMONE]);
123 napi_close_handle_scope(data->context.env, scope);
124 }
125
CreateJSAppAccountInfo(napi_env env,const std::string & name,const std::string & owner)126 static napi_value CreateJSAppAccountInfo(napi_env env, const std::string &name, const std::string &owner)
127 {
128 napi_value object = nullptr;
129 NAPI_CALL(env, napi_create_object(env, &object));
130 napi_value value = nullptr;
131 NAPI_CALL(env, napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &value));
132 NAPI_CALL(env, napi_set_named_property(env, object, "name", value));
133 NAPI_CALL(env, napi_create_string_utf8(env, owner.c_str(), NAPI_AUTO_LENGTH, &value));
134 NAPI_CALL(env, napi_set_named_property(env, object, "owner", value));
135 return object;
136 }
137
SelectAccountsOnResultWork(uv_work_t * work,int status)138 void SelectAccountsOnResultWork(uv_work_t *work, int status)
139 {
140 napi_handle_scope scope = nullptr;
141 std::unique_ptr<uv_work_t> workPtr(work);
142 if (!InitUvWorkCallbackEnv(work, scope)) {
143 return;
144 }
145 std::unique_ptr<AuthenticatorCallbackParam> param(reinterpret_cast<AuthenticatorCallbackParam *>(work->data));
146 std::vector<std::string> names = param->result.GetStringArrayParam(Constants::KEY_ACCOUNT_NAMES);
147 std::vector<std::string> owners = param->result.GetStringArrayParam(Constants::KEY_ACCOUNT_OWNERS);
148 if (names.size() != owners.size()) {
149 param->context.errCode = ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION;
150 }
151 napi_env env = param->context.env;
152 napi_value selectResult[RESULT_COUNT] = {0};
153 if (param->context.errCode == ERR_JS_SUCCESS) {
154 napi_create_array(env, &selectResult[PARAMONE]);
155 for (size_t i = 0; i < names.size(); ++i) {
156 napi_value object = CreateJSAppAccountInfo(env, names[i], owners[i]);
157 napi_set_element(env, selectResult[PARAMONE], i, object);
158 }
159 } else {
160 selectResult[PARAMZERO] = GetErrorCodeValue(env, param->context.errCode);
161 }
162 ProcessCallbackOrPromise(env, &(param->context), selectResult[PARAMZERO], selectResult[PARAMONE]);
163 napi_close_handle_scope(env, scope);
164 }
165
AuthenticatorAsyncCallback(const CommonAsyncContext & context,uv_after_work_cb workCb)166 AuthenticatorAsyncCallback::AuthenticatorAsyncCallback(
167 const CommonAsyncContext &context, uv_after_work_cb workCb)
168 : context_(context), workCb_(workCb)
169 {}
170
~AuthenticatorAsyncCallback()171 AuthenticatorAsyncCallback::~AuthenticatorAsyncCallback()
172 {}
173
OnResult(int32_t resultCode,const AAFwk::Want & result)174 void AuthenticatorAsyncCallback::OnResult(int32_t resultCode, const AAFwk::Want &result)
175 {
176 {
177 std::lock_guard<std::mutex> lock(mutex_);
178 if (isDone) {
179 return;
180 }
181 isDone = true;
182 }
183 uv_loop_s *loop = nullptr;
184 uv_work_t *work = nullptr;
185 AuthenticatorCallbackParam *param = nullptr;
186 if (!InitAuthenticatorWorkEnv(context_.env, &loop, &work, ¶m)) {
187 ACCOUNT_LOGE("failed to init work environment");
188 return;
189 }
190 param->context = context_;
191 param->context.errCode = resultCode;
192 param->result = result;
193 work->data = param;
194 if (uv_queue_work(loop, work, [](uv_work_t *work) {}, workCb_) == ERR_OK) {
195 return;
196 }
197 ReleaseNapiRefAsync(context_.env, context_.callbackRef);
198 delete param;
199 delete work;
200 }
201
OnRequestRedirected(AAFwk::Want & request)202 void AuthenticatorAsyncCallback::OnRequestRedirected(AAFwk::Want &request)
203 {}
204
OnRequestContinued()205 void AuthenticatorAsyncCallback::OnRequestContinued()
206 {}
207
AppAccountManagerCallback(napi_env env,JSAuthCallback callback)208 AppAccountManagerCallback::AppAccountManagerCallback(napi_env env, JSAuthCallback callback)
209 : env_(env), callback_(callback)
210 {}
211
~AppAccountManagerCallback()212 AppAccountManagerCallback::~AppAccountManagerCallback()
213 {}
214
UvQueueWorkOnResult(uv_work_t * work,int status)215 void UvQueueWorkOnResult(uv_work_t *work, int status)
216 {
217 std::unique_ptr<uv_work_t> workPtr(work);
218 napi_handle_scope scope = nullptr;
219 if (!InitUvWorkCallbackEnv(work, scope)) {
220 return;
221 }
222 std::unique_ptr<AuthenticatorCallbackParam> data(reinterpret_cast<AuthenticatorCallbackParam *>(work->data));
223 ProcessOnResultCallback(data->env, data->callback, data->resultCode, data->result.GetParams());
224 napi_close_handle_scope(data->env, scope);
225 }
226
UvQueueWorkOnRequestRedirected(uv_work_t * work,int status)227 void UvQueueWorkOnRequestRedirected(uv_work_t *work, int status)
228 {
229 std::unique_ptr<uv_work_t> workPtr(work);
230 napi_handle_scope scope = nullptr;
231 if (!InitUvWorkCallbackEnv(work, scope)) {
232 return;
233 }
234 std::unique_ptr<AuthenticatorCallbackParam> data(reinterpret_cast<AuthenticatorCallbackParam *>(work->data));
235 napi_value results[ARGS_SIZE_ONE] = {AppExecFwk::WrapWant(data->env, data->request)};
236 NapiCallVoidFunction(data->env, results, ARGS_SIZE_ONE, data->callback.onRequestRedirected);
237 napi_close_handle_scope(data->env, scope);
238 }
239
UvQueueWorkOnRequestContinued(uv_work_t * work,int status)240 void UvQueueWorkOnRequestContinued(uv_work_t *work, int status)
241 {
242 std::unique_ptr<uv_work_t> workPtr(work);
243 napi_handle_scope scope = nullptr;
244 if (!InitUvWorkCallbackEnv(work, scope)) {
245 return;
246 }
247 std::unique_ptr<AuthenticatorCallbackParam> data(reinterpret_cast<AuthenticatorCallbackParam *>(work->data));
248 NapiCallVoidFunction(data->env, nullptr, 0, data->callback.onRequestContinued);
249 napi_close_handle_scope(data->env, scope);
250 }
251
OnResult(int32_t resultCode,const AAFwk::Want & result)252 void AppAccountManagerCallback::OnResult(int32_t resultCode, const AAFwk::Want &result)
253 {
254 uv_loop_s *loop = nullptr;
255 uv_work_t *work = nullptr;
256 AuthenticatorCallbackParam *param = nullptr;
257 if (!InitAuthenticatorWorkEnv(env_, &loop, &work, ¶m)) {
258 ACCOUNT_LOGE("failed to init authenticator work environment");
259 return;
260 }
261 param->resultCode = resultCode;
262 param->result = result;
263 param->callback = callback_;
264 work->data = reinterpret_cast<void *>(param);
265 uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnResult);
266 }
267
OnRequestRedirected(AAFwk::Want & request)268 void AppAccountManagerCallback::OnRequestRedirected(AAFwk::Want &request)
269 {
270 uv_loop_s *loop = nullptr;
271 uv_work_t *work = nullptr;
272 AuthenticatorCallbackParam *param = nullptr;
273 if (!InitAuthenticatorWorkEnv(env_, &loop, &work, ¶m)) {
274 ACCOUNT_LOGE("failed to init authenticator work environment");
275 return;
276 }
277 param->request = request;
278 param->callback = callback_;
279 work->data = reinterpret_cast<void *>(param);
280 uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnRequestRedirected);
281 }
282
OnRequestContinued()283 void AppAccountManagerCallback::OnRequestContinued()
284 {
285 uv_loop_s *loop = nullptr;
286 uv_work_t *work = nullptr;
287 AuthenticatorCallbackParam *param = nullptr;
288 if (!InitAuthenticatorWorkEnv(env_, &loop, &work, ¶m)) {
289 ACCOUNT_LOGE("failed to init authenticator work environment");
290 return;
291 }
292 param->callback = callback_;
293 work->data = reinterpret_cast<void *>(param);
294 uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnRequestContinued);
295 }
296
InitAuthenticatorWorkEnv(napi_env env,uv_loop_s ** loop,uv_work_t ** work,AuthenticatorCallbackParam ** param)297 bool InitAuthenticatorWorkEnv(napi_env env, uv_loop_s **loop, uv_work_t **work,
298 AuthenticatorCallbackParam **param)
299 {
300 napi_get_uv_event_loop(env, loop);
301 if (*loop == nullptr) {
302 ACCOUNT_LOGE("loop instance is nullptr");
303 return false;
304 }
305 *work = new (std::nothrow) uv_work_t;
306 if (*work == nullptr) {
307 ACCOUNT_LOGE("work is null");
308 return false;
309 }
310 *param = new (std::nothrow) AuthenticatorCallbackParam(env);
311 if (*param == nullptr) {
312 ACCOUNT_LOGE("failed to create AuthenticatorCallbackParam");
313 delete *work;
314 *work = nullptr;
315 *loop = nullptr;
316 return false;
317 }
318 return true;
319 }
320
NapiGetNull(napi_env env)321 napi_value NapiGetNull(napi_env env)
322 {
323 napi_value result = nullptr;
324 napi_get_null(env, &result);
325
326 return result;
327 }
328
GetNamedProperty(napi_env env,napi_value obj)329 std::string GetNamedProperty(napi_env env, napi_value obj)
330 {
331 char propValue[MAX_VALUE_LEN] = {0};
332 size_t propLen;
333 if (napi_get_value_string_utf8(env, obj, propValue, MAX_VALUE_LEN, &propLen) != napi_ok) {
334 ACCOUNT_LOGE("Can not get string param from argv");
335 }
336
337 return std::string(propValue);
338 }
339
SetNamedProperty(napi_env env,napi_value dstObj,const char * objName,const char * propName)340 void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName)
341 {
342 napi_value prop = nullptr;
343 napi_create_string_utf8(env, objName, NAPI_AUTO_LENGTH, &prop);
344 napi_set_named_property(env, dstObj, propName, prop);
345 }
346
SetNamedProperty(napi_env env,napi_value dstObj,const int32_t objValue,const char * propName)347 void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName)
348 {
349 napi_value prop = nullptr;
350 napi_create_int32(env, objValue, &prop);
351 napi_set_named_property(env, dstObj, propName, prop);
352 }
353
GetErrorCodeValue(napi_env env,int errCode)354 napi_value GetErrorCodeValue(napi_env env, int errCode)
355 {
356 napi_value jsObject = nullptr;
357 napi_value jsValue = nullptr;
358 NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
359 NAPI_CALL(env, napi_create_object(env, &jsObject));
360 NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
361 return jsObject;
362 }
363
GetAppAccountInfoForResult(napi_env env,const std::vector<AppAccountInfo> & info,napi_value & result)364 void GetAppAccountInfoForResult(napi_env env, const std::vector<AppAccountInfo> &info, napi_value &result)
365 {
366 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &result));
367 uint32_t index = 0;
368 for (auto item : info) {
369 std::string name;
370 item.GetName(name);
371 std::string owner;
372 item.GetOwner(owner);
373 napi_value objAppAccountInfo = CreateJSAppAccountInfo(env, name, owner);
374 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index++, objAppAccountInfo));
375 }
376 }
377
GetAuthenticatorInfoForResult(napi_env env,const AuthenticatorInfo & info,napi_value & result)378 void GetAuthenticatorInfoForResult(napi_env env, const AuthenticatorInfo &info, napi_value &result)
379 {
380 napi_value nOwner = nullptr;
381 napi_create_string_utf8(env, info.owner.c_str(), NAPI_AUTO_LENGTH, &nOwner);
382 napi_set_named_property(env, result, "owner", nOwner);
383
384 napi_value nIconId = nullptr;
385 napi_create_int32(env, info.iconId, &nIconId);
386 napi_set_named_property(env, result, "iconId", nIconId);
387
388 napi_value nLabelId = nullptr;
389 napi_create_int32(env, info.labelId, &nLabelId);
390 napi_set_named_property(env, result, "labelId", nLabelId);
391 }
392
GetOAuthTokenInfoForResult(napi_env env,const std::vector<OAuthTokenInfo> & info,napi_value result)393 void GetOAuthTokenInfoForResult(napi_env env, const std::vector<OAuthTokenInfo> &info, napi_value result)
394 {
395 int32_t index = 0;
396 for (auto item : info) {
397 napi_value objOAuthTokenInfo = nullptr;
398 napi_create_object(env, &objOAuthTokenInfo);
399
400 napi_value nToken = nullptr;
401 napi_create_string_utf8(env, item.token.c_str(), NAPI_AUTO_LENGTH, &nToken);
402 napi_set_named_property(env, objOAuthTokenInfo, "token", nToken);
403
404 napi_value nAuthType = nullptr;
405 napi_create_string_utf8(env, item.authType.c_str(), NAPI_AUTO_LENGTH, &nAuthType);
406 napi_set_named_property(env, objOAuthTokenInfo, "authType", nAuthType);
407
408 napi_set_element(env, result, index, objOAuthTokenInfo);
409 index++;
410 }
411 }
412
GetOAuthListForResult(napi_env env,const std::set<std::string> & info,napi_value result)413 void GetOAuthListForResult(napi_env env, const std::set<std::string> &info, napi_value result)
414 {
415 int32_t index = 0;
416 for (auto item : info) {
417 napi_value nBundleName = nullptr;
418 napi_create_string_utf8(env, item.c_str(), NAPI_AUTO_LENGTH, &nBundleName);
419 napi_set_element(env, result, index, nBundleName);
420 index++;
421 }
422 }
423
GetAuthenticatorCallbackForResult(napi_env env,sptr<IRemoteObject> callback,napi_value * result)424 void GetAuthenticatorCallbackForResult(napi_env env, sptr<IRemoteObject> callback, napi_value *result)
425 {
426 if (callback == nullptr) {
427 napi_get_undefined(env, result);
428 return;
429 }
430 napi_value remote;
431 napi_create_int64(env, reinterpret_cast<int64_t>(callback.GetRefPtr()), &remote);
432 napi_value global = nullptr;
433 napi_get_global(env, &global);
434 if (global == nullptr) {
435 ACCOUNT_LOGE("get napi global failed");
436 return;
437 }
438 napi_value jsAuthCallbackConstructor = nullptr;
439 napi_get_named_property(env, global, "AuthCallbackConstructor_", &jsAuthCallbackConstructor);
440 if (jsAuthCallbackConstructor == nullptr) {
441 ACCOUNT_LOGE("jsAuthCallbackConstructor is null");
442 return;
443 }
444 size_t argc = ARGS_SIZE_ONE;
445 napi_value argv[ARGS_SIZE_ONE] = { remote };
446 napi_new_instance(env, jsAuthCallbackConstructor, argc, argv, result);
447 }
448
ParseContextWithExInfo(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)449 bool ParseContextWithExInfo(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
450 {
451 size_t argc = ARGS_SIZE_THREE;
452 napi_value argv[ARGS_SIZE_THREE] = {0};
453 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
454 if (argc < ARGS_SIZE_ONE) {
455 asyncContext->errMsg = "the number of parameters should be at least 1";
456 return false;
457 }
458 for (size_t i = 0; i < argc; i++) {
459 napi_valuetype valueType = napi_undefined;
460 napi_typeof(env, argv[i], &valueType);
461 if (i == 0 && valueType == napi_string) {
462 GetStringProperty(env, argv[i], asyncContext->name);
463 } else if (i == 1 && valueType == napi_string) {
464 GetStringProperty(env, argv[i], asyncContext->extraInfo);
465 } else if (i == 1 && valueType == napi_function) {
466 napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef);
467 break;
468 } else if (i == PARAMTWO && valueType == napi_function) {
469 napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef);
470 break;
471 } else {
472 ACCOUNT_LOGE("Type matching failed");
473 asyncContext->errMsg = "the type of param " + std::to_string(i) + " is incorrect";
474 return false;
475 }
476 }
477 return true;
478 }
479
ParseContextForSetExInfo(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)480 bool ParseContextForSetExInfo(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
481 {
482 size_t argc = ARGS_SIZE_THREE;
483 napi_value argv[ARGS_SIZE_THREE] = {0};
484 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
485 if (argc < ARGS_SIZE_TWO) {
486 asyncContext->errMsg = "the number of parameter should be at least 2";
487 return false;
488 }
489 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
490 asyncContext->errMsg = "the callback is not a function";
491 return false;
492 }
493 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
494 asyncContext->errMsg = "the name is not a string";
495 return false;
496 }
497 if (!GetStringProperty(env, argv[1], asyncContext->extraInfo)) {
498 asyncContext->errMsg = "the extraInfo is not a string";
499 return false;
500 }
501 return true;
502 }
503
ParseArguments(napi_env env,napi_value * argv,const napi_valuetype * valueTypes,size_t argc)504 bool ParseArguments(napi_env env, napi_value *argv, const napi_valuetype *valueTypes, size_t argc)
505 {
506 napi_valuetype valuetype = napi_undefined;
507 for (size_t i = 0; i < argc; ++i) {
508 napi_typeof(env, argv[i], &valuetype);
509 if (valuetype != valueTypes[i]) {
510 argv[i] = nullptr;
511 return false;
512 }
513 }
514 return true;
515 }
516
ParseContextForAuth(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * context)517 bool ParseContextForAuth(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *context)
518 {
519 std::string abilityName;
520 GetAbilityName(env, abilityName);
521 context->options.SetParam(Constants::KEY_CALLER_ABILITY_NAME, abilityName);
522 size_t argc = ARGS_SIZE_FIVE;
523 napi_value argv[ARGS_SIZE_FIVE] = {0};
524 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
525 if (argc < ARGS_SIZE_FOUR) {
526 context->errMsg = "the number of parameters should be at least 4";
527 return false;
528 }
529 size_t index = 0;
530 if (!GetStringProperty(env, argv[index++], context->name)) {
531 context->errMsg = "the name is not string";
532 return false;
533 }
534 if (!GetStringProperty(env, argv[index++], context->owner)) {
535 context->errMsg = "the owner is not string";
536 return false;
537 }
538 if (!GetStringProperty(env, argv[index++], context->authType)) {
539 context->errMsg = "the authType is not string";
540 return false;
541 }
542 AAFwk::WantParams params;
543 if ((argc == ARGS_SIZE_FIVE) && (!AppExecFwk::UnwrapWantParams(env, argv[index++], params))) {
544 ACCOUNT_LOGE("UnwrapWantParams failed");
545 context->errMsg = "the type of options is incorrect";
546 return false;
547 }
548 context->options.SetParams(params);
549 context->options.SetParam(Constants::KEY_CALLER_ABILITY_NAME, abilityName);
550 JSAuthCallback callback;
551 if (!ParseJSAuthCallback(env, argv[index], callback)) {
552 context->errMsg = "the type of authCallback is incorrect";
553 return false;
554 }
555 context->appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(env, callback);
556 return true;
557 }
558
ParseContextForAuthenticate(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext,size_t argc)559 void ParseContextForAuthenticate(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext, size_t argc)
560 {
561 napi_value argv[ARGS_SIZE_FIVE] = {0};
562 napi_value thisVar;
563 napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, nullptr);
564 napi_valuetype valueTypes[ARGS_SIZE_FIVE] = {napi_string, napi_string, napi_string, napi_object, napi_object};
565 size_t index = 0;
566 if (argc == ARGS_SIZE_FIVE) {
567 ParseArguments(env, argv, valueTypes, argc);
568 asyncContext->name = GetNamedProperty(env, argv[index++]);
569 } else {
570 argc = ARGS_SIZE_FOUR;
571 ParseArguments(env, argv, &valueTypes[1], argc);
572 }
573 asyncContext->owner = GetNamedProperty(env, argv[index++]);
574 asyncContext->authType = GetNamedProperty(env, argv[index++]);
575 AAFwk::WantParams params;
576 if (!AppExecFwk::UnwrapWantParams(env, argv[index++], params)) {
577 ACCOUNT_LOGE("UnwrapWantParams failed");
578 }
579 asyncContext->options.SetParams(params);
580 std::string abilityName;
581 GetAbilityName(env, abilityName);
582 asyncContext->options.SetParam(Constants::KEY_CALLER_ABILITY_NAME, abilityName);
583 JSAuthCallback callback;
584 ParseJSAuthCallback(env, argv[index], callback);
585 asyncContext->appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(env, callback);
586 }
587
ParseContextForGetOAuthToken(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)588 bool ParseContextForGetOAuthToken(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
589 {
590 size_t argc = ARGS_SIZE_FOUR;
591 napi_value argv[ARGS_SIZE_FOUR] = {0};
592 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
593 if (argc < ARGS_SIZE_THREE) {
594 asyncContext->errMsg = "the number of parameter should be at least 3";
595 return false;
596 }
597 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], asyncContext->callbackRef, 1))) {
598 asyncContext->errMsg = "the callback is not a function";
599 return false;
600 }
601 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
602 asyncContext->errMsg = "the name is not a string";
603 return false;
604 }
605 if (!GetStringProperty(env, argv[1], asyncContext->owner)) {
606 asyncContext->errMsg = "the owner is not a string";
607 return false;
608 }
609 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->authType)) {
610 asyncContext->errMsg = "the authType is not a string";
611 return false;
612 }
613 return true;
614 }
615
ParseContextForSetOAuthToken(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)616 bool ParseContextForSetOAuthToken(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
617 {
618 size_t argc = ARGS_SIZE_FOUR;
619 napi_value argv[ARGS_SIZE_FOUR] = {0};
620 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
621 if (argc < ARGS_SIZE_THREE) {
622 asyncContext->errMsg = "the number of parameter should be at least 3";
623 return false;
624 }
625 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], asyncContext->callbackRef, 1))) {
626 asyncContext->errMsg = "the callback is not a function";
627 return false;
628 }
629 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
630 asyncContext->errMsg = "the name is not a string";
631 return false;
632 }
633 if (!GetStringProperty(env, argv[1], asyncContext->authType)) {
634 asyncContext->errMsg = "the authType is not a string";
635 return false;
636 }
637 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->token)) {
638 asyncContext->errMsg = "the token is not a string";
639 return false;
640 }
641 return true;
642 }
643
ParseContextForDeleteOAuthToken(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)644 bool ParseContextForDeleteOAuthToken(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
645 {
646 size_t argc = ARGS_SIZE_FIVE;
647 napi_value argv[ARGS_SIZE_FIVE] = {0};
648 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
649 if (argc < ARGS_SIZE_FOUR) {
650 asyncContext->errMsg = "the number of parameter should be at least 4";
651 return false;
652 }
653 if ((argc == ARGS_SIZE_FIVE) && (!GetCallbackProperty(env, argv[PARAMFOUR], asyncContext->callbackRef, 1))) {
654 asyncContext->errMsg = "the callback is not a function";
655 return false;
656 }
657 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
658 asyncContext->errMsg = "the name is not a string";
659 return false;
660 }
661 if (!GetStringProperty(env, argv[1], asyncContext->owner)) {
662 asyncContext->errMsg = "the owner is not a string";
663 return false;
664 }
665 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->authType)) {
666 asyncContext->errMsg = "the authType is not a string";
667 return false;
668 }
669 if (!GetStringProperty(env, argv[PARAMTHREE], asyncContext->token)) {
670 asyncContext->errMsg = "the token is not a string";
671 return false;
672 }
673 return true;
674 }
675
ParseContextForSetOAuthTokenVisibility(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)676 bool ParseContextForSetOAuthTokenVisibility(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
677 {
678 size_t argc = ARGS_SIZE_FIVE;
679 napi_value argv[ARGS_SIZE_FIVE] = {0};
680 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
681 if (argc < ARGS_SIZE_FOUR) {
682 asyncContext->errMsg = "the number of parameter should be at least 4";
683 return false;
684 }
685 if ((argc == ARGS_SIZE_FIVE) && (!GetCallbackProperty(env, argv[PARAMFOUR], asyncContext->callbackRef, 1))) {
686 asyncContext->errMsg = "the callback is not a function";
687 return false;
688 }
689 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
690 asyncContext->errMsg = "the name is not a string";
691 return false;
692 }
693 if (!GetStringProperty(env, argv[1], asyncContext->authType)) {
694 asyncContext->errMsg = "the authType is not a string";
695 return false;
696 }
697 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->bundleName)) {
698 asyncContext->errMsg = "the bundleName is not a string";
699 return false;
700 }
701 if (napi_get_value_bool(env, argv[PARAMTHREE], &asyncContext->isVisible) != napi_ok) {
702 asyncContext->errMsg = "the isVisible is not a bool value";
703 return false;
704 }
705 return true;
706 }
707
ParseContextForCheckOAuthTokenVisibility(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)708 bool ParseContextForCheckOAuthTokenVisibility(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
709 {
710 size_t argc = ARGS_SIZE_FOUR;
711 napi_value argv[ARGS_SIZE_FOUR] = {0};
712 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
713 if (argc < ARGS_SIZE_THREE) {
714 asyncContext->errMsg = "the number of parameter should be at least 3";
715 return false;
716 }
717 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], asyncContext->callbackRef, 1))) {
718 asyncContext->errMsg = "the callback is not a function";
719 return false;
720 }
721 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
722 asyncContext->errMsg = "the name is not a string";
723 return false;
724 }
725 if (!GetStringProperty(env, argv[1], asyncContext->authType)) {
726 asyncContext->errMsg = "the authType is not a string";
727 return false;
728 }
729 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->bundleName)) {
730 asyncContext->errMsg = "the bundleName is not a string";
731 return false;
732 }
733 return true;
734 }
735
ParseContextForGetAuthenticatorInfo(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)736 bool ParseContextForGetAuthenticatorInfo(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
737 {
738 size_t argc = ARGS_SIZE_TWO;
739 napi_value argv[ARGS_SIZE_TWO] = {0};
740 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
741 if (argc < ARGS_SIZE_ONE) {
742 asyncContext->errMsg = "the number of parameter should be at least 1";
743 return false;
744 }
745 if ((argc == ARGS_SIZE_TWO) && (!GetCallbackProperty(env, argv[1], asyncContext->callbackRef, 1))) {
746 asyncContext->errMsg = "the callback is not a function";
747 return false;
748 }
749 if (!GetStringProperty(env, argv[0], asyncContext->owner)) {
750 asyncContext->errMsg = "the owner is not a string";
751 return false;
752 }
753 return true;
754 }
755
ParseContextForGetAllOAuthTokens(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)756 bool ParseContextForGetAllOAuthTokens(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
757 {
758 size_t argc = ARGS_SIZE_THREE;
759 napi_value argv[ARGS_SIZE_THREE] = {0};
760 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
761 if (argc < ARGS_SIZE_TWO) {
762 asyncContext->errMsg = "the number of parameter should be at least 2";
763 return false;
764 }
765 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
766 asyncContext->errMsg = "the callback is not a function";
767 return false;
768 }
769 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
770 asyncContext->errMsg = "the name is not a string";
771 return false;
772 }
773 if (!GetStringProperty(env, argv[1], asyncContext->owner)) {
774 asyncContext->errMsg = "the owner is not a string";
775 return false;
776 }
777 return true;
778 }
779
ParseContextForGetOAuthList(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)780 bool ParseContextForGetOAuthList(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
781 {
782 size_t argc = ARGS_SIZE_THREE;
783 napi_value argv[ARGS_SIZE_THREE] = {0};
784 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
785 if (argc < ARGS_SIZE_TWO) {
786 asyncContext->errMsg = "the number of parameter should be at least 2";
787 return false;
788 }
789 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
790 asyncContext->errMsg = "the callback is not a function";
791 return false;
792 }
793 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
794 asyncContext->errMsg = "the name is not a string";
795 return false;
796 }
797 if (!GetStringProperty(env, argv[1], asyncContext->authType)) {
798 asyncContext->errMsg = "the authType is not a string";
799 return false;
800 }
801 return true;
802 }
803
ParseContextForGetAuthenticatorCallback(napi_env env,napi_callback_info cbInfo,OAuthAsyncContext * asyncContext)804 bool ParseContextForGetAuthenticatorCallback(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext)
805 {
806 size_t argc = ARGS_SIZE_TWO;
807 napi_value argv[ARGS_SIZE_TWO] = {0};
808 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
809 if (argc < ARGS_SIZE_ONE) {
810 asyncContext->errMsg = "the number of parameters should be at least 1";
811 return false;
812 }
813 if ((argc == ARGS_SIZE_TWO) && (!GetCallbackProperty(env, argv[1], asyncContext->callbackRef, 1))) {
814 asyncContext->errMsg = "the callback is not a function";
815 return false;
816 }
817 if (!GetStringProperty(env, argv[0], asyncContext->sessionId)) {
818 asyncContext->errMsg = "the sessionId is not a string";
819 return false;
820 }
821 return true;
822 }
823
ParseContextWithBdName(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)824 bool ParseContextWithBdName(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
825 {
826 size_t argc = ARGS_SIZE_THREE;
827 napi_value argv[ARGS_SIZE_THREE] = {0};
828 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
829 if (argc < ARGS_SIZE_TWO) {
830 asyncContext->errMsg = "the number of parameters should be at least 2";
831 return false;
832 }
833 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
834 asyncContext->errMsg = "the callback is not a function";
835 return false;
836 }
837 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
838 asyncContext->errMsg = "the name is not a string";
839 return false;
840 }
841 if (!GetStringProperty(env, argv[1], asyncContext->bundleName)) {
842 asyncContext->errMsg = "the bundleName is not a string";
843 return false;
844 }
845 return true;
846 }
847
ParseContextForSetAppAccess(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)848 bool ParseContextForSetAppAccess(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
849 {
850 size_t argc = ARGS_SIZE_FOUR;
851 napi_value argv[ARGS_SIZE_FOUR] = {0};
852 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
853 if (argc < ARGS_SIZE_THREE) {
854 asyncContext->errMsg = "the number of parameters should be at least 2";
855 return false;
856 }
857 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], asyncContext->callbackRef, 1))) {
858 asyncContext->errMsg = "the callback is not a function";
859 return false;
860 }
861 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
862 asyncContext->errMsg = "the name is not a string";
863 return false;
864 }
865 if (!GetStringProperty(env, argv[1], asyncContext->bundleName)) {
866 asyncContext->errMsg = "the bundleName is not a string";
867 return false;
868 }
869 if (napi_get_value_bool(env, argv[PARAMTWO], &asyncContext->isAccessible) != napi_ok) {
870 asyncContext->errMsg = "the isAccessible is not a bool value";
871 return false;
872 }
873 return true;
874 }
875
ParseContextWithIsEnable(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)876 bool ParseContextWithIsEnable(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
877 {
878 size_t argc = ARGS_SIZE_THREE;
879 napi_value argv[ARGS_SIZE_THREE] = {0};
880 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
881 if (argc < ARGS_SIZE_TWO) {
882 asyncContext->errMsg = "the number of parameters should be at least 2";
883 return false;
884 }
885 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
886 asyncContext->errMsg = "the callback is not a function";
887 return false;
888 }
889 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
890 asyncContext->errMsg = "the name is not a string";
891 return false;
892 }
893 if (napi_get_value_bool(env, argv[1], &asyncContext->isEnable) != napi_ok) {
894 asyncContext->errMsg = "the isEnable is not a string";
895 return false;
896 }
897 return true;
898 }
899
ParseContextWithTwoPara(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)900 bool ParseContextWithTwoPara(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
901 {
902 size_t argc = ARGS_SIZE_TWO;
903 napi_value argv[ARGS_SIZE_TWO] = {0};
904 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
905 if (argc < ARGS_SIZE_ONE) {
906 asyncContext->errMsg = "the number of parameters should be at least 1";
907 return false;
908 }
909 if ((argc == ARGS_SIZE_TWO) && (!GetCallbackProperty(env, argv[1], asyncContext->callbackRef, 1))) {
910 asyncContext->errMsg = "the callback is not a function";
911 return false;
912 }
913 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
914 asyncContext->errMsg = "the name is not a string";
915 return false;
916 }
917 return true;
918 }
919
ParseContextToSetCredential(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)920 bool ParseContextToSetCredential(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
921 {
922 size_t argc = ARGS_SIZE_FOUR;
923 napi_value argv[ARGS_SIZE_FOUR] = {0};
924 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
925 if (argc < ARGS_SIZE_THREE) {
926 asyncContext->errMsg = "the number of parameters should be at least 3";
927 return false;
928 }
929 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], asyncContext->callbackRef, 1))) {
930 asyncContext->errMsg = "the callback is not a function";
931 return false;
932 }
933 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
934 asyncContext->errMsg = "the name is not a string";
935 return false;
936 }
937 if (!GetStringProperty(env, argv[1], asyncContext->credentialType)) {
938 asyncContext->errMsg = "the credentialType is not a string";
939 return false;
940 }
941 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->credential)) {
942 asyncContext->errMsg = "the credential is not a string";
943 return false;
944 }
945 return true;
946 }
947
ParseContextForAssociatedData(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)948 bool ParseContextForAssociatedData(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
949 {
950 size_t argc = ARGS_SIZE_FOUR;
951 napi_value argv[ARGS_SIZE_FOUR] = {0};
952 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
953 if (argc < ARGS_SIZE_THREE) {
954 asyncContext->errMsg = "the number of parameters should be at least 3";
955 return false;
956 }
957 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], asyncContext->callbackRef, 1))) {
958 asyncContext->errMsg = "the callback is not a function";
959 return false;
960 }
961 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
962 asyncContext->errMsg = "the name is not a string";
963 return false;
964 }
965 if (!GetStringProperty(env, argv[1], asyncContext->key)) {
966 asyncContext->errMsg = "the key is not a string";
967 return false;
968 }
969 if (!GetStringProperty(env, argv[PARAMTWO], asyncContext->value)) {
970 asyncContext->errMsg = "the value is not a string";
971 return false;
972 }
973 return true;
974 }
975
ParseContextToGetData(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)976 bool ParseContextToGetData(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
977 {
978 size_t argc = ARGS_SIZE_THREE;
979 napi_value argv[ARGS_SIZE_THREE] = {0};
980 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
981 if (argc < ARGS_SIZE_TWO) {
982 asyncContext->errMsg = "the number of parameter shoulde be 3";
983 return false;
984 }
985 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
986 asyncContext->errMsg = "the callback is not a function";
987 return false;
988 }
989 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
990 asyncContext->errMsg = "the name is not a string";
991 return false;
992 }
993 if (!GetStringProperty(env, argv[1], asyncContext->key)) {
994 asyncContext->errMsg = "the key is not a string";
995 return false;
996 }
997 return true;
998 }
999
ParseContextCBArray(napi_env env,napi_callback_info cbInfo,GetAccountsAsyncContext * asyncContext)1000 bool ParseContextCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext)
1001 {
1002 size_t argc = ARGS_SIZE_ONE;
1003 napi_value argv[ARGS_SIZE_ONE] = {0};
1004 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1005 if ((argc == ARGS_SIZE_ONE) && (!GetCallbackProperty(env, argv[0], asyncContext->callbackRef, 1))) {
1006 asyncContext->errMsg = "the callback is not a function";
1007 return false;
1008 }
1009 return true;
1010 }
1011
ParseContextWithCredentialType(napi_env env,napi_callback_info cbInfo,AppAccountAsyncContext * asyncContext)1012 bool ParseContextWithCredentialType(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext)
1013 {
1014 size_t argc = ARGS_SIZE_THREE;
1015 napi_value argv[ARGS_SIZE_THREE] = {0};
1016 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1017 if (argc < ARGS_SIZE_TWO) {
1018 asyncContext->errMsg = "the number of parameter should be at least 2";
1019 return false;
1020 }
1021 if ((argc == ARGS_SIZE_THREE) && (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1))) {
1022 asyncContext->errMsg = "the callback is not a function";
1023 return false;
1024 }
1025 if (!GetStringProperty(env, argv[0], asyncContext->name)) {
1026 asyncContext->errMsg = "the name is not a string";
1027 return false;
1028 }
1029 if (!GetStringProperty(env, argv[1], asyncContext->credentialType)) {
1030 asyncContext->errMsg = "the credentialType is not a string";
1031 return false;
1032 }
1033 return true;
1034 }
1035
ParseContextWithStrCBArray(napi_env env,napi_callback_info cbInfo,GetAccountsAsyncContext * asyncContext)1036 bool ParseContextWithStrCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext)
1037 {
1038 size_t argc = ARGS_SIZE_TWO;
1039 napi_value argv[ARGS_SIZE_TWO] = {0};
1040 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1041 if (argc < ARGS_SIZE_ONE) {
1042 asyncContext->errMsg = "the number of parameter should be at least 2";
1043 return false;
1044 }
1045 if ((argc == ARGS_SIZE_TWO) && (!GetCallbackProperty(env, argv[1], asyncContext->callbackRef, 1))) {
1046 asyncContext->errMsg = "the callback is not a function";
1047 return false;
1048 }
1049 if (!GetStringProperty(env, argv[0], asyncContext->owner)) {
1050 asyncContext->errMsg = "the owner is not a string";
1051 return false;
1052 }
1053 return true;
1054 }
1055
ParseParametersBySubscribe(const napi_env & env,napi_callback_info cbInfo,AsyncContextForSubscribe * context)1056 bool ParseParametersBySubscribe(const napi_env &env, napi_callback_info cbInfo, AsyncContextForSubscribe *context)
1057 {
1058 size_t argc = SUBSCRIBE_MAX_PARA;
1059 napi_value argv[SUBSCRIBE_MAX_PARA] = {nullptr};
1060 napi_value thisVar = nullptr;
1061 napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, nullptr);
1062 context->errCode = ERR_JS_PARAMETER_ERROR;
1063 if (argc != SUBSCRIBE_MAX_PARA) {
1064 context->errMsg = "the number of parameters should be 3";
1065 return false;
1066 }
1067 if (!GetStringProperty(env, argv[0], context->type)) {
1068 context->errMsg = "the type is not a string";
1069 return false;
1070 }
1071 if ((context->type != "change") && (context->type != "accountChange")) {
1072 context->errMsg = "the type is invalid";
1073 context->errCode = ERR_JS_INVALID_PARAMETER;
1074 return false;
1075 }
1076 bool isArray = false;
1077 napi_is_array(env, argv[1], &isArray);
1078 if (!isArray) {
1079 context->errMsg = "the owners is not a string array";
1080 return false;
1081 }
1082 uint32_t length = 0;
1083 napi_get_array_length(env, argv[1], &length);
1084 if (length == 0) {
1085 context->errMsg = "the owers should not be empty";
1086 context->errCode = ERR_JS_INVALID_PARAMETER;
1087 return false;
1088 }
1089 for (size_t i = 0; i < length; i++) {
1090 napi_value ownerStr = nullptr;
1091 napi_get_element(env, argv[1], i, &ownerStr);
1092 std::string owner;
1093 if (!GetStringProperty(env, ownerStr, owner)) {
1094 context->errMsg = "the owners is not a string array";
1095 return false;
1096 }
1097 context->owners.emplace_back(owner);
1098 }
1099 if (!GetCallbackProperty(env, argv[PARAMTWO], context->callbackRef, 1)) {
1100 context->errMsg = "the callback is not a function";
1101 return false;
1102 }
1103 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&context->appAccountManager));
1104 return true;
1105 }
1106
GetSubscriberByUnsubscribe(const napi_env & env,std::vector<std::shared_ptr<SubscriberPtr>> & subscribers,AsyncContextForUnsubscribe * asyncContextForOff,bool & isFind)1107 napi_value GetSubscriberByUnsubscribe(const napi_env &env, std::vector<std::shared_ptr<SubscriberPtr>> &subscribers,
1108 AsyncContextForUnsubscribe *asyncContextForOff, bool &isFind)
1109 {
1110 napi_value result;
1111
1112 {
1113 std::lock_guard<std::mutex> lock(g_lockForAppAccountSubscribers);
1114
1115 for (auto subscriberInstance : g_AppAccountSubscribers) {
1116 if (subscriberInstance.first == asyncContextForOff->appAccountManager) {
1117 for (auto item : subscriberInstance.second) {
1118 subscribers.emplace_back(item->subscriber);
1119 }
1120 isFind = true;
1121 break;
1122 }
1123 }
1124 }
1125
1126 NAPI_CALL(env, napi_get_boolean(env, isFind, &result));
1127 return result;
1128 }
1129
ParseParametersByUnsubscribe(const napi_env & env,napi_callback_info cbInfo,AsyncContextForUnsubscribe * context)1130 bool ParseParametersByUnsubscribe(
1131 const napi_env &env, napi_callback_info cbInfo, AsyncContextForUnsubscribe *context)
1132 {
1133 size_t argc = UNSUBSCRIBE_MAX_PARA;
1134 napi_value argv[UNSUBSCRIBE_MAX_PARA] = {nullptr};
1135 napi_value thisVar = nullptr;
1136 NAPI_CALL_BASE(env, napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, NULL), false);
1137 if (argc < 1) {
1138 context->errMsg = "the number of parameters should be at least 1";
1139 context->errCode = ERR_JS_PARAMETER_ERROR;
1140 return false;
1141 }
1142 if (!GetStringProperty(env, argv[0], context->type)) {
1143 context->errMsg = "the type is not a string";
1144 context->errCode = ERR_JS_PARAMETER_ERROR;
1145 return false;
1146 }
1147 if ((context->type != "change") && (context->type != "accountChange")) {
1148 context->errMsg = "the type is invalid";
1149 context->errCode = ERR_JS_INVALID_PARAMETER;
1150 return false;
1151 }
1152 if ((argc == UNSUBSCRIBE_MAX_PARA) && (!GetCallbackProperty(env, argv[1], context->callbackRef, 1))) {
1153 context->errMsg = "the callback is not a function";
1154 context->errCode = ERR_JS_PARAMETER_ERROR;
1155 return false;
1156 }
1157 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&context->appAccountManager));
1158 if (context->appAccountManager == nullptr) {
1159 ACCOUNT_LOGE("appAccountManager is nullptr");
1160 return false;
1161 }
1162 context->argc = argc;
1163 return true;
1164 }
1165
UnsubscribeExecuteCB(napi_env env,void * data)1166 void UnsubscribeExecuteCB(napi_env env, void *data)
1167 {
1168 AsyncContextForUnsubscribe *asyncContextForOff = reinterpret_cast<AsyncContextForUnsubscribe *>(data);
1169 for (auto offSubscriber : asyncContextForOff->subscribers) {
1170 int errCode = AppAccountManager::UnsubscribeAppAccount(offSubscriber);
1171 ACCOUNT_LOGD("Unsubscribe errcode parameter is %{public}d", errCode);
1172 }
1173 }
1174
UnsubscribeCallbackCompletedCB(napi_env env,napi_status status,void * data)1175 void UnsubscribeCallbackCompletedCB(napi_env env, napi_status status, void *data)
1176 {
1177 AsyncContextForUnsubscribe *asyncContextForOff = reinterpret_cast<AsyncContextForUnsubscribe *>(data);
1178 if (asyncContextForOff == nullptr) {
1179 return;
1180 }
1181
1182 if (asyncContextForOff->argc >= UNSUBSCRIBE_MAX_PARA) {
1183 napi_value result = nullptr;
1184 napi_get_null(env, &result);
1185 napi_value results[ARGS_SIZE_ONE] = {result};
1186 NapiCallVoidFunction(env, results, ARGS_SIZE_ONE, asyncContextForOff->callbackRef);
1187 }
1188
1189 if (asyncContextForOff->callbackRef != nullptr) {
1190 napi_delete_reference(env, asyncContextForOff->callbackRef);
1191 }
1192
1193 napi_delete_async_work(env, asyncContextForOff->work);
1194
1195 {
1196 std::lock_guard<std::mutex> lock(g_lockForAppAccountSubscribers);
1197 ACCOUNT_LOGD("Erase before g_AppAccountSubscribers.size = %{public}zu", g_AppAccountSubscribers.size());
1198 // erase the info from map
1199 auto subscribe = g_AppAccountSubscribers.find(asyncContextForOff->appAccountManager);
1200 if (subscribe != g_AppAccountSubscribers.end()) {
1201 for (auto offCBInfo : subscribe->second) {
1202 napi_delete_reference(env, offCBInfo->callbackRef);
1203 delete offCBInfo;
1204 }
1205 g_AppAccountSubscribers.erase(subscribe);
1206 }
1207 ACCOUNT_LOGD("Erase end g_AppAccountSubscribers.size = %{public}zu", g_AppAccountSubscribers.size());
1208 }
1209 delete asyncContextForOff;
1210 asyncContextForOff = nullptr;
1211 }
1212
ParseVerifyCredentialOptions(napi_env env,napi_value object,VerifyCredentialOptions & options)1213 bool ParseVerifyCredentialOptions(napi_env env, napi_value object, VerifyCredentialOptions &options)
1214 {
1215 napi_valuetype valueType = napi_undefined;
1216 napi_typeof(env, object, &valueType);
1217 if (valueType != napi_object) {
1218 ACCOUNT_LOGE("the type of object is not napi_object");
1219 return false;
1220 }
1221 napi_value value = nullptr;
1222 bool hasProp = false;
1223 napi_has_named_property(env, object, "credential", &hasProp);
1224 if (hasProp) {
1225 napi_get_named_property(env, object, "credential", &value);
1226 if (!GetStringProperty(env, value, options.credential)) {
1227 return false;
1228 }
1229 }
1230 hasProp = false;
1231 napi_has_named_property(env, object, "credentialType", &hasProp);
1232 if (hasProp) {
1233 napi_get_named_property(env, object, "credentialType", &value);
1234 if (!GetStringProperty(env, value, options.credentialType)) {
1235 return false;
1236 }
1237 }
1238 hasProp = false;
1239 napi_has_named_property(env, object, "parameters", &hasProp);
1240 if (hasProp) {
1241 napi_get_named_property(env, object, "parameters", &value);
1242 if (!AppExecFwk::UnwrapWantParams(env, value, options.parameters)) {
1243 return false;
1244 }
1245 }
1246 return true;
1247 }
1248
ParseSelectAccountsOptions(napi_env env,napi_value object,SelectAccountsOptions & options)1249 bool ParseSelectAccountsOptions(napi_env env, napi_value object, SelectAccountsOptions &options)
1250 {
1251 napi_valuetype valueType = napi_undefined;
1252 napi_typeof(env, object, &valueType);
1253 if (valueType != napi_object) {
1254 return false;
1255 }
1256 napi_value value = nullptr;
1257 napi_has_named_property(env, object, "allowedAccounts", &options.hasAccounts);
1258 if (options.hasAccounts) {
1259 napi_get_named_property(env, object, "allowedAccounts", &value);
1260 if (!ParseAccountVector(env, value, options.allowedAccounts)) {
1261 return false;
1262 }
1263 }
1264 napi_has_named_property(env, object, "allowedOwners", &options.hasOwners);
1265 if (options.hasOwners) {
1266 value = nullptr;
1267 napi_get_named_property(env, object, "allowedOwners", &value);
1268 if (!ParseStringVector(env, value, options.allowedOwners)) {
1269 return false;
1270 }
1271 }
1272 napi_has_named_property(env, object, "requiredLabels", &options.hasLabels);
1273 if (options.hasLabels) {
1274 value = nullptr;
1275 napi_get_named_property(env, object, "requiredLabels", &value);
1276 if (!ParseStringVector(env, value, options.requiredLabels)) {
1277 return false;
1278 }
1279 }
1280 return true;
1281 }
1282
ParseSetPropertiesOptions(napi_env env,napi_value object,SetPropertiesOptions & options)1283 bool ParseSetPropertiesOptions(napi_env env, napi_value object, SetPropertiesOptions &options)
1284 {
1285 napi_valuetype valueType = napi_undefined;
1286 napi_typeof(env, object, &valueType);
1287 if (valueType != napi_object) {
1288 return false;
1289 }
1290 napi_value value = nullptr;
1291 bool hasProp = false;
1292 napi_has_named_property(env, object, "properties", &hasProp);
1293 if (hasProp) {
1294 napi_get_named_property(env, object, "properties", &value);
1295 if (!AppExecFwk::UnwrapWantParams(env, value, options.properties)) {
1296 return false;
1297 }
1298 }
1299 hasProp = false;
1300 napi_has_named_property(env, object, "parameters", &hasProp);
1301 if (hasProp) {
1302 napi_get_named_property(env, object, "parameters", &value);
1303 if (!AppExecFwk::UnwrapWantParams(env, value, options.parameters)) {
1304 return false;
1305 }
1306 }
1307 return true;
1308 }
1309
GetNamedFunction(napi_env env,napi_value object,const std::string & name,napi_ref & funcRef)1310 bool GetNamedFunction(napi_env env, napi_value object, const std::string &name, napi_ref &funcRef)
1311 {
1312 napi_value value = nullptr;
1313 napi_get_named_property(env, object, name.c_str(), &value);
1314 return GetCallbackProperty(env, value, funcRef, 1);
1315 }
1316
ParseJSAuthCallback(napi_env env,napi_value object,JSAuthCallback & callback)1317 bool ParseJSAuthCallback(napi_env env, napi_value object, JSAuthCallback &callback)
1318 {
1319 napi_valuetype valueType = napi_undefined;
1320 napi_typeof(env, object, &valueType);
1321 if (valueType != napi_object) {
1322 return false;
1323 }
1324 bool hasProp = false;
1325 napi_has_named_property(env, object, "onRequestContinued", &hasProp);
1326 if (hasProp && (!GetNamedFunction(env, object, "onRequestContinued", callback.onRequestContinued))) {
1327 return false;
1328 }
1329 return GetNamedFunction(env, object, "onResult", callback.onResult) ||
1330 GetNamedFunction(env, object, "onRequestRedirected", callback.onRequestRedirected);
1331 }
1332
ParseContextForVerifyCredential(napi_env env,napi_callback_info info,VerifyCredentialContext * context)1333 bool ParseContextForVerifyCredential(napi_env env, napi_callback_info info, VerifyCredentialContext *context)
1334 {
1335 size_t argc = ARGS_SIZE_FOUR;
1336 napi_value argv[ARGS_SIZE_FOUR] = {0};
1337 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1338 if (argc < ARGS_SIZE_THREE) {
1339 context->errMsg = "the number of parameter shoulde be at least 3";
1340 return false;
1341 }
1342 int32_t index = 0;
1343 if (!GetStringProperty(env, argv[index++], context->name)) {
1344 context->errMsg = "the name is not a string";
1345 return false;
1346 }
1347 if (!GetStringProperty(env, argv[index++], context->owner)) {
1348 context->errMsg = "the owner is not a string";
1349 return false;
1350 }
1351 if ((argc == ARGS_SIZE_FOUR) && (!ParseVerifyCredentialOptions(env, argv[index++], context->options))) {
1352 context->errMsg = "the type of options is not VerifyCredentialOptions";
1353 return false;
1354 }
1355 if (!ParseJSAuthCallback(env, argv[index], context->callback)) {
1356 context->errMsg = "the type of callback is not AuthCallback";
1357 return false;
1358 }
1359 return true;
1360 }
1361
ParseContextForSetProperties(napi_env env,napi_callback_info info,SetPropertiesContext * context)1362 bool ParseContextForSetProperties(napi_env env, napi_callback_info info, SetPropertiesContext *context)
1363 {
1364 size_t argc = ARGS_SIZE_THREE;
1365 napi_value argv[ARGS_SIZE_THREE] = {0};
1366 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1367 if (argc < ARGS_SIZE_TWO) {
1368 context->errMsg = "the number of parameter shoulde be at least 2";
1369 return false;
1370 }
1371 int32_t index = 0;
1372 if (!GetStringProperty(env, argv[index++], context->owner)) {
1373 context->errMsg = "the owner is not a string";
1374 return false;
1375 }
1376 if (argc == ARGS_SIZE_THREE) {
1377 if (!ParseSetPropertiesOptions(env, argv[index++], context->options)) {
1378 context->errMsg = "the type of options is not SetPropertiesOptions";
1379 return false;
1380 }
1381 }
1382 if (!ParseJSAuthCallback(env, argv[index], context->callback)) {
1383 context->errMsg = "the type of callback is not AuthCallback";
1384 return false;
1385 }
1386 return true;
1387 }
1388
ParseContextForSelectAccount(napi_env env,napi_callback_info info,SelectAccountsContext * context)1389 bool ParseContextForSelectAccount(napi_env env, napi_callback_info info, SelectAccountsContext *context)
1390 {
1391 size_t argc = ARGS_SIZE_TWO;
1392 napi_value argv[ARGS_SIZE_TWO] = {0};
1393 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1394 if (argc < ARGS_SIZE_ONE) {
1395 context->errMsg = "the number of parameter shoulde be at least 1";
1396 return false;
1397 }
1398 if ((argc == ARGS_SIZE_TWO) && (!GetCallbackProperty(env, argv[PARAMONE], context->callbackRef, PARAMTWO))) {
1399 context->errMsg = "the callback is not a function";
1400 return false;
1401 }
1402 if (!ParseSelectAccountsOptions(env, argv[0], context->options)) {
1403 context->errMsg = "the type of options is not SelectAccountsOptions";
1404 return false;
1405 }
1406 return true;
1407 }
1408
GetArrayLength(napi_env env,napi_value value,uint32_t & length)1409 bool GetArrayLength(napi_env env, napi_value value, uint32_t &length)
1410 {
1411 bool isArray = false;
1412 napi_is_array(env, value, &isArray);
1413 if (!isArray) {
1414 ACCOUNT_LOGE("wrong argument type, array expected");
1415 return false;
1416 }
1417 napi_get_array_length(env, value, &length);
1418 return true;
1419 }
1420
ParseAccountVector(napi_env env,napi_value value,std::vector<std::pair<std::string,std::string>> & accountVec)1421 bool ParseAccountVector(napi_env env, napi_value value, std::vector<std::pair<std::string, std::string>> &accountVec)
1422 {
1423 uint32_t length = 0;
1424 if (!GetArrayLength(env, value, length)) {
1425 return false;
1426 }
1427 napi_valuetype valueType = napi_undefined;
1428 for (uint32_t i = 0; i < length; ++i) {
1429 napi_value item = nullptr;
1430 napi_get_element(env, value, i, &item);
1431 NAPI_CALL_BASE(env, napi_typeof(env, item, &valueType), false);
1432 if (valueType != napi_object) {
1433 ACCOUNT_LOGD("Wrong argument type, Object expected");
1434 return false;
1435 }
1436 std::string name;
1437 if (!GetStringPropertyByKey(env, item, "name", name)) {
1438 return false;
1439 }
1440 std::string owner;
1441 if (!GetStringPropertyByKey(env, item, "owner", owner)) {
1442 return false;
1443 }
1444 accountVec.push_back(std::make_pair(owner, name));
1445 }
1446 return true;
1447 }
1448
ParseStringVector(napi_env env,napi_value value,std::vector<std::string> & strVec)1449 bool ParseStringVector(napi_env env, napi_value value, std::vector<std::string> &strVec)
1450 {
1451 uint32_t length = 0;
1452 if (!GetArrayLength(env, value, length)) {
1453 return false;
1454 }
1455 for (uint32_t i = 0; i < length; ++i) {
1456 napi_value item = nullptr;
1457 napi_get_element(env, value, i, &item);
1458 std::string str;
1459 if (!GetStringProperty(env, item, str)) {
1460 return false;
1461 }
1462 strVec.push_back(str);
1463 }
1464 return true;
1465 }
1466
ParseContextForCheckAccountLabels(napi_env env,napi_callback_info info,CheckAccountLabelsContext * context)1467 bool ParseContextForCheckAccountLabels(napi_env env, napi_callback_info info, CheckAccountLabelsContext *context)
1468 {
1469 size_t argc = ARGS_SIZE_FOUR;
1470 napi_value argv[ARGS_SIZE_FOUR] = {0};
1471 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1472 if (argc < ARGS_SIZE_THREE) {
1473 context->errMsg = "the number of parameter should be at least 3";
1474 return false;
1475 }
1476 if ((argc == ARGS_SIZE_FOUR) && (!GetCallbackProperty(env, argv[PARAMTHREE], context->callbackRef, PARAMTWO))) {
1477 context->errMsg = "the callback is not a function";
1478 return false;
1479 }
1480 if (!GetStringProperty(env, argv[0], context->name)) {
1481 context->errMsg = "the name is not a string";
1482 return false;
1483 }
1484 if (!GetStringProperty(env, argv[PARAMONE], context->owner)) {
1485 context->errMsg = "the owner is not a string";
1486 return false;
1487 }
1488 if (!ParseStringVector(env, argv[PARAMTWO], context->labels)) {
1489 context->errMsg = "the labels is not a string vector";
1490 return false;
1491 }
1492 return true;
1493 }
1494
VerifyCredCompleteCB(napi_env env,napi_status status,void * data)1495 void VerifyCredCompleteCB(napi_env env, napi_status status, void *data)
1496 {
1497 (void) status;
1498 auto context = reinterpret_cast<VerifyCredentialContext *>(data);
1499 if ((context->errCode != ERR_JS_SUCCESS) && (context->appAccountMgrCb != nullptr)) {
1500 AAFwk::Want errResult;
1501 context->appAccountMgrCb->OnResult(context->errCode, errResult);
1502 }
1503 napi_delete_async_work(env, context->work);
1504 delete context;
1505 context = nullptr;
1506 }
1507
ProcessOnResultCallback(napi_env env,JSAuthCallback & callback,int32_t resultCode,const AAFwk::WantParams & result)1508 void ProcessOnResultCallback(
1509 napi_env env, JSAuthCallback &callback, int32_t resultCode, const AAFwk::WantParams &result)
1510 {
1511 napi_value results[ARGS_SIZE_TWO] = {nullptr};
1512 napi_create_int32(env, resultCode, &results[0]);
1513 results[ARGS_SIZE_ONE] = AppExecFwk::WrapWantParams(env, result);
1514 NapiCallVoidFunction(env, results, ARGS_SIZE_TWO, callback.onResult);
1515 if (callback.onResult != nullptr) {
1516 napi_delete_reference(env, callback.onResult);
1517 callback.onResult = nullptr;
1518 }
1519 if (callback.onRequestRedirected != nullptr) {
1520 napi_delete_reference(env, callback.onRequestRedirected);
1521 callback.onRequestRedirected = nullptr;
1522 }
1523 if (callback.onRequestContinued != nullptr) {
1524 napi_delete_reference(env, callback.onRequestContinued);
1525 callback.onRequestContinued = nullptr;
1526 }
1527 }
1528
ParseCreateAccountOptions(napi_env env,napi_value object,CreateAccountOptions & options)1529 bool ParseCreateAccountOptions(napi_env env, napi_value object, CreateAccountOptions &options)
1530 {
1531 bool hasCustomData = false;
1532 napi_has_named_property(env, object, "customData", &hasCustomData);
1533 if (!hasCustomData) {
1534 return true;
1535 }
1536 napi_value customDataValue = nullptr;
1537 napi_get_named_property(env, object, "customData", &customDataValue);
1538 napi_valuetype valueType = napi_undefined;
1539 napi_typeof(env, customDataValue, &valueType);
1540 if (valueType != napi_object) {
1541 ACCOUNT_LOGE("not napi object");
1542 return false;
1543 }
1544 napi_value keyArr = nullptr;
1545 napi_get_property_names(env, customDataValue, &keyArr);
1546 uint32_t keyNum = 0;
1547 napi_get_array_length(env, keyArr, &keyNum);
1548 for (uint32_t i = 0; i < keyNum; ++i) {
1549 napi_value item = nullptr;
1550 napi_get_element(env, keyArr, i, &item);
1551 std::string keyStr;
1552 if (!GetStringProperty(env, item, keyStr)) {
1553 ACCOUNT_LOGE("fail to get string");
1554 return false;
1555 }
1556 napi_value val = nullptr;
1557 napi_get_named_property(env, customDataValue, keyStr.c_str(), &val);
1558 std::string valStr;
1559 if (!GetStringProperty(env, val, valStr)) {
1560 ACCOUNT_LOGE("fail to get string");
1561 return false;
1562 }
1563 options.customData.emplace(keyStr, valStr);
1564 }
1565 return true;
1566 }
1567
ParseCreateAccountImplicitlyOptions(napi_env env,napi_value object,CreateAccountImplicitlyOptions & options)1568 bool ParseCreateAccountImplicitlyOptions(napi_env env, napi_value object, CreateAccountImplicitlyOptions &options)
1569 {
1570 napi_valuetype valueType = napi_undefined;
1571 napi_typeof(env, object, &valueType);
1572 if (valueType != napi_object) {
1573 return false;
1574 }
1575 napi_value value = nullptr;
1576 napi_has_named_property(env, object, "requiredLabels", &options.hasRequiredLabels);
1577 if (options.hasRequiredLabels) {
1578 napi_get_named_property(env, object, "requiredLabels", &value);
1579 if (!ParseStringVector(env, value, options.requiredLabels)) {
1580 return false;
1581 }
1582 }
1583 napi_has_named_property(env, object, "authType", &options.hasAuthType);
1584 if (options.hasAuthType) {
1585 napi_get_named_property(env, object, "authType", &value);
1586 if (!GetStringProperty(env, value, options.authType)) {
1587 return false;
1588 }
1589 }
1590 bool hasParam = false;
1591 napi_has_named_property(env, object, "parameters", &hasParam);
1592 AAFwk::WantParams params;
1593 if (hasParam) {
1594 napi_get_named_property(env, object, "parameters", &value);
1595 if (!AppExecFwk::UnwrapWantParams(env, value, params)) {
1596 return false;
1597 }
1598 }
1599 options.parameters.SetParams(params);
1600 return true;
1601 }
1602
ParseContextForCreateAccount(napi_env env,napi_callback_info cbInfo,CreateAccountContext * context)1603 bool ParseContextForCreateAccount(napi_env env, napi_callback_info cbInfo, CreateAccountContext *context)
1604 {
1605 size_t argc = ARGS_SIZE_THREE;
1606 napi_value argv[ARGS_SIZE_THREE] = {0};
1607 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1608 if (argc < ARGS_SIZE_ONE) {
1609 context->errMsg = "the number of parameters should be at least 1";
1610 return false;
1611 }
1612 for (size_t i = 0; i < argc; i++) {
1613 napi_valuetype valueType = napi_undefined;
1614 napi_typeof(env, argv[i], &valueType);
1615 if (i == 0 && valueType == napi_string) {
1616 GetStringProperty(env, argv[i], context->name);
1617 } else if (i == 1 && valueType == napi_object) {
1618 if (!ParseCreateAccountOptions(env, argv[i], context->options)) {
1619 context->errMsg = "the type of options is not CreateAccountOptions";
1620 return false;
1621 }
1622 } else if (i == 1 && valueType == napi_function) {
1623 napi_create_reference(env, argv[i], 1, &context->callbackRef);
1624 break;
1625 } else if (i == PARAMTWO && valueType == napi_function) {
1626 napi_create_reference(env, argv[i], 1, &context->callbackRef);
1627 break;
1628 } else {
1629 ACCOUNT_LOGE("Type matching failed");
1630 context->errMsg = "the type of param " + std::to_string(i) + " is incorrect";
1631 return false;
1632 }
1633 }
1634 return true;
1635 }
1636
ParseContextForCreateAccountImplicitly(napi_env env,napi_callback_info cbInfo,CreateAccountImplicitlyContext * context)1637 bool ParseContextForCreateAccountImplicitly(
1638 napi_env env, napi_callback_info cbInfo, CreateAccountImplicitlyContext *context)
1639 {
1640 size_t argc = ARGS_SIZE_THREE;
1641 napi_value argv[ARGS_SIZE_THREE] = {0};
1642 napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1643 if (argc < ARGS_SIZE_TWO) {
1644 context->errMsg = "the number of parameters should be at least 2";
1645 return false;
1646 }
1647 size_t index = 0;
1648 if (!GetStringProperty(env, argv[index++], context->owner)) {
1649 context->errMsg = "the type of owner is not string";
1650 return false;
1651 }
1652 if ((argc == ARGS_SIZE_THREE) && (!ParseCreateAccountImplicitlyOptions(env, argv[index++], context->options))) {
1653 context->errMsg = "the type of options is not CreateAccountImplicitlyOptions";
1654 return false;
1655 }
1656 if (!ParseJSAuthCallback(env, argv[index], context->callback)) {
1657 context->errMsg = "the type of callback is not AuthCallback";
1658 return false;
1659 }
1660 std::string abilityName;
1661 GetAbilityName(env, abilityName);
1662 context->options.parameters.SetParam(Constants::KEY_CALLER_ABILITY_NAME, abilityName);
1663 return true;
1664 }
1665
GetAbilityName(napi_env env,std::string & abilityName)1666 bool GetAbilityName(napi_env env, std::string &abilityName)
1667 {
1668 napi_value global;
1669 napi_get_global(env, &global);
1670 napi_value abilityObj;
1671 napi_get_named_property(env, global, "ability", &abilityObj);
1672 if (abilityObj == nullptr) {
1673 return false;
1674 }
1675 AppExecFwk::Ability *ability = nullptr;
1676 napi_get_value_external(env, abilityObj, reinterpret_cast<void **>(&ability));
1677 if (ability == nullptr) {
1678 return false;
1679 }
1680 auto abilityInfo = ability->GetAbilityInfo();
1681 if (abilityInfo == nullptr) {
1682 return false;
1683 }
1684 abilityName = abilityInfo->name;
1685 return true;
1686 }
1687 } // namespace AccountJsKit
1688 } // namespace OHOS
1689