1 /* 2 * Copyright (c) 2022-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 INTERFACE_KITS_JS_UTILS_H 17 #define INTERFACE_KITS_JS_UTILS_H 18 19 #include <map> 20 21 #include "global.h" 22 #include "napi/native_api.h" 23 #include "napi/native_common.h" 24 #include "napi/native_node_api.h" 25 #include "string_ex.h" 26 #include "js_callback_object.h" 27 28 namespace OHOS { 29 namespace MiscServices { 30 enum IMFErrorCode : int32_t { 31 EXCEPTION_PERMISSION = 201, 32 EXCEPTION_PARAMCHECK = 401, 33 EXCEPTION_UNSUPPORTED = 801, 34 EXCEPTION_OTHERS = 12899999, 35 EXCEPTION_PACKAGEMANAGER = 12800001, 36 EXCEPTION_IMENGINE, 37 EXCEPTION_IMCLIENT, 38 EXCEPTION_KEYEVENT, 39 EXCEPTION_CONFPERSIST, 40 EXCEPTION_CONTROLLER, 41 EXCEPTION_SETTINGS, 42 EXCEPTION_IMMS, 43 }; 44 45 enum TypeCode : int32_t { 46 TYPE_NONE = 0, 47 TYPE_UNDEFINED, 48 TYPE_NULL, 49 TYPE_BOOLEAN, 50 TYPE_NUMBER, 51 TYPE_STRING, 52 TYPE_SYMBOL, 53 TYPE_OBJECT, 54 TYPE_FUNCTION, 55 TYPE_EXTERNAL, 56 TYPE_BIGINT, 57 }; 58 59 class JsUtils { 60 public: 61 using ArgsProvider = std::function<bool(napi_value args[], uint8_t argc, std::shared_ptr<JSCallbackObject>)>; 62 63 static void ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type); 64 65 static napi_value ToError(napi_env env, int32_t err); 66 67 static bool TraverseCallback(const std::vector<std::shared_ptr<JSCallbackObject>> &vecCopy, size_t paramNum, 68 ArgsProvider argsProvider); 69 70 private: 71 static int32_t Convert(int32_t code); 72 73 static const std::string ToMessage(int32_t code); 74 75 static const std::map<int32_t, int32_t> ERROR_CODE_MAP; 76 77 static const std::map<int32_t, std::string> ERROR_CODE_CONVERT_MESSAGE_MAP; 78 79 static const std::map<int32_t, std::string> PARAMETER_TYPE; 80 81 static constexpr int32_t ERROR_CODE_QUERY_FAILED = 1; 82 83 static constexpr uint8_t MAX_ARGMENT_COUNT = 10; 84 }; 85 } // namespace MiscServices 86 } // namespace OHOS 87 #endif // INTERFACE_KITS_JS_UTILS_H