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