1 /*
2 * Copyright (c) 2024 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
16 #include "disable_notification.h"
17 #include "ans_inner_errors.h"
18
19 namespace OHOS {
20 namespace NotificationNapi {
21 constexpr int8_t DISABLE_MAX_PARA = 2;
22
ParseDisabledParameters(const napi_env & env,const napi_value & value,bool & disabled)23 bool ParseDisabledParameters(const napi_env &env, const napi_value &value, bool &disabled)
24 {
25 ANS_LOGD("parse disabled");
26 napi_valuetype valuetype = napi_undefined;
27 NAPI_CALL_BASE(env, napi_typeof(env, value, &valuetype), false);
28 if (valuetype != napi_boolean) {
29 ANS_LOGW("wrong argument type. Bool expected");
30 std::string msg = "Incorrect parameter types.The type of disabled must be boolean.";
31 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
32 return false;
33 }
34 napi_get_value_bool(env, value, &disabled);
35 return true;
36 }
37
ParseBundleListParameters(const napi_env & env,const napi_value & value,std::vector<std::string> & bundleList)38 bool ParseBundleListParameters(const napi_env &env, const napi_value &value, std::vector<std::string> &bundleList)
39 {
40 ANS_LOGD("parse bundle list");
41 bool isArray = false;
42 napi_is_array(env, value, &isArray);
43 if (!isArray) {
44 ANS_LOGE("wrong argument type. Array expected");
45 std::string msg = "Incorrect parameter types.The type of bundle list must be array.";
46 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
47 return false;
48 }
49 uint32_t length = 0;
50 napi_get_array_length(env, value, &length);
51 if (length == 0) {
52 ANS_LOGE("the bundle list length is zero");
53 std::string msg = "Mandatory parameters are left unspecified. The bundle list length is zero.";
54 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
55 return false;
56 }
57 napi_valuetype valuetype = napi_undefined;
58 for (size_t index = 0; index < length; index++) {
59 napi_value nBundle = nullptr;
60 napi_get_element(env, value, index, &nBundle);
61 NAPI_CALL_BASE(env, napi_typeof(env, nBundle, &valuetype), false);
62 if (valuetype != napi_string) {
63 ANS_LOGE("wrong bundle name type");
64 std::string msg = "Incorrect parameter types.The type of bundle name must be string.";
65 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
66 return false;
67 }
68 char str[STR_MAX_SIZE] = {0};
69 size_t strLen = 0;
70 napi_get_value_string_utf8(env, nBundle, str, STR_MAX_SIZE - 1, &strLen);
71 if (std::strlen(str) == 0) {
72 ANS_LOGE("bundle name length is zero");
73 std::string msg = "Mandatory parameters are left unspecified.The bundle name length is zero.";
74 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
75 return false;
76 }
77 bundleList.emplace_back(str);
78 }
79 return true;
80 }
81
ParseDisableNotificationParameters(const napi_env & env,const napi_callback_info & info,NotificationDisable & param)82 bool ParseDisableNotificationParameters(
83 const napi_env &env, const napi_callback_info &info, NotificationDisable ¶m)
84 {
85 ANS_LOGD("enter");
86 size_t argc = DISABLE_MAX_PARA;
87 napi_value argv[DISABLE_MAX_PARA] = {nullptr};
88 napi_value thisVar = nullptr;
89 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false);
90 if (argc != DISABLE_MAX_PARA) {
91 ANS_LOGE("wrong number of arguments");
92 std::string msg =
93 "Wrong number of arguments.The number of parameters is not " + std::to_string(DISABLE_MAX_PARA);
94 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
95 return false;
96 }
97 // argv[0]: disabled
98 bool disabled = false;
99 if (!ParseDisabledParameters(env, argv[PARAM0], disabled)) {
100 return false;
101 }
102 param.SetDisabled(disabled);
103 std::vector<std::string> bundleList;
104 if (!ParseBundleListParameters(env, argv[PARAM1], bundleList)) {
105 return false;
106 }
107 param.SetBundleList(bundleList);
108 return true;
109 }
110 }
111 }