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 <optional>
17 #include "remove.h"
18
19 namespace OHOS {
20 namespace NotificationNapi {
21 const int REMOVE_MIN_PARA = 2;
22 const int REMOVE_OR_BUNDLE_MAX_PARA = 3;
23
24 const int REMOVE_ALL_MAX_PARA = 2;
25
26 const int REMOVE_BY_BUNDLE_AND_KEY_MIN_PARA = 3;
27 const int REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA = 4;
28
29 const int REMOVE_GROUP_BY_BUNDLE_MIN_PARA = 2;
30 const int REMOVE_GROUP_BY_BUNDLE_MAX_PARA = 3;
31
ParseRemoveReason(const napi_env & env,const napi_value & value,RemoveParams & params)32 bool ParseRemoveReason(const napi_env &env, const napi_value &value, RemoveParams ¶ms)
33 {
34 napi_valuetype valueType = napi_undefined;
35 NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false);
36 if (valueType != napi_number) {
37 ANS_LOGW("RemoveReason valueType unexpected.");
38 return false;
39 }
40 int32_t removeReason = 0;
41 napi_get_value_int32(env, value, &removeReason);
42 if (!Common::IsValidRemoveReason(removeReason)) {
43 ANS_LOGW("RemoveReason value unexpected.");
44 return false;
45 }
46 params.removeReason = removeReason;
47 return true;
48 }
49
ParseCallbackFunc(const napi_env & env,const napi_value & value,RemoveParams & params)50 bool ParseCallbackFunc(const napi_env &env, const napi_value &value,
51 RemoveParams ¶ms)
52 {
53 napi_valuetype valueType = napi_undefined;
54 NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false);
55 if (valueType != napi_function) {
56 ANS_LOGW("Callback is not function excute promise.");
57 return true;
58 }
59 napi_create_reference(env, value, 1, ¶ms.callback);
60 return true;
61 }
62
ParseHashcodeTypeParams(const napi_env & env,napi_value * argv,size_t argc,napi_valuetype valueType,RemoveParams & params)63 bool ParseHashcodeTypeParams(
64 const napi_env &env, napi_value* argv, size_t argc, napi_valuetype valueType, RemoveParams ¶ms)
65 {
66 // argv[0]: hashCode
67 bool isArray = false;
68 napi_is_array(env, argv[PARAM0], &isArray);
69 if (isArray) {
70 std::vector<std::string> hashcodes;
71 auto retValue = Common::GetHashCodes(env, argv[PARAM0], hashcodes);
72 if (retValue == nullptr) {
73 ANS_LOGW("GetHashCodes failed.");
74 return false;
75 }
76 params.hashcodes = hashcodes;
77 } else if (valueType == napi_string) {
78 size_t strLen = 0;
79 char str[STR_MAX_SIZE] = {0};
80 NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen), false);
81 params.hashcode = str;
82 } else if (valueType == napi_number) {
83 int64_t number = 0;
84 NAPI_CALL_BASE(env, napi_get_value_int64(env, argv[PARAM0], &number), false);
85 params.hashcode = std::to_string(number);
86 } else {
87 bool result = false;
88 NAPI_CALL_BASE(env, napi_get_value_bool(env, argv[PARAM0], &result), false);
89 params.hashcode = std::to_string(result);
90 }
91 // argv[1]:removeReason
92 if (!ParseRemoveReason(env, argv[PARAM1], params)) {
93 return false;
94 }
95 // argv[2]:callback
96 if (argc >= REMOVE_OR_BUNDLE_MAX_PARA) {
97 if (!ParseCallbackFunc(env, argv[PARAM2], params)) {
98 return false;
99 }
100 }
101 return true;
102 }
103
ParseBundleOptionTypeParams(const napi_env & env,napi_value * argv,size_t argc,RemoveParams & params)104 bool ParseBundleOptionTypeParams(const napi_env &env, napi_value* argv, size_t argc, RemoveParams ¶ms)
105 {
106 if (argc < REMOVE_BY_BUNDLE_AND_KEY_MIN_PARA) {
107 ANS_LOGW("Wrong number of arguments.");
108 return false;
109 }
110 BundleAndKeyInfo bundleInfo {};
111 // argv[0]: BundleOption argv[1]: NotificationKey
112 if (Common::GetBundleOption(env, argv[PARAM0], bundleInfo.option) == nullptr ||
113 Common::GetNotificationKey(env, argv[PARAM1], bundleInfo.key) == nullptr) {
114 ANS_LOGE("GetBundleOption failed.");
115 return false;
116 }
117 params.bundleAndKeyInfo = bundleInfo;
118 // argv[2]:removeReason
119 if (!ParseRemoveReason(env, argv[PARAM2], params)) {
120 return false;
121 }
122 // argv[3]:callback
123 if (argc >= REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA) {
124 if (!ParseCallbackFunc(env, argv[PARAM3], params)) {
125 return false;
126 }
127 }
128 return true;
129 }
130
ParseParameters(const napi_env & env,const napi_callback_info & info,RemoveParams & params)131 bool ParseParameters(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms)
132 {
133 ANS_LOGD("enter");
134 size_t argc = REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA;
135 napi_value argv[REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA] = {nullptr};
136 napi_value thisVar = nullptr;
137 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false);
138 if (argc < REMOVE_MIN_PARA) {
139 ANS_LOGW("Wrong number of arguments.");
140 return false;
141 }
142 bool isArray = false;
143 napi_is_array(env, argv[PARAM0], &isArray);
144 napi_valuetype valueType = napi_undefined;
145 NAPI_CALL_BASE(env, napi_typeof(env, argv[PARAM0], &valueType), false);
146 if ((valueType != napi_string) && (valueType != napi_object) &&
147 (valueType != napi_number) && (valueType != napi_boolean) && !isArray) {
148 ANS_LOGW("Wrong argument type. String or object expected.");
149 return false;
150 }
151 if ((valueType == napi_string) || (valueType == napi_number) || (valueType == napi_boolean) || isArray) {
152 return ParseHashcodeTypeParams(env, argv, argc, valueType, params);
153 }
154 return ParseBundleOptionTypeParams(env, argv, argc, params);
155 }
156
ParseParametersByRemoveAll(const napi_env & env,const napi_callback_info & info,RemoveParams & params)157 napi_value ParseParametersByRemoveAll(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms)
158 {
159 ANS_LOGD("enter");
160
161 size_t argc = REMOVE_ALL_MAX_PARA;
162 napi_value argv[REMOVE_ALL_MAX_PARA] = {nullptr};
163 napi_value thisVar = nullptr;
164 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
165
166 if (argc == 0) {
167 return Common::NapiGetNull(env);
168 }
169
170 // argv[0]: bundle / userId / callback
171 napi_valuetype valuetype = napi_undefined;
172 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
173 if ((valuetype != napi_object) && (valuetype != napi_number) && (valuetype != napi_function)) {
174 ANS_LOGW("Wrong argument type. Function or object expected. Excute promise.");
175 return Common::NapiGetNull(env);
176 }
177 if (valuetype == napi_object) {
178 BundleAndKeyInfo bundleandKeyInfo {};
179 auto retValue = Common::GetBundleOption(env, argv[PARAM0], bundleandKeyInfo.option);
180 if (retValue == nullptr) {
181 ANS_LOGW("GetBundleOption failed.");
182 return nullptr;
183 }
184 params.bundleAndKeyInfo = bundleandKeyInfo;
185 } else if (valuetype == napi_number) {
186 NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId));
187 params.hasUserId = true;
188 } else {
189 napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback);
190 }
191
192 // argv[1]:callback
193 if (argc >= REMOVE_ALL_MAX_PARA) {
194 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
195 if (valuetype != napi_function) {
196 ANS_LOGW("Callback is not function excute promise.");
197 return Common::NapiGetNull(env);
198 }
199 napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback);
200 }
201
202 return Common::NapiGetNull(env);
203 }
204
ParseParameters(const napi_env & env,const napi_callback_info & info,RemoveParamsGroupByBundle & params)205 napi_value ParseParameters(
206 const napi_env &env, const napi_callback_info &info, RemoveParamsGroupByBundle ¶ms)
207 {
208 ANS_LOGD("enter");
209
210 size_t argc = REMOVE_GROUP_BY_BUNDLE_MAX_PARA;
211 napi_value argv[REMOVE_GROUP_BY_BUNDLE_MAX_PARA] = {nullptr};
212 napi_value thisVar = nullptr;
213 napi_valuetype valuetype = napi_undefined;
214 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
215 if (argc < REMOVE_GROUP_BY_BUNDLE_MIN_PARA) {
216 ANS_LOGW("Error number of arguments.");
217 return nullptr;
218 }
219
220 // argv[0]: bundle
221 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
222 if (valuetype != napi_object) {
223 ANS_LOGW("Valuetype is not object.");
224 return nullptr;
225 }
226 auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
227 if (retValue == nullptr) {
228 ANS_LOGE("GetBundleOption failed.");
229 return nullptr;
230 }
231
232 // argv[1]: groupName: string
233 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
234 if (valuetype != napi_string && valuetype != napi_number && valuetype != napi_boolean) {
235 ANS_LOGW("Error argument type. String number boolean anticipate.");
236 return nullptr;
237 }
238 if (valuetype == napi_string) {
239 char str[STR_MAX_SIZE] = {0};
240 size_t strLen = 0;
241 NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen));
242 params.groupName = str;
243 } else if (valuetype == napi_number) {
244 ANS_LOGD("valuetype is number.");
245 int64_t number = 0;
246 NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM1], &number));
247 params.groupName = std::to_string(number);
248 } else {
249 ANS_LOGD("valuetype is other types.");
250 bool result = false;
251 NAPI_CALL(env, napi_get_value_bool(env, argv[PARAM1], &result));
252 params.groupName = std::to_string(result);
253 }
254 // argv[2]:callback
255 if (argc >= REMOVE_GROUP_BY_BUNDLE_MAX_PARA) {
256 NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
257 if (valuetype != napi_function) {
258 ANS_LOGW("Callback is not function.");
259 return Common::NapiGetNull(env);
260 }
261 napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback);
262 }
263 return Common::NapiGetNull(env);
264 }
265
RemoveExecuteCallback(napi_env env,void * data)266 void RemoveExecuteCallback(napi_env env, void *data)
267 {
268 ANS_LOGI("Remove napi_create_async_work start");
269 if (!data) {
270 ANS_LOGE("Invalid async callback data");
271 return;
272 }
273 auto removeInfo = static_cast<AsyncCallbackInfoRemove *>(data);
274 if (removeInfo) {
275 if (removeInfo->params.hashcode.has_value()) {
276 removeInfo->info.errorCode = NotificationHelper::RemoveNotification(removeInfo->params.hashcode.value(),
277 removeInfo->params.removeReason);
278 } else if (removeInfo->params.bundleAndKeyInfo.has_value()) {
279 auto &infos = removeInfo->params.bundleAndKeyInfo.value();
280 removeInfo->info.errorCode = NotificationHelper::RemoveNotification(infos.option,
281 infos.key.id, infos.key.label, removeInfo->params.removeReason);
282 }
283 }
284 }
285
RemoveCompleteCallback(napi_env env,napi_status status,void * data)286 void RemoveCompleteCallback(napi_env env, napi_status status, void *data)
287 {
288 ANS_LOGI("Remove napi_create_async_work end");
289 if (!data) {
290 ANS_LOGE("Invalid async callback data");
291 return;
292 }
293 auto removeInfo = static_cast<AsyncCallbackInfoRemove *>(data);
294 if (removeInfo) {
295 Common::ReturnCallbackPromise(env, removeInfo->info, Common::NapiGetNull(env));
296 if (removeInfo->info.callback != nullptr) {
297 napi_delete_reference(env, removeInfo->info.callback);
298 }
299 napi_delete_async_work(env, removeInfo->asyncWork);
300 delete removeInfo;
301 removeInfo = nullptr;
302 }
303 }
304
Remove(napi_env env,napi_callback_info info)305 napi_value Remove(napi_env env, napi_callback_info info)
306 {
307 ANS_LOGD("enter");
308 RemoveParams params {};
309 if (!ParseParameters(env, info, params)) {
310 return Common::NapiGetUndefined(env);
311 }
312 auto removeInfo = new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params};
313 if (!removeInfo) {
314 ANS_LOGD("RemoveInfo is nullptr.");
315 return Common::JSParaError(env, params.callback);
316 }
317 napi_value promise = nullptr;
318 Common::PaddingCallbackPromiseInfo(env, params.callback, removeInfo->info, promise);
319
320 napi_value resourceName = nullptr;
321 napi_create_string_latin1(env, "remove", NAPI_AUTO_LENGTH, &resourceName);
322 // Asynchronous function call
323 napi_create_async_work(env, nullptr, resourceName, RemoveExecuteCallback, RemoveCompleteCallback,
324 (void *)removeInfo, &removeInfo->asyncWork);
325 NAPI_CALL(env, napi_queue_async_work_with_qos(env, removeInfo->asyncWork, napi_qos_user_initiated));
326 if (removeInfo->info.isCallback) {
327 return Common::NapiGetNull(env);
328 } else {
329 return promise;
330 }
331 }
332
RemoveAll(napi_env env,napi_callback_info info)333 napi_value RemoveAll(napi_env env, napi_callback_info info)
334 {
335 ANS_LOGD("enter");
336
337 RemoveParams params {};
338 if (ParseParametersByRemoveAll(env, info, params) == nullptr) {
339 return Common::NapiGetUndefined(env);
340 }
341
342 AsyncCallbackInfoRemove *asynccallbackinfo =
343 new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params};
344 if (!asynccallbackinfo) {
345 ANS_LOGD("Asynccallbackinfo is nullptr.");
346 return Common::JSParaError(env, params.callback);
347 }
348 napi_value promise = nullptr;
349 Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
350
351 ANS_LOGD("Create removeAll string.");
352 napi_value resourceName = nullptr;
353 napi_create_string_latin1(env, "removeAll", NAPI_AUTO_LENGTH, &resourceName);
354 // Asynchronous function call
355 napi_create_async_work(env,
356 nullptr,
357 resourceName,
358 [](napi_env env, void *data) {
359 ANS_LOGI("RemoveAll napi_create_async_work start");
360 AsyncCallbackInfoRemove *asynccallbackinfo = static_cast<AsyncCallbackInfoRemove *>(data);
361 if (asynccallbackinfo) {
362 if (asynccallbackinfo->params.bundleAndKeyInfo.has_value()) {
363 auto &infos = asynccallbackinfo->params.bundleAndKeyInfo.value();
364 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveAllNotifications(infos.option);
365 } else if (asynccallbackinfo->params.hasUserId) {
366 ANS_LOGD("hasUserId is true.");
367 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications(
368 asynccallbackinfo->params.userId);
369 } else {
370 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications();
371 }
372 }
373 },
374 [](napi_env env, napi_status status, void *data) {
375 ANS_LOGI("RemoveAll napi_create_async_work end");
376 AsyncCallbackInfoRemove *asynccallbackinfo = static_cast<AsyncCallbackInfoRemove *>(data);
377 if (asynccallbackinfo) {
378 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
379 if (asynccallbackinfo->info.callback != nullptr) {
380 ANS_LOGD("Delete removeAll callback reference.");
381 napi_delete_reference(env, asynccallbackinfo->info.callback);
382 }
383 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
384 delete asynccallbackinfo;
385 asynccallbackinfo = nullptr;
386 }
387 ANS_LOGD("RemoveAll work complete end.");
388 },
389 (void *)asynccallbackinfo,
390 &asynccallbackinfo->asyncWork);
391
392 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
393
394 if (asynccallbackinfo->info.isCallback) {
395 ANS_LOGD("removeAll callback is nullptr.");
396 return Common::NapiGetNull(env);
397 } else {
398 return promise;
399 }
400 }
401
AsyncCompleteCallbackRemoveGroupByBundle(napi_env env,napi_status status,void * data)402 void AsyncCompleteCallbackRemoveGroupByBundle(napi_env env, napi_status status, void *data)
403 {
404 ANS_LOGD("enter");
405 if (!data) {
406 ANS_LOGE("Invalid async callback data");
407 return;
408 }
409 AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo = static_cast<AsyncCallbackInfoRemoveGroupByBundle *>(data);
410 if (asynccallbackinfo) {
411 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
412 if (asynccallbackinfo->info.callback != nullptr) {
413 napi_delete_reference(env, asynccallbackinfo->info.callback);
414 }
415 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
416 delete asynccallbackinfo;
417 asynccallbackinfo = nullptr;
418 }
419 }
420
RemoveGroupByBundle(napi_env env,napi_callback_info info)421 napi_value RemoveGroupByBundle(napi_env env, napi_callback_info info)
422 {
423 ANS_LOGD("enter");
424
425 RemoveParamsGroupByBundle params {};
426 if (ParseParameters(env, info, params) == nullptr) {
427 return Common::NapiGetUndefined(env);
428 }
429
430 AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo =
431 new (std::nothrow) AsyncCallbackInfoRemoveGroupByBundle {.env = env, .asyncWork = nullptr, .params = params};
432 if (!asynccallbackinfo) {
433 ANS_LOGD("Create asynccallbackinfo failed.");
434 return Common::JSParaError(env, params.callback);
435 }
436 napi_value promise = nullptr;
437 Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
438
439 ANS_LOGD("Create removeGroupByBundle string.");
440 napi_value resourceName = nullptr;
441 napi_create_string_latin1(env, "removeGroupByBundle", NAPI_AUTO_LENGTH, &resourceName);
442 // Asynchronous function call
443 napi_create_async_work(env,
444 nullptr,
445 resourceName,
446 [](napi_env env, void *data) {
447 ANS_LOGI("RemoveGroupByBundle napi_create_async_work start");
448 AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo =
449 static_cast<AsyncCallbackInfoRemoveGroupByBundle *>(data);
450 if (asynccallbackinfo) {
451 ANS_LOGI("option.bundle = %{public}s, option.uid = %{public}d, groupName = %{public}s",
452 asynccallbackinfo->params.option.GetBundleName().c_str(),
453 asynccallbackinfo->params.option.GetUid(),
454 asynccallbackinfo->params.groupName.c_str());
455 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveGroupByBundle(
456 asynccallbackinfo->params.option, asynccallbackinfo->params.groupName);
457 }
458 },
459 AsyncCompleteCallbackRemoveGroupByBundle,
460 (void *)asynccallbackinfo,
461 &asynccallbackinfo->asyncWork);
462
463 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
464
465 if (asynccallbackinfo->info.isCallback) {
466 ANS_LOGD("removeGroupByBundle callback is nullptr.");
467 return Common::NapiGetNull(env);
468 } else {
469 return promise;
470 }
471 }
472 } // namespace NotificationNapi
473 } // namespace OHOS