diff options
Diffstat (limited to 'src/webengine')
| -rw-r--r-- | src/webengine/urlinterceptor.cpp | 49 | ||||
| -rw-r--r-- | src/webengine/urlinterceptor.h | 18 | 
2 files changed, 48 insertions, 19 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;  } diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index acd076a..4466523 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -18,27 +18,31 @@   **   ******************************************************************************/ -#ifndef ADBLOCKINTERCEPTOR_H -#define ADBLOCKINTERCEPTOR_H +#ifndef URLREQUESTINTERCEPTOR_H +#define URLREQUESTINTERCEPTOR_H  #include <QWebEngineUrlRequestInterceptor> -#include "filter/blockermanager.h" +#include <vector> +#include "filter/filter.h"  class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor  {      Q_OBJECT  public: -    explicit UrlRequestInterceptor(QObject *parent = 0); +    explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr); +    //~UrlRequestInterceptor();      void interceptRequest(QWebEngineUrlRequestInfo &info); -    void setSubscription(BlockerManager *manager);  signals:  public slots:  private: -    BlockerManager *m_manager; + +    std::vector<FilterRule> m_rules;  }; -#endif // ADBLOCKINTERCEPTOR_H +bool shouldBlock(const QUrl &url, const QString &test); + +#endif // URLREQUESTINTERCEPTOR_H | 
