diff options
Diffstat (limited to 'src/webengine/urlinterceptor.cpp')
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 19bc64f..cbf1c86 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -19,31 +19,56 @@ ******************************************************************************/ #include "urlinterceptor.h" -#include "filter/filtercollection.h" +#include <QFile> +#include <QTextStream> -UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) : +UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { +#ifdef QT_DEBUG + qDebug("Reading request blocklist: %s", qUtf8Printable(path)); +#endif + + int n_rules = 0; + + QFile filter(path); + if(filter.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream out(&filter); + while(!out.atEnd()) { + const QString &line = out.readLine(); + if(!line.startsWith('!')) { + FilterRule r(line); + // check if valid + m_rules.push_back(std::move(r)); + ++n_rules; + } + } + filter.close(); + } + + qDebug("Added %i rules", n_rules); } void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { -#ifdef DEBUG_VERBOSE - qDebug("%s -[%i]-> %s", qUtf8Printable(info.firstPartyUrl().toString()), info.resourceType(), qUtf8Printable(info.requestUrl().toString())); -#endif - for(auto s : m_manager->subscriptions()) { - bool shouldBlock = s->match(info); - if(shouldBlock) { + for(const FilterRule &test : m_rules) { + const QUrl &url = info.requestUrl(); + + if(test.shouldBlock(url)) { info.block(true); -#ifdef DEBUG_VERBOSE - qDebug(">> blocked"); + +#ifdef QT_DEBUG + qDebug("blocked [%s] %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(url.toString())); #endif return; } } } -void UrlRequestInterceptor::setSubscription(BlockerManager *manager) +bool shouldBlock(const QUrl &url, const QString &test) { - m_manager = manager; + if(url.toString().contains(test)) { + return true; + } + return false; } |