1 /*
2 * Copyright (c) 2022-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_edm_common.h"
17
18 #include "edm_errors.h"
19 #include "edm_log.h"
20 #include "js_native_api.h"
21 #include "js_native_api_types.h"
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "napi/native_node_api.h"
25 #include "napi_edm_error.h"
26
27 namespace OHOS {
28 namespace EDM {
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)29 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
30 {
31 napi_valuetype valueType = napi_undefined;
32 napi_typeof(env, value, &valueType);
33 return valueType == targetType;
34 }
35
ParseElementName(napi_env env,AppExecFwk::ElementName & elementName,napi_value args)36 bool ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args)
37 {
38 napi_valuetype valueType;
39 NAPI_CALL_BASE(env, napi_typeof(env, args, &valueType), false);
40 if (valueType != napi_object) {
41 EDMLOGE("Parameter element valueType error");
42 return false;
43 }
44 std::string bundleName;
45 std::string abilityName;
46 if (!JsObjectToString(env, args, "bundleName", true, bundleName) ||
47 !JsObjectToString(env, args, "abilityName", true, abilityName)) {
48 EDMLOGE("Parameter element bundleName error");
49 return false;
50 }
51 EDMLOGD("ParseElementName bundleName %{public}s ", bundleName.c_str());
52 EDMLOGD("ParseElementName abilityname %{public}s", abilityName.c_str());
53
54 elementName.SetBundleName(bundleName);
55 elementName.SetAbilityName(abilityName);
56 return true;
57 }
58
ParseLong(napi_env env,int64_t & param,napi_value args)59 bool ParseLong(napi_env env, int64_t ¶m, napi_value args)
60 {
61 napi_valuetype valueType = napi_undefined;
62 if (napi_typeof(env, args, &valueType) != napi_ok || valueType != napi_number ||
63 napi_get_value_int64(env, args, ¶m) != napi_ok) {
64 EDMLOGE("Wrong argument type. int64 expected.");
65 return false;
66 }
67 return true;
68 }
69
ParseInt(napi_env env,int32_t & param,napi_value args)70 bool ParseInt(napi_env env, int32_t ¶m, napi_value args)
71 {
72 napi_valuetype valueType = napi_undefined;
73 if (napi_typeof(env, args, &valueType) != napi_ok || valueType != napi_number ||
74 napi_get_value_int32(env, args, ¶m) != napi_ok) {
75 EDMLOGE("Wrong argument type. int32 expected.");
76 return false;
77 }
78 return true;
79 }
80
ParseCallback(napi_env env,napi_ref & param,napi_value args)81 bool ParseCallback(napi_env env, napi_ref ¶m, napi_value args)
82 {
83 napi_valuetype valueType = napi_undefined;
84 if (napi_typeof(env, args, &valueType) != napi_ok || valueType != napi_function ||
85 napi_create_reference(env, args, NAPI_RETURN_ONE, ¶m) != napi_ok) {
86 EDMLOGE("Wrong argument type. napi_function expected.");
87 return false;
88 }
89 return true;
90 }
91
ParseUint(napi_env env,uint32_t & param,napi_value args)92 bool ParseUint(napi_env env, uint32_t ¶m, napi_value args)
93 {
94 napi_valuetype valueType = napi_undefined;
95 if (napi_typeof(env, args, &valueType) != napi_ok || valueType != napi_number ||
96 napi_get_value_uint32(env, args, ¶m) != napi_ok) {
97 EDMLOGE("Wrong argument type. uint32 expected.");
98 return false;
99 }
100 return true;
101 }
102
ParseBool(napi_env env,bool & param,napi_value args)103 bool ParseBool(napi_env env, bool ¶m, napi_value args)
104 {
105 napi_valuetype valueType = napi_undefined;
106 if (napi_typeof(env, args, &valueType) != napi_ok || valueType != napi_boolean ||
107 napi_get_value_bool(env, args, ¶m) != napi_ok) {
108 EDMLOGE("Wrong argument type. bool expected.");
109 return false;
110 }
111 return true;
112 }
113
ParseString(napi_env env,std::string & param,napi_value args)114 bool ParseString(napi_env env, std::string ¶m, napi_value args)
115 {
116 napi_valuetype valuetype;
117 if (napi_typeof(env, args, &valuetype) != napi_ok || valuetype != napi_string ||
118 !GetStringFromNAPI(env, args, param)) {
119 EDMLOGE("can not get string value");
120 return false;
121 }
122 EDMLOGD("ParseString param = %{public}s.", param.c_str());
123 return true;
124 }
125
GetStringFromNAPI(napi_env env,napi_value value,std::string & resultStr)126 bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr)
127 {
128 std::string result;
129 size_t size = 0;
130
131 if (napi_get_value_string_utf8(env, value, nullptr, NAPI_RETURN_ZERO, &size) != napi_ok) {
132 EDMLOGE("can not get string size");
133 return false;
134 }
135 result.reserve(size + NAPI_RETURN_ONE);
136 result.resize(size);
137 if (napi_get_value_string_utf8(env, value, result.data(), (size + NAPI_RETURN_ONE), &size) != napi_ok) {
138 EDMLOGE("can not get string value");
139 return false;
140 }
141 resultStr = result;
142 return true;
143 }
144
ParseCharArray(napi_env env,napi_value args,size_t maxLength,char * param)145 bool ParseCharArray(napi_env env, napi_value args, size_t maxLength, char *param)
146 {
147 napi_valuetype valuetype;
148 if (napi_typeof(env, args, &valuetype) != napi_ok || valuetype != napi_string) {
149 EDMLOGE("can not get string value");
150 return false;
151 }
152 size_t size = 0;
153 if (napi_get_value_string_utf8(env, args, nullptr, NAPI_RETURN_ZERO, &size) != napi_ok) {
154 EDMLOGE("can not get string size");
155 return false;
156 }
157 if (size >= maxLength) {
158 EDMLOGE("string size too long");
159 return false;
160 }
161 if (napi_get_value_string_utf8(env, args, param, (size + NAPI_RETURN_ONE), &size) != napi_ok) {
162 EDMLOGE("can not get string value");
163 return false;
164 }
165 return true;
166 }
167
ParseStringArray(napi_env env,std::vector<std::string> & stringArray,napi_value args)168 napi_value ParseStringArray(napi_env env, std::vector<std::string> &stringArray, napi_value args)
169 {
170 EDMLOGD("begin to parse string array");
171 bool isArray = false;
172 NAPI_CALL(env, napi_is_array(env, args, &isArray));
173 if (!isArray) {
174 return nullptr;
175 }
176 uint32_t arrayLength = 0;
177 NAPI_CALL(env, napi_get_array_length(env, args, &arrayLength));
178 EDMLOGD("length=%{public}ud", arrayLength);
179 for (uint32_t j = 0; j < arrayLength; j++) {
180 napi_value value = nullptr;
181 NAPI_CALL(env, napi_get_element(env, args, j, &value));
182 napi_valuetype valueType = napi_undefined;
183 NAPI_CALL(env, napi_typeof(env, value, &valueType));
184 if (valueType != napi_string) {
185 stringArray.clear();
186 return nullptr;
187 }
188 std::string str;
189 GetStringFromNAPI(env, value, str);
190 stringArray.push_back(str);
191 }
192 // create result code
193 napi_value result;
194 napi_status status = napi_create_int32(env, NAPI_RETURN_ONE, &result);
195 if (status != napi_ok) {
196 return nullptr;
197 }
198 return result;
199 }
200
JsObjectToInt(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,int32_t & result)201 bool JsObjectToInt(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, int32_t &result)
202 {
203 bool hasProperty = false;
204 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
205 EDMLOGE("get js property failed.");
206 return false;
207 }
208 if (isNecessaryProp && !hasProperty) {
209 return false;
210 }
211 if (hasProperty) {
212 napi_value prop = nullptr;
213 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseInt(env, result, prop);
214 }
215 return true;
216 }
217
JsObjectToUint(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,uint32_t & result)218 bool JsObjectToUint(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, uint32_t &result)
219 {
220 bool hasProperty = false;
221 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
222 EDMLOGE("get js property failed.");
223 return false;
224 }
225 if (isNecessaryProp && !hasProperty) {
226 return false;
227 }
228 if (hasProperty) {
229 napi_value prop = nullptr;
230 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseUint(env, result, prop);
231 }
232 return true;
233 }
234
JsObjectToBool(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,bool & result)235 bool JsObjectToBool(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, bool &result)
236 {
237 bool hasProperty = false;
238 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
239 EDMLOGE("get js property failed.");
240 return false;
241 }
242 if (isNecessaryProp && !hasProperty) {
243 return false;
244 }
245 if (hasProperty) {
246 napi_value prop = nullptr;
247 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseBool(env, result, prop);
248 }
249 return true;
250 }
251
JsObjectToString(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,std::string & resultStr)252 bool JsObjectToString(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp,
253 std::string &resultStr)
254 {
255 bool hasProperty = false;
256 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
257 EDMLOGE("get js property failed.");
258 return false;
259 }
260 if (isNecessaryProp && !hasProperty) {
261 return false;
262 }
263 if (hasProperty) {
264 napi_value prop = nullptr;
265 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseString(env, resultStr, prop);
266 }
267 return true;
268 }
269
JsObjectToCharArray(napi_env env,napi_value object,const char * filedStr,int maxLength,bool isNecessaryProp,char * result)270 bool JsObjectToCharArray(napi_env env, napi_value object, const char *filedStr, int maxLength, bool isNecessaryProp,
271 char *result)
272 {
273 bool hasProperty = false;
274 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
275 EDMLOGE("get js property failed.");
276 return false;
277 }
278 if (isNecessaryProp && !hasProperty) {
279 return false;
280 }
281 if (hasProperty) {
282 napi_value prop = nullptr;
283 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok &&
284 ParseCharArray(env, prop, maxLength, result);
285 }
286 return true;
287 }
288
GetJsProperty(napi_env env,napi_value object,const char * filedStr,napi_value & result)289 bool GetJsProperty(napi_env env, napi_value object, const char *filedStr, napi_value &result)
290 {
291 bool hasProperty = false;
292 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok || !hasProperty ||
293 napi_get_named_property(env, object, filedStr, &result) != napi_ok) {
294 EDMLOGE("Js has no property");
295 return false;
296 }
297 return true;
298 }
299
JsObjectToU8Vector(napi_env env,napi_value object,const char * fieldStr,std::vector<uint8_t> & certVector)300 bool JsObjectToU8Vector(napi_env env, napi_value object, const char *fieldStr, std::vector<uint8_t> &certVector)
301 {
302 napi_value certEntry;
303 if (!GetJsProperty(env, object, fieldStr, certEntry)) {
304 return false;
305 }
306 bool isTypedArray = false;
307 if (napi_is_typedarray(env, certEntry, &isTypedArray) != napi_ok || !isTypedArray) {
308 EDMLOGE("js property is not typedarray");
309 return false;
310 }
311 size_t length = 0;
312 size_t offset = 0;
313 napi_typedarray_type type;
314 napi_value buffer = nullptr;
315 void *data = nullptr;
316 if (napi_get_typedarray_info(env, certEntry, &type, &length, &data, &buffer, &offset) != napi_ok ||
317 type != napi_uint8_array || buffer == nullptr) {
318 EDMLOGE("js object type is not uint8 array");
319 return false;
320 }
321 if (length > NAPI_MAX_DATA_LEN) {
322 EDMLOGE("uint8 array range failed");
323 return false;
324 }
325 if (data == nullptr) {
326 EDMLOGE("uint8 array data failed");
327 return false;
328 }
329 certVector.clear();
330 certVector.assign(static_cast<uint8_t *>(data), (static_cast<uint8_t *>(data) + length));
331 return true;
332 }
333
JsObjectToStringVector(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,std::vector<std::string> & vec)334 bool JsObjectToStringVector(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp,
335 std::vector<std::string> &vec)
336 {
337 bool hasProperty = false;
338 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
339 EDMLOGE("get js property failed.");
340 return false;
341 }
342 if (isNecessaryProp && !hasProperty) {
343 EDMLOGE("no yaobaohua hasProperty.");
344 return false;
345 }
346 if (hasProperty) {
347 napi_value prop = nullptr;
348 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseStringArray(env, vec, prop);
349 }
350 return true;
351 }
352
NativeVoidCallbackComplete(napi_env env,napi_status status,void * data)353 void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data)
354 {
355 if (data == nullptr) {
356 EDMLOGE("data is nullptr");
357 return;
358 }
359 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
360 napi_value error = nullptr;
361 if (asyncCallbackInfo->callback == nullptr) {
362 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
363 if (asyncCallbackInfo->ret == ERR_OK) {
364 napi_get_null(env, &error);
365 napi_resolve_deferred(env, asyncCallbackInfo->deferred, error);
366 } else {
367 if (asyncCallbackInfo->innerCodeMsg.empty()) {
368 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
369 } else {
370 napi_reject_deferred(env, asyncCallbackInfo->deferred,
371 CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg));
372 }
373 }
374 } else {
375 EDMLOGD("asyncCallbackInfo->callback != nullptr");
376 if (asyncCallbackInfo->ret == ERR_OK) {
377 napi_get_null(env, &error);
378 } else {
379 if (asyncCallbackInfo->innerCodeMsg.empty()) {
380 error = CreateError(env, asyncCallbackInfo->ret);
381 } else {
382 error = CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg);
383 }
384 }
385 napi_value callback = nullptr;
386 napi_value result = nullptr;
387 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
388 napi_call_function(env, nullptr, callback, ARGS_SIZE_ONE, &error, &result);
389 napi_delete_reference(env, asyncCallbackInfo->callback);
390 }
391 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
392 delete asyncCallbackInfo;
393 }
394
HandleAsyncWork(napi_env env,AsyncCallbackInfo * context,const std::string & workName,napi_async_execute_callback execute,napi_async_complete_callback complete)395 napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, const std::string &workName,
396 napi_async_execute_callback execute, napi_async_complete_callback complete)
397 {
398 napi_value result = nullptr;
399 if (context->callback == nullptr) {
400 napi_create_promise(env, &context->deferred, &result);
401 } else {
402 napi_get_undefined(env, &result);
403 }
404 napi_value resource = nullptr;
405 napi_get_undefined(env, &resource);
406 napi_value resourceName = nullptr;
407 napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName);
408 napi_create_async_work(env, resource, resourceName, execute, complete, static_cast<void *>(context),
409 &context->asyncWork);
410 napi_queue_async_work(env, context->asyncWork);
411 return result;
412 }
413
NativeBoolCallbackComplete(napi_env env,napi_status status,void * data)414 void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data)
415 {
416 if (data == nullptr) {
417 EDMLOGE("data is nullptr");
418 return;
419 }
420 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
421 if (asyncCallbackInfo->deferred != nullptr) {
422 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
423 if (asyncCallbackInfo->ret == ERR_OK) {
424 EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet);
425 napi_value result = nullptr;
426 napi_get_boolean(env, asyncCallbackInfo->boolRet, &result);
427 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
428 } else {
429 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
430 }
431 } else {
432 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
433 if (asyncCallbackInfo->ret == ERR_OK) {
434 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
435 EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet);
436 napi_get_boolean(env, asyncCallbackInfo->boolRet, &callbackValue[ARR_INDEX_ONE]);
437 } else {
438 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
439 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
440 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
441 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
442 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
443 }
444 napi_value callback = nullptr;
445 napi_value result = nullptr;
446 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
447 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
448 napi_delete_reference(env, asyncCallbackInfo->callback);
449 }
450 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
451 delete asyncCallbackInfo;
452 }
453
NativeNumberCallbackComplete(napi_env env,napi_status status,void * data)454 void NativeNumberCallbackComplete(napi_env env, napi_status status, void *data)
455 {
456 if (data == nullptr) {
457 EDMLOGE("data is nullptr");
458 return;
459 }
460 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
461 if (asyncCallbackInfo->deferred != nullptr) {
462 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
463 if (asyncCallbackInfo->ret == ERR_OK) {
464 EDMLOGD("asyncCallbackInfo->intRet = %{public}d", asyncCallbackInfo->intRet);
465 napi_value result = nullptr;
466 napi_create_int32(env, asyncCallbackInfo->intRet, &result);
467 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
468 } else {
469 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
470 }
471 } else {
472 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
473 if (asyncCallbackInfo->ret == ERR_OK) {
474 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
475 EDMLOGD("asyncCallbackInfo->intRet = %{public}d", asyncCallbackInfo->intRet);
476 napi_create_int32(env, asyncCallbackInfo->intRet, &callbackValue[ARR_INDEX_ONE]);
477 } else {
478 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
479 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
480 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
481 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
482 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
483 }
484 napi_value callback = nullptr;
485 napi_value result = nullptr;
486 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
487 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
488 napi_delete_reference(env, asyncCallbackInfo->callback);
489 }
490 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
491 delete asyncCallbackInfo;
492 }
493
NativeStringCallbackComplete(napi_env env,napi_status status,void * data)494 void NativeStringCallbackComplete(napi_env env, napi_status status, void *data)
495 {
496 if (data == nullptr) {
497 EDMLOGE("data is nullptr");
498 return;
499 }
500 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
501 if (asyncCallbackInfo->deferred != nullptr) {
502 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
503 if (asyncCallbackInfo->ret == ERR_OK) {
504 EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str());
505 napi_value result = nullptr;
506 napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH, &result);
507 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
508 } else {
509 if (asyncCallbackInfo->innerCodeMsg.empty()) {
510 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
511 } else {
512 napi_reject_deferred(env, asyncCallbackInfo->deferred,
513 CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg));
514 }
515 }
516 } else {
517 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
518 if (asyncCallbackInfo->ret == ERR_OK) {
519 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
520 EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str());
521 napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH,
522 &callbackValue[ARR_INDEX_ONE]);
523 } else {
524 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
525 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
526 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
527 if (asyncCallbackInfo->innerCodeMsg.empty()) {
528 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
529 } else {
530 callbackValue[ARR_INDEX_ZERO] =
531 CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg);
532 }
533 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
534 }
535 napi_value callback = nullptr;
536 napi_value result = nullptr;
537 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
538 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
539 napi_delete_reference(env, asyncCallbackInfo->callback);
540 }
541 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
542 delete asyncCallbackInfo;
543 }
544
NativeArrayStringCallbackComplete(napi_env env,napi_status status,void * data)545 void NativeArrayStringCallbackComplete(napi_env env, napi_status status, void *data)
546 {
547 if (data == nullptr) {
548 EDMLOGE("data is nullptr");
549 return;
550 }
551 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
552 if (asyncCallbackInfo->deferred != nullptr) {
553 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
554 if (asyncCallbackInfo->ret == ERR_OK) {
555 napi_value result = nullptr;
556 napi_create_array(env, &result);
557 ConvertStringVectorToJS(env, asyncCallbackInfo->arrayStringRet, result);
558 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
559 } else {
560 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
561 }
562 } else {
563 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
564 if (asyncCallbackInfo->ret == ERR_OK) {
565 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
566 napi_create_array(env, &callbackValue[ARGS_SIZE_ONE]);
567 ConvertStringVectorToJS(env, asyncCallbackInfo->arrayStringRet, callbackValue[ARGS_SIZE_ONE]);
568 } else {
569 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
570 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
571 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
572 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
573 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
574 }
575 napi_value callback = nullptr;
576 napi_value result = nullptr;
577 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
578 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
579 napi_delete_reference(env, asyncCallbackInfo->callback);
580 }
581 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
582 delete asyncCallbackInfo;
583 }
584
ConvertStringVectorToJS(napi_env env,const std::vector<std::string> & stringVector,napi_value result)585 void ConvertStringVectorToJS(napi_env env, const std::vector<std::string> &stringVector, napi_value result)
586 {
587 EDMLOGD("vector size: %{public}zu", stringVector.size());
588 size_t idx = 0;
589 for (const auto &str : stringVector) {
590 napi_value obj = nullptr;
591 napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &obj);
592 napi_set_element(env, result, idx, obj);
593 idx++;
594 }
595 }
596
CheckAdminWithUserIdParamType(napi_env env,size_t argc,napi_value * argv,bool & hasCallback,bool & hasUserId)597 bool CheckAdminWithUserIdParamType(napi_env env, size_t argc, napi_value *argv, bool &hasCallback, bool &hasUserId)
598 {
599 if (!MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object)) {
600 EDMLOGE("CheckAdminWithUserIdParamType admin type check failed");
601 return false;
602 }
603 EDMLOGI("argc = %{public}zu", argc);
604 if (argc == ARGS_SIZE_ONE) {
605 hasCallback = false;
606 hasUserId = false;
607 EDMLOGI("hasCallback = false; hasUserId = false;");
608 return true;
609 }
610
611 if (argc == ARGS_SIZE_TWO) {
612 if (MatchValueType(env, argv[ARR_INDEX_ONE], napi_function)) {
613 hasCallback = true;
614 hasUserId = false;
615 EDMLOGI("hasCallback = true; hasUserId = false;");
616 return true;
617 } else {
618 hasCallback = false;
619 hasUserId = true;
620 EDMLOGI("hasCallback = false; hasUserId = true;");
621 return MatchValueType(env, argv[ARR_INDEX_ONE], napi_number);
622 }
623 }
624 hasCallback = true;
625 hasUserId = true;
626 EDMLOGI("hasCallback = true; hasUserId = true;");
627 return MatchValueType(env, argv[ARR_INDEX_ONE], napi_number) &&
628 MatchValueType(env, argv[ARR_INDEX_TWO], napi_function);
629 }
630 } // namespace EDM
631 } // namespace OHOS