• 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 };
49 
50 enum TypeCode : int32_t {
51     TYPE_NONE = 0,
52     TYPE_UNDEFINED,
53     TYPE_NULL,
54     TYPE_BOOLEAN,
55     TYPE_NUMBER,
56     TYPE_STRING,
57     TYPE_SYMBOL,
58     TYPE_OBJECT,
59     TYPE_FUNCTION,
60     TYPE_EXTERNAL,
61     TYPE_BIGINT,
62 };
63 
64 /* check condition, return and logging if condition not true. */
65 #define PARAM_CHECK_RETURN(env, condition, message, typeCode, retVal)                            \
66     do {                                                                                         \
67         if (!(condition)) {                                                                      \
68             JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \
69             return retVal;                                                                       \
70         }                                                                                        \
71     } while (0)
72 
73 /* check condition, return and logging. */
74 #define CHECK_RETURN_VOID(condition, message)                      \
75     do {                                                           \
76         if (!(condition)) {                                        \
77             IMSA_HILOGE("test (" #condition ") failed: " message); \
78             return;                                                \
79         }                                                          \
80     } while (0)
81 
82 /* check condition, return and logging. */
83 #define CHECK_RETURN(condition, message, retVal)                   \
84     do {                                                           \
85         if (!(condition)) {                                        \
86             IMSA_HILOGE("test (" #condition ") failed: " message); \
87             return retVal;                                         \
88         }                                                          \
89     } while (0)
90 
91 class JsUtils {
92 public:
93     static void ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type);
94 
95     static napi_value ToError(napi_env env, int32_t code);
96 
97     static int32_t Convert(int32_t code);
98 
99     static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId);
100 
101     static void *GetNativeSelf(napi_env env, napi_callback_info info);
102 
103     static napi_status GetValue(napi_env env, napi_value in, int32_t &out);
104     static napi_status GetValue(napi_env env, napi_value in, uint32_t &out);
105     static napi_status GetValue(napi_env env, napi_value in, bool &out);
106     static napi_status GetValue(napi_env env, napi_value in, double &out);
107     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
108     static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out);
109     static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out);
110     static napi_value GetValue(napi_env env, const std::vector<InputWindowInfo> &in);
111     static napi_value GetValue(napi_env env, const InputWindowInfo &in);
112     static napi_value GetValue(napi_env env, const InputAttribute &attribute);
113     static napi_status GetValue(napi_env env, const std::string &in, napi_value &out);
114 
115 private:
116     static const std::string ToMessage(int32_t code);
117 
118     static const std::map<int32_t, int32_t> ERROR_CODE_MAP;
119 
120     static const std::map<int32_t, std::string> ERROR_CODE_CONVERT_MESSAGE_MAP;
121 
122     static const std::map<int32_t, std::string> PARAMETER_TYPE;
123 
124     static constexpr int32_t ERROR_CODE_QUERY_FAILED = 1;
125 
126     static constexpr uint8_t MAX_ARGMENT_COUNT = 10;
127 };
128 } // namespace MiscServices
129 } // namespace OHOS
130 #endif // INTERFACE_KITS_JS_UTILS_H