• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     EXCEPTION_IME_NOT_FOUND = 12800018,
58     EXCEPTION_OPERATE_DEFAULTIME = 12800019,
59     EXCEPTION_INVALID_IMMERSIVE_EFFECT = 12800020,
60     EXCEPTION_PRECONDITION_REQUIRED = 12800021,
61 };
62 
63 enum TypeCode : int32_t {
64     TYPE_NONE = 0,
65     TYPE_UNDEFINED,
66     TYPE_NULL,
67     TYPE_BOOLEAN,
68     TYPE_NUMBER,
69     TYPE_STRING,
70     TYPE_SYMBOL,
71     TYPE_OBJECT,
72     TYPE_FUNCTION,
73     TYPE_EXTERNAL,
74     TYPE_BIGINT,
75     TYPE_ARRAY_BUFFER,
76     TYPE_ARRAY,
77 };
78 
79 /* check condition, return and logging if condition not true. */
80 #define PARAM_CHECK_RETURN(env, condition, message, typeCode, retVal)                            \
81     do {                                                                                         \
82         if (!(condition)) {                                                                      \
83             JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \
84             return retVal;                                                                       \
85         }                                                                                        \
86     } while (0)
87 
88 #define PARAM_CHECK_RETURN_VOID(env, condition, message, typeCode)                            \
89     do {                                                                                         \
90         if (!(condition)) {                                                                      \
91             JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \
92             return;                                                                       \
93         }                                                                                        \
94     } while (0)
95 
96 #define RESULT_CHECK_RETURN(env, condition, errCode, message, typeCode, retVal) \
97     do {                                                                       \
98         if (!(condition)) {                                                    \
99             JsUtils::ThrowException(env, errCode, message, typeCode);          \
100             return retVal;                                                     \
101         }                                                                      \
102     } while (0)
103 
104 #define RESULT_CHECK_RETURN_VOID(env, condition, errCode, message, typeCode) \
105     do {                                                                     \
106         if (!(condition)) {                                                  \
107             JsUtils::ThrowException(env, errCode, message, typeCode);        \
108             return;                                                          \
109         }                                                                    \
110     } while (0)
111 
112 /* check condition, return and logging. */
113 #define CHECK_RETURN_VOID(condition, message)                      \
114     do {                                                           \
115         if (!(condition)) {                                        \
116             IMSA_HILOGE("test (" #condition ") failed: " message); \
117             return;                                                \
118         }                                                          \
119     } while (0)
120 
121 /* check condition, return and logging. */
122 #define CHECK_RETURN(condition, message, retVal)                   \
123     do {                                                           \
124         if (!(condition)) {                                        \
125             IMSA_HILOGE("test (" #condition ") failed: " message); \
126             return retVal;                                         \
127         }                                                          \
128     } while (0)
129 
130 struct JsPropertyInfo {
131     napi_valuetype type;
132     TypeCode typeCode;
133     std::string propertyName;
134 };
135 
136 class JsUtils {
137 public:
138     static void ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type);
139 
140     static napi_value ToError(napi_env env, int32_t code, const std::string &msg);
141 
142     static int32_t Convert(int32_t code);
143 
144     static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId);
145 
146     static void *GetNativeSelf(napi_env env, napi_callback_info info);
147 
148     static const std::string ToMessage(int32_t code);
149 
150     template<typename T>
ReadOptionalProperty(napi_env env,napi_value object,const JsPropertyInfo & jsPropInfo,T & value)151     static bool ReadOptionalProperty(napi_env env, napi_value object, const JsPropertyInfo &jsPropInfo, T &value)
152     {
153         if (!JsUtil::HasProperty(env, object, jsPropInfo.propertyName.c_str())) {
154             return false;
155         }
156         napi_value jsObject = nullptr;
157         napi_get_named_property(env, object, jsPropInfo.propertyName.c_str(), &jsObject);
158         PARAM_CHECK_RETURN(env, JsUtil::GetType(env, jsObject) == jsPropInfo.type, jsPropInfo.propertyName,
159             jsPropInfo.typeCode, false);
160         PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, jsObject, value) == napi_ok,
161             "failed to convert " + jsPropInfo.propertyName, TYPE_NONE, false);
162         return true;
163     }
164 
165     static napi_status GetValue(napi_env env, napi_value in, int32_t &out);
166     static napi_status GetValue(napi_env env, napi_value in, uint32_t &out);
167     static napi_status GetValue(napi_env env, napi_value in, bool &out);
168     static napi_status GetValue(napi_env env, napi_value in, double &out);
169     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
170     static napi_status GetValue(napi_env env, napi_value in, std::unordered_map<std::string, PrivateDataValue> &out);
171     static napi_status GetValue(napi_env env, napi_value in, PrivateDataValue &out);
172     static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out);
173     static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out);
174     static napi_status GetValue(napi_env env, napi_value in, Rosen::Rect &out);
175     static napi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out);
176     static napi_value GetValue(napi_env env, const std::vector<InputWindowInfo> &in);
177     static napi_value GetValue(napi_env env, const InputWindowInfo &in);
178     static napi_value GetValue(napi_env env, const Rosen::Rect &in);
179     static napi_value GetJsPrivateCommand(napi_env env, const std::unordered_map<std::string, PrivateDataValue> &in);
180     static napi_value GetValue(napi_env env, const std::vector<uint8_t> &in);
181     static napi_status GetValue(napi_env env, const std::string &in, napi_value &out);
182     static napi_status GetMessageHandlerCallbackParam(napi_value *argv,
183         const std::shared_ptr<JSMsgHandlerCallbackObject> &jsMessageHandler, const ArrayBuffer &arrayBuffer,
184             size_t size);
185 
186 private:
187     static const std::map<int32_t, int32_t> ERROR_CODE_MAP;
188 
189     static const std::map<int32_t, std::string> ERROR_CODE_CONVERT_MESSAGE_MAP;
190 
191     static const std::map<int32_t, std::string> PARAMETER_TYPE;
192 
193     static constexpr int32_t ERROR_CODE_QUERY_FAILED = 1;
194 
195     static constexpr uint8_t MAX_ARGMENT_COUNT = 10;
196 };
197 } // namespace MiscServices
198 } // namespace OHOS
199 #endif // INTERFACE_KITS_JS_UTILS_H