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 EDMLOGE("napi object is not array.");
175 return nullptr;
176 }
177 uint32_t arrayLength = 0;
178 NAPI_CALL(env, napi_get_array_length(env, args, &arrayLength));
179 EDMLOGD("length=%{public}ud", arrayLength);
180 for (uint32_t j = 0; j < arrayLength; j++) {
181 napi_value value = nullptr;
182 NAPI_CALL(env, napi_get_element(env, args, j, &value));
183 napi_valuetype valueType = napi_undefined;
184 NAPI_CALL(env, napi_typeof(env, value, &valueType));
185 if (valueType != napi_string) {
186 stringArray.clear();
187 return nullptr;
188 }
189 std::string str;
190 GetStringFromNAPI(env, value, str);
191 stringArray.push_back(str);
192 }
193 // create result code
194 napi_value result;
195 napi_status status = napi_create_int32(env, NAPI_RETURN_ONE, &result);
196 if (status != napi_ok) {
197 return nullptr;
198 }
199 return result;
200 }
201
ParseElementArray(napi_env env,std::vector<AppExecFwk::ElementName> & elementArray,napi_value args)202 napi_value ParseElementArray(napi_env env, std::vector<AppExecFwk::ElementName> &elementArray, napi_value args)
203 {
204 EDMLOGD("begin to parse element array");
205 bool isArray = false;
206 NAPI_CALL(env, napi_is_array(env, args, &isArray));
207 if (!isArray) {
208 EDMLOGE("napi object is not array.");
209 return nullptr;
210 }
211 uint32_t arrayLength = 0;
212 NAPI_CALL(env, napi_get_array_length(env, args, &arrayLength));
213 EDMLOGD("length=%{public}ud", arrayLength);
214 for (uint32_t j = 0; j < arrayLength; j++) {
215 napi_value value = nullptr;
216 NAPI_CALL(env, napi_get_element(env, args, j, &value));
217 napi_valuetype valueType = napi_undefined;
218 NAPI_CALL(env, napi_typeof(env, value, &valueType));
219 if (valueType != napi_object) {
220 elementArray.clear();
221 return nullptr;
222 }
223 AppExecFwk::ElementName element;
224 ParseElementName(env, element, value);
225 elementArray.push_back(element);
226 }
227 // create result code
228 napi_value result;
229 napi_status status = napi_create_int32(env, NAPI_RETURN_ONE, &result);
230 if (status != napi_ok) {
231 return nullptr;
232 }
233 return result;
234 }
235
JsObjectToInt(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,int32_t & result)236 bool JsObjectToInt(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, int32_t &result)
237 {
238 bool hasProperty = false;
239 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
240 EDMLOGE("get js property failed.");
241 return false;
242 }
243 if (isNecessaryProp && !hasProperty) {
244 return false;
245 }
246 if (hasProperty) {
247 napi_value prop = nullptr;
248 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseInt(env, result, prop);
249 }
250 return true;
251 }
252
JsObjectToUint(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,uint32_t & result)253 bool JsObjectToUint(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, uint32_t &result)
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 && ParseUint(env, result, prop);
266 }
267 return true;
268 }
269
JsObjectToBool(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,bool & result)270 bool JsObjectToBool(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, bool &result)
271 {
272 bool hasProperty = false;
273 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
274 EDMLOGE("get js property failed.");
275 return false;
276 }
277 if (isNecessaryProp && !hasProperty) {
278 return false;
279 }
280 if (hasProperty) {
281 napi_value prop = nullptr;
282 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseBool(env, result, prop);
283 }
284 return true;
285 }
286
JsObjectToString(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,std::string & resultStr)287 bool JsObjectToString(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp,
288 std::string &resultStr)
289 {
290 bool hasProperty = false;
291 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
292 EDMLOGE("get js property failed.");
293 return false;
294 }
295 if (isNecessaryProp && !hasProperty) {
296 return false;
297 }
298 if (hasProperty) {
299 napi_value prop = nullptr;
300 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseString(env, resultStr, prop);
301 }
302 return true;
303 }
304
JsObjectToCharArray(napi_env env,napi_value object,const char * filedStr,std::tuple<int,bool> charArrayProp,char * result)305 bool JsObjectToCharArray(napi_env env, napi_value object, const char *filedStr, std::tuple<int, bool> charArrayProp,
306 char *result)
307 {
308 bool hasProperty = false;
309 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
310 EDMLOGE("get js property failed.");
311 return false;
312 }
313 int maxLength = 0;
314 bool isNecessaryProp = false;
315 std::tie(maxLength, isNecessaryProp) = charArrayProp;
316 if (isNecessaryProp && !hasProperty) {
317 return false;
318 }
319 if (hasProperty) {
320 napi_value prop = nullptr;
321 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok &&
322 ParseCharArray(env, prop, maxLength, result);
323 }
324 return true;
325 }
326
GetJsProperty(napi_env env,napi_value object,const char * filedStr,napi_value & result)327 bool GetJsProperty(napi_env env, napi_value object, const char *filedStr, napi_value &result)
328 {
329 bool hasProperty = false;
330 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok || !hasProperty ||
331 napi_get_named_property(env, object, filedStr, &result) != napi_ok) {
332 EDMLOGE("Js has no property");
333 return false;
334 }
335 return true;
336 }
337
JsObjectToU8Vector(napi_env env,napi_value object,const char * fieldStr,std::vector<uint8_t> & certVector)338 bool JsObjectToU8Vector(napi_env env, napi_value object, const char *fieldStr, std::vector<uint8_t> &certVector)
339 {
340 napi_value certEntry;
341 if (!GetJsProperty(env, object, fieldStr, certEntry)) {
342 return false;
343 }
344 bool isTypedArray = false;
345 if (napi_is_typedarray(env, certEntry, &isTypedArray) != napi_ok || !isTypedArray) {
346 EDMLOGE("js property is not typedarray");
347 return false;
348 }
349 size_t length = 0;
350 size_t offset = 0;
351 napi_typedarray_type type;
352 napi_value buffer = nullptr;
353 void *data = nullptr;
354 if (napi_get_typedarray_info(env, certEntry, &type, &length, &data, &buffer, &offset) != napi_ok ||
355 type != napi_uint8_array || buffer == nullptr) {
356 EDMLOGE("js object type is not uint8 array");
357 return false;
358 }
359 if (length > NAPI_MAX_DATA_LEN) {
360 EDMLOGE("uint8 array range failed");
361 return false;
362 }
363 if (data == nullptr) {
364 EDMLOGE("uint8 array data failed");
365 return false;
366 }
367 certVector.clear();
368 certVector.assign(static_cast<uint8_t *>(data), (static_cast<uint8_t *>(data) + length));
369 return true;
370 }
371
JsObjectToStringVector(napi_env env,napi_value object,const char * filedStr,bool isNecessaryProp,std::vector<std::string> & vec)372 bool JsObjectToStringVector(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp,
373 std::vector<std::string> &vec)
374 {
375 bool hasProperty = false;
376 if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
377 EDMLOGE("get js property failed.");
378 return false;
379 }
380 if (isNecessaryProp && !hasProperty) {
381 EDMLOGE("no yaobaohua hasProperty.");
382 return false;
383 }
384 if (hasProperty) {
385 napi_value prop = nullptr;
386 return napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseStringArray(env, vec, prop);
387 }
388 return true;
389 }
390
NativeVoidCallbackComplete(napi_env env,napi_status status,void * data)391 void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data)
392 {
393 if (data == nullptr) {
394 EDMLOGE("data is nullptr");
395 return;
396 }
397 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
398 napi_value error = nullptr;
399 if (asyncCallbackInfo->callback == nullptr) {
400 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
401 if (asyncCallbackInfo->ret == ERR_OK) {
402 napi_get_null(env, &error);
403 napi_resolve_deferred(env, asyncCallbackInfo->deferred, error);
404 } else {
405 if (asyncCallbackInfo->innerCodeMsg.empty()) {
406 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
407 } else {
408 napi_reject_deferred(env, asyncCallbackInfo->deferred,
409 CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg));
410 }
411 }
412 } else {
413 EDMLOGD("asyncCallbackInfo->callback != nullptr");
414 if (asyncCallbackInfo->ret == ERR_OK) {
415 napi_get_null(env, &error);
416 } else {
417 if (asyncCallbackInfo->innerCodeMsg.empty()) {
418 error = CreateError(env, asyncCallbackInfo->ret);
419 } else {
420 error = CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg);
421 }
422 }
423 napi_value callback = nullptr;
424 napi_value result = nullptr;
425 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
426 napi_call_function(env, nullptr, callback, ARGS_SIZE_ONE, &error, &result);
427 napi_delete_reference(env, asyncCallbackInfo->callback);
428 }
429 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
430 delete asyncCallbackInfo;
431 }
432
HandleAsyncWork(napi_env env,AsyncCallbackInfo * context,const std::string & workName,napi_async_execute_callback execute,napi_async_complete_callback complete)433 napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, const std::string &workName,
434 napi_async_execute_callback execute, napi_async_complete_callback complete)
435 {
436 napi_value result = nullptr;
437 if (context->callback == nullptr) {
438 napi_create_promise(env, &context->deferred, &result);
439 } else {
440 napi_get_undefined(env, &result);
441 }
442 napi_value resource = nullptr;
443 napi_get_undefined(env, &resource);
444 napi_value resourceName = nullptr;
445 napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName);
446 napi_create_async_work(env, resource, resourceName, execute, complete, static_cast<void *>(context),
447 &context->asyncWork);
448 napi_queue_async_work(env, context->asyncWork);
449 return result;
450 }
451
NativeBoolCallbackComplete(napi_env env,napi_status status,void * data)452 void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data)
453 {
454 if (data == nullptr) {
455 EDMLOGE("data is nullptr");
456 return;
457 }
458 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
459 if (asyncCallbackInfo->deferred != nullptr) {
460 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
461 if (asyncCallbackInfo->ret == ERR_OK) {
462 EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet);
463 napi_value result = nullptr;
464 napi_get_boolean(env, asyncCallbackInfo->boolRet, &result);
465 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
466 } else {
467 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
468 }
469 } else {
470 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
471 if (asyncCallbackInfo->ret == ERR_OK) {
472 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
473 EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet);
474 napi_get_boolean(env, asyncCallbackInfo->boolRet, &callbackValue[ARR_INDEX_ONE]);
475 } else {
476 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
477 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
478 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
479 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
480 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
481 }
482 napi_value callback = nullptr;
483 napi_value result = nullptr;
484 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
485 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
486 napi_delete_reference(env, asyncCallbackInfo->callback);
487 }
488 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
489 delete asyncCallbackInfo;
490 }
491
NativeNumberCallbackComplete(napi_env env,napi_status status,void * data)492 void NativeNumberCallbackComplete(napi_env env, napi_status status, void *data)
493 {
494 if (data == nullptr) {
495 EDMLOGE("data is nullptr");
496 return;
497 }
498 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
499 if (asyncCallbackInfo->deferred != nullptr) {
500 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
501 if (asyncCallbackInfo->ret == ERR_OK) {
502 EDMLOGD("asyncCallbackInfo->intRet = %{public}d", asyncCallbackInfo->intRet);
503 napi_value result = nullptr;
504 napi_create_int32(env, asyncCallbackInfo->intRet, &result);
505 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
506 } else {
507 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
508 }
509 } else {
510 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
511 if (asyncCallbackInfo->ret == ERR_OK) {
512 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
513 EDMLOGD("asyncCallbackInfo->intRet = %{public}d", asyncCallbackInfo->intRet);
514 napi_create_int32(env, asyncCallbackInfo->intRet, &callbackValue[ARR_INDEX_ONE]);
515 } else {
516 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
517 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
518 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
519 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
520 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
521 }
522 napi_value callback = nullptr;
523 napi_value result = nullptr;
524 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
525 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
526 napi_delete_reference(env, asyncCallbackInfo->callback);
527 }
528 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
529 delete asyncCallbackInfo;
530 }
531
NativeStringCallbackComplete(napi_env env,napi_status status,void * data)532 void NativeStringCallbackComplete(napi_env env, napi_status status, void *data)
533 {
534 if (data == nullptr) {
535 EDMLOGE("data is nullptr");
536 return;
537 }
538 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
539 if (asyncCallbackInfo->deferred != nullptr) {
540 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
541 if (asyncCallbackInfo->ret == ERR_OK) {
542 EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str());
543 napi_value result = nullptr;
544 napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH, &result);
545 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
546 } else {
547 if (asyncCallbackInfo->innerCodeMsg.empty()) {
548 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
549 } else {
550 napi_reject_deferred(env, asyncCallbackInfo->deferred,
551 CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg));
552 }
553 }
554 } else {
555 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
556 if (asyncCallbackInfo->ret == ERR_OK) {
557 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
558 EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str());
559 napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH,
560 &callbackValue[ARR_INDEX_ONE]);
561 } else {
562 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
563 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
564 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
565 if (asyncCallbackInfo->innerCodeMsg.empty()) {
566 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
567 } else {
568 callbackValue[ARR_INDEX_ZERO] =
569 CreateErrorWithInnerCode(env, asyncCallbackInfo->ret, asyncCallbackInfo->innerCodeMsg);
570 }
571 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
572 }
573 napi_value callback = nullptr;
574 napi_value result = nullptr;
575 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
576 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
577 napi_delete_reference(env, asyncCallbackInfo->callback);
578 }
579 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
580 delete asyncCallbackInfo;
581 }
582
NativeArrayStringCallbackComplete(napi_env env,napi_status status,void * data)583 void NativeArrayStringCallbackComplete(napi_env env, napi_status status, void *data)
584 {
585 if (data == nullptr) {
586 EDMLOGE("data is nullptr");
587 return;
588 }
589 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
590 if (asyncCallbackInfo->deferred != nullptr) {
591 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
592 if (asyncCallbackInfo->ret == ERR_OK) {
593 napi_value result = nullptr;
594 napi_create_array(env, &result);
595 ConvertStringVectorToJS(env, asyncCallbackInfo->arrayStringRet, result);
596 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
597 } else {
598 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
599 }
600 } else {
601 napi_value callbackValue[ARGS_SIZE_TWO] = {0};
602 if (asyncCallbackInfo->ret == ERR_OK) {
603 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
604 napi_create_array(env, &callbackValue[ARGS_SIZE_ONE]);
605 ConvertStringVectorToJS(env, asyncCallbackInfo->arrayStringRet, callbackValue[ARGS_SIZE_ONE]);
606 } else {
607 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
608 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
609 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
610 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
611 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
612 }
613 napi_value callback = nullptr;
614 napi_value result = nullptr;
615 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
616 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
617 napi_delete_reference(env, asyncCallbackInfo->callback);
618 }
619 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
620 delete asyncCallbackInfo;
621 }
622
ConvertStringVectorToJS(napi_env env,const std::vector<std::string> & stringVector,napi_value result)623 void ConvertStringVectorToJS(napi_env env, const std::vector<std::string> &stringVector, napi_value result)
624 {
625 EDMLOGD("vector size: %{public}zu", stringVector.size());
626 size_t idx = 0;
627 for (const auto &str : stringVector) {
628 napi_value obj = nullptr;
629 napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &obj);
630 napi_set_element(env, result, idx, obj);
631 idx++;
632 }
633 }
634
CheckAdminWithUserIdParamType(napi_env env,size_t argc,napi_value * argv,bool & hasCallback,bool & hasUserId)635 bool CheckAdminWithUserIdParamType(napi_env env, size_t argc, napi_value *argv, bool &hasCallback, bool &hasUserId)
636 {
637 if (!MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object)) {
638 EDMLOGE("CheckAdminWithUserIdParamType admin type check failed");
639 return false;
640 }
641 EDMLOGI("argc = %{public}zu", argc);
642 if (argc == ARGS_SIZE_ONE) {
643 hasCallback = false;
644 hasUserId = false;
645 EDMLOGI("hasCallback = false; hasUserId = false;");
646 return true;
647 }
648
649 if (argc == ARGS_SIZE_TWO) {
650 if (MatchValueType(env, argv[ARR_INDEX_ONE], napi_function)) {
651 hasCallback = true;
652 hasUserId = false;
653 EDMLOGI("hasCallback = true; hasUserId = false;");
654 return true;
655 } else {
656 hasCallback = false;
657 hasUserId = true;
658 EDMLOGI("hasCallback = false; hasUserId = true;");
659 return MatchValueType(env, argv[ARR_INDEX_ONE], napi_number);
660 }
661 }
662 hasCallback = true;
663 hasUserId = true;
664 EDMLOGI("hasCallback = true; hasUserId = true;");
665 return MatchValueType(env, argv[ARR_INDEX_ONE], napi_number) &&
666 MatchValueType(env, argv[ARR_INDEX_TWO], napi_function);
667 }
668
CheckGetPolicyAdminParam(napi_env env,napi_value value,bool & hasAdmin,OHOS::AppExecFwk::ElementName & elementName)669 bool CheckGetPolicyAdminParam(napi_env env, napi_value value, bool &hasAdmin,
670 OHOS::AppExecFwk::ElementName &elementName)
671 {
672 if (MatchValueType(env, value, napi_null)) {
673 hasAdmin = false;
674 return true;
675 }
676 if (MatchValueType(env, value, napi_object)) {
677 hasAdmin = true;
678 if (ParseElementName(env, elementName, value)) {
679 return true;
680 }
681 }
682 return false;
683 }
684 } // namespace EDM
685 } // namespace OHOS