Falkon Develop
Cross-platform Qt-based web browser
adblocksearchtree.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2013-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "adblocksearchtree.h"
19#include "adblockrule.h"
20
21#include <QWebEngineUrlRequestInfo>
22
24 : m_root(new Node)
25{
26}
27
29{
30 deleteNode(m_root);
31}
32
34{
35 deleteNode(m_root);
36 m_root = new Node;
37}
38
40{
41 if (rule->m_type != AdBlockRule::StringContainsMatchRule) {
42 return false;
43 }
44
45 const QString filter = rule->m_matchString;
46 int len = filter.size();
47
48 if (len <= 0) {
49 qDebug() << "AdBlockSearchTree: Inserting rule with filter len <= 0!" << rule->filter();
50 return false;
51 }
52
53 Node* node = m_root;
54
55 for (int i = 0; i < len; ++i) {
56 const QChar c = filter.at(i);
57 Node *next = node->children.value(c);
58 if (!next) {
59 next = new Node;
60 next->c = c;
61 node->children[c] = next;
62 }
63 node = next;
64 }
65
66 node->rule = rule;
67
68 return true;
69}
70
71const AdBlockRule* AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const
72{
73 int len = urlString.size();
74
75 if (len <= 0) {
76 return nullptr;
77 }
78
79 const QChar* string = urlString.constData();
80
81 for (int i = 0; i < len; ++i) {
82 const AdBlockRule* rule = prefixSearch(request, domain, urlString, string++, len - i);
83 if (rule) {
84 return rule;
85 }
86 }
87
88 return nullptr;
89}
90
91const AdBlockRule* AdBlockSearchTree::prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString, const QChar* string, int len) const
92{
93 if (len <= 0) {
94 return nullptr;
95 }
96
97 QChar c = string[0];
98
99 Node* node = m_root->children.value(c);
100 if (!node) {
101 return nullptr;
102 }
103
104 for (int i = 1; i < len; ++i) {
105 const QChar c = (++string)[0];
106
107 if (node->rule && node->rule->networkMatch(request, domain, urlString)) {
108 return node->rule;
109 }
110
111 node = node->children.value(c);
112 if (!node) {
113 return nullptr;
114 }
115 }
116
117 if (node->rule && node->rule->networkMatch(request, domain, urlString)) {
118 return node->rule;
119 }
120
121 return nullptr;
122}
123
124void AdBlockSearchTree::deleteNode(AdBlockSearchTree::Node* node)
125{
126 if (!node) {
127 return;
128 }
129
130 QHashIterator<QChar, Node*> i(node->children);
131 while (i.hasNext()) {
132 i.next();
133 deleteNode(i.value());
134 }
135
136 delete node;
137}
QString filter() const
const AdBlockRule * find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const
bool add(const AdBlockRule *rule)
i
Definition: i18n.py:23