• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_distributed_account.h"
17 #include <map>
18 #include <string>
19 #include <unistd.h>
20 #include "account_log_wrapper.h"
21 #include "js_native_api.h"
22 #include "js_native_api_types.h"
23 #include "napi_account_common.h"
24 #include "napi/native_api.h"
25 #include "napi/native_common.h"
26 #include "napi/native_node_api.h"
27 #include "node_api.h"
28 #include "ohos_account_kits.h"
29 #include "napi_account_error.h"
30 #include "napi_common.h"
31 
32 using namespace OHOS::AccountSA;
33 
34 namespace OHOS {
35 namespace AccountJsKit {
36 constexpr std::int32_t PARAM_ZERO = 0;
37 constexpr std::int32_t PARAM_ONE = 1;
38 constexpr std::int32_t PARAM_TWO = 2;
39 constexpr std::int32_t PARAM_THREE = 3;
40 constexpr int RESULT_COUNT = 2;
41 const std::string DISTRIBUTED_ACCOUNT_CLASS_NAME = "DistributedAccountAbility";
42 const std::string PROPERTY_KEY_NAME = "name";
43 const std::string PROPERTY_KEY_ID = "id";
44 const std::string PROPERTY_KEY_EVENT = "event";
45 const std::string PROPERTY_KEY_NICKNAME = "nickname";
46 const std::string PROPERTY_KEY_AVATAR = "avatar";
47 const std::string PROPERTY_KEY_SCALABLE = "scalableData";
48 const std::string PROPERTY_KEY_STATUS = "status";
49 
50 static thread_local napi_ref distributedAccountRef_ = nullptr;
51 
DistributedAccountAsyncContext(napi_env napiEnv)52 DistributedAccountAsyncContext::DistributedAccountAsyncContext(napi_env napiEnv) : env(napiEnv)
53 {}
54 
~DistributedAccountAsyncContext()55 DistributedAccountAsyncContext::~DistributedAccountAsyncContext()
56 {
57     if (callbackRef != nullptr) {
58         NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, callbackRef));
59         callbackRef = nullptr;
60     }
61 }
62 
DistributedAccountStatusConstructor(napi_env env)63 napi_value DistributedAccountStatusConstructor(napi_env env)
64 {
65     napi_value distributedAccountStatus = nullptr;
66     napi_value status1 = nullptr;
67     napi_value status2 = nullptr;
68     NAPI_CALL(env, napi_create_object(env, &distributedAccountStatus));
69     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(OHOS_ACCOUNT_STATE::ACCOUNT_STATE_UNBOUND), &status1));
70     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(OHOS_ACCOUNT_STATE::ACCOUNT_STATE_LOGIN), &status2));
71     NAPI_CALL(env, napi_set_named_property(env, distributedAccountStatus, "NOT_LOGGED_IN", status1));
72     NAPI_CALL(env, napi_set_named_property(env, distributedAccountStatus, "LOGGED_IN", status2));
73     return distributedAccountStatus;
74 }
75 
ParseQueryOhosAccountInfoAsyncContext(napi_env env,napi_callback_info cbInfo,DistributedAccountAsyncContext * asyncContext)76 bool ParseQueryOhosAccountInfoAsyncContext(
77     napi_env env, napi_callback_info cbInfo, DistributedAccountAsyncContext *asyncContext)
78 {
79     size_t argc = PARAM_TWO;
80     napi_value argv[PARAM_TWO] = {nullptr};
81     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
82     if (argc == PARAM_ONE) {
83         napi_valuetype valueType = napi_undefined;
84         napi_typeof(env, argv[PARAM_ZERO], &valueType);
85         if (valueType == napi_number) {
86             if (!GetIntProperty(env, argv[PARAM_ZERO], asyncContext->localId)) {
87                 ACCOUNT_LOGE("Get localId failed");
88                 return false;
89             }
90             asyncContext->withLocalId = true;
91             return true;
92         }
93         if (!GetCallbackProperty(env, argv[PARAM_ZERO], asyncContext->callbackRef, PARAM_ONE)) {
94             ACCOUNT_LOGE("Get callback failed");
95             return false;
96         }
97     }
98     if (argc == PARAM_TWO) {
99         if ((!GetCallbackProperty(env, argv[PARAM_TWO - 1], asyncContext->callbackRef, PARAM_ONE))) {
100             ACCOUNT_LOGE("Get callbackRef failed");
101             return false;
102         }
103         if (!GetIntProperty(env, argv[PARAM_ZERO], asyncContext->localId)) {
104             ACCOUNT_LOGE("Get localId failed");
105             return false;
106         }
107         asyncContext->withLocalId = true;
108         return true;
109     }
110     return true;
111 }
112 
GetAccountInfo(napi_env env,napi_value object,DistributedAccountAsyncContext * asyncContext)113 bool GetAccountInfo(napi_env env, napi_value object, DistributedAccountAsyncContext *asyncContext)
114 {
115     if (!GetStringPropertyByKey(env, object, PROPERTY_KEY_NAME, asyncContext->ohosAccountInfo.name_)) {
116         ACCOUNT_LOGE("Failed to get DistributedInfo's %{public}s property", PROPERTY_KEY_NAME.c_str());
117         std::string errMsg = "The type of " + PROPERTY_KEY_NAME + " must be string";
118         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
119         return false;
120     }
121     if (!GetStringPropertyByKey(env, object, PROPERTY_KEY_ID, asyncContext->ohosAccountInfo.uid_)) {
122         ACCOUNT_LOGE("Failed to get DistributedInfo's %{public}s property", PROPERTY_KEY_ID.c_str());
123         std::string errMsg = "The type of " + PROPERTY_KEY_ID + " must be string";
124         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
125         return false;
126     }
127     if (!GetOptionalStringPropertyByKey(env, object, PROPERTY_KEY_NICKNAME, asyncContext->ohosAccountInfo.nickname_)) {
128         ACCOUNT_LOGE("Failed to get DistributedInfo's %{public}s property", PROPERTY_KEY_NICKNAME.c_str());
129         std::string errMsg = "The type of " + PROPERTY_KEY_NICKNAME + " must be string";
130         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
131         return false;
132     }
133     if (!GetOptionalStringPropertyByKey(env, object, PROPERTY_KEY_AVATAR, asyncContext->ohosAccountInfo.avatar_)) {
134         ACCOUNT_LOGE("Failed to get DistributedInfo's %{public}s property", PROPERTY_KEY_AVATAR.c_str());
135         std::string errMsg = "The type of " + PROPERTY_KEY_AVATAR + " must be string";
136         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
137         return false;
138     }
139     bool hasProp = false;
140     napi_has_named_property(env, object, "scalableData", &hasProp);
141     AAFwk::WantParams params;
142     if (hasProp) {
143         napi_value value = nullptr;
144         napi_get_named_property(env, object, "scalableData", &value);
145         napi_valuetype valuetype = napi_undefined;
146         NAPI_CALL_BASE(env, napi_typeof(env, value, &valuetype), false);
147         if ((valuetype == napi_undefined) || (valuetype == napi_null)) {
148             ACCOUNT_LOGI("the scalableData is undefined or null");
149         } else {
150             if (!AppExecFwk::UnwrapWantParams(env, value, params)) {
151                 ACCOUNT_LOGE("Failed to get DistributedInfo's %{public}s property", PROPERTY_KEY_SCALABLE.c_str());
152                 std::string errMsg = "The type of " + PROPERTY_KEY_SCALABLE + " must be object";
153                 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr);
154                 return false;
155             }
156         }
157     }
158     asyncContext->ohosAccountInfo.scalableData_.SetParams(params);
159     return true;
160 }
161 
ParseInfoAndEvent(napi_env env,napi_value argv,DistributedAccountAsyncContext * asyncContext)162 static bool ParseInfoAndEvent(napi_env env, napi_value argv, DistributedAccountAsyncContext *asyncContext)
163 {
164     if (!GetAccountInfo(env, argv, asyncContext)) {
165         ACCOUNT_LOGE("Failed to get account info");
166         return false;
167     }
168     if (!GetStringPropertyByKey(env, argv, PROPERTY_KEY_EVENT, asyncContext->event)) {
169         ACCOUNT_LOGE("Failed to get DistributedInfo's %{public}s property", PROPERTY_KEY_EVENT.c_str());
170         return false;
171     }
172     return true;
173 }
174 
ParseUpdateOhosAccountInfoWithTwoArgs(napi_env env,napi_value * argv,DistributedAccountAsyncContext * asyncContext,const int32_t size)175 static bool ParseUpdateOhosAccountInfoWithTwoArgs(
176     napi_env env, napi_value *argv, DistributedAccountAsyncContext *asyncContext, const int32_t size)
177 {
178     if (size != PARAM_TWO) {
179         ACCOUNT_LOGE("argv size is not two");
180         return false;
181     }
182     napi_valuetype valueType = napi_undefined;
183     napi_typeof(env, argv[PARAM_ONE], &valueType);
184     if (valueType == napi_object) {
185         if (!GetIntProperty(env, argv[PARAM_ZERO], asyncContext->localId)) {
186             ACCOUNT_LOGE("Get localId failed");
187             return false;
188         }
189         asyncContext->withLocalId = true;
190         return ParseInfoAndEvent(env, argv[PARAM_ONE], asyncContext);
191     }
192     if (valueType == napi_function) {
193         if (!GetCallbackProperty(env, argv[PARAM_ONE], asyncContext->callbackRef, PARAM_ONE)) {
194             ACCOUNT_LOGE("Failed to get callback property");
195             return false;
196         }
197         return ParseInfoAndEvent(env, argv[PARAM_ZERO], asyncContext);
198     }
199     return false;
200 }
201 
ParseUpdateOhosAccountInfoAsyncContext(napi_env env,napi_callback_info cbInfo,DistributedAccountAsyncContext * asyncContext)202 bool ParseUpdateOhosAccountInfoAsyncContext(
203     napi_env env, napi_callback_info cbInfo, DistributedAccountAsyncContext *asyncContext)
204 {
205     size_t argc = PARAM_THREE;
206     napi_value argv[PARAM_THREE] = {nullptr};
207     napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr);
208     if (argc == PARAM_ZERO) {
209         ACCOUNT_LOGE("paramter should be at least one");
210         return false;
211     }
212     if (argc == PARAM_ONE) {
213         return ParseInfoAndEvent(env, argv[PARAM_ZERO], asyncContext);
214     }
215     if (argc == PARAM_TWO) {
216         return ParseUpdateOhosAccountInfoWithTwoArgs(env, argv, asyncContext, PARAM_TWO);
217     }
218     if (!GetCallbackProperty(env, argv[PARAM_TWO], asyncContext->callbackRef, PARAM_ONE)) {
219         ACCOUNT_LOGE("Failed to get callback property");
220         return false;
221     }
222     if (!GetIntProperty(env, argv[PARAM_ZERO], asyncContext->localId)) {
223         ACCOUNT_LOGE("Get localId failed");
224         return false;
225     }
226     asyncContext->withLocalId = true;
227     return ParseInfoAndEvent(env, argv[PARAM_ONE], asyncContext);
228 }
229 
ProcessCallbackOrPromise(napi_env env,const DistributedAccountAsyncContext * asyncContext,napi_value err,napi_value data)230 void ProcessCallbackOrPromise(
231     napi_env env, const DistributedAccountAsyncContext *asyncContext, napi_value err, napi_value data)
232 {
233     if (asyncContext->deferred != nullptr) {
234         if (asyncContext->errCode == ERR_OK) {
235             napi_resolve_deferred(env, asyncContext->deferred, data);
236         } else {
237             napi_reject_deferred(env, asyncContext->deferred, err);
238         }
239     } else {
240         napi_value args[RESULT_COUNT] = { err, data };
241         napi_value callback = nullptr;
242         napi_get_reference_value(env, asyncContext->callbackRef, &callback);
243         napi_value returnVal = nullptr;
244         napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &returnVal);
245     }
246 }
247 
ProcessSetNamedProperty(napi_env env,const DistributedAccountAsyncContext * asyncContext)248 void ProcessSetNamedProperty(napi_env env, const DistributedAccountAsyncContext *asyncContext)
249 {
250     napi_value result[RESULT_COUNT] = {0};
251     if (asyncContext->errCode == ERR_OK) {
252         if (asyncContext->throwErr) {
253             napi_get_null(env, &result[0]);
254         } else {
255             napi_get_undefined(env, &result[0]);
256         }
257         napi_create_object(env, &result[1]);
258         napi_value value = nullptr;
259         napi_create_string_utf8(env, asyncContext->ohosAccountInfo.name_.c_str(),
260                                 asyncContext->ohosAccountInfo.name_.size(), &value);
261         napi_set_named_property(env, result[1], PROPERTY_KEY_NAME.c_str(), value);
262         napi_create_string_utf8(env, asyncContext->ohosAccountInfo.uid_.c_str(),
263                                 asyncContext->ohosAccountInfo.uid_.size(), &value);
264         napi_set_named_property(env, result[1], PROPERTY_KEY_ID.c_str(), value);
265         napi_create_string_utf8(env, asyncContext->event.c_str(), asyncContext->event.size(), &value);
266         napi_set_named_property(env, result[1], PROPERTY_KEY_EVENT.c_str(), value);
267         napi_create_string_utf8(env, asyncContext->ohosAccountInfo.nickname_.c_str(),
268                                 asyncContext->ohosAccountInfo.nickname_.size(), &value);
269         napi_set_named_property(env, result[1], PROPERTY_KEY_NICKNAME.c_str(), value);
270         napi_create_string_utf8(env, asyncContext->ohosAccountInfo.avatar_.c_str(),
271                                 asyncContext->ohosAccountInfo.avatar_.size(), &value);
272         napi_set_named_property(env, result[1], PROPERTY_KEY_AVATAR.c_str(), value);
273         napi_create_int32(env, asyncContext->ohosAccountInfo.status_, &value);
274         napi_set_named_property(env, result[1], PROPERTY_KEY_STATUS.c_str(), value);
275         napi_value scalable = nullptr;
276         napi_create_object(env, &scalable);
277         scalable = AppExecFwk::WrapWantParams(env, (asyncContext->ohosAccountInfo.scalableData_).GetParams());
278         napi_set_named_property(env, result[1], PROPERTY_KEY_SCALABLE.c_str(), scalable);
279     } else {
280         if (asyncContext->throwErr) {
281             result[0] = GenerateBusinessError(env, asyncContext->errCode);
282             napi_get_null(env, &result[1]);
283         } else {
284             napi_value message = nullptr;
285             napi_create_string_utf8(env, "query ohos account info failed", NAPI_AUTO_LENGTH, &message);
286             napi_create_error(env, nullptr, message, &result[0]);
287             napi_get_undefined(env, &result[1]);
288         }
289     }
290     ProcessCallbackOrPromise(env, asyncContext, result[0], result[1]);
291 }
292 
Init(napi_env env,napi_value exports)293 napi_value NapiDistributedAccount::Init(napi_env env, napi_value exports)
294 {
295     napi_property_descriptor descriptor[] = {
296         DECLARE_NAPI_FUNCTION("getDistributedAccountAbility", GetDistributedAccountAbility),
297         DECLARE_NAPI_PROPERTY("DistributedAccountStatus", DistributedAccountStatusConstructor(env)),
298     };
299     napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor);
300 
301     napi_property_descriptor properties[] = {
302         DECLARE_NAPI_FUNCTION("queryOsAccountDistributedInfo", QueryOsAccountDistributedInfo),
303         DECLARE_NAPI_FUNCTION("getOsAccountDistributedInfo", GetOsAccountDistributedInfo),
304         DECLARE_NAPI_FUNCTION("updateOsAccountDistributedInfo", UpdateOsAccountDistributedInfo),
305         DECLARE_NAPI_FUNCTION("setOsAccountDistributedInfo", SetOsAccountDistributedInfo),
306         DECLARE_NAPI_FUNCTION("getOsAccountDistributedInfoByLocalId", GetOsAccountDistributedInfo),
307         DECLARE_NAPI_FUNCTION("setOsAccountDistributedInfoByLocalId", SetOsAccountDistributedInfo),
308     };
309     napi_value cons = nullptr;
310     napi_define_class(env, DISTRIBUTED_ACCOUNT_CLASS_NAME.c_str(), DISTRIBUTED_ACCOUNT_CLASS_NAME.size(),
311         JsConstructor, nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons);
312     napi_create_reference(env, cons, 1, &distributedAccountRef_);
313     napi_set_named_property(env, exports, DISTRIBUTED_ACCOUNT_CLASS_NAME.c_str(), cons);
314 
315     return exports;
316 }
317 
JsConstructor(napi_env env,napi_callback_info cbinfo)318 napi_value NapiDistributedAccount::JsConstructor(napi_env env, napi_callback_info cbinfo)
319 {
320     napi_value thisVar = nullptr;
321     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr);
322     return thisVar;
323 }
324 
GetDistributedAccountAbility(napi_env env,napi_callback_info cbInfo)325 napi_value NapiDistributedAccount::GetDistributedAccountAbility(napi_env env, napi_callback_info cbInfo)
326 {
327     napi_value instance = nullptr;
328     napi_value cons = nullptr;
329     if (napi_get_reference_value(env, distributedAccountRef_, &cons) != napi_ok) {
330         return nullptr;
331     }
332 
333     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
334         return nullptr;
335     }
336 
337     return instance;
338 }
339 
QueryOsAccountDistributedInfo(napi_env env,napi_callback_info cbInfo)340 napi_value NapiDistributedAccount::QueryOsAccountDistributedInfo(napi_env env, napi_callback_info cbInfo)
341 {
342     return QueryOhosAccountInfo(env, cbInfo, false);
343 }
344 
GetOsAccountDistributedInfo(napi_env env,napi_callback_info cbInfo)345 napi_value NapiDistributedAccount::GetOsAccountDistributedInfo(napi_env env, napi_callback_info cbInfo)
346 {
347     return QueryOhosAccountInfo(env, cbInfo, true);
348 }
349 
QueryOhosAccountInfoExecuteCB(napi_env env,void * data)350 static void QueryOhosAccountInfoExecuteCB(napi_env env, void *data)
351 {
352     DistributedAccountAsyncContext *asyncContext = reinterpret_cast<DistributedAccountAsyncContext *>(data);
353     if (!asyncContext->throwErr) {
354         std::pair<bool, OhosAccountInfo> accountInfo = OhosAccountKits::GetInstance().QueryOhosAccountInfo();
355         if (accountInfo.first) {
356             asyncContext->ohosAccountInfo.name_ = accountInfo.second.name_;
357             asyncContext->ohosAccountInfo.uid_ = accountInfo.second.uid_;
358             asyncContext->ohosAccountInfo.status_ = accountInfo.second.status_;
359             asyncContext->errCode = napi_ok;
360         } else {
361             asyncContext->errCode = napi_generic_failure;
362         }
363     } else if (!asyncContext->withLocalId) {
364         asyncContext->errCode = OhosAccountKits::GetInstance().GetOhosAccountInfo(asyncContext->ohosAccountInfo);
365     } else {
366         asyncContext->errCode = OhosAccountKits::GetInstance().GetOhosAccountInfoByUserId(
367             asyncContext->localId, asyncContext->ohosAccountInfo);
368     }
369 }
370 
QueryOhosAccountInfoCompletedCB(napi_env env,napi_status status,void * data)371 static void QueryOhosAccountInfoCompletedCB(napi_env env, napi_status status, void *data)
372 {
373     DistributedAccountAsyncContext *asyncContext = reinterpret_cast<DistributedAccountAsyncContext *>(data);
374     ProcessSetNamedProperty(env, asyncContext);
375     napi_delete_async_work(env, asyncContext->work);
376     delete asyncContext;
377 }
378 
QueryOhosAccountInfo(napi_env env,napi_callback_info cbInfo,bool throwErr)379 napi_value NapiDistributedAccount::QueryOhosAccountInfo(napi_env env, napi_callback_info cbInfo, bool throwErr)
380 {
381     auto *asyncContext = new (std::nothrow) DistributedAccountAsyncContext(env);
382     if (asyncContext == nullptr) {
383         ACCOUNT_LOGE("insufficient memory for asyncContext!");
384         return nullptr;
385     }
386     std::unique_ptr<DistributedAccountAsyncContext> contextPtr(asyncContext);
387     asyncContext->throwErr = throwErr;
388     if (!ParseQueryOhosAccountInfoAsyncContext(env, cbInfo, asyncContext) && throwErr) {
389         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, true);
390         return nullptr;
391     }
392     napi_value result = nullptr;
393     if (asyncContext->callbackRef == nullptr) {
394         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
395     }
396     napi_value resource = nullptr;
397     napi_create_string_utf8(env, "QueryOhosAccountInfo", NAPI_AUTO_LENGTH, &resource);
398     NAPI_CALL(env, napi_create_async_work(env,
399         nullptr,
400         resource,
401         QueryOhosAccountInfoExecuteCB,
402         QueryOhosAccountInfoCompletedCB,
403         reinterpret_cast<void *>(asyncContext), &asyncContext->work));
404     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_default));
405     contextPtr.release();
406     return result;
407 }
408 
UpdateOsAccountDistributedInfo(napi_env env,napi_callback_info cbInfo)409 napi_value NapiDistributedAccount::UpdateOsAccountDistributedInfo(napi_env env, napi_callback_info cbInfo)
410 {
411     return UpdateOhosAccountInfo(env, cbInfo, false);
412 }
413 
SetOsAccountDistributedInfo(napi_env env,napi_callback_info cbInfo)414 napi_value NapiDistributedAccount::SetOsAccountDistributedInfo(napi_env env, napi_callback_info cbInfo)
415 {
416     return UpdateOhosAccountInfo(env, cbInfo, true);
417 }
418 
UpdateOhosAccountInfoExecuteCB(napi_env env,void * data)419 static void UpdateOhosAccountInfoExecuteCB(napi_env env, void *data)
420 {
421     DistributedAccountAsyncContext *context = reinterpret_cast<DistributedAccountAsyncContext *>(data);
422     if (!context->throwErr) {
423         context->errCode = OhosAccountKits::GetInstance().UpdateOhosAccountInfo(context->ohosAccountInfo.name_,
424             context->ohosAccountInfo.uid_, context->event) ? napi_ok: napi_generic_failure;
425     } else if (context->withLocalId) {
426         context->errCode = OhosAccountKits::GetInstance().SetOhosAccountInfoByUserId(
427             context->localId, context->ohosAccountInfo, context->event);
428     } else {
429         context->errCode = OhosAccountKits::GetInstance().SetOhosAccountInfo(context->ohosAccountInfo, context->event);
430     }
431 }
432 
UpdateOhosAccountInfoCompletedCB(napi_env env,napi_status status,void * data)433 static void UpdateOhosAccountInfoCompletedCB(napi_env env, napi_status status, void *data)
434 {
435     DistributedAccountAsyncContext *asyncContext = reinterpret_cast<DistributedAccountAsyncContext *>(data);
436     napi_value result[RESULT_COUNT] = {0};
437     if (asyncContext->errCode == ERR_OK) {
438         if (asyncContext->throwErr) {
439             napi_get_null(env, &result[0]);
440             napi_get_null(env, &result[1]);
441         } else {
442             napi_get_undefined(env, &result[1]);
443         }
444     } else if (asyncContext->throwErr) {
445         result[0] = GenerateBusinessError(env, asyncContext->errCode);
446     } else {
447         napi_value message = nullptr;
448         napi_create_string_utf8(env, "Update distributed account info failed", NAPI_AUTO_LENGTH, &message);
449         napi_create_error(env, nullptr, message, &result[0]);
450     }
451     ProcessCallbackOrPromise(env, asyncContext, result[0], result[1]);
452     napi_delete_async_work(env, asyncContext->work);
453     delete asyncContext;
454 }
455 
UpdateOhosAccountInfo(napi_env env,napi_callback_info cbInfo,bool throwErr)456 napi_value NapiDistributedAccount::UpdateOhosAccountInfo(napi_env env, napi_callback_info cbInfo, bool throwErr)
457 {
458     auto *asyncContext = new (std::nothrow) DistributedAccountAsyncContext(env);
459     if (asyncContext == nullptr) {
460         ACCOUNT_LOGE("insufficient memory for asyncContext!");
461         return nullptr;
462     }
463     asyncContext->throwErr = throwErr;
464     std::unique_ptr<DistributedAccountAsyncContext> contextPtr(asyncContext);
465     if (!ParseUpdateOhosAccountInfoAsyncContext(env, cbInfo, asyncContext) && throwErr) {
466         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, true);
467         return nullptr;
468     }
469     napi_value result = nullptr;
470     if (asyncContext->callbackRef == nullptr) {
471         NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
472     }
473     napi_value resource = nullptr;
474     napi_create_string_utf8(env, "UpdateOhosAccountInfo", NAPI_AUTO_LENGTH, &resource);
475     NAPI_CALL(env, napi_create_async_work(env,
476         nullptr,
477         resource,
478         UpdateOhosAccountInfoExecuteCB,
479         UpdateOhosAccountInfoCompletedCB,
480         reinterpret_cast<void *>(asyncContext), &asyncContext->work));
481     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_default));
482     contextPtr.release();
483     return result;
484 }
485 } // namespace AccountJsKit
486 } // namespace OHOS
487 
488 EXTERN_C_START
489 /*
490  * function for module exports
491  */
Init(napi_env env,napi_value exports)492 static napi_value Init(napi_env env, napi_value exports)
493 {
494     return OHOS::AccountJsKit::NapiDistributedAccount::Init(env, exports);
495 }
496 EXTERN_C_END
497 
498 /*
499  * module define
500  */
501 static napi_module _module = {
502     .nm_version = 1,
503     .nm_flags = 0,
504     .nm_filename = nullptr,
505     .nm_register_func = Init,
506     .nm_modname = "account.distributedAccount",
507     .nm_priv = ((void*)0),
508     .reserved = {0}
509 };
510 /*
511  * module register
512  */
Register()513 extern "C" __attribute__((constructor)) void Register()
514 {
515     napi_module_register(&_module);
516 }
517