From 67e32cc1f0e9dd6afd949510185714fe724691d3 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 31 Dec 2015 14:14:48 -0800 Subject: [PATCH] Consolidating URL handling, exposing to all QML objects --- interface/resources/qml/QmlWebWindow.qml | 24 +++++++++++++----------- libraries/ui/src/OffscreenUi.cpp | 22 +++++++++++++++++----- libraries/ui/src/QmlWebWindowClass.cpp | 15 ++++----------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/interface/resources/qml/QmlWebWindow.qml b/interface/resources/qml/QmlWebWindow.qml index 22ff5708dc..c2076c93e3 100644 --- a/interface/resources/qml/QmlWebWindow.qml +++ b/interface/resources/qml/QmlWebWindow.qml @@ -30,16 +30,6 @@ VrDialog { console.log("Web Window JS message: " + sourceID + " " + lineNumber + " " + message); }); - // Required to support clicking on "hifi://" links - webview.loadingChanged.connect(handleWebviewLoading) - } - - // Required to support clicking on "hifi://" links - function handleWebviewLoading(loadRequest) { - if (WebEngineView.LoadStartedStatus == loadRequest.status) { - var newUrl = loadRequest.url.toString(); - root.navigating(newUrl) - } } Item { @@ -59,11 +49,23 @@ VrDialog { onUrlChanged: { var currentUrl = url.toString(); - var newUrl = urlFixer.fixupUrl(currentUrl); + var newUrl = urlHandler.fixupUrl(currentUrl); if (newUrl != currentUrl) { url = newUrl; } } + + onLoadingChanged: { + // Required to support clicking on "hifi://" links + if (WebEngineView.LoadStartedStatus == loadRequest.status) { + var url = loadRequest.url.toString(); + if (urlHandler.canHandleUrl(url)) { + if (urlHandler.handleUrl(url)) { + webview.stop(); + } + } + } + } profile: WebEngineProfile { id: webviewProfile diff --git a/libraries/ui/src/OffscreenUi.cpp b/libraries/ui/src/OffscreenUi.cpp index 572b3c018e..db5a9a6009 100644 --- a/libraries/ui/src/OffscreenUi.cpp +++ b/libraries/ui/src/OffscreenUi.cpp @@ -13,7 +13,9 @@ #include #include +#include #include + #include "ErrorDialog.h" #include "MessageDialog.h" @@ -50,10 +52,20 @@ private: bool _navigationFocused { false }; }; - -class UrlFixer : public QObject { +class UrlHandler : public QObject { Q_OBJECT public: + Q_INVOKABLE bool canHandleUrl(const QString& url) { + static auto handler = dynamic_cast(qApp); + return handler->canAcceptURL(url); + } + + Q_INVOKABLE bool handleUrl(const QString& url) { + static auto handler = dynamic_cast(qApp); + return handler->acceptURL(url); + } + + // FIXME hack for authentication, remove when we migrate to Qt 5.6 Q_INVOKABLE QString fixupUrl(const QString& originalUrl) { static const QString ACCESS_TOKEN_PARAMETER = "access_token"; static const QString ALLOWED_HOST = "metaverse.highfidelity.com"; @@ -72,7 +84,7 @@ public: } }; -static UrlFixer * urlFixer { nullptr }; +static UrlHandler * urlHandler { nullptr }; static OffscreenFlags* offscreenFlags { nullptr }; // This hack allows the QML UI to work with keys that are also bound as @@ -110,8 +122,8 @@ void OffscreenUi::create(QOpenGLContext* context) { offscreenFlags = new OffscreenFlags(); rootContext->setContextProperty("offscreenFlags", offscreenFlags); - urlFixer = new UrlFixer(); - rootContext->setContextProperty("urlFixer", urlFixer); + urlHandler = new UrlHandler(); + rootContext->setContextProperty("urlHandler", urlHandler); } void OffscreenUi::show(const QUrl& url, const QString& name, std::function f) { diff --git a/libraries/ui/src/QmlWebWindowClass.cpp b/libraries/ui/src/QmlWebWindowClass.cpp index 7e78a43879..940ba121f3 100644 --- a/libraries/ui/src/QmlWebWindowClass.cpp +++ b/libraries/ui/src/QmlWebWindowClass.cpp @@ -27,7 +27,6 @@ #include "OffscreenUi.h" static const char* const URL_PROPERTY = "source"; -static const QRegExp HIFI_URL_PATTERN { "^hifi://" }; // Method called by Qt scripts to create a new web window in the overlay QScriptValue QmlWebWindowClass::constructor(QScriptContext* context, QScriptEngine* engine) { @@ -41,16 +40,10 @@ QmlWebWindowClass::QmlWebWindowClass(QObject* qmlWindow) : QmlWindowClass(qmlWin void QmlWebWindowClass::handleNavigation(const QString& url) { bool handled = false; - - if (url.contains(HIFI_URL_PATTERN)) { - DependencyManager::get()->handleLookupString(url); - handled = true; - } else { - static auto handler = dynamic_cast(qApp); - if (handler) { - if (handler->canAcceptURL(url)) { - handled = handler->acceptURL(url); - } + static auto handler = dynamic_cast(qApp); + if (handler) { + if (handler->canAcceptURL(url)) { + handled = handler->acceptURL(url); } }