1 /*
2 * Copyright (c) 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 "notification_napi.h"
17 #include "ans_inner_errors.h"
18 #include "location_log.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 Location {
32 namespace {
33 static const std::unordered_map<int32_t, std::string> ERROR_CODE_MESSAGE {
34 {ERROR_PERMISSION_DENIED, "Permission denied"},
35 {ERROR_NOT_SYSTEM_APP, "The application isn't system application"},
36 {ERROR_PARAM_INVALID, "Invalid parameter"},
37 {ERROR_SYSTEM_CAP_ERROR, "SystemCapability not found"},
38 {ERROR_INTERNAL_ERROR, "Internal error"},
39 {ERROR_IPC_ERROR, "Marshalling or unmarshalling error"},
40 {ERROR_SERVICE_CONNECT_ERROR, "Failed to connect service"},
41 {ERROR_NOTIFICATION_CLOSED, "Notification is not enabled"},
42 {ERROR_SLOT_CLOSED, "Notification slot is not enabled"},
43 {ERROR_NOTIFICATION_UNREMOVABLE, "Notification is not allowed to remove"},
44 {ERROR_NOTIFICATION_NOT_EXIST, "The notification is not exist"},
45 {ERROR_USER_NOT_EXIST, "The user is not exist"},
46 {ERROR_OVER_MAX_NUM_PER_SECOND, "Over max number notifications per second"},
47 {ERROR_DISTRIBUTED_OPERATION_FAILED, "Distributed operation failed"},
48 {ERROR_READ_TEMPLATE_CONFIG_FAILED, "Read template config failed"},
49 {ERROR_NO_MEMORY, "No memory space"},
50 {ERROR_BUNDLE_NOT_FOUND, "The specified bundle name was not found"},
51 {ERROR_NO_AGENT_SETTING, "There is no corresponding agent relationship configuration"},
52 };
53 }
54
NapiGetBoolean(napi_env env,const bool & isValue)55 napi_value NotificationNapi::NapiGetBoolean(napi_env env, const bool &isValue)
56 {
57 napi_value result = nullptr;
58 napi_get_boolean(env, isValue, &result);
59 return result;
60 }
61
NapiGetNull(napi_env env)62 napi_value NotificationNapi::NapiGetNull(napi_env env)
63 {
64 napi_value result = nullptr;
65 napi_get_null(env, &result);
66 return result;
67 }
68
NapiGetUndefined(napi_env env)69 napi_value NotificationNapi::NapiGetUndefined(napi_env env)
70 {
71 napi_value result = nullptr;
72 napi_get_undefined(env, &result);
73 return result;
74 }
75
CreateErrorValue(napi_env env,int32_t errCode,bool newType)76 napi_value NotificationNapi::CreateErrorValue(napi_env env, int32_t errCode, bool newType)
77 {
78 LBSLOGI(NAPI_UTILS, "enter, errorCode[%{public}d]", errCode);
79 napi_value error = NotificationNapi::NapiGetNull(env);
80 if (errCode == ERR_OK && newType) {
81 return error;
82 }
83
84 napi_value code = nullptr;
85 napi_create_int32(env, errCode, &code);
86
87 auto iter = ERROR_CODE_MESSAGE.find(errCode);
88 std::string errMsg = iter != ERROR_CODE_MESSAGE.end() ? iter->second : "";
89 napi_value message = nullptr;
90 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
91
92 napi_create_error(env, nullptr, message, &error);
93 napi_set_named_property(env, error, "code", code);
94 return error;
95 }
96
NapiThrow(napi_env env,int32_t errCode)97 void NotificationNapi::NapiThrow(napi_env env, int32_t errCode)
98 {
99 LBSLOGD(NAPI_UTILS, "enter");
100
101 napi_throw(env, CreateErrorValue(env, errCode, true));
102 }
103
GetCallbackErrorValue(napi_env env,int32_t errCode)104 napi_value NotificationNapi::GetCallbackErrorValue(napi_env env, int32_t errCode)
105 {
106 napi_value result = nullptr;
107 napi_value eCode = nullptr;
108 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
109 NAPI_CALL(env, napi_create_object(env, &result));
110 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
111 return result;
112 }
113
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)114 void NotificationNapi::PaddingCallbackPromiseInfo(
115 const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
116 {
117 LBSLOGD(NAPI_UTILS, "enter");
118
119 if (callback) {
120 LBSLOGD(NAPI_UTILS, "Callback is not nullptr.");
121 info.callback = callback;
122 info.isCallback = true;
123 } else {
124 napi_deferred deferred = nullptr;
125 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
126 info.deferred = deferred;
127 info.isCallback = false;
128 }
129 }
130
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)131 void NotificationNapi::ReturnCallbackPromise(
132 const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
133 {
134 LBSLOGD(NAPI_UTILS, "enter errorCode=%{public}d", info.errorCode);
135 if (info.isCallback) {
136 SetCallback(env, info.callback, info.errorCode, result, false);
137 } else {
138 SetPromise(env, info.deferred, info.errorCode, result, false);
139 }
140 LBSLOGD(NAPI_UTILS, "end");
141 }
142
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int32_t & errorCode,const napi_value & result,bool newType)143 void NotificationNapi::SetCallback(
144 const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result, bool newType)
145 {
146 LBSLOGD(NAPI_UTILS, "enter");
147 napi_value undefined = nullptr;
148 napi_get_undefined(env, &undefined);
149
150 napi_value callback = nullptr;
151 napi_value resultout = nullptr;
152 napi_get_reference_value(env, callbackIn, &callback);
153 napi_value results[ARGS_TWO] = {nullptr};
154 results[PARAM0] = CreateErrorValue(env, errorCode, newType);
155 results[PARAM1] = result;
156 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM0], &resultout));
157 LBSLOGD(NAPI_UTILS, "end");
158 }
159
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value & result)160 void NotificationNapi::SetCallback(
161 const napi_env &env, const napi_ref &callbackIn, const napi_value &result)
162 {
163 LBSLOGD(NAPI_UTILS, "enter");
164 napi_value undefined = nullptr;
165 napi_get_undefined(env, &undefined);
166
167 napi_value callback = nullptr;
168 napi_value resultout = nullptr;
169 napi_get_reference_value(env, callbackIn, &callback);
170 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_ONE, &result, &resultout));
171 LBSLOGD(NAPI_UTILS, "end");
172 }
173
SetCallbackArg2(const napi_env & env,const napi_ref & callbackIn,const napi_value & result0,const napi_value & result1)174 void NotificationNapi::SetCallbackArg2(
175 const napi_env &env, const napi_ref &callbackIn, const napi_value &result0, const napi_value &result1)
176 {
177 LBSLOGD(NAPI_UTILS, "enter");
178 napi_value result[ARGS_TWO] = {result0, result1};
179 napi_value undefined = nullptr;
180 napi_get_undefined(env, &undefined);
181
182 napi_value callback = nullptr;
183 napi_value resultout = nullptr;
184 napi_get_reference_value(env, callbackIn, &callback);
185 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, result, &resultout));
186 LBSLOGD(NAPI_UTILS, "end");
187 }
188
SetPromise(const napi_env & env,const napi_deferred & deferred,const int32_t & errorCode,const napi_value & result,bool newType)189 void NotificationNapi::SetPromise(const napi_env &env,
190 const napi_deferred &deferred, const int32_t &errorCode, const napi_value &result, bool newType)
191 {
192 LBSLOGD(NAPI_UTILS, "enter");
193 if (errorCode == ERR_OK) {
194 napi_resolve_deferred(env, deferred, result);
195 } else {
196 napi_reject_deferred(env, deferred, CreateErrorValue(env, errorCode, newType));
197 }
198 LBSLOGD(NAPI_UTILS, "end");
199 }
200
JSParaError(const napi_env & env,const napi_ref & callback)201 napi_value NotificationNapi::JSParaError(const napi_env &env, const napi_ref &callback)
202 {
203 if (callback) {
204 return NotificationNapi::NapiGetNull(env);
205 }
206 napi_value promise = nullptr;
207 napi_deferred deferred = nullptr;
208 napi_create_promise(env, &deferred, &promise);
209 SetPromise(env, deferred, ERROR, NotificationNapi::NapiGetNull(env), false);
210 return promise;
211 }
212 } // namespace Location
213 } // namespace OHOS
214