summaryrefslogtreecommitdiff
path: root/src/adblock/adblockrulefallbackimpl.cpp
diff options
context:
space:
mode:
authorBenjamin Poulain <benjamin.poulain@nokia.com>2010-08-21 21:40:59 +0200
committerBenjamin Poulain <benjamin.poulain@nokia.com>2010-08-22 00:50:13 +0200
commit12f8323ae8e5e469fb0b662f29bd8fedadb85faa (patch)
tree61a394d0911239e02fbd495cd171e8594fd833b8 /src/adblock/adblockrulefallbackimpl.cpp
parentRestore bk icon on the right (diff)
downloadrekonq-12f8323ae8e5e469fb0b662f29bd8fedadb85faa.tar.xz
Add support for domain option in the ad block module
Some ads were not filtered because they were incorrectly matched by the whitelist. This is because we ignore options, including the domain restrictions. For example, the white filter: @@||pagead2.googlesyndication.com/pagead/show_ads.js$domain=kde.org would match any page regardless of the domain restriction. So no ads from pagead2.googlesyndication.com were filtered. This patch adds support for "domain" options in the fallback rules (the other AdBlockRuleImpl rejects options). The domain of the frame making the request is now compared to the option to take the right decision. This patch requires Qt 4.7 with a recent QtWebKit 2.0.
Diffstat (limited to 'src/adblock/adblockrulefallbackimpl.cpp')
-rw-r--r--src/adblock/adblockrulefallbackimpl.cpp42
1 files changed, 39 insertions, 3 deletions
diff --git a/src/adblock/adblockrulefallbackimpl.cpp b/src/adblock/adblockrulefallbackimpl.cpp
index 988f2895..959050da 100644
--- a/src/adblock/adblockrulefallbackimpl.cpp
+++ b/src/adblock/adblockrulefallbackimpl.cpp
@@ -30,8 +30,11 @@
#include "rekonq_defines.h"
// Qt Includes
+#include <QWebFrame>
+#include <QNetworkReply>
#include <QStringList>
+
static inline bool isRegExpFilter(const QString &filter)
{
return filter.startsWith(QL1C('/')) && filter.endsWith(QL1C('/'));
@@ -48,9 +51,24 @@ AdBlockRuleFallbackImpl::AdBlockRuleFallbackImpl(const QString &filter)
const int optionsNumber = parsedLine.lastIndexOf(QL1C('$'));
if (optionsNumber >= 0 && !isRegExpFilter(parsedLine)) {
const QStringList options(parsedLine.mid(optionsNumber + 1).split(QL1C(',')));
+ parsedLine = parsedLine.left(optionsNumber);
+
if (options.contains(QL1S("match-case")))
m_regExp.setCaseSensitivity(Qt::CaseSensitive);
- parsedLine = parsedLine.left(optionsNumber);
+
+ foreach (const QString &option, options) {
+ // Domain restricted filter
+ const QString domainKeyword(QL1S("domain="));
+ if (option.startsWith(domainKeyword)) {
+ QStringList domainList = option.mid(domainKeyword.length()).split(QL1C('|'));
+ foreach (const QString &domain, domainList) {
+ if (domain.startsWith(QL1C('~')))
+ m_whiteDomains.insert(domain.toLower());
+ else
+ m_blackDomains.insert(domain.toLower());
+ }
+ }
+ }
}
if (isRegExpFilter(parsedLine))
@@ -61,9 +79,27 @@ AdBlockRuleFallbackImpl::AdBlockRuleFallbackImpl(const QString &filter)
m_regExp.setPattern(parsedLine);
}
-bool AdBlockRuleFallbackImpl::match(const QString &encodedUrl, const QString &) const
+bool AdBlockRuleFallbackImpl::match(const QNetworkRequest &request, const QString &encodedUrl, const QString &) const
{
- return m_regExp.indexIn(encodedUrl) != -1;
+ const bool regexpMatch = m_regExp.indexIn(encodedUrl) != -1;
+
+ if (regexpMatch && (!m_whiteDomains.isEmpty() || !m_blackDomains.isEmpty())) {
+ Q_ASSERT(qobject_cast<QWebFrame*>(request.originatingObject()));
+ const QWebFrame *const origin = static_cast<QWebFrame *const>(request.originatingObject());
+
+ const QString originDomain = origin->url().host();
+
+ if (!m_whiteDomains.isEmpty()) {
+ // In this context, white domains means we block anything but what is in the list.
+ if (m_whiteDomains.contains(originDomain))
+ return false;
+ return true;
+ } else if (m_blackDomains.contains(originDomain)) {
+ return true;
+ }
+ return false;
+ }
+ return regexpMatch;
}
QString AdBlockRuleFallbackImpl::convertPatternToRegExp(const QString &wildcardPattern)