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 #ifndef INTERFACE_KITS_JS_UTILS_H 17 #define INTERFACE_KITS_JS_UTILS_H 18 19 #include <map> 20 21 #include "ability.h" 22 #include "global.h" 23 #include "input_method_panel.h" 24 #include "input_method_utils.h" 25 #include "js_callback_object.h" 26 #include "js_util.h" 27 #include "napi/native_api.h" 28 #include "napi/native_common.h" 29 #include "napi/native_node_api.h" 30 #include "string_ex.h" 31 32 using Ability = OHOS::AppExecFwk::Ability; 33 namespace OHOS { 34 namespace MiscServices { 35 enum IMFErrorCode : int32_t { 36 EXCEPTION_PERMISSION = 201, 37 EXCEPTION_SYSTEM_PERMISSION = 202, 38 EXCEPTION_PARAMCHECK = 401, 39 EXCEPTION_UNSUPPORTED = 801, 40 EXCEPTION_PACKAGEMANAGER = 12800001, 41 EXCEPTION_IMENGINE = 12800002, 42 EXCEPTION_IMCLIENT = 12800003, 43 EXCEPTION_IME = 12800004, 44 EXCEPTION_CONFPERSIST = 12800005, 45 EXCEPTION_CONTROLLER = 12800006, 46 EXCEPTION_SETTINGS = 12800007, 47 EXCEPTION_IMMS = 12800008, 48 EXCEPTION_DETACHED = 12800009, 49 EXCEPTION_DEFAULTIME = 12800010, 50 EXCEPTION_TEXT_PREVIEW_NOT_SUPPORTED = 12800011, 51 EXCEPTION_PANEL_NOT_FOUND = 12800012, 52 EXCEPTION_WINDOW_MANAGER = 12800013, 53 EXCEPTION_BASIC_MODE = 12800014, 54 EXCEPTION_REQUEST_NOT_ACCEPT = 12800015, 55 EXCEPTION_EDITABLE = 12800016, 56 EXCEPTION_INVALID_PANEL_TYPE_FLAG = 12800017, 57 }; 58 59 enum TypeCode : int32_t { 60 TYPE_NONE = 0, 61 TYPE_UNDEFINED, 62 TYPE_NULL, 63 TYPE_BOOLEAN, 64 TYPE_NUMBER, 65 TYPE_STRING, 66 TYPE_SYMBOL, 67 TYPE_OBJECT, 68 TYPE_FUNCTION, 69 TYPE_EXTERNAL, 70 TYPE_BIGINT, 71 TYPE_ARRAY_BUFFER, 72 TYPE_ARRAY, 73 }; 74 75 /* check condition, return and logging if condition not true. */ 76 #define PARAM_CHECK_RETURN(env, condition, message, typeCode, retVal) \ 77 do { \ 78 if (!(condition)) { \ 79 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \ 80 return retVal; \ 81 } \ 82 } while (0) 83 84 #define PARAM_CHECK_RETURN_VOID(env, condition, message, typeCode) \ 85 do { \ 86 if (!(condition)) { \ 87 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \ 88 return; \ 89 } \ 90 } while (0) 91 92 #define RESULT_CHECK_RETURN(env, condition, errCode, message, typeCode, retVal) \ 93 do { \ 94 if (!(condition)) { \ 95 JsUtils::ThrowException(env, errCode, message, typeCode); \ 96 return retVal; \ 97 } \ 98 } while (0) 99 100 #define RESULT_CHECK_RETURN_VOID(env, condition, errCode, message, typeCode) \ 101 do { \ 102 if (!(condition)) { \ 103 JsUtils::ThrowException(env, errCode, message, typeCode); \ 104 return; \ 105 } \ 106 } while (0) 107 108 /* check condition, return and logging. */ 109 #define CHECK_RETURN_VOID(condition, message) \ 110 do { \ 111 if (!(condition)) { \ 112 IMSA_HILOGE("test (" #condition ") failed: " message); \ 113 return; \ 114 } \ 115 } while (0) 116 117 /* check condition, return and logging. */ 118 #define CHECK_RETURN(condition, message, retVal) \ 119 do { \ 120 if (!(condition)) { \ 121 IMSA_HILOGE("test (" #condition ") failed: " message); \ 122 return retVal; \ 123 } \ 124 } while (0) 125 126 struct JsPropertyInfo { 127 napi_valuetype type; 128 TypeCode typeCode; 129 std::string propertyName; 130 }; 131 132 class JsUtils { 133 public: 134 static void ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type); 135 136 static napi_value ToError(napi_env env, int32_t code, const std::string &msg); 137 138 static int32_t Convert(int32_t code); 139 140 static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId); 141 142 static void *GetNativeSelf(napi_env env, napi_callback_info info); 143 144 static const std::string ToMessage(int32_t code); 145 146 template<typename T> ReadOptionalProperty(napi_env env,napi_value object,const JsPropertyInfo & jsPropInfo,T & value)147 static bool ReadOptionalProperty(napi_env env, napi_value object, const JsPropertyInfo &jsPropInfo, T &value) 148 { 149 if (!JsUtil::HasProperty(env, object, jsPropInfo.propertyName.c_str())) { 150 return false; 151 } 152 napi_value jsObject = nullptr; 153 napi_get_named_property(env, object, jsPropInfo.propertyName.c_str(), &jsObject); 154 PARAM_CHECK_RETURN(env, JsUtil::GetType(env, jsObject) == jsPropInfo.type, jsPropInfo.propertyName, 155 jsPropInfo.typeCode, false); 156 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, jsObject, value) == napi_ok, 157 "failed to convert " + jsPropInfo.propertyName, TYPE_NONE, false); 158 return true; 159 } 160 161 static napi_status GetValue(napi_env env, napi_value in, int32_t &out); 162 static napi_status GetValue(napi_env env, napi_value in, uint32_t &out); 163 static napi_status GetValue(napi_env env, napi_value in, bool &out); 164 static napi_status GetValue(napi_env env, napi_value in, double &out); 165 static napi_status GetValue(napi_env env, napi_value in, std::string &out); 166 static napi_status GetValue(napi_env env, napi_value in, std::unordered_map<std::string, PrivateDataValue> &out); 167 static napi_status GetValue(napi_env env, napi_value in, PrivateDataValue &out); 168 static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out); 169 static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out); 170 static napi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out); 171 static napi_status GetValue(napi_env env, napi_value in, Rosen::Rect &out); 172 static napi_value GetValue(napi_env env, const std::vector<InputWindowInfo> &in); 173 static napi_value GetValue(napi_env env, const InputWindowInfo &in); 174 static napi_value GetValue(napi_env env, const Rosen::Rect &in); 175 static napi_value GetJsPrivateCommand(napi_env env, const std::unordered_map<std::string, PrivateDataValue> &in); 176 static napi_value GetValue(napi_env env, const std::vector<uint8_t> &in); 177 static napi_status GetValue(napi_env env, const std::string &in, napi_value &out); 178 static napi_status GetMessageHandlerCallbackParam(napi_value *argv, 179 const std::shared_ptr<JSMsgHandlerCallbackObject> &jsMessageHandler, const ArrayBuffer &arrayBuffer); 180 181 private: 182 static const std::map<int32_t, int32_t> ERROR_CODE_MAP; 183 184 static const std::map<int32_t, std::string> ERROR_CODE_CONVERT_MESSAGE_MAP; 185 186 static const std::map<int32_t, std::string> PARAMETER_TYPE; 187 188 static constexpr int32_t ERROR_CODE_QUERY_FAILED = 1; 189 190 static constexpr uint8_t MAX_ARGMENT_COUNT = 10; 191 }; 192 } // namespace MiscServices 193 } // namespace OHOS 194 #endif // INTERFACE_KITS_JS_UTILS_H