From 2daa4950118dd76d0d0bf15e347217740b0cb5c5 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 19 Jan 2018 13:30:51 +0100 Subject: Add initiator to QWebEngineUrlRequestJob Add a property that can be used to tell what is making the URL request. Change-Id: Ic7224382165e93d3c043c30e9a7cc5be9f29d9db Reviewed-by: Michal Klocek --- src/core/api/qwebengineurlrequestjob.cpp | 11 +++++++++++ src/core/api/qwebengineurlrequestjob.h | 1 + src/core/url_request_custom_job.cpp | 2 +- src/core/url_request_custom_job_delegate.cpp | 11 +++++++++-- src/core/url_request_custom_job_delegate.h | 5 ++++- src/core/url_request_custom_job_proxy.cpp | 9 +++++++-- src/core/url_request_custom_job_proxy.h | 4 +++- 7 files changed, 36 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/core/api/qwebengineurlrequestjob.cpp b/src/core/api/qwebengineurlrequestjob.cpp index 47aab48a0..941c70b1d 100644 --- a/src/core/api/qwebengineurlrequestjob.cpp +++ b/src/core/api/qwebengineurlrequestjob.cpp @@ -113,6 +113,17 @@ QByteArray QWebEngineUrlRequestJob::requestMethod() const return d_ptr->method(); } +/*! + \since 5.11 + Returns the origin URL of the content that initiated the request. If the + request was not initiated by web content the function will return an + empty QUrl. +*/ +QUrl QWebEngineUrlRequestJob::initiator() const +{ + return d_ptr->initiator(); +} + /*! Replies to the request with \a device and the MIME type \a contentType. diff --git a/src/core/api/qwebengineurlrequestjob.h b/src/core/api/qwebengineurlrequestjob.h index 4f23ab401..7a7dbd83d 100644 --- a/src/core/api/qwebengineurlrequestjob.h +++ b/src/core/api/qwebengineurlrequestjob.h @@ -72,6 +72,7 @@ public: QUrl requestUrl() const; QByteArray requestMethod() const; + QUrl initiator() const; void reply(const QByteArray &contentType, QIODevice *device); void fail(Error error); diff --git a/src/core/url_request_custom_job.cpp b/src/core/url_request_custom_job.cpp index 070414b4b..cf96cd6d9 100644 --- a/src/core/url_request_custom_job.cpp +++ b/src/core/url_request_custom_job.cpp @@ -78,7 +78,7 @@ void URLRequestCustomJob::Start() DCHECK_CURRENTLY_ON(content::BrowserThread::IO); content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, base::Bind(&URLRequestCustomJobProxy::initialize, - m_proxy, request()->url(), request()->method())); + m_proxy, request()->url(), request()->method(), request()->initiator())); } void URLRequestCustomJob::Kill() diff --git a/src/core/url_request_custom_job_delegate.cpp b/src/core/url_request_custom_job_delegate.cpp index 6b82cebb5..338bd7137 100644 --- a/src/core/url_request_custom_job_delegate.cpp +++ b/src/core/url_request_custom_job_delegate.cpp @@ -50,10 +50,12 @@ namespace QtWebEngineCore { URLRequestCustomJobDelegate::URLRequestCustomJobDelegate(URLRequestCustomJobProxy *proxy, const QUrl &url, - const QByteArray &method) + const QByteArray &method, + const QUrl &initiatorOrigin) : m_proxy(proxy), m_request(url), - m_method(method) + m_method(method), + m_initiatorOrigin(initiatorOrigin) { } @@ -71,6 +73,11 @@ QByteArray URLRequestCustomJobDelegate::method() const return m_method; } +QUrl URLRequestCustomJobDelegate::initiator() const +{ + return m_initiatorOrigin; +} + void URLRequestCustomJobDelegate::reply(const QByteArray &contentType, QIODevice *device) { if (device) diff --git a/src/core/url_request_custom_job_delegate.h b/src/core/url_request_custom_job_delegate.h index 3f5e6d591..6bbd10909 100644 --- a/src/core/url_request_custom_job_delegate.h +++ b/src/core/url_request_custom_job_delegate.h @@ -68,6 +68,7 @@ public: QUrl url() const; QByteArray method() const; + QUrl initiator() const; void reply(const QByteArray &contentType, QIODevice *device); void redirect(const QUrl& url); @@ -80,12 +81,14 @@ private Q_SLOTS: private: URLRequestCustomJobDelegate(URLRequestCustomJobProxy *proxy, const QUrl &url, - const QByteArray &method); + const QByteArray &method, + const QUrl &initiatorOrigin); friend class URLRequestCustomJobProxy; scoped_refptr m_proxy; QUrl m_request; QByteArray m_method; + QUrl m_initiatorOrigin; }; } // namespace diff --git a/src/core/url_request_custom_job_proxy.cpp b/src/core/url_request_custom_job_proxy.cpp index 832d3d11e..6c9824bb9 100644 --- a/src/core/url_request_custom_job_proxy.cpp +++ b/src/core/url_request_custom_job_proxy.cpp @@ -151,18 +151,23 @@ void URLRequestCustomJobProxy::readyRead() m_job->notifyReadyRead(); } -void URLRequestCustomJobProxy::initialize(GURL url, std::string method) +void URLRequestCustomJobProxy::initialize(GURL url, std::string method, base::Optional initiator) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); Q_ASSERT(!m_delegate); + QUrl initiatorOrigin; + if (initiator.has_value()) + initiatorOrigin = toQt(initiator.value().GetURL()); + QWebEngineUrlSchemeHandler *schemeHandler = 0; QSharedPointer browserContext = m_adapter.toStrongRef(); if (browserContext) schemeHandler = browserContext->customUrlSchemeHandlers()[toQByteArray(m_scheme)]; if (schemeHandler) { m_delegate = new URLRequestCustomJobDelegate(this, toQt(url), - QByteArray::fromStdString(method)); + QByteArray::fromStdString(method), + initiatorOrigin); QWebEngineUrlRequestJob *requestJob = new QWebEngineUrlRequestJob(m_delegate); schemeHandler->requestStarted(requestJob); } diff --git a/src/core/url_request_custom_job_proxy.h b/src/core/url_request_custom_job_proxy.h index 3ea30a4e1..603ad5840 100644 --- a/src/core/url_request_custom_job_proxy.h +++ b/src/core/url_request_custom_job_proxy.h @@ -41,7 +41,9 @@ #define URL_REQUEST_CUSTOM_JOB_PROXY_H_ #include "base/memory/weak_ptr.h" +#include "base/optional.h" #include "url/gurl.h" +#include "url/origin.h" #include QT_FORWARD_DECLARE_CLASS(QIODevice) @@ -70,7 +72,7 @@ public: void abort(); void fail(int error); void release(); - void initialize(GURL url, std::string method); + void initialize(GURL url, std::string method, base::Optional initiatorOrigin); void readyRead(); // IO thread owned: -- cgit v1.2.3