Consolidating URL handling, exposing to all QML objects

This commit is contained in:
Brad Davis 2015-12-31 14:14:48 -08:00
parent 462cc325e5
commit 67e32cc1f0
3 changed files with 34 additions and 27 deletions

View file

@ -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

View file

@ -13,7 +13,9 @@
#include <QtQml/QtQml>
#include <QtQuick/QQuickWindow>
#include <AbstractUriHandler.h>
#include <AccountManager.h>
#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<AbstractUriHandler*>(qApp);
return handler->canAcceptURL(url);
}
Q_INVOKABLE bool handleUrl(const QString& url) {
static auto handler = dynamic_cast<AbstractUriHandler*>(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<void(QQmlContext*, QObject*)> f) {

View file

@ -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<AddressManager>()->handleLookupString(url);
handled = true;
} else {
static auto handler = dynamic_cast<AbstractUriHandler*>(qApp);
if (handler) {
if (handler->canAcceptURL(url)) {
handled = handler->acceptURL(url);
}
static auto handler = dynamic_cast<AbstractUriHandler*>(qApp);
if (handler) {
if (handler->canAcceptURL(url)) {
handled = handler->acceptURL(url);
}
}