From 691d4a008d90b7d69a7b4b05404475dae204848f Mon Sep 17 00:00:00 2001 From: Peter Varga Date: Wed, 26 Aug 2015 10:43:05 +0200 Subject: Add AuthenticationDialogController The new controller makes possible to handle authentication requests asynchronously. This is essential for the authentication support in the QtQuick API. Change-Id: Ib60b58448a60e817e64477529ec4bfd1535b3d19 Reviewed-by: Allan Sandfeld Jensen --- src/core/authentication_dialog_controller.cpp | 96 +++++++++++++++++++++++ src/core/authentication_dialog_controller.h | 71 +++++++++++++++++ src/core/authentication_dialog_controller_p.h | 56 +++++++++++++ src/core/core_gyp_generator.pro | 3 + src/core/resource_dispatcher_host_delegate_qt.cpp | 52 ++++++++---- src/core/resource_dispatcher_host_delegate_qt.h | 17 +++- src/core/web_contents_adapter_client.h | 3 +- src/webengine/api/qquickwebengineview_p_p.h | 2 +- src/webenginewidgets/api/qwebenginepage.cpp | 21 ++--- src/webenginewidgets/api/qwebenginepage_p.h | 2 +- 10 files changed, 292 insertions(+), 31 deletions(-) create mode 100644 src/core/authentication_dialog_controller.cpp create mode 100644 src/core/authentication_dialog_controller.h create mode 100644 src/core/authentication_dialog_controller_p.h diff --git a/src/core/authentication_dialog_controller.cpp b/src/core/authentication_dialog_controller.cpp new file mode 100644 index 000000000..db534a24e --- /dev/null +++ b/src/core/authentication_dialog_controller.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** 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 "authentication_dialog_controller.h" +#include "authentication_dialog_controller_p.h" + +#include "content/public/browser/browser_thread.h" + +namespace QtWebEngineCore { + +AuthenticationDialogControllerPrivate::AuthenticationDialogControllerPrivate(ResourceDispatcherHostLoginDelegateQt *loginDelegate) + : loginDelegate(loginDelegate) +{ +} + +void AuthenticationDialogControllerPrivate::dialogFinished(bool accepted, const QString &user, const QString &password) +{ + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, + base::Bind(&ResourceDispatcherHostLoginDelegateQt::sendAuthToRequester, loginDelegate, accepted, user, password)); +} + +AuthenticationDialogController::AuthenticationDialogController(AuthenticationDialogControllerPrivate *dd) +{ + Q_ASSERT(dd); + d.reset(dd); +} + +AuthenticationDialogController::~AuthenticationDialogController() +{ +} + +QUrl AuthenticationDialogController::url() const +{ + return d->loginDelegate->url(); +} + +QString AuthenticationDialogController::realm() const +{ + return d->loginDelegate->realm(); +} + +QString AuthenticationDialogController::host() const +{ + return d->loginDelegate->host(); +} + +bool AuthenticationDialogController::isProxy() const +{ + return d->loginDelegate->isProxy(); +} + +void AuthenticationDialogController::accept(const QString &user, const QString &password) +{ + d->dialogFinished(true, user, password); +} + +void AuthenticationDialogController::reject() +{ + d->dialogFinished(false); +} + +} // namespace QtWebEngineCore diff --git a/src/core/authentication_dialog_controller.h b/src/core/authentication_dialog_controller.h new file mode 100644 index 000000000..ae741f537 --- /dev/null +++ b/src/core/authentication_dialog_controller.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** 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 AUTHENTICATION_DIALOG_CONTROLLER_H +#define AUTHENTICATION_DIALOG_CONTROLLER_H + +#include "qtwebenginecoreglobal.h" + +#include + +namespace QtWebEngineCore { + +class AuthenticationDialogControllerPrivate; + +class QWEBENGINE_EXPORT AuthenticationDialogController : public QObject { + Q_OBJECT +public: + ~AuthenticationDialogController(); + + QUrl url() const; + QString realm() const; + QString host() const; + bool isProxy() const; + +public Q_SLOTS: + void accept(const QString &user, const QString &password); + void reject(); + +private: + AuthenticationDialogController(AuthenticationDialogControllerPrivate *); + + QScopedPointer d; + friend class ResourceDispatcherHostLoginDelegateQt; +}; + +} // namespace QtWebEngineCore + +#endif // AUTHENTICATION_DIALOG_CONTROLLER_H diff --git a/src/core/authentication_dialog_controller_p.h b/src/core/authentication_dialog_controller_p.h new file mode 100644 index 000000000..5b1d21ae0 --- /dev/null +++ b/src/core/authentication_dialog_controller_p.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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 AUTHENTICATION_DIALOG_CONTROLLER_P_H +#define AUTHENTICATION_DIALOG_CONTROLLER_P_H + +#include "base/memory/ref_counted.h" +#include "resource_dispatcher_host_delegate_qt.h" + +namespace QtWebEngineCore { + +class AuthenticationDialogControllerPrivate { + +public: + AuthenticationDialogControllerPrivate(ResourceDispatcherHostLoginDelegateQt *loginDelegate); + void dialogFinished(bool accepted, const QString &user = QString(), const QString &password = QString()); + + scoped_refptr loginDelegate; +}; + +} // namespace QtWebEngineCore + +#endif // AUTHENTICATION_DIALOG_CONTROLLER_H diff --git a/src/core/core_gyp_generator.pro b/src/core/core_gyp_generator.pro index 247ec482a..17d73ccfd 100644 --- a/src/core/core_gyp_generator.pro +++ b/src/core/core_gyp_generator.pro @@ -29,6 +29,7 @@ INCLUDEPATH += $$PWD $$PWD/api SOURCES = \ access_token_store_qt.cpp \ + authentication_dialog_controller.cpp \ browser_accessibility_manager_qt.cpp \ browser_accessibility_qt.cpp \ browser_context_adapter.cpp \ @@ -98,6 +99,8 @@ SOURCES = \ HEADERS = \ access_token_store_qt.h \ + authentication_dialog_controller_p.h \ + authentication_dialog_controller.h \ browser_accessibility_manager_qt.h \ browser_accessibility_qt.h \ browser_context_adapter.h \ diff --git a/src/core/resource_dispatcher_host_delegate_qt.cpp b/src/core/resource_dispatcher_host_delegate_qt.cpp index 43d3d98ef..b63ecd5c7 100644 --- a/src/core/resource_dispatcher_host_delegate_qt.cpp +++ b/src/core/resource_dispatcher_host_delegate_qt.cpp @@ -46,22 +46,20 @@ #include "content/public/browser/resource_request_info.h" #include "net/url_request/url_request.h" +#include "authentication_dialog_controller.h" +#include "authentication_dialog_controller_p.h" #include "type_conversion.h" #include "web_contents_view_qt.h" namespace QtWebEngineCore { ResourceDispatcherHostLoginDelegateQt::ResourceDispatcherHostLoginDelegateQt(net::AuthChallengeInfo *authInfo, net::URLRequest *request) - : m_request(request) + : m_authInfo(authInfo) + , m_request(request) { Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); content::ResourceRequestInfo::GetRenderFrameForRequest(request, &m_renderProcessId, &m_renderFrameId); - m_url = toQt(request->url()); - m_realm = QString::fromStdString(authInfo->realm); - m_isProxy = authInfo->is_proxy; - m_host = QString::fromStdString(authInfo->challenger.ToString()); - content::BrowserThread::PostTask( content::BrowserThread::UI, FROM_HERE, base::Bind(&ResourceDispatcherHostLoginDelegateQt::triggerDialog, this)); @@ -69,13 +67,34 @@ ResourceDispatcherHostLoginDelegateQt::ResourceDispatcherHostLoginDelegateQt(net ResourceDispatcherHostLoginDelegateQt::~ResourceDispatcherHostLoginDelegateQt() { + Q_ASSERT(m_dialogController.isNull()); // We must have called ClearLoginDelegateForRequest if we didn't receive an OnRequestCancelled. Q_ASSERT(!m_request); } void ResourceDispatcherHostLoginDelegateQt::OnRequestCancelled() { - m_request = 0; + destroy(); +} + +QUrl ResourceDispatcherHostLoginDelegateQt::url() const +{ + return toQt(m_request->url()); +} + +QString ResourceDispatcherHostLoginDelegateQt::realm() const +{ + return QString::fromStdString(m_authInfo->realm); +} + +QString ResourceDispatcherHostLoginDelegateQt::host() const +{ + return QString::fromStdString(m_authInfo->challenger.ToString()); +} + +bool ResourceDispatcherHostLoginDelegateQt::isProxy() const +{ + return m_authInfo->is_proxy; } void ResourceDispatcherHostLoginDelegateQt::triggerDialog() @@ -85,16 +104,9 @@ void ResourceDispatcherHostLoginDelegateQt::triggerDialog() content::WebContentsImpl *webContents = static_cast(content::WebContents::FromRenderViewHost(renderViewHost)); WebContentsAdapterClient *client = WebContentsViewQt::from(webContents->GetView())->client(); - // The widgets API will ask for credentials synchronouly, keep it simple for now. - // We'll have to figure out a mechanism to keep a ref to the ResourceDispatcherHostLoginDelegateQt - // to avoid crashing in the OnRequestCancelled case if we want to allow the credentials to - // come back asynchronously in the QtQuick API. - QString user, password; - bool success = client->authenticationRequired(m_url , m_realm , m_isProxy , m_host, &user, &password); - - content::BrowserThread::PostTask( - content::BrowserThread::IO, FROM_HERE, - base::Bind(&ResourceDispatcherHostLoginDelegateQt::sendAuthToRequester, this, success, user, password)); + AuthenticationDialogControllerPrivate *dialogControllerData = new AuthenticationDialogControllerPrivate(this); + m_dialogController.reset(new AuthenticationDialogController(dialogControllerData)); + client->authenticationRequired(m_dialogController); } void ResourceDispatcherHostLoginDelegateQt::sendAuthToRequester(bool success, const QString &user, const QString &password) @@ -109,6 +121,12 @@ void ResourceDispatcherHostLoginDelegateQt::sendAuthToRequester(bool success, co m_request->CancelAuth(); content::ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(m_request); + destroy(); +} + +void ResourceDispatcherHostLoginDelegateQt::destroy() +{ + m_dialogController.reset(); m_request = 0; } diff --git a/src/core/resource_dispatcher_host_delegate_qt.h b/src/core/resource_dispatcher_host_delegate_qt.h index 2cba210d3..d62292995 100644 --- a/src/core/resource_dispatcher_host_delegate_qt.h +++ b/src/core/resource_dispatcher_host_delegate_qt.h @@ -44,6 +44,8 @@ namespace QtWebEngineCore { +class AuthenticationDialogController; + class ResourceDispatcherHostLoginDelegateQt : public content::ResourceDispatcherHostLoginDelegate { public: ResourceDispatcherHostLoginDelegateQt(net::AuthChallengeInfo *authInfo, net::URLRequest *request); @@ -52,9 +54,16 @@ public: // ResourceDispatcherHostLoginDelegate implementation virtual void OnRequestCancelled(); + QUrl url() const; + QString realm() const; + QString host() const; + bool isProxy() const; + + void sendAuthToRequester(bool success, const QString &user, const QString &password); + private: void triggerDialog(); - void sendAuthToRequester(bool success, const QString &user, const QString &password); + void destroy(); QUrl m_url; QString m_realm; @@ -64,9 +73,15 @@ private: int m_renderProcessId; int m_renderFrameId; + net::AuthChallengeInfo *m_authInfo; + // The request that wants login data. // Must only be accessed on the IO thread. net::URLRequest *m_request; + + // This member is used to keep authentication dialog controller alive until + // authorization is sent or cancelled. + QSharedPointer m_dialogController; }; class ResourceDispatcherHostDelegateQt : public content::ResourceDispatcherHostDelegate { diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h index 7a99b6ad4..8166224a4 100644 --- a/src/core/web_contents_adapter_client.h +++ b/src/core/web_contents_adapter_client.h @@ -52,6 +52,7 @@ QT_FORWARD_DECLARE_CLASS(CertificateErrorController) namespace QtWebEngineCore { +class AuthenticationDialogController; class BrowserContextAdapter; class FilePickerController; class JavaScriptDialogController; @@ -216,7 +217,7 @@ public: virtual QObject *accessibilityParentObject() = 0; #endif // QT_NO_ACCESSIBILITY virtual void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) = 0; - virtual bool authenticationRequired(const QUrl &requestUrl, const QString &realm, bool isProxy, const QString &challengingHost, QString *outUser, QString *outPassword) = 0; + virtual void authenticationRequired(QSharedPointer) = 0; virtual void runGeolocationPermissionRequest(const QUrl &securityOrigin) = 0; virtual void runMediaAccessPermissionRequest(const QUrl &securityOrigin, MediaRequestFlags requestFlags) = 0; virtual void runMouseLockPermissionRequest(const QUrl &securityOrigin) = 0; diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h index 71528c020..6db6b199e 100644 --- a/src/webengine/api/qquickwebengineview_p_p.h +++ b/src/webengine/api/qquickwebengineview_p_p.h @@ -155,7 +155,7 @@ public: virtual void didFindText(quint64, int) Q_DECL_OVERRIDE; virtual void passOnFocus(bool reverse) Q_DECL_OVERRIDE; virtual void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) Q_DECL_OVERRIDE; - virtual bool authenticationRequired(const QUrl&, const QString&, bool, const QString&, QString*, QString*) Q_DECL_OVERRIDE { return false; } + virtual void authenticationRequired(QSharedPointer) Q_DECL_OVERRIDE { } virtual void runMediaAccessPermissionRequest(const QUrl &securityOrigin, MediaRequestFlags requestFlags) Q_DECL_OVERRIDE; virtual void runMouseLockPermissionRequest(const QUrl &securityOrigin) Q_DECL_OVERRIDE; #ifndef QT_NO_ACCESSIBILITY diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index a4cc0ac12..bd3b2772a 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -23,6 +23,7 @@ #include "qwebenginepage.h" #include "qwebenginepage_p.h" +#include "authentication_dialog_controller.h" #include "browser_context_adapter.h" #include "certificate_error_controller.h" #include "file_picker_controller.h" @@ -249,24 +250,24 @@ void QWebEnginePagePrivate::passOnFocus(bool reverse) view->focusNextPrevChild(!reverse); } -bool QWebEnginePagePrivate::authenticationRequired(const QUrl &requestUrl, const QString &realm, bool isProxy, const QString &challengingHost, QString *outUser, QString *outPassword) +void QWebEnginePagePrivate::authenticationRequired(QSharedPointer controller) { Q_Q(QWebEnginePage); QAuthenticator networkAuth; - networkAuth.setRealm(realm); + networkAuth.setRealm(controller->realm()); - if (isProxy) - Q_EMIT q->proxyAuthenticationRequired(requestUrl, &networkAuth, challengingHost); + if (controller->isProxy()) + Q_EMIT q->proxyAuthenticationRequired(controller->url(), &networkAuth, controller->host()); else - Q_EMIT q->authenticationRequired(requestUrl, &networkAuth); + Q_EMIT q->authenticationRequired(controller->url(), &networkAuth); // Authentication has been cancelled - if (networkAuth.isNull()) - return false; + if (networkAuth.isNull()) { + controller->reject(); + return; + } - *outUser = networkAuth.user(); - *outPassword = networkAuth.password(); - return true; + controller->accept(networkAuth.user(), networkAuth.password()); } void QWebEnginePagePrivate::runMediaAccessPermissionRequest(const QUrl &securityOrigin, WebContentsAdapterClient::MediaRequestFlags requestFlags) diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h index 1d2bc0344..a72debee2 100644 --- a/src/webenginewidgets/api/qwebenginepage_p.h +++ b/src/webenginewidgets/api/qwebenginepage_p.h @@ -107,7 +107,7 @@ public: virtual void didFindText(quint64 requestId, int matchCount) Q_DECL_OVERRIDE; virtual void passOnFocus(bool reverse) Q_DECL_OVERRIDE; virtual void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) Q_DECL_OVERRIDE; - virtual bool authenticationRequired(const QUrl &requestUrl, const QString &realm, bool isProxy, const QString &challengingHost, QString *outUser, QString *outPassword) Q_DECL_OVERRIDE; + virtual void authenticationRequired(QSharedPointer) Q_DECL_OVERRIDE; virtual void runMediaAccessPermissionRequest(const QUrl &securityOrigin, MediaRequestFlags requestFlags) Q_DECL_OVERRIDE; virtual void runGeolocationPermissionRequest(const QUrl &securityOrigin) Q_DECL_OVERRIDE; virtual void runMouseLockPermissionRequest(const QUrl &securityOrigin) Q_DECL_OVERRIDE; -- cgit v1.2.3