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 #include "napi_utils.h"
17
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20 #include "time_common.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 namespace Time {
25 static constexpr int32_t STR_MAX_LENGTH = 4096;
26 static constexpr size_t STR_TAIL_LENGTH = 1;
27 static constexpr const char *SYSTEM_ERROR = "system error";
ConvertErrorCode(int32_t timeErrorCode)28 int32_t NapiUtils::ConvertErrorCode(int32_t timeErrorCode)
29 {
30 switch (timeErrorCode) {
31 case MiscServices::E_TIME_NOT_SYSTEM_APP:
32 return JsErrorCode::SYSTEM_APP_ERROR;
33 case MiscServices::E_TIME_NO_PERMISSION:
34 return JsErrorCode::PERMISSION_ERROR;
35 case MiscServices::E_TIME_PARAMETERS_INVALID:
36 return JsErrorCode::PARAMETER_ERROR;
37 default:
38 return JsErrorCode::ERROR;
39 }
40 }
41
CreateNapiNumber(napi_env env,int32_t objName)42 napi_value NapiUtils::CreateNapiNumber(napi_env env, int32_t objName)
43 {
44 napi_value prop = nullptr;
45 napi_create_int32(env, objName, &prop);
46 return prop;
47 }
48
GetUndefinedValue(napi_env env)49 napi_value NapiUtils::GetUndefinedValue(napi_env env)
50 {
51 napi_value result{};
52 napi_get_undefined(env, &result);
53 return result;
54 }
55
GetValue(napi_env env,napi_value in,std::string & out)56 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::string &out)
57 {
58 napi_valuetype type = napi_undefined;
59 napi_status status = napi_typeof(env, in, &type);
60 CHECK_RETURN(TIME_MODULE_JS_NAPI, (status == napi_ok) && (type == napi_string), "invalid type", napi_invalid_arg);
61 size_t maxLen = STR_MAX_LENGTH;
62 napi_get_value_string_utf8(env, in, nullptr, 0, &maxLen);
63 if (maxLen >= STR_MAX_LENGTH) {
64 return napi_invalid_arg;
65 }
66 char buf[STR_MAX_LENGTH + STR_TAIL_LENGTH]{};
67 size_t len = 0;
68 status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len);
69 if (status == napi_ok) {
70 out = std::string(buf);
71 }
72 return status;
73 }
74
ThrowError(napi_env env,const char * napiMessage,int32_t napiCode)75 napi_status NapiUtils::ThrowError(napi_env env, const char *napiMessage, int32_t napiCode)
76 {
77 napi_value message = nullptr;
78 napi_value code = nullptr;
79 napi_value result = nullptr;
80 napi_create_string_utf8(env, napiMessage, NAPI_AUTO_LENGTH, &message);
81 napi_create_error(env, nullptr, message, &result);
82 napi_create_int32(env, napiCode, &code);
83 napi_set_named_property(env, result, "code", code);
84 napi_throw(env, result);
85 return napi_ok;
86 }
87
GetErrorMessage(int32_t errCode)88 std::string NapiUtils::GetErrorMessage(int32_t errCode)
89 {
90 auto it = CODE_TO_MESSAGE.find(errCode);
91 if (it != CODE_TO_MESSAGE.end()) {
92 return it->second;
93 }
94 return std::string(SYSTEM_ERROR);
95 }
96
GetCallbackErrorValue(napi_env env,int32_t errCode,const std::string & message)97 napi_value NapiUtils::GetCallbackErrorValue(napi_env env, int32_t errCode, const std::string &message)
98 {
99 napi_value result = nullptr;
100 napi_value eCode = nullptr;
101 if (errCode == JsErrorCode::ERROR_OK) {
102 napi_get_undefined(env, &result);
103 return result;
104 }
105 NAPI_CALL(env, napi_create_object(env, &result));
106 if (errCode == JsErrorCode::ERROR) {
107 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
108 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
109
110 napi_value str;
111 size_t str_len = strlen(message.c_str());
112 NAPI_CALL(env, napi_create_string_utf8(env, message.c_str(), str_len, &str));
113 NAPI_CALL(env, napi_set_named_property(env, result, "message", str));
114 }
115 return result;
116 }
117
NapiGetNull(napi_env env)118 napi_value NapiUtils::NapiGetNull(napi_env env)
119 {
120 napi_value result = nullptr;
121 napi_get_null(env, &result);
122 return result;
123 }
124
SetPromise(napi_env env,napi_deferred deferred,int32_t errorCode,const std::string & message,napi_value result)125 void NapiUtils::SetPromise(napi_env env, napi_deferred deferred, int32_t errorCode, const std::string &message,
126 napi_value result)
127 {
128 if (errorCode == JsErrorCode::ERROR_OK) {
129 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, deferred, result));
130 return;
131 }
132 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, deferred, GetCallbackErrorValue(env, errorCode, message)));
133 }
134
SetCallback(napi_env env,napi_ref callbackIn,int32_t errorCode,const std::string & message,napi_value result)135 void NapiUtils::SetCallback(napi_env env, napi_ref callbackIn, int32_t errorCode, const std::string &message,
136 napi_value result)
137 {
138 napi_value undefined = nullptr;
139 napi_get_undefined(env, &undefined);
140
141 napi_value callback = nullptr;
142 napi_value resultOut = nullptr;
143 napi_get_reference_value(env, callbackIn, &callback);
144 napi_value results[2] = { 0 };
145 results[0] = GetCallbackErrorValue(env, errorCode, message);
146 results[1] = result;
147 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGC_TWO, &results[0], &resultOut));
148 }
149
ReturnCallbackPromise(napi_env env,const CallbackPromiseInfo & info,napi_value result)150 void NapiUtils::ReturnCallbackPromise(napi_env env, const CallbackPromiseInfo &info, napi_value result)
151 {
152 if (info.isCallback) {
153 SetCallback(env, info.callback, info.errorCode, info.message, result);
154 } else {
155 SetPromise(env, info.deferred, info.errorCode, info.message, result);
156 }
157 }
158
JSParaError(napi_env env,napi_ref callback)159 napi_value NapiUtils::JSParaError(napi_env env, napi_ref callback)
160 {
161 if (callback) {
162 return GetCallbackErrorValue(env, ERROR, NapiUtils::GetErrorMessage(JsErrorCode::PARAMETER_ERROR));
163 } else {
164 napi_value promise = nullptr;
165 napi_deferred deferred = nullptr;
166 napi_create_promise(env, &deferred, &promise);
167 SetPromise(env, deferred, ERROR, NapiUtils::GetErrorMessage(JsErrorCode::PARAMETER_ERROR), NapiGetNull(env));
168 return promise;
169 }
170 }
171
ParseParametersBySetTime(napi_env env,const napi_value (& argv)[SET_TIME_MAX_PARA],size_t argc,int64_t & times,napi_ref & callback)172 napi_value NapiUtils::ParseParametersBySetTime(napi_env env, const napi_value (&argv)[SET_TIME_MAX_PARA], size_t argc,
173 int64_t ×, napi_ref &callback)
174 {
175 NAPI_ASSERTP(env, argc >= SET_TIME_MAX_PARA - 1, "Wrong number of arguments");
176 napi_valuetype valueType = napi_undefined;
177
178 // argv[0]: times or date object
179 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
180 NAPI_ASSERTP(env, valueType == napi_number || valueType == napi_object,
181 "Parameter error. The type of time must be number or date.");
182 if (valueType == napi_number) {
183 napi_get_value_int64(env, argv[0], ×);
184 NAPI_ASSERTP(env, times >= 0, "Wrong argument timer. Positive number expected.");
185 } else {
186 bool hasProperty = false;
187 napi_valuetype resValueType = napi_undefined;
188 NAPI_CALL(env, napi_has_named_property(env, argv[0], "getTime", &hasProperty));
189 NAPI_ASSERTP(env, hasProperty, "type expected.");
190 napi_value getTimeFunc = nullptr;
191 napi_get_named_property(env, argv[0], "getTime", &getTimeFunc);
192 napi_value getTimeResult = nullptr;
193 napi_call_function(env, argv[0], getTimeFunc, 0, nullptr, &getTimeResult);
194 NAPI_CALL(env, napi_typeof(env, getTimeResult, &resValueType));
195 NAPI_ASSERTP(env, resValueType == napi_number, "type mismatch");
196 napi_get_value_int64(env, getTimeResult, ×);
197 }
198
199 // argv[1]:callback
200 if (argc >= SET_TIME_MAX_PARA) {
201 NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
202 NAPI_ASSERTP(env, valueType == napi_function, "Parameter error. The type of callback must be function.");
203 napi_create_reference(env, argv[1], 1, &callback);
204 }
205 return NapiGetNull(env);
206 }
207
ParseParametersBySetTimezone(napi_env env,const napi_value (& argv)[SET_TIMEZONE_MAX_PARA],size_t argc,std::string & timezoneId,napi_ref & callback)208 napi_value NapiUtils::ParseParametersBySetTimezone(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA],
209 size_t argc, std::string &timezoneId, napi_ref &callback)
210 {
211 NAPI_ASSERTP(env, argc >= SET_TIMEZONE_MAX_PARA - 1, "Wrong number of arguments");
212 napi_valuetype valueType = napi_undefined;
213
214 // argv[0]: timezoneid
215 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
216 NAPI_ASSERTP(env, valueType == napi_string, "Parameter error. The type of timezone must be string.");
217 char timeZoneChars[MAX_TIME_ZONE_ID];
218 size_t copied;
219 napi_get_value_string_utf8(env, argv[0], timeZoneChars, MAX_TIME_ZONE_ID - 1, &copied);
220 TIME_HILOGD(TIME_MODULE_JNI, "timezone str: %{public}s", timeZoneChars);
221
222 timezoneId = std::string(timeZoneChars);
223
224 // argv[1]:callback
225 if (argc >= SET_TIMEZONE_MAX_PARA) {
226 NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
227 NAPI_ASSERTP(env, valueType == napi_function, "Parameter error. The type of callback must be function.");
228 napi_create_reference(env, argv[1], 1, &callback);
229 }
230 return NapiGetNull(env);
231 }
232
ParseParametersGet(napi_env env,const napi_value (& argv)[SET_TIMEZONE_MAX_PARA],size_t argc,napi_ref & callback)233 napi_value NapiUtils::ParseParametersGet(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc,
234 napi_ref &callback)
235 {
236 napi_valuetype valueType = napi_undefined;
237 if (argc == 1) {
238 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
239 NAPI_ASSERTP(env, valueType == napi_function, "Parameter error. The type of callback must be function.");
240 napi_create_reference(env, argv[0], 1, &callback);
241 }
242 return NapiGetNull(env);
243 }
244
ParseParametersGetNA(napi_env env,const napi_value (& argv)[SET_TIMEZONE_MAX_PARA],size_t argc,napi_ref & callback,bool * isNano)245 napi_value NapiUtils::ParseParametersGetNA(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc,
246 napi_ref &callback, bool *isNano)
247 {
248 napi_valuetype valueType = napi_undefined;
249 if (argc == 1) {
250 NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
251 if (valueType == napi_function) {
252 napi_create_reference(env, argv[0], 1, &callback);
253 } else if (valueType == napi_boolean) {
254 napi_get_value_bool(env, argv[0], isNano);
255 }
256 } else if (argc == ARGC_TWO) {
257 napi_get_value_bool(env, argv[0], isNano);
258 napi_create_reference(env, argv[1], 1, &callback);
259 }
260 return NapiGetNull(env);
261 }
262 } // namespace Time
263 } // namespace MiscServices
264 } // namespace OHOS