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