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

View File

@ -1,131 +1,119 @@
/* ============================================================
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
* Copyright (C) 2011-2015 QuiteRSS Team <quiterssteam@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/>.
* ============================================================ */
/* ============================================================
* 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"
// This file is part of RSS Guard.
//
// Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard 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.
//
// RSS Guard 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 RSS Guard. If not, see <http://www.gnu.org/licenses/>.
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)));
}
AdBlockMatcher::~AdBlockMatcher()
{
AdBlockMatcher::~AdBlockMatcher() {
clear();
}
const AdBlockRule* AdBlockMatcher::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const
{
// Exception rules
if (m_networkExceptionTree.find(request, urlDomain, urlString))
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))
const AdBlockRule *AdBlockMatcher::match(const QNetworkRequest &request, const QString &url_domain,
const QString &url_string) const {
// Exception rules.
if (m_networkExceptionTree.find(request, url_domain, url_string)) {
return NULL;
}
// Block rules
if (const AdBlockRule* rule = m_networkBlockTree.find(request, urlDomain, urlString))
return rule;
for (int i = 0, count = m_networkExceptionRules.size(); i < count; i++) {
const AdBlockRule *rule = m_networkExceptionRules.at(i);
count = m_networkBlockRules.count();
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_networkBlockRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString))
if (rule->networkMatch(request, url_domain, url_string)) {
return NULL;
}
}
// 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 NULL;
}
bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const
{
int count = m_documentRules.count();
for (int i = 0; i < count; ++i)
if (m_documentRules.at(i)->urlMatch(url))
bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const {
for (int i = 0, count = m_documentRules.size(); i < count; i++) {
if (m_documentRules.at(i)->urlMatch(url)) {
return true;
}
}
return false;
}
bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const
{
if (adBlockDisabledForUrl(url))
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))
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
{
QString AdBlockMatcher::elementHidingRules() const {
return m_elementHidingRules;
}
QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
{
QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const {
QString rules;
int addedRulesCount = 0;
int count = m_domainRestrictedCssRules.count();
int added_rules_count = 0;
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
if (!rule->matchDomain(domain))
for (int i = 0, count = m_domainRestrictedCssRules.size(); i < count; i++) {
const AdBlockRule *rule = m_domainRestrictedCssRules.at(i);
if (!rule->matchDomain(domain)) {
continue;
}
if (addedRulesCount == 1000) {
if (added_rules_count == 1000) {
rules.append(rule->cssSelector());
rules.append(QLatin1String("{display:none !important;}\n"));
addedRulesCount = 0;
rules.append(QL1S("{display:none !important;}\n"));
added_rules_count = 0;
}
else {
rules.append(rule->cssSelector() + QLatin1Char(','));
addedRulesCount++;
rules.append(rule->cssSelector() + QL1C(','));
added_rules_count++;
}
}
if (addedRulesCount != 0) {
if (added_rules_count != 0) {
rules = rules.left(rules.size() - 1);
rules.append(QLatin1String("{display:none !important;}\n"));
}
@ -133,29 +121,32 @@ QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
return rules;
}
void AdBlockMatcher::update()
{
void AdBlockMatcher::update() {
clear();
QHash<QString, const AdBlockRule*> cssRulesHash;
QVector<const AdBlockRule*> exceptionCssRules;
QHash<QString, const AdBlockRule*> css_rules_hash;
QVector<const AdBlockRule*> exception_css_rules;
foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) {
foreach (const AdBlockRule* rule, subscription->allRules()) {
foreach (AdBlockSubscription *subscription, m_manager->subscriptions()) {
foreach (const AdBlockRule *rule, subscription->allRules()) {
// Don't add internally disabled rules to cache
if (rule->isInternalDisabled())
if (rule->isInternalDisabled()) {
continue;
}
if (rule->isCssRule()) {
// We will add only enabled css rules to cache, because there is no enabled/disabled
// check on match. They are directly embedded to pages.
if (!rule->isEnabled())
if (!rule->isEnabled()) {
continue;
}
if (rule->isException())
exceptionCssRules.append(rule);
else
cssRulesHash.insert(rule->cssSelector(), rule);
if (rule->isException()) {
exception_css_rules.append(rule);
}
else {
css_rules_hash.insert(rule->cssSelector(), rule);
}
}
else if (rule->isDocument()) {
m_documentRules.append(rule);
@ -164,77 +155,82 @@ void AdBlockMatcher::update()
m_elemhideRules.append(rule);
}
else if (rule->isException()) {
if (!m_networkExceptionTree.add(rule))
if (!m_networkExceptionTree.add(rule)) {
m_networkExceptionRules.append(rule);
}
}
else {
if (!m_networkBlockTree.add(rule))
if (!m_networkBlockTree.add(rule)) {
m_networkBlockRules.append(rule);
}
}
}
}
foreach (const AdBlockRule* rule, exceptionCssRules) {
const AdBlockRule* originalRule = cssRulesHash.value(rule->cssSelector());
foreach (const AdBlockRule *rule, exception_css_rules) {
const AdBlockRule *original_rule = css_rules_hash.value(rule->cssSelector());
// If we don't have this selector, the exception does nothing
if (!originalRule)
// If we don't have this selector, the exception does nothing.
if (original_rule == NULL) {
continue;
}
AdBlockRule* copiedRule = originalRule->copy();
copiedRule->m_options |= AdBlockRule::DomainRestrictedOption;
copiedRule->m_blockedDomains.append(rule->m_allowedDomains);
AdBlockRule *copied_rule = original_rule->copy();
cssRulesHash[rule->cssSelector()] = copiedRule;
m_createdRules.append(copiedRule);
copied_rule->m_options |= AdBlockRule::DomainRestrictedOption;
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.
// (In my testings, 4931 is the number that makes it crash)
// So let's split it by 1000 selectors...
int hidingRulesCount = 0;
// So let's split it by 1000 selectors.
int hiding_rules_count = 0;
QHashIterator<QString,const AdBlockRule*> it(css_rules_hash);
QHashIterator<QString, const AdBlockRule*> it(cssRulesHash);
while (it.hasNext()) {
it.next();
const AdBlockRule* rule = it.value();
const AdBlockRule *rule = it.value();
if (rule->isDomainRestricted()) {
m_domainRestrictedCssRules.append(rule);
}
else if (hidingRulesCount == 1000) {
else if (hiding_rules_count == 1000) {
m_elementHidingRules.append(rule->cssSelector());
m_elementHidingRules.append(QLatin1String("{display:none !important;} "));
hidingRulesCount = 0;
m_elementHidingRules.append(QL1S("{display:none !important;} "));
hiding_rules_count = 0;
}
else {
m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(','));
hidingRulesCount++;
m_elementHidingRules.append(rule->cssSelector() + QL1C(','));
hiding_rules_count++;
}
}
if (hidingRulesCount != 0) {
if (hiding_rules_count != 0) {
m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1);
m_elementHidingRules.append(QLatin1String("{display:none !important;} "));
}
}
void AdBlockMatcher::clear()
{
void AdBlockMatcher::clear() {
m_networkExceptionTree.clear();
m_networkExceptionRules.clear();
m_networkBlockTree.clear();
m_networkBlockRules.clear();
m_domainRestrictedCssRules.clear();
m_elementHidingRules.clear();
m_documentRules.clear();
m_elemhideRules.clear();
qDeleteAll(m_createdRules);
m_createdRules.clear();
}
void AdBlockMatcher::enabledChanged(bool enabled)
{
void AdBlockMatcher::enabledChanged(bool enabled) {
if (enabled) {
update();
}

View File

@ -1,58 +1,43 @@
/* ============================================================
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
* Copyright (C) 2011-2015 QuiteRSS Team <quiterssteam@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/>.
* ============================================================ */
/* ============================================================
* 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/>.
* ============================================================ */
// This file is part of RSS Guard.
//
// Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard 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.
//
// RSS Guard 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 RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef ADBLOCKMATCHER_H
#define ADBLOCKMATCHER_H
#include <QUrl>
#include <QObject>
#include <QUrl>
#include <QVector>
#include "adblocksearchtree.h"
#include "network-web/adblock/adblocksearchtree.h"
class AdBlockManager;
class AdBlockRule;
class AdBlockMatcher : public QObject
{
class AdBlockMatcher : public QObject {
Q_OBJECT
public:
explicit AdBlockMatcher(AdBlockManager* manager);
~AdBlockMatcher();
public:
explicit AdBlockMatcher(AdBlockManager *manager);
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 elemHideDisabledForUrl(const QUrl &url) const;
@ -60,14 +45,14 @@ public:
QString elementHidingRules() const;
QString elementHidingRulesForDomain(const QString &domain) const;
public slots:
public slots:
void update();
void clear();
private slots:
private slots:
void enabledChanged(bool enabled);
private:
private:
AdBlockManager* m_manager;
QVector<AdBlockRule*> m_createdRules;

View File

@ -91,7 +91,7 @@ const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const
int len = urlString.size();
if (len <= 0) {
return 0;
return NULL;
}
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