From 902d3f756c98774c4ff584ec5b65e4752b968f4a Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Wed, 17 Jun 2015 21:12:49 +0200 Subject: [PATCH] Adblockkkkk. --- src/network-web/adblock/adblockmanager.h | 22 +- src/network-web/adblock/adblockmatcher.cpp | 284 +++++++++--------- src/network-web/adblock/adblockmatcher.h | 111 +++---- src/network-web/adblock/adblocksearchtree.cpp | 4 +- 4 files changed, 202 insertions(+), 219 deletions(-) diff --git a/src/network-web/adblock/adblockmanager.h b/src/network-web/adblock/adblockmanager.h index 10004d814..1f11f0814 100755 --- a/src/network-web/adblock/adblockmanager.h +++ b/src/network-web/adblock/adblockmanager.h @@ -20,9 +20,11 @@ #define ADBLOCKMANAGER_H #include + #include #include + 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 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; diff --git a/src/network-web/adblock/adblockmatcher.cpp b/src/network-web/adblock/adblockmatcher.cpp index 83cb9d9de..e566fe552 100755 --- a/src/network-web/adblock/adblockmatcher.cpp +++ b/src/network-web/adblock/adblockmatcher.cpp @@ -1,131 +1,119 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2014 David Rosca -* -* 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 . -* ============================================================ */ -#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 +// Copyright (C) 2014 by David Rosca +// +// 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 . -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)) +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; - - 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 - 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)) - 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.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 cssRulesHash; - QVector exceptionCssRules; + QHash css_rules_hash; + QVector 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 it(css_rules_hash); - QHashIterator 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(); } diff --git a/src/network-web/adblock/adblockmatcher.h b/src/network-web/adblock/adblockmatcher.h index 2524d9df5..e0bab02c7 100755 --- a/src/network-web/adblock/adblockmatcher.h +++ b/src/network-web/adblock/adblockmatcher.h @@ -1,85 +1,70 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2014 by David Rosca +// +// 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 . + #ifndef ADBLOCKMATCHER_H #define ADBLOCKMATCHER_H -#include #include + +#include #include -#include "adblocksearchtree.h" +#include "network-web/adblock/adblocksearchtree.h" + class AdBlockManager; class AdBlockRule; -class AdBlockMatcher : public QObject -{ - Q_OBJECT +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; + bool adBlockDisabledForUrl(const QUrl &url) const; + bool elemHideDisabledForUrl(const QUrl &url) const; - QString elementHidingRules() const; - QString elementHidingRulesForDomain(const QString &domain) const; + QString elementHidingRules() const; + QString elementHidingRulesForDomain(const QString &domain) const; -public slots: - void update(); - void clear(); + public slots: + void update(); + void clear(); -private slots: - void enabledChanged(bool enabled); + private slots: + void enabledChanged(bool enabled); -private: - AdBlockManager* m_manager; + private: + AdBlockManager* m_manager; - QVector m_createdRules; - QVector m_networkExceptionRules; - QVector m_networkBlockRules; - QVector m_domainRestrictedCssRules; - QVector m_documentRules; - QVector m_elemhideRules; + QVector m_createdRules; + QVector m_networkExceptionRules; + QVector m_networkBlockRules; + QVector m_domainRestrictedCssRules; + QVector m_documentRules; + QVector m_elemhideRules; - QString m_elementHidingRules; - AdBlockSearchTree m_networkBlockTree; - AdBlockSearchTree m_networkExceptionTree; + QString m_elementHidingRules; + AdBlockSearchTree m_networkBlockTree; + AdBlockSearchTree m_networkExceptionTree; }; #endif // ADBLOCKMATCHER_H diff --git a/src/network-web/adblock/adblocksearchtree.cpp b/src/network-web/adblock/adblocksearchtree.cpp index c0c6d0060..a8d694f99 100755 --- a/src/network-web/adblock/adblocksearchtree.cpp +++ b/src/network-web/adblock/adblocksearchtree.cpp @@ -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