Falkon Develop
Cross-platform Qt-based web browser
adblockrule.h
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-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* ============================================================ */
46#ifndef ADBLOCKRULE_H
47#define ADBLOCKRULE_H
48
49#include <QObject>
50#include <QStringList>
51#include <QStringMatcher>
52#include <QRegularExpression>
53
54#include "qzcommon.h"
55
56class QUrl;
57class QWebEngineUrlRequestInfo;
58
60
62{
63 Q_DISABLE_COPY(AdBlockRule)
64
65public:
66 AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = nullptr);
68
69 AdBlockRule* copy() const;
70
71 AdBlockSubscription* subscription() const;
72 void setSubscription(AdBlockSubscription* subscription);
73
74 QString filter() const;
75 void setFilter(const QString &filter);
76
77 bool isCssRule() const;
78 QString cssSelector() const;
79
80 bool isUnsupportedRule() const;
81
82 bool isDocument() const;
83 bool isElemhide() const;
84 bool isGenerichide() const;
85
86 bool isDomainRestricted() const;
87 bool isException() const;
88
89 bool isComment() const;
90 bool isEnabled() const;
91 void setEnabled(bool enabled);
92
93 bool isSlow() const;
94 bool isInternalDisabled() const;
95
96 bool urlMatch(const QUrl &url) const;
97 bool networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const;
98
99 bool matchDomain(const QString &domain) const;
100 bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const;
101
102 bool matchType(const QWebEngineUrlRequestInfo &request) const;
103
104protected:
105 bool stringMatch(const QString &domain, const QString &encodedUrl) const;
106 bool isMatchingDomain(const QString &domain, const QString &filter) const;
107 bool isMatchingRegExpStrings(const QString &url) const;
108 QStringList parseRegExpFilter(const QString &filter) const;
109
110private:
111 enum RuleType {
112 CssRule = 0,
113 DomainMatchRule = 1,
114 RegExpMatchRule = 2,
115 StringEndsMatchRule = 3,
116 StringContainsMatchRule = 4,
117 MatchAllUrlsRule = 5,
118 ExtendedCssRule = 6,
119 SnippetRule = 7,
120 Invalid = 8
121 };
122
123 enum RuleOption {
124 NoOption = 0,
125 DomainRestrictedOption = 1,
126 ThirdPartyOption = 1 << 1,
127
128 ObjectOption = 1 << 2,
129 SubdocumentOption = 1 << 3,
130 XMLHttpRequestOption = 1 << 4,
131 ImageOption = 1 << 5,
132 ScriptOption = 1 << 6,
133 StyleSheetOption = 1 << 7,
134 ObjectSubrequestOption = 1 << 8,
135 PingOption = 1 << 9,
136 MediaOption = 1 << 10,
137 FontOption = 1 << 11,
138 OtherOption = 1 << 12,
139 TypeOptions = ObjectOption
140 | SubdocumentOption
141 | XMLHttpRequestOption
142 | ImageOption
143 | ScriptOption
144 | StyleSheetOption
145 | ObjectSubrequestOption
146 | PingOption
147 | MediaOption
148 | FontOption
149 | OtherOption,
150
151 PopupOption = 1 << 13,
152
153 // Exception only options
154 DocumentOption = 1 << 20,
155 ElementHideOption = 1 << 21,
156 GenericHideOption = 1 << 22,
157 GenericBlockOption = 1 << 23,
158 };
159
160 Q_DECLARE_FLAGS(RuleOptions, RuleOption)
161
162 inline bool hasOption(const RuleOption &opt) const;
163 inline bool hasException(const RuleOption &opt) const;
164
165 inline void setOption(const RuleOption &opt);
166 inline void setException(const RuleOption &opt, bool on);
167
168 void parseFilter();
169 void parseDomains(const QString &domains, const QChar &separator);
170 bool filterIsOnlyDomain(const QString &filter) const;
171 bool filterIsOnlyEndsMatch(const QString &filter) const;
172 QString createRegExpFromFilter(const QString &filter) const;
173 QList<QStringMatcher> createStringMatchers(const QStringList &filters) const;
174
175 AdBlockSubscription* m_subscription;
176
177 RuleType m_type;
178 RuleOptions m_options;
179 RuleOptions m_exceptions;
180
181 // Original rule filter
182 QString m_filter;
183 // Parsed rule for string matching (CSS Selector for CSS rules)
184 QString m_matchString;
185 // Case sensitivity for string matching
186 Qt::CaseSensitivity m_caseSensitivity;
187
188 bool m_isEnabled;
189 bool m_isException;
190 bool m_isInternalDisabled;
191
192 QStringList m_allowedDomains;
193 QStringList m_blockedDomains;
194
195 struct RegExp {
196 QRegularExpression regExp;
197 QList<QStringMatcher> matchers;
198 };
199
200 // Use dynamic allocation to save memory
201 RegExp* m_regExp;
202
203 friend class AdBlockMatcher;
204 friend class AdBlockSearchTree;
206};
207
208#endif // ADBLOCKRULE_H
209
#define FALKON_EXPORT
Definition: qzcommon.h:28