Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

port the original hashing rule selector, to the new API

  • Loading branch information...
commit d8aeb29875c9a2687b26fae1e19e0ae91898ca04 1 parent 37b78db
@gzsombor authored
View
6 src/adblock/adblock.pri
@@ -10,7 +10,8 @@ HEADERS += \
adblockpage.h \
adblockrule.h \
adblockschemeaccesshandler.h \
- adblocksubscription.h
+ adblocksubscription.h \
+ ruleselector.h
SOURCES += \
adblockblockednetworkreply.cpp \
@@ -21,7 +22,8 @@ SOURCES += \
adblockpage.cpp \
adblockrule.cpp \
adblockschemeaccesshandler.cpp \
- adblocksubscription.cpp
+ adblocksubscription.cpp \
+ ruleselector.cpp
FORMS += \
adblockdialog.ui
View
6 src/adblock/adblockrule.cpp
@@ -192,7 +192,13 @@ static QString convertPatternToRegExp(const QString &wildcardPattern) {
void AdBlockRule::setPattern(const QString &pattern, bool isRegExp)
{
+ m_pattern = pattern;
+ m_regexpRule = isRegExp;
m_regExp = QRegExp(isRegExp ? pattern : convertPatternToRegExp(pattern),
Qt::CaseInsensitive, QRegExp::RegExp2);
}
+QString AdBlockRule::pattern() const
+{
+ return m_pattern;
+}
View
5 src/adblock/adblockrule.h
@@ -33,6 +33,7 @@
class QUrl;
class QRegExp;
+
class AdBlockRule
{
@@ -51,15 +52,19 @@ class AdBlockRule
bool isEnabled() const;
void setEnabled(bool enabled);
+ QString pattern() const;
QString regExpPattern() const;
+ bool isRegexp() const { return m_regexpRule; }
void setPattern(const QString &pattern, bool isRegExp);
private:
QString m_filter;
+ QString m_pattern;
bool m_cssRule;
bool m_exception;
bool m_enabled;
+ bool m_regexpRule;
QRegExp m_regExp;
QStringList m_options;
};
View
11 src/adblock/adblocksubscription.cpp
@@ -38,6 +38,7 @@
#include <qtextstream.h>
// #define ADBLOCKSUBSCRIPTION_DEBUG
+// #define ADBLOCK_LINEAR_SEARCH
AdBlockSubscription::AdBlockSubscription(const QUrl &url, QObject *parent)
: QObject(parent)
@@ -295,20 +296,28 @@ QList<const AdBlockRule*> AdBlockSubscription::pageRules() const
const AdBlockRule *AdBlockSubscription::allow(const QString &urlString) const
{
+#if defined(ADBLOCK_LINEAR_SEARCH)
foreach (const AdBlockRule *rule, m_networkExceptionRules) {
if (rule->networkMatch(urlString))
return rule;
}
return 0;
+#else
+ return m_networkExceptionRuleSelector.get(urlString);
+#endif
}
const AdBlockRule *AdBlockSubscription::block(const QString &urlString) const
{
+#if defined(ADBLOCK_LINEAR_SEARCH)
foreach (const AdBlockRule *rule, m_networkBlockRules) {
if (rule->networkMatch(urlString))
return rule;
}
return 0;
+#else
+ return m_networkBlockRuleSelector.get(urlString);
+#endif
}
QList<AdBlockRule> AdBlockSubscription::allRules() const
@@ -371,5 +380,7 @@ void AdBlockSubscription::populateCache()
m_networkBlockRules.append(rule);
}
}
+ m_networkExceptionRuleSelector.rehash(m_networkExceptionRules, true);
+ m_networkBlockRuleSelector.rehash(m_networkBlockRules, false);
}
View
4 src/adblock/adblocksubscription.h
@@ -32,6 +32,7 @@
#include <qobject.h>
#include "adblockrule.h"
+#include "ruleselector.h"
#include <qlist.h>
#include <qdatetime.h>
@@ -96,6 +97,9 @@ private slots:
QList<const AdBlockRule*> m_networkExceptionRules;
QList<const AdBlockRule*> m_networkBlockRules;
QList<const AdBlockRule*> m_pageRules;
+
+ RuleSelector m_networkBlockRuleSelector;
+ RuleSelector m_networkExceptionRuleSelector;
};
#endif // ADBLOCKSUBSCRIPTION_H
View
123 src/adblock/ruleselector.cpp
@@ -0,0 +1,123 @@
+/**
+ * Copyright (c) 2009, Zsombor Gegesy <gzsombor@gmail.com>
+ *
+ * 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 "ruleselector.h"
+
+#define HASH_LEN 8
+
+// #define RULESELECTOR_DEBUG
+
+#if defined(RULESELECTOR_DEBUG)
+#include <qdebug.h>
+#endif
+
+RuleSelector::RuleSelector(QObject *parent)
+ : QObject(parent)
+{
+}
+
+
+void RuleSelector::rehash(QList<const AdBlockRule *> &rules, bool exceptionRules)
+{
+ m_ruleHash.clear();
+ m_extraRules.clear();
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << __FUNCTION__ << " rehash of " << rules.size() << " rules.";
+#endif
+
+ for (int i = 0; i < rules.size(); ++i) {
+ const AdBlockRule *rule = rules[i];
+
+ // filter out not enabled rules, or rules which are for blocking, when this filter selector is for accepting
+ if (!rule->isCSSRule() && rule->isEnabled() && rule->isException() == exceptionRules) {
+ bool foundHash = false;
+
+ if (!rule->isRegexp()) {
+ QString pattern = rule->pattern();
+
+ for (int i = 0; (i < (pattern.length() - HASH_LEN)) && !foundHash; ++i) {
+ QString substr = pattern.mid(i, HASH_LEN);
+
+ if (!substr.contains(QLatin1Char('*'))
+ && !substr.contains(QLatin1Char('|'))
+ && !substr.contains(QLatin1Char('^'))
+ && !m_ruleHash.contains(substr)) {
+
+ m_ruleHash.insert(substr, rule);
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << "adding rule " << pattern << " with hash " << substr;
+#endif
+ foundHash = true;
+ }
+ }
+ }
+
+ if (!foundHash) {
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << "adding as extra rule : " << rule->pattern();
+#endif
+ m_extraRules.append(rule);
+ }
+ }
+ }
+
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << " rehash finished, extra rules:" << m_extraRules.size()
+ << " hashed : " << m_ruleHash.size() << "mode " << exceptionRules;
+#endif
+}
+
+
+ const AdBlockRule *RuleSelector::get(const QString &url) const
+ {
+ for (int i = 0; i < url.length() - HASH_LEN; ++i) {
+ QString substr = url.mid(i, HASH_LEN);
+ const AdBlockRule *candidate = m_ruleHash.value(substr);
+ if (candidate && candidate->networkMatch(url))
+ return candidate;
+ }
+
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << "get rule for " << url;
+#endif
+ for (int i = 0; i < m_extraRules.length(); ++i) {
+ const AdBlockRule *candidate = m_extraRules.at(i);
+ if (candidate->networkMatch(url)) {
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << "rule " << candidate->filter() << " MATCH";
+#endif
+ return candidate;
+ } else {
+#if defined(RULESELECTOR_DEBUG)
+ qDebug() << "rule " << candidate->filter() << " NOT MATCH";
+#endif
+ }
+ }
+
+ return 0;
+ }
View
54 src/adblock/ruleselector.h
@@ -0,0 +1,54 @@
+/**
+ * Copyright (c) 2009, Zsombor Gegesy <gzsombor@gmail.com>
+ *
+ * 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 RULESELECTOR_H
+#define RULESELECTOR_H
+
+#include <qobject.h>
+#include <qlist.h>
+#include <qhash.h>
+
+#include "adblockrule.h"
+
+class RuleSelector : QObject
+{
+ Q_OBJECT
+
+public:
+ RuleSelector(QObject *parent = 0);
+
+ const AdBlockRule *get(const QString &url) const;
+
+ void rehash(QList<const AdBlockRule*> &rules, bool exceptionRules);
+
+private:
+ QHash<QString, const AdBlockRule*> m_ruleHash;
+ QList<const AdBlockRule*> m_extraRules;
+};
+
+#endif // RULESELECTOR_H
Please sign in to comment.
Something went wrong with that request. Please try again.