• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "napi_app_account.h"
17 
18 #include <string>
19 #include <cstring>
20 #include <vector>
21 #include "account_log_wrapper.h"
22 #include "app_account_common.h"
23 #include "app_account_manager.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "napi_account_common.h"
27 #include "napi_account_error.h"
28 #include "napi_app_account_common.h"
29 
30 using namespace OHOS::AccountSA;
31 namespace OHOS {
32 namespace AccountJsKit {
Init(napi_env env,napi_value exports)33 napi_value NapiAppAccount::Init(napi_env env, napi_value exports)
34 {
35     napi_property_descriptor descriptor[] = {
36         DECLARE_NAPI_FUNCTION("createAppAccountManager", CreateAppAccountManager),
37     };
38     NAPI_CALL(
39         env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
40 
41     napi_property_descriptor properties[] = {
42         DECLARE_NAPI_FUNCTION("addAccount", AddAccount),
43         DECLARE_NAPI_FUNCTION("addAccountImplicitly", AddAccountImplicitly),
44         DECLARE_NAPI_FUNCTION("deleteAccount", DeleteAccount),
45         DECLARE_NAPI_FUNCTION("disableAppAccess", DisableAppAccess),
46         DECLARE_NAPI_FUNCTION("enableAppAccess", EnableAppAccess),
47         DECLARE_NAPI_FUNCTION("checkAppAccountSyncEnable", CheckAppAccountSyncEnable),
48         DECLARE_NAPI_FUNCTION("setAccountCredential", SetAccountCredential),
49         DECLARE_NAPI_FUNCTION("setAccountExtraInfo", SetAccountExtraInfo),
50         DECLARE_NAPI_FUNCTION("setAppAccountSyncEnable", SetAppAccountSyncEnable),
51         DECLARE_NAPI_FUNCTION("setAssociatedData", SetAssociatedData),
52         DECLARE_NAPI_FUNCTION("authenticate", Authenticate),
53         DECLARE_NAPI_FUNCTION("getAllAccessibleAccounts", GetAllAccessibleAccounts),
54         DECLARE_NAPI_FUNCTION("getAllAccounts", GetAllAccounts),
55         DECLARE_NAPI_FUNCTION("getAccountCredential", GetAccountCredential),
56         DECLARE_NAPI_FUNCTION("getAccountExtraInfo", GetAccountExtraInfo),
57         DECLARE_NAPI_FUNCTION("getAssociatedData", GetAssociatedData),
58         DECLARE_NAPI_FUNCTION("getAssociatedDataSync", GetAssociatedDataSync),
59         DECLARE_NAPI_FUNCTION("getOAuthToken", GetOAuthToken),
60         DECLARE_NAPI_FUNCTION("setOAuthToken", SetOAuthToken),
61         DECLARE_NAPI_FUNCTION("deleteOAuthToken", DeleteOAuthToken),
62         DECLARE_NAPI_FUNCTION("getAuthenticatorInfo", GetAuthenticatorInfo),
63         DECLARE_NAPI_FUNCTION("getAllOAuthTokens", GetAllOAuthTokens),
64         DECLARE_NAPI_FUNCTION("getOAuthList", GetOAuthList),
65         DECLARE_NAPI_FUNCTION("setOAuthTokenVisibility", SetOAuthTokenVisibility),
66         DECLARE_NAPI_FUNCTION("checkOAuthTokenVisibility", CheckOAuthTokenVisibility),
67         DECLARE_NAPI_FUNCTION("getAuthenticatorCallback", GetAuthenticatorCallback),
68         DECLARE_NAPI_FUNCTION("on", Subscribe),
69         DECLARE_NAPI_FUNCTION("off", Unsubscribe),
70         DECLARE_NAPI_FUNCTION("checkAppAccess", CheckAppAccess),
71         DECLARE_NAPI_FUNCTION("checkAccountLabels", CheckAccountLabels),
72         DECLARE_NAPI_FUNCTION("setAuthenticatorProperties", SetAuthenticatorProperties),
73         DECLARE_NAPI_FUNCTION("verifyCredential", VerifyCredential),
74         DECLARE_NAPI_FUNCTION("selectAccountsByOptions", SelectAccountsByOptions),
75         DECLARE_NAPI_FUNCTION("deleteAccountCredential", DeleteAccountCredential),
76         // new api
77         DECLARE_NAPI_FUNCTION("createAccount", CreateAccount),
78         DECLARE_NAPI_FUNCTION("createAccountImplicitly", CreateAccountImplicitly),
79         DECLARE_NAPI_FUNCTION("auth", Auth),
80         DECLARE_NAPI_FUNCTION("removeAccount", RemoveAccount),
81         DECLARE_NAPI_FUNCTION("setAppAccess", SetAppAccess),
82         DECLARE_NAPI_FUNCTION("setCredential", SetCredential),
83         DECLARE_NAPI_FUNCTION("getCredential", GetCredential),
84         DECLARE_NAPI_FUNCTION("deleteCredential", DeleteCredential),
85         DECLARE_NAPI_FUNCTION("setDataSyncEnabled", SetDataSyncEnabled),
86         DECLARE_NAPI_FUNCTION("checkDataSyncEnabled", CheckDataSyncEnabled),
87         DECLARE_NAPI_FUNCTION("setCustomData", SetCustomData),
88         DECLARE_NAPI_FUNCTION("getCustomData", GetCustomData),
89         DECLARE_NAPI_FUNCTION("getCustomDataSync", GetAssociatedDataSync),
90         DECLARE_NAPI_FUNCTION("getAccountsByOwner", GetAccountsByOwner),
91         DECLARE_NAPI_FUNCTION("getAuthToken", GetAuthToken),
92         DECLARE_NAPI_FUNCTION("setAuthToken", SetAuthToken),
93         DECLARE_NAPI_FUNCTION("deleteAuthToken", DeleteAuthToken),
94         DECLARE_NAPI_FUNCTION("getAllAuthTokens", GetAllAuthTokens),
95         DECLARE_NAPI_FUNCTION("getAuthList", GetAuthList),
96         DECLARE_NAPI_FUNCTION("setAuthTokenVisibility", SetAuthTokenVisibility),
97         DECLARE_NAPI_FUNCTION("checkAuthTokenVisibility", CheckAuthTokenVisibility),
98         DECLARE_NAPI_FUNCTION("getAuthCallback", GetAuthCallback),
99         DECLARE_NAPI_FUNCTION("queryAuthenticatorInfo", QueryAuthenticatorInfo)
100     };
101     napi_value cons = nullptr;
102     NAPI_CALL(env, napi_define_class(env, APP_ACCOUNT_CLASS_NAME.c_str(), APP_ACCOUNT_CLASS_NAME.size(),
103         JsConstructor, nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
104     NAPI_CALL(env, napi_create_reference(env, cons, 1, &appAccountRef_));
105     NAPI_CALL(env, napi_set_named_property(env, exports, APP_ACCOUNT_CLASS_NAME.c_str(), cons));
106     return exports;
107 }
108 
JsConstructor(napi_env env,napi_callback_info cbinfo)109 napi_value NapiAppAccount::JsConstructor(napi_env env, napi_callback_info cbinfo)
110 {
111     napi_value thisVar = nullptr;
112     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
113     return thisVar;
114 }
115 
CreateAppAccountManager(napi_env env,napi_callback_info cbInfo)116 napi_value NapiAppAccount::CreateAppAccountManager(napi_env env, napi_callback_info cbInfo)
117 {
118     napi_value instance = nullptr;
119     napi_value cons = nullptr;
120     if (napi_get_reference_value(env, appAccountRef_, &cons) != napi_ok) {
121         return nullptr;
122     }
123 
124     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
125         return nullptr;
126     }
127 
128     AppAccountManager *objectInfo = new (std::nothrow) AppAccountManager();
129     if (objectInfo == nullptr) {
130         ACCOUNT_LOGE("failed to create AppAccountManager for insufficient memory");
131         return nullptr;
132     }
133     napi_status status = napi_wrap(env, instance, objectInfo,
134         [](napi_env env, void *data, void *hint) {
135             ACCOUNT_LOGI("js AppAccountManager instance garbage collection");
136             delete reinterpret_cast<AppAccountManager *>(data);
137         }, nullptr, nullptr);
138     if (status != napi_ok) {
139         ACCOUNT_LOGE("failed to wrap js instance with native object");
140         delete objectInfo;
141         return nullptr;
142     }
143     return instance;
144 }
145 
AddAccount(napi_env env,napi_callback_info cbInfo)146 napi_value NapiAppAccount::AddAccount(napi_env env, napi_callback_info cbInfo)
147 {
148     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
149     if (asyncContext == nullptr) {
150         ACCOUNT_LOGE("insufficient memory for asyncContext!");
151         return NapiGetNull(env);
152     }
153     asyncContext->env = env;
154     ParseContextWithExInfo(env, cbInfo, asyncContext);
155     napi_value result = nullptr;
156     if (asyncContext->callbackRef == nullptr) {
157         napi_create_promise(env, &asyncContext->deferred, &result);
158     } else {
159         napi_get_undefined(env, &result);
160     }
161     napi_value resource = nullptr;
162     napi_create_string_utf8(env, "AddAccountInternal", NAPI_AUTO_LENGTH, &resource);
163     napi_create_async_work(env,
164         nullptr,
165         resource,
166         [](napi_env env, void *data) {
167             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
168             asyncContext->errCode = AppAccountManager::AddAccount(asyncContext->name, asyncContext->extraInfo);
169         },
170         [](napi_env env, napi_status status, void *data) {
171             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
172             ProcessCallbackOrPromise(env, asyncContext,
173                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode)), NapiGetNull(env));
174             napi_delete_async_work(env, asyncContext->work);
175             delete asyncContext;
176             asyncContext = nullptr;
177         },
178         reinterpret_cast<void *>(asyncContext),
179         &asyncContext->work);
180     napi_queue_async_work(env, asyncContext->work);
181     return result;
182 }
183 
CreateAccount(napi_env env,napi_callback_info cbInfo)184 napi_value NapiAppAccount::CreateAccount(napi_env env, napi_callback_info cbInfo)
185 {
186     auto *context = new (std::nothrow) CreateAccountContext(env);
187     if (context == nullptr) {
188         ACCOUNT_LOGE("insufficient memory for asyncContext!");
189         return NapiGetNull(env);
190     }
191     if (!ParseContextForCreateAccount(env, cbInfo, context)) {
192         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
193         delete context;
194         return NapiGetNull(env);
195     }
196     napi_value result = nullptr;
197     if (context->callbackRef == nullptr) {
198         napi_create_promise(env, &context->deferred, &result);
199     } else {
200         napi_get_undefined(env, &result);
201     }
202     napi_value resource = nullptr;
203     napi_create_string_utf8(env, "CreateAccount", NAPI_AUTO_LENGTH, &resource);
204     napi_create_async_work(env, nullptr, resource,
205         [](napi_env env, void *data) {
206             CreateAccountContext *context = reinterpret_cast<CreateAccountContext *>(data);
207             context->errCode = AppAccountManager::CreateAccount(context->name, context->options);
208         },
209         [](napi_env env, napi_status status, void *data) {
210             CreateAccountContext *context = reinterpret_cast<CreateAccountContext *>(data);
211             ProcessCallbackOrPromise(env, context,
212                 GenerateBusinessError(env, context->errCode), NapiGetNull(env));
213             napi_delete_async_work(env, context->work);
214             delete context;
215             context = nullptr;
216         }, reinterpret_cast<void *>(context), &context->work);
217     napi_queue_async_work(env, context->work);
218     return result;
219 }
220 
CreateAccountImplicitly(napi_env env,napi_callback_info cbInfo)221 napi_value NapiAppAccount::CreateAccountImplicitly(napi_env env, napi_callback_info cbInfo)
222 {
223     auto *context = new (std::nothrow) CreateAccountImplicitlyContext(env);
224     if (context == nullptr) {
225         ACCOUNT_LOGE("insufficient memory for context!");
226         return NapiGetNull(env);
227     }
228     if (!ParseContextForCreateAccountImplicitly(env, cbInfo, context)) {
229         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
230         delete context;
231         return NapiGetNull(env);
232     }
233     context->appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(env, context->callback);
234     if (context->appAccountMgrCb == nullptr) {
235         ACCOUNT_LOGE("insufficient memory for AppAccountManagerCallback!");
236         delete context;
237         return NapiGetNull(env);
238     }
239     napi_value resourceName = nullptr;
240     napi_create_string_latin1(env, "CreateAccountImplicitly", NAPI_AUTO_LENGTH, &resourceName);
241     napi_create_async_work(env, nullptr, resourceName,
242         [](napi_env env, void *data) {
243             auto context = reinterpret_cast<CreateAccountImplicitlyContext *>(data);
244             ErrCode errCode = AppAccountManager::CreateAccountImplicitly(context->owner,
245                 context->options, context->appAccountMgrCb);
246             context->errCode = ConvertToJSErrCode(errCode);
247         },
248         [](napi_env env, napi_status status, void *data) {
249             auto context = reinterpret_cast<CreateAccountImplicitlyContext *>(data);
250             AAFwk::Want errResult;
251             if ((context->errCode != 0) && (context->appAccountMgrCb != nullptr)) {
252                 context->appAccountMgrCb->OnResult(context->errCode, errResult);
253             }
254             napi_delete_async_work(env, context->work);
255             delete context;
256             context = nullptr;
257         }, reinterpret_cast<void *>(context), &context->work);
258     napi_queue_async_work(env, context->work);
259     return NapiGetNull(env);
260 }
261 
AddAccountImplicitly(napi_env env,napi_callback_info cbInfo)262 napi_value NapiAppAccount::AddAccountImplicitly(napi_env env, napi_callback_info cbInfo)
263 {
264     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
265     if (asyncContext == nullptr) {
266         ACCOUNT_LOGE("insufficient memory for asyncContext!");
267         return NapiGetNull(env);
268     }
269     asyncContext->env = env;
270     ParseContextForAuthenticate(env, cbInfo, asyncContext, ARGS_SIZE_FOUR);
271     if (asyncContext->appAccountMgrCb == nullptr) {
272         ACCOUNT_LOGE("insufficient memory for AppAccountManagerCallback!");
273         delete asyncContext;
274         return NapiGetNull(env);
275     }
276     napi_value resourceName = nullptr;
277     NAPI_CALL(env, napi_create_string_latin1(env, "AddAccountImplicitly", NAPI_AUTO_LENGTH, &resourceName));
278     NAPI_CALL(env,
279         napi_create_async_work(env,
280             nullptr,
281             resourceName,
282             [](napi_env env, void *data) {
283                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
284                 ErrCode errCode = AppAccountManager::AddAccountImplicitly(asyncContext->owner,
285                     asyncContext->authType, asyncContext->options, asyncContext->appAccountMgrCb);
286                 asyncContext->errCode = ConvertToJSErrCodeV8(errCode);
287             },
288             [](napi_env env, napi_status status, void *data) {
289                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
290                 AAFwk::Want errResult;
291                 if ((asyncContext->errCode != 0) && (asyncContext->appAccountMgrCb != nullptr)) {
292                     asyncContext->appAccountMgrCb->OnResult(asyncContext->errCode, errResult);
293                 }
294                 napi_delete_async_work(env, asyncContext->work);
295                 delete asyncContext;
296                 asyncContext = nullptr;
297             },
298             reinterpret_cast<void *>(asyncContext),
299             &asyncContext->work));
300     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
301     return NapiGetNull(env);
302 }
303 
DeleteAccount(napi_env env,napi_callback_info cbInfo)304 napi_value NapiAppAccount::DeleteAccount(napi_env env, napi_callback_info cbInfo)
305 {
306     return RemoveAccountInternal(env, cbInfo, false);
307 }
308 
RemoveAccount(napi_env env,napi_callback_info cbInfo)309 napi_value NapiAppAccount::RemoveAccount(napi_env env, napi_callback_info cbInfo)
310 {
311     return RemoveAccountInternal(env, cbInfo, true);
312 }
313 
RemoveAccountInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)314 napi_value NapiAppAccount::RemoveAccountInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
315 {
316     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
317     if (asyncContext == nullptr) {
318         ACCOUNT_LOGE("insufficient memory for asyncContext!");
319         return NapiGetNull(env);
320     }
321     asyncContext->env = env;
322     asyncContext->throwErr = isThrowable;
323     if ((!ParseContextWithTwoPara(env, cbInfo, asyncContext)) && isThrowable) {
324         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
325         delete asyncContext;
326         return NapiGetNull(env);
327     }
328 
329     napi_value result = nullptr;
330     if (asyncContext->callbackRef == nullptr) {
331         napi_create_promise(env, &asyncContext->deferred, &result);
332     } else {
333         napi_get_undefined(env, &result);
334     }
335 
336     napi_value resource = nullptr;
337     napi_create_string_utf8(env, "DeleteAccount", NAPI_AUTO_LENGTH, &resource);
338 
339     napi_create_async_work(env,
340         nullptr,
341         resource,
342         [](napi_env env, void *data) {
343             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
344             asyncContext->errCode = AppAccountManager::DeleteAccount(asyncContext->name);
345         },
346         [](napi_env env, napi_status status, void *data) {
347             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
348             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
349                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
350             ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
351             napi_delete_async_work(env, asyncContext->work);
352             delete asyncContext;
353             asyncContext = nullptr;
354         },
355         reinterpret_cast<void *>(asyncContext),
356         &asyncContext->work);
357     napi_queue_async_work(env, asyncContext->work);
358     return result;
359 }
360 
DisableAppAccess(napi_env env,napi_callback_info cbInfo)361 napi_value NapiAppAccount::DisableAppAccess(napi_env env, napi_callback_info cbInfo)
362 {
363     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
364     if (asyncContext == nullptr) {
365         ACCOUNT_LOGE("insufficient memory for asyncContext!");
366         return NapiGetNull(env);
367     }
368     asyncContext->env = env;
369     ParseContextWithBdName(env, cbInfo, asyncContext);
370     napi_value result = nullptr;
371     if (asyncContext->callbackRef == nullptr) {
372         napi_create_promise(env, &asyncContext->deferred, &result);
373     } else {
374         napi_get_undefined(env, &result);
375     }
376     napi_value resource = nullptr;
377     napi_create_string_utf8(env, "DisableAppAccess", NAPI_AUTO_LENGTH, &resource);
378     napi_create_async_work(env,
379         nullptr,
380         resource,
381         [](napi_env env, void *data) {
382             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
383             asyncContext->errCode = AppAccountManager::DisableAppAccess(asyncContext->name, asyncContext->bundleName);
384         },
385         [](napi_env env, napi_status status, void *data) {
386             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
387             ProcessCallbackOrPromise(env, asyncContext,
388                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode)), NapiGetNull(env));
389             napi_delete_async_work(env, asyncContext->work);
390             delete asyncContext;
391             asyncContext = nullptr;
392         },
393         reinterpret_cast<void *>(asyncContext),
394         &asyncContext->work);
395     napi_queue_async_work(env, asyncContext->work);
396     return result;
397 }
398 
EnableAppAccess(napi_env env,napi_callback_info cbInfo)399 napi_value NapiAppAccount::EnableAppAccess(napi_env env, napi_callback_info cbInfo)
400 {
401     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
402     if (asyncContext == nullptr) {
403         ACCOUNT_LOGE("insufficient memory for asyncContext!");
404         return NapiGetNull(env);
405     }
406     asyncContext->env = env;
407     ParseContextWithBdName(env, cbInfo, asyncContext);
408     napi_value result = nullptr;
409     if (asyncContext->callbackRef == nullptr) {
410         napi_create_promise(env, &asyncContext->deferred, &result);
411     } else {
412         napi_get_undefined(env, &result);
413     }
414     napi_value resource = nullptr;
415     napi_create_string_utf8(env, "EnableAppAccess", NAPI_AUTO_LENGTH, &resource);
416     napi_create_async_work(env,
417         nullptr,
418         resource,
419         [](napi_env env, void *data) {
420             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
421             ErrCode errCode = AppAccountManager::EnableAppAccess(asyncContext->name, asyncContext->bundleName);
422             asyncContext->errCode = ConvertToJSErrCode(errCode);
423         },
424         [](napi_env env, napi_status status, void *data) {
425             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
426             ProcessCallbackOrPromise(env, asyncContext,
427                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode)), NapiGetNull(env));
428             napi_delete_async_work(env, asyncContext->work);
429             delete asyncContext;
430             asyncContext = nullptr;
431         },
432         reinterpret_cast<void *>(asyncContext),
433         &asyncContext->work);
434     napi_queue_async_work(env, asyncContext->work);
435     return result;
436 }
437 
SetAppAccess(napi_env env,napi_callback_info cbInfo)438 napi_value NapiAppAccount::SetAppAccess(napi_env env, napi_callback_info cbInfo)
439 {
440     auto *context = new (std::nothrow) AppAccountAsyncContext();
441     if (context == nullptr) {
442         ACCOUNT_LOGE("insufficient memory for asyncContext!");
443         return NapiGetNull(env);
444     }
445     if (!ParseContextForSetAppAccess(env, cbInfo, context)) {
446         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
447         delete context;
448         return NapiGetNull(env);
449     }
450     context->env = env;
451     napi_value result = nullptr;
452     if (context->callbackRef == nullptr) {
453         napi_create_promise(env, &context->deferred, &result);
454     } else {
455         napi_get_undefined(env, &result);
456     }
457     napi_value resource = nullptr;
458     napi_create_string_utf8(env, "SetAppAccess", NAPI_AUTO_LENGTH, &resource);
459     napi_create_async_work(env, nullptr, resource,
460         [](napi_env env, void *data) {
461             AppAccountAsyncContext *context = reinterpret_cast<AppAccountAsyncContext *>(data);
462             context->errCode =
463                 AppAccountManager::SetAppAccess(context->name, context->bundleName, context->isAccessible);
464         },
465         [](napi_env env, napi_status status, void *data) {
466             AppAccountAsyncContext *context = reinterpret_cast<AppAccountAsyncContext *>(data);
467             ProcessCallbackOrPromise(env, context,
468                 GenerateBusinessError(env, context->errCode), NapiGetNull(env));
469             napi_delete_async_work(env, context->work);
470             delete context;
471             context = nullptr;
472         }, reinterpret_cast<void *>(context), &context->work);
473     napi_queue_async_work(env, context->work);
474     return result;
475 }
476 
CheckAppAccountSyncEnable(napi_env env,napi_callback_info cbInfo)477 napi_value NapiAppAccount::CheckAppAccountSyncEnable(napi_env env, napi_callback_info cbInfo)
478 {
479     return CheckDataSyncEnabledInternal(env, cbInfo, false);
480 }
481 
CheckDataSyncEnabled(napi_env env,napi_callback_info cbInfo)482 napi_value NapiAppAccount::CheckDataSyncEnabled(napi_env env, napi_callback_info cbInfo)
483 {
484     return CheckDataSyncEnabledInternal(env, cbInfo, true);
485 }
486 
CheckDataSyncEnabledInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)487 napi_value NapiAppAccount::CheckDataSyncEnabledInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
488 {
489     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
490     if (asyncContext == nullptr) {
491         ACCOUNT_LOGE("insufficient memory for asyncContext!");
492         return NapiGetNull(env);
493     }
494     asyncContext->env = env;
495     asyncContext->throwErr = isThrowable;
496     if ((!ParseContextWithTwoPara(env, cbInfo, asyncContext)) && isThrowable) {
497         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
498         delete asyncContext;
499         return NapiGetNull(env);
500     }
501 
502     napi_value result = nullptr;
503     if (asyncContext->callbackRef == nullptr) {
504         napi_create_promise(env, &asyncContext->deferred, &result);
505     } else {
506         napi_get_undefined(env, &result);
507     }
508 
509     napi_value resource = nullptr;
510     napi_create_string_utf8(env, "CheckAppAccountSyncEnable", NAPI_AUTO_LENGTH, &resource);
511 
512     napi_create_async_work(env,
513         nullptr,
514         resource,
515         [](napi_env env, void *data) {
516             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
517             asyncContext->errCode =
518                 AppAccountManager::CheckAppAccountSyncEnable(asyncContext->name, asyncContext->result);
519         },
520         [](napi_env env, napi_status status, void *data) {
521             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
522             napi_value boolVal = nullptr;
523             napi_get_boolean(env, asyncContext->result, &boolVal);
524             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
525                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
526             ProcessCallbackOrPromise(env, asyncContext, err, boolVal);
527             napi_delete_async_work(env, asyncContext->work);
528             delete asyncContext;
529             asyncContext = nullptr;
530         },
531         reinterpret_cast<void *>(asyncContext),
532         &asyncContext->work);
533     napi_queue_async_work(env, asyncContext->work);
534     return result;
535 }
536 
SetAccountCredential(napi_env env,napi_callback_info cbInfo)537 napi_value NapiAppAccount::SetAccountCredential(napi_env env, napi_callback_info cbInfo)
538 {
539     return SetCredentialInternal(env, cbInfo, false);
540 }
541 
SetCredential(napi_env env,napi_callback_info cbInfo)542 napi_value NapiAppAccount::SetCredential(napi_env env, napi_callback_info cbInfo)
543 {
544     return SetCredentialInternal(env, cbInfo, true);
545 }
546 
SetCredentialInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)547 napi_value NapiAppAccount::SetCredentialInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
548 {
549     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
550     if (asyncContext == nullptr) {
551         ACCOUNT_LOGE("insufficient memory for asyncContext!");
552         return NapiGetNull(env);
553     }
554     asyncContext->env = env;
555     asyncContext->throwErr = isThrowable;
556     if ((!ParseContextToSetCredential(env, cbInfo, asyncContext)) && isThrowable) {
557         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
558         delete asyncContext;
559         return NapiGetNull(env);
560     }
561 
562     napi_value result = nullptr;
563     if (asyncContext->callbackRef == nullptr) {
564         napi_create_promise(env, &asyncContext->deferred, &result);
565     } else {
566         napi_get_undefined(env, &result);
567     }
568 
569     napi_value resource = nullptr;
570     napi_create_string_utf8(env, "SetAccountCredential", NAPI_AUTO_LENGTH, &resource);
571 
572     napi_create_async_work(env,
573         nullptr,
574         resource,
575         [](napi_env env, void *data) {
576             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
577             asyncContext->errCode = AppAccountManager::SetAccountCredential(
578                 asyncContext->name, asyncContext->credentialType, asyncContext->credential);
579         },
580         [](napi_env env, napi_status status, void *data) {
581             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
582             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
583                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
584             ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
585             napi_delete_async_work(env, asyncContext->work);
586             delete asyncContext;
587             asyncContext = nullptr;
588         },
589         reinterpret_cast<void *>(asyncContext),
590         &asyncContext->work);
591     napi_queue_async_work(env, asyncContext->work);
592     return result;
593 }
594 
SetAccountExtraInfo(napi_env env,napi_callback_info cbInfo)595 napi_value NapiAppAccount::SetAccountExtraInfo(napi_env env, napi_callback_info cbInfo)
596 {
597     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
598     if (asyncContext == nullptr) {
599         ACCOUNT_LOGE("insufficient memory for asyncContext!");
600         return NapiGetNull(env);
601     }
602     asyncContext->env = env;
603     ParseContextForSetExInfo(env, cbInfo, asyncContext);
604 
605     napi_value result = nullptr;
606     if (asyncContext->callbackRef == nullptr) {
607         napi_create_promise(env, &asyncContext->deferred, &result);
608     } else {
609         napi_get_undefined(env, &result);
610     }
611 
612     napi_value resource = nullptr;
613     napi_create_string_utf8(env, "SetAccountExtraInfo", NAPI_AUTO_LENGTH, &resource);
614 
615     napi_create_async_work(env,
616         nullptr,
617         resource,
618         [](napi_env env, void *data) {
619             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
620             asyncContext->errCode = AppAccountManager::SetAccountExtraInfo(
621                 asyncContext->name, asyncContext->extraInfo);
622         },
623         [](napi_env env, napi_status status, void *data) {
624             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
625             ProcessCallbackOrPromise(env, asyncContext,
626                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode)), NapiGetNull(env));
627             napi_delete_async_work(env, asyncContext->work);
628             delete asyncContext;
629             asyncContext = nullptr;
630         },
631         reinterpret_cast<void *>(asyncContext),
632         &asyncContext->work);
633     napi_queue_async_work(env, asyncContext->work);
634     return result;
635 }
636 
SetAppAccountSyncEnable(napi_env env,napi_callback_info cbInfo)637 napi_value NapiAppAccount::SetAppAccountSyncEnable(napi_env env, napi_callback_info cbInfo)
638 {
639     return SetDataSyncEnabledInternal(env, cbInfo, false);
640 }
641 
SetDataSyncEnabled(napi_env env,napi_callback_info cbInfo)642 napi_value NapiAppAccount::SetDataSyncEnabled(napi_env env, napi_callback_info cbInfo)
643 {
644     return SetDataSyncEnabledInternal(env, cbInfo, true);
645 }
646 
SetDataSyncEnabledInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)647 napi_value NapiAppAccount::SetDataSyncEnabledInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
648 {
649     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
650     if (asyncContext == nullptr) {
651         ACCOUNT_LOGE("insufficient memory for asyncContext!");
652         return NapiGetNull(env);
653     }
654     asyncContext->env = env;
655     asyncContext->throwErr = isThrowable;
656     if ((!ParseContextWithIsEnable(env, cbInfo, asyncContext)) && isThrowable) {
657         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
658         delete asyncContext;
659         return NapiGetNull(env);
660     }
661 
662     napi_value result = nullptr;
663     if (asyncContext->callbackRef == nullptr) {
664         napi_create_promise(env, &asyncContext->deferred, &result);
665     } else {
666         napi_get_undefined(env, &result);
667     }
668 
669     napi_value resource = nullptr;
670     napi_create_string_utf8(env, "SetAppAccountSyncEnable", NAPI_AUTO_LENGTH, &resource);
671 
672     napi_create_async_work(env,
673         nullptr,
674         resource,
675         [](napi_env env, void *data) {
676             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
677             asyncContext->errCode =
678                 AppAccountManager::SetAppAccountSyncEnable(asyncContext->name, asyncContext->isEnable);
679         },
680         [](napi_env env, napi_status status, void *data) {
681             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
682             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
683                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
684             ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
685             napi_delete_async_work(env, asyncContext->work);
686             delete asyncContext;
687             asyncContext = nullptr;
688         },
689         reinterpret_cast<void *>(asyncContext),
690         &asyncContext->work);
691     napi_queue_async_work(env, asyncContext->work);
692     return result;
693 }
694 
SetAssociatedData(napi_env env,napi_callback_info cbInfo)695 napi_value NapiAppAccount::SetAssociatedData(napi_env env, napi_callback_info cbInfo)
696 {
697     return SetCustomDataInternal(env, cbInfo, false);
698 }
699 
SetCustomData(napi_env env,napi_callback_info cbInfo)700 napi_value NapiAppAccount::SetCustomData(napi_env env, napi_callback_info cbInfo)
701 {
702     return SetCustomDataInternal(env, cbInfo, true);
703 }
704 
SetCustomDataInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)705 napi_value NapiAppAccount::SetCustomDataInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
706 {
707     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
708     if (asyncContext == nullptr) {
709         ACCOUNT_LOGE("insufficient memory for asyncContext!");
710         return NapiGetNull(env);
711     }
712     asyncContext->env = env;
713     asyncContext->throwErr = isThrowable;
714     if ((!ParseContextForAssociatedData(env, cbInfo, asyncContext)) && isThrowable) {
715         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
716         delete asyncContext;
717         return NapiGetNull(env);
718     }
719 
720     napi_value result = nullptr;
721     if (asyncContext->callbackRef == nullptr) {
722         napi_create_promise(env, &asyncContext->deferred, &result);
723     } else {
724         napi_get_undefined(env, &result);
725     }
726 
727     napi_value resource = nullptr;
728     napi_create_string_utf8(env, "SetAssociatedData", NAPI_AUTO_LENGTH, &resource);
729 
730     napi_create_async_work(env,
731         nullptr,
732         resource,
733         [](napi_env env, void *data) {
734             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
735             asyncContext->errCode =
736                 AppAccountManager::SetAssociatedData(asyncContext->name, asyncContext->key, asyncContext->value);
737         },
738         [](napi_env env, napi_status status, void *data) {
739             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
740             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
741                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
742             ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
743             napi_delete_async_work(env, asyncContext->work);
744             delete asyncContext;
745             asyncContext = nullptr;
746         },
747         reinterpret_cast<void *>(asyncContext),
748         &asyncContext->work);
749     napi_queue_async_work(env, asyncContext->work);
750     return result;
751 }
752 
GetAllAccessibleAccounts(napi_env env,napi_callback_info cbInfo)753 napi_value NapiAppAccount::GetAllAccessibleAccounts(napi_env env, napi_callback_info cbInfo)
754 {
755     return GetAllAccessibleAccountsInternal(env, cbInfo, false);
756 }
757 
GetAllAccessibleAccountsInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)758 napi_value NapiAppAccount::GetAllAccessibleAccountsInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
759 {
760     auto *asyncContext = new (std::nothrow) GetAccountsAsyncContext();
761     if (asyncContext == nullptr) {
762         ACCOUNT_LOGE("insufficient memory for asyncContext!");
763         return NapiGetNull(env);
764     }
765     asyncContext->env = env;
766     asyncContext->throwErr = isThrowable;
767     if ((!ParseContextCBArray(env, cbInfo, asyncContext)) && isThrowable) {
768         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
769         delete asyncContext;
770         return NapiGetNull(env);
771     }
772 
773     napi_value result = nullptr;
774     if (asyncContext->callbackRef == nullptr) {
775         napi_create_promise(env, &asyncContext->deferred, &result);
776     } else {
777         napi_get_undefined(env, &result);
778     }
779 
780     napi_value resource = nullptr;
781     napi_create_string_utf8(env, "GetAllAccessibleAccounts", NAPI_AUTO_LENGTH, &resource);
782 
783     napi_create_async_work(env, nullptr, resource,
784         [](napi_env env, void *data) {
785             GetAccountsAsyncContext *asyncContext = reinterpret_cast<GetAccountsAsyncContext *>(data);
786             if (asyncContext->throwErr) {
787                 asyncContext->errCode =
788                     AppAccountManager::QueryAllAccessibleAccounts(asyncContext->owner, asyncContext->appAccounts);
789             } else {
790                 asyncContext->errCode =
791                     AppAccountManager::GetAllAccessibleAccounts(asyncContext->appAccounts);
792             }
793         },
794         [](napi_env env, napi_status status, void *data) {
795             GetAccountsAsyncContext *asyncContext = reinterpret_cast<GetAccountsAsyncContext *>(data);
796             napi_value arrVal = nullptr;
797             GetAppAccountInfoForResult(env, asyncContext->appAccounts, arrVal);
798             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
799                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
800             ProcessCallbackOrPromise(env, asyncContext, err, arrVal);
801             napi_delete_async_work(env, asyncContext->work);
802             delete asyncContext;
803             asyncContext = nullptr;
804         },
805         reinterpret_cast<void *>(asyncContext),
806         &asyncContext->work);
807     napi_queue_async_work(env, asyncContext->work);
808     return result;
809 }
810 
GetAllAccounts(napi_env env,napi_callback_info cbInfo)811 napi_value NapiAppAccount::GetAllAccounts(napi_env env, napi_callback_info cbInfo)
812 {
813     size_t argc = ARGS_SIZE_TWO;
814     napi_value argv[ARGS_SIZE_TWO] = {0};
815     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
816     if (argc == 0) {
817         return GetAllAccessibleAccountsInternal(env, cbInfo, true);
818     }
819     napi_valuetype valueType = napi_undefined;
820     napi_typeof(env, argv[0], &valueType);
821     if (valueType == napi_function) {
822         return GetAllAccessibleAccountsInternal(env, cbInfo, true);
823     }
824     return GetAccountsByOwnerInternal(env, cbInfo, false);
825 }
826 
GetAccountsByOwner(napi_env env,napi_callback_info cbInfo)827 napi_value NapiAppAccount::GetAccountsByOwner(napi_env env, napi_callback_info cbInfo)
828 {
829     return GetAccountsByOwnerInternal(env, cbInfo, true);
830 }
831 
GetAccountsByOwnerInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)832 napi_value NapiAppAccount::GetAccountsByOwnerInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
833 {
834     auto *asyncContext = new (std::nothrow) GetAccountsAsyncContext();
835     if (asyncContext == nullptr) {
836         ACCOUNT_LOGE("insufficient memory for asyncContext!");
837         return NapiGetNull(env);
838     }
839     asyncContext->env = env;
840     asyncContext->throwErr = isThrowable;
841     if ((!ParseContextWithStrCBArray(env, cbInfo, asyncContext)) && isThrowable) {
842         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
843         delete asyncContext;
844         return NapiGetNull(env);
845     }
846 
847     napi_value result = nullptr;
848     if (asyncContext->callbackRef == nullptr) {
849         napi_create_promise(env, &asyncContext->deferred, &result);
850     } else {
851         napi_get_undefined(env, &result);
852     }
853 
854     napi_value resource = nullptr;
855     napi_create_string_utf8(env, "GetAllAccounts", NAPI_AUTO_LENGTH, &resource);
856 
857     napi_create_async_work(env, nullptr, resource,
858         [](napi_env env, void *data) {
859             GetAccountsAsyncContext *asyncContext = reinterpret_cast<GetAccountsAsyncContext *>(data);
860             if (!asyncContext->throwErr) {
861                 asyncContext->errCode =
862                     AppAccountManager::GetAllAccounts(asyncContext->owner, asyncContext->appAccounts);
863                 return;
864             }
865             if (!asyncContext->owner.empty()) {
866                 asyncContext->errCode =
867                     AppAccountManager::QueryAllAccessibleAccounts(asyncContext->owner, asyncContext->appAccounts);
868                 return;
869             }
870             asyncContext->errCode = ERR_APPACCOUNT_KIT_INVALID_PARAMETER;
871         },
872         [](napi_env env, napi_status status, void *data) {
873             GetAccountsAsyncContext *asyncContext = reinterpret_cast<GetAccountsAsyncContext *>(data);
874             napi_value arrVal = nullptr;
875             GetAppAccountInfoForResult(env, asyncContext->appAccounts, arrVal);
876             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
877                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
878             ProcessCallbackOrPromise(env, asyncContext, err, arrVal);
879             napi_delete_async_work(env, asyncContext->work);
880             delete asyncContext;
881             asyncContext = nullptr;
882         }, reinterpret_cast<void *>(asyncContext), &asyncContext->work);
883     napi_queue_async_work(env, asyncContext->work);
884     return result;
885 }
886 
GetAccountCredential(napi_env env,napi_callback_info cbInfo)887 napi_value NapiAppAccount::GetAccountCredential(napi_env env, napi_callback_info cbInfo)
888 {
889     return GetCredentialInternal(env, cbInfo, false);
890 }
891 
GetCredential(napi_env env,napi_callback_info cbInfo)892 napi_value NapiAppAccount::GetCredential(napi_env env, napi_callback_info cbInfo)
893 {
894     return GetCredentialInternal(env, cbInfo, true);
895 }
896 
GetCredentialInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)897 napi_value NapiAppAccount::GetCredentialInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
898 {
899     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
900     if (asyncContext == nullptr) {
901         ACCOUNT_LOGE("insufficient memory for asyncContext!");
902         return NapiGetNull(env);
903     }
904     asyncContext->env = env;
905     asyncContext->throwErr = isThrowable;
906     if ((!ParseContextWithCredentialType(env, cbInfo, asyncContext)) && isThrowable) {
907         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
908         delete asyncContext;
909         return NapiGetNull(env);
910     }
911     napi_value result = nullptr;
912     if (asyncContext->callbackRef == nullptr) {
913         napi_create_promise(env, &asyncContext->deferred, &result);
914     } else {
915         napi_get_undefined(env, &result);
916     }
917 
918     napi_value resource = nullptr;
919     napi_create_string_utf8(env, "GetAccountCredential", NAPI_AUTO_LENGTH, &resource);
920 
921     napi_create_async_work(env,
922         nullptr,
923         resource,
924         [](napi_env env, void *data) {
925             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
926             asyncContext->errCode = AppAccountManager::GetAccountCredential(
927                 asyncContext->name, asyncContext->credentialType, asyncContext->credential);
928         },
929         [](napi_env env, napi_status status, void *data) {
930             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
931             napi_value strVal = nullptr;
932             napi_create_string_utf8(env, asyncContext->credential.c_str(), NAPI_AUTO_LENGTH, &strVal);
933             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
934                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
935             ProcessCallbackOrPromise(env, asyncContext, err, strVal);
936             napi_delete_async_work(env, asyncContext->work);
937             delete asyncContext;
938             asyncContext = nullptr;
939         },
940         reinterpret_cast<void *>(asyncContext),
941         &asyncContext->work);
942     napi_queue_async_work(env, asyncContext->work);
943     return result;
944 }
945 
GetAccountExtraInfo(napi_env env,napi_callback_info cbInfo)946 napi_value NapiAppAccount::GetAccountExtraInfo(napi_env env, napi_callback_info cbInfo)
947 {
948     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
949     if (asyncContext == nullptr) {
950         ACCOUNT_LOGE("insufficient memory for asyncContext!");
951         return NapiGetNull(env);
952     }
953     asyncContext->env = env;
954     ParseContextWithTwoPara(env, cbInfo, asyncContext);
955     napi_value result = nullptr;
956     if (asyncContext->callbackRef == nullptr) {
957         napi_create_promise(env, &asyncContext->deferred, &result);
958     } else {
959         napi_get_undefined(env, &result);
960     }
961 
962     napi_value resource = nullptr;
963     napi_create_string_utf8(env, "GetAccountExtraInfo", NAPI_AUTO_LENGTH, &resource);
964 
965     napi_create_async_work(env,
966         nullptr,
967         resource,
968         [](napi_env env, void *data) {
969             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
970             asyncContext->errCode = AppAccountManager::GetAccountExtraInfo(
971                 asyncContext->name, asyncContext->extraInfo);
972         },
973         [](napi_env env, napi_status status, void *data) {
974             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
975             napi_value strVal = nullptr;
976             napi_create_string_utf8(env, asyncContext->extraInfo.c_str(), NAPI_AUTO_LENGTH, &strVal);
977             ProcessCallbackOrPromise(env, asyncContext,
978                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode)), strVal);
979             napi_delete_async_work(env, asyncContext->work);
980             delete asyncContext;
981             asyncContext = nullptr;
982         },
983         reinterpret_cast<void *>(asyncContext),
984         &asyncContext->work);
985     napi_queue_async_work(env, asyncContext->work);
986     return result;
987 }
988 
GetAssociatedData(napi_env env,napi_callback_info cbInfo)989 napi_value NapiAppAccount::GetAssociatedData(napi_env env, napi_callback_info cbInfo)
990 {
991     return GetCustomDataInternal(env, cbInfo, false);
992 }
993 
GetCustomData(napi_env env,napi_callback_info cbInfo)994 napi_value NapiAppAccount::GetCustomData(napi_env env, napi_callback_info cbInfo)
995 {
996     return GetCustomDataInternal(env, cbInfo, true);
997 }
998 
GetCustomDataInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)999 napi_value NapiAppAccount::GetCustomDataInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1000 {
1001     auto *asyncContext = new (std::nothrow) AppAccountAsyncContext();
1002     if (asyncContext == nullptr) {
1003         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1004         return NapiGetNull(env);
1005     }
1006     asyncContext->env = env;
1007     asyncContext->throwErr = isThrowable;
1008     if ((!ParseContextToGetData(env, cbInfo, asyncContext)) && isThrowable) {
1009         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1010         delete asyncContext;
1011         return NapiGetNull(env);
1012     }
1013     napi_value result = nullptr;
1014     if (asyncContext->callbackRef == nullptr) {
1015         napi_create_promise(env, &asyncContext->deferred, &result);
1016     } else {
1017         napi_get_undefined(env, &result);
1018     }
1019     napi_value resource = nullptr;
1020     napi_create_string_utf8(env, "GetAssociatedData", NAPI_AUTO_LENGTH, &resource);
1021     napi_create_async_work(env,
1022         nullptr,
1023         resource,
1024         [](napi_env env, void *data) {
1025             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
1026             asyncContext->errCode =
1027                 AppAccountManager::GetAssociatedData(asyncContext->name, asyncContext->key, asyncContext->value);
1028         },
1029         [](napi_env env, napi_status status, void *data) {
1030             AppAccountAsyncContext *asyncContext = reinterpret_cast<AppAccountAsyncContext *>(data);
1031             napi_value strVal = NapiGetNull(env);
1032             napi_create_string_utf8(env, asyncContext->value.c_str(), NAPI_AUTO_LENGTH, &strVal);
1033             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1034                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1035             ProcessCallbackOrPromise(env, asyncContext, err, strVal);
1036             napi_delete_async_work(env, asyncContext->work);
1037             delete asyncContext;
1038             asyncContext = nullptr;
1039         },
1040         reinterpret_cast<void *>(asyncContext),
1041         &asyncContext->work);
1042     napi_queue_async_work(env, asyncContext->work);
1043     return result;
1044 }
1045 
GetAssociatedDataSync(napi_env env,napi_callback_info cbInfo)1046 napi_value NapiAppAccount::GetAssociatedDataSync(napi_env env, napi_callback_info cbInfo)
1047 {
1048     AppAccountAsyncContext asyncContext;
1049     if (!ParseContextToGetData(env, cbInfo, &asyncContext)) {
1050         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext.errMsg));
1051         return NapiGetNull(env);
1052     }
1053     napi_value result = nullptr;
1054     ErrCode errCode = AppAccountManager::GetAssociatedData(asyncContext.name, asyncContext.key, asyncContext.value);
1055     if (errCode == ERR_OK) {
1056         NAPI_CALL(env, napi_create_string_utf8(env, asyncContext.value.c_str(), NAPI_AUTO_LENGTH, &result));
1057     } else {
1058         napi_throw(env, GenerateBusinessError(env, errCode));
1059     }
1060     return result;
1061 }
1062 
Authenticate(napi_env env,napi_callback_info cbInfo)1063 napi_value NapiAppAccount::Authenticate(napi_env env, napi_callback_info cbInfo)
1064 {
1065     return AuthInternal(env, cbInfo, false);
1066 }
1067 
Auth(napi_env env,napi_callback_info cbInfo)1068 napi_value NapiAppAccount::Auth(napi_env env, napi_callback_info cbInfo)
1069 {
1070     return AuthInternal(env, cbInfo, true);
1071 }
1072 
AuthInternal(napi_env env,napi_callback_info cbInfo,bool isNewApi)1073 napi_value NapiAppAccount::AuthInternal(napi_env env, napi_callback_info cbInfo, bool isNewApi)
1074 {
1075     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1076     if (asyncContext == nullptr) {
1077         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1078         return NapiGetNull(env);
1079     }
1080     asyncContext->env = env;
1081     asyncContext->throwErr = isNewApi;
1082     if (isNewApi) {
1083         if (!ParseContextForAuth(env, cbInfo, asyncContext)) {
1084             napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1085             delete asyncContext;
1086             return NapiGetNull(env);
1087         }
1088         asyncContext->options.SetParam(Constants::API_V9, true);
1089     } else {
1090         ParseContextForAuthenticate(env, cbInfo, asyncContext, ARGS_SIZE_FIVE);
1091     }
1092     napi_value result = nullptr;
1093     if (asyncContext->appAccountMgrCb == nullptr) {
1094         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1095     } else {
1096         NAPI_CALL(env, napi_get_undefined(env, &result));
1097     }
1098     napi_value resourceName = nullptr;
1099     NAPI_CALL(env, napi_create_string_latin1(env, "Authenticate", NAPI_AUTO_LENGTH, &resourceName));
1100     NAPI_CALL(env,
1101         napi_create_async_work(env, nullptr, resourceName,
1102             [](napi_env env, void *data) {
1103                 auto asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1104                 ErrCode errCode = AppAccountManager::Authenticate(asyncContext->name, asyncContext->owner,
1105                     asyncContext->authType, asyncContext->options, asyncContext->appAccountMgrCb);
1106                 asyncContext->errCode =
1107                     asyncContext->throwErr ? ConvertToJSErrCode(errCode) : ConvertToJSErrCodeV8(errCode);
1108             },
1109             [](napi_env env, napi_status status, void *data) {
1110                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1111                 AAFwk::Want errResult;
1112                 if ((asyncContext->errCode != 0) && (asyncContext->appAccountMgrCb != nullptr)) {
1113                     asyncContext->appAccountMgrCb->OnResult(asyncContext->errCode, errResult);
1114                 }
1115                 napi_delete_async_work(env, asyncContext->work);
1116                 delete asyncContext;
1117                 asyncContext = nullptr;
1118             },
1119             reinterpret_cast<void *>(asyncContext),
1120             &asyncContext->work));
1121     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1122     return NapiGetNull(env);
1123 }
1124 
GetOAuthToken(napi_env env,napi_callback_info cbInfo)1125 napi_value NapiAppAccount::GetOAuthToken(napi_env env, napi_callback_info cbInfo)
1126 {
1127     return GetAuthTokenInternal(env, cbInfo, false);
1128 }
1129 
GetAuthToken(napi_env env,napi_callback_info cbInfo)1130 napi_value NapiAppAccount::GetAuthToken(napi_env env, napi_callback_info cbInfo)
1131 {
1132     return GetAuthTokenInternal(env, cbInfo, true);
1133 }
1134 
GetAuthTokenInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1135 napi_value NapiAppAccount::GetAuthTokenInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1136 {
1137     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1138     if (asyncContext == nullptr) {
1139         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1140         return NapiGetNull(env);
1141     }
1142     asyncContext->env = env;
1143     asyncContext->throwErr = isThrowable;
1144     if ((!ParseContextForGetOAuthToken(env, cbInfo, asyncContext)) && isThrowable) {
1145         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1146         delete asyncContext;
1147         return NapiGetNull(env);
1148     }
1149     napi_value result = nullptr;
1150     if (asyncContext->callbackRef == nullptr) {
1151         napi_create_promise(env, &asyncContext->deferred, &result);
1152     } else {
1153         napi_get_undefined(env, &result);
1154     }
1155     napi_value resource = nullptr;
1156     napi_create_string_utf8(env, "GetOAuthToken", NAPI_AUTO_LENGTH, &resource);
1157     napi_create_async_work(env, nullptr, resource,
1158         [](napi_env env, void *data) {
1159             auto asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1160             if (asyncContext->throwErr) {
1161                 asyncContext->errCode = AppAccountManager::GetAuthToken(
1162                     asyncContext->name, asyncContext->owner, asyncContext->authType, asyncContext->token);
1163             } else {
1164                 asyncContext->errCode = AppAccountManager::GetOAuthToken(
1165                     asyncContext->name, asyncContext->owner, asyncContext->authType, asyncContext->token);
1166             }
1167         },
1168         [](napi_env env, napi_status status, void *data) {
1169             OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1170             napi_value strVal = nullptr;
1171             napi_create_string_utf8(env, asyncContext->token.c_str(), NAPI_AUTO_LENGTH, &strVal);
1172             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1173                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1174             ProcessCallbackOrPromise(env, asyncContext, err, strVal);
1175             napi_delete_async_work(env, asyncContext->work);
1176             delete asyncContext;
1177             asyncContext = nullptr;
1178         },
1179         reinterpret_cast<void *>(asyncContext),
1180         &asyncContext->work);
1181     napi_queue_async_work(env, asyncContext->work);
1182     return result;
1183 }
1184 
SetOAuthToken(napi_env env,napi_callback_info cbInfo)1185 napi_value NapiAppAccount::SetOAuthToken(napi_env env, napi_callback_info cbInfo)
1186 {
1187     return SetAuthTokenInternal(env, cbInfo, false);
1188 }
1189 
SetAuthToken(napi_env env,napi_callback_info cbInfo)1190 napi_value NapiAppAccount::SetAuthToken(napi_env env, napi_callback_info cbInfo)
1191 {
1192     return SetAuthTokenInternal(env, cbInfo, true);
1193 }
1194 
SetAuthTokenInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1195 napi_value NapiAppAccount::SetAuthTokenInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1196 {
1197     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1198     if (asyncContext == nullptr) {
1199         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1200         return NapiGetNull(env);
1201     }
1202     asyncContext->env = env;
1203     asyncContext->throwErr = isThrowable;
1204     if ((!ParseContextForSetOAuthToken(env, cbInfo, asyncContext)) && isThrowable) {
1205         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1206         delete asyncContext;
1207         return NapiGetNull(env);
1208     }
1209     napi_value result = nullptr;
1210     if (asyncContext->callbackRef == nullptr) {
1211         napi_create_promise(env, &asyncContext->deferred, &result);
1212     } else {
1213         napi_get_undefined(env, &result);
1214     }
1215     napi_value resource = nullptr;
1216     napi_create_string_utf8(env, "SetOAuthToken", NAPI_AUTO_LENGTH, &resource);
1217     napi_create_async_work(env,
1218         nullptr,
1219         resource,
1220         [](napi_env env, void *data) {
1221             OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1222             asyncContext->errCode = AppAccountManager::SetOAuthToken(
1223                 asyncContext->name, asyncContext->authType, asyncContext->token);
1224         },
1225         [](napi_env env, napi_status status, void *data) {
1226             OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1227             napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1228                 GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1229             ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
1230             napi_delete_async_work(env, asyncContext->work);
1231             delete asyncContext;
1232             asyncContext = nullptr;
1233         },
1234         reinterpret_cast<void *>(asyncContext),
1235         &asyncContext->work);
1236     napi_queue_async_work(env, asyncContext->work);
1237     return result;
1238 }
1239 
DeleteOAuthToken(napi_env env,napi_callback_info cbInfo)1240 napi_value NapiAppAccount::DeleteOAuthToken(napi_env env, napi_callback_info cbInfo)
1241 {
1242     return DeleteAuthTokenInternal(env, cbInfo, false);
1243 }
1244 
DeleteAuthToken(napi_env env,napi_callback_info cbInfo)1245 napi_value NapiAppAccount::DeleteAuthToken(napi_env env, napi_callback_info cbInfo)
1246 {
1247     return DeleteAuthTokenInternal(env, cbInfo, true);
1248 }
1249 
DeleteAuthTokenInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1250 napi_value NapiAppAccount::DeleteAuthTokenInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1251 {
1252     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1253     if (asyncContext == nullptr) {
1254         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1255         return NapiGetNull(env);
1256     }
1257     asyncContext->env = env;
1258     asyncContext->throwErr = isThrowable;
1259     if ((!ParseContextForDeleteOAuthToken(env, cbInfo, asyncContext)) && isThrowable) {
1260         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1261         delete asyncContext;
1262         return NapiGetNull(env);
1263     }
1264     napi_value result = nullptr;
1265     if (asyncContext->callbackRef == nullptr) {
1266         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1267     } else {
1268         NAPI_CALL(env, napi_get_undefined(env, &result));
1269     }
1270     napi_value resource = nullptr;
1271     NAPI_CALL(env, napi_create_string_utf8(env, "DeleteOAuthToken", NAPI_AUTO_LENGTH, &resource));
1272     NAPI_CALL(env,
1273         napi_create_async_work(env, nullptr, resource,
1274             [](napi_env env, void *data) {
1275                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1276                 if (asyncContext->throwErr) {
1277                     asyncContext->errCode = AppAccountManager::DeleteAuthToken(
1278                         asyncContext->name, asyncContext->owner, asyncContext->authType, asyncContext->token);
1279                 } else {
1280                     asyncContext->errCode = AppAccountManager::DeleteOAuthToken(
1281                         asyncContext->name, asyncContext->owner, asyncContext->authType, asyncContext->token);
1282                 }
1283             },
1284             [](napi_env env, napi_status status, void *data) {
1285                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1286                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1287                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1288                 ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
1289                 napi_delete_async_work(env, asyncContext->work);
1290                 delete asyncContext;
1291                 asyncContext = nullptr;
1292             },
1293             reinterpret_cast<void *>(asyncContext),
1294             &asyncContext->work));
1295     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1296     return result;
1297 }
1298 
SetOAuthTokenVisibility(napi_env env,napi_callback_info cbInfo)1299 napi_value NapiAppAccount::SetOAuthTokenVisibility(napi_env env, napi_callback_info cbInfo)
1300 {
1301     return SetAuthTokenVisibilityInternal(env, cbInfo, false);
1302 }
1303 
SetAuthTokenVisibility(napi_env env,napi_callback_info cbInfo)1304 napi_value NapiAppAccount::SetAuthTokenVisibility(napi_env env, napi_callback_info cbInfo)
1305 {
1306     return SetAuthTokenVisibilityInternal(env, cbInfo, true);
1307 }
1308 
SetAuthTokenVisibilityInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1309 napi_value NapiAppAccount::SetAuthTokenVisibilityInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1310 {
1311     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1312     if (asyncContext == nullptr) {
1313         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1314         return NapiGetNull(env);
1315     }
1316     asyncContext->env = env;
1317     asyncContext->throwErr = isThrowable;
1318     if ((!ParseContextForSetOAuthTokenVisibility(env, cbInfo, asyncContext)) && isThrowable) {
1319         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1320         delete asyncContext;
1321         return NapiGetNull(env);
1322     }
1323     napi_value result = nullptr;
1324     if (asyncContext->callbackRef == nullptr) {
1325         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1326     } else {
1327         NAPI_CALL(env, napi_get_undefined(env, &result));
1328     }
1329     napi_value resource = nullptr;
1330     NAPI_CALL(env, napi_create_string_utf8(env, "SetOAuthTokenVisibility", NAPI_AUTO_LENGTH, &resource));
1331     NAPI_CALL(env,
1332         napi_create_async_work(env,
1333             nullptr,
1334             resource,
1335             [](napi_env env, void *data) {
1336                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1337                 if (asyncContext->throwErr) {
1338                     asyncContext->errCode = AppAccountManager::SetAuthTokenVisibility(
1339                         asyncContext->name, asyncContext->authType, asyncContext->bundleName, asyncContext->isVisible);
1340                 } else {
1341                     asyncContext->errCode = AppAccountManager::SetOAuthTokenVisibility(
1342                         asyncContext->name, asyncContext->authType, asyncContext->bundleName, asyncContext->isVisible);
1343                 }
1344             },
1345             [](napi_env env, napi_status status, void *data) {
1346                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1347                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1348                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1349                 ProcessCallbackOrPromise(env, asyncContext, err, NapiGetNull(env));
1350                 napi_delete_async_work(env, asyncContext->work);
1351                 delete asyncContext;
1352                 asyncContext = nullptr;
1353             },
1354             reinterpret_cast<void *>(asyncContext),
1355             &asyncContext->work));
1356     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1357     return result;
1358 }
1359 
CheckOAuthTokenVisibility(napi_env env,napi_callback_info cbInfo)1360 napi_value NapiAppAccount::CheckOAuthTokenVisibility(napi_env env, napi_callback_info cbInfo)
1361 {
1362     return CheckAuthTokenVisibilityInternal(env, cbInfo, false);
1363 }
1364 
CheckAuthTokenVisibility(napi_env env,napi_callback_info cbInfo)1365 napi_value NapiAppAccount::CheckAuthTokenVisibility(napi_env env, napi_callback_info cbInfo)
1366 {
1367     return CheckAuthTokenVisibilityInternal(env, cbInfo, true);
1368 }
1369 
CheckAuthTokenVisibilityInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1370 napi_value NapiAppAccount::CheckAuthTokenVisibilityInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1371 {
1372     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1373     if (asyncContext == nullptr) {
1374         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1375         return NapiGetNull(env);
1376     }
1377     asyncContext->env = env;
1378     asyncContext->throwErr = isThrowable;
1379     if ((!ParseContextForCheckOAuthTokenVisibility(env, cbInfo, asyncContext)) && isThrowable) {
1380         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1381         delete asyncContext;
1382         return NapiGetNull(env);
1383     }
1384     napi_value result = nullptr;
1385     if (asyncContext->callbackRef == nullptr) {
1386         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1387     } else {
1388         NAPI_CALL(env, napi_get_undefined(env, &result));
1389     }
1390     napi_value resource = nullptr;
1391     NAPI_CALL(env, napi_create_string_utf8(env, "CheckOAuthTokenVisibility", NAPI_AUTO_LENGTH, &resource));
1392     NAPI_CALL(env,
1393         napi_create_async_work(env,
1394             nullptr,
1395             resource,
1396             [](napi_env env, void *data) {
1397                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1398                 if (asyncContext->throwErr) {
1399                     asyncContext->errCode = AppAccountManager::CheckAuthTokenVisibility(
1400                         asyncContext->name, asyncContext->authType, asyncContext->bundleName, asyncContext->isVisible);
1401                 } else {
1402                     asyncContext->errCode = AppAccountManager::CheckOAuthTokenVisibility(
1403                         asyncContext->name, asyncContext->authType, asyncContext->bundleName, asyncContext->isVisible);
1404                 }
1405             },
1406             [](napi_env env, napi_status status, void *data) {
1407                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1408                 napi_value boolVal = nullptr;
1409                 napi_get_boolean(env, asyncContext->isVisible, &boolVal);
1410                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1411                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1412                 ProcessCallbackOrPromise(env, asyncContext, err, boolVal);
1413                 napi_delete_async_work(env, asyncContext->work);
1414                 delete asyncContext;
1415                 asyncContext = nullptr;
1416             },
1417             reinterpret_cast<void *>(asyncContext),
1418             &asyncContext->work));
1419     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1420     return result;
1421 }
1422 
GetAuthenticatorInfo(napi_env env,napi_callback_info cbInfo)1423 napi_value NapiAppAccount::GetAuthenticatorInfo(napi_env env, napi_callback_info cbInfo)
1424 {
1425     return QueryAuthenticatorInfoInternal(env, cbInfo, false);
1426 }
1427 
QueryAuthenticatorInfo(napi_env env,napi_callback_info cbInfo)1428 napi_value NapiAppAccount::QueryAuthenticatorInfo(napi_env env, napi_callback_info cbInfo)
1429 {
1430     return QueryAuthenticatorInfoInternal(env, cbInfo, true);
1431 }
1432 
QueryAuthenticatorInfoInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1433 napi_value NapiAppAccount::QueryAuthenticatorInfoInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1434 {
1435     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1436     if (asyncContext == nullptr) {
1437         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1438         return NapiGetNull(env);
1439     }
1440     asyncContext->env = env;
1441     asyncContext->throwErr = isThrowable;
1442     if ((!ParseContextForGetAuthenticatorInfo(env, cbInfo, asyncContext)) && isThrowable) {
1443         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1444         delete asyncContext;
1445         return NapiGetNull(env);
1446     }
1447     napi_value result = nullptr;
1448     if (asyncContext->callbackRef == nullptr) {
1449         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1450     } else {
1451         NAPI_CALL(env, napi_get_undefined(env, &result));
1452     }
1453     napi_value resource = nullptr;
1454     NAPI_CALL(env, napi_create_string_utf8(env, "GetAuthenticatorInfo", NAPI_AUTO_LENGTH, &resource));
1455     NAPI_CALL(env,
1456         napi_create_async_work(env,
1457             nullptr,
1458             resource,
1459             [](napi_env env, void *data) {
1460                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1461                 asyncContext->errCode = AppAccountManager::GetAuthenticatorInfo(
1462                     asyncContext->owner, asyncContext->authenticatorInfo);
1463             },
1464             [](napi_env env, napi_status status, void *data) {
1465                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1466                 napi_value result = nullptr;
1467                 napi_create_object(env, &result);
1468                 GetAuthenticatorInfoForResult(env, asyncContext->authenticatorInfo, result);
1469                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1470                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1471                 ProcessCallbackOrPromise(env, asyncContext, err, result);
1472                 napi_delete_async_work(env, asyncContext->work);
1473                 delete asyncContext;
1474                 asyncContext = nullptr;
1475             },
1476             reinterpret_cast<void *>(asyncContext),
1477             &asyncContext->work));
1478     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1479     return result;
1480 }
1481 
GetAllOAuthTokens(napi_env env,napi_callback_info cbInfo)1482 napi_value NapiAppAccount::GetAllOAuthTokens(napi_env env, napi_callback_info cbInfo)
1483 {
1484     return GetAllAuthTokensInternal(env, cbInfo, false);
1485 }
1486 
GetAllAuthTokens(napi_env env,napi_callback_info cbInfo)1487 napi_value NapiAppAccount::GetAllAuthTokens(napi_env env, napi_callback_info cbInfo)
1488 {
1489     return GetAllAuthTokensInternal(env, cbInfo, true);
1490 }
1491 
GetAllAuthTokensInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1492 napi_value NapiAppAccount::GetAllAuthTokensInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1493 {
1494     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1495     if (asyncContext == nullptr) {
1496         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1497         return NapiGetNull(env);
1498     }
1499     asyncContext->env = env;
1500     asyncContext->throwErr = isThrowable;
1501     if ((!ParseContextForGetAllOAuthTokens(env, cbInfo, asyncContext)) && isThrowable) {
1502         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1503         delete asyncContext;
1504         return NapiGetNull(env);
1505     }
1506     napi_value result = nullptr;
1507     if (asyncContext->callbackRef == nullptr) {
1508         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1509     } else {
1510         NAPI_CALL(env, napi_get_undefined(env, &result));
1511     }
1512     napi_value resource = nullptr;
1513     NAPI_CALL(env, napi_create_string_utf8(env, "GetAllOAuthTokens", NAPI_AUTO_LENGTH, &resource));
1514     NAPI_CALL(env,
1515         napi_create_async_work(env,
1516             nullptr,
1517             resource,
1518             [](napi_env env, void *data) {
1519                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1520                 asyncContext->errCode = AppAccountManager::GetAllOAuthTokens(
1521                     asyncContext->name, asyncContext->owner, asyncContext->oauthTokenInfos);
1522             },
1523             [](napi_env env, napi_status status, void *data) {
1524                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1525                 napi_value arrVal = nullptr;
1526                 napi_create_array(env, &arrVal);
1527                 GetOAuthTokenInfoForResult(env, asyncContext->oauthTokenInfos, arrVal);
1528                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1529                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1530                 ProcessCallbackOrPromise(env, asyncContext, err, arrVal);
1531                 napi_delete_async_work(env, asyncContext->work);
1532                 delete asyncContext;
1533                 asyncContext = nullptr;
1534             },
1535             reinterpret_cast<void *>(asyncContext),
1536             &asyncContext->work));
1537     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1538     return result;
1539 }
1540 
GetOAuthList(napi_env env,napi_callback_info cbInfo)1541 napi_value NapiAppAccount::GetOAuthList(napi_env env, napi_callback_info cbInfo)
1542 {
1543     return GetAuthListInternal(env, cbInfo, false);
1544 }
1545 
GetAuthList(napi_env env,napi_callback_info cbInfo)1546 napi_value NapiAppAccount::GetAuthList(napi_env env, napi_callback_info cbInfo)
1547 {
1548     return GetAuthListInternal(env, cbInfo, true);
1549 }
1550 
GetAuthListInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1551 napi_value NapiAppAccount::GetAuthListInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1552 {
1553     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1554     if (asyncContext == nullptr) {
1555         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1556         return NapiGetNull(env);
1557     }
1558     asyncContext->env = env;
1559     asyncContext->throwErr = isThrowable;
1560     if ((!ParseContextForGetOAuthList(env, cbInfo, asyncContext)) && isThrowable) {
1561         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1562         delete asyncContext;
1563         return NapiGetNull(env);
1564     }
1565     napi_value result = nullptr;
1566     if (asyncContext->callbackRef == nullptr) {
1567         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1568     } else {
1569         NAPI_CALL(env, napi_get_undefined(env, &result));
1570     }
1571     napi_value resource = nullptr;
1572     NAPI_CALL(env, napi_create_string_utf8(env, "GetOAuthList", NAPI_AUTO_LENGTH, &resource));
1573     NAPI_CALL(env,
1574         napi_create_async_work(env, nullptr, resource,
1575             [](napi_env env, void *data) {
1576                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1577                 if (asyncContext->throwErr) {
1578                     asyncContext->errCode = AppAccountManager::GetAuthList(
1579                         asyncContext->name, asyncContext->authType, asyncContext->authList);
1580                 } else {
1581                     asyncContext->errCode = AppAccountManager::GetOAuthList(
1582                         asyncContext->name, asyncContext->authType, asyncContext->authList);
1583                 }
1584             },
1585             [](napi_env env, napi_status status, void *data) {
1586                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1587                 napi_value arrVal = nullptr;
1588                 napi_create_array(env, &arrVal);
1589                 GetOAuthListForResult(env, asyncContext->authList, arrVal);
1590                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1591                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1592                 ProcessCallbackOrPromise(env, asyncContext, err, arrVal);
1593                 napi_delete_async_work(env, asyncContext->work);
1594                 delete asyncContext;
1595                 asyncContext = nullptr;
1596             },
1597             reinterpret_cast<void *>(asyncContext),
1598             &asyncContext->work));
1599     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1600     return result;
1601 }
1602 
GetAuthenticatorCallback(napi_env env,napi_callback_info cbInfo)1603 napi_value NapiAppAccount::GetAuthenticatorCallback(napi_env env, napi_callback_info cbInfo)
1604 {
1605     return GetAuthCallbackInternal(env, cbInfo, false);
1606 }
1607 
GetAuthCallback(napi_env env,napi_callback_info cbInfo)1608 napi_value NapiAppAccount::GetAuthCallback(napi_env env, napi_callback_info cbInfo)
1609 {
1610     return GetAuthCallbackInternal(env, cbInfo, true);
1611 }
1612 
GetAuthCallbackInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1613 napi_value NapiAppAccount::GetAuthCallbackInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1614 {
1615     auto *asyncContext = new (std::nothrow) OAuthAsyncContext();
1616     if (asyncContext == nullptr) {
1617         ACCOUNT_LOGE("insufficient memory for asyncContext!");
1618         return NapiGetNull(env);
1619     }
1620     asyncContext->env = env;
1621     asyncContext->throwErr = isThrowable;
1622     if ((!ParseContextForGetAuthenticatorCallback(env, cbInfo, asyncContext)) && isThrowable) {
1623         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, asyncContext->errMsg));
1624         delete asyncContext;
1625         return NapiGetNull(env);
1626     }
1627     napi_value result = nullptr;
1628     if (asyncContext->callbackRef == nullptr) {
1629         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1630     } else {
1631         NAPI_CALL(env, napi_get_undefined(env, &result));
1632     }
1633     napi_value resource = nullptr;
1634     NAPI_CALL(env, napi_create_string_utf8(env, "GetAuthenticatorCallback", NAPI_AUTO_LENGTH, &resource));
1635     NAPI_CALL(env,
1636         napi_create_async_work(env,
1637             nullptr,
1638             resource,
1639             [](napi_env env, void *data) {
1640                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1641                 asyncContext->errCode = AppAccountManager::GetAuthenticatorCallback(
1642                     asyncContext->sessionId, asyncContext->authenticatorCb);
1643             },
1644             [](napi_env env, napi_status status, void *data) {
1645                 OAuthAsyncContext *asyncContext = reinterpret_cast<OAuthAsyncContext *>(data);
1646                 napi_value result = nullptr;
1647                 GetAuthenticatorCallbackForResult(env, asyncContext->authenticatorCb, &result);
1648                 napi_value err = asyncContext->throwErr ? GenerateBusinessError(env, asyncContext->errCode) :
1649                     GetErrorCodeValue(env, ConvertToJSErrCodeV8(asyncContext->errCode));
1650                 ProcessCallbackOrPromise(env, asyncContext, err, result);
1651                 napi_delete_async_work(env, asyncContext->work);
1652                 delete asyncContext;
1653                 asyncContext = nullptr;
1654             },
1655             reinterpret_cast<void *>(asyncContext),
1656             &asyncContext->work));
1657     NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1658     return result;
1659 }
1660 
CheckAppAccess(napi_env env,napi_callback_info cbInfo)1661 napi_value NapiAppAccount::CheckAppAccess(napi_env env, napi_callback_info cbInfo)
1662 {
1663     auto *context = new (std::nothrow) AppAccountAsyncContext();
1664     if (context == nullptr) {
1665         ACCOUNT_LOGE("insufficient memory for context!");
1666         return NapiGetNull(env);
1667     }
1668     context->env = env;
1669     if (!ParseContextWithBdName(env, cbInfo, context)) {
1670         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
1671         delete context;
1672         return NapiGetNull(env);
1673     }
1674     napi_value result = nullptr;
1675     if (context->callbackRef == nullptr) {
1676         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
1677     } else {
1678         NAPI_CALL(env, napi_get_undefined(env, &result));
1679     }
1680     napi_value resource = nullptr;
1681     NAPI_CALL(env, napi_create_string_utf8(env, "CheckAppAccess", NAPI_AUTO_LENGTH, &resource));
1682     NAPI_CALL(env, napi_create_async_work(env,
1683         nullptr,
1684         resource,
1685         [](napi_env env, void *data) {
1686             auto context = reinterpret_cast<AppAccountAsyncContext *>(data);
1687             context->errCode = AppAccountManager::CheckAppAccess(
1688                 context->name, context->bundleName, context->isAccessible);
1689         },
1690         [](napi_env env, napi_status status, void *data) {
1691             auto context = reinterpret_cast<AppAccountAsyncContext *>(data);
1692             napi_value boolVal = nullptr;
1693             napi_get_boolean(env, context->isAccessible, &boolVal);
1694             ProcessCallbackOrPromise(env, context, GenerateBusinessError(env, context->errCode), boolVal);
1695             napi_delete_async_work(env, context->work);
1696             delete context;
1697             context = nullptr;
1698         },
1699         reinterpret_cast<void *>(context),
1700         &context->work));
1701     NAPI_CALL(env, napi_queue_async_work(env, context->work));
1702     return result;
1703 }
1704 
DeleteAccountCredential(napi_env env,napi_callback_info cbInfo)1705 napi_value NapiAppAccount::DeleteAccountCredential(napi_env env, napi_callback_info cbInfo)
1706 {
1707     return DeleteCredentialInternal(env, cbInfo, false);
1708 }
1709 
DeleteCredential(napi_env env,napi_callback_info cbInfo)1710 napi_value NapiAppAccount::DeleteCredential(napi_env env, napi_callback_info cbInfo)
1711 {
1712     return DeleteCredentialInternal(env, cbInfo, true);
1713 }
1714 
DeleteCredentialInternal(napi_env env,napi_callback_info cbInfo,bool isThrowable)1715 napi_value NapiAppAccount::DeleteCredentialInternal(napi_env env, napi_callback_info cbInfo, bool isThrowable)
1716 {
1717     napi_value result = NapiGetNull(env);
1718     auto *context = new (std::nothrow) AppAccountAsyncContext();
1719     if (context == nullptr) {
1720         ACCOUNT_LOGE("insufficient memory for context!");
1721         return result;
1722     }
1723     context->env = env;
1724     context->throwErr = isThrowable;
1725     if (!ParseContextWithCredentialType(env, cbInfo, context) && isThrowable) {
1726         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
1727         delete context;
1728         return result;
1729     }
1730     if (context->callbackRef == nullptr) {
1731         napi_create_promise(env, &context->deferred, &result);
1732     }
1733     napi_value resource = nullptr;
1734     napi_create_string_utf8(env, "DeleteAccountCredential", NAPI_AUTO_LENGTH, &resource);
1735     napi_create_async_work(env, nullptr, resource,
1736         [](napi_env env, void *data) {
1737             auto context = reinterpret_cast<AppAccountAsyncContext *>(data);
1738             context->errCode = AppAccountManager::DeleteAccountCredential(
1739                 context->name, context->credentialType);
1740         },
1741         [](napi_env env, napi_status status, void *data) {
1742             auto context = reinterpret_cast<AppAccountAsyncContext *>(data);
1743             if (context->throwErr) {
1744                 ProcessCallbackOrPromise(env, context, GenerateBusinessError(env, context->errCode), NapiGetNull(env));
1745             } else {
1746                 napi_value ret = nullptr;
1747                 napi_get_undefined(env, &ret);
1748                 ProcessCallbackOrPromise(env, context, GenerateBusinessError(env, context->errCode), ret);
1749             }
1750             napi_delete_async_work(env, context->work);
1751             delete context;
1752             context = nullptr;
1753         }, reinterpret_cast<void *>(context), &context->work);
1754     napi_queue_async_work(env, context->work);
1755     return result;
1756 }
1757 
CheckAccountLabels(napi_env env,napi_callback_info cbInfo)1758 napi_value NapiAppAccount::CheckAccountLabels(napi_env env, napi_callback_info cbInfo)
1759 {
1760     auto context = new (std::nothrow) CheckAccountLabelsContext();
1761     if (context == nullptr) {
1762         ACCOUNT_LOGE("insufficient memory for context!");
1763         return NapiGetNull(env);
1764     }
1765     context->env = env;
1766     if (!ParseContextForCheckAccountLabels(env, cbInfo, context)) {
1767         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
1768         delete context;
1769         return NapiGetNull(env);
1770     }
1771     napi_value result = nullptr;
1772     if (context->callbackRef == nullptr) {
1773         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
1774     } else {
1775         NAPI_CALL(env, napi_get_undefined(env, &result));
1776     }
1777     napi_value resource = nullptr;
1778     NAPI_CALL(env, napi_create_string_utf8(env, "CheckAccountLabels", NAPI_AUTO_LENGTH, &resource));
1779     NAPI_CALL(env, napi_create_async_work(env,
1780         nullptr,
1781         resource,
1782         [](napi_env env, void *data) {
1783             auto context = reinterpret_cast<CheckAccountLabelsContext *>(data);
1784             sptr<AuthenticatorAsyncCallback> callback = new (std::nothrow) AuthenticatorAsyncCallback(
1785                 *context, CheckAccountLabelsOnResultWork);
1786             if (callback == nullptr) {
1787                 ACCOUNT_LOGE("failed to create AuthenticatorAsyncCallback for insufficient memory");
1788                 context->errCode = ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
1789                 return;
1790             }
1791             context->errCode = AppAccountManager::CheckAccountLabels(
1792                 context->name, context->owner, context->labels, callback);
1793         },
1794         [](napi_env env, napi_status status, void *data) {
1795             auto context = reinterpret_cast<CheckAccountLabelsContext *>(data);
1796             if (context->errCode != ERR_OK) {
1797                 ProcessCallbackOrPromise(env, context, GenerateBusinessError(env, context->errCode), NapiGetNull(env));
1798             }
1799             napi_delete_async_work(env, context->work);
1800             delete context;
1801             context = nullptr;
1802         },
1803         reinterpret_cast<void *>(context), &context->work));
1804     NAPI_CALL(env, napi_queue_async_work(env, context->work));
1805     return result;
1806 }
1807 
SelectAccountsByOptions(napi_env env,napi_callback_info cbInfo)1808 napi_value NapiAppAccount::SelectAccountsByOptions(napi_env env, napi_callback_info cbInfo)
1809 {
1810     auto *context = new (std::nothrow) SelectAccountsContext();
1811     if (context == nullptr) {
1812         ACCOUNT_LOGE("insufficient memory for context!");
1813         return NapiGetNull(env);
1814     }
1815     context->env = env;
1816     if (!ParseContextForSelectAccount(env, cbInfo, context)) {
1817         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
1818         delete context;
1819         return NapiGetNull(env);
1820     }
1821     napi_value result = nullptr;
1822     if (context->callbackRef == nullptr) {
1823         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
1824     } else {
1825         NAPI_CALL(env, napi_get_undefined(env, &result));
1826     }
1827     napi_value resource = nullptr;
1828     NAPI_CALL(env, napi_create_string_utf8(env, "SelectAccountsByOptions", NAPI_AUTO_LENGTH, &resource));
1829     NAPI_CALL(env, napi_create_async_work(env,
1830         nullptr,
1831         resource,
1832         [](napi_env env, void *data) {
1833             auto context = reinterpret_cast<SelectAccountsContext *>(data);
1834             sptr<AuthenticatorAsyncCallback> callback = new (std::nothrow) AuthenticatorAsyncCallback(
1835                 *context, SelectAccountsOnResultWork);
1836             if (callback == nullptr) {
1837                 ACCOUNT_LOGD("failed to create AuthenticatorAsyncCallback for insufficient memory");
1838                 context->errCode = ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
1839                 return;
1840             }
1841             context->errCode =
1842                 AppAccountManager::SelectAccountsByOptions(context->options, callback);
1843         },
1844         [](napi_env env, napi_status status, void *data) {
1845             auto context = reinterpret_cast<SelectAccountsContext *>(data);
1846             if (context->errCode != ERR_OK) {
1847                 ProcessCallbackOrPromise(env, context, GenerateBusinessError(env, context->errCode), NapiGetNull(env));
1848             }
1849             napi_delete_async_work(env, context->work);
1850             delete context;
1851             context = nullptr;
1852         },
1853         reinterpret_cast<void *>(context), &context->work));
1854     NAPI_CALL(env, napi_queue_async_work(env, context->work));
1855     return result;
1856 }
1857 
VerifyCredential(napi_env env,napi_callback_info cbInfo)1858 napi_value NapiAppAccount::VerifyCredential(napi_env env, napi_callback_info cbInfo)
1859 {
1860     auto *context = new (std::nothrow) VerifyCredentialContext();
1861     if (context == nullptr) {
1862         ACCOUNT_LOGE("insufficient memory for context!");
1863         return NapiGetNull(env);
1864     }
1865     context->env = env;
1866     if (!ParseContextForVerifyCredential(env, cbInfo, context)) {
1867         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
1868         delete context;
1869         return NapiGetNull(env);
1870     }
1871     context->appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(env, context->callback);
1872     if (context->appAccountMgrCb == nullptr) {
1873         ACCOUNT_LOGD("failed to create AppAccountManagerCallback for insufficient memory");
1874         AAFwk::WantParams result;
1875         ProcessOnResultCallback(env, context->callback, ERR_JS_SYSTEM_SERVICE_EXCEPTION, result);
1876         delete context;
1877         return NapiGetNull(env);
1878     }
1879     napi_value resource = nullptr;
1880     NAPI_CALL(env, napi_create_string_utf8(env, "VerifyCredential", NAPI_AUTO_LENGTH, &resource));
1881     NAPI_CALL(env, napi_create_async_work(env,
1882         nullptr,
1883         resource,
1884         [](napi_env env, void *data) {
1885             auto context = reinterpret_cast<VerifyCredentialContext *>(data);
1886             ErrCode errCode = AppAccountManager::VerifyCredential(
1887                 context->name, context->owner, context->options, context->appAccountMgrCb);
1888             context->errCode = ConvertToJSErrCode(errCode);
1889         },
1890         VerifyCredCompleteCB, reinterpret_cast<void *>(context), &context->work));
1891     NAPI_CALL(env, napi_queue_async_work(env, context->work));
1892     return NapiGetNull(env);
1893 }
1894 
SetAuthenticatorProperties(napi_env env,napi_callback_info cbInfo)1895 napi_value NapiAppAccount::SetAuthenticatorProperties(napi_env env, napi_callback_info cbInfo)
1896 {
1897     auto *context = new (std::nothrow) SetPropertiesContext();
1898     if (context == nullptr) {
1899         ACCOUNT_LOGE("insufficient memory for context!");
1900         return NapiGetNull(env);
1901     }
1902     context->env = env;
1903     if (!ParseContextForSetProperties(env, cbInfo, context)) {
1904         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, context->errMsg));
1905         delete context;
1906         return NapiGetNull(env);
1907     }
1908     context->appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(env, context->callback);
1909     if (context->appAccountMgrCb == nullptr) {
1910         ACCOUNT_LOGD("failed to create AppAccountManagerCallback for insufficient memory");
1911         AAFwk::WantParams result;
1912         ProcessOnResultCallback(env, context->callback, ERR_JS_SYSTEM_SERVICE_EXCEPTION, result);
1913         delete context;
1914         return NapiGetNull(env);
1915     }
1916     napi_value resource = nullptr;
1917     NAPI_CALL(env, napi_create_string_utf8(env, "SetAuthenticatorProperties", NAPI_AUTO_LENGTH, &resource));
1918     NAPI_CALL(env, napi_create_async_work(env,
1919         nullptr,
1920         resource,
1921         [](napi_env env, void *data) {
1922             auto context = reinterpret_cast<SetPropertiesContext *>(data);
1923             ErrCode errCode = AppAccountManager::SetAuthenticatorProperties(
1924                 context->owner, context->options, context->appAccountMgrCb);
1925             context->errCode = ConvertToJSErrCode(errCode);
1926         },
1927         [](napi_env env, napi_status status, void *data) {
1928             auto context = reinterpret_cast<SetPropertiesContext *>(data);
1929             if ((context->errCode != ERR_JS_SUCCESS) && (context->appAccountMgrCb != nullptr)) {
1930                 AAFwk::Want errResult;
1931                 context->appAccountMgrCb->OnResult(context->errCode, errResult);
1932             }
1933             napi_delete_async_work(env, context->work);
1934             delete context;
1935             context = nullptr;
1936         },
1937         reinterpret_cast<void *>(context),
1938         &context->work));
1939     NAPI_CALL(env, napi_queue_async_work(env, context->work));
1940     return NapiGetNull(env);
1941 }
1942 
Subscribe(napi_env env,napi_callback_info cbInfo)1943 napi_value NapiAppAccount::Subscribe(napi_env env, napi_callback_info cbInfo)
1944 {
1945     AsyncContextForSubscribe *context = new (std::nothrow) AsyncContextForSubscribe(env);
1946     if (context == nullptr) {
1947         ACCOUNT_LOGE("asyncContextForOn is null");
1948         return NapiGetNull(env);
1949     }
1950     if (!ParseParametersBySubscribe(env, cbInfo, context)) {
1951         if (context->type != TYPE_CHANGE) {
1952             napi_throw(env, GenerateBusinessError(env, context->errCode, context->errMsg));
1953         }
1954         delete context;
1955         return NapiGetNull(env);
1956     }
1957     if (context->appAccountManager == nullptr) {
1958         if (context->type != TYPE_CHANGE) {
1959             napi_throw(env, GenerateBusinessError(env, ERR_JS_SYSTEM_SERVICE_EXCEPTION,
1960                 std::string("system service exception")));
1961         }
1962         delete context;
1963         return NapiGetNull(env);
1964     }
1965     AppAccountSubscribeInfo subscribeInfo(context->owners);
1966     context->subscriber = std::make_shared<SubscriberPtr>(subscribeInfo);
1967     if (context->subscriber == nullptr) {
1968         ACCOUNT_LOGE("fail to create subscriber");
1969         delete context;
1970         return NapiGetNull(env);
1971     }
1972     context->subscriber->SetEnv(env);
1973     context->subscriber->SetCallbackRef(context->callbackRef);
1974 
1975     {
1976         std::lock_guard<std::mutex> lock(g_lockForAppAccountSubscribers);
1977         g_AppAccountSubscribers[context->appAccountManager].emplace_back(context);
1978     }
1979 
1980     ErrCode errCode = AppAccountManager::SubscribeAppAccount(context->subscriber);
1981     if ((errCode != ERR_OK) && (context->type != TYPE_CHANGE)) {
1982         napi_throw(env, GenerateBusinessError(env, errCode));
1983     }
1984     return NapiGetNull(env);
1985 }
1986 
Unsubscribe(napi_env env,napi_callback_info cbInfo)1987 napi_value NapiAppAccount::Unsubscribe(napi_env env, napi_callback_info cbInfo)
1988 {
1989     AsyncContextForUnsubscribe *context = new (std::nothrow) AsyncContextForUnsubscribe(env);
1990     if (context == nullptr) {
1991         ACCOUNT_LOGE("asyncContextForOff is null");
1992         return NapiGetNull(env);
1993     }
1994     if (!ParseParametersByUnsubscribe(env, cbInfo, context)) {
1995         if (context->type != TYPE_CHANGE) {
1996             napi_throw(env, GenerateBusinessError(env, context->errCode, context->errMsg));
1997         }
1998         delete context;
1999         return NapiGetNull(env);
2000     };
2001     bool isFind = false;
2002     std::vector<std::shared_ptr<SubscriberPtr>> subscribers = {nullptr};
2003     napi_value result = GetSubscriberByUnsubscribe(env, subscribers, context, isFind);
2004     if (!result) {
2005         ACCOUNT_LOGE("Unsubscribe failed. The current subscriber does not exist");
2006         return NapiGetNull(env);
2007     }
2008     context->subscribers = subscribers;
2009 
2010     napi_value resourceName = nullptr;
2011     napi_create_string_latin1(env, "Unsubscribe", NAPI_AUTO_LENGTH, &resourceName);
2012 
2013     napi_create_async_work(env,
2014         nullptr,
2015         resourceName,
2016         UnsubscribeExecuteCB,
2017         UnsubscribeCallbackCompletedCB,
2018         reinterpret_cast<void *>(context),
2019         &context->work);
2020     napi_queue_async_work(env, context->work);
2021     return NapiGetNull(env);
2022 }
2023 }  // namespace AccountJsKit
2024 }  // namespace OHOS
2025