Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

port the original hashing rule selector, to the new API

  • Loading branch information...
commit d8aeb29875c9a2687b26fae1e19e0ae91898ca04 1 parent 37b78db
Zsombor authored
6 src/adblock/adblock.pri
@@ -10,7 +10,8 @@ HEADERS += \
10 10 adblockpage.h \
11 11 adblockrule.h \
12 12 adblockschemeaccesshandler.h \
13   - adblocksubscription.h
  13 + adblocksubscription.h \
  14 + ruleselector.h
14 15
15 16 SOURCES += \
16 17 adblockblockednetworkreply.cpp \
@@ -21,7 +22,8 @@ SOURCES += \
21 22 adblockpage.cpp \
22 23 adblockrule.cpp \
23 24 adblockschemeaccesshandler.cpp \
24   - adblocksubscription.cpp
  25 + adblocksubscription.cpp \
  26 + ruleselector.cpp
25 27
26 28 FORMS += \
27 29 adblockdialog.ui
6 src/adblock/adblockrule.cpp
@@ -192,7 +192,13 @@ static QString convertPatternToRegExp(const QString &wildcardPattern) {
192 192
193 193 void AdBlockRule::setPattern(const QString &pattern, bool isRegExp)
194 194 {
  195 + m_pattern = pattern;
  196 + m_regexpRule = isRegExp;
195 197 m_regExp = QRegExp(isRegExp ? pattern : convertPatternToRegExp(pattern),
196 198 Qt::CaseInsensitive, QRegExp::RegExp2);
197 199 }
198 200
  201 +QString AdBlockRule::pattern() const
  202 +{
  203 + return m_pattern;
  204 +}
5 src/adblock/adblockrule.h
@@ -33,6 +33,7 @@
33 33
34 34 class QUrl;
35 35 class QRegExp;
  36 +
36 37 class AdBlockRule
37 38 {
38 39
@@ -51,15 +52,19 @@ class AdBlockRule
51 52 bool isEnabled() const;
52 53 void setEnabled(bool enabled);
53 54
  55 + QString pattern() const;
54 56 QString regExpPattern() const;
  57 + bool isRegexp() const { return m_regexpRule; }
55 58 void setPattern(const QString &pattern, bool isRegExp);
56 59
57 60 private:
58 61 QString m_filter;
  62 + QString m_pattern;
59 63
60 64 bool m_cssRule;
61 65 bool m_exception;
62 66 bool m_enabled;
  67 + bool m_regexpRule;
63 68 QRegExp m_regExp;
64 69 QStringList m_options;
65 70 };
11 src/adblock/adblocksubscription.cpp
@@ -38,6 +38,7 @@
38 38 #include <qtextstream.h>
39 39
40 40 // #define ADBLOCKSUBSCRIPTION_DEBUG
  41 +// #define ADBLOCK_LINEAR_SEARCH
41 42
42 43 AdBlockSubscription::AdBlockSubscription(const QUrl &url, QObject *parent)
43 44 : QObject(parent)
@@ -295,20 +296,28 @@ QList<const AdBlockRule*> AdBlockSubscription::pageRules() const
295 296
296 297 const AdBlockRule *AdBlockSubscription::allow(const QString &urlString) const
297 298 {
  299 +#if defined(ADBLOCK_LINEAR_SEARCH)
298 300 foreach (const AdBlockRule *rule, m_networkExceptionRules) {
299 301 if (rule->networkMatch(urlString))
300 302 return rule;
301 303 }
302 304 return 0;
  305 +#else
  306 + return m_networkExceptionRuleSelector.get(urlString);
  307 +#endif
303 308 }
304 309
305 310 const AdBlockRule *AdBlockSubscription::block(const QString &urlString) const
306 311 {
  312 +#if defined(ADBLOCK_LINEAR_SEARCH)
307 313 foreach (const AdBlockRule *rule, m_networkBlockRules) {
308 314 if (rule->networkMatch(urlString))
309 315 return rule;
310 316 }
311 317 return 0;
  318 +#else
  319 + return m_networkBlockRuleSelector.get(urlString);
  320 +#endif
312 321 }
313 322
314 323 QList<AdBlockRule> AdBlockSubscription::allRules() const
@@ -371,5 +380,7 @@ void AdBlockSubscription::populateCache()
371 380 m_networkBlockRules.append(rule);
372 381 }
373 382 }
  383 + m_networkExceptionRuleSelector.rehash(m_networkExceptionRules, true);
  384 + m_networkBlockRuleSelector.rehash(m_networkBlockRules, false);
374 385 }
375 386
4 src/adblock/adblocksubscription.h
@@ -32,6 +32,7 @@
32 32 #include <qobject.h>
33 33
34 34 #include "adblockrule.h"
  35 +#include "ruleselector.h"
35 36
36 37 #include <qlist.h>
37 38 #include <qdatetime.h>
@@ -96,6 +97,9 @@ private slots:
96 97 QList<const AdBlockRule*> m_networkExceptionRules;
97 98 QList<const AdBlockRule*> m_networkBlockRules;
98 99 QList<const AdBlockRule*> m_pageRules;
  100 +
  101 + RuleSelector m_networkBlockRuleSelector;
  102 + RuleSelector m_networkExceptionRuleSelector;
99 103 };
100 104
101 105 #endif // ADBLOCKSUBSCRIPTION_H
123 src/adblock/ruleselector.cpp
... ... @@ -0,0 +1,123 @@
  1 +/**
  2 + * Copyright (c) 2009, Zsombor Gegesy <gzsombor@gmail.com>
  3 + *
  4 + * Redistribution and use in source and binary forms, with or without
  5 + * modification, are permitted provided that the following conditions
  6 + * are met:
  7 + * 1. Redistributions of source code must retain the above copyright
  8 + * notice, this list of conditions and the following disclaimer.
  9 + * 2. Redistributions in binary form must reproduce the above copyright
  10 + * notice, this list of conditions and the following disclaimer in the
  11 + * documentation and/or other materials provided with the distribution.
  12 + * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
  13 + * may be used to endorse or promote products derived from this software
  14 + * without specific prior written permission.
  15 + *
  16 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  17 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  20 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26 + * SUCH DAMAGE.
  27 + */
  28 +
  29 +#include "ruleselector.h"
  30 +
  31 +#define HASH_LEN 8
  32 +
  33 +// #define RULESELECTOR_DEBUG
  34 +
  35 +#if defined(RULESELECTOR_DEBUG)
  36 +#include <qdebug.h>
  37 +#endif
  38 +
  39 +RuleSelector::RuleSelector(QObject *parent)
  40 + : QObject(parent)
  41 +{
  42 +}
  43 +
  44 +
  45 +void RuleSelector::rehash(QList<const AdBlockRule *> &rules, bool exceptionRules)
  46 +{
  47 + m_ruleHash.clear();
  48 + m_extraRules.clear();
  49 +#if defined(RULESELECTOR_DEBUG)
  50 + qDebug() << __FUNCTION__ << " rehash of " << rules.size() << " rules.";
  51 +#endif
  52 +
  53 + for (int i = 0; i < rules.size(); ++i) {
  54 + const AdBlockRule *rule = rules[i];
  55 +
  56 + // filter out not enabled rules, or rules which are for blocking, when this filter selector is for accepting
  57 + if (!rule->isCSSRule() && rule->isEnabled() && rule->isException() == exceptionRules) {
  58 + bool foundHash = false;
  59 +
  60 + if (!rule->isRegexp()) {
  61 + QString pattern = rule->pattern();
  62 +
  63 + for (int i = 0; (i < (pattern.length() - HASH_LEN)) && !foundHash; ++i) {
  64 + QString substr = pattern.mid(i, HASH_LEN);
  65 +
  66 + if (!substr.contains(QLatin1Char('*'))
  67 + && !substr.contains(QLatin1Char('|'))
  68 + && !substr.contains(QLatin1Char('^'))
  69 + && !m_ruleHash.contains(substr)) {
  70 +
  71 + m_ruleHash.insert(substr, rule);
  72 +#if defined(RULESELECTOR_DEBUG)
  73 + qDebug() << "adding rule " << pattern << " with hash " << substr;
  74 +#endif
  75 + foundHash = true;
  76 + }
  77 + }
  78 + }
  79 +
  80 + if (!foundHash) {
  81 +#if defined(RULESELECTOR_DEBUG)
  82 + qDebug() << "adding as extra rule : " << rule->pattern();
  83 +#endif
  84 + m_extraRules.append(rule);
  85 + }
  86 + }
  87 + }
  88 +
  89 +#if defined(RULESELECTOR_DEBUG)
  90 + qDebug() << " rehash finished, extra rules:" << m_extraRules.size()
  91 + << " hashed : " << m_ruleHash.size() << "mode " << exceptionRules;
  92 +#endif
  93 +}
  94 +
  95 +
  96 + const AdBlockRule *RuleSelector::get(const QString &url) const
  97 + {
  98 + for (int i = 0; i < url.length() - HASH_LEN; ++i) {
  99 + QString substr = url.mid(i, HASH_LEN);
  100 + const AdBlockRule *candidate = m_ruleHash.value(substr);
  101 + if (candidate && candidate->networkMatch(url))
  102 + return candidate;
  103 + }
  104 +
  105 +#if defined(RULESELECTOR_DEBUG)
  106 + qDebug() << "get rule for " << url;
  107 +#endif
  108 + for (int i = 0; i < m_extraRules.length(); ++i) {
  109 + const AdBlockRule *candidate = m_extraRules.at(i);
  110 + if (candidate->networkMatch(url)) {
  111 +#if defined(RULESELECTOR_DEBUG)
  112 + qDebug() << "rule " << candidate->filter() << " MATCH";
  113 +#endif
  114 + return candidate;
  115 + } else {
  116 +#if defined(RULESELECTOR_DEBUG)
  117 + qDebug() << "rule " << candidate->filter() << " NOT MATCH";
  118 +#endif
  119 + }
  120 + }
  121 +
  122 + return 0;
  123 + }
54 src/adblock/ruleselector.h
... ... @@ -0,0 +1,54 @@
  1 +/**
  2 + * Copyright (c) 2009, Zsombor Gegesy <gzsombor@gmail.com>
  3 + *
  4 + * Redistribution and use in source and binary forms, with or without
  5 + * modification, are permitted provided that the following conditions
  6 + * are met:
  7 + * 1. Redistributions of source code must retain the above copyright
  8 + * notice, this list of conditions and the following disclaimer.
  9 + * 2. Redistributions in binary form must reproduce the above copyright
  10 + * notice, this list of conditions and the following disclaimer in the
  11 + * documentation and/or other materials provided with the distribution.
  12 + * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
  13 + * may be used to endorse or promote products derived from this software
  14 + * without specific prior written permission.
  15 + *
  16 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  17 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  20 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26 + * SUCH DAMAGE.
  27 + */
  28 +
  29 +#ifndef RULESELECTOR_H
  30 +#define RULESELECTOR_H
  31 +
  32 +#include <qobject.h>
  33 +#include <qlist.h>
  34 +#include <qhash.h>
  35 +
  36 +#include "adblockrule.h"
  37 +
  38 +class RuleSelector : QObject
  39 +{
  40 + Q_OBJECT
  41 +
  42 +public:
  43 + RuleSelector(QObject *parent = 0);
  44 +
  45 + const AdBlockRule *get(const QString &url) const;
  46 +
  47 + void rehash(QList<const AdBlockRule*> &rules, bool exceptionRules);
  48 +
  49 +private:
  50 + QHash<QString, const AdBlockRule*> m_ruleHash;
  51 + QList<const AdBlockRule*> m_extraRules;
  52 +};
  53 +
  54 +#endif // RULESELECTOR_H

0 comments on commit d8aeb29

Please sign in to comment.
Something went wrong with that request. Please try again.