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