Adblockkkkk.

This commit is contained in:
Martin Rotter 2015-06-17 21:12:49 +02:00
parent 5dbdc02172
commit 902d3f756c
4 changed files with 202 additions and 219 deletions

View File

@ -20,9 +20,11 @@
#define ADBLOCKMANAGER_H #define ADBLOCKMANAGER_H
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <QPointer> #include <QPointer>
class QUrl; class QUrl;
class QNetworkReply; class QNetworkReply;
class QNetworkRequest; class QNetworkRequest;
@ -33,15 +35,13 @@ class AdBlockMatcher;
class AdBlockCustomList; class AdBlockCustomList;
class AdBlockSubscription; class AdBlockSubscription;
class AdBlockManager : public QObject class AdBlockManager : public QObject {
{
Q_OBJECT Q_OBJECT
public: public:
AdBlockManager(QObject* parent = 0); // Constructors.
~AdBlockManager(); explicit AdBlockManager(QObject* parent = 0);
virtual ~AdBlockManager();
static AdBlockManager* instance();
void load(); void load();
void save(); void save();
@ -55,10 +55,10 @@ class AdBlockManager : public QObject
QString elementHidingRules() const; QString elementHidingRules() const;
QString elementHidingRulesForDomain(const QUrl &url) const; QString elementHidingRulesForDomain(const QUrl &url) const;
AdBlockSubscription* subscriptionByName(const QString &name) const; AdBlockSubscription *subscriptionByName(const QString &name) const;
QList<AdBlockSubscription*> subscriptions() const; QList<AdBlockSubscription*> subscriptions() const;
QNetworkReply* block(const QNetworkRequest &request); QNetworkReply *block(const QNetworkRequest &request);
QStringList disabledRules() const; QStringList disabledRules() const;
void addDisabledRule(const QString &filter); void addDisabledRule(const QString &filter);
@ -67,7 +67,9 @@ class AdBlockManager : public QObject
AdBlockSubscription *addSubscription(const QString &title, const QString &url); AdBlockSubscription *addSubscription(const QString &title, const QString &url);
bool removeSubscription(AdBlockSubscription* subscription); bool removeSubscription(AdBlockSubscription* subscription);
AdBlockCustomList* customList() const; AdBlockCustomList *customList() const;
static AdBlockManager *instance();
signals: signals:
void enabledChanged(bool enabled); void enabledChanged(bool enabled);
@ -80,7 +82,7 @@ class AdBlockManager : public QObject
AdBlockDialog *showDialog(); AdBlockDialog *showDialog();
private: private:
inline bool canBeBlocked(const QUrl &url) const; bool canBeBlocked(const QUrl &url) const;
bool m_loaded; bool m_loaded;
bool m_enabled; bool m_enabled;

View File

@ -1,131 +1,119 @@
/* ============================================================ // This file is part of RSS Guard.
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader //
* Copyright (C) 2011-2015 QuiteRSS Team <quiterssteam@gmail.com> // Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
* // Copyright (C) 2014 by David Rosca <nowrep@gmail.com>
* 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 // RSS Guard is free software: you can redistribute it and/or modify
* the Free Software Foundation, either version 3 of the License, or // it under the terms of the GNU General Public License as published by
* (at your option) any later version. // 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 // RSS Guard is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. // 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/>. // You should have received a copy of the GNU General Public License
* ============================================================ */ // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#include "adblockmatcher.h"
#include "adblockmanager.h"
#include "adblockrule.h"
#include "adblocksubscription.h"
AdBlockMatcher::AdBlockMatcher(AdBlockManager* manager) : QObject(manager), m_manager(manager) { #include "network-web/adblock/adblockmatcher.h"
#include "network-web/adblock/adblockmanager.h"
#include "network-web/adblock/adblockrule.h"
#include "network-web/adblock/adblocksubscription.h"
#include "definitions/definitions.h"
AdBlockMatcher::AdBlockMatcher(AdBlockManager *manager) : QObject(manager), m_manager(manager) {
connect(manager, SIGNAL(enabledChanged(bool)), this, SLOT(enabledChanged(bool))); connect(manager, SIGNAL(enabledChanged(bool)), this, SLOT(enabledChanged(bool)));
} }
AdBlockMatcher::~AdBlockMatcher() AdBlockMatcher::~AdBlockMatcher() {
{
clear(); clear();
} }
const AdBlockRule* AdBlockMatcher::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const const AdBlockRule *AdBlockMatcher::match(const QNetworkRequest &request, const QString &url_domain,
{ const QString &url_string) const {
// Exception rules // Exception rules.
if (m_networkExceptionTree.find(request, urlDomain, urlString)) if (m_networkExceptionTree.find(request, url_domain, url_string)) {
return NULL; return NULL;
int count = m_networkExceptionRules.count();
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_networkExceptionRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString))
return NULL;
} }
// Block rules for (int i = 0, count = m_networkExceptionRules.size(); i < count; i++) {
if (const AdBlockRule* rule = m_networkBlockTree.find(request, urlDomain, urlString)) const AdBlockRule *rule = m_networkExceptionRules.at(i);
return rule;
count = m_networkBlockRules.count(); if (rule->networkMatch(request, url_domain, url_string)) {
for (int i = 0; i < count; ++i) { return NULL;
const AdBlockRule* rule = m_networkBlockRules.at(i); }
if (rule->networkMatch(request, urlDomain, urlString)) }
// Block rules.
if (const AdBlockRule* rule = m_networkBlockTree.find(request, url_domain, url_string)) {
return rule;
}
for (int i = 0, count = m_networkBlockRules.size(); i < count; i++) {
const AdBlockRule *rule = m_networkBlockRules.at(i);
if (rule->networkMatch(request, url_domain, url_string)) {
return rule; return rule;
}
} }
return NULL; return NULL;
} }
bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const {
{ for (int i = 0, count = m_documentRules.size(); i < count; i++) {
int count = m_documentRules.count(); if (m_documentRules.at(i)->urlMatch(url)) {
for (int i = 0; i < count; ++i)
if (m_documentRules.at(i)->urlMatch(url))
return true; return true;
return false;
}
bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const
{
if (adBlockDisabledForUrl(url))
return true;
int count = m_elemhideRules.count();
for (int i = 0; i < count; ++i)
if (m_elemhideRules.at(i)->urlMatch(url))
return true;
return false;
}
QString AdBlockMatcher::elementHidingRules() const
{
return m_elementHidingRules;
}
QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
{
QString rules;
int addedRulesCount = 0;
int count = m_domainRestrictedCssRules.count();
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
if (!rule->matchDomain(domain))
continue;
if (addedRulesCount == 1000) {
rules.append(rule->cssSelector());
rules.append(QLatin1String("{display:none !important;}\n"));
addedRulesCount = 0;
}
else {
rules.append(rule->cssSelector() + QLatin1Char(','));
addedRulesCount++;
} }
} }
if (addedRulesCount != 0) { return false;
}
bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const {
if (adBlockDisabledForUrl(url)) {
return true;
}
for (int i = 0, count = m_elemhideRules.size(); i < count; i++) {
if (m_elemhideRules.at(i)->urlMatch(url)) {
return true;
}
}
return false;
}
QString AdBlockMatcher::elementHidingRules() const {
return m_elementHidingRules;
}
QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const {
QString rules;
int added_rules_count = 0;
for (int i = 0, count = m_domainRestrictedCssRules.size(); i < count; i++) {
const AdBlockRule *rule = m_domainRestrictedCssRules.at(i);
if (!rule->matchDomain(domain)) {
continue;
}
if (added_rules_count == 1000) {
rules.append(rule->cssSelector());
rules.append(QL1S("{display:none !important;}\n"));
added_rules_count = 0;
}
else {
rules.append(rule->cssSelector() + QL1C(','));
added_rules_count++;
}
}
if (added_rules_count != 0) {
rules = rules.left(rules.size() - 1); rules = rules.left(rules.size() - 1);
rules.append(QLatin1String("{display:none !important;}\n")); rules.append(QLatin1String("{display:none !important;}\n"));
} }
@ -133,29 +121,32 @@ QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
return rules; return rules;
} }
void AdBlockMatcher::update() void AdBlockMatcher::update() {
{
clear(); clear();
QHash<QString, const AdBlockRule*> cssRulesHash; QHash<QString, const AdBlockRule*> css_rules_hash;
QVector<const AdBlockRule*> exceptionCssRules; QVector<const AdBlockRule*> exception_css_rules;
foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) { foreach (AdBlockSubscription *subscription, m_manager->subscriptions()) {
foreach (const AdBlockRule* rule, subscription->allRules()) { foreach (const AdBlockRule *rule, subscription->allRules()) {
// Don't add internally disabled rules to cache // Don't add internally disabled rules to cache
if (rule->isInternalDisabled()) if (rule->isInternalDisabled()) {
continue; continue;
}
if (rule->isCssRule()) { if (rule->isCssRule()) {
// We will add only enabled css rules to cache, because there is no enabled/disabled // We will add only enabled css rules to cache, because there is no enabled/disabled
// check on match. They are directly embedded to pages. // check on match. They are directly embedded to pages.
if (!rule->isEnabled()) if (!rule->isEnabled()) {
continue; continue;
}
if (rule->isException()) if (rule->isException()) {
exceptionCssRules.append(rule); exception_css_rules.append(rule);
else }
cssRulesHash.insert(rule->cssSelector(), rule); else {
css_rules_hash.insert(rule->cssSelector(), rule);
}
} }
else if (rule->isDocument()) { else if (rule->isDocument()) {
m_documentRules.append(rule); m_documentRules.append(rule);
@ -164,77 +155,82 @@ void AdBlockMatcher::update()
m_elemhideRules.append(rule); m_elemhideRules.append(rule);
} }
else if (rule->isException()) { else if (rule->isException()) {
if (!m_networkExceptionTree.add(rule)) if (!m_networkExceptionTree.add(rule)) {
m_networkExceptionRules.append(rule); m_networkExceptionRules.append(rule);
}
} }
else { else {
if (!m_networkBlockTree.add(rule)) if (!m_networkBlockTree.add(rule)) {
m_networkBlockRules.append(rule); m_networkBlockRules.append(rule);
}
} }
} }
} }
foreach (const AdBlockRule* rule, exceptionCssRules) { foreach (const AdBlockRule *rule, exception_css_rules) {
const AdBlockRule* originalRule = cssRulesHash.value(rule->cssSelector()); const AdBlockRule *original_rule = css_rules_hash.value(rule->cssSelector());
// If we don't have this selector, the exception does nothing // If we don't have this selector, the exception does nothing.
if (!originalRule) if (original_rule == NULL) {
continue; continue;
}
AdBlockRule* copiedRule = originalRule->copy(); AdBlockRule *copied_rule = original_rule->copy();
copiedRule->m_options |= AdBlockRule::DomainRestrictedOption;
copiedRule->m_blockedDomains.append(rule->m_allowedDomains);
cssRulesHash[rule->cssSelector()] = copiedRule; copied_rule->m_options |= AdBlockRule::DomainRestrictedOption;
m_createdRules.append(copiedRule); copied_rule->m_blockedDomains.append(rule->m_allowedDomains);
css_rules_hash[rule->cssSelector()] = copied_rule;
m_createdRules.append(copied_rule);
} }
// Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes. // Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes.
// (In my testings, 4931 is the number that makes it crash) // (In my testings, 4931 is the number that makes it crash)
// So let's split it by 1000 selectors... // So let's split it by 1000 selectors.
int hidingRulesCount = 0; int hiding_rules_count = 0;
QHashIterator<QString,const AdBlockRule*> it(css_rules_hash);
QHashIterator<QString, const AdBlockRule*> it(cssRulesHash);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
const AdBlockRule* rule = it.value(); const AdBlockRule *rule = it.value();
if (rule->isDomainRestricted()) { if (rule->isDomainRestricted()) {
m_domainRestrictedCssRules.append(rule); m_domainRestrictedCssRules.append(rule);
} }
else if (hidingRulesCount == 1000) { else if (hiding_rules_count == 1000) {
m_elementHidingRules.append(rule->cssSelector()); m_elementHidingRules.append(rule->cssSelector());
m_elementHidingRules.append(QLatin1String("{display:none !important;} ")); m_elementHidingRules.append(QL1S("{display:none !important;} "));
hidingRulesCount = 0; hiding_rules_count = 0;
} }
else { else {
m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(',')); m_elementHidingRules.append(rule->cssSelector() + QL1C(','));
hidingRulesCount++; hiding_rules_count++;
} }
} }
if (hidingRulesCount != 0) { if (hiding_rules_count != 0) {
m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1); m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1);
m_elementHidingRules.append(QLatin1String("{display:none !important;} ")); m_elementHidingRules.append(QLatin1String("{display:none !important;} "));
} }
} }
void AdBlockMatcher::clear() void AdBlockMatcher::clear() {
{
m_networkExceptionTree.clear(); m_networkExceptionTree.clear();
m_networkExceptionRules.clear(); m_networkExceptionRules.clear();
m_networkBlockTree.clear(); m_networkBlockTree.clear();
m_networkBlockRules.clear(); m_networkBlockRules.clear();
m_domainRestrictedCssRules.clear(); m_domainRestrictedCssRules.clear();
m_elementHidingRules.clear(); m_elementHidingRules.clear();
m_documentRules.clear(); m_documentRules.clear();
m_elemhideRules.clear(); m_elemhideRules.clear();
qDeleteAll(m_createdRules); qDeleteAll(m_createdRules);
m_createdRules.clear(); m_createdRules.clear();
} }
void AdBlockMatcher::enabledChanged(bool enabled) void AdBlockMatcher::enabledChanged(bool enabled) {
{
if (enabled) { if (enabled) {
update(); update();
} }

View File

@ -1,85 +1,70 @@
/* ============================================================ // This file is part of RSS Guard.
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader //
* Copyright (C) 2011-2015 QuiteRSS Team <quiterssteam@gmail.com> // Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
* // Copyright (C) 2014 by David Rosca <nowrep@gmail.com>
* 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 // RSS Guard is free software: you can redistribute it and/or modify
* the Free Software Foundation, either version 3 of the License, or // it under the terms of the GNU General Public License as published by
* (at your option) any later version. // 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 // RSS Guard is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. // 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/>. // You should have received a copy of the GNU General Public License
* ============================================================ */ // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#ifndef ADBLOCKMATCHER_H #ifndef ADBLOCKMATCHER_H
#define ADBLOCKMATCHER_H #define ADBLOCKMATCHER_H
#include <QUrl>
#include <QObject> #include <QObject>
#include <QUrl>
#include <QVector> #include <QVector>
#include "adblocksearchtree.h" #include "network-web/adblock/adblocksearchtree.h"
class AdBlockManager; class AdBlockManager;
class AdBlockRule; class AdBlockRule;
class AdBlockMatcher : public QObject class AdBlockMatcher : public QObject {
{ Q_OBJECT
Q_OBJECT
public: public:
explicit AdBlockMatcher(AdBlockManager* manager); explicit AdBlockMatcher(AdBlockManager *manager);
~AdBlockMatcher(); virtual ~AdBlockMatcher();
const AdBlockRule* match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const; const AdBlockRule *match(const QNetworkRequest &request, const QString &url_domain, const QString &url_string) const;
bool adBlockDisabledForUrl(const QUrl &url) const; bool adBlockDisabledForUrl(const QUrl &url) const;
bool elemHideDisabledForUrl(const QUrl &url) const; bool elemHideDisabledForUrl(const QUrl &url) const;
QString elementHidingRules() const; QString elementHidingRules() const;
QString elementHidingRulesForDomain(const QString &domain) const; QString elementHidingRulesForDomain(const QString &domain) const;
public slots: public slots:
void update(); void update();
void clear(); void clear();
private slots: private slots:
void enabledChanged(bool enabled); void enabledChanged(bool enabled);
private: private:
AdBlockManager* m_manager; AdBlockManager* m_manager;
QVector<AdBlockRule*> m_createdRules; QVector<AdBlockRule*> m_createdRules;
QVector<const AdBlockRule*> m_networkExceptionRules; QVector<const AdBlockRule*> m_networkExceptionRules;
QVector<const AdBlockRule*> m_networkBlockRules; QVector<const AdBlockRule*> m_networkBlockRules;
QVector<const AdBlockRule*> m_domainRestrictedCssRules; QVector<const AdBlockRule*> m_domainRestrictedCssRules;
QVector<const AdBlockRule*> m_documentRules; QVector<const AdBlockRule*> m_documentRules;
QVector<const AdBlockRule*> m_elemhideRules; QVector<const AdBlockRule*> m_elemhideRules;
QString m_elementHidingRules; QString m_elementHidingRules;
AdBlockSearchTree m_networkBlockTree; AdBlockSearchTree m_networkBlockTree;
AdBlockSearchTree m_networkExceptionTree; AdBlockSearchTree m_networkExceptionTree;
}; };
#endif // ADBLOCKMATCHER_H #endif // ADBLOCKMATCHER_H

View File

@ -91,7 +91,7 @@ const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const
int len = urlString.size(); int len = urlString.size();
if (len <= 0) { if (len <= 0) {
return 0; return NULL;
} }
const QChar* string = urlString.constData(); const QChar* string = urlString.constData();
@ -103,7 +103,7 @@ const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const
} }
} }
return 0; return NULL;
} }
const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QChar* string, int len) const const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QChar* string, int len) const