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