• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "webview_adsblock_manager.h"
17 
18 #include "nweb_helper.h"
19 #include "web_errors.h"
20 #include "webview_utils.h"
21 #include "webview_log.h"
22 #include "nweb_adsblock_manager.h"
23 
24 using namespace OHOS::NWeb;
25 
26 namespace OHOS {
27 namespace Webview {
28     constexpr int MAX_URL_RULES_FILEPATH_LENGTH = 255;
29 
SetAdsBlockRules(const char * rulesFile,bool replace)30     int32_t AdsBlockManagerImpl::SetAdsBlockRules(const char *rulesFile, bool replace)
31     {
32         if (rulesFile == nullptr) {
33             WEBVIEWLOGE("AdsBlockManagerImpl::SetAdsBlockRules failed: rulesFile is null.");
34             return NWebError::PARAM_CHECK_ERROR;
35         }
36         std::string rulesFileStr(rulesFile);
37         if (rulesFileStr.length() > MAX_URL_RULES_FILEPATH_LENGTH) {
38             WEBVIEWLOGE("AdsBlockManagerImpl::SetAdsBlockRules failed: rulesFile path too long");
39             return NWebError::PARAM_CHECK_ERROR;
40         }
41         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
42         if (adsBlockManager != nullptr) {
43             adsBlockManager->SetAdsBlockRules(rulesFile, replace);
44         }
45         return NWebError::NO_ERROR;
46     }
47 
AddAdsBlockDisallowedList(CArrString domainSuffixes)48     int32_t AdsBlockManagerImpl::AddAdsBlockDisallowedList(CArrString domainSuffixes)
49     {
50         if (domainSuffixes.size <= 0 || domainSuffixes.head == nullptr) {
51             WEBVIEWLOGE("AdsBlockManagerImpl::AddAdsBlockDisallowedList failed: domainSuffixes is null.");
52             return NWebError::PARAM_CHECK_ERROR;
53         }
54         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
55         if (adsBlockManager != nullptr) {
56             adsBlockManager->AddAdsBlockDisallowedList(OHOS::Webview::CArrStringToVector(domainSuffixes));
57         }
58         return NWebError::NO_ERROR;
59     }
60 
RemoveAdsBlockDisallowedList(CArrString domainSuffixes)61     int32_t AdsBlockManagerImpl::RemoveAdsBlockDisallowedList(CArrString domainSuffixes)
62     {
63         if (domainSuffixes.size <= 0 || domainSuffixes.head == nullptr) {
64             WEBVIEWLOGE("AdsBlockManagerImpl::RemoveAdsBlockDisallowedList failed: domainSuffixes is null.");
65             return NWebError::PARAM_CHECK_ERROR;
66         }
67         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
68         if (adsBlockManager != nullptr) {
69             adsBlockManager->RemoveAdsBlockDisallowedList(OHOS::Webview::CArrStringToVector(domainSuffixes));
70         }
71         return NWebError::NO_ERROR;
72     }
73 
AddAdsBlockAllowedList(CArrString domainSuffixes)74     int32_t AdsBlockManagerImpl::AddAdsBlockAllowedList(CArrString domainSuffixes)
75     {
76         if (domainSuffixes.size <= 0 || domainSuffixes.head == nullptr) {
77             WEBVIEWLOGE("AdsBlockManagerImpl::AddAdsBlockAllowedList failed: domainSuffixes is null.");
78             return NWebError::PARAM_CHECK_ERROR;
79         }
80         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
81         if (adsBlockManager != nullptr) {
82             adsBlockManager->AddAdsBlockAllowedList(OHOS::Webview::CArrStringToVector(domainSuffixes));
83         }
84         return NWebError::NO_ERROR;
85     }
86 
RemoveAdsBlockAllowedList(CArrString domainSuffixes)87     int32_t AdsBlockManagerImpl::RemoveAdsBlockAllowedList(CArrString domainSuffixes)
88     {
89         if (domainSuffixes.size <= 0 || domainSuffixes.head == nullptr) {
90             WEBVIEWLOGE("AdsBlockManagerImpl::RemoveAdsBlockAllowedList failed: domainSuffixes is null.");
91             return NWebError::PARAM_CHECK_ERROR;
92         }
93         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
94         if (adsBlockManager != nullptr) {
95             adsBlockManager->RemoveAdsBlockAllowedList(OHOS::Webview::CArrStringToVector(domainSuffixes));
96         }
97         return NWebError::NO_ERROR;
98     }
99 
ClearAdsBlockAllowedList()100     void AdsBlockManagerImpl::ClearAdsBlockAllowedList()
101     {
102         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
103         if (adsBlockManager != nullptr) {
104             adsBlockManager->ClearAdsBlockAllowedList();
105         }
106     }
107 
ClearAdsBlockDisallowedList()108     void AdsBlockManagerImpl::ClearAdsBlockDisallowedList()
109     {
110         std::shared_ptr<NWebAdsBlockManager> adsBlockManager = NWebHelper::Instance().GetAdsBlockManager();
111         if (adsBlockManager != nullptr) {
112             adsBlockManager->ClearAdsBlockDisallowedList();
113         }
114     }
115 }
116 }