Falkon Develop
Cross-platform Qt-based web browser
adblockmatcher.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2014-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "adblockmatcher.h"
19#include "adblockmanager.h"
20#include "adblockrule.h"
21#include "adblocksubscription.h"
22
24 : QObject(manager)
25 , m_manager(manager)
26{
27}
28
30{
31 clear();
32}
33
34const AdBlockRule* AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const
35{
36 // Exception rules
37 if (m_networkExceptionTree.find(request, urlDomain, urlString))
38 return nullptr;
39
40 int count = m_networkExceptionRules.count();
41 for (int i = 0; i < count; ++i) {
42 const AdBlockRule* rule = m_networkExceptionRules.at(i);
43 if (rule->networkMatch(request, urlDomain, urlString))
44 return nullptr;
45 }
46
47 // Block rules
48 if (const AdBlockRule* rule = m_networkBlockTree.find(request, urlDomain, urlString))
49 return rule;
50
51 count = m_networkBlockRules.count();
52 for (int i = 0; i < count; ++i) {
53 const AdBlockRule* rule = m_networkBlockRules.at(i);
54 if (rule->networkMatch(request, urlDomain, urlString))
55 return rule;
56 }
57
58 return nullptr;
59}
60
61bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const
62{
63 int count = m_documentRules.count();
64
65 for (int i = 0; i < count; ++i)
66 if (m_documentRules.at(i)->urlMatch(url))
67 return true;
68
69 return false;
70}
71
72bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const
73{
74 if (adBlockDisabledForUrl(url))
75 return true;
76
77 int count = m_elemhideRules.count();
78
79 for (int i = 0; i < count; ++i)
80 if (m_elemhideRules.at(i)->urlMatch(url))
81 return true;
82
83 return false;
84}
85
87{
89 return true;
90
91 int count = m_generichideRules.count();
92
93 for (int i = 0; i < count; ++i)
94 if (m_generichideRules.at(i)->urlMatch(url))
95 return true;
96
97 return false;
98}
99
101{
102 return m_elementHidingRules;
103}
104
105QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
106{
107 QString rules;
108 int addedRulesCount = 0;
109 int count = m_domainRestrictedCssRules.count();
110
111 for (int i = 0; i < count; ++i) {
112 const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
113 if (!rule->matchDomain(domain))
114 continue;
115
116 if (Q_UNLIKELY(addedRulesCount == 1000)) {
117 rules.append(rule->cssSelector());
118 rules.append(QL1S("{display:none !important;}\n"));
119 addedRulesCount = 0;
120 }
121 else {
122 rules.append(rule->cssSelector() + QLatin1Char(','));
123 addedRulesCount++;
124 }
125 }
126
127 if (addedRulesCount != 0) {
128 rules = rules.left(rules.size() - 1);
129 rules.append(QL1S("{display:none !important;}\n"));
130 }
131
132 return rules;
133}
134
136{
137 clear();
138
139 QHash<QString, const AdBlockRule*> cssRulesHash;
140 QVector<const AdBlockRule*> exceptionCssRules;
141
142 const auto subscriptions = m_manager->subscriptions();
143 for (AdBlockSubscription* subscription : subscriptions) {
144 const auto rules = subscription->allRules();
145 for (const AdBlockRule* rule : rules) {
146 // Don't add internally disabled rules to cache
147 if (rule->isInternalDisabled())
148 continue;
149 // Or unsupported ones
150 if (rule->isUnsupportedRule())
151 continue;
152
153 if (rule->isCssRule()) {
154 // We will add only enabled css rules to cache, because there is no enabled/disabled
155 // check on match. They are directly embedded to pages.
156 if (!rule->isEnabled())
157 continue;
158
159 if (rule->isException())
160 exceptionCssRules.append(rule);
161 else
162 cssRulesHash.insert(rule->cssSelector(), rule);
163 }
164 else if (rule->isDocument()) {
165 m_documentRules.append(rule);
166 }
167 else if (rule->isElemhide()) {
168 m_elemhideRules.append(rule);
169 }
170 else if (rule->isGenerichide()) {
171 m_generichideRules.append(rule);
172 }
173 else if (rule->isException()) {
174 if (!m_networkExceptionTree.add(rule))
175 m_networkExceptionRules.append(rule);
176 } else {
177 if (!m_networkBlockTree.add(rule))
178 m_networkBlockRules.append(rule);
179 }
180 }
181 }
182
183 for (const AdBlockRule* rule : std::as_const(exceptionCssRules)) {
184 const AdBlockRule* originalRule = cssRulesHash.value(rule->cssSelector());
185
186 // If we don't have this selector, the exception does nothing
187 if (!originalRule)
188 continue;
189
190 AdBlockRule* copiedRule = originalRule->copy();
191 copiedRule->m_options |= AdBlockRule::DomainRestrictedOption;
192 copiedRule->m_blockedDomains.append(rule->m_allowedDomains);
193
194 cssRulesHash[rule->cssSelector()] = copiedRule;
195 m_createdRules.append(copiedRule);
196 }
197
198 // Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes.
199 // (In my testings, 4931 is the number that makes it crash)
200 // So let's split it by 1000 selectors...
201 int hidingRulesCount = 0;
202
203 QHashIterator<QString, const AdBlockRule*> it(cssRulesHash);
204 while (it.hasNext()) {
205 it.next();
206 const AdBlockRule* rule = it.value();
207
208 if (rule->isDomainRestricted()) {
209 m_domainRestrictedCssRules.append(rule);
210 }
211 else if (Q_UNLIKELY(hidingRulesCount == 1000)) {
212 m_elementHidingRules.append(rule->cssSelector());
213 m_elementHidingRules.append(QL1S("{display:none !important;} "));
214 hidingRulesCount = 0;
215 }
216 else {
217 m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(','));
218 hidingRulesCount++;
219 }
220 }
221
222 if (hidingRulesCount != 0) {
223 m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1);
224 m_elementHidingRules.append(QL1S("{display:none !important;} "));
225 }
226}
227
229{
230 m_networkExceptionTree.clear();
231 m_networkExceptionRules.clear();
232 m_networkBlockTree.clear();
233 m_networkBlockRules.clear();
234 m_domainRestrictedCssRules.clear();
235 m_elementHidingRules.clear();
236 m_documentRules.clear();
237 m_elemhideRules.clear();
238 m_generichideRules.clear();
239 qDeleteAll(m_createdRules);
240 m_createdRules.clear();
241}
QList< AdBlockSubscription * > subscriptions() const
bool adBlockDisabledForUrl(const QUrl &url) const
const AdBlockRule * match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const
QString elementHidingRules() const
QString elementHidingRulesForDomain(const QString &domain) const
bool elemHideDisabledForUrl(const QUrl &url) const
bool genericElemHideDisabledForUrl(const QUrl &url) const
AdBlockMatcher(AdBlockManager *manager)
bool isDomainRestricted() const
bool matchDomain(const QString &domain) const
QString cssSelector() const
bool networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const
AdBlockRule * copy() const
const AdBlockRule * find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const
bool add(const AdBlockRule *rule)
i
Definition: i18n.py:23
#define Q_UNLIKELY(x)
Definition: qzcommon.h:32
#define QL1S(x)
Definition: qzcommon.h:44