122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#ifndef INTERFACE_KITS_JS_UTILS_H
1722736c2fSopenharmony_ci#define INTERFACE_KITS_JS_UTILS_H
1822736c2fSopenharmony_ci
1922736c2fSopenharmony_ci#include <map>
2022736c2fSopenharmony_ci
2122736c2fSopenharmony_ci#include "ability.h"
2222736c2fSopenharmony_ci#include "global.h"
2322736c2fSopenharmony_ci#include "input_method_panel.h"
2422736c2fSopenharmony_ci#include "input_method_utils.h"
2522736c2fSopenharmony_ci#include "js_callback_object.h"
2622736c2fSopenharmony_ci#include "napi/native_api.h"
2722736c2fSopenharmony_ci#include "napi/native_common.h"
2822736c2fSopenharmony_ci#include "napi/native_node_api.h"
2922736c2fSopenharmony_ci#include "string_ex.h"
3022736c2fSopenharmony_ci
3122736c2fSopenharmony_ciusing Ability = OHOS::AppExecFwk::Ability;
3222736c2fSopenharmony_cinamespace OHOS {
3322736c2fSopenharmony_cinamespace MiscServices {
3422736c2fSopenharmony_cienum IMFErrorCode : int32_t {
3522736c2fSopenharmony_ci    EXCEPTION_PERMISSION = 201,
3622736c2fSopenharmony_ci    EXCEPTION_SYSTEM_PERMISSION = 202,
3722736c2fSopenharmony_ci    EXCEPTION_PARAMCHECK = 401,
3822736c2fSopenharmony_ci    EXCEPTION_UNSUPPORTED = 801,
3922736c2fSopenharmony_ci    EXCEPTION_PACKAGEMANAGER = 12800001,
4022736c2fSopenharmony_ci    EXCEPTION_IMENGINE = 12800002,
4122736c2fSopenharmony_ci    EXCEPTION_IMCLIENT = 12800003,
4222736c2fSopenharmony_ci    EXCEPTION_IME = 12800004,
4322736c2fSopenharmony_ci    EXCEPTION_CONFPERSIST = 12800005,
4422736c2fSopenharmony_ci    EXCEPTION_CONTROLLER = 12800006,
4522736c2fSopenharmony_ci    EXCEPTION_SETTINGS = 12800007,
4622736c2fSopenharmony_ci    EXCEPTION_IMMS = 12800008,
4722736c2fSopenharmony_ci    EXCEPTION_DETACHED = 12800009,
4822736c2fSopenharmony_ci    EXCEPTION_DEFAULTIME = 12800010,
4922736c2fSopenharmony_ci    EXCEPTION_TEXT_PREVIEW_NOT_SUPPORTED = 12800011,
5022736c2fSopenharmony_ci    EXCEPTION_PANEL_NOT_FOUND = 12800012,
5122736c2fSopenharmony_ci    EXCEPTION_WINDOW_MANAGER = 12800013,
5222736c2fSopenharmony_ci};
5322736c2fSopenharmony_ci
5422736c2fSopenharmony_cienum TypeCode : int32_t {
5522736c2fSopenharmony_ci    TYPE_NONE = 0,
5622736c2fSopenharmony_ci    TYPE_UNDEFINED,
5722736c2fSopenharmony_ci    TYPE_NULL,
5822736c2fSopenharmony_ci    TYPE_BOOLEAN,
5922736c2fSopenharmony_ci    TYPE_NUMBER,
6022736c2fSopenharmony_ci    TYPE_STRING,
6122736c2fSopenharmony_ci    TYPE_SYMBOL,
6222736c2fSopenharmony_ci    TYPE_OBJECT,
6322736c2fSopenharmony_ci    TYPE_FUNCTION,
6422736c2fSopenharmony_ci    TYPE_EXTERNAL,
6522736c2fSopenharmony_ci    TYPE_BIGINT,
6622736c2fSopenharmony_ci};
6722736c2fSopenharmony_ci
6822736c2fSopenharmony_ci/* check condition, return and logging if condition not true. */
6922736c2fSopenharmony_ci#define PARAM_CHECK_RETURN(env, condition, message, typeCode, retVal)                            \
7022736c2fSopenharmony_ci    do {                                                                                         \
7122736c2fSopenharmony_ci        if (!(condition)) {                                                                      \
7222736c2fSopenharmony_ci            JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, message, typeCode); \
7322736c2fSopenharmony_ci            return retVal;                                                                       \
7422736c2fSopenharmony_ci        }                                                                                        \
7522736c2fSopenharmony_ci    } while (0)
7622736c2fSopenharmony_ci
7722736c2fSopenharmony_ci/* check condition, return and logging. */
7822736c2fSopenharmony_ci#define CHECK_RETURN_VOID(condition, message)                      \
7922736c2fSopenharmony_ci    do {                                                           \
8022736c2fSopenharmony_ci        if (!(condition)) {                                        \
8122736c2fSopenharmony_ci            IMSA_HILOGE("test (" #condition ") failed: " message); \
8222736c2fSopenharmony_ci            return;                                                \
8322736c2fSopenharmony_ci        }                                                          \
8422736c2fSopenharmony_ci    } while (0)
8522736c2fSopenharmony_ci
8622736c2fSopenharmony_ci/* check condition, return and logging. */
8722736c2fSopenharmony_ci#define CHECK_RETURN(condition, message, retVal)                   \
8822736c2fSopenharmony_ci    do {                                                           \
8922736c2fSopenharmony_ci        if (!(condition)) {                                        \
9022736c2fSopenharmony_ci            IMSA_HILOGE("test (" #condition ") failed: " message); \
9122736c2fSopenharmony_ci            return retVal;                                         \
9222736c2fSopenharmony_ci        }                                                          \
9322736c2fSopenharmony_ci    } while (0)
9422736c2fSopenharmony_ci
9522736c2fSopenharmony_ciclass JsUtils {
9622736c2fSopenharmony_cipublic:
9722736c2fSopenharmony_ci    static void ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type);
9822736c2fSopenharmony_ci
9922736c2fSopenharmony_ci    static napi_value ToError(napi_env env, int32_t code, const std::string &msg);
10022736c2fSopenharmony_ci
10122736c2fSopenharmony_ci    static int32_t Convert(int32_t code);
10222736c2fSopenharmony_ci
10322736c2fSopenharmony_ci    static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId);
10422736c2fSopenharmony_ci
10522736c2fSopenharmony_ci    static void *GetNativeSelf(napi_env env, napi_callback_info info);
10622736c2fSopenharmony_ci
10722736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, int32_t &out);
10822736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, uint32_t &out);
10922736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, bool &out);
11022736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, double &out);
11122736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, std::string &out);
11222736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, std::unordered_map<std::string, PrivateDataValue> &out);
11322736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, PrivateDataValue &out);
11422736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out);
11522736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out);
11622736c2fSopenharmony_ci    static napi_value GetValue(napi_env env, const std::vector<InputWindowInfo> &in);
11722736c2fSopenharmony_ci    static napi_value GetValue(napi_env env, const InputWindowInfo &in);
11822736c2fSopenharmony_ci    static napi_value GetJsPrivateCommand(napi_env env, const std::unordered_map<std::string, PrivateDataValue> &in);
11922736c2fSopenharmony_ci    static napi_status GetValue(napi_env env, const std::string &in, napi_value &out);
12022736c2fSopenharmony_ci
12122736c2fSopenharmony_ciprivate:
12222736c2fSopenharmony_ci    static const std::string ToMessage(int32_t code);
12322736c2fSopenharmony_ci
12422736c2fSopenharmony_ci    static const std::map<int32_t, int32_t> ERROR_CODE_MAP;
12522736c2fSopenharmony_ci
12622736c2fSopenharmony_ci    static const std::map<int32_t, std::string> ERROR_CODE_CONVERT_MESSAGE_MAP;
12722736c2fSopenharmony_ci
12822736c2fSopenharmony_ci    static const std::map<int32_t, std::string> PARAMETER_TYPE;
12922736c2fSopenharmony_ci
13022736c2fSopenharmony_ci    static constexpr int32_t ERROR_CODE_QUERY_FAILED = 1;
13122736c2fSopenharmony_ci
13222736c2fSopenharmony_ci    static constexpr uint8_t MAX_ARGMENT_COUNT = 10;
13322736c2fSopenharmony_ci};
13422736c2fSopenharmony_ci} // namespace MiscServices
13522736c2fSopenharmony_ci} // namespace OHOS
13622736c2fSopenharmony_ci#endif // INTERFACE_KITS_JS_UTILS_H