• 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.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             NapiUtils::ThrowError(env, message, code);           \
34             return retVal;                                       \
35         }                                                        \
36     } while (0)
37 
38 #define NAPI_ASSERTP(env, assertion, message) \
39     NAPI_ASSERTS_BASE(env, assertion, ERROR, 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_ZERO = 0;
81 constexpr size_t ARGC_ONE = 1;
82 constexpr size_t ARGC_TWO = 2;
83 constexpr size_t ARGC_THREE = 3;
84 
85 constexpr size_t ARGV_FIRST = 0;
86 constexpr size_t ARGV_SECOND = 1;
87 constexpr size_t ARGV_THIRD = 2;
88 
89 constexpr int32_t SET_TIME_MAX_PARA = 2;
90 constexpr int32_t SET_TIMEZONE_MAX_PARA = 2;
91 
92 constexpr int32_t CREATE_MAX_PARA = 2;
93 constexpr int32_t START_MAX_PARA = 3;
94 constexpr int32_t STOP_MAX_PARA = 2;
95 constexpr int32_t DESTROY_MAX_PARA = 2;
96 
97 constexpr int32_t MAX_TIME_ZONE_ID = 1024;
98 constexpr int32_t INVALID_TIME = -1;
99 
100 enum JsErrorCode : int32_t {
101     ERROR_OK = 0,
102     ERROR = -1,
103     PERMISSION_ERROR = 201,
104     SYSTEM_APP_ERROR = 202,
105     PARAMETER_ERROR = 401,
106 };
107 
108 struct CallbackPromiseInfo {
109     napi_ref callback = nullptr;
110     napi_deferred deferred = nullptr;
111     bool isCallback = false;
112     int errorCode = JsErrorCode::ERROR_OK;
113     std::string message;
114 };
115 
116 const std::map<int32_t, std::string> CODE_TO_MESSAGE = {
117     { JsErrorCode::SYSTEM_APP_ERROR, "not system app" },
118     { JsErrorCode::PARAMETER_ERROR, "parameter invalid" },
119     { JsErrorCode::PERMISSION_ERROR, "permission denied" },
120     { JsErrorCode::ERROR, "system error" },
121 };
122 
123 class NapiUtils {
124 public:
125     static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode, const std::string &message);
126     static napi_value NapiGetNull(napi_env env);
127     static void SetPromise(napi_env env, const napi_deferred deferred, int errorCode, const std::string &message,
128         napi_value result);
129     static void SetCallback(napi_env env, napi_ref callbackIn, int32_t errorCode, const std::string &message,
130         napi_value result);
131     static void ReturnCallbackPromise(napi_env env, const CallbackPromiseInfo &info, napi_value result);
132     static napi_value JSParaError(napi_env env, napi_ref callback);
133     static napi_value ParseParametersBySetTime(napi_env env, const napi_value (&argv)[SET_TIME_MAX_PARA], size_t argc,
134         int64_t &times, napi_ref &callback);
135     static napi_value ParseParametersBySetTimezone(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA],
136         size_t argc, std::string &timezoneId, napi_ref &callback);
137     static napi_value ParseParametersGet(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc,
138         napi_ref &callback);
139     static napi_value ParseParametersGetNA(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc,
140         napi_ref &callback, bool *isNano);
141     static int32_t ConvertErrorCode(int32_t timeErrorCode);
142     static napi_value CreateNapiNumber(napi_env env, int32_t objName);
143     static napi_value GetUndefinedValue(napi_env env);
144     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
145     static napi_status ThrowError(napi_env env, const char *napiMessage, int32_t napiCode);
146     static std::string GetErrorMessage(int32_t errCode);
147 };
148 } // namespace Time
149 } // namespace MiscServices
150 } // namespace OHOS
151 
152 #endif // NAPI_UTILS_H