1 /* 2 * Copyright (c) 2022 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 #ifndef NAPI_SCAN_UTILS_H 17 #define NAPI_SCAN_UTILS_H 18 19 #include <string> 20 #include <vector> 21 #include <sstream> 22 23 #include "napi/native_api.h" 24 #include "napi/native_common.h" 25 #include "scan_constant.h" 26 #include "scan_log.h" 27 28 namespace OHOS::Scan { 29 class NapiScanUtils { 30 public: 31 static constexpr size_t MAX_ARGC = 6; 32 static constexpr size_t ARGC_ZERO = 0; 33 static constexpr size_t ARGC_ONE = 1; 34 static constexpr size_t ARGC_TWO = 2; 35 static constexpr size_t ARGC_THREE = 3; 36 static constexpr size_t ARGC_FOUR = 4; 37 static constexpr size_t ARGC_FIVE = 5; 38 static constexpr size_t ARGC_SIX = 6; 39 40 static constexpr uint32_t INDEX_ZERO = 0; 41 static constexpr uint32_t INDEX_ONE = 1; 42 static constexpr uint32_t INDEX_TWO = 2; 43 static constexpr uint32_t INDEX_THREE = 3; 44 static constexpr uint32_t INDEX_FOUR = 4; 45 static constexpr uint32_t INDEX_FIVE = 5; 46 47 static constexpr int32_t MAX_NUMBER_BYTES = 8; 48 static constexpr int32_t MAX_LEN = 4096; 49 static constexpr int32_t MAX_JOBSTRING_LENGTH = 10; 50 51 static napi_valuetype GetValueType(napi_env env, napi_value value); 52 static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 53 static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 54 static void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value); 55 static std::vector<std::string> GetPropertyNames(napi_env env, napi_value object); 56 static napi_value CreateUint32(napi_env env, uint32_t code); 57 static uint32_t GetUint32FromValue(napi_env env, napi_value value); 58 static uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName); 59 static void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value); 60 static napi_value CreateInt32(napi_env env, int32_t code); 61 static int32_t GetInt32FromValue(napi_env env, napi_value value); 62 static int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName); 63 static void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value); 64 static napi_value CreateStringUtf8(napi_env env, const std::string &str); 65 static std::string GetStringFromValueUtf8(napi_env env, napi_value value); 66 static std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName); 67 static void SetStringPropertyUtf8( 68 napi_env env, napi_value object, const std::string &name, const std::string &value); 69 static std::string GetValueString(napi_env env, napi_value value); 70 static bool ValueIsArrayBuffer(napi_env env, napi_value value); 71 static void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length); 72 static napi_value CreateArrayBuffer(napi_env env, size_t length, void **data); 73 static napi_value CreateObject(napi_env env); 74 static napi_value GetUndefined(napi_env env); 75 static napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv); 76 static napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg); 77 static napi_ref CreateReference(napi_env env, napi_value callback); 78 static napi_value GetReference(napi_env env, napi_ref callbackRef); 79 static void DeleteReference(napi_env env, napi_ref callbackRef); 80 static napi_value CreateBoolean(napi_env env, bool value); 81 static bool GetBooleanFromValue(napi_env env, napi_value value); 82 static bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName); 83 static void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value); 84 static void DefineProperties( 85 napi_env env, napi_value object, const std::initializer_list<napi_property_descriptor> &properties); 86 static std::string ToLower(const std::string &s); 87 static std::string GetExtensionId(const std::string &globalId); 88 static std::string GetGlobalId(const std::string& extensionId, const std::string& localId); 89 static std::string GetLocalId(const std::string& globalId, const std::string& extensionId); 90 static std::string EncodeExtensionCid(const std::string &extensionId, uint32_t callbackId); 91 static bool DecodeExtensionCid(const std::string &cid, std::string &extensionId, uint32_t &callbackId); 92 static std::string GetTaskEventId(const std::string &taskId, const std::string &type); 93 static int32_t OpenFile(const std::string &filePath); 94 static bool IsPathValid(const std::string &filePath); 95 static uint32_t GetIdFromFdPath(const std::string &fdPath); 96 static size_t GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length); 97 }; 98 } // namespace OHOS::Scan 99 #endif // NAPI_SCAN_UTILS_H 100