2011-03-27 21:59:40 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2017-08-12 16:25:45 +02:00
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2011-03-27 21:59:40 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ADBLOCKRULE_H
|
|
|
|
#define ADBLOCKRULE_H
|
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QObject>
|
2014-04-13 12:24:37 +02:00
|
|
|
#include <QStringList>
|
2014-04-13 11:49:39 +02:00
|
|
|
#include <QStringMatcher>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2014-02-26 20:03:20 +01:00
|
|
|
#include "qzcommon.h"
|
2013-02-24 10:57:58 +01:00
|
|
|
#include "qzregexp.h"
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
class QUrl;
|
2015-10-05 20:16:51 +02:00
|
|
|
class QWebEngineUrlRequestInfo;
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-07-01 18:11:43 +02:00
|
|
|
class AdBlockSubscription;
|
|
|
|
|
2017-08-25 17:11:29 +02:00
|
|
|
class FALKON_EXPORT AdBlockRule
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2015-05-28 11:10:41 +02:00
|
|
|
Q_DISABLE_COPY(AdBlockRule)
|
|
|
|
|
2011-03-27 21:59:40 +02:00
|
|
|
public:
|
2012-07-01 18:11:43 +02:00
|
|
|
AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = 0);
|
2013-02-26 10:42:48 +01:00
|
|
|
~AdBlockRule();
|
2012-07-01 18:11:43 +02:00
|
|
|
|
2014-09-16 10:38:28 +02:00
|
|
|
AdBlockRule* copy() const;
|
|
|
|
|
2012-07-01 18:11:43 +02:00
|
|
|
AdBlockSubscription* subscription() const;
|
|
|
|
void setSubscription(AdBlockSubscription* subscription);
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
QString filter() const;
|
|
|
|
void setFilter(const QString &filter);
|
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
bool isCssRule() const;
|
|
|
|
QString cssSelector() const;
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-07-04 16:00:53 +02:00
|
|
|
bool isDocument() const;
|
|
|
|
bool isElemhide() const;
|
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
bool isDomainRestricted() const;
|
2011-03-27 21:59:40 +02:00
|
|
|
bool isException() const;
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
bool isComment() const;
|
2011-03-27 21:59:40 +02:00
|
|
|
bool isEnabled() const;
|
|
|
|
void setEnabled(bool enabled);
|
|
|
|
|
2012-07-01 14:44:01 +02:00
|
|
|
bool isSlow() const;
|
2012-06-25 16:07:25 +02:00
|
|
|
bool isInternalDisabled() const;
|
|
|
|
|
2012-07-04 16:00:53 +02:00
|
|
|
bool urlMatch(const QUrl &url) const;
|
2015-10-05 20:16:51 +02:00
|
|
|
bool networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const;
|
2012-06-28 01:41:01 +02:00
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
bool matchDomain(const QString &domain) const;
|
2015-10-05 20:16:51 +02:00
|
|
|
bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchObject(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchSubdocument(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchImage(const QWebEngineUrlRequestInfo &request) const;
|
2015-10-05 20:43:05 +02:00
|
|
|
bool matchScript(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchStyleSheet(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const;
|
2017-08-12 17:31:11 +02:00
|
|
|
bool matchPing(const QWebEngineUrlRequestInfo &request) const;
|
|
|
|
bool matchMedia(const QWebEngineUrlRequestInfo &request) const;
|
2017-12-03 12:39:48 +01:00
|
|
|
bool matchFont(const QWebEngineUrlRequestInfo &request) const;
|
2017-08-12 17:31:11 +02:00
|
|
|
bool matchOther(const QWebEngineUrlRequestInfo &request) const;
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2013-02-26 15:48:47 +01:00
|
|
|
protected:
|
2015-10-05 20:16:51 +02:00
|
|
|
bool stringMatch(const QString &domain, const QString &encodedUrl) const;
|
2013-02-26 15:48:47 +01:00
|
|
|
bool isMatchingDomain(const QString &domain, const QString &filter) const;
|
|
|
|
bool isMatchingRegExpStrings(const QString &url) const;
|
[AdBlock] Improved performance of loading rules
Don't use regexps for parsing rules.
Added benchmark for loading subscriptions
Before:
********* Start testing of AdBlockParseRule *********
Config: Using QTest library 4.8.6, Qt 4.8.6
PASS : AdBlockParseRule::initTestCase()
RESULT : AdBlockParseRule::parseEasyList():
596.3 msecs per iteration (total: 2,982, iterations: 5)
PASS : AdBlockParseRule::parseEasyList()
PASS : AdBlockParseRule::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of AdBlockParseRule *********
After:
********* Start testing of AdBlockParseRule *********
Config: Using QTest library 4.8.6, Qt 4.8.6
PASS : AdBlockParseRule::initTestCase()
RESULT : AdBlockParseRule::parseEasyList():
481.8 msecs per iteration (total: 2,409, iterations: 5)
PASS : AdBlockParseRule::parseEasyList()
PASS : AdBlockParseRule::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of AdBlockParseRule *********
2014-04-06 13:34:01 +02:00
|
|
|
QStringList parseRegExpFilter(const QString &filter) const;
|
2013-02-26 15:48:47 +01:00
|
|
|
|
2011-03-27 21:59:40 +02:00
|
|
|
private:
|
2013-02-27 21:26:41 +01:00
|
|
|
enum RuleType {
|
|
|
|
CssRule = 0,
|
|
|
|
DomainMatchRule = 1,
|
|
|
|
RegExpMatchRule = 2,
|
|
|
|
StringEndsMatchRule = 3,
|
2013-11-02 17:41:51 +01:00
|
|
|
StringContainsMatchRule = 4,
|
2017-08-12 16:25:45 +02:00
|
|
|
MatchAllUrlsRule = 5,
|
|
|
|
Invalid = 6
|
2013-02-27 21:26:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum RuleOption {
|
2017-08-12 16:25:45 +02:00
|
|
|
NoOption = 0,
|
2013-02-27 21:26:41 +01:00
|
|
|
DomainRestrictedOption = 1,
|
|
|
|
ThirdPartyOption = 2,
|
|
|
|
ObjectOption = 4,
|
|
|
|
SubdocumentOption = 8,
|
|
|
|
XMLHttpRequestOption = 16,
|
|
|
|
ImageOption = 32,
|
2015-10-05 20:43:05 +02:00
|
|
|
ScriptOption = 64,
|
|
|
|
StyleSheetOption = 128,
|
|
|
|
ObjectSubrequestOption = 256,
|
2017-08-12 17:31:11 +02:00
|
|
|
PingOption = 512,
|
|
|
|
MediaOption = 1024,
|
2017-12-03 12:39:48 +01:00
|
|
|
FontOption = 2048,
|
|
|
|
OtherOption = 4096,
|
2013-02-27 21:26:41 +01:00
|
|
|
|
|
|
|
// Exception only options
|
2017-12-03 12:39:48 +01:00
|
|
|
DocumentOption = 8192,
|
|
|
|
ElementHideOption = 16384
|
2013-02-27 21:26:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_FLAGS(RuleOptions, RuleOption)
|
|
|
|
|
|
|
|
inline bool hasOption(const RuleOption &opt) const;
|
|
|
|
inline bool hasException(const RuleOption &opt) const;
|
|
|
|
|
|
|
|
inline void setOption(const RuleOption &opt);
|
|
|
|
inline void setException(const RuleOption &opt, bool on);
|
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
void parseFilter();
|
|
|
|
void parseDomains(const QString &domains, const QChar &separator);
|
[AdBlock] Improved performance of loading rules
Don't use regexps for parsing rules.
Added benchmark for loading subscriptions
Before:
********* Start testing of AdBlockParseRule *********
Config: Using QTest library 4.8.6, Qt 4.8.6
PASS : AdBlockParseRule::initTestCase()
RESULT : AdBlockParseRule::parseEasyList():
596.3 msecs per iteration (total: 2,982, iterations: 5)
PASS : AdBlockParseRule::parseEasyList()
PASS : AdBlockParseRule::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of AdBlockParseRule *********
After:
********* Start testing of AdBlockParseRule *********
Config: Using QTest library 4.8.6, Qt 4.8.6
PASS : AdBlockParseRule::initTestCase()
RESULT : AdBlockParseRule::parseEasyList():
481.8 msecs per iteration (total: 2,409, iterations: 5)
PASS : AdBlockParseRule::parseEasyList()
PASS : AdBlockParseRule::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of AdBlockParseRule *********
2014-04-06 13:34:01 +02:00
|
|
|
bool filterIsOnlyDomain(const QString &filter) const;
|
|
|
|
bool filterIsOnlyEndsMatch(const QString &filter) const;
|
|
|
|
QString createRegExpFromFilter(const QString &filter) const;
|
2014-04-13 11:49:39 +02:00
|
|
|
QList<QStringMatcher> createStringMatchers(const QStringList &filters) const;
|
2012-06-25 16:07:25 +02:00
|
|
|
|
2012-07-01 18:11:43 +02:00
|
|
|
AdBlockSubscription* m_subscription;
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2013-02-27 21:26:41 +01:00
|
|
|
RuleType m_type;
|
|
|
|
RuleOptions m_options;
|
|
|
|
RuleOptions m_exceptions;
|
2012-06-25 16:07:25 +02:00
|
|
|
|
2014-03-12 13:25:12 +01:00
|
|
|
// Original rule filter
|
2013-02-27 21:26:41 +01:00
|
|
|
QString m_filter;
|
2014-03-12 13:25:12 +01:00
|
|
|
// Parsed rule for string matching (CSS Selector for CSS rules)
|
2013-02-27 21:26:41 +01:00
|
|
|
QString m_matchString;
|
2014-03-12 13:25:12 +01:00
|
|
|
// Case sensitivity for string matching
|
2013-02-27 21:26:41 +01:00
|
|
|
Qt::CaseSensitivity m_caseSensitivity;
|
|
|
|
|
|
|
|
bool m_isEnabled;
|
|
|
|
bool m_isException;
|
|
|
|
bool m_isInternalDisabled;
|
2012-06-25 16:07:25 +02:00
|
|
|
|
|
|
|
QStringList m_allowedDomains;
|
|
|
|
QStringList m_blockedDomains;
|
2013-11-02 17:41:51 +01:00
|
|
|
|
2014-03-12 13:25:12 +01:00
|
|
|
struct RegExp {
|
|
|
|
QzRegExp regExp;
|
2014-04-13 11:49:39 +02:00
|
|
|
QList<QStringMatcher> matchers;
|
2014-03-12 13:25:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Use dynamic allocation to save memory
|
|
|
|
RegExp* m_regExp;
|
|
|
|
|
2014-10-31 15:39:02 +01:00
|
|
|
friend class AdBlockMatcher;
|
2013-11-02 17:41:51 +01:00
|
|
|
friend class AdBlockSearchTree;
|
2014-09-16 09:38:11 +02:00
|
|
|
friend class AdBlockSubscription;
|
2011-03-27 21:59:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ADBLOCKRULE_H
|
|
|
|
|