1 /*
2 * Copyright (c) 2022-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 #include "napi_remove.h"
16
17 #include "ans_inner_errors.h"
18 #include <optional>
19 #include "remove.h"
20
21 namespace OHOS {
22 namespace NotificationNapi {
NapiRemoveExecuteCallback(napi_env env,void * data)23 void NapiRemoveExecuteCallback(napi_env env, void *data)
24 {
25 ANS_LOGI("Remove napi_create_async_work start");
26 if (!data) {
27 ANS_LOGE("Invalid async callback data");
28 return;
29 }
30 auto removeInfo = static_cast<AsyncCallbackInfoRemove *>(data);
31 if (removeInfo) {
32 if (!removeInfo->params.hashcodes.empty()) {
33 removeInfo->info.errorCode = NotificationHelper::RemoveNotifications(removeInfo->params.hashcodes,
34 removeInfo->params.removeReason);
35 } else if (removeInfo->params.hashcode.has_value()) {
36 removeInfo->info.errorCode = NotificationHelper::RemoveNotification(removeInfo->params.hashcode.value(),
37 removeInfo->params.removeReason);
38 } else if (removeInfo->params.bundleAndKeyInfo.has_value()) {
39 auto &infos = removeInfo->params.bundleAndKeyInfo.value();
40 removeInfo->info.errorCode = NotificationHelper::RemoveNotification(infos.option,
41 infos.key.id, infos.key.label, removeInfo->params.removeReason);
42 }
43 }
44 }
45
NapiRemoveCompleteCallback(napi_env env,napi_status status,void * data)46 void NapiRemoveCompleteCallback(napi_env env, napi_status status, void *data)
47 {
48 ANS_LOGI("Remove napi_create_async_work end");
49 if (!data) {
50 ANS_LOGE("Invalid async callback data");
51 return;
52 }
53 auto removeInfo = static_cast<AsyncCallbackInfoRemove *>(data);
54 if (removeInfo) {
55 Common::CreateReturnValue(env, removeInfo->info, Common::NapiGetNull(env));
56 if (removeInfo->info.callback != nullptr) {
57 napi_delete_reference(env, removeInfo->info.callback);
58 }
59 napi_delete_async_work(env, removeInfo->asyncWork);
60 delete removeInfo;
61 removeInfo = nullptr;
62 }
63 }
64
NapiRemove(napi_env env,napi_callback_info info)65 napi_value NapiRemove(napi_env env, napi_callback_info info)
66 {
67 ANS_LOGD("enter");
68 RemoveParams params {};
69 if (!ParseParameters(env, info, params)) {
70 Common::NapiThrow(env, ERROR_PARAM_INVALID);
71 return Common::NapiGetUndefined(env);
72 }
73 auto removeInfo = new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params};
74 if (!removeInfo) {
75 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
76 return Common::JSParaError(env, params.callback);
77 }
78 napi_value promise = nullptr;
79 Common::PaddingCallbackPromiseInfo(env, params.callback, removeInfo->info, promise);
80
81 napi_value resourceName = nullptr;
82 napi_create_string_latin1(env, "remove", NAPI_AUTO_LENGTH, &resourceName);
83
84 // Asynchronous function call
85 napi_create_async_work(env, nullptr, resourceName, NapiRemoveExecuteCallback, NapiRemoveCompleteCallback,
86 (void *)removeInfo, &removeInfo->asyncWork);
87 napi_queue_async_work_with_qos(env, removeInfo->asyncWork, napi_qos_user_initiated);
88 if (removeInfo->info.isCallback) {
89 return Common::NapiGetNull(env);
90 } else {
91 return promise;
92 }
93 }
94
NapiRemoveAll(napi_env env,napi_callback_info info)95 napi_value NapiRemoveAll(napi_env env, napi_callback_info info)
96 {
97 ANS_LOGD("enter");
98 RemoveParams params {};
99 if (ParseParametersByRemoveAll(env, info, params) == nullptr) {
100 Common::NapiThrow(env, ERROR_PARAM_INVALID);
101 return Common::NapiGetUndefined(env);
102 }
103
104 AsyncCallbackInfoRemove *asynccallbackinfo =
105 new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params};
106 if (!asynccallbackinfo) {
107 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
108 return Common::JSParaError(env, params.callback);
109 }
110 napi_value promise = nullptr;
111 Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
112
113 napi_value resourceName = nullptr;
114 napi_create_string_latin1(env, "removeAll", NAPI_AUTO_LENGTH, &resourceName);
115 // Asynchronous function call
116 napi_create_async_work(env,
117 nullptr,
118 resourceName,
119 [](napi_env env, void *data) {
120 ANS_LOGI("NapiRemoveAll work excute.");
121 AsyncCallbackInfoRemove *asynccallbackinfo = static_cast<AsyncCallbackInfoRemove *>(data);
122 if (asynccallbackinfo) {
123 if (asynccallbackinfo->params.bundleAndKeyInfo.has_value()) {
124 auto &infos = asynccallbackinfo->params.bundleAndKeyInfo.value();
125 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveAllNotifications(infos.option);
126 } else if (asynccallbackinfo->params.hasUserId) {
127 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications(
128 asynccallbackinfo->params.userId);
129 } else {
130 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications();
131 }
132 }
133 },
134 [](napi_env env, napi_status status, void *data) {
135 ANS_LOGD("NapiRemoveAll work complete.");
136 AsyncCallbackInfoRemove *asynccallbackinfo = static_cast<AsyncCallbackInfoRemove *>(data);
137 if (asynccallbackinfo) {
138 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
139 if (asynccallbackinfo->info.callback != nullptr) {
140 ANS_LOGD("Delete napiRemoveAll callback reference.");
141 napi_delete_reference(env, asynccallbackinfo->info.callback);
142 }
143 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
144 delete asynccallbackinfo;
145 asynccallbackinfo = nullptr;
146 }
147 ANS_LOGD("NapiRemoveAll work complete end.");
148 },
149 (void *)asynccallbackinfo,
150 &asynccallbackinfo->asyncWork);
151
152 bool isCallback = asynccallbackinfo->info.isCallback;
153 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
154
155 if (isCallback) {
156 ANS_LOGD("napiRemoveAll callback is nullptr.");
157 return Common::NapiGetNull(env);
158 } else {
159 return promise;
160 }
161 }
162 } // namespace NotificationNapi
163 } // namespace OHOS
164