diff options
author | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-07-10 17:27:45 +0200 |
---|---|---|
committer | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-08-03 20:46:55 +0000 |
commit | e11cd75ff506e8dcbfc990d70baeec821f1f0563 (patch) | |
tree | db4e58d9b0c66656e5eccb368dd61cdb36034c75 /src/core/api | |
parent | 853d5867644fd53f2d779c3c7b39a34803a3e566 (diff) |
Add API for intercepting url requests
This patch is adding a QWebEngineUrlRequestInterceptor interface
that can be subclassed to observe or intercept all resource requests
making it possible to implement browser features like adblocking
and setting custom request headers for url requests.
The interceptRequest virtual function is executed on the IO thread.
This patch also includes a unit test for the testable API parts.
Change-Id: Ibe740fc55551a9a5da40794088ccb6d03d913631
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
Reviewed-by: Martin Rotter <rotter.martinos@gmail.com>
Reviewed-by: Andras Becsi <andras.becsi@theqtcompany.com>
Diffstat (limited to 'src/core/api')
-rw-r--r-- | src/core/api/core_api.pro | 4 | ||||
-rw-r--r-- | src/core/api/qwebengineurlrequestinfo.cpp | 133 | ||||
-rw-r--r-- | src/core/api/qwebengineurlrequestinfo.h | 107 | ||||
-rw-r--r-- | src/core/api/qwebengineurlrequestinfo_p.h | 76 | ||||
-rw-r--r-- | src/core/api/qwebengineurlrequestinterceptor.h | 67 |
5 files changed, 387 insertions, 0 deletions
diff --git a/src/core/api/core_api.pro b/src/core/api/core_api.pro index 605a6dc3b..d72b27229 100644 --- a/src/core/api/core_api.pro +++ b/src/core/api/core_api.pro @@ -35,6 +35,10 @@ HEADERS = \ qtwebenginecoreglobal_p.h \ qwebenginecookiestoreclient.h \ qwebenginecookiestoreclient_p.h \ + qwebengineurlrequestinterceptor.h \ + qwebengineurlrequestinfo.h \ + qwebengineurlrequestinfo_p.h SOURCES = \ qwebenginecookiestoreclient.cpp \ + qwebengineurlrequestinfo.cpp diff --git a/src/core/api/qwebengineurlrequestinfo.cpp b/src/core/api/qwebengineurlrequestinfo.cpp new file mode 100644 index 000000000..e97b0c7b9 --- /dev/null +++ b/src/core/api/qwebengineurlrequestinfo.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwebengineurlrequestinfo.h" +#include "qwebengineurlrequestinfo_p.h" + +#include "content/public/common/resource_type.h" + +#include "web_contents_adapter_client.h" + +QT_BEGIN_NAMESPACE + +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeMainFrame, content::RESOURCE_TYPE_MAIN_FRAME) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeSubFrame, content::RESOURCE_TYPE_SUB_FRAME) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, content::RESOURCE_TYPE_STYLESHEET) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeScript, content::RESOURCE_TYPE_SCRIPT) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeImage, content::RESOURCE_TYPE_IMAGE) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeFontResource, content::RESOURCE_TYPE_FONT_RESOURCE) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeSubResource, content::RESOURCE_TYPE_SUB_RESOURCE) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeObject, content::RESOURCE_TYPE_OBJECT) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeMedia, content::RESOURCE_TYPE_MEDIA) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeWorker, content::RESOURCE_TYPE_WORKER) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeSharedWorker, content::RESOURCE_TYPE_SHARED_WORKER) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypePrefetch, content::RESOURCE_TYPE_PREFETCH) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeFavicon, content::RESOURCE_TYPE_FAVICON) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeXhr, content::RESOURCE_TYPE_XHR) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypePing, content::RESOURCE_TYPE_PING) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeServiceWorker, content::RESOURCE_TYPE_SERVICE_WORKER) +ASSERT_ENUMS_MATCH(QWebEngineUrlRequestInfo::ResourceTypeUnknown, content::RESOURCE_TYPE_LAST_TYPE) + +ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::LinkNavigation, QWebEngineUrlRequestInfo::NavigationTypeLink) +ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::TypedNavigation, QWebEngineUrlRequestInfo::NavigationTypeTyped) +ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::FormSubmittedNavigation, QWebEngineUrlRequestInfo::NavigationTypeFormSubmitted) +ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::BackForwardNavigation, QWebEngineUrlRequestInfo::NavigationTypeBackForward) +ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::ReloadNavigation, QWebEngineUrlRequestInfo::NavigationTypeReload) +ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::OtherNavigation, QWebEngineUrlRequestInfo::NavigationTypeOther) + +QWebEngineUrlRequestInfoPrivate::QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRequestInfo::ResourceType resource, QWebEngineUrlRequestInfo::NavigationType navigation, const QUrl &u, const QByteArray &m) + : resourceType(resource) + , navigationType(navigation) + , shouldBlockRequest(false) + , url(u) + , method(m) +{ +} + +QWebEngineUrlRequestInfo::~QWebEngineUrlRequestInfo() +{ + +} + +QWebEngineUrlRequestInfo::QWebEngineUrlRequestInfo(QWebEngineUrlRequestInfoPrivate *p) + : d_ptr(p) +{ + d_ptr->q_ptr = this; +} + +QWebEngineUrlRequestInfo::ResourceType QWebEngineUrlRequestInfo::resourceType() const +{ + Q_D(const QWebEngineUrlRequestInfo); + return d->resourceType; +} + +QWebEngineUrlRequestInfo::NavigationType QWebEngineUrlRequestInfo::navigationType() const +{ + Q_D(const QWebEngineUrlRequestInfo); + return d->navigationType; +} + +const QUrl &QWebEngineUrlRequestInfo::url() const +{ + Q_D(const QWebEngineUrlRequestInfo); + return d->url; +} + +const QByteArray &QWebEngineUrlRequestInfo::method() const +{ + Q_D(const QWebEngineUrlRequestInfo); + return d->method; +} + +void QWebEngineUrlRequestInfo::redirectTo(const QUrl &url) +{ + Q_D(QWebEngineUrlRequestInfo); + d->url = url; +} + +void QWebEngineUrlRequestInfo::blockRequest(bool shouldBlock) +{ + Q_D(QWebEngineUrlRequestInfo); + d->shouldBlockRequest = shouldBlock; +} + +void QWebEngineUrlRequestInfo::setExtraHeader(const QByteArray &name, const QByteArray &value) +{ + Q_D(QWebEngineUrlRequestInfo); + d->extraHeaders.insert(name, value); +} + +QT_END_NAMESPACE diff --git a/src/core/api/qwebengineurlrequestinfo.h b/src/core/api/qwebengineurlrequestinfo.h new file mode 100644 index 000000000..8ef787995 --- /dev/null +++ b/src/core/api/qwebengineurlrequestinfo.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWEBENGINEURLREQUESTINFO_H +#define QWEBENGINEURLREQUESTINFO_H + +#include "qtwebenginecoreglobal.h" + +#include <QScopedPointer> +#include <QUrl> + +namespace QtWebEngineCore { +class NetworkDelegateQt; +} + +QT_BEGIN_NAMESPACE + +class QWebEngineUrlRequestInfoPrivate; + +class QWEBENGINE_EXPORT QWebEngineUrlRequestInfo { +public: + enum ResourceType { + ResourceTypeMainFrame = 0, // top level page + ResourceTypeSubFrame, // frame or iframe + ResourceTypeStylesheet, // a CSS stylesheet + ResourceTypeScript, // an external script + ResourceTypeImage, // an image (jpg/gif/png/etc) + ResourceTypeFontResource, // a font + ResourceTypeSubResource, // an "other" subresource. + ResourceTypeObject, // an object (or embed) tag for a plugin, + // or a resource that a plugin requested. + ResourceTypeMedia, // a media resource. + ResourceTypeWorker, // the main resource of a dedicated worker. + ResourceTypeSharedWorker, // the main resource of a shared worker. + ResourceTypePrefetch, // an explicitly requested prefetch + ResourceTypeFavicon, // a favicon + ResourceTypeXhr, // a XMLHttpRequest + ResourceTypePing, // a ping request for <a ping> + ResourceTypeServiceWorker, // the main resource of a service worker. + ResourceTypeUnknown + }; + + enum NavigationType { + NavigationTypeLink, + NavigationTypeTyped, + NavigationTypeFormSubmitted, + NavigationTypeBackForward, + NavigationTypeReload, + NavigationTypeOther + }; + + ResourceType resourceType() const; + NavigationType navigationType() const; + + const QUrl &url() const; + const QByteArray &method() const; + + void blockRequest(bool shouldBlock); + void redirectTo(const QUrl &url); + void setExtraHeader(const QByteArray &name, const QByteArray &value); + +private: + friend class QtWebEngineCore::NetworkDelegateQt; + Q_DISABLE_COPY(QWebEngineUrlRequestInfo) + Q_DECLARE_PRIVATE(QWebEngineUrlRequestInfo) + + QWebEngineUrlRequestInfo(QWebEngineUrlRequestInfoPrivate *p); + ~QWebEngineUrlRequestInfo(); + QScopedPointer<QWebEngineUrlRequestInfoPrivate> d_ptr; +}; + +QT_END_NAMESPACE + +#endif // QWEBENGINEURLREQUESTINFO_H diff --git a/src/core/api/qwebengineurlrequestinfo_p.h b/src/core/api/qwebengineurlrequestinfo_p.h new file mode 100644 index 000000000..b6a304a03 --- /dev/null +++ b/src/core/api/qwebengineurlrequestinfo_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWEBENGINEURLREQUESTINFO_P_H +#define QWEBENGINEURLREQUESTINFO_P_H + +#include "qtwebenginecoreglobal_p.h" + +#include "qwebengineurlrequestinfo.h" + +#include <QByteArray> +#include <QHash> +#include <QUrl> + +namespace net { +class URLRequest; +} + +QT_BEGIN_NAMESPACE + +class QWebEngineUrlRequestInfoPrivate +{ + Q_DECLARE_PUBLIC(QWebEngineUrlRequestInfo) +public: + QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRequestInfo::ResourceType resource + , QWebEngineUrlRequestInfo::NavigationType navigation + , const QUrl &u + , const QByteArray &m); + + QWebEngineUrlRequestInfo::ResourceType resourceType; + QWebEngineUrlRequestInfo::NavigationType navigationType; + bool shouldBlockRequest; + + QUrl url; + const QByteArray method; + QHash<QByteArray, QByteArray> extraHeaders; + + QWebEngineUrlRequestInfo *q_ptr; +}; + +QT_END_NAMESPACE + +#endif // QWEBENGINEURLREQUESTINFO_P_H diff --git a/src/core/api/qwebengineurlrequestinterceptor.h b/src/core/api/qwebengineurlrequestinterceptor.h new file mode 100644 index 000000000..a38a697af --- /dev/null +++ b/src/core/api/qwebengineurlrequestinterceptor.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWEBENINGEURLREQUESTINTERCEPTOR_H +#define QWEBENINGEURLREQUESTINTERCEPTOR_H + +#include "qtwebenginecoreglobal.h" + +#include "qwebengineurlrequestinfo.h" +#include <QByteArray> +#include <QHash> +#include <QObject> +#include <QUrl> + +QT_BEGIN_NAMESPACE + +class QWEBENGINE_EXPORT QWebEngineUrlRequestInterceptor : public QObject +{ + Q_OBJECT +public: + explicit QWebEngineUrlRequestInterceptor(QObject *p = 0) + : QObject (p) + { + } + virtual ~QWebEngineUrlRequestInterceptor() + { + } + + virtual bool interceptRequest(QWebEngineUrlRequestInfo &info) = 0; +}; + +QT_END_NAMESPACE + +#endif // QWEBENINGEURLREQUESTINTERCEPTOR_H |