From 5e92adf5f85c4ec8340d5b7e92166a5e4f8e9883 Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Fri, 1 Feb 2019 12:01:26 +0100 Subject: Move QWebEngineUrlRequestInterceptor::intercept to ui thread Currently interceptor sufferers thread safety issues, when custom profiles are deleted, interceptor is set to be nullptr, however it can be still referenced in IO thread. Since profile was split to ui and io part, where io part can outlive the ui part, this can boost thread safety issues. Since QWebEngineUrlRequestInterceptor is living on ui thread simplify the logic move intercept call to ui thread. This fixes the issue of referencing interceptor in io thread. Add new method to install interceptor setUrlRequestInterceptor, and deprecate old one. Update interceptor install method name on page to match the profile one. Task-number: QTBUG-69844 Change-Id: I5dd2b6b734fd91906cccc6c1408ffbe7b1b4250c Reviewed-by: Allan Sandfeld Jensen --- src/core/api/qwebengineurlrequestinfo.cpp | 10 ++--- src/core/api/qwebengineurlrequestinterceptor.h | 2 +- src/core/net/network_delegate_qt.cpp | 53 +++++++++++++++----------- src/core/net/url_request_notification.cpp | 34 +++++++++++------ src/core/net/url_request_notification.h | 8 +++- src/core/profile_io_data_qt.cpp | 10 +++++ src/core/profile_io_data_qt.h | 2 + 7 files changed, 77 insertions(+), 42 deletions(-) (limited to 'src/core') diff --git a/src/core/api/qwebengineurlrequestinfo.cpp b/src/core/api/qwebengineurlrequestinfo.cpp index c3e5b5445..dc2d07740 100644 --- a/src/core/api/qwebengineurlrequestinfo.cpp +++ b/src/core/api/qwebengineurlrequestinfo.cpp @@ -96,8 +96,8 @@ ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::OtherNavigation, Q interceptor on the profile enables intercepting, blocking, and modifying URL requests before they reach the networking stack of Chromium. - You can install the interceptor on a profile via QWebEngineProfile::setRequestInterceptor() - or QQuickWebEngineProfile::setRequestInterceptor(). + You can install the interceptor on a profile via QWebEngineProfile::setUrlRequestInterceptor() + or QQuickWebEngineProfile::setUrlRequestInterceptor(). When using the \l{Qt WebEngine Widgets Module}, \l{QWebEnginePage::acceptNavigationRequest()} offers further options to accept or block requests. @@ -115,11 +115,7 @@ ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::OtherNavigation, Q \fn void QWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) Reimplementing this virtual function makes it possible to intercept URL - requests. For interceptors installed on a QWebEngineProfile, the function is executed - on the I/O thread, and thus it may not be thread-safe to interact with pages. If the - interceptor was installed on a QWebEnginePage, the function is executed on the main - application thread, and can safely interact with other user classes. Both versions will - be stalling the URL request until handled. + requests. This method will be stalling the URL request until handled. \a info contains the information about the URL request and will track internally whether its members have been altered. diff --git a/src/core/api/qwebengineurlrequestinterceptor.h b/src/core/api/qwebengineurlrequestinterceptor.h index dc2a15ee3..2b07681ca 100644 --- a/src/core/api/qwebengineurlrequestinterceptor.h +++ b/src/core/api/qwebengineurlrequestinterceptor.h @@ -55,7 +55,7 @@ class QWEBENGINECORE_EXPORT QWebEngineUrlRequestInterceptor : public QObject Q_OBJECT Q_DISABLE_COPY(QWebEngineUrlRequestInterceptor) public: - explicit QWebEngineUrlRequestInterceptor(QObject *p = Q_NULLPTR) + explicit QWebEngineUrlRequestInterceptor(QObject *p = nullptr) : QObject (p) { } diff --git a/src/core/net/network_delegate_qt.cpp b/src/core/net/network_delegate_qt.cpp index d2b9f06b3..3641cb845 100644 --- a/src/core/net/network_delegate_qt.cpp +++ b/src/core/net/network_delegate_qt.cpp @@ -131,33 +131,41 @@ int NetworkDelegateQt::OnBeforeURLRequest(net::URLRequest *request, net::Complet QByteArray::fromStdString(request->method())); QWebEngineUrlRequestInfo requestInfo(infoPrivate); - if (QWebEngineUrlRequestInterceptor* interceptor = m_profileIOData->acquireInterceptor()) { - interceptor->interceptRequest(requestInfo); - m_profileIOData->releaseInterceptor(); - if (requestInfo.changed()) { - int result = infoPrivate->shouldBlockRequest ? net::ERR_BLOCKED_BY_CLIENT : net::OK; - - if (qUrl != infoPrivate->url) - *newUrl = toGurl(infoPrivate->url); - - if (!infoPrivate->extraHeaders.isEmpty()) { - auto end = infoPrivate->extraHeaders.constEnd(); - for (auto header = infoPrivate->extraHeaders.constBegin(); header != end; ++header) - request->SetExtraRequestHeaderByName(header.key().toStdString(), header.value().toStdString(), /* overwrite */ true); + // Deprecated =begin + // quick peek if deprecated + QWebEngineUrlRequestInterceptor* profileInterceptor = m_profileIOData->requestInterceptor(); + if (profileInterceptor && profileInterceptor->property("deprecated").toBool()) { + profileInterceptor = nullptr; + if (QWebEngineUrlRequestInterceptor* interceptor = m_profileIOData->acquireInterceptor()) { + interceptor->interceptRequest(requestInfo); + m_profileIOData->releaseInterceptor(); + if (requestInfo.changed()) { + int result = infoPrivate->shouldBlockRequest ? net::ERR_BLOCKED_BY_CLIENT : net::OK; + + if (qUrl != infoPrivate->url) + *newUrl = toGurl(infoPrivate->url); + + if (!infoPrivate->extraHeaders.isEmpty()) { + auto end = infoPrivate->extraHeaders.constEnd(); + for (auto header = infoPrivate->extraHeaders.constBegin(); header != end; ++header) + request->SetExtraRequestHeaderByName(header.key().toStdString(), header.value().toStdString(), /* overwrite */ true); + } + + if (result != net::OK) + return result; + + requestInfo.resetChanged(); } - - if (result != net::OK) - return result; - - requestInfo.resetChanged(); + } else { + m_profileIOData->releaseInterceptor(); } - } else - m_profileIOData->releaseInterceptor(); + } + // Deprecated =cut if (!resourceInfo) return net::OK; - if (!m_profileIOData->hasPageInterceptors() && !content::IsResourceTypeFrame(resourceType)) + if (!m_profileIOData->hasPageInterceptors() && !profileInterceptor && !content::IsResourceTypeFrame(resourceType)) return net::OK; auto webContentsGetter = resourceInfo->GetWebContentsGetterForRequest(); @@ -167,7 +175,8 @@ int NetworkDelegateQt::OnBeforeURLRequest(net::URLRequest *request, net::Complet newUrl, std::move(requestInfo), webContentsGetter, - std::move(callback) + std::move(callback), + profileInterceptor ? m_profileIOData->profileAdapter() : nullptr ); // We'll run the callback after we notified the UI thread. diff --git a/src/core/net/url_request_notification.cpp b/src/core/net/url_request_notification.cpp index 325474bc4..6da661cff 100644 --- a/src/core/net/url_request_notification.cpp +++ b/src/core/net/url_request_notification.cpp @@ -46,6 +46,7 @@ #include "net/url_request/url_request.h" #include "web_contents_adapter_client.h" #include "web_contents_view_qt.h" +#include "profile_io_data_qt.h" #include "qwebengineurlrequestinfo_p.h" #include "type_conversion.h" @@ -73,7 +74,8 @@ URLRequestNotification::URLRequestNotification(net::URLRequest *request, GURL *newUrl, QWebEngineUrlRequestInfo &&requestInfo, content::ResourceRequestInfo::WebContentsGetter webContentsGetter, - net::CompletionOnceCallback callback) + net::CompletionOnceCallback callback, + QPointer adapter) : m_request(request) , m_isMainFrameRequest(isMainFrameRequest) , m_newUrl(newUrl) @@ -81,6 +83,7 @@ URLRequestNotification::URLRequestNotification(net::URLRequest *request, , m_requestInfo(std::move(requestInfo)) , m_webContentsGetter(webContentsGetter) , m_callback(std::move(callback)) + , m_profileAdapter(adapter) { DCHECK_CURRENTLY_ON(content::BrowserThread::IO); @@ -99,36 +102,45 @@ void URLRequestNotification::notify() // May run concurrently with cancel() so no peeking at m_request here. - int error = net::OK; + int result = net::OK; content::WebContents *webContents = m_webContentsGetter.Run(); if (webContents) { + + if (m_profileAdapter) { + QWebEngineUrlRequestInterceptor* interceptor = m_profileAdapter->requestInterceptor(); + interceptor->interceptRequest(m_requestInfo); + } + WebContentsAdapterClient *client = - WebContentsViewQt::from(static_cast(webContents)->GetView())->client(); + WebContentsViewQt::from(static_cast(webContents)->GetView())->client(); + + if (!m_requestInfo.changed()) { + client->interceptRequest(m_requestInfo); + } - client->interceptRequest(m_requestInfo); if (m_requestInfo.changed()) { - error = m_requestInfo.d_ptr->shouldBlockRequest ? net::ERR_BLOCKED_BY_CLIENT : net::OK; + result = m_requestInfo.d_ptr->shouldBlockRequest ? net::ERR_BLOCKED_BY_CLIENT : net::OK; // We handle the rest of the changes later when we are back in I/O thread } // Only do navigationRequested on MAIN_FRAME and SUB_FRAME resources - if (error == net::OK && content::IsResourceTypeFrame(fromQt(m_requestInfo.resourceType()))) { + if (result == net::OK && content::IsResourceTypeFrame(fromQt(m_requestInfo.resourceType()))) { int navigationRequestAction = WebContentsAdapterClient::AcceptRequest; client->navigationRequested(m_requestInfo.navigationType(), m_requestInfo.requestUrl(), navigationRequestAction, m_isMainFrameRequest); - error = net::ERR_FAILED; + result = net::ERR_FAILED; switch (static_cast(navigationRequestAction)) { case WebContentsAdapterClient::AcceptRequest: - error = net::OK; + result = net::OK; break; case WebContentsAdapterClient::IgnoreRequest: - error = net::ERR_ABORTED; + result = net::ERR_ABORTED; break; } - DCHECK(error != net::ERR_FAILED); + DCHECK(result != net::ERR_FAILED); } } @@ -136,7 +148,7 @@ void URLRequestNotification::notify() base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&URLRequestNotification::complete, base::Unretained(this), error)); + base::BindOnce(&URLRequestNotification::complete, base::Unretained(this), result)); } void URLRequestNotification::cancel() diff --git a/src/core/net/url_request_notification.h b/src/core/net/url_request_notification.h index 172e034a3..1d9acf12f 100644 --- a/src/core/net/url_request_notification.h +++ b/src/core/net/url_request_notification.h @@ -43,6 +43,7 @@ #include "content/public/browser/resource_request_info.h" #include "net/base/completion_once_callback.h" #include "qwebengineurlrequestinfo.h" +#include class GURL; @@ -52,6 +53,9 @@ class URLRequest; namespace QtWebEngineCore { +class ProfileAdapter; +class ProfileIoDataQt; + // Notifies WebContentsAdapterClient of a new URLRequest. class URLRequestNotification { public: @@ -60,7 +64,8 @@ public: GURL *newUrl, QWebEngineUrlRequestInfo &&requestInfo, content::ResourceRequestInfo::WebContentsGetter webContentsGetter, - net::CompletionOnceCallback callback); + net::CompletionOnceCallback callback, + QPointer adapter); ~URLRequestNotification() = default; void cancel(); void notify(); @@ -74,6 +79,7 @@ private: QWebEngineUrlRequestInfo m_requestInfo; content::ResourceRequestInfo::WebContentsGetter m_webContentsGetter; net::CompletionOnceCallback m_callback; + QPointer m_profileAdapter; }; } #endif diff --git a/src/core/profile_io_data_qt.cpp b/src/core/profile_io_data_qt.cpp index 55d3bba5a..dc5b6624e 100644 --- a/src/core/profile_io_data_qt.cpp +++ b/src/core/profile_io_data_qt.cpp @@ -195,6 +195,11 @@ ProfileIODataQt::~ProfileIODataQt() delete m_proxyConfigService.fetchAndStoreAcquire(0); } +QPointer ProfileIODataQt::profileAdapter() +{ + return m_profileAdapter; +} + void ProfileIODataQt::shutdownOnUIThread() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); @@ -741,6 +746,11 @@ QWebEngineUrlRequestInterceptor *ProfileIODataQt::acquireInterceptor() return m_requestInterceptor; } +QWebEngineUrlRequestInterceptor *ProfileIODataQt::requestInterceptor() +{ + return m_requestInterceptor; +} + bool ProfileIODataQt::hasPageInterceptors() { // used in NetworkDelegateQt::OnBeforeURLRequest diff --git a/src/core/profile_io_data_qt.h b/src/core/profile_io_data_qt.h index bcf49e22b..f3460935a 100644 --- a/src/core/profile_io_data_qt.h +++ b/src/core/profile_io_data_qt.h @@ -75,6 +75,7 @@ public: ProfileIODataQt(ProfileQt *profile); // runs on ui thread virtual ~ProfileIODataQt(); + QPointer profileAdapter(); content::ResourceContext *resourceContext(); net::URLRequestContext *urlRequestContext(); void initializeOnIOThread(); @@ -96,6 +97,7 @@ public: // Used in NetworkDelegateQt::OnBeforeURLRequest. QWebEngineUrlRequestInterceptor *acquireInterceptor(); void releaseInterceptor(); + QWebEngineUrlRequestInterceptor *requestInterceptor(); void setRequestContextData(content::ProtocolHandlerMap *protocolHandlers, content::URLRequestInterceptorScopedVector request_interceptors); -- cgit v1.2.3