1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Bring back AdBlock blocking network requests

This commit is contained in:
David Rosca 2015-10-05 20:16:51 +02:00
parent 40eb1281f8
commit e91d6482d1
13 changed files with 155 additions and 252 deletions

View File

@ -1,82 +0,0 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-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/>.
* ============================================================ */
/**
* 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.
*/
#include "adblockblockednetworkreply.h"
#include "adblocksubscription.h"
#include "adblockrule.h"
#include <QNetworkRequest>
#include <QTimer>
AdBlockBlockedNetworkReply::AdBlockBlockedNetworkReply(const AdBlockRule* rule, QObject* parent)
: QNetworkReply(parent)
{
setOperation(QNetworkAccessManager::GetOperation);
setError(QNetworkReply::ContentAccessDenied, QString("AdBlock: %1 (%2)").arg(rule->subscription()->title(), rule->filter()));
open(QIODevice::ReadOnly);
QTimer::singleShot(0, this, SLOT(delayedFinished()));
}
void AdBlockBlockedNetworkReply::setRequest(const QNetworkRequest &request)
{
QNetworkReply::setRequest(request);
setUrl(request.url());
}
qint64 AdBlockBlockedNetworkReply::readData(char* data, qint64 maxSize)
{
Q_UNUSED(data);
Q_UNUSED(maxSize);
return -1;
}
void AdBlockBlockedNetworkReply::delayedFinished()
{
emit error(QNetworkReply::ContentAccessDenied);
emit finished();
}

View File

@ -1,75 +0,0 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-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/>.
* ============================================================ */
/**
* 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 ADBLOCKBLOCKEDNETWORKREPLY_H
#define ADBLOCKBLOCKEDNETWORKREPLY_H
#include <QNetworkReply>
#include "qzcommon.h"
class AdBlockRule;
class AdBlockSubscription;
class QUPZILLA_EXPORT AdBlockBlockedNetworkReply : public QNetworkReply
{
Q_OBJECT
public:
AdBlockBlockedNetworkReply(const AdBlockRule* rule, QObject* parent = 0);
void abort() {}
void setRequest(const QNetworkRequest &request);
protected:
qint64 readData(char* data, qint64 maxSize);
private slots:
void delayedFinished();
};
#endif // ADBLOCKBLOCKEDNETWORKREPLY_H

View File

@ -19,13 +19,14 @@
#include "adblockdialog.h" #include "adblockdialog.h"
#include "adblockmatcher.h" #include "adblockmatcher.h"
#include "adblocksubscription.h" #include "adblocksubscription.h"
#include "adblockblockednetworkreply.h" #include "adblockurlinterceptor.h"
#include "datapaths.h" #include "datapaths.h"
#include "mainapplication.h" #include "mainapplication.h"
#include "webpage.h" #include "webpage.h"
#include "qztools.h" #include "qztools.h"
#include "browserwindow.h" #include "browserwindow.h"
#include "settings.h" #include "settings.h"
#include "networkmanager.h"
#include <QDateTime> #include <QDateTime>
#include <QTextStream> #include <QTextStream>
@ -46,6 +47,7 @@ AdBlockManager::AdBlockManager(QObject* parent)
, m_enabled(true) , m_enabled(true)
, m_useLimitedEasyList(true) , m_useLimitedEasyList(true)
, m_matcher(new AdBlockMatcher(this)) , m_matcher(new AdBlockMatcher(this))
, m_interceptor(new AdBlockUrlInterceptor(this))
{ {
load(); load();
} }
@ -83,7 +85,7 @@ QList<AdBlockSubscription*> AdBlockManager::subscriptions() const
return m_subscriptions; return m_subscriptions;
} }
QNetworkReply* AdBlockManager::block(const QNetworkRequest &request) bool AdBlockManager::block(QWebEngineUrlRequestInfo &request)
{ {
#ifdef ADBLOCK_DEBUG #ifdef ADBLOCK_DEBUG
QElapsedTimer timer; QElapsedTimer timer;
@ -96,34 +98,22 @@ QNetworkReply* AdBlockManager::block(const QNetworkRequest &request)
if (!isEnabled() || !canRunOnScheme(urlScheme)) if (!isEnabled() || !canRunOnScheme(urlScheme))
return 0; return 0;
bool res = false;
const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString); const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString);
if (blockedRule) { if (blockedRule) {
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100)); res = true;
WebPage* webPage = static_cast<WebPage*>(v.value<void*>()); request.blockRequest(true);
if (webPage) {
if (!canBeBlocked(webPage->url())) {
return 0;
}
webPage->addAdBlockRule(blockedRule, request.url());
}
AdBlockBlockedNetworkReply* reply = new AdBlockBlockedNetworkReply(blockedRule, this);
reply->setRequest(request);
#ifdef ADBLOCK_DEBUG #ifdef ADBLOCK_DEBUG
qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.url(); qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.url();
#endif #endif
return reply;
} }
#ifdef ADBLOCK_DEBUG #ifdef ADBLOCK_DEBUG
qDebug() << timer.elapsed() << request.url(); qDebug() << timer.elapsed() << request.url();
#endif #endif
return 0; return res;
} }
QStringList AdBlockManager::disabledRules() const QStringList AdBlockManager::disabledRules() const
@ -217,6 +207,7 @@ void AdBlockManager::load()
settings.endGroup(); settings.endGroup();
if (!m_enabled) { if (!m_enabled) {
mApp->networkManager()->removeUrlInterceptor(m_interceptor);
return; return;
} }
@ -285,6 +276,8 @@ void AdBlockManager::load()
m_matcher->update(); m_matcher->update();
m_loaded = true; m_loaded = true;
mApp->networkManager()->installUrlInterceptor(m_interceptor);
} }
void AdBlockManager::updateAllSubscriptions() void AdBlockManager::updateAllSubscriptions()

View File

@ -25,14 +25,14 @@
#include "qzcommon.h" #include "qzcommon.h"
class QUrl; class QUrl;
class QNetworkReply; class QWebEngineUrlRequestInfo;
class QNetworkRequest;
class AdBlockRule; class AdBlockRule;
class AdBlockDialog; class AdBlockDialog;
class AdBlockMatcher; class AdBlockMatcher;
class AdBlockCustomList; class AdBlockCustomList;
class AdBlockSubscription; class AdBlockSubscription;
class AdBlockUrlInterceptor;
class QUPZILLA_EXPORT AdBlockManager : public QObject class QUPZILLA_EXPORT AdBlockManager : public QObject
{ {
@ -57,7 +57,7 @@ public:
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); bool block(QWebEngineUrlRequestInfo &request);
QStringList disabledRules() const; QStringList disabledRules() const;
void addDisabledRule(const QString &filter); void addDisabledRule(const QString &filter);
@ -92,6 +92,7 @@ private:
AdBlockMatcher* m_matcher; AdBlockMatcher* m_matcher;
QStringList m_disabledRules; QStringList m_disabledRules;
AdBlockUrlInterceptor *m_interceptor;
QPointer<AdBlockDialog> m_adBlockDialog; QPointer<AdBlockDialog> m_adBlockDialog;
}; };

View File

@ -32,7 +32,7 @@ AdBlockMatcher::~AdBlockMatcher()
clear(); clear();
} }
const AdBlockRule* AdBlockMatcher::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const const AdBlockRule* AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const
{ {
// Exception rules // Exception rules
if (m_networkExceptionTree.find(request, urlDomain, urlString)) if (m_networkExceptionTree.find(request, urlDomain, urlString))

View File

@ -24,6 +24,8 @@
#include "qzcommon.h" #include "qzcommon.h"
#include "adblocksearchtree.h" #include "adblocksearchtree.h"
class QWebEngineUrlRequestInfo;
class AdBlockManager; class AdBlockManager;
class QUPZILLA_EXPORT AdBlockMatcher : public QObject class QUPZILLA_EXPORT AdBlockMatcher : public QObject
@ -34,7 +36,7 @@ public:
explicit AdBlockMatcher(AdBlockManager* manager); explicit AdBlockMatcher(AdBlockManager* manager);
~AdBlockMatcher(); ~AdBlockMatcher();
const AdBlockRule* match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const; const AdBlockRule* match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) 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;

View File

@ -52,11 +52,10 @@
#include <QUrl> #include <QUrl>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QNetworkRequest>
#include <QWebEnginePage> #include <QWebEnginePage>
#include <QWebEngineUrlRequestInfo>
// Version for Qt < 4.8 has one issue, it will wrongly #if QTWEBENGINE_DISABLED
// count .co.uk (and others) as second-level domain
static QString toSecondLevelDomain(const QUrl &url) static QString toSecondLevelDomain(const QUrl &url)
{ {
const QString topLevelDomain = url.topLevelDomain(); const QString topLevelDomain = url.topLevelDomain();
@ -78,6 +77,7 @@ static QString toSecondLevelDomain(const QUrl &url)
return domain + topLevelDomain; return domain + topLevelDomain;
} }
#endif
AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscription) AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscription)
: m_subscription(subscription) : m_subscription(subscription)
@ -203,36 +203,20 @@ bool AdBlockRule::urlMatch(const QUrl &url) const
return false; return false;
} }
const QString encodedUrl = url.toEncoded(); const QString encodedUrl = url.toEncoded();
const QString domain = url.host(); const QString domain = url.host();
return networkMatch(QNetworkRequest(url), domain, encodedUrl); return stringMatch(domain, encodedUrl);
} }
bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &domain, const QString &encodedUrl) const bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const
{ {
if (m_type == CssRule || !m_isEnabled || m_isInternalDisabled) { if (m_type == CssRule || !m_isEnabled || m_isInternalDisabled) {
return false; return false;
} }
bool matched = false; bool matched = stringMatch(domain, encodedUrl);
if (m_type == StringContainsMatchRule) {
matched = encodedUrl.contains(m_matchString, m_caseSensitivity);
}
else if (m_type == DomainMatchRule) {
matched = isMatchingDomain(domain, m_matchString);
}
else if (m_type == StringEndsMatchRule) {
matched = encodedUrl.endsWith(m_matchString, m_caseSensitivity);
}
else if (m_type == RegExpMatchRule) {
if (!isMatchingRegExpStrings(encodedUrl)) {
return false;
}
matched = (m_regExp->regExp.indexIn(encodedUrl) != -1);
}
if (matched) { if (matched) {
// Check domain restrictions // Check domain restrictions
@ -261,7 +245,7 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do
} }
// Check image restriction // Check image restriction
if (hasOption(ImageOption) && !matchImage(encodedUrl)) { if (hasOption(ImageOption) && !matchImage(request)) {
return false; return false;
} }
} }
@ -311,8 +295,9 @@ bool AdBlockRule::matchDomain(const QString &domain) const
return false; return false;
} }
bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const bool AdBlockRule::matchThirdParty(const QWebEngineUrlRequestInfo &request) const
{ {
#if QTWEBENGINE_DISABLED
const QString referer = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()).toString(); const QString referer = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()).toString();
if (referer.isEmpty()) { if (referer.isEmpty()) {
@ -326,50 +311,36 @@ bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const
bool match = refererHost != host; bool match = refererHost != host;
return hasException(ThirdPartyOption) ? !match : match; return hasException(ThirdPartyOption) ? !match : match;
}
bool AdBlockRule::matchObject(const QNetworkRequest &request) const
{
bool match = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 150)).toString() == QL1S("object");
return hasException(ObjectOption) ? !match : match;
}
bool AdBlockRule::matchSubdocument(const QNetworkRequest &request) const
{
Q_UNUSED(request)
#if QTWEBENGINE_DISABLED
QWebEngineFrame* originatingFrame = static_cast<QWebEngineFrame*>(request.originatingObject());
if (!originatingFrame) {
return false;
}
QWebEnginePage* page = originatingFrame->page();
if (!page) {
return false;
}
bool match = !(originatingFrame == page->mainFrame());
return hasException(SubdocumentOption) ? !match : match;
#else #else
Q_UNUSED(request)
return false; return false;
#endif #endif
} }
bool AdBlockRule::matchXmlHttpRequest(const QNetworkRequest &request) const bool AdBlockRule::matchObject(const QWebEngineUrlRequestInfo &request) const
{ {
bool match = request.rawHeader("X-Requested-With") == QByteArray("XMLHttpRequest"); bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeObject;
return hasException(ObjectOption) ? !match : match;
}
bool AdBlockRule::matchSubdocument(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSubFrame;
return hasException(SubdocumentOption) ? !match : match;
}
bool AdBlockRule::matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeXhr;
return hasException(XMLHttpRequestOption) ? !match : match; return hasException(XMLHttpRequestOption) ? !match : match;
} }
bool AdBlockRule::matchImage(const QString &encodedUrl) const bool AdBlockRule::matchImage(const QWebEngineUrlRequestInfo &request) const
{ {
bool match = encodedUrl.endsWith(QL1S(".png")) || bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage;
encodedUrl.endsWith(QL1S(".jpg")) ||
encodedUrl.endsWith(QL1S(".gif")) ||
encodedUrl.endsWith(QL1S(".jpeg"));
return hasException(ImageOption) ? !match : match; return hasException(ImageOption) ? !match : match;
} }
@ -660,6 +631,27 @@ QList<QStringMatcher> AdBlockRule::createStringMatchers(const QStringList &filte
return matchers; return matchers;
} }
bool AdBlockRule::stringMatch(const QString &domain, const QString &encodedUrl) const
{
if (m_type == StringContainsMatchRule) {
return encodedUrl.contains(m_matchString, m_caseSensitivity);
}
else if (m_type == DomainMatchRule) {
return isMatchingDomain(domain, m_matchString);
}
else if (m_type == StringEndsMatchRule) {
return encodedUrl.endsWith(m_matchString, m_caseSensitivity);
}
else if (m_type == RegExpMatchRule) {
if (!isMatchingRegExpStrings(encodedUrl)) {
return false;
}
return (m_regExp->regExp.indexIn(encodedUrl) != -1);
}
return false;
}
bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &filter) const bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &filter) const
{ {
return QzTools::matchDomain(filter, domain); return QzTools::matchDomain(filter, domain);

View File

@ -53,8 +53,8 @@
#include "qzcommon.h" #include "qzcommon.h"
#include "qzregexp.h" #include "qzregexp.h"
class QNetworkRequest;
class QUrl; class QUrl;
class QWebEngineUrlRequestInfo;
class AdBlockSubscription; class AdBlockSubscription;
@ -91,16 +91,17 @@ public:
bool isInternalDisabled() const; bool isInternalDisabled() const;
bool urlMatch(const QUrl &url) const; bool urlMatch(const QUrl &url) const;
bool networkMatch(const QNetworkRequest &request, const QString &domain, const QString &encodedUrl) const; bool networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const;
bool matchDomain(const QString &domain) const; bool matchDomain(const QString &domain) const;
bool matchThirdParty(const QNetworkRequest &request) const; bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const;
bool matchObject(const QNetworkRequest &request) const; bool matchObject(const QWebEngineUrlRequestInfo &request) const;
bool matchSubdocument(const QNetworkRequest &request) const; bool matchSubdocument(const QWebEngineUrlRequestInfo &request) const;
bool matchXmlHttpRequest(const QNetworkRequest &request) const; bool matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const;
bool matchImage(const QString &encodedUrl) const; bool matchImage(const QWebEngineUrlRequestInfo &request) const;
protected: protected:
bool stringMatch(const QString &domain, const QString &encodedUrl) const;
bool isMatchingDomain(const QString &domain, const QString &filter) const; bool isMatchingDomain(const QString &domain, const QString &filter) const;
bool isMatchingRegExpStrings(const QString &url) const; bool isMatchingRegExpStrings(const QString &url) const;
QStringList parseRegExpFilter(const QString &filter) const; QStringList parseRegExpFilter(const QString &filter) const;

View File

@ -18,6 +18,8 @@
#include "adblocksearchtree.h" #include "adblocksearchtree.h"
#include "adblockrule.h" #include "adblockrule.h"
#include <QWebEngineUrlRequestInfo>
AdBlockSearchTree::AdBlockSearchTree() AdBlockSearchTree::AdBlockSearchTree()
: m_root(new Node) : m_root(new Node)
{ {
@ -67,7 +69,7 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule)
return true; return true;
} }
const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const QString &domain, const QString &urlString) const const AdBlockRule* AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const
{ {
int len = urlString.size(); int len = urlString.size();
@ -87,7 +89,7 @@ const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const
return 0; return 0;
} }
const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QChar* string, int len) const const AdBlockRule* AdBlockSearchTree::prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString, const QChar* string, int len) const
{ {
if (len <= 0) { if (len <= 0) {
return 0; return 0;

View File

@ -23,7 +23,7 @@
#include "qzcommon.h" #include "qzcommon.h"
class QNetworkRequest; class QWebEngineUrlRequestInfo;
class AdBlockRule; class AdBlockRule;
@ -36,7 +36,7 @@ public:
void clear(); void clear();
bool add(const AdBlockRule* rule); bool add(const AdBlockRule* rule);
const AdBlockRule* find(const QNetworkRequest &request, const QString &domain, const QString &urlString) const; const AdBlockRule* find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const;
private: private:
struct Node { struct Node {
@ -47,7 +47,7 @@ private:
Node() : c(0) , rule(0) { } Node() : c(0) , rule(0) { }
}; };
const AdBlockRule* prefixSearch(const QNetworkRequest &request, const QString &domain, const AdBlockRule* prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain,
const QString &urlString, const QChar* string, int len) const; const QString &urlString, const QChar* string, int len) const;
void deleteNode(Node* node); void deleteNode(Node* node);

View File

@ -0,0 +1,31 @@
/* ============================================================
* QupZilla - QtWebEngine based browser
* Copyright (C) 2015 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 "adblockurlinterceptor.h"
#include "adblockmanager.h"
AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager)
: UrlInterceptor(manager)
, m_manager(manager)
{
}
bool AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
return m_manager->block(info);
}

View File

@ -0,0 +1,38 @@
/* ============================================================
* QupZilla - QtWebEngine based browser
* Copyright (C) 2015 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 ADBLOCKURLINTERCEPTOR_H
#define ADBLOCKURLINTERCEPTOR_H
#include "urlinterceptor.h"
#include "qzcommon.h"
class AdBlockManager;
class QUPZILLA_EXPORT AdBlockUrlInterceptor : public UrlInterceptor
{
public:
explicit AdBlockUrlInterceptor(AdBlockManager* manager);
bool interceptRequest(QWebEngineUrlRequestInfo &info);
private:
AdBlockManager *m_manager;
};
#endif // ADBLOCKURLINTERCEPTOR_H

View File

@ -54,7 +54,7 @@ SOURCES += \
3rdparty/squeezelabelv2.cpp \ 3rdparty/squeezelabelv2.cpp \
3rdparty/stylehelper.cpp \ 3rdparty/stylehelper.cpp \
adblock/adblockaddsubscriptiondialog.cpp \ adblock/adblockaddsubscriptiondialog.cpp \
adblock/adblockblockednetworkreply.cpp \ adblock/adblockurlinterceptor.cpp \
adblock/adblockdialog.cpp \ adblock/adblockdialog.cpp \
adblock/adblockicon.cpp \ adblock/adblockicon.cpp \
adblock/adblockmanager.cpp \ adblock/adblockmanager.cpp \
@ -248,7 +248,7 @@ HEADERS += \
3rdparty/squeezelabelv2.h \ 3rdparty/squeezelabelv2.h \
3rdparty/stylehelper.h \ 3rdparty/stylehelper.h \
adblock/adblockaddsubscriptiondialog.h \ adblock/adblockaddsubscriptiondialog.h \
adblock/adblockblockednetworkreply.h \ adblock/adblockurlinterceptor.h \
adblock/adblockdialog.h \ adblock/adblockdialog.h \
adblock/adblockicon.h \ adblock/adblockicon.h \
adblock/adblockmanager.h \ adblock/adblockmanager.h \