• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <optional>
16 
17 #include "ans_inner_errors.h"
18 #include "napi_remove_group.h"
19 
20 namespace OHOS {
21 namespace NotificationNapi {
22 namespace {
23     const int REMOVE_GROUP_BY_BUNDLE_MIN_PARA = 2;
24     const int REMOVE_GROUP_BY_BUNDLE_MAX_PARA = 3;
25 }
26 
ParseParameters(const napi_env & env,const napi_callback_info & info,RemoveParamsGroupByBundle & params)27 napi_value ParseParameters(
28     const napi_env &env, const napi_callback_info &info, RemoveParamsGroupByBundle &params)
29 {
30     ANS_LOGD("enter");
31 
32     size_t argc = REMOVE_GROUP_BY_BUNDLE_MAX_PARA;
33     napi_value argv[REMOVE_GROUP_BY_BUNDLE_MAX_PARA] = {nullptr};
34     napi_value thisVar = nullptr;
35     napi_valuetype valuetype = napi_undefined;
36     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
37     if (argc < REMOVE_GROUP_BY_BUNDLE_MIN_PARA) {
38         ANS_LOGW("Wrong number of arguments.");
39         return nullptr;
40     }
41 
42     // argv[0]: bundle
43     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
44     if (valuetype != napi_object) {
45         ANS_LOGW("Wrong argument type. Object expected.");
46         return nullptr;
47     }
48     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
49     if (retValue == nullptr) {
50         ANS_LOGE("GetBundleOption failed.");
51         return nullptr;
52     }
53 
54     // argv[1]: groupName: string
55     NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
56     if (valuetype != napi_string) {
57         ANS_LOGW("Wrong argument type. String expected.");
58         return nullptr;
59     }
60     char str[STR_MAX_SIZE] = {0};
61     size_t strLen = 0;
62     NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen));
63     params.groupName = str;
64     // argv[2]:callback
65     if (argc >= REMOVE_GROUP_BY_BUNDLE_MAX_PARA) {
66         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
67         if (valuetype != napi_function) {
68             ANS_LOGW("Wrong argument type. Function expected.");
69             return nullptr;
70         }
71         napi_create_reference(env, argv[PARAM2], 1, &params.callback);
72     }
73     return Common::NapiGetNull(env);
74 }
75 
AsyncCompleteCallbackNapiRemoveGroupByBundle(napi_env env,napi_status status,void * data)76 void AsyncCompleteCallbackNapiRemoveGroupByBundle(napi_env env, napi_status status, void *data)
77 {
78     ANS_LOGD("enter");
79     if (!data) {
80         ANS_LOGE("Invalid async callback data");
81         return;
82     }
83     AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo = static_cast<AsyncCallbackInfoRemoveGroupByBundle *>(data);
84     if (asynccallbackinfo) {
85         Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
86         if (asynccallbackinfo->info.callback != nullptr) {
87             napi_delete_reference(env, asynccallbackinfo->info.callback);
88         }
89         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
90         delete asynccallbackinfo;
91         asynccallbackinfo = nullptr;
92     }
93 }
94 
NapiRemoveGroupByBundle(napi_env env,napi_callback_info info)95 napi_value NapiRemoveGroupByBundle(napi_env env, napi_callback_info info)
96 {
97     ANS_LOGD("enter");
98     RemoveParamsGroupByBundle params {};
99     if (ParseParameters(env, info, params) == nullptr) {
100         Common::NapiThrow(env, ERROR_PARAM_INVALID);
101         return Common::NapiGetUndefined(env);
102     }
103 
104     AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo =
105         new (std::nothrow) AsyncCallbackInfoRemoveGroupByBundle {.env = env, .asyncWork = nullptr, .params = params};
106     if (!asynccallbackinfo) {
107         return Common::JSParaError(env, params.callback);
108     }
109     napi_value promise = nullptr;
110     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
111 
112     napi_value resourceName = nullptr;
113     napi_create_string_latin1(env, "removeGroupByBundle", NAPI_AUTO_LENGTH, &resourceName);
114     // Asynchronous function call
115     napi_create_async_work(env,
116         nullptr,
117         resourceName,
118         [](napi_env env, void *data) {
119             ANS_LOGI("RemoveGroupByBundle napi_create_async_work start");
120             AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo =
121                 static_cast<AsyncCallbackInfoRemoveGroupByBundle *>(data);
122             if (asynccallbackinfo) {
123                 ANS_LOGI("option.bundle = %{public}s, option.uid = %{public}d, groupName = %{public}s",
124                     asynccallbackinfo->params.option.GetBundleName().c_str(),
125                     asynccallbackinfo->params.option.GetUid(),
126                     asynccallbackinfo->params.groupName.c_str());
127                 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveGroupByBundle(
128                     asynccallbackinfo->params.option, asynccallbackinfo->params.groupName);
129             }
130         },
131         AsyncCompleteCallbackNapiRemoveGroupByBundle,
132         (void *)asynccallbackinfo,
133         &asynccallbackinfo->asyncWork);
134 
135     bool isCallback = asynccallbackinfo->info.isCallback;
136     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
137     if (status != napi_ok) {
138         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
139         asynccallbackinfo->info.errorCode = ERROR_INTERNAL_ERROR;
140         Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
141         if (asynccallbackinfo->info.callback != nullptr) {
142             napi_delete_reference(env, asynccallbackinfo->info.callback);
143         }
144         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
145         delete asynccallbackinfo;
146         asynccallbackinfo = nullptr;
147     }
148 
149     if (isCallback) {
150         return Common::NapiGetNull(env);
151     } else {
152         return promise;
153     }
154 }
155 }  // namespace NotificationNapi
156 }  // namespace OHOS