• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "nfc_napi_common_utils.h"
17 #include <cstring>
18 #include "loghelper.h"
19 #include "nfc_sdk_common.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace NFC {
24 namespace KITS {
ParseString(napi_env env,std::string & param,napi_value args)25 bool ParseString(napi_env env, std::string &param, napi_value args)
26 {
27     napi_valuetype valuetype;
28     napi_typeof(env, args, &valuetype);
29 
30     DebugLog("param=%{public}d.", valuetype);
31     if (valuetype != napi_string) {
32         DebugLog("Wrong argument type. String expected.");
33         return false;
34     }
35     size_t size = 0;
36 
37     if (napi_get_value_string_utf8(env, args, nullptr, 0, &size) != napi_ok) {
38         ErrorLog("can not get string size");
39         param = "";
40         return false;
41     }
42     param.reserve(size + 1);
43     param.resize(size);
44     if (napi_get_value_string_utf8(env, args, param.data(), (size + 1), &size) != napi_ok) {
45         ErrorLog("can not get string value");
46         param = "";
47         return false;
48     }
49     return true;
50 }
ParseInt32(napi_env env,int32_t & param,napi_value args)51 bool ParseInt32(napi_env env, int32_t &param, napi_value args)
52 {
53     napi_valuetype valuetype;
54     napi_typeof(env, args, &valuetype);
55 
56     DebugLog("ParseInt32, valuetype %{public}d.", valuetype);
57     if (valuetype != napi_number) {
58         ErrorLog("Wrong argument type. Int32 expected.");
59         return false;
60     }
61     napi_get_value_int32(env, args, &param);
62     return true;
63 }
64 
ParseBool(napi_env env,bool & param,napi_value args)65 bool ParseBool(napi_env env, bool &param, napi_value args)
66 {
67     napi_valuetype valuetype;
68     napi_typeof(env, args, &valuetype);
69 
70     DebugLog("param=%{public}d.", valuetype);
71     if (valuetype != napi_boolean) {
72         ErrorLog("Wrong argument type. bool expected.");
73         return false;
74     }
75     napi_get_value_bool(env, args, &param);
76     return true;
77 }
78 
ParseBytesVector(napi_env env,std::vector<unsigned char> & vec,napi_value args)79 bool ParseBytesVector(napi_env env, std::vector<unsigned char> &vec, napi_value args)
80 {
81     bool isArray = false;
82     napi_status status = napi_is_array(env, args, &isArray);
83     if (status != napi_ok || !isArray) {
84         ErrorLog("ParseBytesVector, not array");
85         return false;
86     }
87     uint32_t arrayLength = 0;
88     napi_get_array_length(env, args, &arrayLength);
89     for (uint32_t i = 0; i < arrayLength; i++) {
90         napi_value element = nullptr;
91         napi_get_element(env, args, i, &element);
92 
93         napi_valuetype valueType = napi_undefined;
94         napi_typeof(env, element, &valueType);
95         if (valueType != napi_number) {
96             ErrorLog("ParseBytesVector, not number!");
97             return false;
98         }
99 
100         uint32_t byteValue = 0x0;
101         napi_get_value_uint32(env, element, &byteValue);
102         vec.push_back(static_cast<unsigned char>(byteValue));
103     }
104     return true;
105 }
106 
ParseUInt32Vector(napi_env & env,std::vector<uint32_t> & vec,napi_value & args)107 bool ParseUInt32Vector(napi_env& env, std::vector<uint32_t>& vec, napi_value &args)
108 {
109     bool isArray = false;
110     napi_status status = napi_is_array(env, args, &isArray);
111     if (status != napi_ok || !isArray) {
112         ErrorLog("ParseUInt32Vector: not array");
113         return false;
114     }
115     uint32_t arrayLen = 0;
116     napi_get_array_length(env, args, &arrayLen);
117     for (uint32_t i = 0; i < arrayLen; i++) {
118         napi_value element = nullptr;
119         napi_get_element(env, args, i, &element);
120 
121         napi_valuetype valueType = napi_undefined;
122         napi_typeof(env, element, &valueType);
123         if (valueType != napi_number) {
124             ErrorLog("ParseUInt32Vector, not number!");
125             return false;
126         }
127 
128         uint32_t uint32Value = 0;
129         napi_get_value_uint32(env, element, &uint32Value);
130         vec.push_back(static_cast<uint32_t>(uint32Value));
131     }
132     return true;
133 }
134 
ParseStringVector(napi_env & env,std::vector<std::string> & vec,napi_value & args,uint32_t maxLen)135 bool ParseStringVector(napi_env &env, std::vector<std::string> &vec, napi_value &args, uint32_t maxLen)
136 {
137     bool isArray = false;
138     napi_status status = napi_is_array(env, args, &isArray);
139     if (status != napi_ok || !isArray) {
140         ErrorLog("ParseStringVector: not array");
141         return false;
142     }
143     uint32_t arrayLen = 0;
144     napi_get_array_length(env, args, &arrayLen);
145     if (arrayLen > maxLen) {
146         ErrorLog("ParseStringVector, too big array!");
147         return false;
148     }
149     for (uint32_t i = 0; i < arrayLen; i++) {
150         napi_value element = nullptr;
151         napi_get_element(env, args, i, &element);
152 
153         napi_valuetype valueType;
154         napi_typeof(env, element, &valueType);
155         if (valueType != napi_string) {
156             ErrorLog("ParseStringVector, not string!");
157             return false;
158         }
159 
160         std::string stringValue;
161         ParseString(env, stringValue, element);
162         vec.push_back(stringValue);
163     }
164     return true;
165 }
166 
ParseElementName(napi_env & env,ElementName & element,napi_value & args)167 bool ParseElementName(napi_env &env, ElementName &element, napi_value &args)
168 {
169     napi_valuetype valueType = napi_undefined;
170     napi_typeof(env, args, &valueType);
171     if (valueType != napi_object) {
172         ErrorLog("ParseElementName, not object!");
173         return false;
174     }
175     napi_value param = nullptr;
176     napi_get_named_property(env, args, "bundleName", &param);
177     std::string bundleName;
178     ParseString(env, bundleName, param);
179 
180     param = nullptr;
181     napi_get_named_property(env, args, "moduleName", &param);
182     std::string moduleName;
183     ParseString(env, moduleName, param);
184 
185     param = nullptr;
186     napi_get_named_property(env, args, "abilityName", &param);
187     std::string abilityName;
188     ParseString(env, abilityName, param);
189 
190     DebugLog("ParseElementName: bundleName:%{public}s, moduleName:%{public}s, abilityName:%{public}s",
191         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
192     element.SetBundleName(bundleName);
193     element.SetModuleName(moduleName);
194     element.SetAbilityName(abilityName);
195     return true;
196 }
197 
ParseArrayBuffer(napi_env env,uint8_t ** data,size_t & size,napi_value args)198 bool ParseArrayBuffer(napi_env env, uint8_t **data, size_t &size, napi_value args)
199 {
200     napi_status status;
201     napi_valuetype valuetype;
202     napi_typeof(env, args, &valuetype);
203 
204     DebugLog("param=%{public}d.", valuetype);
205     if (valuetype != napi_object) {
206         ErrorLog("Wrong argument type. object expected.");
207         return false;
208     }
209 
210     status = napi_get_arraybuffer_info(env, args, reinterpret_cast<void **>(data), &size);
211     if (status != napi_ok) {
212         ErrorLog("can not get arraybuffer, error is %{public}d", status);
213         (*data)[0] = 0;
214         return false;
215     }
216     DebugLog("arraybuffer size is %{public}zu,buffer is %{public}d", size, (*data)[0]);
217     return true;
218 }
219 
UndefinedNapiValue(const napi_env & env)220 napi_value UndefinedNapiValue(const napi_env &env)
221 {
222     napi_value result;
223     napi_get_undefined(env, &result);
224     return result;
225 }
226 
ConvertStringVector(napi_env env,napi_value jsValue)227 std::vector<std::string> ConvertStringVector(napi_env env, napi_value jsValue)
228 {
229     bool isTypedArray = false;
230     napi_status status = napi_is_typedarray(env, jsValue, &isTypedArray);
231     if (status != napi_ok || !isTypedArray) {
232         ErrorLog("%{public}s called, napi_is_typedarray error", __func__);
233         return {};
234     }
235 
236     napi_typedarray_type type;
237     size_t length = 0;
238     napi_value buffer = nullptr;
239     size_t offset = 0;
240     NAPI_CALL_BASE(env, napi_get_typedarray_info(env, jsValue, &type, &length, nullptr, &buffer, &offset), {});
241     if (type != napi_uint8_array) {
242         ErrorLog("%{public}s called, napi_uint8_array is null", __func__);
243         return {};
244     }
245     std::string *data = nullptr;
246     size_t total = 0;
247     NAPI_CALL_BASE(env, napi_get_arraybuffer_info(env, buffer, reinterpret_cast<void **>(&data), &total), {});
248     length = std::min<size_t>(length, total - offset);
249     std::vector<std::string> result(sizeof(std::string) + length);
250     int retCode = memcpy_s(result.data(), result.size(), &data[offset], length);
251     if (retCode != 0) {
252         return {};
253     }
254     return result;
255 }
256 
CreateErrorMessage(napi_env env,const std::string & msg,int32_t errorCode)257 napi_value CreateErrorMessage(napi_env env, const std::string &msg, int32_t errorCode)
258 {
259     napi_value result = nullptr;
260     napi_value message = nullptr;
261     NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.length(), &message));
262     napi_value codeValue = nullptr;
263     std::string errCode = std::to_string(errorCode);
264     NAPI_CALL(env, napi_create_string_utf8(env, errCode.c_str(), errCode.length(), &codeValue));
265     NAPI_CALL(env, napi_create_error(env, codeValue, message, &result));
266     return result;
267 }
268 
CreateUndefined(napi_env env)269 napi_value CreateUndefined(napi_env env)
270 {
271     napi_value result = nullptr;
272     NAPI_CALL(env, napi_get_undefined(env, &result));
273     return result;
274 }
275 
GetNapiStringValue(napi_env env,napi_value napiValue,const std::string & name,const std::string & defValue)276 std::string GetNapiStringValue(
277     napi_env env, napi_value napiValue, const std::string &name, const std::string &defValue)
278 {
279     napi_value value = GetNamedProperty(env, napiValue, name);
280     if (value != nullptr) {
281         return GetStringFromValue(env, value);
282     } else {
283         return defValue;
284     }
285 }
286 
GetStringFromValue(napi_env env,napi_value value)287 std::string GetStringFromValue(napi_env env, napi_value value)
288 {
289     constexpr int32_t maxTextLength = 4096;
290     char msgChars[maxTextLength] = {0};
291     size_t msgLength = 0;
292     NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, msgChars, maxTextLength, &msgLength), "");
293     if (msgLength > 0) {
294         return std::string(msgChars, 0, msgLength);
295     } else {
296         return "";
297     }
298 }
299 
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)300 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
301 {
302     napi_value value = nullptr;
303     bool hasProperty = false;
304     NAPI_CALL(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty));
305     if (hasProperty) {
306         NAPI_CALL(env, napi_get_named_property(env, object, propertyName.data(), &value));
307     }
308     return value;
309 }
310 
GetNapiInt32Value(napi_env env,napi_value napiValue,const std::string & name,const int32_t & defValue)311 int32_t GetNapiInt32Value(napi_env env, napi_value napiValue, const std::string &name, const int32_t &defValue)
312 {
313     napi_value value = GetNamedProperty(env, napiValue, name);
314     if (value != nullptr) {
315         int32_t intValue = 0;
316         napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
317         if (getIntStatus == napi_ok) {
318             return intValue;
319         }
320     }
321     return defValue;
322 }
323 
UnwrapStringFromJS(napi_env env,napi_value param)324 std::string UnwrapStringFromJS(napi_env env, napi_value param)
325 {
326     constexpr size_t maxTextLength = 1024;
327     char msgChars[maxTextLength] = {0};
328     size_t msgLength = 0;
329     NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, param, msgChars, maxTextLength, &msgLength), "");
330     DebugLog("NapiUtil GetStringFromValue msgLength = %{public}zu", msgLength);
331     if (msgLength > 0) {
332         return std::string(msgChars, 0, msgLength);
333     } else {
334         return "";
335     }
336 }
337 
JsStringToBytesVector(napi_env env,napi_value & src,std::vector<unsigned char> & values)338 void JsStringToBytesVector(napi_env env, napi_value &src, std::vector<unsigned char> &values)
339 {
340     napi_valuetype valueType = napi_undefined;
341     napi_typeof(env, src, &valueType);
342     if (valueType != napi_string) {
343         return;
344     }
345 
346     std::string data;
347     ParseString(env, data, src);
348     NfcSdkCommon::HexStringToBytes(data, values);
349 }
350 
ConvertStringVectorToJS(napi_env env,napi_value & result,std::vector<std::string> & stringVector)351 void ConvertStringVectorToJS(napi_env env, napi_value &result, std::vector<std::string>& stringVector)
352 {
353     DebugLog("ConvertStringVectorToJS called");
354     size_t idx = 0;
355 
356     if (stringVector.empty()) {
357         WarnLog("ConvertStringVectorToJS stringVector empty");
358         napi_create_array_with_length(env, 0, &result);
359         return;
360     }
361     DebugLog("ConvertStringVectorToJS size is %{public}zu", stringVector.size());
362     for (auto& str : stringVector) {
363         napi_value obj = nullptr;
364         napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &obj);
365         napi_set_element(env, result, idx, obj);
366         idx++;
367     }
368 }
369 
BytesVectorToJS(napi_env env,napi_value & result,std::vector<unsigned char> & src)370 void BytesVectorToJS(napi_env env, napi_value &result, std::vector<unsigned char>& src)
371 {
372     if (src.empty()) {
373         WarnLog("BytesVectorToJS src empty");
374         napi_create_array_with_length(env, 0, &result);
375         return;
376     }
377     size_t idx = 0;
378     DebugLog("BytesVectorToJS size is %{public}zu", src.size());
379     napi_create_array_with_length(env, src.size(), &result);
380     for (auto& num : src) {
381         napi_value obj = nullptr;
382         napi_create_uint32(env, num, &obj);
383         napi_set_element(env, result, idx, obj);
384         idx++;
385     }
386 }
387 
ConvertStringToNumberArray(napi_env env,napi_value & result,std::string srcValue)388 void ConvertStringToNumberArray(napi_env env, napi_value &result, std::string srcValue)
389 {
390     if (srcValue.empty()) {
391         WarnLog("ConvertStringToNumberArray srcValue empty");
392         napi_create_array_with_length(env, 0, &result);
393         return;
394     }
395     uint32_t strLength = srcValue.length();
396     if (strLength % HEX_BYTE_LEN != 0) {
397         srcValue = '0' + srcValue;
398         strLength++;
399     }
400 
401     napi_create_array_with_length(env, (strLength / HEX_BYTE_LEN), &result);
402     unsigned int srcIntValue;
403     for (uint32_t i = 0; i < strLength; i += HEX_BYTE_LEN) {
404         // parse the hex string bytes into array.
405         std::string oneByte = srcValue.substr(i, HEX_BYTE_LEN);
406         if (sscanf_s(oneByte.c_str(), "%x", &srcIntValue) <= 0) {
407             ErrorLog("ConvertStringToNumberArray, sscanf_s failed.");
408             return;
409         }
410         unsigned char hexByte = static_cast<unsigned char>(srcIntValue & 0xFF);
411         napi_value hexByteValue = nullptr;
412         napi_create_int32(env, hexByte, &hexByteValue);
413         napi_set_element(env, result, (i / HEX_BYTE_LEN), hexByteValue);
414     }
415 }
416 
ConvertNdefRecordVectorToJS(napi_env env,napi_value & result,std::vector<std::shared_ptr<NdefRecord>> & ndefRecords)417 void ConvertNdefRecordVectorToJS(napi_env env, napi_value &result,
418                                  std::vector<std::shared_ptr<NdefRecord>> &ndefRecords)
419 {
420     napi_create_array(env, &result);
421     if (ndefRecords.empty()) {
422         WarnLog("ConvertNdefRecordVectorToJS ndefRecords is empty.");
423         return;
424     }
425     size_t idx = 0;
426     for (auto& ndefRecord : ndefRecords) {
427         napi_value obj = nullptr;
428         ConvertNdefRecordToJS(env, obj, ndefRecord);
429         napi_set_element(env, result, idx, obj);
430         idx++;
431     }
432 }
433 
ConvertNdefRecordToJS(napi_env env,napi_value & result,std::shared_ptr<NdefRecord> & ndefRecord)434 void ConvertNdefRecordToJS(napi_env env, napi_value &result, std::shared_ptr<NdefRecord> &ndefRecord)
435 {
436     napi_create_object(env, &result);
437     if (ndefRecord == nullptr) {
438         WarnLog("ConvertNdefRecordToJS ndefRecord is null.");
439         return;
440     }
441 
442     napi_value tnf;
443     napi_create_int32(env, ndefRecord->tnf_, &tnf);
444     napi_set_named_property(env, result, "tnf", tnf);
445 
446     napi_value rtdType;
447     std::vector<unsigned char> rtdTypeBytes;
448     NfcSdkCommon::HexStringToBytes(ndefRecord->tagRtdType_, rtdTypeBytes);
449     BytesVectorToJS(env, rtdType, rtdTypeBytes);
450     napi_set_named_property(env, result, "rtdType", rtdType);
451 
452     napi_value id;
453     std::vector<unsigned char> idBytes;
454     NfcSdkCommon::HexStringToBytes(ndefRecord->id_, idBytes);
455     BytesVectorToJS(env, id, idBytes);
456     napi_set_named_property(env, result, "id", id);
457 
458     napi_value payload;
459     std::vector<unsigned char> payloadBytes;
460     NfcSdkCommon::HexStringToBytes(ndefRecord->payload_, payloadBytes);
461     BytesVectorToJS(env, payload, payloadBytes);
462     napi_set_named_property(env, result, "payload", payload);
463 }
464 
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)465 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
466 {
467     napi_valuetype valueType = napi_undefined;
468     NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false);
469     return valueType == targetType;
470 }
471 
MatchParameters(napi_env env,const napi_value parameters[],std::initializer_list<napi_valuetype> valueTypes)472 bool MatchParameters(napi_env env, const napi_value parameters[], std::initializer_list<napi_valuetype> valueTypes)
473 {
474     if (parameters == nullptr) {
475         return false;
476     }
477     int i = 0;
478     for (auto beg = valueTypes.begin(); beg != valueTypes.end(); ++beg) {
479         if (!MatchValueType(env, parameters[i], *beg)) {
480             return false;
481         }
482         ++i;
483     }
484     return true;
485 }
486 
HandleAsyncWork(napi_env env,BaseContext * baseContext,const std::string & workName,napi_async_execute_callback execute,napi_async_complete_callback complete)487 napi_value HandleAsyncWork(napi_env env, BaseContext *baseContext, const std::string &workName,
488     napi_async_execute_callback execute, napi_async_complete_callback complete)
489 {
490     DebugLog("NfcUtil HandleAsyncWork workName = %{public}s", workName.c_str());
491     std::unique_ptr<BaseContext> context(baseContext);
492     if (context == nullptr) {
493         std::string errorCode = std::to_string(napi_invalid_arg);
494         NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), ERR_INIT_CONTEXT.c_str()));
495     }
496     napi_value result = nullptr;
497     if (context != nullptr && context->callbackRef == nullptr) {
498         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
499     } else {
500         NAPI_CALL(env, napi_get_undefined(env, &result));
501     }
502     napi_value resource = CreateUndefined(env);
503     napi_value resourceName = nullptr;
504     NAPI_CALL(env, napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName));
505     NAPI_CALL(env,
506         napi_create_async_work(env, resource, resourceName, execute, complete, static_cast<void *>(context.get()),
507             &context->work));
508     napi_status queueWorkStatus = napi_queue_async_work(env, context->work);
509     if (queueWorkStatus == napi_ok) {
510         context.release();
511         DebugLog("NapiUtil HandleAsyncWork napi_queue_async_work ok");
512     } else {
513         std::string errorCode = std::to_string(queueWorkStatus);
514         NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), ERR_INIT_CONTEXT.c_str()));
515     }
516     DebugLog("NfcUtil HandleAsyncWork end");
517     return result;
518 }
519 
DoAsyncCallbackOrPromise(const napi_env & env,BaseContext * baseContext,napi_value callbackValue)520 void DoAsyncCallbackOrPromise(const napi_env &env, BaseContext *baseContext, napi_value callbackValue)
521 {
522     if (baseContext == nullptr) {
523         ErrorLog("DoAsyncCallbackOrPromise serious error baseContext nullptr");
524         return;
525     }
526     if (baseContext->callbackRef != nullptr) {
527         DebugLog("DoAsyncCallbackOrPromise for callback");
528         napi_value recv = CreateUndefined(env);
529         napi_value callbackFunc = nullptr;
530         NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, baseContext->callbackRef, &callbackFunc));
531         napi_value callbackValues[] = {nullptr, nullptr};
532         callbackValues[0] = baseContext->resolved ? CreateUndefined(env) : callbackValue;
533         callbackValues[1] = baseContext->resolved ? callbackValue : CreateUndefined(env);
534         napi_value result = nullptr;
535         NAPI_CALL_RETURN_VOID(
536             env, napi_call_function(env, recv, callbackFunc, std::size(callbackValues), callbackValues, &result));
537         NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, baseContext->callbackRef));
538     } else if (baseContext->deferred != nullptr) {
539         DebugLog("DoAsyncCallbackOrPromise for promise");
540         if (baseContext->resolved) {
541             NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, baseContext->deferred, callbackValue));
542         } else {
543             NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, baseContext->deferred, callbackValue));
544         }
545         if (baseContext->eventReport == nullptr) {
546             ErrorLog("eventReport is nullptr");
547             return;
548         }
549         baseContext->eventReport->ReportSdkEvent(RESULT_SUCCESS, baseContext->errorCode);
550     }
551     napi_delete_async_work(env, baseContext->work);
552     delete baseContext;
553     baseContext = nullptr;
554 }
555 
ThrowAsyncError(const napi_env & env,BaseContext * baseContext,int errCode,const std::string & errMsg)556 void ThrowAsyncError(const napi_env &env, BaseContext *baseContext, int errCode, const std::string &errMsg)
557 {
558     if (baseContext == nullptr) {
559         ErrorLog("ThrowAsyncError serious error baseContext nullptr");
560         return;
561     }
562     napi_value businessError = CreateErrorMessage(env, errMsg, errCode);
563     if (baseContext->callbackRef != nullptr) {
564         DebugLog("ThrowAsyncError for callback");
565         napi_value recv = CreateUndefined(env);
566         napi_value callbackFunc = nullptr;
567         NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, baseContext->callbackRef, &callbackFunc));
568         napi_value callbackValues[] = {nullptr, nullptr};
569         callbackValues[0] = businessError; // parameter "error"
570         callbackValues[1] = CreateUndefined(env); // parameter "callback"
571         napi_value result = nullptr;
572         NAPI_CALL_RETURN_VOID(
573             env, napi_call_function(env, recv, callbackFunc, std::size(callbackValues), callbackValues, &result));
574         NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, baseContext->callbackRef));
575     } else if (baseContext->deferred != nullptr) {
576         DebugLog("ThrowAsyncError for promise");
577         NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, baseContext->deferred, businessError));
578     }
579     if (baseContext->work != nullptr) {
580         napi_delete_async_work(env, baseContext->work);
581     }
582     delete baseContext;
583     baseContext = nullptr;
584 }
585 
IsNumberArray(const napi_env & env,const napi_value & param)586 bool IsNumberArray(const napi_env &env, const napi_value &param)
587 {
588     if (!IsArray(env, param)) {
589         return false;
590     }
591 
592     uint32_t arrayLength = 0;
593     napi_get_array_length(env, param, &arrayLength);
594     napi_value elementValue = nullptr;
595     for (uint32_t i = 0; i < arrayLength; ++i) {
596         napi_get_element(env, param, i, &elementValue);
597         napi_valuetype elementType = napi_undefined;
598         napi_typeof(env, elementValue, &elementType);
599         if (elementType != napi_number) {
600             return false;
601         }
602     }
603     return true;
604 }
605 
IsObjectArray(const napi_env & env,const napi_value & param)606 bool IsObjectArray(const napi_env &env, const napi_value &param)
607 {
608     if (!IsArray(env, param)) {
609         return false;
610     }
611 
612     uint32_t arrayLength = 0;
613     napi_get_array_length(env, param, &arrayLength);
614     napi_value elementValue = nullptr;
615     for (uint32_t i = 0; i < arrayLength; ++i) {
616         napi_get_element(env, param, i, &elementValue);
617         napi_valuetype elementType = napi_undefined;
618         napi_typeof(env, elementValue, &elementType);
619         if (elementType != napi_object) {
620             return false;
621         }
622     }
623     return true;
624 }
625 
IsArray(const napi_env & env,const napi_value & param)626 bool IsArray(const napi_env &env, const napi_value &param)
627 {
628     bool arrayType = false;
629     napi_status status = napi_is_array(env, param, &arrayType);
630     if (status != napi_ok || !arrayType) {
631         return false;
632     }
633 
634     uint32_t arrayLength = 0;
635     napi_get_array_length(env, param, &arrayLength);
636     if (arrayLength == 0) {
637         return false;
638     }
639     return true;
640 }
641 
IsNumber(const napi_env & env,const napi_value & param)642 bool IsNumber(const napi_env &env, const napi_value &param)
643 {
644     napi_valuetype valueType = napi_undefined;
645     napi_typeof(env, param, &valueType);
646     return valueType == napi_number;
647 }
648 
IsString(const napi_env & env,const napi_value & param)649 bool IsString(const napi_env &env, const napi_value &param)
650 {
651     napi_valuetype valueType = napi_undefined;
652     napi_typeof(env, param, &valueType);
653     return valueType == napi_string;
654 }
655 
IsObject(const napi_env & env,const napi_value & param)656 bool IsObject(const napi_env &env, const napi_value &param)
657 {
658     napi_valuetype valueType = napi_undefined;
659     napi_typeof(env, param, &valueType);
660     return valueType == napi_object;
661 }
662 
IsFunction(const napi_env & env,const napi_value & param)663 bool IsFunction(const napi_env &env, const napi_value &param)
664 {
665     napi_valuetype valueType = napi_undefined;
666     napi_typeof(env, param, &valueType);
667     return valueType == napi_function;
668 }
669 
BuildOutputErrorCode(int errCode)670 int BuildOutputErrorCode(int errCode)
671 {
672     if (errCode == BUSI_ERR_PERM) {
673         return BUSI_ERR_PERM;
674     } else if (errCode == BUSI_ERR_PARAM) {
675         return BUSI_ERR_PARAM;
676     } else if (errCode == ERR_TAG_STATE_IO_FAILED) {
677         return BUSI_ERR_IO_OPERATION_INVALID;
678     } else if (errCode >= ERR_TAG_BASE && errCode < ERR_CE_BASE) {
679         return BUSI_ERR_TAG_STATE_INVALID;
680     }
681     return errCode;
682 }
683 
BuildOutputErrorCodeHce(int errCode)684 int BuildOutputErrorCodeHce(int errCode)
685 {
686     if (errCode == BUSI_ERR_PERM) {
687         return BUSI_ERR_PERM;
688     }
689     if (errCode == BUSI_ERR_PARAM) {
690         return BUSI_ERR_PARAM;
691     }
692     if (errCode == BUSI_ERR_NOT_SYSTEM_APP) {
693         return BUSI_ERR_NOT_SYSTEM_APP;
694     }
695     return BUSI_ERR_HCE_STATE_INVALID;
696 }
697 
BuildErrorMessage(int errCode,std::string funcName,std::string forbiddenPerm,std::string paramName,std::string expertedType)698 std::string BuildErrorMessage(int errCode, std::string funcName, std::string forbiddenPerm,
699     std::string paramName, std::string expertedType)
700 {
701     std::string errMsg;
702     if (errCode == BUSI_ERR_PERM) {
703         return errMsg.append("Permission denied. An attempt was made to ${")
704             .append(funcName)
705             .append("} forbidden by permission: ${")
706             .append(forbiddenPerm)
707             .append("}.");
708     } else if (errCode == BUSI_ERR_PARAM) {
709         if (paramName.length() > 0) {
710             return errMsg.append("Parameter error. The type of \"${")
711                 .append(paramName)
712                 .append("}\" must be ${")
713                 .append(expertedType)
714                 .append("}.");
715         } else {
716             return "Parameter error. The parameter number is invalid.";
717         }
718     } else if (errCode == BUSI_ERR_TAG_STATE_INVALID) {
719         return "The tag running state is abnormal in the service.";
720     } else if (errCode == BUSI_ERR_ELEMENT_STATE_INVALID) {
721         return "The element state is invalid.";
722     } else if (errCode == BUSI_ERR_REGISTER_STATE_INVALID) {
723         return "The off() API can be called only when the on() has been called.";
724     } else if (errCode == BUSI_ERR_HCE_STATE_INVALID) {
725         return "Card emulation running state is abnormal in service.";
726     } else if (errCode == BUSI_ERR_NOT_SYSTEM_APP) {
727         return "Not system application.";
728     }
729     return "Unknown error message";
730 }
731 
GenerateBusinessError(const napi_env & env,int errCode,const std::string & errMessage)732 napi_value GenerateBusinessError(const napi_env &env, int errCode, const std::string &errMessage)
733 {
734     napi_value code = nullptr;
735     napi_create_uint32(env, errCode, &code);
736     napi_value message = nullptr;
737     napi_create_string_utf8(env, errMessage.c_str(), NAPI_AUTO_LENGTH, &message);
738     napi_value businessError = nullptr;
739     napi_create_error(env, nullptr, message, &businessError);
740     napi_set_named_property(env, businessError, KEY_CODE.c_str(), code);
741     return businessError;
742 }
743 
CheckUnwrapStatusAndThrow(const napi_env & env,napi_status status,int errCode)744 bool CheckUnwrapStatusAndThrow(const napi_env &env, napi_status status, int errCode)
745 {
746     if (status != napi_ok) {
747         napi_throw(env, GenerateBusinessError(env, errCode, BuildErrorMessage(errCode, "", "", "", "")));
748         return false;
749     }
750     return true;
751 }
CheckContextAndThrow(const napi_env & env,const BaseContext * context,int errCode)752 bool CheckContextAndThrow(const napi_env &env, const BaseContext *context, int errCode)
753 {
754     if (context == nullptr) {
755         napi_throw(env, GenerateBusinessError(env, errCode, BuildErrorMessage(errCode, "", "", "", "")));
756         return false;
757     }
758     return true;
759 }
CheckParametersAndThrow(const napi_env & env,const napi_value parameters[],std::initializer_list<napi_valuetype> types,const std::string & argName,const std::string & argType)760 bool CheckParametersAndThrow(const napi_env &env, const napi_value parameters[],
761     std::initializer_list<napi_valuetype> types, const std::string &argName, const std::string &argType)
762 {
763     if (!MatchParameters(env, parameters, types)) {
764         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
765             "", "", argName, argType)));
766         return false;
767     }
768     return true;
769 }
CheckArrayNumberAndThrow(const napi_env & env,const napi_value & param,const std::string & argName,const std::string & argType)770 bool CheckArrayNumberAndThrow(const napi_env &env, const napi_value &param, const std::string &argName,
771     const std::string &argType)
772 {
773     if (!IsNumberArray(env, param)) {
774         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
775             "", "", argName, argType)));
776         return false;
777     }
778     return true;
779 }
CheckNumberAndThrow(const napi_env & env,const napi_value & param,const std::string & argName,const std::string & argType)780 bool CheckNumberAndThrow(const napi_env &env, const napi_value &param, const std::string &argName,
781     const std::string &argType)
782 {
783     if (!IsNumber(env, param)) {
784         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
785             "", "", argName, argType)));
786         return false;
787     }
788     return true;
789 }
CheckStringAndThrow(const napi_env & env,const napi_value & param,const std::string & argName,const std::string & argType)790 bool CheckStringAndThrow(const napi_env &env, const napi_value &param, const std::string &argName,
791     const std::string &argType)
792 {
793     if (!IsString(env, param)) {
794         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
795             "", "", argName, argType)));
796         return false;
797     }
798     return true;
799 }
CheckObjectAndThrow(const napi_env & env,const napi_value & param,const std::string & argName,const std::string & argType)800 bool CheckObjectAndThrow(const napi_env &env, const napi_value &param, const std::string &argName,
801     const std::string &argType)
802 {
803     if (!IsObject(env, param)) {
804         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
805             "", "", argName, argType)));
806         return false;
807     }
808     return true;
809 }
810 
CheckFunctionAndThrow(const napi_env & env,const napi_value & param,const std::string & argName,const std::string & argType)811 bool CheckFunctionAndThrow(const napi_env &env, const napi_value &param, const std::string &argName,
812     const std::string &argType)
813 {
814     if (!IsFunction(env, param)) {
815         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
816             "", "", argName, argType)));
817         return false;
818     }
819     return true;
820 }
821 
CheckArgCountAndThrow(const napi_env & env,int argCount,int expCount)822 bool CheckArgCountAndThrow(const napi_env &env, int argCount, int expCount)
823 {
824     if (argCount != expCount) {
825         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PARAM, BuildErrorMessage(BUSI_ERR_PARAM,
826             "", "", "", "")));
827         return false;
828     }
829     return true;
830 }
CheckTagStatusCodeAndThrow(const napi_env & env,int statusCode,const std::string & funcName)831 bool CheckTagStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string &funcName)
832 {
833     if (statusCode == BUSI_ERR_PERM) {
834         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PERM,
835             BuildErrorMessage(BUSI_ERR_PERM, funcName, TAG_PERM_DESC, "", "")));
836         return false;
837     } else if (statusCode == ERR_TAG_STATE_IO_FAILED) {
838         napi_throw(env, GenerateBusinessError(env, ERR_TAG_STATE_IO_FAILED,
839             BuildErrorMessage(BUSI_ERR_IO_OPERATION_INVALID, funcName, "", "", "")));
840         return false;
841     } else if (statusCode >= ErrorCode::ERR_TAG_PARAMETERS && statusCode < ErrorCode::ERR_CE_BASE) {
842         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_TAG_STATE_INVALID,
843             BuildErrorMessage(BUSI_ERR_TAG_STATE_INVALID, "", "", "", "")));
844         return false;
845     }
846     return true;
847 }
848 
CheckHceStatusCodeAndThrow(const napi_env & env,int statusCode,const std::string & funcName)849 bool CheckHceStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string &funcName)
850 {
851     if (statusCode == KITS::ERR_NONE) {
852         return true;
853     }
854     if (statusCode == BUSI_ERR_NOT_SYSTEM_APP) {
855         napi_throw(env, GenerateBusinessError(env, BUSI_ERR_NOT_SYSTEM_APP,
856                                               BuildErrorMessage(BUSI_ERR_NOT_SYSTEM_APP, funcName, "", "", "")));
857         return false;
858     }
859     if (statusCode == BUSI_ERR_PERM) {
860         napi_value busErr = GenerateBusinessError(
861             env, BUSI_ERR_PERM, BuildErrorMessage(BUSI_ERR_PERM, funcName, CARD_EMULATION_PERM_DESC, "", ""));
862         napi_throw(env, busErr);
863         return false;
864     }
865 
866     napi_throw(env, GenerateBusinessError(env, BUSI_ERR_HCE_STATE_INVALID,
867                                           BuildErrorMessage(BUSI_ERR_HCE_STATE_INVALID, "", "", "", "")));
868     return false;
869 }
870 } // namespace KITS
871 } // namespace NFC
872 } // namespace OHOS
873