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