• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "common.h"
17 #include "ans_inner_errors.h"
18 #include "ans_log_wrapper.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi_common.h"
22 #include "napi_common_util.h"
23 #include "notification_action_button.h"
24 #include "notification_capsule.h"
25 #include "notification_constant.h"
26 #include "notification_progress.h"
27 #include "notification_time.h"
28 #include "pixel_map_napi.h"
29 
30 namespace OHOS {
31 namespace NotificationNapi {
32 const uint32_t MAX_PARAM_NUM = 5;
33 
NapiGetBoolean(napi_env env,const bool & isValue)34 napi_value Common::NapiGetBoolean(napi_env env, const bool &isValue)
35 {
36     napi_value result = nullptr;
37     napi_get_boolean(env, isValue, &result);
38     return result;
39 }
40 
NapiGetNull(napi_env env)41 napi_value Common::NapiGetNull(napi_env env)
42 {
43     napi_value result = nullptr;
44     napi_get_null(env, &result);
45     return result;
46 }
47 
NapiGetUndefined(napi_env env)48 napi_value Common::NapiGetUndefined(napi_env env)
49 {
50     napi_value result = nullptr;
51     napi_get_undefined(env, &result);
52     return result;
53 }
54 
CreateErrorValue(napi_env env,int32_t errCode,bool newType)55 napi_value Common::CreateErrorValue(napi_env env, int32_t errCode, bool newType)
56 {
57     ANS_LOGD("called, errorCode[%{public}d]", errCode);
58     napi_value error = Common::NapiGetNull(env);
59     if (errCode == ERR_OK && newType) {
60         return error;
61     }
62 
63     napi_value code = nullptr;
64     napi_create_int32(env, errCode, &code);
65 
66     std::string errMsg = OHOS::Notification::GetAnsErrMessage(errCode);
67     napi_value message = nullptr;
68     napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
69 
70     napi_create_error(env, nullptr, message, &error);
71     napi_set_named_property(env, error, "code", code);
72     return error;
73 }
74 
CreateErrorValue(napi_env env,int32_t errCode,std::string & msg)75 napi_value Common::CreateErrorValue(napi_env env, int32_t errCode, std::string &msg)
76 {
77     ANS_LOGD("called, errorCode[%{public}d]", errCode);
78     napi_value error = Common::NapiGetNull(env);
79     if (errCode == ERR_OK) {
80         return error;
81     }
82 
83     napi_value code = nullptr;
84     napi_create_int32(env, errCode, &code);
85 
86     std::string errMsg = OHOS::Notification::GetAnsErrMessage(errCode);
87     napi_value message = nullptr;
88     napi_create_string_utf8(env, errMsg.append(" ").append(msg).c_str(), NAPI_AUTO_LENGTH, &message);
89 
90     napi_create_error(env, nullptr, message, &error);
91     napi_set_named_property(env, error, "code", code);
92     return error;
93 }
94 
NapiThrow(napi_env env,int32_t errCode)95 void Common::NapiThrow(napi_env env, int32_t errCode)
96 {
97     ANS_LOGD("called");
98 
99     napi_throw(env, CreateErrorValue(env, errCode, true));
100 }
101 
NapiThrow(napi_env env,int32_t errCode,std::string & msg)102 void Common::NapiThrow(napi_env env, int32_t errCode, std::string &msg)
103 {
104     ANS_LOGD("called");
105 
106     napi_throw(env, CreateErrorValue(env, errCode, msg));
107 }
108 
GetCallbackErrorValue(napi_env env,int32_t errCode)109 napi_value Common::GetCallbackErrorValue(napi_env env, int32_t errCode)
110 {
111     napi_value result = nullptr;
112     napi_value eCode = nullptr;
113     NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
114     NAPI_CALL(env, napi_create_object(env, &result));
115     NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
116     return result;
117 }
118 
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)119 void Common::PaddingCallbackPromiseInfo(
120     const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
121 {
122     ANS_LOGD("called");
123 
124     if (callback) {
125         ANS_LOGD("null callback");
126         info.callback = callback;
127         info.isCallback = true;
128     } else {
129         napi_deferred deferred = nullptr;
130         NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
131         info.deferred = deferred;
132         info.isCallback = false;
133     }
134 }
135 
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)136 void Common::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
137 {
138     ANS_LOGD("start, errorCode=%{public}d", info.errorCode);
139     if (info.isCallback) {
140         SetCallback(env, info.callback, info.errorCode, result, false);
141     } else {
142         SetPromise(env, info.deferred, info.errorCode, result, false);
143     }
144     ANS_LOGD("end");
145 }
146 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int32_t & errorCode,const napi_value & result,bool newType)147 void Common::SetCallback(
148     const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result, bool newType)
149 {
150     ANS_LOGD("start");
151     napi_value undefined = nullptr;
152     napi_get_undefined(env, &undefined);
153 
154     napi_value callback = nullptr;
155     napi_value resultout = nullptr;
156     napi_get_reference_value(env, callbackIn, &callback);
157     napi_value results[ARGS_TWO] = {nullptr};
158     results[PARAM0] = CreateErrorValue(env, errorCode, newType);
159     results[PARAM1] = result;
160     napi_status napi_result = napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM0], &resultout);
161     if (napi_result != napi_ok) {
162         ANS_LOGE("napi_call_function failed, result = %{public}d", napi_result);
163     }
164     NAPI_CALL_RETURN_VOID(env, napi_result);
165     ANS_LOGD("end");
166 }
167 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value & result)168 void Common::SetCallback(
169     const napi_env &env, const napi_ref &callbackIn, const napi_value &result)
170 {
171     ANS_LOGD("start");
172     napi_value undefined = nullptr;
173     napi_get_undefined(env, &undefined);
174 
175     napi_value callback = nullptr;
176     napi_value resultout = nullptr;
177     napi_get_reference_value(env, callbackIn, &callback);
178     napi_status napi_result = napi_call_function(env, undefined, callback, ARGS_ONE, &result, &resultout);
179     if (napi_result != napi_ok) {
180         ANS_LOGE("napi_call_function failed, result = %{public}d", napi_result);
181     }
182     NAPI_CALL_RETURN_VOID(env, napi_result);
183     ANS_LOGD("end");
184 }
185 
SetCallbackArg2(const napi_env & env,const napi_ref & callbackIn,const napi_value & result0,const napi_value & result1)186 void Common::SetCallbackArg2(
187     const napi_env &env, const napi_ref &callbackIn, const napi_value &result0, const napi_value &result1)
188 {
189     ANS_LOGD("start");
190     napi_value result[ARGS_TWO] = {result0, result1};
191     napi_value undefined = nullptr;
192     napi_get_undefined(env, &undefined);
193 
194     napi_value callback = nullptr;
195     napi_value resultout = nullptr;
196     napi_get_reference_value(env, callbackIn, &callback);
197     napi_status napi_result = napi_call_function(env, undefined, callback, ARGS_TWO, result, &resultout);
198     if (napi_result != napi_ok) {
199         ANS_LOGE("napi_call_function failed, result = %{public}d", napi_result);
200     }
201     NAPI_CALL_RETURN_VOID(env, napi_result);
202     ANS_LOGD("end");
203 }
204 
SetPromise(const napi_env & env,const napi_deferred & deferred,const int32_t & errorCode,const napi_value & result,bool newType)205 void Common::SetPromise(const napi_env &env,
206     const napi_deferred &deferred, const int32_t &errorCode, const napi_value &result, bool newType)
207 {
208     ANS_LOGD("start");
209     if (errorCode == ERR_OK) {
210         napi_resolve_deferred(env, deferred, result);
211     } else {
212         napi_reject_deferred(env, deferred, CreateErrorValue(env, errorCode, newType));
213     }
214     ANS_LOGD("end");
215 }
216 
JSParaError(const napi_env & env,const napi_ref & callback)217 napi_value Common::JSParaError(const napi_env &env, const napi_ref &callback)
218 {
219     if (callback) {
220         return Common::NapiGetNull(env);
221     }
222     napi_value promise = nullptr;
223     napi_deferred deferred = nullptr;
224     napi_create_promise(env, &deferred, &promise);
225     SetPromise(env, deferred, ERROR, Common::NapiGetNull(env), false);
226     return promise;
227 }
228 
ParseParaOnlyCallback(const napi_env & env,const napi_callback_info & info,napi_ref & callback)229 napi_value Common::ParseParaOnlyCallback(const napi_env &env, const napi_callback_info &info, napi_ref &callback)
230 {
231     ANS_LOGD("called");
232 
233     size_t argc = ONLY_CALLBACK_MAX_PARA;
234     napi_value argv[ONLY_CALLBACK_MAX_PARA] = {nullptr};
235     napi_value thisVar = nullptr;
236     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
237     if (argc < ONLY_CALLBACK_MIN_PARA) {
238         ANS_LOGE("Wrong number of arguments");
239         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
240         return nullptr;
241     }
242 
243     // argv[0]:callback
244     napi_valuetype valuetype = napi_undefined;
245     if (argc >= ONLY_CALLBACK_MAX_PARA) {
246         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
247         if (valuetype != napi_function) {
248             ANS_LOGE("Callback is not function excute promise.");
249             return Common::NapiGetNull(env);
250         }
251         napi_create_reference(env, argv[PARAM0], 1, &callback);
252     }
253 
254     return Common::NapiGetNull(env);
255 }
256 
CreateReturnValue(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)257 void Common::CreateReturnValue(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
258 {
259     ANS_LOGD("start, errorCode=%{public}d", info.errorCode);
260     int32_t errorCode = info.errorCode == ERR_OK ? ERR_OK : ErrorToExternal(info.errorCode);
261     if (info.isCallback) {
262         SetCallback(env, info.callback, errorCode, result, true);
263     } else {
264         SetPromise(env, info.deferred, errorCode, result, true);
265     }
266     ANS_LOGD("end");
267 }
268 
NapiReturnCapErrCb(napi_env env,napi_callback_info info)269 napi_value Common::NapiReturnCapErrCb(napi_env env, napi_callback_info info)
270 {
271     size_t argc = MAX_PARAM_NUM;
272     napi_value argv[MAX_PARAM_NUM] = {nullptr};
273     napi_value thisVar = nullptr;
274     napi_ref callback = nullptr;
275     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
276     for (size_t i = 0; i < argc && i < MAX_PARAM_NUM; ++i) {
277         napi_valuetype valuetype = napi_undefined;
278         NAPI_CALL(env, napi_typeof(env, argv[i], &valuetype));
279         if (valuetype == napi_function) {
280             napi_create_reference(env, argv[i], 1, &callback);
281             SetCallback(env, callback, ERROR_SYSTEM_CAP_ERROR, nullptr, false);
282             napi_delete_reference(env, callback);
283             return NapiGetNull(env);
284         }
285     }
286 
287     return NapiReturnCapErr(env, info);
288 }
289 
NapiReturnCapErr(napi_env env,napi_callback_info info)290 napi_value Common::NapiReturnCapErr(napi_env env, napi_callback_info info)
291 {
292     napi_value promise = nullptr;
293     napi_deferred deferred = nullptr;
294     napi_create_promise(env, &deferred, &promise);
295     SetPromise(env, deferred, ERROR_SYSTEM_CAP_ERROR, Common::NapiGetNull(env), false);
296     return promise;
297 }
298 
NapiReturnFalseCb(napi_env env,napi_callback_info info)299 napi_value Common::NapiReturnFalseCb(napi_env env, napi_callback_info info)
300 {
301     return Common::NapiReturnFalseCbInner(env, info, false);
302 }
303 
NapiReturnFalseCbNewType(napi_env env,napi_callback_info info)304 napi_value Common::NapiReturnFalseCbNewType(napi_env env, napi_callback_info info)
305 {
306     return Common::NapiReturnFalseCbInner(env, info, true);
307 }
308 
NapiReturnFalseCbInner(napi_env env,napi_callback_info info,bool newType)309 napi_value Common::NapiReturnFalseCbInner(napi_env env, napi_callback_info info, bool newType)
310 {
311     size_t argc = ONLY_CALLBACK_MAX_PARA;
312     napi_value argv[ONLY_CALLBACK_MAX_PARA] = {nullptr};
313     napi_value thisVar = nullptr;
314     napi_ref callback = nullptr;
315     napi_value result = nullptr;
316     napi_get_boolean(env, false, &result);
317     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
318     if (argc >= ONLY_CALLBACK_MIN_PARA) {
319         napi_valuetype valuetype = napi_undefined;
320         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
321         if (valuetype == napi_function) {
322             napi_create_reference(env, argv[PARAM0], 1, &callback);
323             SetCallback(env, callback, 0, result, true);
324             napi_delete_reference(env, callback);
325             return NapiGetNull(env);
326         }
327     }
328     napi_value promise = nullptr;
329     napi_deferred deferred = nullptr;
330     napi_create_promise(env, &deferred, &promise);
331     SetPromise(env, deferred, 0, result, false);
332     return promise;
333 }
334 }  // namespace NotificationNapi
335 }  // namespace OHOS
336