1 /*
2 * Copyright (c) 2024-2025 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 = 3;
22 constexpr int8_t DISABLE_MIN_PARA = 2;
23 constexpr int32_t MAX_USER_ID = 10736;
24
ParseDisabledParameters(const napi_env & env,const napi_value & value,bool & disabled)25 bool ParseDisabledParameters(const napi_env &env, const napi_value &value, bool &disabled)
26 {
27 ANS_LOGD("called");
28 napi_valuetype valuetype = napi_undefined;
29 NAPI_CALL_BASE(env, napi_typeof(env, value, &valuetype), false);
30 if (valuetype != napi_boolean) {
31 ANS_LOGE("wrong argument type. Bool expected");
32 std::string msg = "Incorrect parameter types.The type of disabled must be boolean.";
33 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
34 return false;
35 }
36 napi_get_value_bool(env, value, &disabled);
37 return true;
38 }
39
ParseBundleListParameters(const napi_env & env,const napi_value & value,std::vector<std::string> & bundleList)40 bool ParseBundleListParameters(const napi_env &env, const napi_value &value, std::vector<std::string> &bundleList)
41 {
42 ANS_LOGD("called");
43 bool isArray = false;
44 napi_is_array(env, value, &isArray);
45 if (!isArray) {
46 ANS_LOGE("wrong argument type. Array expected");
47 std::string msg = "Incorrect parameter types.The type of bundle list must be array.";
48 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
49 return false;
50 }
51 uint32_t length = 0;
52 napi_get_array_length(env, value, &length);
53 if (length == 0) {
54 ANS_LOGE("the bundle list length is zero");
55 std::string msg = "Mandatory parameters are left unspecified. The bundle list length is zero.";
56 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
57 return false;
58 }
59 napi_valuetype valuetype = napi_undefined;
60 for (size_t index = 0; index < length; index++) {
61 napi_value nBundle = nullptr;
62 napi_get_element(env, value, index, &nBundle);
63 NAPI_CALL_BASE(env, napi_typeof(env, nBundle, &valuetype), false);
64 if (valuetype != napi_string) {
65 ANS_LOGE("wrong bundle name type");
66 std::string msg = "Incorrect parameter types.The type of bundle name must be string.";
67 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
68 return false;
69 }
70 char str[STR_MAX_SIZE] = {0};
71 size_t strLen = 0;
72 napi_get_value_string_utf8(env, nBundle, str, STR_MAX_SIZE - 1, &strLen);
73 if (std::strlen(str) == 0) {
74 ANS_LOGE("bundle name length is zero");
75 std::string msg = "Mandatory parameters are left unspecified.The bundle name length is zero.";
76 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
77 return false;
78 }
79 bundleList.emplace_back(str);
80 }
81 return true;
82 }
83
ParseUserIdParameters(const napi_env & env,const napi_value & value,int32_t & userId)84 bool ParseUserIdParameters(const napi_env &env, const napi_value &value, int32_t &userId)
85 {
86 napi_status status = napi_get_value_int32(env, value, &userId);
87 if (status != napi_ok) {
88 ANS_LOGE("Failed to parse the third parameter as number");
89 std::string msg = "Third argument must be a number";
90 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
91 return false;
92 }
93 if (userId < 0 || userId > MAX_USER_ID) {
94 ANS_LOGE("Invalid userId");
95 std::string msg = "UserId must be a non-negative integer and less than " + std::to_string(MAX_USER_ID);
96 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
97 return false;
98 }
99 return true;
100 }
101
ParseDisableNotificationParameters(const napi_env & env,const napi_callback_info & info,NotificationDisable & param)102 bool ParseDisableNotificationParameters(
103 const napi_env &env, const napi_callback_info &info, NotificationDisable ¶m)
104 {
105 ANS_LOGD("called");
106 size_t argc = DISABLE_MAX_PARA;
107 napi_value argv[DISABLE_MAX_PARA] = {nullptr};
108 napi_value thisVar = nullptr;
109 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false);
110 if (argc > DISABLE_MAX_PARA || argc < DISABLE_MIN_PARA) {
111 ANS_LOGE("wrong number of arguments");
112 std::string msg =
113 "Wrong number of arguments. Expected 2 or 3, but get " + std::to_string(argc);
114 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
115 return false;
116 }
117 // argv[0]: disabled
118 bool disabled = false;
119 if (!ParseDisabledParameters(env, argv[PARAM0], disabled)) {
120 return false;
121 }
122 param.SetDisabled(disabled);
123 std::vector<std::string> bundleList;
124 if (!ParseBundleListParameters(env, argv[PARAM1], bundleList)) {
125 return false;
126 }
127 param.SetBundleList(bundleList);
128
129 if (argc == DISABLE_MAX_PARA) {
130 int32_t userId = SUBSCRIBE_USER_INIT;
131 if (!ParseUserIdParameters(env, argv[PARAM2], userId)) {
132 return false;
133 }
134 param.SetUserId(userId);
135 }
136 return true;
137 }
138 }
139 }