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