summaryrefslogtreecommitdiff
path: root/Source/WebKit2/UIProcess/API
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-07-03 19:12:37 +0200
committerAllan Sandfeld Jensen <allan.jensen@digia.com>2014-08-16 09:49:41 +0200
commit05ea746d3042d1409305bc1ebcaf15095870b6d9 (patch)
tree5155938d7e853d96a6a4de44c314d715b88d547c /Source/WebKit2/UIProcess/API
parent6d998167b9f14d441172022636091251fa34a15a (diff)
downloadqtwebkit-05ea746d3042d1409305bc1ebcaf15095870b6d9.tar.gz
Integrate QWebChannel with QtWebKit.
WebView.experimental gets a new webChannel property. By default, this is initialized to a QQmlWebChannel internal to the WebView. By setting it from the outside, it is possible to share the same WebChannel between different WebViews. The webChannel property can be set to null to disable this new feature. For IPC, a navigator.qtWebChannelTransport property is added, which is essentially a copy of navigator.qt. This is required to be able to use both independently. The transport is implicitly connected to the WebView's webChannel. On the JavaScript client side, some manual boiler plate code is still required. Potentially, this can be adapted in the future to preload the qwebchannel.js. Furthermore, we might be able to instantiate and initialize the QWebChannel before we emit window.onload. A basic test is added which verifies that this basic integration works. Change-Id: I5eb0389e6edd6d0c978917e441365e11a02b5c3f Reviewed-by: Milian Wolff <milian.wolff@kdab.com> Reviewed-by: Allan Sandfeld Jensen <allan.jensen@digia.com>
Diffstat (limited to 'Source/WebKit2/UIProcess/API')
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp53
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h20
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h4
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport.cpp74
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport_p.h63
-rw-r--r--Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_webchannel.qml117
-rw-r--r--Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/webchannel.html21
7 files changed, 352 insertions, 0 deletions
diff --git a/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp b/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp
index a46b6972e..86b8cb4d4 100644
--- a/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp
+++ b/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp
@@ -84,6 +84,11 @@
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
+#ifdef HAVE_WEBCHANNEL
+#include <QtWebChannel/QQmlWebChannel>
+#include "qwebchannelwebkittransport_p.h"
+#endif
+
using namespace WebCore;
using namespace WebKit;
@@ -959,6 +964,14 @@ void QQuickWebViewPrivate::didReceiveMessageFromNavigatorQtObject(WKStringRef me
emit q_ptr->experimental()->messageReceived(variantMap);
}
+#ifdef HAVE_WEBCHANNEL
+void QQuickWebViewPrivate::didReceiveMessageFromNavigatorQtWebChannelTransportObject(WKStringRef message)
+{
+ // TODO: can I convert a WKStringRef to a UTF8 QByteArray directly?
+ q_ptr->experimental()->m_webChannelTransport->receiveMessage(WKStringCopyQString(message).toUtf8());
+}
+#endif
+
CoordinatedGraphicsScene* QQuickWebViewPrivate::coordinatedGraphicsScene()
{
if (webPageProxy && webPageProxy->drawingArea() && webPageProxy->drawingArea()->coordinatedLayerTreeHostProxy())
@@ -1074,7 +1087,14 @@ QQuickWebViewExperimental::QQuickWebViewExperimental(QQuickWebView *webView, QQu
, d_ptr(webViewPrivate)
, schemeParent(new QObject(this))
, m_test(new QWebKitTest(webViewPrivate, this))
+#ifdef HAVE_WEBCHANNEL
+ , m_webChannel(new QQmlWebChannel(this))
+ , m_webChannelTransport(new QWebChannelWebKitTransport(this))
+#endif
{
+#ifdef HAVE_WEBCHANNEL
+ m_webChannel->connectTo(m_webChannelTransport);
+#endif
}
QQuickWebViewExperimental::~QQuickWebViewExperimental()
@@ -1163,6 +1183,29 @@ bool QQuickWebViewExperimental::flickableViewportEnabled()
return s_flickableViewportEnabled;
}
+#ifdef HAVE_WEBCHANNEL
+QQmlWebChannel* QQuickWebViewExperimental::webChannel() const
+{
+ return m_webChannel;
+}
+
+void QQuickWebViewExperimental::setWebChannel(QQmlWebChannel* channel)
+{
+ if (channel == m_webChannel)
+ return;
+
+ if (m_webChannel)
+ m_webChannel->disconnectFrom(m_webChannelTransport);
+
+ m_webChannel = channel;
+
+ if (m_webChannel)
+ m_webChannel->connectTo(m_webChannelTransport);
+
+ emit webChannelChanged(channel);
+}
+#endif
+
/*!
\internal
@@ -1182,6 +1225,16 @@ void QQuickWebViewExperimental::postMessage(const QString& message)
WKPagePostMessageToInjectedBundle(d->webPage.get(), messageName, contents.get());
}
+#ifdef HAVE_WEBCHANNEL
+void QQuickWebViewExperimental::postQtWebChannelTransportMessage(const QByteArray& message)
+{
+ Q_D(QQuickWebView);
+ static WKStringRef messageName = WKStringCreateWithUTF8CString("MessageToNavigatorQtWebChannelTransportObject");
+ WKRetainPtr<WKStringRef> contents = adoptWK(WKStringCreateWithUTF8CString(message.constData()));
+ WKPagePostMessageToInjectedBundle(d->webPage.get(), messageName, contents.get());
+}
+#endif
+
QQmlComponent* QQuickWebViewExperimental::alertDialog() const
{
Q_D(const QQuickWebView);
diff --git a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h
index 86d68fc15..8604dead2 100644
--- a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h
+++ b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h
@@ -30,6 +30,7 @@
QT_BEGIN_NAMESPACE
class QQmlComponent;
+class QQmlWebChannel;
QT_END_NAMESPACE
class QWebNavigationRequest;
class QQuickWebPage;
@@ -43,6 +44,7 @@ class QWebPreferences;
class QWebPermissionRequest;
class QWebKitTest;
class QQuickNetworkReply;
+class QWebChannelWebKitTransport;
namespace WTR {
class PlatformWebView;
@@ -280,6 +282,9 @@ class QWEBKIT_EXPORT QQuickWebViewExperimental : public QObject {
Q_PROPERTY(QString userAgent READ userAgent WRITE setUserAgent NOTIFY userAgentChanged)
Q_PROPERTY(QList<QUrl> userScripts READ userScripts WRITE setUserScripts NOTIFY userScriptsChanged)
Q_PROPERTY(QUrl remoteInspectorUrl READ remoteInspectorUrl NOTIFY remoteInspectorUrlChanged FINAL)
+#ifdef HAVE_WEBCHANNEL
+ Q_PROPERTY(QQmlWebChannel* webChannel READ webChannel WRITE setWebChannel NOTIFY webChannelChanged)
+#endif
Q_ENUMS(NavigationRequestActionExperimental)
Q_FLAGS(FindFlags)
@@ -357,6 +362,12 @@ public:
static void setFlickableViewportEnabled(bool enable);
static bool flickableViewportEnabled();
+#ifdef HAVE_WEBCHANNEL
+ QQmlWebChannel* webChannel() const;
+ void setWebChannel(QQmlWebChannel* channel);
+ void postQtWebChannelTransportMessage(const QByteArray& message);
+#endif
+
public Q_SLOTS:
void goBackTo(int index);
void goForwardTo(int index);
@@ -394,6 +405,10 @@ Q_SIGNALS:
void processDidBecomeUnresponsive();
void processDidBecomeResponsive();
+#ifdef HAVE_WEBCHANNEL
+ void webChannelChanged(QQmlWebChannel* channel);
+#endif
+
private:
QQuickWebViewExperimental(QQuickWebView* webView, QQuickWebViewPrivate* webViewPrivate);
QQuickWebView* q_ptr;
@@ -401,6 +416,11 @@ private:
QObject* schemeParent;
QWebKitTest* m_test;
+#ifdef HAVE_WEBCHANNEL
+ QQmlWebChannel* m_webChannel;
+ QWebChannelWebKitTransport* m_webChannelTransport;
+#endif
+
friend class WebKit::QtWebPageUIClient;
Q_DECLARE_PRIVATE(QQuickWebView)
diff --git a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h
index b39a4f071..f09c16e90 100644
--- a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h
+++ b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h
@@ -55,6 +55,7 @@ class WebPageProxy;
class QWebNavigationHistory;
class QWebKitTest;
+class QWebChannelWebKitTransport;
QT_BEGIN_NAMESPACE
class QQmlComponent;
@@ -137,6 +138,9 @@ public:
void handleDownloadRequest(WebKit::DownloadProxy*);
void didReceiveMessageFromNavigatorQtObject(WKStringRef message);
+#ifdef HAVE_WEBCHANNEL
+ void didReceiveMessageFromNavigatorQtWebChannelTransportObject(WKStringRef message);
+#endif
WebCore::CoordinatedGraphicsScene* coordinatedGraphicsScene();
float deviceScaleFactor();
diff --git a/Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport.cpp b/Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport.cpp
new file mode 100644
index 000000000..209596954
--- /dev/null
+++ b/Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport.cpp
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel 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 Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 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 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qwebchannelwebkittransport_p.h"
+
+#include <QJsonObject>
+#include <QJsonDocument>
+
+#include "qquickwebview_p.h"
+
+QWebChannelWebKitTransport::QWebChannelWebKitTransport(QQuickWebViewExperimental* experimental)
+ : QWebChannelAbstractTransport(experimental)
+ , m_experimental(experimental)
+{
+}
+
+void QWebChannelWebKitTransport::sendMessage(const QJsonObject& message)
+{
+ const QByteArray data = QJsonDocument(message).toJson(QJsonDocument::Compact);
+ m_experimental->postQtWebChannelTransportMessage(data);
+}
+
+void QWebChannelWebKitTransport::receiveMessage(const QByteArray& message)
+{
+ QJsonParseError error;
+ const QJsonDocument doc = QJsonDocument::fromJson(message, &error);
+ if (error.error != QJsonParseError::NoError) {
+ qWarning() << "Failed to parse the client WebKit QWebChannel message as JSON: " << message
+ << "Error message is:" << error.errorString();
+ return;
+ } else if (!doc.isObject()) {
+ qWarning() << "Received WebKit QWebChannel message is not a JSON object: " << message;
+ return;
+ }
+ emit messageReceived(doc.object(), this);
+}
diff --git a/Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport_p.h b/Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport_p.h
new file mode 100644
index 000000000..ef6140359
--- /dev/null
+++ b/Source/WebKit2/UIProcess/API/qt/qwebchannelwebkittransport_p.h
@@ -0,0 +1,63 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel 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 Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 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 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QWEBCHANNELWEBKITTRANSPORT_H
+#define QWEBCHANNELWEBKITTRANSPORT_H
+
+#include <QtWebChannel/QWebChannelAbstractTransport>
+
+class QQuickWebViewExperimental;
+
+class QWebChannelWebKitTransport : public QWebChannelAbstractTransport
+{
+ Q_OBJECT
+public:
+ explicit QWebChannelWebKitTransport(QQuickWebViewExperimental* experimental);
+
+ void sendMessage(const QJsonObject& message) Q_DECL_OVERRIDE;
+
+ void receiveMessage(const QByteArray& message);
+
+private:
+ QQuickWebViewExperimental* m_experimental;
+};
+
+#endif // QWEBCHANNELWEBKITTRANSPORT_H
diff --git a/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_webchannel.qml b/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_webchannel.qml
new file mode 100644
index 000000000..e16b7866e
--- /dev/null
+++ b/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_webchannel.qml
@@ -0,0 +1,117 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel 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 Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 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 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtTest 1.0
+import QtWebKit 3.0
+import QtWebKit.experimental 1.0
+import "../common"
+
+import QtWebChannel 1.0
+
+Item {
+ id: test
+ signal barCalled(var arg)
+ signal clientInitializedCalled(var arg)
+
+ QtObject {
+ id: testObject
+ WebChannel.id: "testObject"
+
+ property var foo: 42
+
+ function clientInitialized(arg)
+ {
+ clientInitializedCalled(arg);
+ }
+
+ function bar(arg) {
+ barCalled(arg);
+ }
+
+ signal runTest(var foo)
+ }
+
+ TestWebView {
+ id: webView
+ experimental.windowObjects: [testObject]
+ experimental.preferences.developerExtrasEnabled: true
+ }
+
+ SignalSpy {
+ id: initializedSpy
+ target: test
+ signalName: "clientInitializedCalled"
+ }
+
+ SignalSpy {
+ id: barSpy
+ target: test
+ signalName: "barCalled"
+ }
+
+ TestCase {
+ name: "WebViewWebChannel"
+ property url testUrl: Qt.resolvedUrl("../common/webchannel.html")
+
+ function init() {
+ initializedSpy.clear();
+ barSpy.clear();
+ }
+
+ function test_basic() {
+ webView.url = testUrl;
+ verify(webView.waitForLoadSucceeded());
+
+ initializedSpy.wait();
+ compare(initializedSpy.signalArguments.length, 1);
+ compare(initializedSpy.signalArguments[0][0], 42);
+
+ var newValue = "roundtrip";
+ testObject.runTest(newValue);
+ barSpy.wait();
+ compare(barSpy.signalArguments.length, 1);
+ compare(barSpy.signalArguments[0][0], newValue);
+
+ compare(testObject.foo, newValue);
+ }
+ }
+}
diff --git a/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/webchannel.html b/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/webchannel.html
new file mode 100644
index 000000000..940821209
--- /dev/null
+++ b/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/webchannel.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript">
+ //BEGIN SETUP
+ var channel = new QWebChannel(navigator.qtWebChannelTransport, function(channel) {
+ window.testObject = channel.objects.testObject;
+ testObject.runTest.connect(function(foo) {
+ testObject.foo = foo;
+ testObject.bar(foo);
+ });
+ testObject.clientInitialized(testObject.foo);
+ });
+ //END SETUP
+ </script>
+ </head>
+ <body>
+ </body>
+</html>