From 1143429887f61d8b3c74a4c3a1fafca632eb4b87 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 25 May 2017 20:59:43 +0200 Subject: Filter code refactoring --- README.md | 4 +- smolbote.qbs | 24 ++-- src/blocker/blockermanager.cpp | 53 --------- src/blocker/blockermanager.h | 46 -------- src/blocker/filtercollection.cpp | 163 -------------------------- src/blocker/filtercollection.h | 61 ---------- src/blocker/filternode.cpp | 168 --------------------------- src/blocker/filternode.h | 103 ----------------- src/blocker/filtertree.cpp | 160 ------------------------- src/blocker/filtertree.h | 51 -------- src/blocker/regexp.cpp | 72 ------------ src/blocker/regexp.h | 38 ------ src/blocker/subscriptiondialog.ui | 67 ----------- src/blocker/subscriptionform.ui | 125 -------------------- src/filter/blockermanager.cpp | 53 +++++++++ src/filter/blockermanager.h | 46 ++++++++ src/filter/filter.cpp | 237 ++++++++++++++++++++++++++++++++++++++ src/filter/filter.h | 106 +++++++++++++++++ src/filter/filtercollection.cpp | 171 +++++++++++++++++++++++++++ src/filter/filtercollection.h | 61 ++++++++++ src/filter/filtertree.cpp | 161 ++++++++++++++++++++++++++ src/filter/filtertree.h | 51 ++++++++ src/filter/regexp.cpp | 72 ++++++++++++ src/filter/regexp.h | 38 ++++++ src/filter/subscriptiondialog.ui | 67 +++++++++++ src/filter/subscriptionform.ui | 125 ++++++++++++++++++++ src/mainwindow.h | 2 +- src/webengine/urlinterceptor.cpp | 14 ++- src/webengine/urlinterceptor.h | 2 +- 29 files changed, 1216 insertions(+), 1125 deletions(-) delete mode 100644 src/blocker/blockermanager.cpp delete mode 100644 src/blocker/blockermanager.h delete mode 100644 src/blocker/filtercollection.cpp delete mode 100644 src/blocker/filtercollection.h delete mode 100644 src/blocker/filternode.cpp delete mode 100644 src/blocker/filternode.h delete mode 100644 src/blocker/filtertree.cpp delete mode 100644 src/blocker/filtertree.h delete mode 100644 src/blocker/regexp.cpp delete mode 100644 src/blocker/regexp.h delete mode 100644 src/blocker/subscriptiondialog.ui delete mode 100644 src/blocker/subscriptionform.ui create mode 100644 src/filter/blockermanager.cpp create mode 100644 src/filter/blockermanager.h create mode 100644 src/filter/filter.cpp create mode 100644 src/filter/filter.h create mode 100644 src/filter/filtercollection.cpp create mode 100644 src/filter/filtercollection.h create mode 100644 src/filter/filtertree.cpp create mode 100644 src/filter/filtertree.h create mode 100644 src/filter/regexp.cpp create mode 100644 src/filter/regexp.h create mode 100644 src/filter/subscriptiondialog.ui create mode 100644 src/filter/subscriptionform.ui diff --git a/README.md b/README.md index a90bc75..c707401 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ _yet another Qt browser_ ### What is this and why should I care? It is supposed to be: -* minimal - without any additions outside its main purpose -* configurable - in easy to find and edit text files +* minimal - just a browser, not a platform +* configurable - settings file in plain sight and plain text ### What's up with the name?! It's a small boat. diff --git a/smolbote.qbs b/smolbote.qbs index 3015750..087249a 100644 --- a/smolbote.qbs +++ b/smolbote.qbs @@ -115,18 +115,18 @@ Project { Group { name: "Request Filter" files: [ - "src/blocker/blockermanager.cpp", - "src/blocker/blockermanager.h", - "src/blocker/filternode.cpp", - "src/blocker/filternode.h", - "src/blocker/filtertree.cpp", - "src/blocker/filtertree.h", - "src/blocker/filtercollection.cpp", - "src/blocker/filtercollection.h", - "src/blocker/regexp.cpp", - "src/blocker/regexp.h", - "src/blocker/subscriptiondialog.ui", - "src/blocker/subscriptionform.ui", + "src/filter/blockermanager.cpp", + "src/filter/blockermanager.h", + "src/filter/filter.cpp", + "src/filter/filter.h", + "src/filter/filtercollection.cpp", + "src/filter/filtercollection.h", + "src/filter/filtertree.cpp", + "src/filter/filtertree.h", + "src/filter/regexp.cpp", + "src/filter/regexp.h", + "src/filter/subscriptiondialog.ui", + "src/filter/subscriptionform.ui", "src/webengine/urlinterceptor.cpp", "src/webengine/urlinterceptor.h", ] diff --git a/src/blocker/blockermanager.cpp b/src/blocker/blockermanager.cpp deleted file mode 100644 index ee30b65..0000000 --- a/src/blocker/blockermanager.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "blockermanager.h" -#include "ui_subscriptiondialog.h" - -#include "browser.h" -#include -#include - -#include "blocker/filtercollection.h" - -BlockerManager::BlockerManager(QWidget *parent) : - QDialog(parent), - ui(new Ui::UrlInterceptorDialog) -{ - ui->setupUi(this); - - const QStringList subscriptions = sSettings->value("blocker.subscriptions").toStringList(); - QStringList::const_iterator i; - for(i = subscriptions.constBegin(); i != subscriptions.constEnd(); ++i) { - FilterCollection *sub = new FilterCollection(QString(*i), this); - m_subscriptions.append(sub); - ui->tabWidget->addTab(sub, sub->name()); - } -} - -BlockerManager::~BlockerManager() -{ - delete ui; -} - -QVector BlockerManager::subscriptions() const -{ - return m_subscriptions; -} diff --git a/src/blocker/blockermanager.h b/src/blocker/blockermanager.h deleted file mode 100644 index cfa2110..0000000 --- a/src/blocker/blockermanager.h +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#ifndef URLINTERCEPTORDIALOG_H -#define URLINTERCEPTORDIALOG_H - -#include - -namespace Ui { -class UrlInterceptorDialog; -} - -class FilterCollection; -class BlockerManager : public QDialog -{ - Q_OBJECT - -public: - explicit BlockerManager(QWidget *parent = 0); - ~BlockerManager(); - - QVector subscriptions() const; - -private: - Ui::UrlInterceptorDialog *ui; - QVector m_subscriptions; -}; - -#endif // URLINTERCEPTORDIALOG_H diff --git a/src/blocker/filtercollection.cpp b/src/blocker/filtercollection.cpp deleted file mode 100644 index 935776c..0000000 --- a/src/blocker/filtercollection.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "filtercollection.h" -#include "ui_subscriptionform.h" - -#include "browser.h" -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include "filtertree.h" -#include "filternode.h" - -FilterCollection::FilterCollection(const QString path, QWidget *parent) : - QWidget(parent), - ui(new Ui::SubscriptionForm) -{ - ui->setupUi(this); - - ui->homepage->setText(path); - - qDebug("Adding subscription [%s]", qUtf8Printable(path)); - - m_filters = new FilterTree(this); - ui->treeView->setModel(m_filters); - - QFile filterFile(path); - if(!filterFile.open(QIODevice::ReadOnly)) { - qWarning("Could not open filter!"); - return; - } - - QJsonDocument filters(QJsonDocument::fromJson(filterFile.readAll())); - load(filters.object()); - - qDebug("Added %i rules", m_filters->columnCount()); -} - -FilterCollection::~FilterCollection() -{ - delete ui; -} - -QString FilterCollection::name() const -{ - return ui->title->text(); -} - -/** - * Check if a URL request should be blocked or not - * @param info - * @return true if it should be blocked; false otherwise - */ -FilterCollection::MatchResult FilterCollection::match(QWebEngineUrlRequestInfo &info) -{ - MatchResult result; - - for(FilterNode *rule : m_filters->filters(info.firstPartyUrl().toString())) { - if(rule->isValid()) { - result.match = rule->hasMatch(info); - if(result.match) { - result.block = rule->shouldBlock(info); - } else { - result.block = false; - } - result.pattern = rule->domain() + " | " + rule->request(); - return result; - } - } - - // request matches neither whitelist nor blacklist - result.match = false; - result.block = false; - return result; -} - -void FilterCollection::load(const QJsonObject &json) -{ - ui->title->setText(json["name"].toString()); - ui->homepage->setText(json["url"].toString()); - - for(QJsonValue v : json["rules"].toArray()) { - QJsonObject obj = v.toObject(); - - FilterNode::ResourceRules r; - r.allowed = parseJsonRules(obj["allowTypes"]); - r.blocked = parseJsonRules(obj["blockTypes"]); - - m_filters->addFilter(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool()); - } -} - -FilterNode::Resources FilterCollection::parseJsonRules(const QJsonValue &obj) -{ - FilterNode::Resources res; - for(QJsonValue v : obj.toArray()) { - QString t = v.toString(); - if(t == "MainFrame") { - res.setFlag(FilterNode::ResourceType::MainFrame); - } else if(t == "SubFrame") { - res.setFlag(FilterNode::ResourceType::SubFrame); - } else if(t == "Stylesheet") { - res.setFlag(FilterNode::ResourceType::Stylesheet); - } else if(t == "Script") { - res.setFlag(FilterNode::ResourceType::Script); - } else if(t == "Image") { - res.setFlag(FilterNode::ResourceType::Image); - } else if(t == "FontResource") { - res.setFlag(FilterNode::ResourceType::FontResource); - } else if(t == "SubResource") { - res.setFlag(FilterNode::ResourceType::SubResource); - } else if(t == "Object") { - res.setFlag(FilterNode::ResourceType::Object); - } else if(t == "Media") { - res.setFlag(FilterNode::ResourceType::Media); - } else if(t == "Worker") { - res.setFlag(FilterNode::ResourceType::Worker); - } else if(t == "SharedWorker") { - res.setFlag(FilterNode::ResourceType::SharedWorker); - } else if(t == "Prefetch") { - res.setFlag(FilterNode::ResourceType::Prefetch); - } else if(t == "Favicon") { - res.setFlag(FilterNode::ResourceType::Favicon); - } else if(t == "Xhr") { - res.setFlag(FilterNode::ResourceType::Xhr); - } else if(t == "Ping") { - res.setFlag(FilterNode::ResourceType::Ping); - } else if(t == "ServiceWorker") { - res.setFlag(FilterNode::ResourceType::ServiceWorker); - } else if(t == "CspWorker") { - res.setFlag(FilterNode::ResourceType::CspReport); - } else if(t == "PluginResource") { - res.setFlag(FilterNode::ResourceType::PluginResource); - } - } - - return res; -} diff --git a/src/blocker/filtercollection.h b/src/blocker/filtercollection.h deleted file mode 100644 index 4059ff1..0000000 --- a/src/blocker/filtercollection.h +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#ifndef SUBSCRIPTIONFORM_H -#define SUBSCRIPTIONFORM_H - -#include -#include -#include "filtertree.h" - -namespace Ui { -class SubscriptionForm; -} - -class FilterCollection : public QWidget -{ - Q_OBJECT - -public: - struct MatchResult { - bool match; - bool block; - QString pattern; - }; - - explicit FilterCollection(const QString path, QWidget *parent = 0); - ~FilterCollection(); - - QString name() const; - MatchResult match(QWebEngineUrlRequestInfo &info); - -private slots: - void load(const QJsonObject &json); - -private: - FilterNode::Resources parseJsonRules(const QJsonValue &obj); - - Ui::SubscriptionForm *ui; - - FilterTree *m_filters; - -}; - -#endif // SUBSCRIPTIONFORM_H diff --git a/src/blocker/filternode.cpp b/src/blocker/filternode.cpp deleted file mode 100644 index 67614ce..0000000 --- a/src/blocker/filternode.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "filternode.h" - -FilterNode::FilterNode(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, FilterNode *parentItem) -{ - m_parentItem = parentItem; - - m_domainUrl.setPattern(domain); - m_requestUrl.setPattern(request); - m_rules = rules; - m_shouldBlock = shouldBlock; - - m_valid = false; - -#ifdef DEBUG_VERBOSE - qDebug("Created rule [%s] [%s]", qUtf8Printable(domain), qUtf8Printable(request)); -#endif -} - -FilterNode::~FilterNode() -{ - qDeleteAll(m_children); -} - -void FilterNode::enable() -{ - m_valid = true; -} - -FilterNode *FilterNode::parentItem() -{ - return m_parentItem; -} - -void FilterNode::appendChild(FilterNode *child) -{ - m_children.append(child); -} - -FilterNode *FilterNode::child(int row) -{ - return m_children.value(row); -} - -int FilterNode::childCount() const -{ - return m_children.count(); -} - -QString FilterNode::domain() const -{ - return m_domainUrl.pattern(); -} - -QString FilterNode::request() const -{ - return m_requestUrl.pattern(); -} - -bool FilterNode::isBlocking() -{ - return m_shouldBlock; -} - -int FilterNode::row() const -{ - if(m_parentItem) { - return m_parentItem->m_children.indexOf(const_cast(this)); - } - - return 0; -} - -bool FilterNode::hasMatch(const QWebEngineUrlRequestInfo &info) -{ - if(m_domainUrl.hasMatch(info.firstPartyUrl().toString()) && m_requestUrl.hasMatch(info.requestUrl().toString())) { - return true; - } else { - return false; - } -} - -bool FilterNode::shouldBlock(const QWebEngineUrlRequestInfo &info) -{ - if(!m_valid) { - return false; - } - - // Check options - switch (info.resourceType()) { - case QWebEngineUrlRequestInfo::ResourceTypeMainFrame: - return testFlag(MainFrame); - case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: - return testFlag(SubFrame); - case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: - return testFlag(Stylesheet); - case QWebEngineUrlRequestInfo::ResourceTypeScript: - return testFlag(Script); - case QWebEngineUrlRequestInfo::ResourceTypeImage: - return testFlag(Image); - case QWebEngineUrlRequestInfo::ResourceTypeFontResource: - return testFlag(FontResource); - case QWebEngineUrlRequestInfo::ResourceTypeSubResource: - return testFlag(SubResource); - case QWebEngineUrlRequestInfo::ResourceTypeObject: - return testFlag(Object); - case QWebEngineUrlRequestInfo::ResourceTypeMedia: - return testFlag(Media); - case QWebEngineUrlRequestInfo::ResourceTypeWorker: - return testFlag(Worker); - case QWebEngineUrlRequestInfo::ResourceTypeSharedWorker: - return testFlag(SharedWorker); - case QWebEngineUrlRequestInfo::ResourceTypePrefetch: - return testFlag(Prefetch); - case QWebEngineUrlRequestInfo::ResourceTypeFavicon: - return testFlag(Favicon); - case QWebEngineUrlRequestInfo::ResourceTypeXhr: - return testFlag(Xhr); - case QWebEngineUrlRequestInfo::ResourceTypePing: - return testFlag(Ping); - case QWebEngineUrlRequestInfo::ResourceTypeServiceWorker: - return testFlag(ServiceWorker); - case QWebEngineUrlRequestInfo::ResourceTypeCspReport: - return testFlag(CspReport); - case QWebEngineUrlRequestInfo::ResourceTypePluginResource: - return testFlag(PluginResource); - case QWebEngineUrlRequestInfo::ResourceTypeUnknown: - break; - } - - return m_shouldBlock; - -} - -bool FilterNode::isValid() -{ - return m_valid; -} - -bool FilterNode::testFlag(ResourceType flag) -{ - if(m_rules.allowed.testFlag(flag)) { - return false; - } else if(m_rules.blocked.testFlag(flag)) { - return true; - } else { - return m_shouldBlock; - } -} diff --git a/src/blocker/filternode.h b/src/blocker/filternode.h deleted file mode 100644 index e580383..0000000 --- a/src/blocker/filternode.h +++ /dev/null @@ -1,103 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#ifndef FILTERNODE_H -#define FILTERNODE_H - -#include -#include -#include -#include "regexp.h" -#include - -class FilterNode -{ -public: - - enum ResourceType { - NoType = 0, - MainFrame = 1, - SubFrame = 2, - Stylesheet = 4, - Script = 8, - Image = 16, - FontResource = 32, - SubResource = 64, - Object = 128, - Media = 256, - Worker = 512, - SharedWorker = 1024, - Prefetch = 2048, - Favicon = 4096, - Xhr = 8192, - Ping = 16384, - ServiceWorker = 32768, - CspReport = 65536, - PluginResource = 131072, - Unknown = 262144 - }; - Q_DECLARE_FLAGS(Resources, ResourceType) - - struct ResourceRules { - Resources allowed; - Resources blocked; - }; - - explicit FilterNode(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, FilterNode *parentItem = 0); - ~FilterNode(); - - void enable(); - - FilterNode *parentItem(); - - // children - void appendChild(FilterNode *child); - FilterNode *child(int row); - int childCount() const; - - // data - QString domain() const; - QString request() const; - bool isBlocking(); - - int row() const; - - // filtering - bool hasMatch(const QWebEngineUrlRequestInfo &info); - bool shouldBlock(const QWebEngineUrlRequestInfo &info); - - bool isValid(); - -private: - bool testFlag(ResourceType flag); - - FilterNode *m_parentItem; - QList m_children; - - RegExp m_domainUrl; - RegExp m_requestUrl; - - ResourceRules m_rules; - - bool m_valid = false; - bool m_shouldBlock; -}; - -#endif // FILTERNODE_H diff --git a/src/blocker/filtertree.cpp b/src/blocker/filtertree.cpp deleted file mode 100644 index e264015..0000000 --- a/src/blocker/filtertree.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "filtertree.h" - -FilterTree::FilterTree(QObject *parent) : - QAbstractItemModel(parent) -{ - rootItem = new FilterNode("", "", FilterNode::ResourceRules(), false); -} - -FilterTree::~FilterTree() -{ - delete rootItem; -} - -QModelIndex FilterTree::index(int row, int column, const QModelIndex &parent) const -{ - if(!hasIndex(row, column, parent)) { - return QModelIndex(); - } - - FilterNode *parentItem; - if(!parent.isValid()) { - parentItem = rootItem; - } else { - parentItem = static_cast(parent.internalPointer()); - } - - FilterNode *childItem = parentItem->child(row); - if(childItem) { - return createIndex(row, column, childItem); - } else { - return QModelIndex(); - } -} - -QModelIndex FilterTree::parent(const QModelIndex &index) const -{ - if(!index.isValid()) { - return QModelIndex(); - } - - FilterNode *childItem = static_cast(index.internalPointer()); - FilterNode *parentItem = childItem->parentItem(); - - if(parentItem == rootItem) { - return QModelIndex(); - } - - return createIndex(parentItem->row(), 0, parentItem); -} - -int FilterTree::rowCount(const QModelIndex &parent) const -{ - FilterNode *parentItem; - if(parent.column() > 0) { - return 0; - } - - if(!parent.isValid()) { - parentItem = rootItem; - } else { - parentItem = static_cast(parent.internalPointer()); - } - - return parentItem->childCount(); -} - -int FilterTree::columnCount(const QModelIndex &parent) const -{ - return 5; -} - -QVariant FilterTree::data(const QModelIndex &index, int role) const -{ - if(!index.isValid()) { - return QVariant(); - } - - if(role != Qt::DisplayRole) { - return QVariant(); - } - - FilterNode *n = static_cast(index.internalPointer()); - switch (index.column()) { - case 0: // domain - return QVariant(n->domain()); - case 1: // request - return QVariant(n->request()); - case 2: // should block - return QVariant(n->isBlocking() ? "true" : "false"); - case 3: // allowed types - return QVariant("---"); - case 4: // blocked types - return QVariant("---"); - default: - return QVariant(); - } -} - -QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const -{ - if(orientation != Qt::Horizontal) { - return QVariant(); - } - - if(role != Qt::DisplayRole) { - return QVariant(); - } - - switch (section) { - case 0: - return QVariant(tr("Domain")); - case 1: - return QVariant(tr("Request")); - case 2: - return QVariant(tr("Should Block")); - case 3: - return QVariant(tr("Allowed Types")); - case 4: - return QVariant(tr("Blocked Types")); - default: - return QVariant(); - } -} - -FilterNode *FilterTree::addFilter(const QString &domain, const QString &request, FilterNode::ResourceRules rules, bool shouldBlock) -{ - FilterNode *node = new FilterNode(domain, request, rules, shouldBlock, rootItem); - node->enable(); - rootItem->appendChild(node); - return node; -} - -QVector FilterTree::filters(const QString &domain) -{ - QVector nodes; - for(int i = 0; i < rootItem->childCount(); ++i) { - nodes.append(rootItem->child(i)); - } - return nodes; -} diff --git a/src/blocker/filtertree.h b/src/blocker/filtertree.h deleted file mode 100644 index 0bae0e0..0000000 --- a/src/blocker/filtertree.h +++ /dev/null @@ -1,51 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#ifndef FILTERTREE_H -#define FILTERTREE_H - -#include -#include -#include -#include "filternode.h" - -class FilterTree : public QAbstractItemModel -{ - Q_OBJECT - -public: - explicit FilterTree(QObject *parent = 0); - ~FilterTree(); - - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; - QModelIndex parent(const QModelIndex &index) const override; - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role) const override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - - FilterNode *addFilter(const QString &domain, const QString &request, FilterNode::ResourceRules rules, bool shouldBlock); - QVector filters(const QString &domain = ""); - -private: - FilterNode *rootItem; -}; - -#endif // FILTERTREE_H diff --git a/src/blocker/regexp.cpp b/src/blocker/regexp.cpp deleted file mode 100644 index 1f3c449..0000000 --- a/src/blocker/regexp.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "regexp.h" - -RegExp::RegExp(const QString &pattern, PatternOptions options) : - QRegularExpression() -{ - setPattern(pattern); - setPatternOptions(options); -} - -bool RegExp::hasMatch(const QString &subject, int offset, MatchType matchType, MatchOptions matchOptions) const -{ - // Empty matches all - if(pattern().isEmpty()) { - return true; - } - - return QRegularExpression::match(subject, offset, matchType, matchOptions).hasMatch(); -} - -void RegExp::setWildcardPattern(const QString &pattern) -{ - QString parsed; - - for(int i=0; i. - ** - ******************************************************************************/ - -#ifndef REGEXP_H -#define REGEXP_H - -#include - -/*! - * Regular Expression class for AdBlockPlus filters - */ -class RegExp : public QRegularExpression -{ -public: - explicit RegExp(const QString &pattern = "", PatternOptions options = NoPatternOption); - - bool hasMatch(const QString &subject, int offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const; - void setWildcardPattern(const QString &pattern); -}; - -#endif // REGEXP_H diff --git a/src/blocker/subscriptiondialog.ui b/src/blocker/subscriptiondialog.ui deleted file mode 100644 index 7a63cc9..0000000 --- a/src/blocker/subscriptiondialog.ui +++ /dev/null @@ -1,67 +0,0 @@ - - - UrlInterceptorDialog - - - - 0 - 0 - 640 - 480 - - - - Blocker - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Close - - - - - - - - - buttonBox - accepted() - UrlInterceptorDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - UrlInterceptorDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/src/blocker/subscriptionform.ui b/src/blocker/subscriptionform.ui deleted file mode 100644 index 0a20582..0000000 --- a/src/blocker/subscriptionform.ui +++ /dev/null @@ -1,125 +0,0 @@ - - - SubscriptionForm - - - - 0 - 0 - 640 - 480 - - - - Form - - - - - - Subscription - - - - - - - - Title - - - - - - - TextLabel - - - - - - - Homepage - - - - - - - TextLabel - - - - - - - License - - - - - - - TextLabel - - - - - - - - - - - Version - - - - - - - TextLabel - - - - - - - Last Modified - - - - - - - TextLabel - - - - - - - Expires - - - - - - - TextLabel - - - - - - - - - - - - - - - - diff --git a/src/filter/blockermanager.cpp b/src/filter/blockermanager.cpp new file mode 100644 index 0000000..90d99a9 --- /dev/null +++ b/src/filter/blockermanager.cpp @@ -0,0 +1,53 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "blockermanager.h" +#include "ui_subscriptiondialog.h" + +#include "browser.h" +#include +#include + +#include "filtercollection.h" + +BlockerManager::BlockerManager(QWidget *parent) : + QDialog(parent), + ui(new Ui::UrlInterceptorDialog) +{ + ui->setupUi(this); + + const QStringList subscriptions = sSettings->value("blocker.subscriptions").toStringList(); + QStringList::const_iterator i; + for(i = subscriptions.constBegin(); i != subscriptions.constEnd(); ++i) { + FilterCollection *sub = new FilterCollection(QString(*i), this); + m_subscriptions.append(sub); + ui->tabWidget->addTab(sub, sub->name()); + } +} + +BlockerManager::~BlockerManager() +{ + delete ui; +} + +QVector BlockerManager::subscriptions() const +{ + return m_subscriptions; +} diff --git a/src/filter/blockermanager.h b/src/filter/blockermanager.h new file mode 100644 index 0000000..cfa2110 --- /dev/null +++ b/src/filter/blockermanager.h @@ -0,0 +1,46 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef URLINTERCEPTORDIALOG_H +#define URLINTERCEPTORDIALOG_H + +#include + +namespace Ui { +class UrlInterceptorDialog; +} + +class FilterCollection; +class BlockerManager : public QDialog +{ + Q_OBJECT + +public: + explicit BlockerManager(QWidget *parent = 0); + ~BlockerManager(); + + QVector subscriptions() const; + +private: + Ui::UrlInterceptorDialog *ui; + QVector m_subscriptions; +}; + +#endif // URLINTERCEPTORDIALOG_H diff --git a/src/filter/filter.cpp b/src/filter/filter.cpp new file mode 100644 index 0000000..e0d01a9 --- /dev/null +++ b/src/filter/filter.cpp @@ -0,0 +1,237 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "filter.h" + +Filter::Filter(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, Filter *parentItem) +{ + m_parentItem = parentItem; + + m_domainUrl.setPattern(domain); + m_requestUrl.setPattern(request); + m_rules = rules; + m_shouldBlock = shouldBlock; + + m_valid = false; +} + +Filter::~Filter() +{ + qDeleteAll(m_children); +} + +void Filter::enable() +{ + m_valid = true; +} + +Filter *Filter::parentItem() +{ + return m_parentItem; +} + +void Filter::appendChild(Filter *child) +{ + m_children.append(child); +} + +Filter *Filter::child(int row) +{ + return m_children.value(row); +} + +int Filter::childCount() const +{ + return m_children.count(); +} + +QString Filter::domain() const +{ + return m_domainUrl.pattern(); +} + +QString Filter::request() const +{ + return m_requestUrl.pattern(); +} + +bool Filter::isBlocking() +{ + return m_shouldBlock; +} + +QString Filter::allowedTypes() const +{ + return flags(m_rules.allowed); +} + +QString Filter::blockedTypes() const +{ + return flags(m_rules.blocked); +} + +int Filter::row() const +{ + if(m_parentItem) { + return m_parentItem->m_children.indexOf(const_cast(this)); + } + + return 0; +} + +bool Filter::hasMatch(const QWebEngineUrlRequestInfo &info) +{ + if(m_domainUrl.hasMatch(info.firstPartyUrl().toString()) && m_requestUrl.hasMatch(info.requestUrl().toString())) { + return true; + } else { + return false; + } +} + +bool Filter::shouldBlock(const QWebEngineUrlRequestInfo &info) +{ + if(!m_valid) { + return false; + } + + // Check options + switch (info.resourceType()) { + case QWebEngineUrlRequestInfo::ResourceTypeMainFrame: + return testFlag(MainFrame); + case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: + return testFlag(SubFrame); + case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: + return testFlag(Stylesheet); + case QWebEngineUrlRequestInfo::ResourceTypeScript: + return testFlag(Script); + case QWebEngineUrlRequestInfo::ResourceTypeImage: + return testFlag(Image); + case QWebEngineUrlRequestInfo::ResourceTypeFontResource: + return testFlag(Font); + case QWebEngineUrlRequestInfo::ResourceTypeSubResource: + return testFlag(SubResource); + case QWebEngineUrlRequestInfo::ResourceTypeObject: + return testFlag(Object); + case QWebEngineUrlRequestInfo::ResourceTypeMedia: + return testFlag(Media); + case QWebEngineUrlRequestInfo::ResourceTypeWorker: + return testFlag(Worker); + case QWebEngineUrlRequestInfo::ResourceTypeSharedWorker: + return testFlag(SharedWorker); + case QWebEngineUrlRequestInfo::ResourceTypePrefetch: + return testFlag(Prefetch); + case QWebEngineUrlRequestInfo::ResourceTypeFavicon: + return testFlag(Favicon); + case QWebEngineUrlRequestInfo::ResourceTypeXhr: + return testFlag(Xhr); + case QWebEngineUrlRequestInfo::ResourceTypePing: + return testFlag(Ping); + case QWebEngineUrlRequestInfo::ResourceTypeServiceWorker: + return testFlag(ServiceWorker); + case QWebEngineUrlRequestInfo::ResourceTypeCspReport: + return testFlag(CspReport); + case QWebEngineUrlRequestInfo::ResourceTypePluginResource: + return testFlag(PluginResource); + case QWebEngineUrlRequestInfo::ResourceTypeUnknown: + break; + } + + return m_shouldBlock; + +} + +bool Filter::isValid() +{ + return m_valid; +} + +bool Filter::testFlag(ResourceType flag) +{ + if(m_rules.allowed.testFlag(flag)) { + return false; + } else if(m_rules.blocked.testFlag(flag)) { + return true; + } else { + return m_shouldBlock; + } +} + +QString Filter::flags(Resources f) const +{ + QStringList r; + if(f.testFlag(MainFrame)) { + r << QObject::tr("Main Frame"); + } + if(f.testFlag(SubFrame)) { + r << QObject::tr("Sub Frame"); + } + if(f.testFlag(Stylesheet)) { + r << QObject::tr("Stylesheet"); + } + if(f.testFlag(Script)) { + r << QObject::tr("Script"); + } + if(f.testFlag(Image)) { + r << QObject::tr("Image"); + } + if(f.testFlag(Font)) { + r << QObject::tr("Font"); + } + if(f.testFlag(SubResource)) { + r << QObject::tr("Sub Resource"); + } + if(f.testFlag(Object)) { + r << QObject::tr("Object"); + } + if(f.testFlag(Media)) { + r << QObject::tr("Media"); + } + if(f.testFlag(Worker)) { + r << QObject::tr("Worker"); + } + if(f.testFlag(SharedWorker)) { + r << QObject::tr("Shared Worker"); + } + if(f.testFlag(Prefetch)) { + r << QObject::tr("Prefetch"); + } + if(f.testFlag(Favicon)) { + r << QObject::tr("Favicon"); + } + if(f.testFlag(Xhr)) { + r << QObject::tr("Xhr"); + } + if(f.testFlag(Ping)) { + r << QObject::tr("Ping"); + } + if(f.testFlag(ServiceWorker)) { + r << QObject::tr("Service Worker"); + } + if(f.testFlag(CspReport)) { + r << QObject::tr("Csp Report"); + } + if(f.testFlag(PluginResource)) { + r << QObject::tr("Plugin Resource"); + } + if(f.testFlag(Unknown)) { + r << QObject::tr("Unknown"); + } + return r.join(','); +} diff --git a/src/filter/filter.h b/src/filter/filter.h new file mode 100644 index 0000000..37604fe --- /dev/null +++ b/src/filter/filter.h @@ -0,0 +1,106 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef FILTERNODE_H +#define FILTERNODE_H + +#include +#include +#include +#include "regexp.h" +#include + +class Filter +{ +public: + + enum ResourceType { + NoType = 0, + MainFrame = 1, + SubFrame = 2, + Stylesheet = 4, + Script = 8, + Image = 16, + Font = 32, + SubResource = 64, + Object = 128, + Media = 256, + Worker = 512, + SharedWorker = 1024, + Prefetch = 2048, + Favicon = 4096, + Xhr = 8192, + Ping = 16384, + ServiceWorker = 32768, + CspReport = 65536, + PluginResource = 131072, + Unknown = 262144 + }; + Q_DECLARE_FLAGS(Resources, ResourceType) + + struct ResourceRules { + Resources allowed; + Resources blocked; + }; + + explicit Filter(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, Filter *parentItem = 0); + ~Filter(); + + void enable(); + + Filter *parentItem(); + + // children + void appendChild(Filter *child); + Filter *child(int row); + int childCount() const; + + // data + QString domain() const; + QString request() const; + bool isBlocking(); + QString allowedTypes() const; + QString blockedTypes() const; + + int row() const; + + // filtering + bool hasMatch(const QWebEngineUrlRequestInfo &info); + bool shouldBlock(const QWebEngineUrlRequestInfo &info); + + bool isValid(); + +private: + bool testFlag(ResourceType flag); + QString flags(Resources f) const; + + Filter *m_parentItem; + QList m_children; + + RegExp m_domainUrl; + RegExp m_requestUrl; + + ResourceRules m_rules; + + bool m_valid = false; + bool m_shouldBlock; +}; + +#endif // FILTERNODE_H diff --git a/src/filter/filtercollection.cpp b/src/filter/filtercollection.cpp new file mode 100644 index 0000000..7b07606 --- /dev/null +++ b/src/filter/filtercollection.cpp @@ -0,0 +1,171 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "filtercollection.h" +#include "ui_subscriptionform.h" + +#include "browser.h" +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include "filtertree.h" +#include "filter.h" + +FilterCollection::FilterCollection(const QString path, QWidget *parent) : + QWidget(parent), + ui(new Ui::SubscriptionForm) +{ + ui->setupUi(this); + + ui->homepage->setText(path); + + qDebug("Adding subscription [%s]", qUtf8Printable(path)); + + m_filters = new FilterTree(this); + ui->treeView->setModel(m_filters); + + QFile filterFile(path); + if(!filterFile.open(QIODevice::ReadOnly)) { + qWarning("Could not open filter!"); + return; + } + + QJsonDocument filters(QJsonDocument::fromJson(filterFile.readAll())); + qDebug("Added %i rules", load(filters.object())); +} + +FilterCollection::~FilterCollection() +{ + delete ui; +} + +QString FilterCollection::name() const +{ + return ui->title->text(); +} + +/** + * Check if a URL request should be blocked or not + * @param info + * @return true if it should be blocked; false otherwise + */ +FilterCollection::MatchResult FilterCollection::match(QWebEngineUrlRequestInfo &info) +{ + MatchResult result; + + for(Filter *rule : m_filters->filters(info.firstPartyUrl().toString())) { + if(rule->isValid()) { + result.match = rule->hasMatch(info); + if(result.match) { + result.block = rule->shouldBlock(info); + } else { + result.block = false; + } + result.pattern = rule->domain() + " | " + rule->request(); + return result; + } + } + + // request matches neither whitelist nor blacklist + result.match = false; + result.block = false; + return result; +} + +/** + * Load rules from JSON object + * @param json + */ +int FilterCollection::load(const QJsonObject &json) +{ + int number = 0; + + ui->title->setText(json["name"].toString()); + ui->homepage->setText(json["url"].toString()); + + for(QJsonValue v : json["rules"].toArray()) { + ++number; + + QJsonObject obj = v.toObject(); + + Filter::ResourceRules r; + r.allowed = parseJsonRules(obj["allowTypes"]); + r.blocked = parseJsonRules(obj["blockTypes"]); + + m_filters->addFilter(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool()); + } + + return number; +} + +Filter::Resources FilterCollection::parseJsonRules(const QJsonValue &obj) +{ + Filter::Resources res; + for(QJsonValue v : obj.toArray()) { + QString t = v.toString(); + if(t == "MainFrame") { + res.setFlag(Filter::ResourceType::MainFrame); + } else if(t == "SubFrame") { + res.setFlag(Filter::ResourceType::SubFrame); + } else if(t == "Stylesheet") { + res.setFlag(Filter::ResourceType::Stylesheet); + } else if(t == "Script") { + res.setFlag(Filter::ResourceType::Script); + } else if(t == "Image") { + res.setFlag(Filter::ResourceType::Image); + } else if(t == "FontResource") { + res.setFlag(Filter::ResourceType::Font); + } else if(t == "SubResource") { + res.setFlag(Filter::ResourceType::SubResource); + } else if(t == "Object") { + res.setFlag(Filter::ResourceType::Object); + } else if(t == "Media") { + res.setFlag(Filter::ResourceType::Media); + } else if(t == "Worker") { + res.setFlag(Filter::ResourceType::Worker); + } else if(t == "SharedWorker") { + res.setFlag(Filter::ResourceType::SharedWorker); + } else if(t == "Prefetch") { + res.setFlag(Filter::ResourceType::Prefetch); + } else if(t == "Favicon") { + res.setFlag(Filter::ResourceType::Favicon); + } else if(t == "Xhr") { + res.setFlag(Filter::ResourceType::Xhr); + } else if(t == "Ping") { + res.setFlag(Filter::ResourceType::Ping); + } else if(t == "ServiceWorker") { + res.setFlag(Filter::ResourceType::ServiceWorker); + } else if(t == "CspWorker") { + res.setFlag(Filter::ResourceType::CspReport); + } else if(t == "PluginResource") { + res.setFlag(Filter::ResourceType::PluginResource); + } + } + + return res; +} diff --git a/src/filter/filtercollection.h b/src/filter/filtercollection.h new file mode 100644 index 0000000..c39fb0a --- /dev/null +++ b/src/filter/filtercollection.h @@ -0,0 +1,61 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef SUBSCRIPTIONFORM_H +#define SUBSCRIPTIONFORM_H + +#include +#include +#include "filtertree.h" + +namespace Ui { +class SubscriptionForm; +} + +class FilterCollection : public QWidget +{ + Q_OBJECT + +public: + struct MatchResult { + bool match; + bool block; + QString pattern; + }; + + explicit FilterCollection(const QString path, QWidget *parent = 0); + ~FilterCollection(); + + QString name() const; + MatchResult match(QWebEngineUrlRequestInfo &info); + +private slots: + int load(const QJsonObject &json); + +private: + Filter::Resources parseJsonRules(const QJsonValue &obj); + + Ui::SubscriptionForm *ui; + + FilterTree *m_filters; + +}; + +#endif // SUBSCRIPTIONFORM_H diff --git a/src/filter/filtertree.cpp b/src/filter/filtertree.cpp new file mode 100644 index 0000000..b71c2a3 --- /dev/null +++ b/src/filter/filtertree.cpp @@ -0,0 +1,161 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "filtertree.h" + +FilterTree::FilterTree(QObject *parent) : + QAbstractItemModel(parent) +{ + rootItem = new Filter("", "", Filter::ResourceRules(), false); +} + +FilterTree::~FilterTree() +{ + delete rootItem; +} + +QModelIndex FilterTree::index(int row, int column, const QModelIndex &parent) const +{ + if(!hasIndex(row, column, parent)) { + return QModelIndex(); + } + + Filter *parentItem; + if(!parent.isValid()) { + parentItem = rootItem; + } else { + parentItem = static_cast(parent.internalPointer()); + } + + Filter *childItem = parentItem->child(row); + if(childItem) { + return createIndex(row, column, childItem); + } else { + return QModelIndex(); + } +} + +QModelIndex FilterTree::parent(const QModelIndex &index) const +{ + if(!index.isValid()) { + return QModelIndex(); + } + + Filter *childItem = static_cast(index.internalPointer()); + Filter *parentItem = childItem->parentItem(); + + if(parentItem == rootItem) { + return QModelIndex(); + } + + return createIndex(parentItem->row(), 0, parentItem); +} + +int FilterTree::rowCount(const QModelIndex &parent) const +{ + Filter *parentItem; + if(parent.column() > 0) { + return 0; + } + + if(!parent.isValid()) { + parentItem = rootItem; + } else { + parentItem = static_cast(parent.internalPointer()); + } + + return parentItem->childCount(); +} + +int FilterTree::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return 5; +} + +QVariant FilterTree::data(const QModelIndex &index, int role) const +{ + if(!index.isValid()) { + return QVariant(); + } + + if(role != Qt::DisplayRole) { + return QVariant(); + } + + Filter *n = static_cast(index.internalPointer()); + switch (index.column()) { + case 0: // domain + return QVariant(n->domain()); + case 1: // request + return QVariant(n->request()); + case 2: // should block + return QVariant(n->isBlocking() ? "true" : "false"); + case 3: // allowed types + return QVariant(n->allowedTypes()); + case 4: // blocked types + return QVariant(n->blockedTypes()); + default: + return QVariant(); + } +} + +QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const +{ + if(orientation != Qt::Horizontal) { + return QVariant(); + } + + if(role != Qt::DisplayRole) { + return QVariant(); + } + + switch (section) { + case 0: + return QVariant(tr("Domain")); + case 1: + return QVariant(tr("Request")); + case 2: + return QVariant(tr("Should Block")); + case 3: + return QVariant(tr("Allowed Types")); + case 4: + return QVariant(tr("Blocked Types")); + default: + return QVariant(); + } +} + +Filter *FilterTree::addFilter(const QString &domain, const QString &request, Filter::ResourceRules rules, bool shouldBlock) +{ + Filter *node = new Filter(domain, request, rules, shouldBlock, rootItem); + node->enable(); + rootItem->appendChild(node); + return node; +} + +QVector FilterTree::filters(const QString &domain) +{ + QVector nodes; + for(int i = 0; i < rootItem->childCount(); ++i) { + nodes.append(rootItem->child(i)); + } + return nodes; +} diff --git a/src/filter/filtertree.h b/src/filter/filtertree.h new file mode 100644 index 0000000..161b199 --- /dev/null +++ b/src/filter/filtertree.h @@ -0,0 +1,51 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef FILTERTREE_H +#define FILTERTREE_H + +#include +#include +#include +#include "filter.h" + +class FilterTree : public QAbstractItemModel +{ + Q_OBJECT + +public: + explicit FilterTree(QObject *parent = 0); + ~FilterTree(); + + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex parent(const QModelIndex &index) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + + Filter *addFilter(const QString &domain, const QString &request, Filter::ResourceRules rules, bool shouldBlock); + QVector filters(const QString &domain = ""); + +private: + Filter *rootItem; +}; + +#endif // FILTERTREE_H diff --git a/src/filter/regexp.cpp b/src/filter/regexp.cpp new file mode 100644 index 0000000..1f3c449 --- /dev/null +++ b/src/filter/regexp.cpp @@ -0,0 +1,72 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "regexp.h" + +RegExp::RegExp(const QString &pattern, PatternOptions options) : + QRegularExpression() +{ + setPattern(pattern); + setPatternOptions(options); +} + +bool RegExp::hasMatch(const QString &subject, int offset, MatchType matchType, MatchOptions matchOptions) const +{ + // Empty matches all + if(pattern().isEmpty()) { + return true; + } + + return QRegularExpression::match(subject, offset, matchType, matchOptions).hasMatch(); +} + +void RegExp::setWildcardPattern(const QString &pattern) +{ + QString parsed; + + for(int i=0; i. + ** + ******************************************************************************/ + +#ifndef REGEXP_H +#define REGEXP_H + +#include + +/*! + * Regular Expression class for AdBlockPlus filters + */ +class RegExp : public QRegularExpression +{ +public: + explicit RegExp(const QString &pattern = "", PatternOptions options = NoPatternOption); + + bool hasMatch(const QString &subject, int offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const; + void setWildcardPattern(const QString &pattern); +}; + +#endif // REGEXP_H diff --git a/src/filter/subscriptiondialog.ui b/src/filter/subscriptiondialog.ui new file mode 100644 index 0000000..7a63cc9 --- /dev/null +++ b/src/filter/subscriptiondialog.ui @@ -0,0 +1,67 @@ + + + UrlInterceptorDialog + + + + 0 + 0 + 640 + 480 + + + + Blocker + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + accepted() + UrlInterceptorDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + UrlInterceptorDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/filter/subscriptionform.ui b/src/filter/subscriptionform.ui new file mode 100644 index 0000000..0a20582 --- /dev/null +++ b/src/filter/subscriptionform.ui @@ -0,0 +1,125 @@ + + + SubscriptionForm + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + + Subscription + + + + + + + + Title + + + + + + + TextLabel + + + + + + + Homepage + + + + + + + TextLabel + + + + + + + License + + + + + + + TextLabel + + + + + + + + + + + Version + + + + + + + TextLabel + + + + + + + Last Modified + + + + + + + TextLabel + + + + + + + Expires + + + + + + + TextLabel + + + + + + + + + + + + + + + + diff --git a/src/mainwindow.h b/src/mainwindow.h index 059da02..ed54461 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -29,7 +29,7 @@ #include #include "widgets/webviewtabbar.h" #include "webengine/urlinterceptor.h" -#include "blocker/blockermanager.h" +#include "filter/blockermanager.h" #include "widgets/loadingbar.h" namespace Ui { diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index da2925c..3ffcb44 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -19,7 +19,11 @@ ******************************************************************************/ #include "urlinterceptor.h" -#include "blocker/filtercollection.h" +#include "filter/filtercollection.h" + +#ifdef DEBUG_VERBOSE +#include +#endif UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) @@ -29,7 +33,9 @@ UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) : void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { #ifdef DEBUG_VERBOSE + QTime time; qDebug("%s --> [%i] %s", qUtf8Printable(info.firstPartyUrl().toString()), info.resourceType(), qUtf8Printable(info.requestUrl().toString())); + time.start(); #endif for(auto s : m_manager->subscriptions()) { @@ -37,11 +43,15 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) if(r.match) { info.block(r.block); #ifdef DEBUG_VERBOSE - qDebug(">> matched [%s] [%s]", r.block ? "blocked" : "allowed", qUtf8Printable(r.pattern)); + qDebug(">> matched [%s] [%s] in %i ms", r.block ? "blocked" : "allowed", qUtf8Printable(r.pattern), time.elapsed()); #endif return; } } + +#ifdef DEBUG_VERBOSE + qDebug(">> passed in %i ms", time.elapsed()); +#endif } void UrlRequestInterceptor::setSubscription(BlockerManager *manager) diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index 65f2ce0..acd076a 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -22,7 +22,7 @@ #define ADBLOCKINTERCEPTOR_H #include -#include "blocker/blockermanager.h" +#include "filter/blockermanager.h" class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { -- cgit v1.2.1