• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 NAPI_UTILS_H
17 #define NAPI_UTILS_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "js_native_api.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "time_hilog_wreapper.h"
27 namespace OHOS {
28 namespace MiscServices {
29 namespace Time {
30 #define NAPI_ASSERTS_BASE(env, assertion, code, message, retVal) \
31     do {                                                         \
32         if (!(assertion)) {                                      \
33             napi_throw_error((env), code, message);              \
34             return retVal;                                       \
35         }                                                        \
36     } while (0)
37 
38 #define NAPI_ASSERTP(env, assertion, message) \
39     NAPI_ASSERTS_BASE(env, assertion, std::to_string(ERROR).c_str(), message, nullptr)
40 
41 /* check condition related to argc/argv, return and logging. */
42 #define CHECK_ARGS_RETURN_VOID(module, context, condition, message, code)  \
43     do {                                                                   \
44         if (!(condition)) {                                                \
45             (context)->status = napi_invalid_arg;                          \
46             (context)->errMessage = std::string(message);                  \
47             (context)->errCode = code;                                     \
48             TIME_HILOGE(module, "test (" #condition ") failed: " message); \
49             return;                                                        \
50         }                                                                  \
51     } while (0)
52 
53 #define CHECK_STATUS_RETURN_VOID(module, context, message, code)                       \
54     do {                                                                               \
55         if ((context)->status != napi_ok) {                                            \
56             (context)->errMessage = std::string(message);                              \
57             (context)->errCode = code;                                                 \
58             TIME_HILOGE(module, "test (context->status == napi_ok) failed: " message); \
59             return;                                                                    \
60         }                                                                              \
61     } while (0)
62 
63 /* check condition, return and logging if condition not true. */
64 #define CHECK_RETURN(module, condition, message, retVal)                   \
65     do {                                                                   \
66         if (!(condition)) {                                                \
67             TIME_HILOGE(module, "test (" #condition ") failed: " message); \
68             return retVal;                                                 \
69         }                                                                  \
70     } while (0)
71 
72 #define CHECK_RETURN_VOID(module, condition, message)                      \
73     do {                                                                   \
74         if (!(condition)) {                                                \
75             TIME_HILOGE(module, "test (" #condition ") failed: " message); \
76             return;                                                        \
77         }                                                                  \
78     } while (0)
79 
80 constexpr size_t ARGC_ONE = 1;
81 constexpr size_t ARGC_TWO = 2;
82 constexpr size_t ARGC_THREE = 3;
83 
84 constexpr size_t ARGV_FIRST = 0;
85 constexpr size_t ARGV_SECOND = 1;
86 constexpr size_t ARGV_THIRD = 2;
87 
88 constexpr int32_t SET_TIME_MAX_PARA = 2;
89 constexpr int32_t SET_TIMEZONE_MAX_PARA = 2;
90 
91 constexpr int32_t CREATE_MAX_PARA = 2;
92 constexpr int32_t START_MAX_PARA = 3;
93 constexpr int32_t STOP_MAX_PARA = 2;
94 constexpr int32_t DESTROY_MAX_PARA = 2;
95 
96 constexpr int32_t MAX_TIME_ZONE_ID = 1024;
97 constexpr int32_t INVALID_TIME = -1;
98 
99 enum JsErrorCode : int32_t {
100     ERROR_OK = 0,
101     ERROR = -1,
102     PERMISSION_ERROR = 201,
103     SYSTEM_APP_ERROR = 202,
104     PARAMETER_ERROR = 401,
105 };
106 
107 struct CallbackPromiseInfo {
108     napi_ref callback = nullptr;
109     napi_deferred deferred = nullptr;
110     bool isCallback = false;
111     int errorCode = JsErrorCode::ERROR_OK;
112     std::string message;
113 };
114 
115 const std::map<int32_t, std::string> CODE_TO_MESSAGE = {
116     { JsErrorCode::SYSTEM_APP_ERROR, "not system app" },
117     { JsErrorCode::PARAMETER_ERROR, "parameter invalid" },
118     { JsErrorCode::PERMISSION_ERROR, "permission denied" },
119     { JsErrorCode::ERROR, "system error" },
120 };
121 
122 class NapiUtils {
123 public:
124     static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode, const std::string &message);
125     static napi_value NapiGetNull(napi_env env);
126     static void SetPromise(napi_env env, const napi_deferred deferred, int errorCode, const std::string &message,
127         napi_value result);
128     static void SetCallback(napi_env env, napi_ref callbackIn, int32_t errorCode, const std::string &message,
129         napi_value result);
130     static void ReturnCallbackPromise(napi_env env, const CallbackPromiseInfo &info, napi_value result);
131     static napi_value JSParaError(napi_env env, napi_ref callback);
132     static napi_value ParseParametersBySetTime(napi_env env, const napi_value (&argv)[SET_TIME_MAX_PARA], size_t argc,
133         int64_t &times, napi_ref &callback);
134     static napi_value ParseParametersBySetTimezone(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA],
135         size_t argc, std::string &timezoneId, napi_ref &callback);
136     static napi_value ParseParametersGet(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc,
137         napi_ref &callback);
138     static napi_value ParseParametersGetNA(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc,
139         napi_ref &callback, bool *isNano);
140     static int32_t ConvertErrorCode(int32_t timeErrorCode);
141     static napi_value CreateNapiNumber(napi_env env, int32_t objName);
142     static napi_value GetUndefinedValue(napi_env env);
143     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
144     static void SetTimerCallback(const napi_env &env, const napi_ref &callbackIn, const int &errorCode,
145         const char *message, const napi_value &result);
146     static napi_status ThrowError(napi_env env, const char *napiMessage, int32_t napiCode);
147     static std::string GetErrorMessage(int32_t errCode);
148 };
149 } // namespace Time
150 } // namespace MiscServices
151 } // namespace OHOS
152 
153 #endif // NAPI_UTILS_H