diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-18 23:39:28 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-19 00:30:13 +0200 |
commit | 5f623dcfc8f02504d745f47f8b9274b2bca5fd67 (patch) | |
tree | f7d6719f6c48c6c038e41631727ccc2ea3368461 /lib/urlfilter/filtertree.cpp | |
parent | urlfilter: add Domain class (diff) | |
download | smolbote-5f623dcfc8f02504d745f47f8b9274b2bca5fd67.tar.xz |
urlfilter: destroy FilterLeaves only when destroying the FilterTree
Add QMutex lock when adding rules
clang-format pass
Diffstat (limited to 'lib/urlfilter/filtertree.cpp')
-rw-r--r-- | lib/urlfilter/filtertree.cpp | 38 |
1 files changed, 23 insertions, 15 deletions
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 <QTextStream> #include "formats/hostlistrule.h" +#include <QTextStream> -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<const FilterLeaf *> FilterTree::match(const QString& domain, const QString& requestUrl) const +QVector<const FilterLeaf *> FilterTree::match(const QString &domain, const QString &requestUrl) const { QVector<const FilterLeaf *> leaves; for(const auto &branch : m_branches) { @@ -61,26 +68,27 @@ QVector<const FilterLeaf *> 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)); } |