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