• 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("called");
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_LOGE("Wrong number of arguments.");
39         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
40         return nullptr;
41     }
42 
43     // argv[0]: bundle
44     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
45     if (valuetype != napi_object) {
46         ANS_LOGE("Argument type error. Object expected.");
47         std::string msg = "Incorrect parameter types.The type of param must be object.";
48         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
49         return nullptr;
50     }
51     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
52     if (retValue == nullptr) {
53         ANS_LOGE("null retValue");
54         Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED);
55         return nullptr;
56     }
57 
58     // argv[1]: groupName: string
59     NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
60     if (valuetype != napi_string && valuetype != napi_number && valuetype != napi_boolean) {
61         ANS_LOGE("Wrong argument type. String number boolean expected.");
62         std::string msg = "Incorrect parameter types.The type of param must be string or number or boolean.";
63         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
64         return nullptr;
65     }
66     if (valuetype == napi_string) {
67         char str[STR_MAX_SIZE] = {0};
68         size_t strLen = 0;
69         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen));
70         params.groupName = str;
71     } else if (valuetype == napi_number) {
72         int64_t number = 0;
73         NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM1], &number));
74         params.groupName = std::to_string(number);
75     } else {
76         bool result = false;
77         NAPI_CALL(env, napi_get_value_bool(env, argv[PARAM1], &result));
78         params.groupName = std::to_string(result);
79     }
80     // argv[2]:callback
81     if (argc >= REMOVE_GROUP_BY_BUNDLE_MAX_PARA) {
82         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
83         if (valuetype != napi_function) {
84             ANS_LOGW("Callback is not function excute promise.");
85             return Common::NapiGetNull(env);
86         }
87         napi_create_reference(env, argv[PARAM2], 1, &params.callback);
88     }
89     return Common::NapiGetNull(env);
90 }
91 
AsyncCompleteCallbackNapiRemoveGroupByBundle(napi_env env,napi_status status,void * data)92 void AsyncCompleteCallbackNapiRemoveGroupByBundle(napi_env env, napi_status status, void *data)
93 {
94     ANS_LOGD("called");
95     if (!data) {
96         ANS_LOGE("null data");
97         return;
98     }
99     AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo = static_cast<AsyncCallbackInfoRemoveGroupByBundle *>(data);
100     if (asynccallbackinfo) {
101         Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
102         if (asynccallbackinfo->info.callback != nullptr) {
103             ANS_LOGD("Delete napiRemoveGroupByBundle callback reference.");
104             napi_delete_reference(env, asynccallbackinfo->info.callback);
105         }
106         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
107         delete asynccallbackinfo;
108         asynccallbackinfo = nullptr;
109     }
110 }
111 
NapiRemoveGroupByBundle(napi_env env,napi_callback_info info)112 napi_value NapiRemoveGroupByBundle(napi_env env, napi_callback_info info)
113 {
114     ANS_LOGD("called");
115     RemoveParamsGroupByBundle params {};
116     if (ParseParameters(env, info, params) == nullptr) {
117         Common::NapiThrow(env, ERROR_PARAM_INVALID);
118         return Common::NapiGetUndefined(env);
119     }
120 
121     AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo =
122         new (std::nothrow) AsyncCallbackInfoRemoveGroupByBundle {.env = env, .asyncWork = nullptr, .params = params};
123     if (!asynccallbackinfo) {
124         Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
125         return Common::JSParaError(env, params.callback);
126     }
127     napi_value promise = nullptr;
128     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
129 
130     napi_value resourceName = nullptr;
131     napi_create_string_latin1(env, "removeGroupByBundle", NAPI_AUTO_LENGTH, &resourceName);
132     // Asynchronous function call
133     napi_create_async_work(env,
134         nullptr,
135         resourceName,
136         [](napi_env env, void *data) {
137             ANS_LOGD("NapiRemoveGroupByBundle work excute.");
138             AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo =
139                 static_cast<AsyncCallbackInfoRemoveGroupByBundle *>(data);
140             if (asynccallbackinfo) {
141                 ANS_LOGI("option.bundle:%{public}s, option.uid:%{public}d, groupName:%{public}s",
142                     asynccallbackinfo->params.option.GetBundleName().c_str(),
143                     asynccallbackinfo->params.option.GetUid(),
144                     asynccallbackinfo->params.groupName.c_str());
145                 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveGroupByBundle(
146                     asynccallbackinfo->params.option, asynccallbackinfo->params.groupName);
147             }
148         },
149         AsyncCompleteCallbackNapiRemoveGroupByBundle,
150         (void *)asynccallbackinfo,
151         &asynccallbackinfo->asyncWork);
152 
153     bool isCallback = asynccallbackinfo->info.isCallback;
154     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
155 
156     if (isCallback) {
157         ANS_LOGD("null isCallback");
158         return Common::NapiGetNull(env);
159     } else {
160         return promise;
161     }
162 }
163 }  // namespace NotificationNapi
164 }  // namespace OHOS
165