From 5f623dcfc8f02504d745f47f8b9274b2bca5fd67 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 18 Oct 2018 23:39:28 +0200 Subject: urlfilter: destroy FilterLeaves only when destroying the FilterTree Add QMutex lock when adding rules clang-format pass --- lib/urlfilter/filtertree.cpp | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'lib/urlfilter/filtertree.cpp') diff --git a/lib/urlfilter/filtertree.cpp b/lib/urlfilter/filtertree.cpp index 8d88140..dcde196 100644 --- a/lib/urlfilter/filtertree.cpp +++ b/lib/urlfilter/filtertree.cpp @@ -8,10 +8,10 @@ #include "filtertree.h" #include "filterleaf.h" -#include #include "formats/hostlistrule.h" +#include -bool loadHostlist(QIODevice &from, FilterTree* tree) +bool loadHostlist(QIODevice &from, FilterTree *tree) { Q_ASSERT(from.isReadable()); QTextStream stream(&from); @@ -36,11 +36,18 @@ bool loadHostlist(QIODevice &from, FilterTree* tree) if(!added) return false; } - } return true; } +FilterTree::~FilterTree() +{ + for(auto &branch : m_branches) { + qDeleteAll(branch.leaves); + branch.leaves.clear(); + } +} + const QStringList FilterTree::branches() const { QStringList branches; @@ -50,7 +57,7 @@ const QStringList FilterTree::branches() const return branches; } -QVector FilterTree::match(const QString& domain, const QString& requestUrl) const +QVector FilterTree::match(const QString &domain, const QString &requestUrl) const { QVector leaves; for(const auto &branch : m_branches) { @@ -61,26 +68,27 @@ QVector FilterTree::match(const QString& domain, const QStri leaves.append(leaf); } } - } } return leaves; } -bool FilterTree::addRule(FilterLeaf *rule, const QString& domain) +bool FilterTree::addRule(FilterLeaf *rule, const QString &domain) +{ + branchLock.lock(); + this->branch(domain).leaves.emplace_back(rule); + branchLock.unlock(); + return true; +} + +FilterTree::Branch & FilterTree::branch(const QString& domain) { for(auto &branch : m_branches) { - if(branch.domain.matches(QUrl(domain))) { - branch.leaves.emplace_back(rule); - return true; - } + if(branch.domain.matches(QUrl(domain))) + return branch; } // no branch was found Branch branch(domain); - //branch.domain = domain.toStdString(); - // TODO: for some reason, can't add rule here - //branch.leaves.emplace_back(rule); - m_branches.emplace_back(std::move(branch)); - return this->addRule(rule, domain); + return m_branches.emplace_back(std::move(branch)); } -- cgit v1.2.1