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 "napi_web_adsblock_manager.h"
17
18 #include <cstdint>
19 #include <uv.h>
20 #include <vector>
21
22 #include "business_error.h"
23 #include "napi/native_common.h"
24 #include "nweb_adsblock_manager.h"
25 #include "nweb_helper.h"
26 #include "nweb_log.h"
27 #include "napi_parse_utils.h"
28 #include "web_errors.h"
29 #include "securec.h"
30 #include "system_properties_adapter_impl.h"
31
32 namespace OHOS {
33 namespace NWeb {
34
35 constexpr int MAX_URL_RULES_FILEPATH_LENGTH = 255;
36
Init(napi_env env,napi_value exports)37 napi_value NapiWebAdsBlockManager::Init(napi_env env, napi_value exports)
38 {
39 napi_property_descriptor properties[] = {
40 DECLARE_NAPI_STATIC_FUNCTION("setAdsBlockRules",
41 NapiWebAdsBlockManager::JsSetAdsBlockRules),
42 DECLARE_NAPI_STATIC_FUNCTION("addAdsBlockDisallowedList",
43 NapiWebAdsBlockManager::JsAddAdsBlockDisallowedList),
44 DECLARE_NAPI_STATIC_FUNCTION("removeAdsBlockDisallowedList",
45 NapiWebAdsBlockManager::JsRemoveAdsBlockDisallowedList),
46 DECLARE_NAPI_STATIC_FUNCTION("clearAdsBlockDisallowedList",
47 NapiWebAdsBlockManager::JsClearAdsBlockDisallowedList),
48 DECLARE_NAPI_STATIC_FUNCTION("addAdsBlockAllowedList",
49 NapiWebAdsBlockManager::JsAddAdsBlockAllowedList),
50 DECLARE_NAPI_STATIC_FUNCTION("removeAdsBlockAllowedList",
51 NapiWebAdsBlockManager::JsRemoveAdsBlockAllowedList),
52 DECLARE_NAPI_STATIC_FUNCTION("clearAdsBlockAllowedList",
53 NapiWebAdsBlockManager::JsClearAdsBlockAllowedList),
54 };
55 napi_value constructor = nullptr;
56
57 napi_define_class(env, WEB_ADSBLOCK_MANAGER_CLASS_NAME.c_str(), WEB_ADSBLOCK_MANAGER_CLASS_NAME.length(),
58 JsConstructor, nullptr, sizeof(properties) / sizeof(properties[0]), properties, &constructor);
59 NAPI_ASSERT(env, constructor != nullptr, "NapiWebAdsBlockManager define js class failed");
60 napi_status status = napi_set_named_property(env, exports, "AdsBlockManager", constructor);
61 NAPI_ASSERT(env, status == napi_ok, "NapiWebAdsBlockManager set property failed");
62 return exports;
63 }
64
JsConstructor(napi_env env,napi_callback_info info)65 napi_value NapiWebAdsBlockManager::JsConstructor(napi_env env, napi_callback_info info)
66 {
67 napi_value thisVar = nullptr;
68
69 size_t argc = INTEGER_TWO;
70 napi_value argv[INTEGER_TWO] = { 0 };
71 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
72 return thisVar;
73 }
74
JsSetAdsBlockRules(napi_env env,napi_callback_info info)75 napi_value NapiWebAdsBlockManager::JsSetAdsBlockRules(napi_env env, napi_callback_info info)
76 {
77 napi_value result = nullptr;
78 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
79 WVLOG_E("SetAdsBlockRules: Capability not supported.");
80 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
81 return result;
82 }
83 napi_value retValue = nullptr;
84 size_t argc = INTEGER_TWO;
85 napi_value argv[INTEGER_TWO] = { 0 };
86
87 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
88 if (argc != INTEGER_TWO) {
89 WVLOG_E("setAdsBlockRules failed: wrong param count");
90 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
91 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "two"));
92 return nullptr;
93 }
94
95 std::string rulesFile;
96 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], rulesFile)) {
97 WVLOG_E("setAdsBlockRules failed: first param should be string");
98 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
99 "BusinessError 401: Parameter error. The type of rulesFile must be string.");
100 return result;
101 }
102
103 if (rulesFile.length() > MAX_URL_RULES_FILEPATH_LENGTH) {
104 WVLOG_E("setAdsBlockRules failed: rulesFile path too long > %{public}d", MAX_URL_RULES_FILEPATH_LENGTH);
105 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
106 "BusinessError 401: Parameter error. rulesFile path too long > 255.");
107 return result;
108 }
109
110 bool replace = false;
111 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ONE], replace)) {
112 WVLOG_E("setAdsBlockRules failed: invalid replace value");
113 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
114 "BusinessError 401: Parameter error. The type of replace must be boolean.");
115 return result;
116 }
117
118 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
119 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
120 if (adsBlockManager != nullptr) {
121 adsBlockManager->SetAdsBlockRules(rulesFile, replace);
122 }
123
124 NAPI_CALL(env, napi_get_undefined(env, &result));
125 return result;
126 }
127
JsAddAdsBlockDisallowedList(napi_env env,napi_callback_info info)128 napi_value NapiWebAdsBlockManager::JsAddAdsBlockDisallowedList(napi_env env, napi_callback_info info)
129 {
130 napi_value result = nullptr;
131 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
132 WVLOG_E("AddAdsBlockDisallowedList: Capability not supported.");
133 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
134 return result;
135 }
136 napi_value retValue = nullptr;
137 size_t argc = INTEGER_ONE;
138 napi_value argv[INTEGER_ONE] = { 0 };
139
140 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
141 if (argc != INTEGER_ONE) {
142 WVLOG_E("AddAdsBlockDisallowedList failed: wrong param count");
143 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
144 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
145 return nullptr;
146 }
147
148 std::vector<std::string> domainSuffixes;
149 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_ZERO], domainSuffixes)) {
150 WVLOG_E("AddAdsBlockDisallowedList failed: domainSuffixes should be an array of string");
151 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
152 "BusinessError 401: Parameter error. The type of domainSuffixes must be an array of string.");
153 return result;
154 }
155
156 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
157 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
158 if (adsBlockManager != nullptr) {
159 adsBlockManager->AddAdsBlockDisallowedList(domainSuffixes);
160 }
161
162 NAPI_CALL(env, napi_get_undefined(env, &result));
163 return result;
164 }
165
JsRemoveAdsBlockDisallowedList(napi_env env,napi_callback_info info)166 napi_value NapiWebAdsBlockManager::JsRemoveAdsBlockDisallowedList(napi_env env, napi_callback_info info)
167 {
168 napi_value result = nullptr;
169 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
170 WVLOG_E("RemoveAdsBlockDisallowedList: Capability not supported.");
171 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
172 return result;
173 }
174 napi_value retValue = nullptr;
175 size_t argc = INTEGER_ONE;
176 napi_value argv[INTEGER_ONE] = { 0 };
177
178 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
179 if (argc != INTEGER_ONE) {
180 WVLOG_E("removeAdsBlockDisallowedList failed: wrong param count");
181 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
182 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
183 return nullptr;
184 }
185
186 std::vector<std::string> domainSuffixes;
187 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_ZERO], domainSuffixes)) {
188 WVLOG_E("removeAdsBlockDisallowedList failed: domainSuffixes should be an array of string");
189 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
190 "BusinessError 401: Parameter error. The type of domainSuffixes must be an array of string.");
191 return result;
192 }
193
194 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
195 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
196 if (adsBlockManager != nullptr) {
197 adsBlockManager->RemoveAdsBlockDisallowedList(domainSuffixes);
198 }
199
200 NAPI_CALL(env, napi_get_undefined(env, &result));
201 return result;
202 }
203
JsClearAdsBlockDisallowedList(napi_env env,napi_callback_info info)204 napi_value NapiWebAdsBlockManager::JsClearAdsBlockDisallowedList(napi_env env, napi_callback_info info)
205 {
206 napi_value result = nullptr;
207 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
208 WVLOG_E("ClearAdsBlockDisallowedList: Capability not supported.");
209 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
210 return result;
211 }
212
213 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
214 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
215 if (adsBlockManager != nullptr) {
216 adsBlockManager->ClearAdsBlockDisallowedList();
217 }
218
219 NAPI_CALL(env, napi_get_undefined(env, &result));
220 return result;
221 }
222
JsAddAdsBlockAllowedList(napi_env env,napi_callback_info info)223 napi_value NapiWebAdsBlockManager::JsAddAdsBlockAllowedList(napi_env env, napi_callback_info info)
224 {
225 napi_value result = nullptr;
226 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
227 WVLOG_E("AddAdsBlockAllowedList: Capability not supported.");
228 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
229 return result;
230 }
231 napi_value retValue = nullptr;
232 size_t argc = INTEGER_ONE;
233 napi_value argv[INTEGER_ONE] = { 0 };
234
235 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
236 if (argc != INTEGER_ONE) {
237 WVLOG_E("AddAdsBlockAllowedList failed: wrong param count");
238 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
239 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
240 return nullptr;
241 }
242
243 std::vector<std::string> domainSuffixes;
244 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_ZERO], domainSuffixes)) {
245 WVLOG_E("AddAdsBlockAllowedList failed: domainSuffixes should be an array of string");
246 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
247 "BusinessError 401: Parameter error. The type of domainSuffixes must be an array of string.");
248 return result;
249 }
250
251 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
252 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
253 if (adsBlockManager != nullptr) {
254 adsBlockManager->AddAdsBlockAllowedList(domainSuffixes);
255 }
256
257 NAPI_CALL(env, napi_get_undefined(env, &result));
258 return result;
259 }
260
JsRemoveAdsBlockAllowedList(napi_env env,napi_callback_info info)261 napi_value NapiWebAdsBlockManager::JsRemoveAdsBlockAllowedList(napi_env env, napi_callback_info info)
262 {
263 napi_value result = nullptr;
264 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
265 WVLOG_E("RemoveAdsBlockAllowedList: Capability not supported.");
266 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
267 return result;
268 }
269 napi_value retValue = nullptr;
270 size_t argc = INTEGER_ONE;
271 napi_value argv[INTEGER_ONE] = { 0 };
272
273 napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
274 if (argc != INTEGER_ONE) {
275 WVLOG_E("removeAdsBlockAllowedList failed: wrong param count");
276 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
277 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
278 return nullptr;
279 }
280
281 std::vector<std::string> domainSuffixes;
282 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_ZERO], domainSuffixes)) {
283 WVLOG_E("removeAdsBlockAllowedList failed: domainSuffixes should be an array of string");
284 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
285 "BusinessError 401: Parameter error. The type of domainSuffixes must be an array of string.");
286 return result;
287 }
288
289 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
290 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
291 if (adsBlockManager != nullptr) {
292 adsBlockManager->RemoveAdsBlockAllowedList(domainSuffixes);
293 }
294
295 NAPI_CALL(env, napi_get_undefined(env, &result));
296 return result;
297 }
298
JsClearAdsBlockAllowedList(napi_env env,napi_callback_info info)299 napi_value NapiWebAdsBlockManager::JsClearAdsBlockAllowedList(napi_env env, napi_callback_info info)
300 {
301 napi_value result = nullptr;
302 if (SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType() == ProductDeviceType::DEVICE_TYPE_WEARABLE) {
303 WVLOG_E("ClearAdsBlockAllowedList: Capability not supported.");
304 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::CAPABILITY_NOT_SUPPORTED_ERROR);
305 return result;
306 }
307
308 std::shared_ptr<OHOS::NWeb::NWebAdsBlockManager> adsBlockManager =
309 OHOS::NWeb::NWebHelper::Instance().GetAdsBlockManager();
310 if (adsBlockManager != nullptr) {
311 adsBlockManager->ClearAdsBlockAllowedList();
312 }
313
314 NAPI_CALL(env, napi_get_undefined(env, &result));
315 return result;
316 }
317
318 } // namespace NWeb
319 } // namespace OHOS
320