• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_os_account.h"
17 #include "napi_account_error.h"
18 #include "napi_os_account_common.h"
19 #include "napi/native_common.h"
20 
21 using namespace OHOS::AccountSA;
22 
23 namespace OHOS {
24 namespace AccountJsKit {
25 namespace {
26 const std::string OS_ACCOUNT_CLASS_NAME = "AccountManager";
27 static thread_local napi_ref osAccountRef_ = nullptr;
28 
29 const int OS_ACCOUNT_TYPE_ADMIN = 0;
30 const int OS_ACCOUNT_TYPE_NORMAL = 1;
31 const int OS_ACCOUNT_TYPE_GUEST = 2;
32 std::mutex g_lockForOsAccountSubscribers;
33 std::map<OsAccountManager *, std::vector<SubscribeCBInfo *>> g_osAccountSubscribers;
34 static napi_property_descriptor g_osAccountProperties[] = {
35     DECLARE_NAPI_FUNCTION("queryOsAccountById", QueryOsAccountById),
36     DECLARE_NAPI_FUNCTION("removeOsAccount", RemoveOsAccount),
37     DECLARE_NAPI_FUNCTION("setOsAccountName", SetOsAccountName),
38     DECLARE_NAPI_FUNCTION("setOsAccountConstraints", SetOsAccountConstraints),
39     DECLARE_NAPI_FUNCTION("activateOsAccount", ActivateOsAccount),
40     DECLARE_NAPI_FUNCTION("createOsAccount", CreateOsAccount),
41     DECLARE_NAPI_FUNCTION("createOsAccountForDomain", CreateOsAccountForDomain),
42     DECLARE_NAPI_FUNCTION("getCreatedOsAccountsCount", GetCreatedOsAccountsCount),
43     DECLARE_NAPI_FUNCTION("getOsAccountCount", GetOsAccountCount),
44     DECLARE_NAPI_FUNCTION("getDistributedVirtualDeviceId", GetDistributedVirtualDeviceId),
45     DECLARE_NAPI_FUNCTION("queryDistributedVirtualDeviceId", QueryDistributedVirtualDeviceId),
46     DECLARE_NAPI_FUNCTION("getOsAccountAllConstraints", GetOsAccountAllConstraints),
47     DECLARE_NAPI_FUNCTION("getOsAccountConstraints", GetOsAccountConstraints),
48     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdFromProcess", GetOsAccountLocalIdFromProcess),
49     DECLARE_NAPI_FUNCTION("queryOsAccountLocalIdFromProcess", QueryOsAccountLocalIdFromProcess),
50     DECLARE_NAPI_FUNCTION("getOsAccountLocalId", QueryOsAccountLocalIdFromProcess),
51     DECLARE_NAPI_FUNCTION("queryAllCreatedOsAccounts", QueryAllCreatedOsAccounts),
52     DECLARE_NAPI_FUNCTION("queryOsAccountConstraintSourceTypes", QueryOsAccountConstraintSourceTypes),
53     DECLARE_NAPI_FUNCTION("getOsAccountConstraintSourceTypes", QueryOsAccountConstraintSourceTypes),
54     DECLARE_NAPI_FUNCTION("queryActivatedOsAccountIds", QueryActivatedOsAccountIds),
55     DECLARE_NAPI_FUNCTION("getActivatedOsAccountIds", GetActivatedOsAccountIds),
56     DECLARE_NAPI_FUNCTION("getActivatedOsAccountLocalIds", GetActivatedOsAccountIds),
57     DECLARE_NAPI_FUNCTION("getOsAccountProfilePhoto", GetOsAccountProfilePhoto),
58     DECLARE_NAPI_FUNCTION("queryCurrentOsAccount", QueryCurrentOsAccount),
59     DECLARE_NAPI_FUNCTION("getCurrentOsAccount", GetCurrentOsAccount),
60     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdFromUid", GetOsAccountLocalIdFromUid),
61     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdForUid", GetOsAccountLocalIdForUid),
62     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdForUidSync", GetOsAccountLocalIdForUidSync),
63     DECLARE_NAPI_FUNCTION("getBundleIdFromUid", GetBundleIdFromUid),
64     DECLARE_NAPI_FUNCTION("getBundleIdForUid", GetBundleIdFromUid),
65     DECLARE_NAPI_FUNCTION("getBundleIdForUidSync", GetBundleIdForUidSync),
66     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdFromDomain", GetOsAccountLocalIdFromDomain),
67     DECLARE_NAPI_FUNCTION("queryOsAccountLocalIdFromDomain", QueryOsAccountLocalIdFromDomain),
68     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdForDomain", QueryOsAccountLocalIdFromDomain),
69     DECLARE_NAPI_FUNCTION("setOsAccountProfilePhoto", SetOsAccountProfilePhoto),
70     DECLARE_NAPI_FUNCTION("queryMaxOsAccountNumber", QueryMaxOsAccountNumber),
71     DECLARE_NAPI_FUNCTION("isOsAccountActived", IsOsAccountActived),
72     DECLARE_NAPI_FUNCTION("checkOsAccountActivated", CheckOsAccountActivated),
73     DECLARE_NAPI_FUNCTION("isOsAccountConstraintEnable", IsOsAccountConstraintEnable),
74     DECLARE_NAPI_FUNCTION("checkConstraintEnabled", CheckConstraintEnabled),
75     DECLARE_NAPI_FUNCTION("checkOsAccountConstraintEnabled", CheckConstraintEnabled),
76     DECLARE_NAPI_FUNCTION("getOsAccountTypeFromProcess", GetOsAccountTypeFromProcess),
77     DECLARE_NAPI_FUNCTION("getOsAccountType", GetOsAccountType),
78     DECLARE_NAPI_FUNCTION("isMultiOsAccountEnable", IsMultiOsAccountEnable),
79     DECLARE_NAPI_FUNCTION("checkMultiOsAccountEnabled", CheckMultiOsAccountEnabled),
80     DECLARE_NAPI_FUNCTION("isOsAccountVerified", IsOsAccountVerified),
81     DECLARE_NAPI_FUNCTION("checkOsAccountVerified", CheckOsAccountVerified),
82     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdBySerialNumber", GetOsAccountLocalIdBySerialNumber),
83     DECLARE_NAPI_FUNCTION("queryOsAccountLocalIdBySerialNumber", QueryOsAccountLocalIdBySerialNumber),
84     DECLARE_NAPI_FUNCTION("getOsAccountLocalIdForSerialNumber", QueryOsAccountLocalIdBySerialNumber),
85     DECLARE_NAPI_FUNCTION("getSerialNumberByOsAccountLocalId", GetSerialNumberByOsAccountLocalId),
86     DECLARE_NAPI_FUNCTION("querySerialNumberByOsAccountLocalId", QuerySerialNumberByOsAccountLocalId),
87     DECLARE_NAPI_FUNCTION("getSerialNumberForOsAccountLocalId", QuerySerialNumberByOsAccountLocalId),
88     DECLARE_NAPI_FUNCTION("isTestOsAccount", IsTestOsAccount),
89     DECLARE_NAPI_FUNCTION("checkOsAccountTestable", CheckOsAccountTestable),
90     DECLARE_NAPI_FUNCTION("isMainOsAccount", IsMainOsAccount),
91     DECLARE_NAPI_FUNCTION("on", Subscribe),
92     DECLARE_NAPI_FUNCTION("off", Unsubscribe),
93 };
94 }  // namespace
OsAccountInit(napi_env env,napi_value exports)95 napi_value OsAccountInit(napi_env env, napi_value exports)
96 {
97     napi_property_descriptor descriptor[] = {
98         DECLARE_NAPI_FUNCTION("getAccountManager", GetAccountManager),
99     };
100     NAPI_CALL(
101         env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
102 
103     napi_value osAccountType = nullptr;
104     napi_create_object(env, &osAccountType);
105 
106     SetEnumProperty(env, osAccountType, OS_ACCOUNT_TYPE_ADMIN, "ADMIN");
107     SetEnumProperty(env, osAccountType, OS_ACCOUNT_TYPE_NORMAL, "NORMAL");
108     SetEnumProperty(env, osAccountType, OS_ACCOUNT_TYPE_GUEST, "GUEST");
109 
110     napi_value constraintSourceType = nullptr;
111     napi_create_object(env, &constraintSourceType);
112     SetEnumProperty(env, constraintSourceType, CONSTRAINT_NOT_EXIST, "CONSTRAINT_NOT_EXIST");
113     SetEnumProperty(env, constraintSourceType, CONSTRAINT_TYPE_BASE, "CONSTRAINT_TYPE_BASE");
114     SetEnumProperty(env, constraintSourceType, CONSTRAINT_TYPE_DEVICE_OWNER, "CONSTRAINT_TYPE_DEVICE_OWNER");
115     SetEnumProperty(env, constraintSourceType, CONSTRAINT_TYPE_PROFILE_OWNER, "CONSTRAINT_TYPE_PROFILE_OWNER");
116 
117     napi_property_descriptor exportEnum[] = {
118         DECLARE_NAPI_PROPERTY("OsAccountType", osAccountType),
119         DECLARE_NAPI_PROPERTY("ConstraintSourceType", constraintSourceType),
120     };
121     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(exportEnum) / sizeof(*exportEnum), exportEnum));
122     napi_value cons = nullptr;
123     NAPI_CALL(env,
124         napi_define_class(env,
125             OS_ACCOUNT_CLASS_NAME.c_str(),
126             OS_ACCOUNT_CLASS_NAME.size(),
127             OsAccountJsConstructor,
128             nullptr,
129             sizeof(g_osAccountProperties) / sizeof(napi_property_descriptor),
130             g_osAccountProperties,
131             &cons));
132     NAPI_CALL(env, napi_create_reference(env, cons, 1, &osAccountRef_));
133     NAPI_CALL(env, napi_set_named_property(env, exports, OS_ACCOUNT_CLASS_NAME.c_str(), cons));
134 
135     return exports;
136 }
137 
GetAccountManager(napi_env env,napi_callback_info cbInfo)138 napi_value GetAccountManager(napi_env env, napi_callback_info cbInfo)
139 {
140     napi_value instance = nullptr;
141     napi_value cons = nullptr;
142     if (napi_get_reference_value(env, osAccountRef_, &cons) != napi_ok) {
143         return nullptr;
144     }
145 
146     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
147         return nullptr;
148     }
149 
150     OsAccountManager *objectInfo = new (std::nothrow) OsAccountManager();
151     if (objectInfo == nullptr) {
152         ACCOUNT_LOGE("failed to create OsAccountManager for insufficient memory");
153         return nullptr;
154     }
155     napi_status status = napi_wrap(env, instance, objectInfo,
156         [](napi_env env, void *data, void *hint) {
157             ACCOUNT_LOGI("js OsAccountManager instance garbage collection");
158             delete reinterpret_cast<OsAccountManager *>(data);
159         }, nullptr, nullptr);
160     if (status != napi_ok) {
161         ACCOUNT_LOGE("failed to wrap js instance with native object");
162         delete objectInfo;
163         return nullptr;
164     }
165     return instance;
166 }
167 
OsAccountJsConstructor(napi_env env,napi_callback_info cbinfo)168 napi_value OsAccountJsConstructor(napi_env env, napi_callback_info cbinfo)
169 {
170     napi_value thisVar = nullptr;
171     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
172     return thisVar;
173 }
174 
SetEnumProperty(napi_env env,napi_value dstObj,const int objValue,const char * propName)175 void SetEnumProperty(napi_env env, napi_value dstObj, const int objValue, const char *propName)
176 {
177     napi_value prop = nullptr;
178     napi_create_int32(env, objValue, &prop);
179     napi_set_named_property(env, dstObj, propName, prop);
180 }
181 
QueryOsAccountById(napi_env env,napi_callback_info cbInfo)182 napi_value QueryOsAccountById(napi_env env, napi_callback_info cbInfo)
183 {
184     auto queryOAByIdCB = std::make_unique<QueryOAByIdAsyncContext>();
185     queryOAByIdCB->env = env;
186     queryOAByIdCB->throwErr = true;
187 
188     if (!ParseParaQueryOAByIdCB(env, cbInfo, queryOAByIdCB.get())) {
189         return nullptr;
190     }
191 
192     napi_value result = nullptr;
193     if (queryOAByIdCB->callbackRef == nullptr) {
194         napi_create_promise(env, &queryOAByIdCB->deferred, &result);
195     } else {
196         napi_get_undefined(env, &result);
197     }
198 
199     napi_value resource = nullptr;
200     napi_create_string_utf8(env, "QueryOsAccountById", NAPI_AUTO_LENGTH, &resource);
201 
202     napi_create_async_work(env,
203         nullptr,
204         resource,
205         QueryOAByIdExecuteCB,
206         QueryOAByIdCallbackCompletedCB,
207         reinterpret_cast<void *>(queryOAByIdCB.get()),
208         &queryOAByIdCB->work);
209 
210     napi_queue_async_work_with_qos(env, queryOAByIdCB->work, napi_qos_default);
211     queryOAByIdCB.release();
212     return result;
213 }
214 
RemoveOsAccount(napi_env env,napi_callback_info cbInfo)215 napi_value RemoveOsAccount(napi_env env, napi_callback_info cbInfo)
216 {
217     auto removeOACB = std::make_unique<RemoveOAAsyncContext>();
218     removeOACB->env = env;
219     removeOACB->throwErr = true;
220 
221     if (!ParseParaRemoveOACB(env, cbInfo, removeOACB.get())) {
222         return nullptr;
223     }
224 
225     napi_value result = nullptr;
226     if (removeOACB->callbackRef == nullptr) {
227         napi_create_promise(env, &removeOACB->deferred, &result);
228     } else {
229         napi_get_undefined(env, &result);
230     }
231 
232     napi_value resource = nullptr;
233     napi_create_string_utf8(env, "RemoveOsAccount", NAPI_AUTO_LENGTH, &resource);
234 
235     napi_create_async_work(env, nullptr, resource, RemoveOAExecuteCB, RemoveOACallbackCompletedCB,
236         reinterpret_cast<void *>(removeOACB.get()), &removeOACB->work);
237 
238     napi_queue_async_work_with_qos(env, removeOACB->work, napi_qos_default);
239     removeOACB.release();
240     return result;
241 }
242 
SetOsAccountName(napi_env env,napi_callback_info cbInfo)243 napi_value SetOsAccountName(napi_env env, napi_callback_info cbInfo)
244 {
245     auto setOANameCB = std::make_unique<SetOANameAsyncContext>();
246     setOANameCB->env = env;
247     setOANameCB->throwErr = true;
248 
249     if (!ParseParaSetOAName(env, cbInfo, setOANameCB.get())) {
250         return nullptr;
251     }
252 
253     napi_value result = nullptr;
254     if (setOANameCB->callbackRef == nullptr) {
255         napi_create_promise(env, &setOANameCB->deferred, &result);
256     } else {
257         napi_get_undefined(env, &result);
258     }
259 
260     napi_value resource = nullptr;
261     napi_create_string_utf8(env, "SetOsAccountName", NAPI_AUTO_LENGTH, &resource);
262 
263     napi_create_async_work(env,
264         nullptr,
265         resource,
266         SetOANameExecuteCB,
267         SetOANameCallbackCompletedCB,
268         reinterpret_cast<void *>(setOANameCB.get()),
269         &setOANameCB->work);
270 
271     napi_queue_async_work_with_qos(env, setOANameCB->work, napi_qos_default);
272     setOANameCB.release();
273     return result;
274 }
275 
SetOsAccountConstraints(napi_env env,napi_callback_info cbInfo)276 napi_value SetOsAccountConstraints(napi_env env, napi_callback_info cbInfo)
277 {
278     auto setOAConsCB = std::make_unique<SetOAConsAsyncContext>();
279     setOAConsCB->env = env;
280     setOAConsCB->throwErr = true;
281 
282     if (!ParseParaSetOAConstraints(env, cbInfo, setOAConsCB.get())) {
283         return nullptr;
284     }
285 
286     napi_value result = nullptr;
287     if (setOAConsCB->callbackRef == nullptr) {
288         napi_create_promise(env, &setOAConsCB->deferred, &result);
289     } else {
290         napi_get_undefined(env, &result);
291     }
292 
293     napi_value resource = nullptr;
294     napi_create_string_utf8(env, "SetOsAccountConstraints", NAPI_AUTO_LENGTH, &resource);
295 
296     napi_create_async_work(env,
297         nullptr,
298         resource,
299         SetOAConsExecuteCB,
300         SetOAConsCallbackCompletedCB,
301         reinterpret_cast<void *>(setOAConsCB.get()),
302         &setOAConsCB->work);
303 
304     napi_queue_async_work_with_qos(env, setOAConsCB->work, napi_qos_default);
305     setOAConsCB.release();
306     return result;
307 }
308 
ActivateOsAccount(napi_env env,napi_callback_info cbInfo)309 napi_value ActivateOsAccount(napi_env env, napi_callback_info cbInfo)
310 {
311     auto activeOACB = std::make_unique<ActivateOAAsyncContext>();
312     activeOACB->env = env;
313     activeOACB->throwErr = true;
314 
315     if (!ParseParaActiveOA(env, cbInfo, activeOACB.get())) {
316         return nullptr;
317     }
318 
319     napi_value result = nullptr;
320     if (activeOACB->callbackRef == nullptr) {
321         napi_create_promise(env, &activeOACB->deferred, &result);
322     } else {
323         napi_get_undefined(env, &result);
324     }
325 
326     napi_value resource = nullptr;
327     napi_create_string_utf8(env, "ActivateOsAccount", NAPI_AUTO_LENGTH, &resource);
328 
329     napi_create_async_work(env,
330         nullptr,
331         resource,
332         ActivateOAExecuteCB,
333         ActivateOACallbackCompletedCB,
334         reinterpret_cast<void *>(activeOACB.get()),
335         &activeOACB->work);
336 
337     napi_queue_async_work_with_qos(env, activeOACB->work, napi_qos_default);
338     activeOACB.release();
339     return result;
340 }
341 
CreateOsAccount(napi_env env,napi_callback_info cbInfo)342 napi_value CreateOsAccount(napi_env env, napi_callback_info cbInfo)
343 {
344     auto createOACB = std::make_unique<CreateOAAsyncContext>();
345     createOACB->env = env;
346     createOACB->throwErr = true;
347 
348     if (!ParseParaCreateOA(env, cbInfo, createOACB.get())) {
349         return nullptr;
350     }
351 
352     napi_value result = nullptr;
353     if (createOACB->callbackRef == nullptr) {
354         napi_create_promise(env, &createOACB->deferred, &result);
355     } else {
356         napi_get_undefined(env, &result);
357     }
358 
359     napi_value resource = nullptr;
360     napi_create_string_utf8(env, "CreateOsAccount", NAPI_AUTO_LENGTH, &resource);
361 
362     napi_create_async_work(env, nullptr, resource, CreateOAExecuteCB, CreateOACallbackCompletedCB,
363         reinterpret_cast<void *>(createOACB.get()), &createOACB->work);
364 
365     napi_queue_async_work_with_qos(env, createOACB->work, napi_qos_default);
366     createOACB.release();
367     return result;
368 }
369 
CreateOsAccountForDomain(napi_env env,napi_callback_info cbInfo)370 napi_value CreateOsAccountForDomain(napi_env env, napi_callback_info cbInfo)
371 {
372     auto createOAForDomainCB = std::make_unique<CreateOAForDomainAsyncContext>();
373     createOAForDomainCB->env = env;
374     createOAForDomainCB->throwErr = true;
375 
376     if (!ParseParaCreateOAForDomain(env, cbInfo, createOAForDomainCB.get())) {
377         return nullptr;
378     }
379 
380     napi_value result = nullptr;
381     if (createOAForDomainCB->callbackRef == nullptr) {
382         napi_create_promise(env, &createOAForDomainCB->deferred, &result);
383     } else {
384         napi_get_undefined(env, &result);
385     }
386 
387     napi_value resource = nullptr;
388     napi_create_string_utf8(env, "CreateOsAccountForDomain", NAPI_AUTO_LENGTH, &resource);
389 
390     napi_create_async_work(env, nullptr, resource, CreateOAForDomainExecuteCB, CreateOAForDomainCompletedCB,
391         createOAForDomainCB.get(), &createOAForDomainCB->work);
392 
393     napi_queue_async_work_with_qos(env, createOAForDomainCB->work, napi_qos_default);
394     createOAForDomainCB.release();
395     return result;
396 }
397 
GetOsAccountCount(napi_env env,napi_callback_info cbInfo)398 napi_value GetOsAccountCount(napi_env env, napi_callback_info cbInfo)
399 {
400     return GetCreatedOsAccountsCountInner(env, cbInfo, true);
401 }
402 
GetCreatedOsAccountsCount(napi_env env,napi_callback_info cbInfo)403 napi_value GetCreatedOsAccountsCount(napi_env env, napi_callback_info cbInfo)
404 {
405     return GetCreatedOsAccountsCountInner(env, cbInfo, false);
406 }
407 
GetCreatedOsAccountsCountInner(napi_env env,napi_callback_info cbInfo,bool throwErr)408 napi_value GetCreatedOsAccountsCountInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
409 {
410     auto getOACount = std::make_unique<GetOACountAsyncContext>();
411     getOACount->env = env;
412     getOACount->throwErr = throwErr;
413 
414     if (!ParseParaGetOACount(env, cbInfo, getOACount.get()) && throwErr) {
415         return nullptr;
416     }
417 
418     napi_value result = nullptr;
419     if (getOACount->callbackRef == nullptr) {
420         napi_create_promise(env, &getOACount->deferred, &result);
421     } else {
422         napi_get_undefined(env, &result);
423     }
424 
425     napi_value resource = nullptr;
426     napi_create_string_utf8(env, "GetCreatedOsAccountsCountInner", NAPI_AUTO_LENGTH, &resource);
427 
428     napi_create_async_work(env,
429         nullptr,
430         resource,
431         GetOACountExecuteCB,
432         GetOACountCallbackCompletedCB,
433         reinterpret_cast<void *>(getOACount.get()),
434         &getOACount->work);
435 
436     napi_queue_async_work_with_qos(env, getOACount->work, napi_qos_default);
437     getOACount.release();
438     return result;
439 }
440 
QueryDistributedVirtualDeviceId(napi_env env,napi_callback_info cbInfo)441 napi_value QueryDistributedVirtualDeviceId(napi_env env, napi_callback_info cbInfo)
442 {
443     return GetDistributedVirtualDeviceIdInner(env, cbInfo, true);
444 }
445 
GetDistributedVirtualDeviceId(napi_env env,napi_callback_info cbInfo)446 napi_value GetDistributedVirtualDeviceId(napi_env env, napi_callback_info cbInfo)
447 {
448     return GetDistributedVirtualDeviceIdInner(env, cbInfo, false);
449 }
450 
GetDistributedVirtualDeviceIdInner(napi_env env,napi_callback_info cbInfo,bool throwErr)451 napi_value GetDistributedVirtualDeviceIdInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
452 {
453     auto dbDeviceId = std::make_unique<DbDeviceIdAsyncContext>();
454     dbDeviceId->env = env;
455     dbDeviceId->throwErr = throwErr;
456 
457     if (!ParseParaDbDeviceId(env, cbInfo, dbDeviceId.get()) && throwErr) {
458         return nullptr;
459     }
460 
461     napi_value result = nullptr;
462     if (dbDeviceId->callbackRef == nullptr) {
463         napi_create_promise(env, &dbDeviceId->deferred, &result);
464     } else {
465         napi_get_undefined(env, &result);
466     }
467 
468     napi_value resource = nullptr;
469     napi_create_string_utf8(env, "GetDistributedVirtualDeviceIdInner", NAPI_AUTO_LENGTH, &resource);
470 
471     napi_create_async_work(env,
472         nullptr,
473         resource,
474         DbDeviceIdExecuteCB,
475         DbDeviceIdCallbackCompletedCB,
476         reinterpret_cast<void *>(dbDeviceId.get()),
477         &dbDeviceId->work);
478 
479     napi_queue_async_work_with_qos(env, dbDeviceId->work, napi_qos_default);
480     dbDeviceId.release();
481     return result;
482 }
483 
GetOsAccountConstraints(napi_env env,napi_callback_info cbInfo)484 napi_value GetOsAccountConstraints(napi_env env, napi_callback_info cbInfo)
485 {
486     return GetOsAccountAllConstraintsInner(env, cbInfo, true);
487 }
488 
GetOsAccountAllConstraints(napi_env env,napi_callback_info cbInfo)489 napi_value GetOsAccountAllConstraints(napi_env env, napi_callback_info cbInfo)
490 {
491     return GetOsAccountAllConstraintsInner(env, cbInfo, false);
492 }
493 
GetOsAccountAllConstraintsInner(napi_env env,napi_callback_info cbInfo,bool throwErr)494 napi_value GetOsAccountAllConstraintsInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
495 {
496     auto getAllConsCB = std::make_unique<GetAllConsAsyncContext>();
497     getAllConsCB->env = env;
498     getAllConsCB->throwErr = throwErr;
499 
500     if (!ParseParaGetAllCons(env, cbInfo, getAllConsCB.get()) && throwErr) {
501         return nullptr;
502     }
503 
504     napi_value result = nullptr;
505     if (getAllConsCB->callbackRef == nullptr) {
506         napi_create_promise(env, &getAllConsCB->deferred, &result);
507     } else {
508         napi_get_undefined(env, &result);
509     }
510 
511     napi_value resource = nullptr;
512     napi_create_string_utf8(env, "GetOsAccountAllConstraints", NAPI_AUTO_LENGTH, &resource);
513 
514     napi_create_async_work(env,
515         nullptr,
516         resource,
517         GetAllConsExecuteCB,
518         GetAllConsCallbackCompletedCB,
519         reinterpret_cast<void *>(getAllConsCB.get()),
520         &getAllConsCB->work);
521 
522     napi_queue_async_work_with_qos(env, getAllConsCB->work, napi_qos_default);
523     getAllConsCB.release();
524     return result;
525 }
526 
QueryOsAccountLocalIdFromProcess(napi_env env,napi_callback_info cbInfo)527 napi_value QueryOsAccountLocalIdFromProcess(napi_env env, napi_callback_info cbInfo)
528 {
529     return GetOsAccountLocalIdFromProcessInner(env, cbInfo, true);
530 }
531 
GetOsAccountLocalIdFromProcess(napi_env env,napi_callback_info cbInfo)532 napi_value GetOsAccountLocalIdFromProcess(napi_env env, napi_callback_info cbInfo)
533 {
534     return GetOsAccountLocalIdFromProcessInner(env, cbInfo, false);
535 }
536 
GetOsAccountLocalIdFromProcessInner(napi_env env,napi_callback_info cbInfo,bool throwErr)537 napi_value GetOsAccountLocalIdFromProcessInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
538 {
539     auto getIdCB = std::make_unique<GetIdAsyncContext>();
540     getIdCB->env = env;
541     getIdCB->throwErr = throwErr;
542 
543     if (!ParseParaProcessId(env, cbInfo, getIdCB.get()) && throwErr) {
544         return nullptr;
545     }
546 
547     napi_value result = nullptr;
548     if (getIdCB->callbackRef == nullptr) {
549         napi_create_promise(env, &getIdCB->deferred, &result);
550     } else {
551         napi_get_undefined(env, &result);
552     }
553 
554     napi_value resource = nullptr;
555     napi_create_string_utf8(env, "GetOsAccountLocalIdFromProcessInner", NAPI_AUTO_LENGTH, &resource);
556 
557     napi_create_async_work(env,
558         nullptr,
559         resource,
560         GetProcessIdExecuteCB,
561         GetProcessIdCallbackCompletedCB,
562         reinterpret_cast<void *>(getIdCB.get()),
563         &getIdCB->work);
564 
565     napi_queue_async_work_with_qos(env, getIdCB->work, napi_qos_default);
566     getIdCB.release();
567     return result;
568 }
569 
QueryAllCreatedOsAccounts(napi_env env,napi_callback_info cbInfo)570 napi_value QueryAllCreatedOsAccounts(napi_env env, napi_callback_info cbInfo)
571 {
572     auto queryAllOA = std::make_unique<QueryCreateOAAsyncContext>();
573     queryAllOA->env = env;
574     queryAllOA->throwErr = true;
575 
576     if (!ParseQueryAllCreateOA(env, cbInfo, queryAllOA.get())) {
577         return nullptr;
578     }
579 
580     napi_value result = nullptr;
581     if (queryAllOA->callbackRef == nullptr) {
582         napi_create_promise(env, &queryAllOA->deferred, &result);
583     } else {
584         napi_get_undefined(env, &result);
585     }
586 
587     napi_value resource = nullptr;
588     napi_create_string_utf8(env, "QueryAllCreatedOsAccounts", NAPI_AUTO_LENGTH, &resource);
589 
590     napi_create_async_work(env,
591         nullptr,
592         resource,
593         QueryCreateOAExecuteCB,
594         QueryCreateOACallbackCompletedCB,
595         reinterpret_cast<void *>(queryAllOA.get()),
596         &queryAllOA->work);
597 
598     napi_queue_async_work_with_qos(env, queryAllOA->work, napi_qos_default);
599     queryAllOA.release();
600     return result;
601 }
602 
QueryOsAccountConstraintSourceTypes(napi_env env,napi_callback_info cbInfo)603 napi_value QueryOsAccountConstraintSourceTypes(napi_env env, napi_callback_info cbInfo)
604 {
605     auto queryConstraintSource = std::make_unique<QueryOAConstraintSrcTypeContext>();
606     queryConstraintSource->env = env;
607     queryConstraintSource->throwErr = true;
608 
609     if (!ParseQueryOAConstraintSrcTypes(env, cbInfo, queryConstraintSource.get())) {
610         return nullptr;
611     }
612 
613     napi_value result = nullptr;
614     if (queryConstraintSource->callbackRef == nullptr) {
615         napi_create_promise(env, &queryConstraintSource->deferred, &result);
616     } else {
617         napi_get_undefined(env, &result);
618     }
619 
620     napi_value resource = nullptr;
621     napi_create_string_utf8(env, "QueryOsAccountConstraintSourceTypes", NAPI_AUTO_LENGTH, &resource);
622 
623     napi_create_async_work(env,
624         nullptr,
625         resource,
626         QueryOAContSrcTypeExecuteCB,
627         QueryOAContSrcTypeCallbackCompletedCB,
628         reinterpret_cast<void *>(queryConstraintSource.get()),
629         &queryConstraintSource->work);
630 
631     napi_queue_async_work_with_qos(env, queryConstraintSource->work, napi_qos_default);
632     queryConstraintSource.release();
633     return result;
634 }
635 
GetActivatedOsAccountIds(napi_env env,napi_callback_info cbInfo)636 napi_value GetActivatedOsAccountIds(napi_env env, napi_callback_info cbInfo)
637 {
638     return QueryActivatedOsAccountIdsInner(env, cbInfo, true);
639 }
640 
QueryActivatedOsAccountIds(napi_env env,napi_callback_info cbInfo)641 napi_value QueryActivatedOsAccountIds(napi_env env, napi_callback_info cbInfo)
642 {
643     return QueryActivatedOsAccountIdsInner(env, cbInfo, false);
644 }
645 
QueryActivatedOsAccountIdsInner(napi_env env,napi_callback_info cbInfo,bool throwErr)646 napi_value QueryActivatedOsAccountIdsInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
647 {
648     auto queryActiveIds = std::make_unique<QueryActiveIdsAsyncContext>();
649     queryActiveIds->env = env;
650     queryActiveIds->throwErr = throwErr;
651 
652     if (!ParseQueryActiveIds(env, cbInfo, queryActiveIds.get()) && throwErr) {
653         return nullptr;
654     }
655 
656     napi_value result = nullptr;
657     if (queryActiveIds->callbackRef == nullptr) {
658         napi_create_promise(env, &queryActiveIds->deferred, &result);
659     } else {
660         napi_get_undefined(env, &result);
661     }
662 
663     napi_value resource = nullptr;
664     napi_create_string_utf8(env, "QueryActivatedOsAccountIdsInner", NAPI_AUTO_LENGTH, &resource);
665 
666     napi_create_async_work(env,
667         nullptr,
668         resource,
669         QueryActiveIdsExecuteCB,
670         QueryActiveIdsCallbackCompletedCB,
671         reinterpret_cast<void *>(queryActiveIds.get()),
672         &queryActiveIds->work);
673 
674     napi_queue_async_work_with_qos(env, queryActiveIds->work, napi_qos_default);
675     queryActiveIds.release();
676     return result;
677 }
678 
GetOsAccountProfilePhoto(napi_env env,napi_callback_info cbInfo)679 napi_value GetOsAccountProfilePhoto(napi_env env, napi_callback_info cbInfo)
680 {
681     auto getPhoto = std::make_unique<GetOAPhotoAsyncContext>();
682     getPhoto->env = env;
683     getPhoto->throwErr = true;
684 
685     if (!ParseParaGetPhoto(env, cbInfo, getPhoto.get())) {
686         return nullptr;
687     }
688 
689     napi_value result = nullptr;
690     if (getPhoto->callbackRef == nullptr) {
691         napi_create_promise(env, &getPhoto->deferred, &result);
692     } else {
693         napi_get_undefined(env, &result);
694     }
695 
696     napi_value resource = nullptr;
697     napi_create_string_utf8(env, "GetOsAccountProfilePhoto", NAPI_AUTO_LENGTH, &resource);
698 
699     napi_create_async_work(env, nullptr, resource, GetOAPhotoExecuteCB, GetOAPhotoCallbackCompletedCB,
700         reinterpret_cast<void *>(getPhoto.get()), &getPhoto->work);
701 
702     napi_queue_async_work_with_qos(env, getPhoto->work, napi_qos_default);
703     getPhoto.release();
704     return result;
705 }
706 
GetCurrentOsAccount(napi_env env,napi_callback_info cbInfo)707 napi_value GetCurrentOsAccount(napi_env env, napi_callback_info cbInfo)
708 {
709     return QueryCurrentOsAccountInner(env, cbInfo, true);
710 }
711 
QueryCurrentOsAccount(napi_env env,napi_callback_info cbInfo)712 napi_value QueryCurrentOsAccount(napi_env env, napi_callback_info cbInfo)
713 {
714     return QueryCurrentOsAccountInner(env, cbInfo, false);
715 }
716 
QueryCurrentOsAccountInner(napi_env env,napi_callback_info cbInfo,bool throwErr)717 napi_value QueryCurrentOsAccountInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
718 {
719     auto currentOA = std::make_unique<CurrentOAAsyncContext>();
720     currentOA->env = env;
721     currentOA->throwErr = throwErr;
722 
723     if (!ParseParaCurrentOA(env, cbInfo, currentOA.get()) && throwErr) {
724         return nullptr;
725     }
726 
727     napi_value result = nullptr;
728     if (currentOA->callbackRef == nullptr) {
729         napi_create_promise(env, &currentOA->deferred, &result);
730     } else {
731         napi_get_undefined(env, &result);
732     }
733 
734     napi_value resource = nullptr;
735     napi_create_string_utf8(env, "QueryCurrentOsAccount", NAPI_AUTO_LENGTH, &resource);
736 
737     napi_create_async_work(env,
738         nullptr,
739         resource,
740         QueryCurrentOAExecuteCB,
741         QueryCurrentOACallbackCompletedCB,
742         reinterpret_cast<void *>(currentOA.get()),
743         &currentOA->work);
744 
745     napi_queue_async_work_with_qos(env, currentOA->work, napi_qos_default);
746     currentOA.release();
747     return result;
748 }
749 
GetOsAccountLocalIdFromUidInner(napi_env env,napi_callback_info cbInfo,bool throwErr)750 napi_value GetOsAccountLocalIdFromUidInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
751 {
752     auto idByUid = std::make_unique<GetIdByUidAsyncContext>();
753     idByUid->env = env;
754     idByUid->throwErr = throwErr;
755 
756     if (!ParseParaGetIdByUid(env, cbInfo, idByUid.get()) && throwErr) {
757         return nullptr;
758     }
759 
760     napi_value result = nullptr;
761     if (idByUid->callbackRef == nullptr) {
762         napi_create_promise(env, &idByUid->deferred, &result);
763     } else {
764         napi_get_undefined(env, &result);
765     }
766 
767     napi_value resource = nullptr;
768     napi_create_string_utf8(env, "GetOsAccountLocalIdFromUidInner", NAPI_AUTO_LENGTH, &resource);
769 
770     napi_create_async_work(env, nullptr, resource, GetIdByUidExecuteCB, GetIdByUidCallbackCompletedCB,
771         reinterpret_cast<void *>(idByUid.get()), &idByUid->work);
772 
773     napi_queue_async_work_with_qos(env, idByUid->work, napi_qos_default);
774     idByUid.release();
775     return result;
776 }
777 
GetOsAccountLocalIdFromUid(napi_env env,napi_callback_info cbInfo)778 napi_value GetOsAccountLocalIdFromUid(napi_env env, napi_callback_info cbInfo)
779 {
780     return GetOsAccountLocalIdFromUidInner(env, cbInfo, false);
781 }
782 
GetOsAccountLocalIdForUid(napi_env env,napi_callback_info cbInfo)783 napi_value GetOsAccountLocalIdForUid(napi_env env, napi_callback_info cbInfo)
784 {
785     return GetOsAccountLocalIdFromUidInner(env, cbInfo, true);
786 }
787 
ParseUidFromCbInfo(napi_env env,napi_callback_info cbInfo,int32_t & uid)788 static ErrCode ParseUidFromCbInfo(napi_env env, napi_callback_info cbInfo, int32_t &uid)
789 {
790     size_t argc = ARGS_SIZE_ONE;
791     napi_value argv[ARGS_SIZE_ONE] = {0};
792     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
793     if ((argc <= 0) || (!GetIntProperty(env, argv[0], uid))) {
794         ACCOUNT_LOGE("failed to parse uid");
795         return ERR_JS_PARAMETER_ERROR;
796     }
797     return ERR_OK;
798 }
799 
GetOsAccountLocalIdForUidSync(napi_env env,napi_callback_info cbInfo)800 napi_value GetOsAccountLocalIdForUidSync(napi_env env, napi_callback_info cbInfo)
801 {
802     napi_value napiValue = nullptr;
803     int32_t uid = 0;
804     ErrCode errCode = ParseUidFromCbInfo(env, cbInfo, uid);
805     if (errCode != ERR_OK) {
806         AccountNapiThrow(env, errCode);
807         return napiValue;
808     }
809     int32_t localId = 0;
810     errCode = OsAccountManager::GetOsAccountLocalIdFromUid(uid, localId);
811     if (errCode != ERR_OK) {
812         AccountNapiThrow(env, errCode);
813         return napiValue;
814     }
815     NAPI_CALL(env, napi_create_int32(env, localId, &napiValue));
816     return napiValue;
817 }
818 
GetBundleIdFromUid(napi_env env,napi_callback_info cbInfo)819 napi_value GetBundleIdFromUid(napi_env env, napi_callback_info cbInfo)
820 {
821     auto bundleIdByUid = std::make_unique<GetIdByUidAsyncContext>();
822     bundleIdByUid->env = env;
823     bundleIdByUid->throwErr = true;
824 
825     if (!ParseParaGetIdByUid(env, cbInfo, bundleIdByUid.get())) {
826         return nullptr;
827     }
828 
829     napi_value result = nullptr;
830     if (bundleIdByUid->callbackRef == nullptr) {
831         napi_create_promise(env, &bundleIdByUid->deferred, &result);
832     } else {
833         napi_get_undefined(env, &result);
834     }
835 
836     napi_value resource = nullptr;
837     napi_create_string_utf8(env, "GetBundleIdFromUid", NAPI_AUTO_LENGTH, &resource);
838 
839     napi_create_async_work(env, nullptr, resource,
840         GetBundleIdByUidExecuteCB,
841         GetBundleIdByUidCallbackCompletedCB,
842         reinterpret_cast<void *>(bundleIdByUid.get()),
843         &bundleIdByUid->work);
844 
845     napi_queue_async_work_with_qos(env, bundleIdByUid->work, napi_qos_default);
846     bundleIdByUid.release();
847     return result;
848 }
849 
GetBundleIdForUidSync(napi_env env,napi_callback_info cbInfo)850 napi_value GetBundleIdForUidSync(napi_env env, napi_callback_info cbInfo)
851 {
852     napi_value retValue = nullptr;
853     int32_t uid = 0;
854     ErrCode errCode = ParseUidFromCbInfo(env, cbInfo, uid);
855     if (errCode != ERR_OK) {
856         AccountNapiThrow(env, errCode);
857         return retValue;
858     }
859     int32_t bundleId = 0;
860     errCode = OsAccountManager::GetBundleIdFromUid(uid, bundleId);
861     if (errCode != ERR_OK) {
862         AccountNapiThrow(env, errCode);
863         return retValue;
864     }
865     NAPI_CALL(env, napi_create_int32(env, bundleId, &retValue));
866     return retValue;
867 }
868 
QueryOsAccountLocalIdFromDomain(napi_env env,napi_callback_info cbInfo)869 napi_value QueryOsAccountLocalIdFromDomain(napi_env env, napi_callback_info cbInfo)
870 {
871     return GetOsAccountLocalIdFromDomainInner(env, cbInfo, true);
872 }
873 
GetOsAccountLocalIdFromDomain(napi_env env,napi_callback_info cbInfo)874 napi_value GetOsAccountLocalIdFromDomain(napi_env env, napi_callback_info cbInfo)
875 {
876     return GetOsAccountLocalIdFromDomainInner(env, cbInfo, false);
877 }
878 
GetOsAccountLocalIdFromDomainInner(napi_env env,napi_callback_info cbInfo,bool throwErr)879 napi_value GetOsAccountLocalIdFromDomainInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
880 {
881     auto idByDomain = std::make_unique<GetIdByDomainAsyncContext>();
882     idByDomain->env = env;
883     idByDomain->throwErr = throwErr;
884 
885     if (!ParseParaGetIdByDomain(env, cbInfo, idByDomain.get()) && throwErr) {
886         return nullptr;
887     }
888 
889     napi_value result = nullptr;
890     if (idByDomain->callbackRef == nullptr) {
891         napi_create_promise(env, &idByDomain->deferred, &result);
892     } else {
893         napi_get_undefined(env, &result);
894     }
895 
896     napi_value resource = nullptr;
897     napi_create_string_utf8(env, "GetOsAccountLocalIdFromDomainInner", NAPI_AUTO_LENGTH, &resource);
898 
899     napi_create_async_work(env, nullptr, resource, GetIdByDomainExecuteCB, GetIdByDomainCallbackCompletedCB,
900         reinterpret_cast<void *>(idByDomain.get()), &idByDomain->work);
901 
902     napi_queue_async_work_with_qos(env, idByDomain->work, napi_qos_default);
903     idByDomain.release();
904     return result;
905 }
906 
SetOsAccountProfilePhoto(napi_env env,napi_callback_info cbInfo)907 napi_value SetOsAccountProfilePhoto(napi_env env, napi_callback_info cbInfo)
908 {
909     auto setPhoto = std::make_unique<SetOAPhotoAsyncContext>();
910     setPhoto->env = env;
911     setPhoto->throwErr = true;
912 
913     if (!ParseParaSetPhoto(env, cbInfo, setPhoto.get())) {
914         return nullptr;
915     }
916 
917     napi_value result = nullptr;
918     if (setPhoto->callbackRef == nullptr) {
919         napi_create_promise(env, &setPhoto->deferred, &result);
920     } else {
921         napi_get_undefined(env, &result);
922     }
923 
924     napi_value resource = nullptr;
925     napi_create_string_utf8(env, "SetOsAccountProfilePhoto", NAPI_AUTO_LENGTH, &resource);
926 
927     napi_create_async_work(env, nullptr, resource, SetPhotoExecuteCB, SetPhotoCompletedCB,
928         reinterpret_cast<void *>(setPhoto.get()), &setPhoto->work);
929 
930     napi_queue_async_work_with_qos(env, setPhoto->work, napi_qos_default);
931     setPhoto.release();
932     return result;
933 }
934 
QueryMaxOsAccountNumber(napi_env env,napi_callback_info cbInfo)935 napi_value QueryMaxOsAccountNumber(napi_env env, napi_callback_info cbInfo)
936 {
937     auto maxNum = std::make_unique<QueryMaxNumAsyncContext>();
938     maxNum->env = env;
939     maxNum->throwErr = true;
940 
941     if (!ParseParaQueryMaxNum(env, cbInfo, maxNum.get())) {
942         return nullptr;
943     }
944 
945     napi_value result = nullptr;
946     if (maxNum->callbackRef == nullptr) {
947         napi_create_promise(env, &maxNum->deferred, &result);
948     } else {
949         napi_get_undefined(env, &result);
950     }
951 
952     napi_value resource = nullptr;
953     napi_create_string_utf8(env, "QueryMaxOsAccountNumber", NAPI_AUTO_LENGTH, &resource);
954 
955     napi_create_async_work(env, nullptr, resource, QueryMaxNumExecuteCB, QueryMaxNumCompletedCB,
956         reinterpret_cast<void *>(maxNum.get()), &maxNum->work);
957 
958     napi_queue_async_work_with_qos(env, maxNum->work, napi_qos_default);
959     maxNum.release();
960     return result;
961 }
962 
InnerIsOsAccountActived(napi_env env,napi_callback_info cbInfo,bool throwErr)963 napi_value InnerIsOsAccountActived(napi_env env, napi_callback_info cbInfo, bool throwErr)
964 {
965     auto isActived = std::make_unique<IsActivedAsyncContext>();
966     isActived->env = env;
967     isActived->throwErr = throwErr;
968 
969     if (!ParseParaIsActived(env, cbInfo, isActived.get()) && throwErr) {
970         return nullptr;
971     }
972 
973     napi_value result = nullptr;
974     if (isActived->callbackRef == nullptr) {
975         napi_create_promise(env, &isActived->deferred, &result);
976     } else {
977         napi_get_undefined(env, &result);
978     }
979 
980     napi_value resource = nullptr;
981     napi_create_string_utf8(env, "IsOsAccountActived", NAPI_AUTO_LENGTH, &resource);
982 
983     napi_create_async_work(env, nullptr, resource, IsActivedExecuteCB, IsActivedCompletedCB,
984         reinterpret_cast<void *>(isActived.get()), &isActived->work);
985 
986     napi_queue_async_work_with_qos(env, isActived->work, napi_qos_default);
987     isActived.release();
988     return result;
989 }
990 
CheckOsAccountActivated(napi_env env,napi_callback_info cbInfo)991 napi_value CheckOsAccountActivated(napi_env env, napi_callback_info cbInfo)
992 {
993     return InnerIsOsAccountActived(env, cbInfo, true);
994 }
995 
IsOsAccountActived(napi_env env,napi_callback_info cbInfo)996 napi_value IsOsAccountActived(napi_env env, napi_callback_info cbInfo)
997 {
998     return InnerIsOsAccountActived(env, cbInfo, false);
999 }
1000 
InnerIsOsAccountConstraintEnable(napi_env env,napi_callback_info cbInfo,bool throwErr)1001 napi_value InnerIsOsAccountConstraintEnable(napi_env env, napi_callback_info cbInfo, bool throwErr)
1002 {
1003     auto isEnable = std::make_unique<IsConEnableAsyncContext>();
1004     isEnable->env = env;
1005     isEnable->throwErr = throwErr;
1006 
1007     if (!ParseParaIsEnable(env, cbInfo, isEnable.get()) && throwErr) {
1008         return nullptr;
1009     }
1010 
1011     napi_value result = nullptr;
1012     if (isEnable->callbackRef == nullptr) {
1013         napi_create_promise(env, &isEnable->deferred, &result);
1014     } else {
1015         napi_get_undefined(env, &result);
1016     }
1017 
1018     napi_value resource = nullptr;
1019     napi_create_string_utf8(env, "IsOsAccountConstraintEnable", NAPI_AUTO_LENGTH, &resource);
1020 
1021     napi_create_async_work(env, nullptr, resource, IsEnableExecuteCB, IsEnableCompletedCB,
1022         reinterpret_cast<void *>(isEnable.get()), &isEnable->work);
1023 
1024     napi_queue_async_work_with_qos(env, isEnable->work, napi_qos_default);
1025     isEnable.release();
1026     return result;
1027 }
1028 
IsOsAccountConstraintEnable(napi_env env,napi_callback_info cbInfo)1029 napi_value IsOsAccountConstraintEnable(napi_env env, napi_callback_info cbInfo)
1030 {
1031     return InnerIsOsAccountConstraintEnable(env, cbInfo, false);
1032 }
1033 
CheckConstraintEnabled(napi_env env,napi_callback_info cbInfo)1034 napi_value CheckConstraintEnabled(napi_env env, napi_callback_info cbInfo)
1035 {
1036     return InnerIsOsAccountConstraintEnable(env, cbInfo, true);
1037 }
1038 
GetOsAccountType(napi_env env,napi_callback_info cbInfo)1039 napi_value GetOsAccountType(napi_env env, napi_callback_info cbInfo)
1040 {
1041     return GetOsAccountTypeFromProcessInner(env, cbInfo, true);
1042 }
1043 
GetOsAccountTypeFromProcess(napi_env env,napi_callback_info cbInfo)1044 napi_value GetOsAccountTypeFromProcess(napi_env env, napi_callback_info cbInfo)
1045 {
1046     return GetOsAccountTypeFromProcessInner(env, cbInfo, false);
1047 }
1048 
GetOsAccountTypeFromProcessInner(napi_env env,napi_callback_info cbInfo,bool throwErr)1049 napi_value GetOsAccountTypeFromProcessInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
1050 {
1051     auto getType = std::make_unique<GetTypeAsyncContext>();
1052     getType->env = env;
1053     getType->throwErr = throwErr;
1054 
1055     if (!ParseParaGetType(env, cbInfo, getType.get()) && throwErr) {
1056         return nullptr;
1057     }
1058 
1059     napi_value result = nullptr;
1060     if (getType->callbackRef == nullptr) {
1061         napi_create_promise(env, &getType->deferred, &result);
1062     } else {
1063         napi_get_undefined(env, &result);
1064     }
1065 
1066     napi_value resource = nullptr;
1067     napi_create_string_utf8(env, "GetOsAccountTypeFromProcessInner", NAPI_AUTO_LENGTH, &resource);
1068 
1069     napi_create_async_work(env, nullptr, resource, GetTypeExecuteCB, GetTypeCompletedCB,
1070         reinterpret_cast<void *>(getType.get()), &getType->work);
1071 
1072     napi_queue_async_work_with_qos(env, getType->work, napi_qos_default);
1073     getType.release();
1074     return result;
1075 }
1076 
IsMultiOsAccountEnable(napi_env env,napi_callback_info cbInfo)1077 napi_value IsMultiOsAccountEnable(napi_env env, napi_callback_info cbInfo)
1078 {
1079     return InnerIsMultiOsAccountEnable(env, cbInfo, false);
1080 }
1081 
CheckMultiOsAccountEnabled(napi_env env,napi_callback_info cbInfo)1082 napi_value CheckMultiOsAccountEnabled(napi_env env, napi_callback_info cbInfo)
1083 {
1084     return InnerIsMultiOsAccountEnable(env, cbInfo, true);
1085 }
1086 
InnerIsMultiOsAccountEnable(napi_env env,napi_callback_info cbInfo,bool throwErr)1087 napi_value InnerIsMultiOsAccountEnable(napi_env env, napi_callback_info cbInfo, bool throwErr)
1088 {
1089     auto multiEn = std::make_unique<IsMultiEnAsyncContext>();
1090     multiEn->env = env;
1091     multiEn->throwErr = throwErr;
1092 
1093     if (!ParseParaIsMultiEn(env, cbInfo, multiEn.get()) && throwErr) {
1094         return nullptr;
1095     }
1096 
1097     napi_value result = nullptr;
1098     if (multiEn->callbackRef == nullptr) {
1099         napi_create_promise(env, &multiEn->deferred, &result);
1100     } else {
1101         napi_get_undefined(env, &result);
1102     }
1103 
1104     napi_value resource = nullptr;
1105     napi_create_string_utf8(env, "IsMultiOsAccountEnable", NAPI_AUTO_LENGTH, &resource);
1106 
1107     napi_create_async_work(env, nullptr, resource, IsMultiEnExecuteCB, IsMultiEnCompletedCB,
1108         reinterpret_cast<void *>(multiEn.get()), &multiEn->work);
1109 
1110     napi_queue_async_work_with_qos(env, multiEn->work, napi_qos_default);
1111     multiEn.release();
1112     return result;
1113 }
1114 
InnerIsOsAccountVerified(napi_env env,napi_callback_info cbInfo,bool throwErr)1115 napi_value InnerIsOsAccountVerified(napi_env env, napi_callback_info cbInfo, bool throwErr)
1116 {
1117     auto isVerified = std::make_unique<IsVerifiedAsyncContext>();
1118     isVerified->env = env;
1119     isVerified->throwErr = throwErr;
1120 
1121     if (!ParseParaIsVerified(env, cbInfo, isVerified.get()) && throwErr) {
1122         return nullptr;
1123     }
1124 
1125     napi_value result = nullptr;
1126     if (isVerified->callbackRef == nullptr) {
1127         napi_create_promise(env, &isVerified->deferred, &result);
1128     } else {
1129         napi_get_undefined(env, &result);
1130     }
1131 
1132     napi_value resource = nullptr;
1133     napi_create_string_utf8(env, "IsOsAccountVerified", NAPI_AUTO_LENGTH, &resource);
1134 
1135     napi_create_async_work(env, nullptr, resource, IsVerifiedExecuteCB, IsVerifiedCompletedCB,
1136         reinterpret_cast<void *>(isVerified.get()), &isVerified->work);
1137 
1138     napi_queue_async_work_with_qos(env, isVerified->work, napi_qos_default);
1139     isVerified.release();
1140     return result;
1141 }
1142 
IsOsAccountVerified(napi_env env,napi_callback_info cbInfo)1143 napi_value IsOsAccountVerified(napi_env env, napi_callback_info cbInfo)
1144 {
1145     return InnerIsOsAccountVerified(env, cbInfo, false);
1146 }
1147 
CheckOsAccountVerified(napi_env env,napi_callback_info cbInfo)1148 napi_value CheckOsAccountVerified(napi_env env, napi_callback_info cbInfo)
1149 {
1150     return InnerIsOsAccountVerified(env, cbInfo, true);
1151 }
1152 
QueryOsAccountLocalIdBySerialNumber(napi_env env,napi_callback_info cbInfo)1153 napi_value QueryOsAccountLocalIdBySerialNumber(napi_env env, napi_callback_info cbInfo)
1154 {
1155     return GetOsAccountLocalIdBySerialNumberInner(env, cbInfo, true);
1156 }
1157 
GetOsAccountLocalIdBySerialNumber(napi_env env,napi_callback_info cbInfo)1158 napi_value GetOsAccountLocalIdBySerialNumber(napi_env env, napi_callback_info cbInfo)
1159 {
1160     return GetOsAccountLocalIdBySerialNumberInner(env, cbInfo, false);
1161 }
1162 
GetOsAccountLocalIdBySerialNumberInner(napi_env env,napi_callback_info cbInfo,bool throwErr)1163 napi_value GetOsAccountLocalIdBySerialNumberInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
1164 {
1165     auto serialNumId = std::make_unique<GetSerialNumIdCBInfo>();
1166     serialNumId->env = env;
1167     serialNumId->throwErr = throwErr;
1168 
1169     if (!ParseParaSerialNumId(env, cbInfo, serialNumId.get()) && throwErr) {
1170         return nullptr;
1171     }
1172 
1173     napi_value result = nullptr;
1174     if (serialNumId->callbackRef == nullptr) {
1175         napi_create_promise(env, &serialNumId->deferred, &result);
1176     } else {
1177         napi_get_undefined(env, &result);
1178     }
1179 
1180     napi_value resource = nullptr;
1181     napi_create_string_utf8(env, "GetOsAccountLocalIdBySerialNumberInner", NAPI_AUTO_LENGTH, &resource);
1182 
1183     napi_create_async_work(env, nullptr, resource, SerialNumIdExecuteCB, SerialNumIdCompletedCB,
1184         reinterpret_cast<void *>(serialNumId.get()), &serialNumId->work);
1185 
1186     napi_queue_async_work_with_qos(env, serialNumId->work, napi_qos_default);
1187     serialNumId.release();
1188     return result;
1189 }
1190 
QuerySerialNumberByOsAccountLocalId(napi_env env,napi_callback_info cbInfo)1191 napi_value QuerySerialNumberByOsAccountLocalId(napi_env env, napi_callback_info cbInfo)
1192 {
1193     return GetSerialNumberByOsAccountLocalIdInner(env, cbInfo, true);
1194 }
1195 
GetSerialNumberByOsAccountLocalId(napi_env env,napi_callback_info cbInfo)1196 napi_value GetSerialNumberByOsAccountLocalId(napi_env env, napi_callback_info cbInfo)
1197 {
1198     return GetSerialNumberByOsAccountLocalIdInner(env, cbInfo, false);
1199 }
1200 
GetSerialNumberByOsAccountLocalIdInner(napi_env env,napi_callback_info cbInfo,bool throwErr)1201 napi_value GetSerialNumberByOsAccountLocalIdInner(napi_env env, napi_callback_info cbInfo, bool throwErr)
1202 {
1203     auto getSerialNum = std::make_unique<GetSerialNumForOAInfo>();
1204     getSerialNum->env = env;
1205     getSerialNum->throwErr = throwErr;
1206 
1207     if (!ParseParaGetSerialNum(env, cbInfo, getSerialNum.get()) && throwErr) {
1208         return nullptr;
1209     }
1210 
1211     napi_value result = nullptr;
1212     if (getSerialNum->callbackRef == nullptr) {
1213         napi_create_promise(env, &getSerialNum->deferred, &result);
1214     } else {
1215         napi_get_undefined(env, &result);
1216     }
1217 
1218     napi_value resource = nullptr;
1219     napi_create_string_utf8(env, "GetSerialNumberByOsAccountLocalId", NAPI_AUTO_LENGTH, &resource);
1220 
1221     napi_create_async_work(env,
1222         nullptr,
1223         resource,
1224         GetSerialNumExecuteCB,
1225         GetSerialNumCompletedCB,
1226         reinterpret_cast<void *>(getSerialNum.get()),
1227         &getSerialNum->work);
1228 
1229     napi_queue_async_work_with_qos(env, getSerialNum->work, napi_qos_default);
1230     getSerialNum.release();
1231     return result;
1232 }
1233 
IsTestOsAccount(napi_env env,napi_callback_info cbInfo)1234 napi_value IsTestOsAccount(napi_env env, napi_callback_info cbInfo)
1235 {
1236     return InnerIsTestOsAccount(env, cbInfo, false);
1237 }
1238 
CheckOsAccountTestable(napi_env env,napi_callback_info cbInfo)1239 napi_value CheckOsAccountTestable(napi_env env, napi_callback_info cbInfo)
1240 {
1241     return InnerIsTestOsAccount(env, cbInfo, true);
1242 }
1243 
InnerIsTestOsAccount(napi_env env,napi_callback_info cbInfo,bool throwErr)1244 napi_value InnerIsTestOsAccount(napi_env env, napi_callback_info cbInfo, bool throwErr)
1245 {
1246     auto isTest = std::make_unique<IsTestOAInfo>();
1247     isTest->env = env;
1248     isTest->throwErr = throwErr;
1249 
1250     if (!ParseParaIsTestOA(env, cbInfo, isTest.get()) && throwErr) {
1251         return nullptr;
1252     }
1253 
1254     napi_value result = nullptr;
1255     if (isTest->callbackRef == nullptr) {
1256         napi_create_promise(env, &isTest->deferred, &result);
1257     } else {
1258         napi_get_undefined(env, &result);
1259     }
1260 
1261     napi_value resource = nullptr;
1262     napi_create_string_utf8(env, "IsTestOsAccount", NAPI_AUTO_LENGTH, &resource);
1263 
1264     napi_create_async_work(env,
1265         nullptr,
1266         resource,
1267         [](napi_env env, void *data) {},
1268         [](napi_env env, napi_status status, void *data) {
1269             ACCOUNT_LOGI("napi_create_async_work complete");
1270             IsTestOAInfo *isTest = reinterpret_cast<IsTestOAInfo *>(data);
1271             isTest->errCode = 0;
1272             isTest->isTestOsAccount = false;
1273             napi_value result[RESULT_COUNT] = {0};
1274             result[PARAMZERO] = GenerateBusinessSuccess(env, isTest->throwErr);
1275             napi_get_boolean(env, isTest->isTestOsAccount, &result[PARAMONE]);
1276             ProcessCallbackOrPromise(env, isTest, result[PARAMZERO], result[PARAMONE]);
1277             delete isTest;
1278         },
1279         reinterpret_cast<void *>(isTest.get()),
1280         &isTest->work);
1281     napi_queue_async_work_with_qos(env, isTest->work, napi_qos_default);
1282     isTest.release();
1283     return result;
1284 }
1285 
IsMainOsAccount(napi_env env,napi_callback_info cbInfo)1286 napi_value IsMainOsAccount(napi_env env, napi_callback_info cbInfo)
1287 {
1288     auto isMain = std::make_unique<IsMainOAInfo>();
1289     isMain->env = env;
1290     isMain->throwErr = true;
1291 
1292     if (!ParseParaIsMainOA(env, cbInfo, isMain.get())) {
1293         return nullptr;
1294     }
1295 
1296     napi_value result = nullptr;
1297     if (isMain->callbackRef == nullptr) {
1298         napi_create_promise(env, &isMain->deferred, &result);
1299     } else {
1300         napi_get_undefined(env, &result);
1301     }
1302 
1303     napi_value resource = nullptr;
1304     napi_create_string_utf8(env, "IsMainOsAccount", NAPI_AUTO_LENGTH, &resource);
1305 
1306     napi_create_async_work(env,
1307         nullptr,
1308         resource,
1309         [](napi_env env, void *data) {
1310             IsMainOAInfo *isMain = reinterpret_cast<IsMainOAInfo *>(data);
1311             isMain->errCode = OsAccountManager::IsMainOsAccount(isMain->isMainOsAccount);
1312             ACCOUNT_LOGD("error code is %{public}d", isMain->errCode);
1313             isMain->status = (isMain->errCode == 0) ? napi_ok : napi_generic_failure;
1314         },
1315         [](napi_env env, napi_status status, void *data) {
1316             ACCOUNT_LOGD("napi_create_async_work complete");
1317             IsMainOAInfo *isMain = reinterpret_cast<IsMainOAInfo *>(data);
1318             napi_value result[RESULT_COUNT] = {0};
1319             result[PARAMZERO] = GenerateBusinessError(env, isMain->errCode);
1320             napi_get_boolean(env, isMain->isMainOsAccount, &result[PARAMONE]);
1321             ProcessCallbackOrPromise(env, isMain, result[PARAMZERO], result[PARAMONE]);
1322             delete isMain;
1323         },
1324         reinterpret_cast<void *>(isMain.get()),
1325         &isMain->work);
1326 
1327     napi_queue_async_work_with_qos(env, isMain->work, napi_qos_default);
1328     isMain.release();
1329     return result;
1330 }
1331 
IsSubscribeInMap(napi_env env,SubscribeCBInfo * subscribeCBInfo)1332 static bool IsSubscribeInMap(napi_env env, SubscribeCBInfo *subscribeCBInfo)
1333 {
1334     std::lock_guard<std::mutex> lock(g_lockForOsAccountSubscribers);
1335     auto subscribe = g_osAccountSubscribers.find(subscribeCBInfo->osManager);
1336     if (subscribe == g_osAccountSubscribers.end()) {
1337         return false;
1338     }
1339     auto it = subscribe->second.begin();
1340     while (it != subscribe->second.end()) {
1341         if (((*it)->osSubscribeType == subscribeCBInfo->osSubscribeType) &&
1342             (CompareOnAndOffRef(env, (*it)->callbackRef, subscribeCBInfo->callbackRef))) {
1343             return true;
1344         }
1345         it++;
1346     }
1347     return false;
1348 }
1349 
Subscribe(napi_env env,napi_callback_info cbInfo)1350 napi_value Subscribe(napi_env env, napi_callback_info cbInfo)
1351 {
1352     SubscribeCBInfo *subscribeCBInfo = new (std::nothrow) SubscribeCBInfo(env);
1353     if (subscribeCBInfo == nullptr) {
1354         ACCOUNT_LOGE("insufficient memory for subscribeCBInfo!");
1355         return nullptr;
1356     }
1357     subscribeCBInfo->throwErr = true;
1358 
1359     napi_value thisVar = nullptr;
1360 
1361     if (!ParseParaToSubscriber(env, cbInfo, subscribeCBInfo, &thisVar)) {
1362         delete subscribeCBInfo;
1363         ACCOUNT_LOGE("Parse subscribe failed");
1364         return nullptr;
1365     }
1366 
1367     // make osaccount subscribe info
1368     OsAccountSubscribeInfo subscribeInfo(subscribeCBInfo->osSubscribeType, subscribeCBInfo->name);
1369     // make a subscriber
1370     subscribeCBInfo->subscriber = std::make_shared<SubscriberPtr>(subscribeInfo);
1371 
1372     OsAccountManager *objectInfo = nullptr;
1373     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&objectInfo));
1374     subscribeCBInfo->osManager = objectInfo;
1375     subscribeCBInfo->subscriber->SetEnv(env);
1376     subscribeCBInfo->subscriber->SetCallbackRef(subscribeCBInfo->callbackRef);
1377     if (IsSubscribeInMap(env, subscribeCBInfo)) {
1378         delete subscribeCBInfo;
1379         return WrapVoidToJS(env);
1380     }
1381     ErrCode errCode = OsAccountManager::SubscribeOsAccount(subscribeCBInfo->subscriber);
1382     if (errCode != ERR_OK) {
1383         delete subscribeCBInfo;
1384         AccountNapiThrow(env, errCode, true);
1385         return WrapVoidToJS(env);
1386     } else {
1387         std::lock_guard<std::mutex> lock(g_lockForOsAccountSubscribers);
1388         g_osAccountSubscribers[objectInfo].emplace_back(subscribeCBInfo);
1389     }
1390     return WrapVoidToJS(env);
1391 }
1392 
SubscriberPtr(const OsAccountSubscribeInfo & subscribeInfo)1393 SubscriberPtr::SubscriberPtr(const OsAccountSubscribeInfo &subscribeInfo) : OsAccountSubscriber(subscribeInfo)
1394 {}
1395 
~SubscriberPtr()1396 SubscriberPtr::~SubscriberPtr()
1397 {}
1398 
OnAccountsChanged(const int & id)1399 void SubscriberPtr::OnAccountsChanged(const int &id)
1400 {
1401     uv_loop_s *loop = nullptr;
1402     napi_get_uv_event_loop(env_, &loop);
1403     if (loop == nullptr) {
1404         ACCOUNT_LOGE("loop instance is nullptr");
1405         return;
1406     }
1407     uv_work_t *work = new (std::nothrow) uv_work_t;
1408     if (work == nullptr) {
1409         ACCOUNT_LOGE("insufficient memory for work!");
1410         return;
1411     }
1412 
1413     SubscriberOAWorker *subscriberOAWorker = new (std::nothrow) SubscriberOAWorker();
1414 
1415     if (subscriberOAWorker == nullptr) {
1416         ACCOUNT_LOGE("insufficient memory for SubscriberAccountsWorker!");
1417         delete work;
1418         return;
1419     }
1420 
1421     subscriberOAWorker->id = id;
1422     subscriberOAWorker->env = env_;
1423     subscriberOAWorker->ref = ref_;
1424     subscriberOAWorker->subscriber = this;
1425     work->data = reinterpret_cast<void *>(subscriberOAWorker);
1426     uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAccountsChanged, uv_qos_default);
1427 }
1428 
UvQueueWorkOnAccountsChanged(uv_work_t * work,int status)1429 void UvQueueWorkOnAccountsChanged(uv_work_t *work, int status)
1430 {
1431     std::unique_ptr<uv_work_t> workPtr(work);
1432     napi_handle_scope scope = nullptr;
1433     if (!InitUvWorkCallbackEnv(work, scope)) {
1434         return;
1435     }
1436     std::unique_ptr<SubscriberOAWorker> subscriberOAWorkerData(reinterpret_cast<SubscriberOAWorker *>(work->data));
1437     bool isFound = false;
1438     {
1439         std::lock_guard<std::mutex> lock(g_lockForOsAccountSubscribers);
1440         SubscriberPtr *subscriber = subscriberOAWorkerData->subscriber;
1441         for (auto subscriberInstance : g_osAccountSubscribers) {
1442             isFound = std::any_of(subscriberInstance.second.begin(), subscriberInstance.second.end(),
1443                 [subscriber](const SubscribeCBInfo *item) {
1444                     return item->subscriber.get() == subscriber;
1445                 });
1446             if (isFound) {
1447                 ACCOUNT_LOGD("os account subscriber has been found.");
1448                 break;
1449             }
1450         }
1451     }
1452     if (isFound) {
1453         napi_value result[ARGS_SIZE_ONE] = {nullptr};
1454         napi_create_int32(subscriberOAWorkerData->env, subscriberOAWorkerData->id, &result[PARAMZERO]);
1455         napi_value undefined = nullptr;
1456         napi_get_undefined(subscriberOAWorkerData->env, &undefined);
1457         napi_value callback = nullptr;
1458         napi_get_reference_value(subscriberOAWorkerData->env, subscriberOAWorkerData->ref, &callback);
1459         napi_value resultOut = nullptr;
1460         napi_call_function(
1461             subscriberOAWorkerData->env, undefined, callback, ARGS_SIZE_ONE, &result[0], &resultOut);
1462     }
1463     napi_close_handle_scope(subscriberOAWorkerData->env, scope);
1464 }
1465 
SetEnv(const napi_env & env)1466 void SubscriberPtr::SetEnv(const napi_env &env)
1467 {
1468     env_ = env;
1469 }
1470 
SetCallbackRef(const napi_ref & ref)1471 void SubscriberPtr::SetCallbackRef(const napi_ref &ref)
1472 {
1473     ref_ = ref;
1474 }
1475 
Unsubscribe(napi_env env,napi_callback_info cbInfo)1476 napi_value Unsubscribe(napi_env env, napi_callback_info cbInfo)
1477 {
1478     UnsubscribeCBInfo *unsubscribeCBInfo = new (std::nothrow) UnsubscribeCBInfo(env);
1479     if (unsubscribeCBInfo == nullptr) {
1480         ACCOUNT_LOGE("insufficient memory for unsubscribeCBInfo!");
1481         return WrapVoidToJS(env);
1482     }
1483     unsubscribeCBInfo->callbackRef = nullptr;
1484     unsubscribeCBInfo->throwErr = true;
1485 
1486     napi_value thisVar = nullptr;
1487 
1488     if (!ParseParaToUnsubscriber(env, cbInfo, unsubscribeCBInfo, &thisVar)) {
1489         delete unsubscribeCBInfo;
1490         ACCOUNT_LOGE("Parse unsubscribe failed");
1491         return nullptr;
1492     }
1493 
1494     OsAccountManager *objectInfo = nullptr;
1495     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&objectInfo));
1496     unsubscribeCBInfo->osManager = objectInfo;
1497 
1498     UnsubscribeSync(env, unsubscribeCBInfo);
1499     delete unsubscribeCBInfo;
1500     return WrapVoidToJS(env);
1501 }
1502 
UnsubscribeSync(napi_env env,UnsubscribeCBInfo * unsubscribeCBInfo)1503 void UnsubscribeSync(napi_env env, UnsubscribeCBInfo *unsubscribeCBInfo)
1504 {
1505     std::lock_guard<std::mutex> lock(g_lockForOsAccountSubscribers);
1506     auto subscribe = g_osAccountSubscribers.find(unsubscribeCBInfo->osManager);
1507     if (subscribe == g_osAccountSubscribers.end()) {
1508         return;
1509     }
1510     auto item = subscribe->second.begin();
1511     while (item != subscribe->second.end()) {
1512         OsAccountSubscribeInfo subscribeInfo;
1513         OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType;
1514         std::string name;
1515         (*item)->subscriber->GetSubscribeInfo(subscribeInfo);
1516         subscribeInfo.GetOsAccountSubscribeType(osSubscribeType);
1517         subscribeInfo.GetName(name);
1518         if (((unsubscribeCBInfo->osSubscribeType != osSubscribeType) || (unsubscribeCBInfo->name != name)) ||
1519             ((unsubscribeCBInfo->callbackRef != nullptr) &&
1520             (!CompareOnAndOffRef(env, (*item)->callbackRef, unsubscribeCBInfo->callbackRef)))) {
1521             item++;
1522             continue;
1523         }
1524         int errCode = OsAccountManager::UnsubscribeOsAccount((*item)->subscriber);
1525         if (errCode != ERR_OK) {
1526             AccountNapiThrow(env, errCode, true);
1527             return;
1528         }
1529         delete (*item);
1530         item = subscribe->second.erase(item);
1531         if (unsubscribeCBInfo->callbackRef != nullptr) {
1532             break;
1533         }
1534     }
1535     if (subscribe->second.empty()) {
1536         g_osAccountSubscribers.erase(subscribe->first);
1537     }
1538 }
1539 }  // namespace AccountJsKit
1540 }  // namespace OHOS