1 /*
2 * Copyright (c) 2021-2023 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 "get_active.h"
17
18 namespace OHOS {
19 namespace NotificationNapi {
AsyncCompleteCallbackGetAllActiveNotifications(napi_env env,napi_status status,void * data)20 void AsyncCompleteCallbackGetAllActiveNotifications(napi_env env, napi_status status, void *data)
21 {
22 ANS_LOGI("GetAllActiveNotifications napi_create_async_work end");
23
24 if (!data) {
25 ANS_LOGE("Invalid async callback data");
26 return;
27 }
28
29 auto asynccallbackinfo = static_cast<AsyncCallbackInfoActive *>(data);
30 if (asynccallbackinfo) {
31 napi_value result = nullptr;
32 if (asynccallbackinfo->info.errorCode != ERR_OK) {
33 result = Common::NapiGetNull(env);
34 } else {
35 napi_value arr = nullptr;
36 int32_t count = 0;
37 napi_create_array(env, &arr);
38 for (auto vec : asynccallbackinfo->notifications) {
39 if (!vec) {
40 ANS_LOGW("Invalid Notification object ptr");
41 continue;
42 }
43 napi_value notificationResult = nullptr;
44 napi_create_object(env, ¬ificationResult);
45 if (!Common::SetNotification(env, vec.GetRefPtr(), notificationResult)) {
46 ANS_LOGW("Set Notification object failed");
47 continue;
48 }
49 napi_set_element(env, arr, count, notificationResult);
50 count++;
51 }
52 ANS_LOGI("GetAllActiveNotifications count = %{public}d", count);
53 result = arr;
54 if ((count == 0) && (asynccallbackinfo->notifications.size() > 0)) {
55 asynccallbackinfo->info.errorCode = ERROR;
56 result = Common::NapiGetNull(env);
57 }
58 }
59 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
60 if (asynccallbackinfo->info.callback != nullptr) {
61 napi_delete_reference(env, asynccallbackinfo->info.callback);
62 }
63 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
64 delete asynccallbackinfo;
65 asynccallbackinfo = nullptr;
66 }
67 }
68
GetAllActiveNotifications(napi_env env,napi_callback_info info)69 napi_value GetAllActiveNotifications(napi_env env, napi_callback_info info)
70 {
71 ANS_LOGI("enter");
72
73 napi_ref callback = nullptr;
74 if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
75 return Common::NapiGetUndefined(env);
76 }
77
78 auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoActive {.env = env, .asyncWork = nullptr};
79 if (!asynccallbackinfo) {
80 return Common::JSParaError(env, callback);
81 }
82 napi_value promise = nullptr;
83 Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
84
85 napi_value resourceName = nullptr;
86 napi_create_string_latin1(env, "getAllActiveNotifications", NAPI_AUTO_LENGTH, &resourceName);
87 // Asynchronous function call
88 napi_create_async_work(
89 env,
90 nullptr,
91 resourceName,
92 [](napi_env env, void *data) {
93 ANS_LOGI("GetAllActiveNotifications napi_create_async_work start");
94 auto asynccallbackinfo = static_cast<AsyncCallbackInfoActive *>(data);
95 if (asynccallbackinfo) {
96 asynccallbackinfo->info.errorCode =
97 NotificationHelper::GetAllActiveNotifications(asynccallbackinfo->notifications);
98 }
99 },
100 AsyncCompleteCallbackGetAllActiveNotifications,
101 (void *)asynccallbackinfo,
102 &asynccallbackinfo->asyncWork);
103
104 napi_status status = napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
105 if (status != napi_ok) {
106 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
107 if (asynccallbackinfo->info.callback != nullptr) {
108 napi_delete_reference(env, asynccallbackinfo->info.callback);
109 }
110 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
111 delete asynccallbackinfo;
112 asynccallbackinfo = nullptr;
113 return Common::JSParaError(env, callback);
114 }
115
116 if (asynccallbackinfo->info.isCallback) {
117 return Common::NapiGetNull(env);
118 } else {
119 return promise;
120 }
121 }
122
AsyncCompleteCallbackGetActiveNotifications(napi_env env,napi_status status,void * data)123 void AsyncCompleteCallbackGetActiveNotifications(napi_env env, napi_status status, void *data)
124 {
125 ANS_LOGI("GetActiveNotifications napi_create_async_work end");
126
127 if (!data) {
128 ANS_LOGE("Invalid async callback data");
129 return;
130 }
131
132 auto asynccallbackinfo = static_cast<AsyncCallbackInfoActive *>(data);
133 if (asynccallbackinfo) {
134 napi_value result = nullptr;
135 if (asynccallbackinfo->info.errorCode != ERR_OK) {
136 result = Common::NapiGetNull(env);
137 } else {
138 napi_value arr = nullptr;
139 int32_t count = 0;
140 napi_create_array(env, &arr);
141 for (auto vec : asynccallbackinfo->requests) {
142 if (!vec) {
143 ANS_LOGW("Invalid NotificationRequest object ptr");
144 continue;
145 }
146 napi_value requestResult = nullptr;
147 napi_create_object(env, &requestResult);
148 if (!Common::SetNotificationRequest(env, vec.GetRefPtr(), requestResult)) {
149 ANS_LOGW("Set NotificationRequest object failed");
150 continue;
151 }
152 napi_set_element(env, arr, count, requestResult);
153 count++;
154 }
155 ANS_LOGI("GetActiveNotifications count = %{public}d", count);
156 result = arr;
157 if ((count == 0) && (asynccallbackinfo->requests.size() > 0)) {
158 asynccallbackinfo->info.errorCode = ERROR;
159 result = Common::NapiGetNull(env);
160 }
161 }
162 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
163 if (asynccallbackinfo->info.callback != nullptr) {
164 napi_delete_reference(env, asynccallbackinfo->info.callback);
165 }
166 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
167 delete asynccallbackinfo;
168 asynccallbackinfo = nullptr;
169 }
170 }
171
GetActiveNotifications(napi_env env,napi_callback_info info)172 napi_value GetActiveNotifications(napi_env env, napi_callback_info info)
173 {
174 ANS_LOGI("enter");
175
176 napi_ref callback = nullptr;
177 if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
178 return Common::NapiGetUndefined(env);
179 }
180
181 auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoActive {.env = env, .asyncWork = nullptr};
182 if (!asynccallbackinfo) {
183 return Common::JSParaError(env, callback);
184 }
185 napi_value promise = nullptr;
186 Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
187
188 napi_value resourceName = nullptr;
189 napi_create_string_latin1(env, "getActiveNotifications", NAPI_AUTO_LENGTH, &resourceName);
190 // Asynchronous function call
191 napi_create_async_work(
192 env,
193 nullptr,
194 resourceName,
195 [](napi_env env, void *data) {
196 ANS_LOGI("GetActiveNotifications napi_create_async_work start");
197 auto asynccallbackinfo = static_cast<AsyncCallbackInfoActive *>(data);
198 if (asynccallbackinfo) {
199 asynccallbackinfo->info.errorCode =
200 NotificationHelper::GetActiveNotifications(asynccallbackinfo->requests);
201 }
202 },
203 AsyncCompleteCallbackGetActiveNotifications,
204 (void *)asynccallbackinfo,
205 &asynccallbackinfo->asyncWork);
206
207 napi_status status = napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
208 if (status != napi_ok) {
209 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
210 if (asynccallbackinfo->info.callback != nullptr) {
211 napi_delete_reference(env, asynccallbackinfo->info.callback);
212 }
213 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
214 delete asynccallbackinfo;
215 asynccallbackinfo = nullptr;
216 return Common::JSParaError(env, callback);
217 }
218
219 if (asynccallbackinfo->info.isCallback) {
220 return Common::NapiGetNull(env);
221 } else {
222 return promise;
223 }
224 }
225
AsyncCompleteCallbackGetActiveNotificationCount(napi_env env,napi_status status,void * data)226 void AsyncCompleteCallbackGetActiveNotificationCount(napi_env env, napi_status status, void *data)
227 {
228 ANS_LOGI("GetActiveNotificationCount napi_create_async_work end");
229
230 if (!data) {
231 ANS_LOGE("Invalid async callback data");
232 return;
233 }
234
235 auto asynccallbackinfo = static_cast<AsyncCallbackInfoActive *>(data);
236 if (asynccallbackinfo) {
237 napi_value result = nullptr;
238 if (asynccallbackinfo->info.errorCode != ERR_OK) {
239 result = Common::NapiGetNull(env);
240 } else {
241 napi_create_uint32(env, asynccallbackinfo->num, &result);
242 }
243 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
244 if (asynccallbackinfo->info.callback != nullptr) {
245 napi_delete_reference(env, asynccallbackinfo->info.callback);
246 }
247 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
248 delete asynccallbackinfo;
249 asynccallbackinfo = nullptr;
250 }
251 }
252
GetActiveNotificationCount(napi_env env,napi_callback_info info)253 napi_value GetActiveNotificationCount(napi_env env, napi_callback_info info)
254 {
255 ANS_LOGI("enter");
256
257 napi_ref callback = nullptr;
258 if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
259 return Common::NapiGetUndefined(env);
260 }
261
262 auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoActive {.env = env, .asyncWork = nullptr};
263 if (!asynccallbackinfo) {
264 return Common::JSParaError(env, callback);
265 }
266 napi_value promise = nullptr;
267 Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
268
269 napi_value resourceName = nullptr;
270 napi_create_string_latin1(env, "getActiveNotificationCount", NAPI_AUTO_LENGTH, &resourceName);
271 // Asynchronous function call
272 napi_create_async_work(
273 env,
274 nullptr,
275 resourceName,
276 [](napi_env env, void *data) {
277 ANS_LOGI("GetActiveNotificationCount napi_create_async_work start");
278 auto asynccallbackinfo = static_cast<AsyncCallbackInfoActive *>(data);
279 if (asynccallbackinfo) {
280 asynccallbackinfo->info.errorCode =
281 NotificationHelper::GetActiveNotificationNums(asynccallbackinfo->num);
282 ANS_LOGI("GetActiveNotificationCount count = %{public}" PRIu64 "", asynccallbackinfo->num);
283 }
284 },
285 AsyncCompleteCallbackGetActiveNotificationCount,
286 (void *)asynccallbackinfo,
287 &asynccallbackinfo->asyncWork);
288
289 napi_status status = napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
290 if (status != napi_ok) {
291 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
292 if (asynccallbackinfo->info.callback != nullptr) {
293 napi_delete_reference(env, asynccallbackinfo->info.callback);
294 }
295 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
296 delete asynccallbackinfo;
297 asynccallbackinfo = nullptr;
298 return Common::JSParaError(env, callback);
299 }
300
301 if (asynccallbackinfo->info.isCallback) {
302 return Common::NapiGetNull(env);
303 } else {
304 return promise;
305 }
306 }
307 } // namespace NotificationNapi
308 } // namespace OHOS