diff options
Diffstat (limited to 'src/webengine')
| -rw-r--r-- | src/webengine/blockerrule.cpp | 21 | ||||
| -rw-r--r-- | src/webengine/blockerrule.h | 4 | ||||
| -rw-r--r-- | src/webengine/blockersubscription.cpp | 28 | ||||
| -rw-r--r-- | src/webengine/blockersubscription.h | 8 | ||||
| -rw-r--r-- | src/webengine/urlinterceptor.cpp | 33 | ||||
| -rw-r--r-- | src/webengine/urlinterceptor.h | 7 | 
6 files changed, 84 insertions, 17 deletions
| diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp index a4a0982..340e8bd 100644 --- a/src/webengine/blockerrule.cpp +++ b/src/webengine/blockerrule.cpp @@ -3,10 +3,29 @@  BlockerRule::BlockerRule(QString rule, QObject *parent) :      QObject(parent)  { -    ruleExpression.setPattern(rule); +    QString pattern = rule; + +    if(rule.startsWith("@@")) { +        m_exception = true; +        pattern = pattern.remove(0, 2); +    } else { +        m_exception = false; +    } + +    ruleExpression.setPattern(pattern); +    m_valid = true;  }  bool BlockerRule::match(const QUrl &url)  {      return ruleExpression.match(url.toString()).hasMatch();  } + +bool BlockerRule::isValid() +{ +    return m_valid; +} +bool BlockerRule::isException() +{ +    return m_exception; +} diff --git a/src/webengine/blockerrule.h b/src/webengine/blockerrule.h index 9d73004..ddbd1c9 100644 --- a/src/webengine/blockerrule.h +++ b/src/webengine/blockerrule.h @@ -12,12 +12,16 @@ public:      explicit BlockerRule(QString rule, QObject *parent = 0);      bool match(const QUrl &url); +    bool isValid(); +    bool isException();  signals:  public slots:  private: +    bool m_valid; +    bool m_exception;      QRegularExpression ruleExpression;  }; diff --git a/src/webengine/blockersubscription.cpp b/src/webengine/blockersubscription.cpp index 5a0664b..c7f647b 100644 --- a/src/webengine/blockersubscription.cpp +++ b/src/webengine/blockersubscription.cpp @@ -24,6 +24,9 @@ int BlockerSubscription::loadFromFile(const QString &file)          return -1;      } +    // clear all lists +    m_urlBlacklist.clear(); +    m_urlWhitelist.clear();      int rules = 0;      while(!subscription.atEnd()) { @@ -33,14 +36,23 @@ int BlockerSubscription::loadFromFile(const QString &file)                  parseComment(line);              } else {                  // The line is not a comment -                BlockerRule *rule = new BlockerRule(line, this); -                m_urlBlacklist.append(rule); +                  rules++; +                BlockerRule *rule = new BlockerRule(line, this); + +                if(rule->isValid()) { +                    if(rule->isException()) { +                        m_urlWhitelist.append(rule); +                    } else { +                        m_urlBlacklist.append(rule); +                    } +                } +              }          }      } -    qDebug("Loaded %i rules from subscription %s", rules, qUtf8Printable(file)); +    qDebug("Loaded %i/%i rules from subscription %s", m_urlBlacklist.count() + m_urlWhitelist.count(), rules, qUtf8Printable(file));      return rules;  } @@ -74,6 +86,16 @@ const QDateTime BlockerSubscription::expires()      return m_expires;  } +const QList<BlockerRule*> BlockerSubscription::urlBlacklist() +{ +    return m_urlBlacklist; +} + +const QList<BlockerRule*> BlockerSubscription::urlWhitelist() +{ +    return m_urlWhitelist; +} +  void BlockerSubscription::parseComment(const QString &line)  {      if(line.startsWith("! Title: ")) { diff --git a/src/webengine/blockersubscription.h b/src/webengine/blockersubscription.h index 1379936..1bb8919 100644 --- a/src/webengine/blockersubscription.h +++ b/src/webengine/blockersubscription.h @@ -20,6 +20,9 @@ public:      const QDateTime lastModified();      const QDateTime expires(); +    const QList<BlockerRule*> urlBlacklist(); +    const QList<BlockerRule*> urlWhitelist(); +  signals:  public slots: @@ -35,7 +38,10 @@ private:      QDateTime m_lastModified;      QDateTime m_expires; -    QList<BlockerRule*> m_urlBlacklist; +    QList<BlockerRule*> m_urlWhitelist; // exception rules +    QList<BlockerRule*> m_urlBlacklist; // block rules +    // element exceptions +    // element blocking  };  #endif // URLINTERCEPTORSUBSCRIPTION_H diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 87879a5..2bd50b8 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -1,19 +1,32 @@  #include "urlinterceptor.h" -AdBlockInterceptor::AdBlockInterceptor(QObject *parent) : +UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) :      QWebEngineUrlRequestInterceptor(parent)  {  } -void AdBlockInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) +void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)  { -//    bool blocked = false; -//    for(AdBlockRule *rule : m_urlBlacklist) { -//        if(rule->match(info.requestUrl())) { -//            info.block(true); -//            blocked = true; -//        } -//    } +    for(BlockerRule *rule : m_sub->urlWhitelist()) { +        if(rule->match(info.requestUrl())) { +            qDebug("OK %s", qUtf8Printable(info.requestUrl().toString())); +            return; +        } +    } -    qDebug("%i %i %s %s", info.navigationType(), info.resourceType(), qUtf8Printable(info.requestMethod()), qUtf8Printable(info.requestUrl().toString())); +    for(BlockerRule *rule : m_sub->urlBlacklist()) { +        if(rule->match(info.requestUrl())) { +            info.block(true); +            qDebug("   %s", qUtf8Printable(info.requestUrl().toString())); +            return; +        } +    } + +    qDebug("OK %s", qUtf8Printable(info.requestUrl().toString())); + +} + +void UrlRequestInterceptor::setSubscription(BlockerSubscription *subscription) +{ +    m_sub = subscription;  } diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index 7834430..31f1256 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -2,20 +2,23 @@  #define ADBLOCKINTERCEPTOR_H  #include <QWebEngineUrlRequestInterceptor> +#include "blockersubscription.h" -class AdBlockInterceptor : public QWebEngineUrlRequestInterceptor +class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor  {      Q_OBJECT  public: -    explicit AdBlockInterceptor(QObject *parent = 0); +    explicit UrlRequestInterceptor(QObject *parent = 0);      void interceptRequest(QWebEngineUrlRequestInfo &info); +    void setSubscription(BlockerSubscription *subscription);  signals:  public slots:  private: +    BlockerSubscription *m_sub;  };  #endif // ADBLOCKINTERCEPTOR_H | 
