File indexing completed on 2024-05-12 04:57:50
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 /** 0019 * Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net> 0020 * 0021 * Redistribution and use in source and binary forms, with or without 0022 * modification, are permitted provided that the following conditions 0023 * are met: 0024 * 1. Redistributions of source code must retain the above copyright 0025 * notice, this list of conditions and the following disclaimer. 0026 * 2. Redistributions in binary form must reproduce the above copyright 0027 * notice, this list of conditions and the following disclaimer in the 0028 * documentation and/or other materials provided with the distribution. 0029 * 3. Neither the name of the Benjamin Meyer nor the names of its contributors 0030 * may be used to endorse or promote products derived from this software 0031 * without specific prior written permission. 0032 * 0033 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 0034 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 0035 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 0036 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 0037 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 0038 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 0039 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 0040 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 0041 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 0042 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 0043 * SUCH DAMAGE. 0044 */ 0045 0046 #ifndef ADBLOCKRULE_H 0047 #define ADBLOCKRULE_H 0048 0049 #include <QObject> 0050 #include <QStringList> 0051 #include <QStringMatcher> 0052 #include <QRegularExpression> 0053 0054 #include "qzcommon.h" 0055 0056 class QUrl; 0057 class QWebEngineUrlRequestInfo; 0058 0059 class AdBlockSubscription; 0060 0061 class FALKON_EXPORT AdBlockRule 0062 { 0063 Q_DISABLE_COPY(AdBlockRule) 0064 0065 public: 0066 AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = nullptr); 0067 ~AdBlockRule(); 0068 0069 AdBlockRule* copy() const; 0070 0071 AdBlockSubscription* subscription() const; 0072 void setSubscription(AdBlockSubscription* subscription); 0073 0074 QString filter() const; 0075 void setFilter(const QString &filter); 0076 0077 bool isCssRule() const; 0078 QString cssSelector() const; 0079 0080 bool isUnsupportedRule() const; 0081 0082 bool isDocument() const; 0083 bool isElemhide() const; 0084 bool isGenerichide() const; 0085 0086 bool isDomainRestricted() const; 0087 bool isException() const; 0088 0089 bool isComment() const; 0090 bool isEnabled() const; 0091 void setEnabled(bool enabled); 0092 0093 bool isSlow() const; 0094 bool isInternalDisabled() const; 0095 0096 bool urlMatch(const QUrl &url) const; 0097 bool networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const; 0098 0099 bool matchDomain(const QString &domain) const; 0100 bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const; 0101 0102 bool matchType(const QWebEngineUrlRequestInfo &request) const; 0103 0104 protected: 0105 bool stringMatch(const QString &domain, const QString &encodedUrl) const; 0106 bool isMatchingDomain(const QString &domain, const QString &filter) const; 0107 bool isMatchingRegExpStrings(const QString &url) const; 0108 QStringList parseRegExpFilter(const QString &filter) const; 0109 0110 private: 0111 enum RuleType { 0112 CssRule = 0, 0113 DomainMatchRule = 1, 0114 RegExpMatchRule = 2, 0115 StringEndsMatchRule = 3, 0116 StringContainsMatchRule = 4, 0117 MatchAllUrlsRule = 5, 0118 ExtendedCssRule = 6, 0119 SnippetRule = 7, 0120 Invalid = 8 0121 }; 0122 0123 enum RuleOption { 0124 NoOption = 0, 0125 DomainRestrictedOption = 1, 0126 ThirdPartyOption = 1 << 1, 0127 0128 ObjectOption = 1 << 2, 0129 SubdocumentOption = 1 << 3, 0130 XMLHttpRequestOption = 1 << 4, 0131 ImageOption = 1 << 5, 0132 ScriptOption = 1 << 6, 0133 StyleSheetOption = 1 << 7, 0134 ObjectSubrequestOption = 1 << 8, 0135 PingOption = 1 << 9, 0136 MediaOption = 1 << 10, 0137 FontOption = 1 << 11, 0138 OtherOption = 1 << 12, 0139 TypeOptions = ObjectOption 0140 | SubdocumentOption 0141 | XMLHttpRequestOption 0142 | ImageOption 0143 | ScriptOption 0144 | StyleSheetOption 0145 | ObjectSubrequestOption 0146 | PingOption 0147 | MediaOption 0148 | FontOption 0149 | OtherOption, 0150 0151 PopupOption = 1 << 13, 0152 0153 // Exception only options 0154 DocumentOption = 1 << 20, 0155 ElementHideOption = 1 << 21, 0156 GenericHideOption = 1 << 22, 0157 GenericBlockOption = 1 << 23, 0158 }; 0159 0160 Q_DECLARE_FLAGS(RuleOptions, RuleOption) 0161 0162 inline bool hasOption(const RuleOption &opt) const; 0163 inline bool hasException(const RuleOption &opt) const; 0164 0165 inline void setOption(const RuleOption &opt); 0166 inline void setException(const RuleOption &opt, bool on); 0167 0168 void parseFilter(); 0169 void parseDomains(const QString &domains, const QChar &separator); 0170 bool filterIsOnlyDomain(const QString &filter) const; 0171 bool filterIsOnlyEndsMatch(const QString &filter) const; 0172 QString createRegExpFromFilter(const QString &filter) const; 0173 QList<QStringMatcher> createStringMatchers(const QStringList &filters) const; 0174 0175 AdBlockSubscription* m_subscription; 0176 0177 RuleType m_type; 0178 RuleOptions m_options; 0179 RuleOptions m_exceptions; 0180 0181 // Original rule filter 0182 QString m_filter; 0183 // Parsed rule for string matching (CSS Selector for CSS rules) 0184 QString m_matchString; 0185 // Case sensitivity for string matching 0186 Qt::CaseSensitivity m_caseSensitivity; 0187 0188 bool m_isEnabled; 0189 bool m_isException; 0190 bool m_isInternalDisabled; 0191 0192 QStringList m_allowedDomains; 0193 QStringList m_blockedDomains; 0194 0195 struct RegExp { 0196 QRegularExpression regExp; 0197 QList<QStringMatcher> matchers; 0198 }; 0199 0200 // Use dynamic allocation to save memory 0201 RegExp* m_regExp; 0202 0203 friend class AdBlockMatcher; 0204 friend class AdBlockSearchTree; 0205 friend class AdBlockSubscription; 0206 }; 0207 0208 #endif // ADBLOCKRULE_H 0209