diff --git a/src/lib/adblock/adblocksearchtree.cpp b/src/lib/adblock/adblocksearchtree.cpp index 68f8a5180..2e3c2e648 100644 --- a/src/lib/adblock/adblocksearchtree.cpp +++ b/src/lib/adblock/adblocksearchtree.cpp @@ -25,6 +25,17 @@ AdBlockSearchTree::AdBlockSearchTree() { } +AdBlockSearchTree::~AdBlockSearchTree() +{ + deleteNode(m_root); +} + +void AdBlockSearchTree::clear() +{ + deleteNode(m_root); + m_root = new Node; +} + bool AdBlockSearchTree::add(const AdBlockRule* rule) { if (rule->m_type != AdBlockRule::StringContainsMatchRule) { @@ -58,7 +69,7 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule) return true; } -const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const QString &domain, const QString &string) +const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const QString &domain, const QString &string) const { int len = string.size(); @@ -76,7 +87,7 @@ const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const return 0; } -const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QString &string) +const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QString &string) const { int len = string.size(); @@ -127,8 +138,3 @@ void AdBlockSearchTree::deleteNode(AdBlockSearchTree::Node* node) delete node; } - -AdBlockSearchTree::~AdBlockSearchTree() -{ - deleteNode(m_root); -} diff --git a/src/lib/adblock/adblocksearchtree.h b/src/lib/adblock/adblocksearchtree.h index f22c0490f..f6b9c4e99 100644 --- a/src/lib/adblock/adblocksearchtree.h +++ b/src/lib/adblock/adblocksearchtree.h @@ -33,8 +33,10 @@ public: explicit AdBlockSearchTree(); ~AdBlockSearchTree(); + void clear(); + bool add(const AdBlockRule* rule); - const AdBlockRule* find(const QNetworkRequest &request, const QString &domain, const QString &string); + const AdBlockRule* find(const QNetworkRequest &request, const QString &domain, const QString &string) const; private: struct Node { @@ -46,7 +48,7 @@ private: }; const AdBlockRule* prefixSearch(const QNetworkRequest &request, const QString &domain, - const QString &urlString, const QString &string); + const QString &urlString, const QString &string) const; void deleteNode(Node* node); diff --git a/src/lib/adblock/adblocksubscription.cpp b/src/lib/adblock/adblocksubscription.cpp index 68aa4d854..29eb09517 100644 --- a/src/lib/adblock/adblocksubscription.cpp +++ b/src/lib/adblock/adblocksubscription.cpp @@ -58,8 +58,6 @@ AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent) : QObject(parent) , m_reply(0) - , m_networkBlockTree(0) - , m_networkExceptionTree(0) , m_title(title) , m_updated(false) { @@ -203,11 +201,11 @@ void AdBlockSubscription::saveDownloadedData(const QByteArray &data) const AdBlockRule* AdBlockSubscription::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const { - if (m_networkExceptionTree->find(request, urlDomain, urlString)) { + if (m_networkExceptionTree.find(request, urlDomain, urlString)) { return 0; } - if (const AdBlockRule* rule = m_networkBlockTree->find(request, urlDomain, urlString)) { + if (const AdBlockRule* rule = m_networkBlockTree.find(request, urlDomain, urlString)) { return rule; } @@ -377,19 +375,15 @@ const AdBlockRule* AdBlockSubscription::replaceRule(AdBlockRule* rule, int offse void AdBlockSubscription::populateCache() { + m_networkExceptionTree.clear(); m_networkExceptionRules.clear(); + m_networkBlockTree.clear(); m_networkBlockRules.clear(); m_domainRestrictedCssRules.clear(); m_elementHidingRules.clear(); m_documentRules.clear(); m_elemhideRules.clear(); - delete m_networkBlockTree; - delete m_networkExceptionTree; - - m_networkBlockTree = new AdBlockSearchTree; - m_networkExceptionTree = new AdBlockSearchTree; - // 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... @@ -423,12 +417,12 @@ void AdBlockSubscription::populateCache() 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); } } @@ -443,8 +437,6 @@ void AdBlockSubscription::populateCache() AdBlockSubscription::~AdBlockSubscription() { qDeleteAll(m_rules); - delete m_networkBlockTree; - delete m_networkExceptionTree; } // AdBlockCustomList diff --git a/src/lib/adblock/adblocksubscription.h b/src/lib/adblock/adblocksubscription.h index a931544d1..3aee142c5 100644 --- a/src/lib/adblock/adblocksubscription.h +++ b/src/lib/adblock/adblocksubscription.h @@ -51,12 +51,12 @@ #include "qz_namespace.h" #include "adblockrule.h" +#include "adblocksearchtree.h" class QNetworkRequest; class QNetworkReply; class QUrl; -class AdBlockSearchTree; class FollowRedirectReply; class QT_QUPZILLA_EXPORT AdBlockSubscription : public QObject @@ -124,8 +124,8 @@ protected: QVector m_documentRules; QVector m_elemhideRules; - AdBlockSearchTree* m_networkBlockTree; - AdBlockSearchTree* m_networkExceptionTree; + AdBlockSearchTree m_networkBlockTree; + AdBlockSearchTree m_networkExceptionTree; private: QString m_title;