From 5b5b7d9a5f922ef5fa99cfac41da348e23970032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= <juri.valdmann@qt.io> Date: Mon, 3 Sep 2018 10:25:04 +0200 Subject: [PATCH] Improve QWebEngineUrlScheme API Following feedback from 5.12 API review: - Use enum class for Syntax - Add Q_FLAG for Flags - Mark constructor from name as explicit - Rename Secure to SecureScheme - Rename Local to LocalScheme - Rename addScheme to registerScheme - Rename findScheme to schemeByName Task-number: QTBUG-70247 Change-Id: Iae332c8d9843349506e8a4b07d70f0d234597375 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io> --- .../webenginewidgets/webui/doc/src/webui.qdoc | 8 ++-- .../webenginewidgets/webui/webuihandler.cpp | 6 +-- src/core/api/qwebengineurlscheme.cpp | 44 +++++++++---------- src/core/api/qwebengineurlscheme.h | 23 +++++----- src/core/web_engine_context.cpp | 4 +- src/webengine/api/qquickwebengineprofile.cpp | 2 +- .../api/qwebengineprofile.cpp | 2 +- tests/auto/widgets/origins/tst_origins.cpp | 38 ++++++++-------- 8 files changed, 65 insertions(+), 62 deletions(-) diff --git a/examples/webenginewidgets/webui/doc/src/webui.qdoc b/examples/webenginewidgets/webui/doc/src/webui.qdoc index d5eb13d02..47d9fbad1 100644 --- a/examples/webenginewidgets/webui/doc/src/webui.qdoc +++ b/examples/webenginewidgets/webui/doc/src/webui.qdoc @@ -84,7 +84,7 @@ In order to take advantage of these possibilities, the custom scheme must first be registered. This means creating and configuring a \l {QWebEngineUrlScheme} object and then handing it over to \l - {QWebEngineUrlScheme::addScheme()}. The example program does exactly this in + {QWebEngineUrlScheme::registerScheme()}. The example program does exactly this in the static method \c {WebUiHandler::registerUrlScheme()}: \quotefromfile webenginewidgets/webui/webuihandler.cpp @@ -95,11 +95,11 @@ the constructor of \c {QWebEngineUrlScheme} or by calling \l {QWebEngineUrlScheme::setName}. In the above, the name \c {webui} is set through the constructor. Additionally, we activate the flags \l - {QWebEngineUrlScheme::Secure}{Secure}, \l - {QWebEngineUrlScheme::Local}{Local} and \l + {QWebEngineUrlScheme::SecureScheme}{SecureScheme}, \l + {QWebEngineUrlScheme::LocalScheme}{LocalScheme} and \l {QWebEngineUrlScheme::LocalAccessAllowed}{LocalAccessAllowed}. Since our custom scheme handler will not deliver resources received from insecure - network connections, we can safely mark it as \c {Secure}. The \c {Local} + network connections, we can safely mark it as a \c {SecureScheme}. The \c {LocalScheme} flag prevents content from non-local schemes (such as \c {http}) from interacting with our custom scheme. Without this flag it would be possible, for example, to embed the \c {webui:about} page in an \c <iframe> element on diff --git a/examples/webenginewidgets/webui/webuihandler.cpp b/examples/webenginewidgets/webui/webuihandler.cpp index 246af9d07..63c249368 100644 --- a/examples/webenginewidgets/webui/webuihandler.cpp +++ b/examples/webenginewidgets/webui/webuihandler.cpp @@ -91,8 +91,8 @@ void WebUiHandler::requestStarted(QWebEngineUrlRequestJob *job) void WebUiHandler::registerUrlScheme() { QWebEngineUrlScheme webUiScheme(schemeName); - webUiScheme.setFlags(QWebEngineUrlScheme::Secure | - QWebEngineUrlScheme::Local | + webUiScheme.setFlags(QWebEngineUrlScheme::SecureScheme | + QWebEngineUrlScheme::LocalScheme | QWebEngineUrlScheme::LocalAccessAllowed); - QWebEngineUrlScheme::addScheme(webUiScheme); + QWebEngineUrlScheme::registerScheme(webUiScheme); } diff --git a/src/core/api/qwebengineurlscheme.cpp b/src/core/api/qwebengineurlscheme.cpp index 59899769b..f36f3335b 100644 --- a/src/core/api/qwebengineurlscheme.cpp +++ b/src/core/api/qwebengineurlscheme.cpp @@ -43,16 +43,16 @@ QT_BEGIN_NAMESPACE -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::PathSyntax, url::SCHEME_WITHOUT_AUTHORITY) -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::HostSyntax, url::SCHEME_WITH_HOST) -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::HostAndPortSyntax, url::SCHEME_WITH_HOST_AND_PORT) -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::HostPortAndUserInformationSyntax, +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::Syntax::Path, url::SCHEME_WITHOUT_AUTHORITY) +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::Syntax::Host, url::SCHEME_WITH_HOST) +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::Syntax::HostAndPort, url::SCHEME_WITH_HOST_AND_PORT) +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::Syntax::HostPortAndUserInformation, url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION) -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::PortUnspecified, url::PORT_UNSPECIFIED); +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::PortUnspecified, url::PORT_UNSPECIFIED) -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::Secure, url::CustomScheme::Secure) -ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::Local, url::CustomScheme::Local) +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::SecureScheme, url::CustomScheme::Secure) +ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::LocalScheme, url::CustomScheme::Local) ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::LocalAccessAllowed, url::CustomScheme::LocalAccessAllowed) ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::NoAccessAllowed, url::CustomScheme::NoAccessAllowed) ASSERT_ENUMS_MATCH(QWebEngineUrlScheme::ServiceWorkersAllowed, url::CustomScheme::ServiceWorkersAllowed) @@ -91,10 +91,10 @@ public: int main(int argc, char **argv) { QWebEngineUrlScheme scheme("myscheme"); - scheme.setSyntax(QWebEngineUrlScheme::HostAndPortSyntax); + scheme.setSyntax(QWebEngineUrlScheme::Syntax::HostAndPort); scheme.setDefaultPort(2345); - scheme.setFlags(QWebEngineUrlScheme::Secure); - QWebEngineUrlScheme::addScheme(scheme); + scheme.setFlags(QWebEngineUrlScheme::SecureScheme); + QWebEngineUrlScheme::registerScheme(scheme); ... } \endcode @@ -113,25 +113,25 @@ public: To apply the same-origin policy to a custom URL scheme, WebEngine must be able to compute the origin (host and port combination) of a URL. The \c {Host...} options indicate that the URL scheme conforms to the standard URL syntax (like - \c http) and automatically enable the same-origin policy. The \c {PathSyntax} + \c http) and automatically enable the same-origin policy. The \c {Path} option indicates that the URL scheme uses a non-standard syntax and that the same-origin policy cannot be applied. - \value HostPortAndUserInformationSyntax + \value HostPortAndUserInformation The authority component of a URL of this type has all of the standard elements: host, port, user name, and password. A URL without a port will use the \l defaultPort (which \e must not be \l PortUnspecified). - \value HostAndPortSyntax + \value HostAndPort The authority component of a URL of this type has only the host and port elements. A URL without a port will use the \l defaultPort (which \e must not be \l PortUnspecified). - \value HostSyntax + \value Host The authority component of a URL of this type has only the host part and no port. The \l defaultPort \e must be set to \l PortUnspecified. - \value PathSyntax + \value Path A URL of this type has no authority component at all. Everything after scheme name and separator character (:) will be preserved as is without validation or canonicalization. All URLs of such a scheme will be considered as having @@ -152,7 +152,7 @@ public: This enum type specifies security options that should apply to a URL scheme. - \value Secure + \value SecureScheme Indicates that the URL scheme is \l{https://www.w3.org/TR/powerful-features/#is-origin-trustworthy}{potentially trustworthy}. This flag should only be applied to URL schemes which ensure @@ -161,7 +161,7 @@ public: (authenticated and encrypted) and \c qrc (local resources only), whereas \c http is an example of an insecure scheme. - \value Local + \value LocalScheme Indicates that the URL scheme provides access to local resources. The purpose of this flag is to prevent network content from accessing local resources. Only schemes with the \c LocalAccessAllowed flag may load resources from a @@ -279,7 +279,7 @@ void QWebEngineUrlScheme::setName(const QByteArray &newValue) /*! Returns the syntax type of this URL scheme. - The default value is \c PathSyntax. + The default value is \c Path. \sa Syntax, setSyntax() */ @@ -351,9 +351,9 @@ void QWebEngineUrlScheme::setFlags(Flags newValue) \warning This function must be called early at application startup, before creating any WebEngine classes. Late calls will be ignored. - \sa findScheme() + \sa schemeByName() */ -void QWebEngineUrlScheme::addScheme(const QWebEngineUrlScheme &scheme) +void QWebEngineUrlScheme::registerScheme(const QWebEngineUrlScheme &scheme) { url::CustomScheme::AddScheme(*scheme.d); } @@ -362,9 +362,9 @@ void QWebEngineUrlScheme::addScheme(const QWebEngineUrlScheme &scheme) Returns the web engine URL scheme with the given \a name or the default-constructed scheme. - \sa addScheme() + \sa registerScheme() */ -QWebEngineUrlScheme QWebEngineUrlScheme::findScheme(const QByteArray &name) +QWebEngineUrlScheme QWebEngineUrlScheme::schemeByName(const QByteArray &name) { base::StringPiece namePiece{name.data(), static_cast<size_t>(name.size())}; if (const url::CustomScheme *cs = url::CustomScheme::FindScheme(namePiece)) diff --git a/src/core/api/qwebengineurlscheme.h b/src/core/api/qwebengineurlscheme.h index dd936bd9d..88a8f5065 100644 --- a/src/core/api/qwebengineurlscheme.h +++ b/src/core/api/qwebengineurlscheme.h @@ -43,6 +43,7 @@ #include <QtWebEngineCore/qtwebenginecoreglobal.h> #include <QtCore/qbytearray.h> +#include <QtCore/qobjectdefs.h> #include <QtCore/qshareddata.h> QT_BEGIN_NAMESPACE @@ -50,12 +51,13 @@ QT_BEGIN_NAMESPACE class QWebEngineUrlSchemePrivate; class QWEBENGINECORE_EXPORT QWebEngineUrlScheme { + Q_GADGET public: - enum Syntax { - HostPortAndUserInformationSyntax, - HostAndPortSyntax, - HostSyntax, - PathSyntax, + enum class Syntax { + HostPortAndUserInformation, + HostAndPort, + Host, + Path, }; enum SpecialPort { @@ -63,8 +65,8 @@ public: }; enum Flag { - Secure = 0x1, - Local = 0x2, + SecureScheme = 0x1, + LocalScheme = 0x2, LocalAccessAllowed = 0x4, NoAccessAllowed = 0x8, ServiceWorkersAllowed = 0x10, @@ -72,9 +74,10 @@ public: ContentSecurityPolicyIgnored = 0x40, }; Q_DECLARE_FLAGS(Flags, Flag) + Q_FLAG(Flags) QWebEngineUrlScheme(); - QWebEngineUrlScheme(const QByteArray &name); + explicit QWebEngineUrlScheme(const QByteArray &name); QWebEngineUrlScheme(const QWebEngineUrlScheme &that); QWebEngineUrlScheme &operator=(const QWebEngineUrlScheme &that); @@ -99,8 +102,8 @@ public: Flags flags() const; void setFlags(Flags newValue); - static void addScheme(const QWebEngineUrlScheme &scheme); - static QWebEngineUrlScheme findScheme(const QByteArray &name); + static void registerScheme(const QWebEngineUrlScheme &scheme); + static QWebEngineUrlScheme schemeByName(const QByteArray &name); private: QWebEngineUrlScheme(QWebEngineUrlSchemePrivate *d); diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp index 899e50e16..aba23b622 100644 --- a/src/core/web_engine_context.cpp +++ b/src/core/web_engine_context.cpp @@ -329,10 +329,10 @@ WebEngineContext::WebEngineContext() #endif QWebEngineUrlScheme qrcScheme(QByteArrayLiteral("qrc")); - qrcScheme.setFlags(QWebEngineUrlScheme::Secure + qrcScheme.setFlags(QWebEngineUrlScheme::SecureScheme | QWebEngineUrlScheme::LocalAccessAllowed | QWebEngineUrlScheme::ViewSourceAllowed); - QWebEngineUrlScheme::addScheme(qrcScheme); + QWebEngineUrlScheme::registerScheme(qrcScheme); // Allow us to inject javascript like any webview toolkit. content::RenderFrameHost::AllowInjectingJavaScriptForAndroidWebView(); diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index 02e15454b..f536695ef 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -836,7 +836,7 @@ static bool checkInternalScheme(const QByteArray &scheme) Registers a handler \a handler for custom URL scheme \a scheme in the profile. It is recommended to first register the scheme with \l - QWebEngineUrlScheme::addScheme at application startup. + QWebEngineUrlScheme::registerScheme at application startup. */ void QQuickWebEngineProfile::installUrlSchemeHandler(const QByteArray &scheme, QWebEngineUrlSchemeHandler *handler) { diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index b66a1aa53..b2fe49da3 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -682,7 +682,7 @@ static bool checkInternalScheme(const QByteArray &scheme) Registers a handler \a handler for custom URL scheme \a scheme in the profile. It is recommended to first register the scheme with \l - QWebEngineUrlScheme::addScheme at application startup. + QWebEngineUrlScheme::registerScheme at application startup. */ void QWebEngineProfile::installUrlSchemeHandler(const QByteArray &scheme, QWebEngineUrlSchemeHandler *handler) { diff --git a/tests/auto/widgets/origins/tst_origins.cpp b/tests/auto/widgets/origins/tst_origins.cpp index 31d16adff..d867ff98d 100644 --- a/tests/auto/widgets/origins/tst_origins.cpp +++ b/tests/auto/widgets/origins/tst_origins.cpp @@ -51,76 +51,76 @@ void registerSchemes() { { QWebEngineUrlScheme scheme(QBAL("PathSyntax")); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-Secure")); - scheme.setFlags(QWebEngineUrlScheme::Secure); - QWebEngineUrlScheme::addScheme(scheme); + scheme.setFlags(QWebEngineUrlScheme::SecureScheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-Secure-ServiceWorkersAllowed")); - scheme.setFlags(QWebEngineUrlScheme::Secure | QWebEngineUrlScheme::ServiceWorkersAllowed); - QWebEngineUrlScheme::addScheme(scheme); + scheme.setFlags(QWebEngineUrlScheme::SecureScheme | QWebEngineUrlScheme::ServiceWorkersAllowed); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-Local")); - scheme.setFlags(QWebEngineUrlScheme::Local); - QWebEngineUrlScheme::addScheme(scheme); + scheme.setFlags(QWebEngineUrlScheme::LocalScheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-LocalAccessAllowed")); scheme.setFlags(QWebEngineUrlScheme::LocalAccessAllowed); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-NoAccessAllowed")); scheme.setFlags(QWebEngineUrlScheme::NoAccessAllowed); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-ServiceWorkersAllowed")); scheme.setFlags(QWebEngineUrlScheme::ServiceWorkersAllowed); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("PathSyntax-ViewSourceAllowed")); scheme.setFlags(QWebEngineUrlScheme::ViewSourceAllowed); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("HostSyntax")); - scheme.setSyntax(QWebEngineUrlScheme::HostSyntax); - QWebEngineUrlScheme::addScheme(scheme); + scheme.setSyntax(QWebEngineUrlScheme::Syntax::Host); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("HostSyntax-ContentSecurityPolicyIgnored")); - scheme.setSyntax(QWebEngineUrlScheme::HostSyntax); + scheme.setSyntax(QWebEngineUrlScheme::Syntax::Host); scheme.setFlags(QWebEngineUrlScheme::ContentSecurityPolicyIgnored); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("HostAndPortSyntax")); - scheme.setSyntax(QWebEngineUrlScheme::HostAndPortSyntax); + scheme.setSyntax(QWebEngineUrlScheme::Syntax::HostAndPort); scheme.setDefaultPort(42); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } { QWebEngineUrlScheme scheme(QBAL("HostPortAndUserInformationSyntax")); - scheme.setSyntax(QWebEngineUrlScheme::HostPortAndUserInformationSyntax); + scheme.setSyntax(QWebEngineUrlScheme::Syntax::HostPortAndUserInformation); scheme.setDefaultPort(42); - QWebEngineUrlScheme::addScheme(scheme); + QWebEngineUrlScheme::registerScheme(scheme); } } Q_CONSTRUCTOR_FUNCTION(registerSchemes) -- GitLab