• 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 "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "napi/native_node_api.h"
29 #include "string_ex.h"
30 
31 using Ability = OHOS::AppExecFwk::Ability;
32 namespace OHOS {
33 namespace MiscServices {
34 enum IMFErrorCode : int32_t {
35     EXCEPTION_PERMISSION = 201,
36     EXCEPTION_SYSTEM_PERMISSION = 202,
37     EXCEPTION_PARAMCHECK = 401,
38     EXCEPTION_UNSUPPORTED = 801,
39     EXCEPTION_PACKAGEMANAGER = 12800001,
40     EXCEPTION_IMENGINE,
41     EXCEPTION_IMCLIENT,
42     EXCEPTION_IME,
43     EXCEPTION_CONFPERSIST,
44     EXCEPTION_CONTROLLER,
45     EXCEPTION_SETTINGS,
46     EXCEPTION_IMMS,
47     EXCEPTION_DETACHED,
48     EXCEPTION_DEFAULTIME,
49 };
50 
51 enum TypeCode : int32_t {
52     TYPE_NONE = 0,
53     TYPE_UNDEFINED,
54     TYPE_NULL,
55     TYPE_BOOLEAN,
56     TYPE_NUMBER,
57     TYPE_STRING,
58     TYPE_SYMBOL,
59     TYPE_OBJECT,
60     TYPE_FUNCTION,
61     TYPE_EXTERNAL,
62     TYPE_BIGINT,
63 };
64 
65 /* check condition, return and logging if condition not true. */
66 #define PARAM_CHECK_RETURN(env, condition, message, typeCode, retVal)                            \
67     do {                                                                                         \
68         if (!(condition)) {                                                                      \
69             JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \
70             return retVal;                                                                       \
71         }                                                                                        \
72     } while (0)
73 
74 /* check condition, return and logging. */
75 #define CHECK_RETURN_VOID(condition, message)                      \
76     do {                                                           \
77         if (!(condition)) {                                        \
78             IMSA_HILOGE("test (" #condition ") failed: " message); \
79             return;                                                \
80         }                                                          \
81     } while (0)
82 
83 /* check condition, return and logging. */
84 #define CHECK_RETURN(condition, message, retVal)                   \
85     do {                                                           \
86         if (!(condition)) {                                        \
87             IMSA_HILOGE("test (" #condition ") failed: " message); \
88             return retVal;                                         \
89         }                                                          \
90     } while (0)
91 
92 class JsUtils {
93 public:
94     static void ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type);
95 
96     static napi_value ToError(napi_env env, int32_t code);
97 
98     static int32_t Convert(int32_t code);
99 
100     static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId);
101 
102     static void *GetNativeSelf(napi_env env, napi_callback_info info);
103 
104     static napi_status GetValue(napi_env env, napi_value in, int32_t &out);
105     static napi_status GetValue(napi_env env, napi_value in, uint32_t &out);
106     static napi_status GetValue(napi_env env, napi_value in, bool &out);
107     static napi_status GetValue(napi_env env, napi_value in, double &out);
108     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
109     static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out);
110     static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out);
111     static napi_value GetValue(napi_env env, const std::vector<InputWindowInfo> &in);
112     static napi_value GetValue(napi_env env, const InputWindowInfo &in);
113     static napi_value GetValue(napi_env env, const InputAttribute &attribute);
114     static napi_status GetValue(napi_env env, const std::string &in, napi_value &out);
115 
116 private:
117     static const std::string ToMessage(int32_t code);
118 
119     static const std::map<int32_t, int32_t> ERROR_CODE_MAP;
120 
121     static const std::map<int32_t, std::string> ERROR_CODE_CONVERT_MESSAGE_MAP;
122 
123     static const std::map<int32_t, std::string> PARAMETER_TYPE;
124 
125     static constexpr int32_t ERROR_CODE_QUERY_FAILED = 1;
126 
127     static constexpr uint8_t MAX_ARGMENT_COUNT = 10;
128 };
129 } // namespace MiscServices
130 } // namespace OHOS
131 #endif // INTERFACE_KITS_JS_UTILS_H