diff options
author | Christian Strømme <christian.stromme@theqtcompany.com> | 2016-01-28 19:00:40 +0100 |
---|---|---|
committer | Christian Stromme <christian.stromme@theqtcompany.com> | 2016-03-04 10:34:15 +0000 |
commit | f014893e2d19ae51c2f9138ab66eccf7684000ee (patch) | |
tree | fdd4d6749d8af691dfdf687ee055650f0d183d83 /src | |
parent | 10134cf8a9e0c89a016e1d31fd70377f174ab693 (diff) |
Add a proper backend for WebEngine
This removes the need for the awkward type re-registering and renaming
that was done in the Qml plugin, to support QtWebEngine.
Among other issues, the main motivation for this change is to unify the
implementations, and make it possible to run the auto tests that
interfaces directly with the QQuickWebView class. Having an actual
backend for Qt WebEngine means we can get rid of the, non-functional,
default implementation as well.
Change-Id: Ia93611ed2755c92207ca86ba3890ac4c2c4d72e9
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/imports/imports.pro | 11 | ||||
-rw-r--r-- | src/imports/webview.cpp | 15 | ||||
-rw-r--r-- | src/webview/qquickwebview.cpp | 5 | ||||
-rw-r--r-- | src/webview/qquickwebview_p.h | 3 | ||||
-rw-r--r-- | src/webview/qwebview_default.cpp | 45 | ||||
-rw-r--r-- | src/webview/qwebview_webengine.cpp | 259 | ||||
-rw-r--r-- | src/webview/qwebview_webengine_p.h | 108 | ||||
-rw-r--r-- | src/webview/webview-lib.pri | 9 |
8 files changed, 384 insertions, 71 deletions
diff --git a/src/imports/imports.pro b/src/imports/imports.pro index 8686784..197f25b 100644 --- a/src/imports/imports.pro +++ b/src/imports/imports.pro @@ -3,19 +3,10 @@ TARGET = declarative_webview TARGETPATH = QtWebView IMPORT_VERSION = 1.0 -QT += qml quick +QT += qml quick webview-private SOURCES += \ $$PWD/webview.cpp -android|ios|winrt|osx_webview_experimental { - QT += webview-private -} else:qtHaveModule(webengine) { - QT += webengine webengine-private - DEFINES += QT_WEBVIEW_WEBENGINE_BACKEND -} else { - error(No WebView backend found!) -} - load(qml_plugin) OTHER_FILES += qmldir diff --git a/src/imports/webview.cpp b/src/imports/webview.cpp index b30ad77..22c80a1 100644 --- a/src/imports/webview.cpp +++ b/src/imports/webview.cpp @@ -37,14 +37,8 @@ #include <QtQml/qqmlextensionplugin.h> #include <QtQml/qqml.h> - -#if defined(QT_WEBVIEW_WEBENGINE_BACKEND) -# include <QtWebEngine/private/qquickwebengineloadrequest_p.h> -# include <QtWebEngine/private/qquickwebengineview_p.h> -#else -# include <QtWebView/private/qquickwebviewloadrequest_p.h> -# include <QtWebView/private/qquickwebview_p.h> -#endif // QT_WEBVIEW_WEBENGINE_BACKEND +#include <QtWebView/private/qquickwebviewloadrequest_p.h> +#include <QtWebView/private/qquickwebview_p.h> QT_BEGIN_NAMESPACE @@ -59,14 +53,9 @@ public: // @uri QtWebView const QString &msg = QObject::tr("Cannot create separate instance of WebViewLoadRequest"); -#if defined(QT_WEBVIEW_WEBENGINE_BACKEND) - qmlRegisterType<QQuickWebEngineView>(uri, 1, 0, "WebView"); - qmlRegisterUncreatableType<QQuickWebEngineLoadRequest>(uri, 1, 1, "WebViewLoadRequest", msg); -#else qmlRegisterType<QQuickWebView>(uri, 1, 0, "WebView"); qmlRegisterType<QQuickWebView, 1>(uri, 1, 1, "WebView"); qmlRegisterUncreatableType<QQuickWebViewLoadRequest>(uri, 1, 1, "WebViewLoadRequest", msg); -#endif } void initializeEngine(QQmlEngine *engine, const char *uri) diff --git a/src/webview/qquickwebview.cpp b/src/webview/qquickwebview.cpp index ce189db..1729f83 100644 --- a/src/webview/qquickwebview.cpp +++ b/src/webview/qquickwebview.cpp @@ -317,3 +317,8 @@ void QQuickWebView::onLoadingChanged(const QWebViewLoadRequestPrivate &loadReque QQuickWebViewLoadRequest qqLoadRequest(loadRequest); Q_EMIT loadingChanged(&qqLoadRequest); } + +QJSValue QQuickWebView::takeCallback(int id) +{ + return callbacks->takeCallback(id); +} diff --git a/src/webview/qquickwebview_p.h b/src/webview/qquickwebview_p.h index 236a06e..3d1a432 100644 --- a/src/webview/qquickwebview_p.h +++ b/src/webview/qquickwebview_p.h @@ -112,6 +112,9 @@ private Q_SLOTS: void onLoadingChanged(const QWebViewLoadRequestPrivate &loadRequest); private: + friend class QWebEngineWebViewPrivate; + static QJSValue takeCallback(int id); + QWebView* m_webView; }; diff --git a/src/webview/qwebview_default.cpp b/src/webview/qwebview_default.cpp deleted file mode 100644 index 5f56b6a..0000000 --- a/src/webview/qwebview_default.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtWebView module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 "qwebview_global.h" - -QT_BEGIN_NAMESPACE - -struct QWebView {}; -struct Q_WEBVIEW_EXPORT QWebViewPrivate { static QWebViewPrivate *create(QWebView *q); }; -QWebViewPrivate *QWebViewPrivate::create(QWebView *) { return 0; } - -QT_END_NAMESPACE diff --git a/src/webview/qwebview_webengine.cpp b/src/webview/qwebview_webengine.cpp new file mode 100644 index 0000000..ca782bb --- /dev/null +++ b/src/webview/qwebview_webengine.cpp @@ -0,0 +1,259 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebView module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 "qwebview_webengine_p.h" +#include "qwebview_p.h" +#include "qwebviewloadrequest_p.h" + +#include <QtWebView/private/qquickwebview_p.h> + +#include <QtCore/qmap.h> +#include <QtGui/qguiapplication.h> +#include <QtCore/qjsondocument.h> +#include <QtCore/qjsonobject.h> +#include <QtCore/qurl.h> +#include <QtCore/qdebug.h> +#include <QtCore/qrunnable.h> + +#include <QtQuick/qquickwindow.h> +#include <QtQuick/qquickview.h> +#include <QtQuick/qquickitem.h> + +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcontext.h> + +#include <QtWebEngine/private/qquickwebengineview_p.h> +#include <QtWebEngine/private/qquickwebengineloadrequest_p.h> + +QT_BEGIN_NAMESPACE + +static QByteArray qmlSource() +{ + return QByteArrayLiteral("import QtWebEngine 1.1\n" + " WebEngineView {\n" + "}\n"); +} + +QWebViewPrivate *QWebViewPrivate::create(QWebView *q) +{ + return new QWebEngineWebViewPrivate(q); +} + +QWebEngineWebViewPrivate::QWebEngineWebViewPrivate(QObject *p) + : QWebViewPrivate(p) + , m_webEngineView(0) +{ + +} + +QWebEngineWebViewPrivate::~QWebEngineWebViewPrivate() +{ +} + +QUrl QWebEngineWebViewPrivate::url() const +{ + if (!m_webEngineView) + return QUrl(); + + return m_webEngineView->url(); +} + +void QWebEngineWebViewPrivate::setUrl(const QUrl &url) +{ + if (m_webEngineView) + m_webEngineView->setUrl(url); +} + +void QWebEngineWebViewPrivate::loadHtml(const QString &html, const QUrl &baseUrl) +{ + if (m_webEngineView) + m_webEngineView->loadHtml(html, baseUrl); +} + +bool QWebEngineWebViewPrivate::canGoBack() const +{ + if (!m_webEngineView) + return false; + + return m_webEngineView->canGoBack(); +} + +void QWebEngineWebViewPrivate::goBack() +{ + if (m_webEngineView) + m_webEngineView->goBack(); +} + +bool QWebEngineWebViewPrivate::canGoForward() const +{ + if (!m_webEngineView) + return false; + + return m_webEngineView->canGoForward(); +} + +void QWebEngineWebViewPrivate::goForward() +{ + if (m_webEngineView) + m_webEngineView->goForward(); +} + +void QWebEngineWebViewPrivate::reload() +{ + if (m_webEngineView) + m_webEngineView->reload(); +} + +QString QWebEngineWebViewPrivate::title() const +{ + if (!m_webEngineView) + return QString(); + + return m_webEngineView->title(); +} + +void QWebEngineWebViewPrivate::setGeometry(const QRect &geometry) +{ + if (m_webEngineView) + m_webEngineView->setSize(geometry.size()); +} + +void QWebEngineWebViewPrivate::setVisibility(QWindow::Visibility visibility) +{ + setVisible(visibility != QWindow::Hidden ? true : false); +} + +void QWebEngineWebViewPrivate::runJavaScriptPrivate(const QString &script, + int callbackId) +{ + if (m_webEngineView) + m_webEngineView->runJavaScript(script, QQuickWebView::takeCallback(callbackId)); +} + +void QWebEngineWebViewPrivate::setVisible(bool visible) +{ + if (m_webEngineView) + m_webEngineView->setVisible(visible); +} + +int QWebEngineWebViewPrivate::loadProgress() const +{ + if (!m_webEngineView) + return 0; + + return m_webEngineView->loadProgress(); +} + +bool QWebEngineWebViewPrivate::isLoading() const +{ + if (!m_webEngineView) + return false; + + return m_webEngineView->isLoading(); +} + +void QWebEngineWebViewPrivate::setParentView(QObject *parentView) +{ + if (m_webEngineView != 0 || parentView == 0) + return; + + QObject *p = parent(); + QQuickItem *parentItem = 0; + while (p != 0) { + p = p->parent(); + parentItem = qobject_cast<QQuickWebView *>(p); + if (parentItem != 0) + break; + } + + if (!parentItem) + return; + + QQmlContext *ctx = QQmlEngine::contextForObject(parentItem); + if (!ctx) + return; + + QQmlEngine *engine = ctx->engine(); + if (!engine) + return; + + QQmlComponent *component = new QQmlComponent(engine); + component->setData(qmlSource(), QUrl::fromLocalFile(QLatin1String(""))); + QQuickWebEngineView *webEngineView = qobject_cast<QQuickWebEngineView *>(component->create()); + connect(webEngineView, &QQuickWebEngineView::urlChanged, this, &QWebEngineWebViewPrivate::q_urlChanged); + connect(webEngineView, &QQuickWebEngineView::loadProgressChanged, this, &QWebEngineWebViewPrivate::q_loadProgressChanged); + connect(webEngineView, &QQuickWebEngineView::loadingChanged, this, &QWebEngineWebViewPrivate::q_loadingChanged); + connect(webEngineView, &QQuickWebEngineView::titleChanged, this, &QWebEngineWebViewPrivate::q_titleChanged); + webEngineView->setParentItem(parentItem); + m_webEngineView.reset(webEngineView); +} + +QObject *QWebEngineWebViewPrivate::parentView() const +{ + return m_webEngineView ? m_webEngineView->window() : 0; +} + +void QWebEngineWebViewPrivate::stop() +{ + if (m_webEngineView) + m_webEngineView->stop(); +} + +void QWebEngineWebViewPrivate::q_urlChanged() +{ + Q_EMIT urlChanged(m_webEngineView->url()); +} + +void QWebEngineWebViewPrivate::q_loadProgressChanged() +{ + Q_EMIT loadProgressChanged(m_webEngineView->loadProgress()); +} + +void QWebEngineWebViewPrivate::q_titleChanged() +{ + Q_EMIT titleChanged(m_webEngineView->title()); +} + +void QWebEngineWebViewPrivate::q_loadingChanged(QQuickWebEngineLoadRequest *loadRequest) +{ + QWebViewLoadRequestPrivate lr(loadRequest->url(), + static_cast<QWebView::LoadStatus>(loadRequest->status()), // These "should" match... + loadRequest->errorString()); + + Q_EMIT loadingChanged(lr); +} + +QT_END_NAMESPACE diff --git a/src/webview/qwebview_webengine_p.h b/src/webview/qwebview_webengine_p.h new file mode 100644 index 0000000..f1f7b05 --- /dev/null +++ b/src/webview/qwebview_webengine_p.h @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebView module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 QWEBVIEW_ANDROID_P_H +#define QWEBVIEW_ANDROID_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qobject.h> +#include <QtCore/qurl.h> +#include <QtGui/qwindow.h> + +#include <QtQml/qqmlcomponent.h> + +#include "qwebview_p_p.h" + +QT_BEGIN_NAMESPACE + +class QQuickWebEngineView; +class QQuickWebEngineLoadRequest; + +class QWebEngineWebViewPrivate : public QWebViewPrivate +{ + Q_OBJECT +public: + explicit QWebEngineWebViewPrivate(QObject *p = 0); + ~QWebEngineWebViewPrivate() Q_DECL_OVERRIDE; + + QUrl url() const Q_DECL_OVERRIDE; + void setUrl(const QUrl &url) Q_DECL_OVERRIDE; + bool canGoBack() const Q_DECL_OVERRIDE; + bool canGoForward() const Q_DECL_OVERRIDE; + QString title() const Q_DECL_OVERRIDE; + int loadProgress() const Q_DECL_OVERRIDE; + bool isLoading() const Q_DECL_OVERRIDE; + + void setParentView(QObject *parentView) Q_DECL_OVERRIDE; + QObject *parentView() const Q_DECL_OVERRIDE; + void setGeometry(const QRect &geometry) Q_DECL_OVERRIDE; + void setVisibility(QWindow::Visibility visibility) Q_DECL_OVERRIDE; + void setVisible(bool visible) Q_DECL_OVERRIDE; + +public Q_SLOTS: + void goBack() Q_DECL_OVERRIDE; + void goForward() Q_DECL_OVERRIDE; + void reload() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) Q_DECL_OVERRIDE; + +private Q_SLOTS: + void q_urlChanged(); + void q_loadProgressChanged(); + void q_titleChanged(); + void q_loadingChanged(QQuickWebEngineLoadRequest *loadRequest); + +protected: + void runJavaScriptPrivate(const QString& script, + int callbackId) Q_DECL_OVERRIDE; + +private: + QScopedPointer<QQuickWebEngineView> m_webEngineView; +}; + +QT_END_NAMESPACE + +#endif // QWEBVIEW_ANDROID_P_H diff --git a/src/webview/webview-lib.pri b/src/webview/webview-lib.pri index 3c6acda..e48c728 100644 --- a/src/webview/webview-lib.pri +++ b/src/webview/webview-lib.pri @@ -69,10 +69,13 @@ android { qwebview_winrt_p.h } else:qtHaveModule(webengine) { QT += webengine webengine-private - DEFINES += QT_WEBVIEW_WEBENGINE_BACKEND SOURCES += \ - qwebview_default.cpp - + $$COMMON_SOURCES \ + qwebview_webengine.cpp + PRIVATE_HEADERS += \ + $$COMMON_HEADERS \ + qwebview_webengine_p.h + DEFINES += QT_WEBVIEW_WEBENGINE_BACKEND } HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS |