• 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_os_account_common.h"
17 #include <string>
18 #include "napi_account_error.h"
19 #include "napi_account_common.h"
20 #include "napi/native_common.h"
21 #include "napi_os_account.h"
22 
23 namespace OHOS {
24 namespace AccountJsKit {
NapiCreateDomainCallback(napi_env env,napi_ref callbackRef,napi_deferred deferred)25 NapiCreateDomainCallback::NapiCreateDomainCallback(napi_env env, napi_ref callbackRef, napi_deferred deferred)
26     : env_(env), callbackRef_(callbackRef), deferred_(deferred)
27 {}
28 
OnResult(const int32_t errCode,Parcel & parcel)29 void NapiCreateDomainCallback::OnResult(const int32_t errCode, Parcel &parcel)
30 {
31     std::shared_ptr<OsAccountInfo> osAccountInfo(OsAccountInfo::Unmarshalling(parcel));
32     if (osAccountInfo == nullptr) {
33         ACCOUNT_LOGE("failed to unmarshalling OsAccountInfo");
34         return;
35     }
36     std::unique_lock<std::mutex> lock(lockInfo_.mutex);
37     if ((callbackRef_ == nullptr) && (deferred_ == nullptr)) {
38         ACCOUNT_LOGE("js callback is nullptr");
39         return;
40     }
41     uv_loop_s *loop = nullptr;
42     uv_work_t *work = nullptr;
43     if (!CreateExecEnv(env_, &loop, &work)) {
44         ACCOUNT_LOGE("failed to init domain plugin execution environment");
45         return;
46     }
47     auto *asyncContext = new (std::nothrow) CreateOAForDomainAsyncContext();
48     if (asyncContext == nullptr) {
49         delete work;
50         return;
51     }
52     asyncContext->osAccountInfos = *osAccountInfo;
53     asyncContext->errCode = errCode;
54     asyncContext->env = env_;
55     asyncContext->callbackRef = callbackRef_;
56     asyncContext->deferred = deferred_;
57     work->data = reinterpret_cast<void *>(asyncContext);
58     int resultCode = uv_queue_work_with_qos(
59         loop, work, [](uv_work_t *work) {}, CreateOAForDomainCallbackCompletedWork, uv_qos_default);
60     if (resultCode != 0) {
61         ACCOUNT_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
62         delete asyncContext;
63         delete work;
64         return;
65     }
66     callbackRef_ = nullptr;
67     deferred_ = nullptr;
68 }
69 
WrapVoidToJS(napi_env env)70 napi_value WrapVoidToJS(napi_env env)
71 {
72     napi_value result = nullptr;
73     NAPI_CALL(env, napi_get_null(env, &result));
74     return result;
75 }
76 
ParseOneParaContext(napi_env env,napi_callback_info cbInfo,CommonAsyncContext * asyncContext)77 static bool ParseOneParaContext(napi_env env, napi_callback_info cbInfo, CommonAsyncContext *asyncContext)
78 {
79     size_t argc = ARGS_SIZE_ONE;
80     napi_value argv[ARGS_SIZE_ONE] = {0};
81     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
82     if (argc == ARGS_SIZE_ONE) {
83         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
84             ACCOUNT_LOGE("Get callbackRef failed");
85             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
86             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
87             return false;
88         }
89     }
90     return true;
91 }
92 
ParseCallbackAndId(napi_env env,napi_callback_info cbInfo,napi_ref & callbackRef,int & id,bool throwErr)93 bool ParseCallbackAndId(napi_env env, napi_callback_info cbInfo, napi_ref &callbackRef, int &id, bool throwErr)
94 {
95     size_t argc = ARGS_SIZE_TWO;
96     napi_value argv[ARGS_SIZE_TWO] = {0};
97     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
98 
99     if (argc == ARGS_SIZE_TWO) {
100         if (!GetCallbackProperty(env, argv[argc - 1], callbackRef, 1)) {
101             ACCOUNT_LOGE("Get callbackRef failed");
102             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
103             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, throwErr);
104             return false;
105         }
106     }
107     if (!GetIntProperty(env, argv[PARAMZERO], id)) {
108         ACCOUNT_LOGE("Get id failed");
109         std::string errMsg = "The type of arg 1 must be number";
110         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, throwErr);
111         return false;
112     }
113     return true;
114 }
115 
ParseParaQueryOAByIdCB(napi_env env,napi_callback_info cbInfo,QueryOAByIdAsyncContext * asyncContext)116 bool ParseParaQueryOAByIdCB(napi_env env, napi_callback_info cbInfo, QueryOAByIdAsyncContext *asyncContext)
117 {
118     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
119 }
120 
QueryOAByIdExecuteCB(napi_env env,void * data)121 void QueryOAByIdExecuteCB(napi_env env, void *data)
122 {
123     QueryOAByIdAsyncContext *asyncContext = reinterpret_cast<QueryOAByIdAsyncContext *>(data);
124     asyncContext->errCode = OsAccountManager::QueryOsAccountById(asyncContext->id, asyncContext->osAccountInfos);
125     ACCOUNT_LOGD("errcode is %{public}d", asyncContext->errCode);
126     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
127 }
128 
QueryOAByIdCallbackCompletedCB(napi_env env,napi_status status,void * data)129 void QueryOAByIdCallbackCompletedCB(napi_env env, napi_status status, void *data)
130 {
131     QueryOAByIdAsyncContext *asyncContext = reinterpret_cast<QueryOAByIdAsyncContext *>(data);
132     napi_value errJs = nullptr;
133     napi_value dataJs = nullptr;
134     if (asyncContext->status == napi_ok) {
135         napi_get_null(env, &errJs);
136         GetOACBInfoToJs(env, asyncContext->osAccountInfos, dataJs);
137     } else {
138         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
139         napi_get_null(env, &dataJs);
140     }
141     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
142     delete asyncContext;
143 }
144 
CreateJsDomainInfo(napi_env env,const DomainAccountInfo & info,napi_value & result)145 void CreateJsDomainInfo(napi_env env, const DomainAccountInfo &info, napi_value &result)
146 {
147     napi_create_object(env, &result);
148     napi_value value = nullptr;
149     // domain
150     napi_create_string_utf8(env, info.domain_.c_str(), info.domain_.size(), &value);
151     napi_set_named_property(env, result, "domain", value);
152 
153     // domain accountName
154     napi_create_string_utf8(env, info.accountName_.c_str(), info.accountName_.size(), &value);
155     napi_set_named_property(env, result, "accountName", value);
156 
157     napi_create_string_utf8(env, info.accountId_.c_str(), info.accountId_.size(), &value);
158     napi_set_named_property(env, result, "accountId", value);
159 }
160 
CreateJsDistributedInfo(napi_env env,const OhosAccountInfo & info,napi_value & result)161 void CreateJsDistributedInfo(napi_env env, const OhosAccountInfo &info, napi_value &result)
162 {
163     napi_create_object(env, &result);
164     napi_value value = nullptr;
165     // name
166     napi_create_string_utf8(env, info.name_.c_str(), info.name_.size(), &value);
167     napi_set_named_property(env, result, "name", value);
168 
169     // id
170     napi_create_string_utf8(env, info.uid_.c_str(), info.uid_.size(), &value);
171     napi_set_named_property(env, result, "id", value);
172 
173     // event
174     napi_create_string_utf8(env, "", 0, &value);
175     napi_set_named_property(env, result, "event", value);
176 
177     // scalableData
178     napi_value scalable = nullptr;
179     napi_create_object(env, &scalable);
180     napi_set_named_property(env, result, "scalableData", scalable);
181 }
182 
GetOACBInfoToJs(napi_env env,OsAccountInfo & info,napi_value & objOAInfo)183 void GetOACBInfoToJs(napi_env env, OsAccountInfo &info, napi_value &objOAInfo)
184 {
185     napi_create_object(env, &objOAInfo);
186     // localId
187     napi_value idToJs = nullptr;
188     napi_create_int32(env, info.GetLocalId(), &idToJs);
189     napi_set_named_property(env, objOAInfo, "localId", idToJs);
190 
191     // localName
192     napi_value nameToJs = nullptr;
193     napi_create_string_utf8(env, info.GetLocalName().c_str(), NAPI_AUTO_LENGTH, &nameToJs);
194     napi_set_named_property(env, objOAInfo, "localName", nameToJs);
195 
196     // type
197     napi_value typeToJsObj = nullptr;
198     napi_create_int32(env, static_cast<int>(info.GetType()), &typeToJsObj);
199     napi_set_named_property(env, objOAInfo, "type", typeToJsObj);
200 
201     // constraints
202     napi_value constraintsToJs = nullptr;
203     napi_create_array(env, &constraintsToJs);
204     MakeArrayToJs(env, info.GetConstraints(), constraintsToJs);
205     napi_set_named_property(env, objOAInfo, "constraints", constraintsToJs);
206 
207     // isVerified
208     napi_value isVerifiedToJs = nullptr;
209     napi_get_boolean(env, info.GetIsVerified(), &isVerifiedToJs);
210     napi_set_named_property(env, objOAInfo, "isVerified", isVerifiedToJs);
211 
212     // photo
213     napi_value photoToJs = nullptr;
214     napi_create_string_utf8(env, info.GetPhoto().c_str(), NAPI_AUTO_LENGTH, &photoToJs);
215     napi_set_named_property(env, objOAInfo, "photo", photoToJs);
216 
217     // createTime
218     napi_value createTimeToJs = nullptr;
219     napi_create_int64(env, info.GetCreateTime(), &createTimeToJs);
220     napi_set_named_property(env, objOAInfo, "createTime", createTimeToJs);
221 
222     // lastLoginTime
223     napi_value lastLoginTimeToJs = nullptr;
224     napi_create_int64(env, info.GetLastLoginTime(), &lastLoginTimeToJs);
225     napi_set_named_property(env, objOAInfo, "lastLoginTime", lastLoginTimeToJs);
226 
227     // serialNumber
228     napi_value serialNumberToJs = nullptr;
229     napi_create_int64(env, info.GetSerialNumber(), &serialNumberToJs);
230     napi_set_named_property(env, objOAInfo, "serialNumber", serialNumberToJs);
231 
232     // isActived
233     napi_value isActivedToJs = nullptr;
234     napi_get_boolean(env, info.GetIsActived(), &isActivedToJs);
235     napi_set_named_property(env, objOAInfo, "isActived", isActivedToJs);
236 
237     // isCreateCompleted
238     napi_value isCreateCompletedToJs = nullptr;
239     napi_get_boolean(env, info.GetIsCreateCompleted(), &isCreateCompletedToJs);
240     napi_set_named_property(env, objOAInfo, "isCreateCompleted", isCreateCompletedToJs);
241 
242     // distributedInfo: distributedAccount.DistributedInfo
243     napi_value dbInfoToJs = nullptr;
244     std::pair<bool, OhosAccountInfo> dbAccountInfo = OhosAccountKits::GetInstance().QueryOhosAccountInfo();
245     if (dbAccountInfo.first) {
246         CreateJsDistributedInfo(env, dbAccountInfo.second, dbInfoToJs);
247     }
248     napi_set_named_property(env, objOAInfo, "distributedInfo", dbInfoToJs);
249 
250     // domainInfo: domainInfo.DomainAccountInfo
251     DomainAccountInfo domainInfo;
252     info.GetDomainInfo(domainInfo);
253     CreateJsDomainInfo(env, domainInfo, dbInfoToJs);
254     napi_set_named_property(env, objOAInfo, "domainInfo", dbInfoToJs);
255 }
256 
MakeArrayToJs(napi_env env,const std::vector<std::string> & constraints,napi_value jsArray)257 void MakeArrayToJs(napi_env env, const std::vector<std::string> &constraints, napi_value jsArray)
258 {
259     uint32_t index = 0;
260 
261     for (auto item : constraints) {
262         napi_value constraint = nullptr;
263         napi_create_string_utf8(env, item.c_str(), NAPI_AUTO_LENGTH, &constraint);
264         napi_set_element(env, jsArray, index, constraint);
265         index++;
266     }
267 }
268 
ParseParaRemoveOACB(napi_env env,napi_callback_info cbInfo,RemoveOAAsyncContext * asyncContext)269 bool ParseParaRemoveOACB(napi_env env, napi_callback_info cbInfo, RemoveOAAsyncContext *asyncContext)
270 {
271     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
272 }
273 
RemoveOAExecuteCB(napi_env env,void * data)274 void RemoveOAExecuteCB(napi_env env, void *data)
275 {
276     RemoveOAAsyncContext *asyncContext = reinterpret_cast<RemoveOAAsyncContext *>(data);
277     asyncContext->errCode = OsAccountManager::RemoveOsAccount(asyncContext->id);
278     ACCOUNT_LOGD("errcode is %{public}d", asyncContext->errCode);
279     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
280 }
281 
RemoveOACallbackCompletedCB(napi_env env,napi_status status,void * data)282 void RemoveOACallbackCompletedCB(napi_env env, napi_status status, void *data)
283 {
284     ACCOUNT_LOGD("napi_create_async_work complete");
285     RemoveOAAsyncContext *asyncContext = reinterpret_cast<RemoveOAAsyncContext *>(data);
286     napi_value errJs = nullptr;
287     napi_value dataJs = nullptr;
288     if (asyncContext->status == napi_ok) {
289         napi_get_null(env, &errJs);
290         napi_get_null(env, &dataJs);
291     } else {
292         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
293         napi_get_null(env, &dataJs);
294     }
295     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
296     delete asyncContext;
297 }
298 
ParseParaSetOAName(napi_env env,napi_callback_info cbInfo,SetOANameAsyncContext * asyncContext)299 bool ParseParaSetOAName(napi_env env, napi_callback_info cbInfo, SetOANameAsyncContext *asyncContext)
300 {
301     size_t argc = ARGS_SIZE_THREE;
302     napi_value argv[ARGS_SIZE_THREE] = {0};
303     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
304 
305     if (argc == ARGS_SIZE_THREE) {
306         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
307             ACCOUNT_LOGE("Get callbackRef failed");
308             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
309             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
310             return false;
311         }
312     }
313 
314     if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->id)) {
315         ACCOUNT_LOGE("Get id failed");
316         std::string errMsg = "The type of arg 1 must be number";
317         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
318         return false;
319     }
320     if (!GetStringProperty(env, argv[PARAMONE], asyncContext->name)) {
321         ACCOUNT_LOGE("Get name failed");
322         std::string errMsg = "The type of arg 2 must be string";
323         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
324         return false;
325     }
326 
327     return true;
328 }
329 
SetOANameExecuteCB(napi_env env,void * data)330 void SetOANameExecuteCB(napi_env env, void *data)
331 {
332     SetOANameAsyncContext *asyncContext = reinterpret_cast<SetOANameAsyncContext *>(data);
333     asyncContext->errCode = OsAccountManager::SetOsAccountName(asyncContext->id, asyncContext->name);
334     ACCOUNT_LOGD("errcode is %{public}d", asyncContext->errCode);
335     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
336 }
337 
SetOANameCallbackCompletedCB(napi_env env,napi_status status,void * data)338 void SetOANameCallbackCompletedCB(napi_env env, napi_status status, void *data)
339 {
340     ACCOUNT_LOGD("napi_create_async_work complete");
341     SetOANameAsyncContext *asyncContext = reinterpret_cast<SetOANameAsyncContext *>(data);
342     napi_value errJs = nullptr;
343     napi_value dataJs = nullptr;
344     if (asyncContext->status == napi_ok) {
345         napi_get_null(env, &errJs);
346         napi_get_null(env, &dataJs);
347     } else {
348         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
349         napi_get_null(env, &dataJs);
350     }
351     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
352     delete asyncContext;
353 }
354 
ParseParaSetOAConstraints(napi_env env,napi_callback_info cbInfo,SetOAConsAsyncContext * asyncContext)355 bool ParseParaSetOAConstraints(napi_env env, napi_callback_info cbInfo, SetOAConsAsyncContext *asyncContext)
356 {
357     size_t argc = ARGS_SIZE_FOUR;
358     napi_value argv[ARGS_SIZE_FOUR] = {0};
359     NAPI_CALL_BASE(env, napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr), false);
360 
361     // argv[3] : callback
362     if (argc == ARGS_SIZE_FOUR) {
363         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
364             ACCOUNT_LOGE("Get callbackRef failed");
365             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
366             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
367             return false;
368         }
369     }
370 
371     // argv[0] : localId
372     if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->id)) {
373         ACCOUNT_LOGE("Get id failed");
374         std::string errMsg = "The type of arg 1 must be number";
375         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
376         return false;
377     }
378 
379     // argv[1] : Array<string>
380     if (!GetStringArrayProperty(env, argv[PARAMONE], asyncContext->constraints, false)) {
381         ACCOUNT_LOGE("Get constraints failed, expected array of strings");
382         std::string errMsg = "The type of arg 2 must be unempty array of strings";
383         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
384         return false;
385     }
386 
387     // argv[2] : enable
388     if (!GetBoolProperty(env, argv[PARAMTWO], asyncContext->enable)) {
389         ACCOUNT_LOGE("Get enable failed");
390         std::string errMsg = "The type of arg 3 must be boolean";
391         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
392         return false;
393     }
394 
395     return true;
396 }
397 
SetOAConsExecuteCB(napi_env env,void * data)398 void SetOAConsExecuteCB(napi_env env, void *data)
399 {
400     SetOAConsAsyncContext *asyncContext = reinterpret_cast<SetOAConsAsyncContext *>(data);
401     asyncContext->errCode =
402         OsAccountManager::SetOsAccountConstraints(asyncContext->id, asyncContext->constraints, asyncContext->enable);
403     ACCOUNT_LOGD("errcode is %{public}d", asyncContext->errCode);
404     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
405 }
406 
SetOAConsCallbackCompletedCB(napi_env env,napi_status status,void * data)407 void SetOAConsCallbackCompletedCB(napi_env env, napi_status status, void *data)
408 {
409     ACCOUNT_LOGD("napi_create_async_work complete");
410     SetOAConsAsyncContext *asyncContext = reinterpret_cast<SetOAConsAsyncContext *>(data);
411     napi_value errJs = nullptr;
412     napi_value dataJs = nullptr;
413     if (asyncContext->status == napi_ok) {
414         napi_get_null(env, &errJs);
415         napi_get_null(env, &dataJs);
416     } else {
417         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
418         napi_get_null(env, &dataJs);
419     }
420     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
421     delete asyncContext;
422 }
423 
ParseParaActiveOA(napi_env env,napi_callback_info cbInfo,ActivateOAAsyncContext * asyncContext)424 bool ParseParaActiveOA(napi_env env, napi_callback_info cbInfo, ActivateOAAsyncContext *asyncContext)
425 {
426     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
427 }
428 
ActivateOAExecuteCB(napi_env env,void * data)429 void ActivateOAExecuteCB(napi_env env, void *data)
430 {
431     ActivateOAAsyncContext *activateOACB = reinterpret_cast<ActivateOAAsyncContext *>(data);
432     activateOACB->errCode = OsAccountManager::ActivateOsAccount(activateOACB->id);
433     ACCOUNT_LOGD("errcode is %{public}d", activateOACB->errCode);
434     activateOACB->status = (activateOACB->errCode == 0) ? napi_ok : napi_generic_failure;
435 }
436 
ActivateOACallbackCompletedCB(napi_env env,napi_status status,void * data)437 void ActivateOACallbackCompletedCB(napi_env env, napi_status status, void *data)
438 {
439     ACCOUNT_LOGD("napi_create_async_work complete");
440     ActivateOAAsyncContext *asyncContext = reinterpret_cast<ActivateOAAsyncContext *>(data);
441     napi_value errJs = nullptr;
442     napi_value dataJs = nullptr;
443     if (asyncContext->status == napi_ok) {
444         napi_get_null(env, &errJs);
445         napi_get_null(env, &dataJs);
446     } else {
447         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
448         napi_get_null(env, &dataJs);
449     }
450     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
451     delete asyncContext;
452 }
453 
ParseParaCreateOA(napi_env env,napi_callback_info cbInfo,CreateOAAsyncContext * asyncContext)454 bool ParseParaCreateOA(napi_env env, napi_callback_info cbInfo, CreateOAAsyncContext *asyncContext)
455 {
456     size_t argc = ARGS_SIZE_THREE;
457     napi_value argv[ARGS_SIZE_THREE] = {0};
458     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
459 
460     if (argc == ARGS_SIZE_THREE) {
461         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
462             ACCOUNT_LOGE("Get callbackRef failed");
463             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
464             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
465             return false;
466         }
467     }
468 
469     if (!GetStringProperty(env, argv[PARAMZERO], asyncContext->name)) {
470         ACCOUNT_LOGE("Get name failed");
471         std::string errMsg = "The type of arg 1 must be string";
472         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
473         return false;
474     }
475     int32_t type = 0;
476     if (!GetIntProperty(env, argv[PARAMONE], type)) {
477         ACCOUNT_LOGE("Get type failed");
478         std::string errMsg = "The type of arg 2 must be number";
479         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
480         return false;
481     }
482     asyncContext->type = static_cast<OsAccountType>(type);
483     return true;
484 }
485 
ParseDomainAccountInfo(napi_env env,napi_value object,DomainAccountInfo & info)486 static bool ParseDomainAccountInfo(napi_env env, napi_value object, DomainAccountInfo &info)
487 {
488     if (!GetStringPropertyByKey(env, object, "domain", info.domain_)) {
489         ACCOUNT_LOGE("get domainInfo's domain failed");
490         return false;
491     }
492     if (!GetStringPropertyByKey(env, object, "accountName", info.accountName_)) {
493         ACCOUNT_LOGE("get domainInfo's accountName failed");
494         return false;
495     }
496     bool hasProp = false;
497     napi_has_named_property(env, object, "accountId", &hasProp);
498     if (hasProp) {
499         napi_value value = nullptr;
500         napi_get_named_property(env, object, "accountId", &value);
501         napi_valuetype valueType = napi_undefined;
502         napi_typeof(env, value, &valueType);
503         if ((valueType == napi_undefined) || (valueType == napi_null)) {
504             ACCOUNT_LOGI("the accountId is undefined or null");
505         } else {
506             if (!GetStringProperty(env, value, info.accountId_)) {
507                 ACCOUNT_LOGE("get domainInfo's accountId failed");
508                 return false;
509             }
510         }
511     }
512     return true;
513 }
514 
ParseParaCreateOAForDomain(napi_env env,napi_callback_info cbInfo,CreateOAForDomainAsyncContext * asyncContext)515 bool ParseParaCreateOAForDomain(napi_env env, napi_callback_info cbInfo,
516     CreateOAForDomainAsyncContext *asyncContext)
517 {
518     size_t argc = ARGS_SIZE_THREE;
519     napi_value argv[ARGS_SIZE_THREE] = {0};
520     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
521 
522     if (argc == ARGS_SIZE_THREE) {
523         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
524             ACCOUNT_LOGE("Get callbackRef failed");
525             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
526             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
527             return false;
528         }
529     }
530 
531     int32_t id = 0;
532     if (!GetIntProperty(env, argv[PARAMZERO], id)) {
533         ACCOUNT_LOGE("Get id failed");
534         std::string errMsg = "The type of arg 1 must be number";
535         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
536         return false;
537     }
538     asyncContext->type = static_cast<OsAccountType>(id);
539 
540     if (!ParseDomainAccountInfo(env, argv[PARAMONE], asyncContext->domainInfo)) {
541         ACCOUNT_LOGE("get domainInfo failed");
542         std::string errMsg = "DomainInfo parse failed";
543         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
544         return false;
545     }
546     return true;
547 }
548 
CreateOAExecuteCB(napi_env env,void * data)549 void CreateOAExecuteCB(napi_env env, void *data)
550 {
551     ACCOUNT_LOGD("napi_create_async_work running");
552     CreateOAAsyncContext *asyncContext = reinterpret_cast<CreateOAAsyncContext *>(data);
553     asyncContext->errCode =
554         OsAccountManager::CreateOsAccount(asyncContext->name, asyncContext->type, asyncContext->osAccountInfos);
555     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
556 }
557 
CreateOAForDomainCompletedCB(napi_env env,napi_status status,void * data)558 void CreateOAForDomainCompletedCB(napi_env env, napi_status status, void *data)
559 {
560     delete reinterpret_cast<CreateOAForDomainAsyncContext *>(data);
561 }
562 
CreateOAForDomainExecuteCB(napi_env env,void * data)563 void CreateOAForDomainExecuteCB(napi_env env, void *data)
564 {
565     CreateOAForDomainAsyncContext *asyncContext = reinterpret_cast<CreateOAForDomainAsyncContext *>(data);
566     auto callback = std::make_shared<NapiCreateDomainCallback>(env, asyncContext->callbackRef, asyncContext->deferred);
567     asyncContext->errCode =
568         OsAccountManager::CreateOsAccountForDomain(asyncContext->type, asyncContext->domainInfo, callback);
569     if (asyncContext->errCode != ERR_OK) {
570         Parcel emptyParcel;
571         callback->OnResult(asyncContext->errCode, emptyParcel);
572     }
573     asyncContext->callbackRef = nullptr;
574     asyncContext->deferred = nullptr;
575 }
576 
CreateOACallbackCompletedCB(napi_env env,napi_status status,void * data)577 void CreateOACallbackCompletedCB(napi_env env, napi_status status, void *data)
578 {
579     ACCOUNT_LOGD("napi_create_async_work complete");
580     CreateOAAsyncContext *asyncContext = reinterpret_cast<CreateOAAsyncContext *>(data);
581     napi_value errJs = nullptr;
582     napi_value dataJs = nullptr;
583     if (asyncContext->status == napi_ok) {
584         napi_get_null(env, &errJs);
585         GetOACBInfoToJs(env, asyncContext->osAccountInfos, dataJs);
586     } else {
587         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
588         napi_get_null(env, &dataJs);
589     }
590     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
591     delete asyncContext;
592 }
593 
CreateOAForDomainCallbackCompletedWork(uv_work_t * work,int status)594 void CreateOAForDomainCallbackCompletedWork(uv_work_t *work, int status)
595 {
596     std::unique_ptr<uv_work_t> workPtr(work);
597     napi_handle_scope scope = nullptr;
598     if (!InitUvWorkCallbackEnv(work, scope)) {
599         return;
600     }
601     std::unique_ptr<CreateOAForDomainAsyncContext> asyncContext(
602         reinterpret_cast<CreateOAForDomainAsyncContext *>(work->data));
603     napi_value errJs = nullptr;
604     napi_value dataJs = nullptr;
605     if (asyncContext->errCode == ERR_OK) {
606         GetOACBInfoToJs(asyncContext->env, asyncContext->osAccountInfos, dataJs);
607     } else {
608         errJs = GenerateBusinessError(asyncContext->env, asyncContext->errCode);
609     }
610     ReturnCallbackOrPromise(asyncContext->env, asyncContext.get(), errJs, dataJs);
611     napi_close_handle_scope(asyncContext->env, scope);
612 }
613 
ParseParaGetOACount(napi_env env,napi_callback_info cbInfo,GetOACountAsyncContext * asyncContext)614 bool ParseParaGetOACount(napi_env env, napi_callback_info cbInfo, GetOACountAsyncContext *asyncContext)
615 {
616     return ParseOneParaContext(env, cbInfo, asyncContext);
617 }
618 
GetOACountExecuteCB(napi_env env,void * data)619 void GetOACountExecuteCB(napi_env env, void *data)
620 {
621     GetOACountAsyncContext *asyncContext = reinterpret_cast<GetOACountAsyncContext *>(data);
622     asyncContext->errCode = OsAccountManager::GetCreatedOsAccountsCount(asyncContext->osAccountsCount);
623     // for compatibility
624     if ((!asyncContext->throwErr) && (asyncContext->errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED)) {
625         asyncContext->errCode = ERR_OSACCOUNT_KIT_GET_CREATED_OS_ACCOUNT_COUNT_ERROR;
626     }
627     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
628 }
629 
GetOACountCallbackCompletedCB(napi_env env,napi_status status,void * data)630 void GetOACountCallbackCompletedCB(napi_env env, napi_status status, void *data)
631 {
632     ACCOUNT_LOGD("napi_create_async_work complete");
633     GetOACountAsyncContext *asyncContext = reinterpret_cast<GetOACountAsyncContext *>(data);
634     napi_value errJs = nullptr;
635     napi_value dataJs = nullptr;
636     if (asyncContext->status == napi_ok) {
637         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
638         napi_create_uint32(env, asyncContext->osAccountsCount, &dataJs);
639     } else {
640         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
641         if (asyncContext->throwErr) {
642             napi_get_null(env, &dataJs);
643         } else {
644             napi_create_uint32(env, asyncContext->osAccountsCount, &dataJs);
645         }
646     }
647     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
648     delete asyncContext;
649 }
650 
ParseParaDbDeviceId(napi_env env,napi_callback_info cbInfo,DbDeviceIdAsyncContext * asyncContext)651 bool ParseParaDbDeviceId(napi_env env, napi_callback_info cbInfo, DbDeviceIdAsyncContext *asyncContext)
652 {
653     return ParseOneParaContext(env, cbInfo, asyncContext);
654 }
655 
DbDeviceIdExecuteCB(napi_env env,void * data)656 void DbDeviceIdExecuteCB(napi_env env, void *data)
657 {
658     DbDeviceIdAsyncContext *asyncContext = reinterpret_cast<DbDeviceIdAsyncContext *>(data);
659     asyncContext->errCode = OsAccountManager::GetDistributedVirtualDeviceId(asyncContext->deviceId);
660     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
661     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
662 }
663 
DbDeviceIdCallbackCompletedCB(napi_env env,napi_status status,void * data)664 void DbDeviceIdCallbackCompletedCB(napi_env env, napi_status status, void *data)
665 {
666     ACCOUNT_LOGD("napi_create_async_work complete");
667     DbDeviceIdAsyncContext *asyncContext = reinterpret_cast<DbDeviceIdAsyncContext *>(data);
668     napi_value errJs = nullptr;
669     napi_value dataJs = nullptr;
670     if (asyncContext->status == napi_ok) {
671         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
672         napi_create_string_utf8(env, asyncContext->deviceId.c_str(), NAPI_AUTO_LENGTH, &dataJs);
673     } else {
674         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
675         if (asyncContext->throwErr) {
676             napi_get_null(env, &dataJs);
677         } else {
678             napi_create_string_utf8(env, asyncContext->deviceId.c_str(), NAPI_AUTO_LENGTH, &dataJs);
679         }
680     }
681     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
682     delete asyncContext;
683 }
684 
ParseParaGetAllCons(napi_env env,napi_callback_info cbInfo,GetAllConsAsyncContext * asyncContext)685 bool ParseParaGetAllCons(napi_env env, napi_callback_info cbInfo, GetAllConsAsyncContext *asyncContext)
686 {
687     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
688 }
689 
GetAllConsExecuteCB(napi_env env,void * data)690 void GetAllConsExecuteCB(napi_env env, void *data)
691 {
692     GetAllConsAsyncContext *asyncContext = reinterpret_cast<GetAllConsAsyncContext *>(data);
693     asyncContext->errCode = OsAccountManager::GetOsAccountAllConstraints(asyncContext->id, asyncContext->constraints);
694     // for compatibility
695     if ((!asyncContext->throwErr) && (asyncContext->errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED)) {
696         asyncContext->errCode = ERR_OSACCOUNT_KIT_GET_OS_ACCOUNT_ALL_CONSTRAINTS_ERROR;
697     }
698     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
699 }
700 
GetAllConsCallbackCompletedCB(napi_env env,napi_status status,void * data)701 void GetAllConsCallbackCompletedCB(napi_env env, napi_status status, void *data)
702 {
703     ACCOUNT_LOGD("napi_create_async_work complete");
704     GetAllConsAsyncContext *asyncContext = reinterpret_cast<GetAllConsAsyncContext *>(data);
705     napi_value errJs = nullptr;
706     napi_value dataJs = nullptr;
707     if (asyncContext->status == napi_ok) {
708         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
709         GetAllAccountCons(env, asyncContext->constraints, dataJs);
710     } else {
711         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
712         if (asyncContext->throwErr) {
713             napi_get_null(env, &dataJs);
714         } else {
715             GetAllAccountCons(env, asyncContext->constraints, dataJs);
716         }
717     }
718     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
719     delete asyncContext;
720 }
721 
GetAllAccountCons(napi_env env,const std::vector<std::string> & info,napi_value & result)722 void GetAllAccountCons(napi_env env, const std::vector<std::string> &info, napi_value &result)
723 {
724     napi_create_array(env, &result);
725     uint32_t index = 0;
726 
727     for (auto item : info) {
728         napi_value consStr = nullptr;
729         napi_create_string_utf8(env, item.c_str(), NAPI_AUTO_LENGTH, &consStr);
730         napi_set_element(env, result, index, consStr);
731         index++;
732     }
733 }
734 
GetActiveIds(napi_env env,const std::vector<int> & ids,napi_value & result)735 void GetActiveIds(napi_env env, const std::vector<int> &ids, napi_value &result)
736 {
737     napi_create_array(env, &result);
738     uint32_t index = 0;
739 
740     for (auto id : ids) {
741         napi_value tempID = nullptr;
742         napi_create_int32(env, id, &tempID);
743         napi_set_element(env, result, index, tempID);
744         index++;
745     }
746 }
747 
ParseParaProcessId(napi_env env,napi_callback_info cbInfo,GetIdAsyncContext * asyncContext)748 bool ParseParaProcessId(napi_env env, napi_callback_info cbInfo, GetIdAsyncContext *asyncContext)
749 {
750     return ParseOneParaContext(env, cbInfo, asyncContext);
751 }
752 
GetProcessIdExecuteCB(napi_env env,void * data)753 void GetProcessIdExecuteCB(napi_env env, void *data)
754 {
755     GetIdAsyncContext *asyncContext = reinterpret_cast<GetIdAsyncContext *>(data);
756     asyncContext->errCode = OsAccountManager::GetOsAccountLocalIdFromProcess(asyncContext->id);
757     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
758     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
759 }
760 
GetProcessIdCallbackCompletedCB(napi_env env,napi_status status,void * data)761 void GetProcessIdCallbackCompletedCB(napi_env env, napi_status status, void *data)
762 {
763     ACCOUNT_LOGD("napi_create_async_work complete");
764     GetIdAsyncContext *asyncContext = reinterpret_cast<GetIdAsyncContext *>(data);
765     napi_value errJs = nullptr;
766     napi_value dataJs = nullptr;
767     if (asyncContext->status == napi_ok) {
768         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
769         napi_create_int32(env, asyncContext->id, &dataJs);
770     } else {
771         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
772         if (asyncContext->throwErr) {
773             napi_get_null(env, &dataJs);
774         } else {
775             napi_create_int32(env, asyncContext->id, &dataJs);
776         }
777     }
778     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
779     delete asyncContext;
780 }
781 
ParseQueryAllCreateOA(napi_env env,napi_callback_info cbInfo,QueryCreateOAAsyncContext * asyncContext)782 bool ParseQueryAllCreateOA(napi_env env, napi_callback_info cbInfo, QueryCreateOAAsyncContext *asyncContext)
783 {
784     return ParseOneParaContext(env, cbInfo, asyncContext);
785 }
786 
ParseQueryOAConstraintSrcTypes(napi_env env,napi_callback_info cbInfo,QueryOAConstraintSrcTypeContext * asyncContext)787 bool ParseQueryOAConstraintSrcTypes(napi_env env, napi_callback_info cbInfo,
788     QueryOAConstraintSrcTypeContext *asyncContext)
789 {
790     size_t argc = ARGS_SIZE_THREE;
791     napi_value argv[ARGS_SIZE_THREE] = {0};
792     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
793     if (argc == ARGS_SIZE_THREE) {
794         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
795             ACCOUNT_LOGE("Get callbackRef failed");
796             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
797             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
798             return false;
799         }
800     }
801 
802     if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->id)) {
803         ACCOUNT_LOGE("Get id failed");
804         std::string errMsg = "The type of arg 1 must be number";
805         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
806         return false;
807     }
808     if (!GetStringProperty(env, argv[PARAMONE], asyncContext->constraint)) {
809         ACCOUNT_LOGE("Get constraint failed");
810         std::string errMsg = "The type of arg 2 must be string";
811         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
812         return false;
813     }
814 
815     return true;
816 }
817 
QueryOAContSrcTypeExecuteCB(napi_env env,void * data)818 void QueryOAContSrcTypeExecuteCB(napi_env env, void *data)
819 {
820     QueryOAConstraintSrcTypeContext *asyncContext = reinterpret_cast<QueryOAConstraintSrcTypeContext *>(data);
821     asyncContext->errCode = OsAccountManager::QueryOsAccountConstraintSourceTypes(asyncContext->id,
822         asyncContext->constraint, asyncContext->constraintSourceTypeInfos);
823     ACCOUNT_LOGI("errocde is %{public}d", asyncContext->errCode);
824     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
825 }
826 
QueryOAContSrcTypeCallbackCompletedCB(napi_env env,napi_status status,void * data)827 void QueryOAContSrcTypeCallbackCompletedCB(napi_env env, napi_status status, void *data)
828 {
829     ACCOUNT_LOGI("napi_create_async_work complete");
830     QueryOAConstraintSrcTypeContext *asyncContext = reinterpret_cast<QueryOAConstraintSrcTypeContext *>(data);
831     napi_value errJs = nullptr;
832     napi_value dataJs = nullptr;
833     if (asyncContext->status == napi_ok) {
834         napi_get_null(env, &errJs);
835         QueryOAContSrcTypeForResult(env, asyncContext->constraintSourceTypeInfos, dataJs);
836     } else {
837         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
838         napi_get_null(env, &dataJs);
839     }
840     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
841     delete asyncContext;
842 }
843 
QueryOAContSrcTypeForResult(napi_env env,const std::vector<ConstraintSourceTypeInfo> & infos,napi_value & result)844 void QueryOAContSrcTypeForResult(napi_env env, const std::vector<ConstraintSourceTypeInfo> &infos, napi_value &result)
845 {
846     napi_create_array(env, &result);
847     uint32_t index = 0;
848 
849     for (auto item : infos) {
850         napi_value objTypeInfo = nullptr;
851         napi_create_object(env, &objTypeInfo);
852 
853         napi_value srcLocalId = nullptr;
854         napi_create_int32(env, item.localId, &srcLocalId);
855         napi_set_named_property(env, objTypeInfo, "localId", srcLocalId);
856 
857         napi_value valToJs = nullptr;
858         napi_create_int32(env, item.typeInfo, &valToJs);
859 
860         napi_set_named_property(env, objTypeInfo, "type", valToJs);
861         napi_set_element(env, result, index, objTypeInfo);
862         index++;
863     }
864 }
865 
ParseQueryActiveIds(napi_env env,napi_callback_info cbInfo,QueryActiveIdsAsyncContext * asyncContext)866 bool ParseQueryActiveIds(napi_env env, napi_callback_info cbInfo, QueryActiveIdsAsyncContext *asyncContext)
867 {
868     return ParseOneParaContext(env, cbInfo, asyncContext);
869 }
870 
QueryCreateOAExecuteCB(napi_env env,void * data)871 void QueryCreateOAExecuteCB(napi_env env, void *data)
872 {
873     QueryCreateOAAsyncContext *asyncContext = reinterpret_cast<QueryCreateOAAsyncContext *>(data);
874     asyncContext->errCode = OsAccountManager::QueryAllCreatedOsAccounts(asyncContext->osAccountInfos);
875     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
876     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
877 }
878 
QueryActiveIdsExecuteCB(napi_env env,void * data)879 void QueryActiveIdsExecuteCB(napi_env env, void *data)
880 {
881     QueryActiveIdsAsyncContext *asyncContext = reinterpret_cast<QueryActiveIdsAsyncContext *>(data);
882     asyncContext->errCode = OsAccountManager::QueryActiveOsAccountIds(asyncContext->osAccountIds);
883     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
884     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
885 }
886 
QueryCreateOACallbackCompletedCB(napi_env env,napi_status status,void * data)887 void QueryCreateOACallbackCompletedCB(napi_env env, napi_status status, void *data)
888 {
889     ACCOUNT_LOGD("napi_create_async_work complete");
890     QueryCreateOAAsyncContext *asyncContext = reinterpret_cast<QueryCreateOAAsyncContext *>(data);
891     napi_value errJs = nullptr;
892     napi_value dataJs = nullptr;
893     if (asyncContext->status == napi_ok) {
894         napi_get_null(env, &errJs);
895         QueryOAInfoForResult(env, asyncContext->osAccountInfos, dataJs);
896     } else {
897         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
898         napi_get_null(env, &dataJs);
899     }
900     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
901     delete asyncContext;
902 }
903 
QueryActiveIdsCallbackCompletedCB(napi_env env,napi_status status,void * data)904 void QueryActiveIdsCallbackCompletedCB(napi_env env, napi_status status, void *data)
905 {
906     ACCOUNT_LOGD("napi_create_async_work complete");
907     QueryActiveIdsAsyncContext *asyncContext = reinterpret_cast<QueryActiveIdsAsyncContext *>(data);
908     napi_value errJs = nullptr;
909     napi_value dataJs = nullptr;
910     if (asyncContext->status == napi_ok) {
911         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
912         GetActiveIds(env, asyncContext->osAccountIds, dataJs);
913     } else {
914         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
915         if (asyncContext->throwErr) {
916             napi_get_null(env, &dataJs);
917         } else {
918             GetActiveIds(env, asyncContext->osAccountIds, dataJs);
919         }
920     }
921     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
922     delete asyncContext;
923 }
924 
QueryOAInfoForResult(napi_env env,const std::vector<OsAccountInfo> & info,napi_value & result)925 void QueryOAInfoForResult(napi_env env, const std::vector<OsAccountInfo> &info, napi_value &result)
926 {
927     napi_create_array(env, &result);
928     uint32_t index = 0;
929 
930     for (auto item : info) {
931         napi_value objOAInfo = nullptr;
932         napi_create_object(env, &objOAInfo);
933         GetOACBInfoToJs(env, item, objOAInfo);
934         napi_set_element(env, result, index, objOAInfo);
935         index++;
936     }
937 }
938 
ParseParaGetPhoto(napi_env env,napi_callback_info cbInfo,GetOAPhotoAsyncContext * asyncContext)939 bool ParseParaGetPhoto(napi_env env, napi_callback_info cbInfo, GetOAPhotoAsyncContext *asyncContext)
940 {
941     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
942 }
943 
GetOAPhotoExecuteCB(napi_env env,void * data)944 void GetOAPhotoExecuteCB(napi_env env, void *data)
945 {
946     ACCOUNT_LOGD("napi_create_async_work running");
947     GetOAPhotoAsyncContext *asyncContext = reinterpret_cast<GetOAPhotoAsyncContext *>(data);
948     asyncContext->errCode = OsAccountManager::GetOsAccountProfilePhoto(asyncContext->id, asyncContext->photo);
949     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
950 }
951 
GetOAPhotoCallbackCompletedCB(napi_env env,napi_status status,void * data)952 void GetOAPhotoCallbackCompletedCB(napi_env env, napi_status status, void *data)
953 {
954     ACCOUNT_LOGD("napi_create_async_work complete");
955     GetOAPhotoAsyncContext *asyncContext = reinterpret_cast<GetOAPhotoAsyncContext *>(data);
956     napi_value errJs = nullptr;
957     napi_value dataJs = nullptr;
958     if (asyncContext->status == napi_ok) {
959         napi_get_null(env, &errJs);
960         napi_create_string_utf8(env, asyncContext->photo.c_str(), NAPI_AUTO_LENGTH, &dataJs);
961     } else {
962         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
963         napi_get_null(env, &dataJs);
964     }
965     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
966     delete asyncContext;
967 }
968 
ParseParaCurrentOA(napi_env env,napi_callback_info cbInfo,CurrentOAAsyncContext * asyncContext)969 bool ParseParaCurrentOA(napi_env env, napi_callback_info cbInfo, CurrentOAAsyncContext *asyncContext)
970 {
971     return ParseOneParaContext(env, cbInfo, asyncContext);
972 }
973 
QueryCurrentOAExecuteCB(napi_env env,void * data)974 void QueryCurrentOAExecuteCB(napi_env env, void *data)
975 {
976     CurrentOAAsyncContext *asyncContext = reinterpret_cast<CurrentOAAsyncContext *>(data);
977     asyncContext->errCode = OsAccountManager::QueryCurrentOsAccount(asyncContext->osAccountInfos);
978     // for compatibility
979     if ((!asyncContext->throwErr) && (asyncContext->errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED)) {
980         asyncContext->errCode = ERR_OSACCOUNT_KIT_QUERY_CURRENT_OS_ACCOUNT_ERROR;
981     }
982     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
983 }
984 
QueryCurrentOACallbackCompletedCB(napi_env env,napi_status status,void * data)985 void QueryCurrentOACallbackCompletedCB(napi_env env, napi_status status, void *data)
986 {
987     CurrentOAAsyncContext *asyncContext = reinterpret_cast<CurrentOAAsyncContext *>(data);
988     napi_value errJs = nullptr;
989     napi_value dataJs = nullptr;
990     if (asyncContext->status == napi_ok) {
991         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
992         GetOACBInfoToJs(env, asyncContext->osAccountInfos, dataJs);
993     } else {
994         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
995         if (asyncContext->throwErr) {
996             napi_get_null(env, &dataJs);
997         } else {
998             GetOACBInfoToJs(env, asyncContext->osAccountInfos, dataJs);
999         }
1000     }
1001     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1002     delete asyncContext;
1003 }
1004 
ParseParaGetIdByUid(napi_env env,napi_callback_info cbInfo,GetIdByUidAsyncContext * asyncContext)1005 bool ParseParaGetIdByUid(napi_env env, napi_callback_info cbInfo, GetIdByUidAsyncContext *asyncContext)
1006 {
1007     size_t argc = ARGS_SIZE_TWO;
1008     napi_value argv[ARGS_SIZE_TWO] = {0};
1009     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1010     if (argc == ARGS_SIZE_TWO) {
1011         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1012             ACCOUNT_LOGE("Get callbackRef failed");
1013             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1014             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1015             return false;
1016         }
1017     }
1018 
1019     if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->uid)) {
1020         ACCOUNT_LOGE("Get uid failed");
1021         std::string errMsg = "The type of arg 1 must be number";
1022         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1023         return false;
1024     }
1025     return true;
1026 }
1027 
ParseParaGetIdByDomain(napi_env env,napi_callback_info cbInfo,GetIdByDomainAsyncContext * asyncContext)1028 bool ParseParaGetIdByDomain(napi_env env, napi_callback_info cbInfo, GetIdByDomainAsyncContext *asyncContext)
1029 {
1030     size_t argc = ARGS_SIZE_TWO;
1031     napi_value argv[ARGS_SIZE_TWO] = {0};
1032     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1033     if (argc == ARGS_SIZE_TWO) {
1034         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1035             ACCOUNT_LOGE("Get callbackRef failed");
1036             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1037             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1038             return false;
1039         }
1040     }
1041 
1042     if (!ParseDomainAccountInfo(env, argv[PARAMZERO], asyncContext->domainInfo)) {
1043         ACCOUNT_LOGE("get domainInfo failed");
1044         std::string errMsg = "DomainInfo parse failed";
1045         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1046         return false;
1047     }
1048     return true;
1049 }
1050 
GetIdByUidExecuteCB(napi_env env,void * data)1051 void GetIdByUidExecuteCB(napi_env env, void *data)
1052 {
1053     GetIdByUidAsyncContext *asyncContext = reinterpret_cast<GetIdByUidAsyncContext *>(data);
1054     asyncContext->errCode = OsAccountManager::GetOsAccountLocalIdFromUid(asyncContext->uid, asyncContext->id);
1055     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1056     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1057 }
1058 
GetBundleIdByUidExecuteCB(napi_env env,void * data)1059 void GetBundleIdByUidExecuteCB(napi_env env, void *data)
1060 {
1061     GetIdByUidAsyncContext *asyncContext = reinterpret_cast<GetIdByUidAsyncContext *>(data);
1062     asyncContext->errCode = OsAccountManager::GetBundleIdFromUid(asyncContext->uid, asyncContext->id);
1063     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1064     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1065 }
1066 
GetIdByDomainExecuteCB(napi_env env,void * data)1067 void GetIdByDomainExecuteCB(napi_env env, void *data)
1068 {
1069     GetIdByDomainAsyncContext *asyncContext = reinterpret_cast<GetIdByDomainAsyncContext *>(data);
1070     asyncContext->errCode = OsAccountManager::GetOsAccountLocalIdFromDomain(
1071         asyncContext->domainInfo, asyncContext->id);
1072     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1073     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1074 }
1075 
GetIdByUidCallbackCompletedCB(napi_env env,napi_status status,void * data)1076 void GetIdByUidCallbackCompletedCB(napi_env env, napi_status status, void *data)
1077 {
1078     ACCOUNT_LOGD("napi_create_async_work complete");
1079     GetIdByUidAsyncContext *asyncContext = reinterpret_cast<GetIdByUidAsyncContext *>(data);
1080     napi_value errJs = nullptr;
1081     napi_value dataJs = nullptr;
1082     if (asyncContext->status == napi_ok) {
1083         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1084         napi_create_int32(env, asyncContext->id, &dataJs);
1085     } else {
1086         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1087         if (asyncContext->throwErr) {
1088             napi_get_null(env, &dataJs);
1089         } else {
1090             napi_create_int32(env, asyncContext->id, &dataJs);
1091         }
1092     }
1093     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1094     delete asyncContext;
1095 }
1096 
GetBundleIdByUidCallbackCompletedCB(napi_env env,napi_status status,void * data)1097 void GetBundleIdByUidCallbackCompletedCB(napi_env env, napi_status status, void *data)
1098 {
1099     ACCOUNT_LOGD("napi_create_async_work complete");
1100     GetIdByUidAsyncContext *asyncContext = reinterpret_cast<GetIdByUidAsyncContext *>(data);
1101     napi_value errJs = nullptr;
1102     napi_value dataJs = nullptr;
1103     if (asyncContext->status == napi_ok) {
1104         napi_get_null(env, &errJs);
1105         napi_create_int32(env, asyncContext->id, &dataJs);
1106     } else {
1107         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1108         napi_get_null(env, &dataJs);
1109     }
1110     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1111     delete asyncContext;
1112 }
1113 
GetIdByDomainCallbackCompletedCB(napi_env env,napi_status status,void * data)1114 void GetIdByDomainCallbackCompletedCB(napi_env env, napi_status status, void *data)
1115 {
1116     ACCOUNT_LOGD("napi_create_async_work complete");
1117     GetIdByDomainAsyncContext *asyncContext = reinterpret_cast<GetIdByDomainAsyncContext *>(data);
1118     napi_value errJs = nullptr;
1119     napi_value dataJs = nullptr;
1120     if (asyncContext->status == napi_ok) {
1121         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1122         napi_create_int32(env, asyncContext->id, &dataJs);
1123     } else {
1124         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1125         if (asyncContext->throwErr) {
1126             napi_get_null(env, &dataJs);
1127         } else {
1128             napi_create_int32(env, asyncContext->id, &dataJs);
1129         }
1130     }
1131     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1132     delete asyncContext;
1133 }
1134 
ParseParaSetPhoto(napi_env env,napi_callback_info cbInfo,SetOAPhotoAsyncContext * asyncContext)1135 bool ParseParaSetPhoto(napi_env env, napi_callback_info cbInfo, SetOAPhotoAsyncContext *asyncContext)
1136 {
1137     size_t argc = ARGS_SIZE_THREE;
1138     napi_value argv[ARGS_SIZE_THREE] = {0};
1139     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1140     if (argc == ARGS_SIZE_THREE) {
1141         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1142             ACCOUNT_LOGE("Get callbackRef failed");
1143             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1144             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1145             return false;
1146         }
1147     }
1148 
1149     if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->id)) {
1150         ACCOUNT_LOGE("Get id failed");
1151         std::string errMsg = "The type of arg 1 must be number";
1152         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1153         return false;
1154     }
1155     if (!GetStringProperty(env, argv[PARAMONE], asyncContext->photo)) {
1156         ACCOUNT_LOGE("Get photo failed");
1157         std::string errMsg = "The type of arg 2 must be string";
1158         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1159         return false;
1160     }
1161 
1162     return true;
1163 }
1164 
SetPhotoExecuteCB(napi_env env,void * data)1165 void SetPhotoExecuteCB(napi_env env, void *data)
1166 {
1167     SetOAPhotoAsyncContext *asyncContext = reinterpret_cast<SetOAPhotoAsyncContext *>(data);
1168     asyncContext->errCode = OsAccountManager::SetOsAccountProfilePhoto(asyncContext->id, asyncContext->photo);
1169     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1170     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1171 }
1172 
SetPhotoCompletedCB(napi_env env,napi_status status,void * data)1173 void SetPhotoCompletedCB(napi_env env, napi_status status, void *data)
1174 {
1175     ACCOUNT_LOGD("napi_create_async_work complete");
1176     SetOAPhotoAsyncContext *asyncContext = reinterpret_cast<SetOAPhotoAsyncContext *>(data);
1177     napi_value errJs = nullptr;
1178     napi_value dataJs = nullptr;
1179     if (asyncContext->status == napi_ok) {
1180         napi_get_null(env, &errJs);
1181         napi_get_null(env, &dataJs);
1182     } else {
1183         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1184         napi_get_null(env, &dataJs);
1185     }
1186     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1187     delete asyncContext;
1188 }
1189 
ParseParaQueryMaxNum(napi_env env,napi_callback_info cbInfo,QueryMaxNumAsyncContext * asyncContext)1190 bool ParseParaQueryMaxNum(napi_env env, napi_callback_info cbInfo, QueryMaxNumAsyncContext *asyncContext)
1191 {
1192     return ParseOneParaContext(env, cbInfo, asyncContext);
1193 }
1194 
QueryMaxNumExecuteCB(napi_env env,void * data)1195 void QueryMaxNumExecuteCB(napi_env env, void *data)
1196 {
1197     QueryMaxNumAsyncContext *asyncContext = reinterpret_cast<QueryMaxNumAsyncContext *>(data);
1198     asyncContext->errCode = OsAccountManager::QueryMaxOsAccountNumber(asyncContext->maxOsAccountNumber);
1199     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1200     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1201 }
1202 
QueryMaxNumCompletedCB(napi_env env,napi_status status,void * data)1203 void QueryMaxNumCompletedCB(napi_env env, napi_status status, void *data)
1204 {
1205     ACCOUNT_LOGD("napi_create_async_work complete");
1206     QueryMaxNumAsyncContext *asyncContext = reinterpret_cast<QueryMaxNumAsyncContext *>(data);
1207     napi_value errJs = nullptr;
1208     napi_value dataJs = nullptr;
1209     if (asyncContext->status == napi_ok) {
1210         napi_get_null(env, &errJs);
1211         napi_create_int32(env, asyncContext->maxOsAccountNumber, &dataJs);
1212     } else {
1213         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1214         napi_get_null(env, &dataJs);
1215     }
1216     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1217     delete asyncContext;
1218 }
1219 
ParseParaIsActived(napi_env env,napi_callback_info cbInfo,IsActivedAsyncContext * asyncContext)1220 bool ParseParaIsActived(napi_env env, napi_callback_info cbInfo, IsActivedAsyncContext *asyncContext)
1221 {
1222     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
1223 }
1224 
IsActivedExecuteCB(napi_env env,void * data)1225 void IsActivedExecuteCB(napi_env env, void *data)
1226 {
1227     IsActivedAsyncContext *asyncContext = reinterpret_cast<IsActivedAsyncContext *>(data);
1228     asyncContext->errCode = OsAccountManager::IsOsAccountActived(asyncContext->id, asyncContext->isOsAccountActived);
1229     // for compatibility
1230     if ((!asyncContext->throwErr) && (asyncContext->errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED)) {
1231         asyncContext->errCode = ERR_OSACCOUNT_KIT_IS_OS_ACCOUNT_ACTIVED_ERROR;
1232     }
1233     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1234 }
1235 
IsActivedCompletedCB(napi_env env,napi_status status,void * data)1236 void IsActivedCompletedCB(napi_env env, napi_status status, void *data)
1237 {
1238     ACCOUNT_LOGD("napi_create_async_work complete");
1239     IsActivedAsyncContext *asyncContext = reinterpret_cast<IsActivedAsyncContext *>(data);
1240     napi_value errJs = nullptr;
1241     napi_value dataJs = nullptr;
1242     if (asyncContext->status == napi_ok) {
1243         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1244         napi_get_boolean(env, asyncContext->isOsAccountActived, &dataJs);
1245     } else {
1246         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1247         if (asyncContext->throwErr) {
1248             napi_get_null(env, &dataJs);
1249         } else {
1250             napi_get_boolean(env, asyncContext->isOsAccountActived, &dataJs);
1251         }
1252     }
1253     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1254     delete asyncContext;
1255 }
1256 
ParseParaIsEnable(napi_env env,napi_callback_info cbInfo,IsConEnableAsyncContext * asyncContext)1257 bool ParseParaIsEnable(napi_env env, napi_callback_info cbInfo, IsConEnableAsyncContext *asyncContext)
1258 {
1259     size_t argc = ARGS_SIZE_THREE;
1260     napi_value argv[ARGS_SIZE_THREE] = {0};
1261     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1262     if (argc == ARGS_SIZE_THREE) {
1263         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1264             ACCOUNT_LOGE("Get callbackRef failed");
1265             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1266             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1267             return false;
1268         }
1269     }
1270 
1271     if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->id)) {
1272         ACCOUNT_LOGE("Get id failed");
1273         std::string errMsg = "The type of arg 1 must be number";
1274         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1275         return false;
1276     }
1277     if (!GetStringProperty(env, argv[PARAMONE], asyncContext->constraint)) {
1278         ACCOUNT_LOGE("Get constraint failed");
1279         std::string errMsg = "The type of arg 2 must be string";
1280         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1281         return false;
1282     }
1283     return true;
1284 }
1285 
IsEnableExecuteCB(napi_env env,void * data)1286 void IsEnableExecuteCB(napi_env env, void *data)
1287 {
1288     IsConEnableAsyncContext *asyncContext = reinterpret_cast<IsConEnableAsyncContext *>(data);
1289     if (asyncContext->throwErr) {
1290         asyncContext->errCode = OsAccountManager::CheckOsAccountConstraintEnabled(asyncContext->id,
1291             asyncContext->constraint, asyncContext->isConsEnable);
1292     } else {
1293         asyncContext->errCode = OsAccountManager::IsOsAccountConstraintEnable(asyncContext->id,
1294             asyncContext->constraint, asyncContext->isConsEnable);
1295     }
1296 
1297     // for compatibility
1298     if ((!asyncContext->throwErr) && (asyncContext->errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED)) {
1299         asyncContext->errCode = ERR_OSACCOUNT_KIT_IS_OS_ACCOUNT_CONSTRAINT_ENABLE_ERROR;
1300     }
1301     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1302 }
1303 
IsEnableCompletedCB(napi_env env,napi_status status,void * data)1304 void IsEnableCompletedCB(napi_env env, napi_status status, void *data)
1305 {
1306     ACCOUNT_LOGD("napi_create_async_work complete");
1307     IsConEnableAsyncContext *asyncContext = reinterpret_cast<IsConEnableAsyncContext *>(data);
1308     napi_value errJs = nullptr;
1309     napi_value dataJs = nullptr;
1310     if (asyncContext->status == napi_ok) {
1311         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1312         napi_get_boolean(env, asyncContext->isConsEnable, &dataJs);
1313     } else {
1314         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1315         if (asyncContext->throwErr) {
1316             napi_get_null(env, &dataJs);
1317         } else {
1318             napi_get_boolean(env, asyncContext->isConsEnable, &dataJs);
1319         }
1320     }
1321     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1322     delete asyncContext;
1323 }
1324 
ParseParaGetType(napi_env env,napi_callback_info cbInfo,GetTypeAsyncContext * asyncContext)1325 bool ParseParaGetType(napi_env env, napi_callback_info cbInfo, GetTypeAsyncContext *asyncContext)
1326 {
1327     return ParseOneParaContext(env, cbInfo, asyncContext);
1328 }
1329 
GetTypeExecuteCB(napi_env env,void * data)1330 void GetTypeExecuteCB(napi_env env, void *data)
1331 {
1332     GetTypeAsyncContext *asyncContext = reinterpret_cast<GetTypeAsyncContext *>(data);
1333     asyncContext->errCode = OsAccountManager::GetOsAccountTypeFromProcess(asyncContext->type);
1334     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1335     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1336 }
1337 
GetTypeCompletedCB(napi_env env,napi_status status,void * data)1338 void GetTypeCompletedCB(napi_env env, napi_status status, void *data)
1339 {
1340     GetTypeAsyncContext *asyncContext = reinterpret_cast<GetTypeAsyncContext *>(data);
1341     napi_value errJs = nullptr;
1342     napi_value dataJs = nullptr;
1343     if (asyncContext->status == napi_ok) {
1344         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1345         int cType = static_cast<int>(asyncContext->type);
1346         napi_create_int32(env, cType, &dataJs);
1347     } else {
1348         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1349         napi_get_null(env, &dataJs);
1350     }
1351     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1352     delete asyncContext;
1353 }
1354 
ParseParaIsMultiEn(napi_env env,napi_callback_info cbInfo,IsMultiEnAsyncContext * asyncContext)1355 bool ParseParaIsMultiEn(napi_env env, napi_callback_info cbInfo, IsMultiEnAsyncContext *asyncContext)
1356 {
1357     return ParseOneParaContext(env, cbInfo, asyncContext);
1358 }
1359 
IsMultiEnExecuteCB(napi_env env,void * data)1360 void IsMultiEnExecuteCB(napi_env env, void *data)
1361 {
1362     IsMultiEnAsyncContext *asyncContext = reinterpret_cast<IsMultiEnAsyncContext *>(data);
1363     asyncContext->errCode = OsAccountManager::IsMultiOsAccountEnable(asyncContext->isMultiOAEnable);
1364     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1365     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1366 }
1367 
IsMultiEnCompletedCB(napi_env env,napi_status status,void * data)1368 void IsMultiEnCompletedCB(napi_env env, napi_status status, void *data)
1369 {
1370     ACCOUNT_LOGD("napi_create_async_work complete");
1371     IsMultiEnAsyncContext *asyncContext = reinterpret_cast<IsMultiEnAsyncContext *>(data);
1372     napi_value errJs = nullptr;
1373     napi_value dataJs = nullptr;
1374     if (asyncContext->status == napi_ok) {
1375         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1376         napi_get_boolean(env, asyncContext->isMultiOAEnable, &dataJs);
1377     } else {
1378         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1379         if (asyncContext->throwErr) {
1380             napi_get_null(env, &dataJs);
1381         } else {
1382             napi_get_boolean(env, asyncContext->isMultiOAEnable, &dataJs);
1383         }
1384     }
1385     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1386     delete asyncContext;
1387 }
1388 
ParseParaIsVerifiedWithOneParam(napi_env env,napi_value value,IsVerifiedAsyncContext * asyncContext)1389 static bool ParseParaIsVerifiedWithOneParam(
1390     napi_env env, napi_value value, IsVerifiedAsyncContext *asyncContext)
1391 {
1392     napi_valuetype valueType = napi_undefined;
1393     napi_typeof(env, value, &valueType);
1394     if (valueType == napi_number) {
1395         if (!GetIntProperty(env, value, asyncContext->id)) {
1396             ACCOUNT_LOGE("Get id failed");
1397             std::string errMsg = "The type of arg 1 must be number";
1398             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1399             return false;
1400         }
1401     } else if (valueType == napi_function) {
1402         if (!GetCallbackProperty(env, value, asyncContext->callbackRef, 1)) {
1403             ACCOUNT_LOGE("Get callbackRef failed");
1404             std::string errMsg = "The type of arg 1 must be function";
1405             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1406             return false;
1407         }
1408     } else if ((valueType == napi_undefined) || (valueType == napi_null)) {
1409         ACCOUNT_LOGI("id is undefined or null");
1410     } else {
1411         ACCOUNT_LOGE("Wrong arg type, expected number or function");
1412         std::string errMsg = "The type of arg 1 must be number or function";
1413         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1414         return false;
1415     }
1416     return true;
1417 }
1418 
ParseParaIsVerified(napi_env env,napi_callback_info cbInfo,IsVerifiedAsyncContext * asyncContext)1419 bool ParseParaIsVerified(napi_env env, napi_callback_info cbInfo, IsVerifiedAsyncContext *asyncContext)
1420 {
1421     size_t argc = ARGS_SIZE_TWO;
1422     napi_value argv[ARGS_SIZE_TWO] = {0};
1423     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1424 
1425     if (argc == 0) {
1426         return true;
1427     }
1428     if (argc == ARGS_SIZE_ONE) {
1429         return ParseParaIsVerifiedWithOneParam(env, argv[PARAMZERO], asyncContext);
1430     }
1431     if (argc == ARGS_SIZE_TWO) {
1432         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1433             ACCOUNT_LOGE("Get callbackRef failed");
1434             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1435             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1436             return false;
1437         }
1438         napi_valuetype valueType = napi_undefined;
1439         napi_typeof(env, argv[PARAMZERO], &valueType);
1440         if ((valueType == napi_undefined) || (valueType == napi_null)) {
1441             ACCOUNT_LOGI("id is undefined or null");
1442         } else {
1443             if (!GetIntProperty(env, argv[PARAMZERO], asyncContext->id)) {
1444                 ACCOUNT_LOGE("Get id failed");
1445                 std::string errMsg = "The type of arg 1 must be number";
1446                 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1447                 return false;
1448             }
1449         }
1450     }
1451     return true;
1452 }
1453 
IsVerifiedExecuteCB(napi_env env,void * data)1454 void IsVerifiedExecuteCB(napi_env env, void *data)
1455 {
1456     IsVerifiedAsyncContext *asyncContext = reinterpret_cast<IsVerifiedAsyncContext *>(data);
1457     if (asyncContext->id < 0) {
1458         asyncContext->errCode = OsAccountManager::IsCurrentOsAccountVerified(asyncContext->isTestOA);
1459     } else {
1460         asyncContext->errCode = OsAccountManager::IsOsAccountVerified(asyncContext->id, asyncContext->isTestOA);
1461     }
1462     // for compatibility
1463     if ((!asyncContext->throwErr) && (asyncContext->errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED)) {
1464         asyncContext->errCode = ERR_OSACCOUNT_KIT_IS_OS_ACCOUNT_VERIFIED_ERROR;
1465     }
1466     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1467 }
1468 
IsVerifiedCompletedCB(napi_env env,napi_status status,void * data)1469 void IsVerifiedCompletedCB(napi_env env, napi_status status, void *data)
1470 {
1471     ACCOUNT_LOGD("napi_create_async_work complete");
1472     IsVerifiedAsyncContext *asyncContext = reinterpret_cast<IsVerifiedAsyncContext *>(data);
1473     napi_value errJs = nullptr;
1474     napi_value dataJs = nullptr;
1475     if (asyncContext->status == napi_ok) {
1476         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1477         napi_get_boolean(env, asyncContext->isTestOA, &dataJs);
1478     } else {
1479         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1480         if (asyncContext->throwErr) {
1481             napi_get_null(env, &dataJs);
1482         } else {
1483             napi_get_boolean(env, asyncContext->isTestOA, &dataJs);
1484         }
1485     }
1486     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1487     delete asyncContext;
1488 }
1489 
ParseParaSerialNumId(napi_env env,napi_callback_info cbInfo,GetSerialNumIdCBInfo * asyncContext)1490 bool ParseParaSerialNumId(napi_env env, napi_callback_info cbInfo, GetSerialNumIdCBInfo *asyncContext)
1491 {
1492     size_t argc = ARGS_SIZE_TWO;
1493     napi_value argv[ARGS_SIZE_TWO] = {0};
1494     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
1495     if (argc == ARGS_SIZE_TWO) {
1496         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1497             ACCOUNT_LOGE("Get callbackRef failed");
1498             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1499             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1500             return false;
1501         }
1502     }
1503     if (!GetLongIntProperty(env, argv[PARAMZERO], asyncContext->serialNumber)) {
1504         ACCOUNT_LOGE("Get serialNumber failed");
1505         std::string errMsg = "The type of arg 1 must be number";
1506         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1507         return false;
1508     }
1509 
1510     return true;
1511 }
1512 
SerialNumIdExecuteCB(napi_env env,void * data)1513 void SerialNumIdExecuteCB(napi_env env, void *data)
1514 {
1515     GetSerialNumIdCBInfo *asyncContext = reinterpret_cast<GetSerialNumIdCBInfo *>(data);
1516     asyncContext->errCode =
1517         OsAccountManager::GetOsAccountLocalIdBySerialNumber(asyncContext->serialNumber, asyncContext->id);
1518     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1519 }
1520 
SerialNumIdCompletedCB(napi_env env,napi_status status,void * data)1521 void SerialNumIdCompletedCB(napi_env env, napi_status status, void *data)
1522 {
1523     GetSerialNumIdCBInfo *asyncContext = reinterpret_cast<GetSerialNumIdCBInfo *>(data);
1524     napi_value errJs = nullptr;
1525     napi_value dataJs = nullptr;
1526     if (asyncContext->status == napi_ok) {
1527         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1528         napi_create_int32(env, asyncContext->id, &dataJs);
1529     } else {
1530         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1531         if (asyncContext->throwErr) {
1532             napi_get_null(env, &dataJs);
1533         } else {
1534             napi_create_int32(env, asyncContext->id, &dataJs);
1535         }
1536     }
1537     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1538     delete asyncContext;
1539 }
1540 
ParseParaGetSerialNum(napi_env env,napi_callback_info cbInfo,GetSerialNumForOAInfo * asyncContext)1541 bool ParseParaGetSerialNum(napi_env env, napi_callback_info cbInfo, GetSerialNumForOAInfo *asyncContext)
1542 {
1543     return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr);
1544 }
1545 
GetSerialNumExecuteCB(napi_env env,void * data)1546 void GetSerialNumExecuteCB(napi_env env, void *data)
1547 {
1548     GetSerialNumForOAInfo *asyncContext = reinterpret_cast<GetSerialNumForOAInfo *>(data);
1549     asyncContext->errCode =
1550         OsAccountManager::GetSerialNumberByOsAccountLocalId(asyncContext->id, asyncContext->serialNum);
1551     ACCOUNT_LOGD("error code is %{public}d", asyncContext->errCode);
1552     asyncContext->status = (asyncContext->errCode == 0) ? napi_ok : napi_generic_failure;
1553 }
1554 
GetSerialNumCompletedCB(napi_env env,napi_status status,void * data)1555 void GetSerialNumCompletedCB(napi_env env, napi_status status, void *data)
1556 {
1557     ACCOUNT_LOGD("napi_create_async_work complete");
1558     GetSerialNumForOAInfo *asyncContext = reinterpret_cast<GetSerialNumForOAInfo *>(data);
1559     napi_value errJs = nullptr;
1560     napi_value dataJs = nullptr;
1561     if (asyncContext->status == napi_ok) {
1562         errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
1563         napi_create_int64(env, asyncContext->serialNum, &dataJs);
1564     } else {
1565         errJs = GenerateBusinessError(env, asyncContext->errCode, asyncContext->throwErr);
1566         if (asyncContext->throwErr) {
1567             napi_get_null(env, &dataJs);
1568         } else {
1569             napi_create_int64(env, asyncContext->serialNum, &dataJs);
1570         }
1571     }
1572     ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs);
1573     delete asyncContext;
1574 }
1575 
ParseParaIsTestOA(napi_env env,napi_callback_info cbInfo,IsTestOAInfo * asyncContext)1576 bool ParseParaIsTestOA(napi_env env, napi_callback_info cbInfo, IsTestOAInfo *asyncContext)
1577 {
1578     return ParseOneParaContext(env, cbInfo, asyncContext);
1579 }
1580 
ParseParaIsMainOA(napi_env env,napi_callback_info cbInfo,IsMainOAInfo * asyncContext)1581 bool ParseParaIsMainOA(napi_env env, napi_callback_info cbInfo, IsMainOAInfo *asyncContext)
1582 {
1583     return ParseOneParaContext(env, cbInfo, asyncContext);
1584 }
1585 
ParseParaToSubscriber(const napi_env & env,napi_callback_info cbInfo,SubscribeCBInfo * asyncContext,napi_value * thisVar)1586 bool ParseParaToSubscriber(const napi_env &env, napi_callback_info cbInfo, SubscribeCBInfo *asyncContext,
1587     napi_value *thisVar)
1588 {
1589     size_t argc = ARGS_SIZE_THREE;
1590     napi_value argv[ARGS_SIZE_THREE] = {nullptr};
1591     napi_get_cb_info(env, cbInfo, &argc, argv, thisVar, NULL);
1592     if (argc < ARGS_SIZE_THREE) {
1593         ACCOUNT_LOGE("The arg number less than 3 characters");
1594         std::string errMsg = "The arg number must be at least 3 characters";
1595         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1596         return false;
1597     }
1598     if (argc == ARGS_SIZE_THREE) {
1599         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1600             ACCOUNT_LOGE("Get callbackRef failed");
1601             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1602             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1603             return false;
1604         }
1605     }
1606 
1607     // argv[0] type: 'activate' | 'activating'
1608     std::string type;
1609     if (!GetStringProperty(env, argv[PARAMZERO], type)) {
1610         ACCOUNT_LOGE("Get type failed");
1611         std::string errMsg = "The type of arg 1 must be string";
1612         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1613         return false;
1614     }
1615     if (type == "activate") {
1616         asyncContext->osSubscribeType = OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED;
1617     } else if (type == "activating") {
1618         asyncContext->osSubscribeType = OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVATING;
1619     } else {
1620         ACCOUNT_LOGE("Get type failed, type is invalid");
1621         std::string errMsg = "The value of arg 1 must be 'activate' or 'activating'";
1622         AccountNapiThrow(env, ERR_JS_INVALID_PARAMETER, errMsg, asyncContext->throwErr);
1623         return false;
1624     }
1625 
1626     // argv[1] name: string
1627     if (!GetStringProperty(env, argv[PARAMONE], asyncContext->name)) {
1628         ACCOUNT_LOGE("Get name failed");
1629         std::string errMsg = "The type of arg 2 must be string";
1630         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1631         return false;
1632     }
1633     size_t nameSize = asyncContext->name.size();
1634     if (nameSize == 0 || nameSize > MAX_SUBSCRIBER_NAME_LEN) {
1635         ACCOUNT_LOGE("Subscriber name size %{public}zu is invalid.", nameSize);
1636         std::string errMsg = "The length of arg 2 is invalid";
1637         AccountNapiThrow(env, ERR_JS_INVALID_PARAMETER, errMsg, asyncContext->throwErr);
1638         return false;
1639     }
1640 
1641     return true;
1642 }
1643 
ParseParaToUnsubscriber(const napi_env & env,napi_callback_info cbInfo,UnsubscribeCBInfo * asyncContext,napi_value * thisVar)1644 bool ParseParaToUnsubscriber(const napi_env &env, napi_callback_info cbInfo, UnsubscribeCBInfo *asyncContext,
1645     napi_value *thisVar)
1646 {
1647     size_t argc = ARGS_SIZE_THREE;
1648     napi_value argv[ARGS_SIZE_THREE] = {nullptr};
1649     napi_get_cb_info(env, cbInfo, &argc, argv, thisVar, NULL);
1650     if (argc < ARGS_SIZE_TWO) {
1651         ACCOUNT_LOGE("The arg number less than 2 characters");
1652         std::string errMsg = "The arg number must be at least 2 characters";
1653         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1654         return false;
1655     }
1656     if (argc == ARGS_SIZE_THREE) {
1657         if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) {
1658             ACCOUNT_LOGE("Get callbackRef failed");
1659             std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function";
1660             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1661             return false;
1662         }
1663     }
1664 
1665     // argv[0] type: 'activate' | 'activating'
1666     std::string type;
1667     if (!GetStringProperty(env, argv[PARAMZERO], type)) {
1668         std::string errMsg = "The type of arg 1 must be string";
1669         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1670         return false;
1671     }
1672     if (type == "activate") {
1673         asyncContext->osSubscribeType = OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED;
1674     } else if (type == "activating") {
1675         asyncContext->osSubscribeType = OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVATING;
1676     } else {
1677         ACCOUNT_LOGE("Get type fail, type is invalid");
1678         std::string errMsg = "The value of arg 1 must be 'activate' or 'activating'";
1679         AccountNapiThrow(env, ERR_JS_INVALID_PARAMETER, errMsg, asyncContext->throwErr);
1680         return false;
1681     }
1682 
1683     // argv[1] name: string
1684     if (!GetStringProperty(env, argv[PARAMONE], asyncContext->name)) {
1685         ACCOUNT_LOGE("Get name failed");
1686         std::string errMsg = "The type of arg 2 must be string";
1687         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
1688         return false;
1689     }
1690     size_t nameSize = asyncContext->name.size();
1691     if (nameSize == 0 || nameSize > MAX_SUBSCRIBER_NAME_LEN) {
1692         ACCOUNT_LOGE("Subscriber name size %{public}zu is invalid.", nameSize);
1693         std::string errMsg = "The length of arg 2 is invalid";
1694         AccountNapiThrow(env, ERR_JS_INVALID_PARAMETER, errMsg, asyncContext->throwErr);
1695         return false;
1696     }
1697 
1698     return true;
1699 }
1700 }  // namespace AccountJsKit
1701 }  // namespace OHOS
1702