From 40eb1281f8de7847f00058fdaeea3206aced19da Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 5 Oct 2015 19:47:42 +0200 Subject: [PATCH] Add UrlInterceptor interface --- src/lib/lib.pro | 3 ++ src/lib/network/networkmanager.cpp | 19 +++++++++- src/lib/network/networkmanager.h | 7 ++++ src/lib/network/networkurlinterceptor.cpp | 46 +++++++++++++++++++++++ src/lib/network/networkurlinterceptor.h | 42 +++++++++++++++++++++ src/lib/network/urlinterceptor.h | 32 ++++++++++++++++ 6 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/lib/network/networkurlinterceptor.cpp create mode 100644 src/lib/network/networkurlinterceptor.h create mode 100644 src/lib/network/urlinterceptor.h diff --git a/src/lib/lib.pro b/src/lib/lib.pro index 694768a3b..82391f857 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -131,6 +131,7 @@ SOURCES += \ #network/cabundleupdater.cpp \ network/networkmanager.cpp \ network/networkproxyfactory.cpp \ + network/networkurlinterceptor.cpp \ network/pac/pacmanager.cpp \ network/pac/proxyautoconfig.cpp \ network/schemehandlers/adblockschemehandler.cpp \ @@ -324,6 +325,7 @@ HEADERS += \ #network/cabundleupdater.h \ network/networkmanager.h \ network/networkproxyfactory.h \ + network/networkurlinterceptor.h \ network/pac/pacdatetime.h \ network/pac/pacmanager.h \ network/pac/proxyautoconfig.h \ @@ -331,6 +333,7 @@ HEADERS += \ #network/schemehandlers/fileschemehandler.h \ network/schemehandlers/qupzillaschemehandler.h \ network/schemehandlers/schemehandler.h \ + network/urlinterceptor.h \ network/sslerrordialog.h \ notifications/desktopnotification.h \ notifications/desktopnotificationsfactory.h \ diff --git a/src/lib/network/networkmanager.cpp b/src/lib/network/networkmanager.cpp index 35a28ecbe..53945b43d 100644 --- a/src/lib/network/networkmanager.cpp +++ b/src/lib/network/networkmanager.cpp @@ -21,8 +21,9 @@ #include "mainapplication.h" #include "passwordmanager.h" #include "sslerrordialog.h" -#include "network/schemehandlers/adblockschemehandler.h" -#include "network/schemehandlers/qupzillaschemehandler.h" +#include "networkurlinterceptor.h" +#include "schemehandlers/adblockschemehandler.h" +#include "schemehandlers/qupzillaschemehandler.h" #include #include @@ -43,6 +44,10 @@ NetworkManager::NetworkManager(QObject *parent) mApp->webProfile()->installUrlSchemeHandler(new AdBlockSchemeHandler(this)); mApp->webProfile()->installUrlSchemeHandler(new QupZillaSchemeHandler(this)); + // Create url interceptor + m_urlInterceptor = new NetworkUrlInterceptor(this); + mApp->webProfile()->setRequestInterceptor(m_urlInterceptor); + connect(this, &QNetworkAccessManager::authenticationRequired, this, [this](QNetworkReply *reply, QAuthenticator *auth) { authentication(reply->url(), auth); }); @@ -207,3 +212,13 @@ void NetworkManager::proxyAuthentication(const QString &proxyHost, QAuthenticato auth->setUser(user->text()); auth->setPassword(pass->text()); } + +void NetworkManager::installUrlInterceptor(UrlInterceptor *interceptor) +{ + m_urlInterceptor->installUrlInterceptor(interceptor); +} + +void NetworkManager::removeUrlInterceptor(UrlInterceptor *interceptor) +{ + m_urlInterceptor->removeUrlInterceptor(interceptor); +} diff --git a/src/lib/network/networkmanager.h b/src/lib/network/networkmanager.h index 59c8d7f86..ded925dc5 100644 --- a/src/lib/network/networkmanager.h +++ b/src/lib/network/networkmanager.h @@ -23,6 +23,9 @@ #include "qzcommon.h" +class UrlInterceptor; +class NetworkUrlInterceptor; + class QUPZILLA_EXPORT NetworkManager : public QNetworkAccessManager { Q_OBJECT @@ -34,7 +37,11 @@ public: void authentication(const QUrl &url, QAuthenticator *auth, QWidget *parent = Q_NULLPTR); void proxyAuthentication(const QString &proxyHost, QAuthenticator *auth, QWidget *parent = Q_NULLPTR); + void installUrlInterceptor(UrlInterceptor *interceptor); + void removeUrlInterceptor(UrlInterceptor *interceptor); + private: + NetworkUrlInterceptor *m_urlInterceptor; QHash m_ignoredSslErrors; }; diff --git a/src/lib/network/networkurlinterceptor.cpp b/src/lib/network/networkurlinterceptor.cpp new file mode 100644 index 000000000..475bb5f62 --- /dev/null +++ b/src/lib/network/networkurlinterceptor.cpp @@ -0,0 +1,46 @@ +/* ============================================================ +* QupZilla - QtWebEngine based browser +* Copyright (C) 2015 David Rosca +* +* 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 "networkurlinterceptor.h" +#include "urlinterceptor.h" + +NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent) + : QWebEngineUrlRequestInterceptor(parent) +{ +} + +bool NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) +{ + foreach (UrlInterceptor *interceptor, m_interceptors) { + if (interceptor->interceptRequest(info)) + return true; + } + + return false; +} + +void NetworkUrlInterceptor::installUrlInterceptor(UrlInterceptor *interceptor) +{ + if (!m_interceptors.contains(interceptor)) + m_interceptors.append(interceptor); +} + +void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor) +{ + m_interceptors.removeOne(interceptor); +} diff --git a/src/lib/network/networkurlinterceptor.h b/src/lib/network/networkurlinterceptor.h new file mode 100644 index 000000000..aeaf3e0df --- /dev/null +++ b/src/lib/network/networkurlinterceptor.h @@ -0,0 +1,42 @@ +/* ============================================================ +* QupZilla - QtWebEngine based browser +* Copyright (C) 2015 David Rosca +* +* 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 NETWORKURLINTERCEPTOR_H +#define NETWORKURLINTERCEPTOR_H + +#include + +#include "qzcommon.h" + +class UrlInterceptor; + +class QUPZILLA_EXPORT NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor +{ +public: + explicit NetworkUrlInterceptor(QObject* parent = Q_NULLPTR); + + bool interceptRequest(QWebEngineUrlRequestInfo &info); + + void installUrlInterceptor(UrlInterceptor *interceptor); + void removeUrlInterceptor(UrlInterceptor *interceptor); + +private: + QList m_interceptors; +}; + +#endif // NETWORKURLINTERCEPTOR_H diff --git a/src/lib/network/urlinterceptor.h b/src/lib/network/urlinterceptor.h new file mode 100644 index 000000000..386fbb6f0 --- /dev/null +++ b/src/lib/network/urlinterceptor.h @@ -0,0 +1,32 @@ +/* ============================================================ +* QupZilla - QtWebEngine based browser +* Copyright (C) 2015 David Rosca +* +* 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 URLINTERCEPTOR_H +#define URLINTERCEPTOR_H + +#include +#include + +class UrlInterceptor : public QObject +{ +public: + explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { } + virtual bool interceptRequest(QWebEngineUrlRequestInfo &info) = 0; +}; + +#endif // URLINTERCEPTOR_H