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