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 38 static constexpr uint32_t INDEX_ZERO = 0; 39 static constexpr uint32_t INDEX_ONE = 1; 40 static constexpr uint32_t INDEX_TWO = 2; 41 static constexpr uint32_t INDEX_THREE = 3; 42 static constexpr uint32_t INDEX_FOUR = 4; 43 static constexpr uint32_t INDEX_FIVE = 5; 44 45 static napi_valuetype GetValueType(napi_env env, napi_value value); 46 static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 47 static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 48 static void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value); 49 static std::vector<std::string> GetPropertyNames(napi_env env, napi_value object); 50 static napi_value CreateUint32(napi_env env, uint32_t code); 51 static uint32_t GetUint32FromValue(napi_env env, napi_value value); 52 static uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName); 53 static void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value); 54 static napi_value CreateInt32(napi_env env, int32_t code); 55 static int32_t GetInt32FromValue(napi_env env, napi_value value); 56 static int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName); 57 static void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value); 58 static napi_value CreateStringUtf8(napi_env env, const std::string &str); 59 static std::string GetStringFromValueUtf8(napi_env env, napi_value value); 60 static std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName); 61 static void SetStringPropertyUtf8( 62 napi_env env, napi_value object, const std::string &name, const std::string &value); 63 static std::string GetValueString(napi_env env, napi_value value); 64 static bool ValueIsArrayBuffer(napi_env env, napi_value value); 65 static void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length); 66 static napi_value CreateArrayBuffer(napi_env env, size_t length, void **data); 67 static napi_value CreateObject(napi_env env); 68 static napi_value GetUndefined(napi_env env); 69 static napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv); 70 static napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg); 71 static napi_ref CreateReference(napi_env env, napi_value callback); 72 static napi_value GetReference(napi_env env, napi_ref callbackRef); 73 static void DeleteReference(napi_env env, napi_ref callbackRef); 74 static napi_value CreateBoolean(napi_env env, bool value); 75 static bool GetBooleanFromValue(napi_env env, napi_value value); 76 static bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName); 77 static void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value); 78 static void DefineProperties( 79 napi_env env, napi_value object, const std::initializer_list<napi_property_descriptor> &properties); 80 static std::string ToLower(const std::string &s); 81 static std::string GetExtensionId(const std::string &globalId); 82 static std::string GetGlobalId(const std::string& extensionId, const std::string& localId); 83 static std::string GetLocalId(const std::string& globalId, const std::string& extensionId); 84 static std::string EncodeExtensionCid(const std::string &extensionId, uint32_t callbackId); 85 static bool DecodeExtensionCid(const std::string &cid, std::string &extensionId, uint32_t &callbackId); 86 static int32_t OpenFile(const std::string &filePath); 87 static bool IsPathValid(const std::string &filePath); 88 static uint32_t GetIdFromFdPath(const std::string &fdPath); 89 static size_t GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length); 90 static void NapiThrowError(napi_env env, uint32_t errCode); 91 static void SetErrorText(uint32_t& code, std::string& message); 92 static bool CheckCallerIsSystemApp(); 93 94 static std::unordered_map<uint32_t, std::string> scanErrorCodeMap_; 95 }; 96 } // namespace OHOS::Scan 97 #endif // NAPI_SCAN_UTILS_H 98