summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2016-07-20 16:27:47 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2016-07-21 16:44:49 +0200
commit91ebebdbf26907c126dcc367832ed524d64b94cf (patch)
treed9d4fee39a1fa88a2eb8ff90bcde6ec248550311 /src
parentfa2374d7c4dedea907e2df26fdad28bdee73b122 (diff)
parent3836fd7d12777fc53836a72d6542d2fbf2f20ac9 (diff)
downloadqtwebchannel-91ebebdbf26907c126dcc367832ed524d64b94cf.tar.gz
Merge remote-tracking branch 'origin/5.7' into dev
Conflicts: src/webchannel/doc/src/index.qdoc src/webchannel/qwebchannelabstracttransport.cpp Overlapping changes to documentation; constructed hybrid. src/webchannel/qmetaobjectpublisher.cpp tests/auto/webchannel/tst_webchannel.cpp tests/auto/webchannel/tst_webchannel.h Both sides made additions; in the same place. Change-Id: Iff12970978b70946dc3e1290841aca2d35c9c1d0
Diffstat (limited to 'src')
-rw-r--r--src/webchannel/doc/qtwebchannel.qdocconf3
-rw-r--r--src/webchannel/doc/src/examples.qdoc3
-rw-r--r--src/webchannel/doc/src/external-resources.qdoc57
-rw-r--r--src/webchannel/doc/src/index.qdoc42
-rw-r--r--src/webchannel/doc/src/javascript.qdoc17
-rw-r--r--src/webchannel/qmetaobjectpublisher.cpp66
-rw-r--r--src/webchannel/qmetaobjectpublisher_p.h9
-rw-r--r--src/webchannel/qqmlwebchannel.cpp21
-rw-r--r--src/webchannel/qwebchannel.cpp10
-rw-r--r--src/webchannel/qwebchannel.js18
-rw-r--r--src/webchannel/qwebchannelabstracttransport.cpp7
11 files changed, 196 insertions, 57 deletions
diff --git a/src/webchannel/doc/qtwebchannel.qdocconf b/src/webchannel/doc/qtwebchannel.qdocconf
index cfb47e4..4026379 100644
--- a/src/webchannel/doc/qtwebchannel.qdocconf
+++ b/src/webchannel/doc/qtwebchannel.qdocconf
@@ -40,7 +40,7 @@ qhp.QtWebChannel.subprojects.javascript.indexTitle = Qt WebChannel JavaScript
tagfile = ../../../doc/qtwebchannel/qtwebchannel.tags
-depends += qtcore qtquick qtqml qmake qtdoc
+depends += qtcore qtquick qtqml qmake qtdoc qtwebengine qtwebsockets
headerdirs += .. \
../../imports
@@ -48,6 +48,7 @@ headerdirs += .. \
sourcedirs += .. \
../../imports
+imagedirs += images
exampledirs += ../../../examples/webchannel
examples.fileextensions += "*.html *.json"
diff --git a/src/webchannel/doc/src/examples.qdoc b/src/webchannel/doc/src/examples.qdoc
index b304ac6..83e614e 100644
--- a/src/webchannel/doc/src/examples.qdoc
+++ b/src/webchannel/doc/src/examples.qdoc
@@ -42,6 +42,7 @@
\group qtwebchannel-examples
\brief List of Qt WebChannel examples.
- The examples show how to use the QWebChannel C++ and WebChannel QML API.
+ The examples show how to use the QWebChannel C++ and \l [QML] WebChannel QML
+ API.
*/
diff --git a/src/webchannel/doc/src/external-resources.qdoc b/src/webchannel/doc/src/external-resources.qdoc
new file mode 100644
index 0000000..0002ec1
--- /dev/null
+++ b/src/webchannel/doc/src/external-resources.qdoc
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** 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 Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: http://www.gnu.org/copyleft/fdl.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*
+ The nolink entries prevent autolinking of each occurrence of 'WebChannel'
+ 'WebEngine' or 'WebSocket' to the QML type.
+ To link to the QML type, use explicit linking. For example:
+ \l [QML] WebChannel
+ \sa {QtWebChannel::}{WebChannel}
+*/
+/*!
+ \externalpage nolink
+ \title WebChannel
+ \internal
+*/
+
+/*!
+ \externalpage nolink
+ \title WebEngine
+ \internal
+*/
+
+/*!
+ \externalpage nolink
+ \title WebSockets
+ \internal
+*/
+
+/*!
+ \externalpage nolink
+ \title WebSocket
+ \internal
+*/
diff --git a/src/webchannel/doc/src/index.qdoc b/src/webchannel/doc/src/index.qdoc
index a4eaad0..5529093 100644
--- a/src/webchannel/doc/src/index.qdoc
+++ b/src/webchannel/doc/src/index.qdoc
@@ -31,25 +31,49 @@
\title Qt WebChannel
\brief Bridges the gap between Qt applications and HTML/JavaScript.
- Qt WebChannel enables peer-to-peer communication between a host
- (QML/C++ application) and a client (HTML/JavaScript application).
- It is supported out of the box by Qt WebKit 2 and Qt WebEngine.
- In addition it can work on all
- browsers that support WebSockets, enabling Qt WebChannel clients
- to run in any JavaScript environment (including QML). This requires
+ Qt WebChannel enables peer-to-peer communication between a server (QML/C++
+ application) and a client (HTML/JavaScript or QML application). It is
+ supported out of the box by \l{Qt WebEngine}. In addition it can work on all
+ browsers that support \l{Qt WebSockets}{WebSockets}, enabling Qt WebChannel
+ clients to run in any JavaScript environment (including QML). This requires
the implementation of a custom transport based on Qt WebSockets.
The module provides a JavaScript library for seamless integration of C++
- and QML applications with HTML/JavaScript clients. The client must use the
+ and QML applications with HTML/JavaScript and QML clients. The clients must use the
JavaScript library to access the serialized QObjects published by the host
applications.
- \section1 Related Information
+ \section1 Getting Started
+
+ To use these classes in your application, use the following include
+ statement:
+
+ \code
+ #include <QtWebChannel/QtWebChannel>
+ \endcode
+
+ To link against the module, add this line to your \l qmake \c .pro file:
+
+ \code
+ QT += webchannel
+ \endcode
+
+ The QML types are accessed by using:
+ \badcode
+ import QtWebChannel 1.0
+ \endcode
+
+ \section1 API Reference
\list
\li \l{Qt WebChannel JavaScript API}{JavaScript API}
\li \l{Qt WebChannel C++ Classes}{C++ API}
\li \l{Qt WebChannel QML Types}{QML API}
- \li \l{Qt WebChannel Examples}{Examples} - show how use the API in practice
+ \endlist
+
+ \section1 Examples
+
+ \list
+ \li \l{Qt WebChannel Examples}{Examples}
\endlist
*/
diff --git a/src/webchannel/doc/src/javascript.qdoc b/src/webchannel/doc/src/javascript.qdoc
index cfb5b21..a508ff9 100644
--- a/src/webchannel/doc/src/javascript.qdoc
+++ b/src/webchannel/doc/src/javascript.qdoc
@@ -31,21 +31,20 @@
\brief This page explains how to use the JavaScript QWebChannel API in HTML clients.
- \section1 Setup
+ \section1 Setting up the JavaScript API
- To communicate with a QWebChannel or WebChannel, any HTML client must use and setup the
- JavaScript API provided by \c qwebchannel.js. For HTML clients run inside Qt WebKit, you
- can load the file via \c qrc:///qtwebchannel/qwebchannel.js. For external clients you will
- need to copy the file to your webserver. Then instantiate a QWebChannel object and pass
+ To communicate with a QWebChannel or \l [QML] WebChannel, a client must use and set up the
+ JavaScript API provided by \c qwebchannel.js. For clients run inside \l{Qt WebEngine}, you
+ can load the file via \c qrc:///qtwebchannel/qwebchannel.js. For external clients, you
+ need to copy the file to your web server. Then instantiate a QWebChannel object and pass
it a transport object and a callback function, which will be invoked once the
- initialization of the channel finished and published objects become available.
+ initialization of the channel finishes and the published objects become available.
The transport object implements a minimal message passing interface. It should be an object
with a \c send() function, which takes a stringified JSON message and transmits it to the
server-side QWebChannelAbstractTransport object. Furthermore, its \c onmessage property should
- be called when a message from the server was received. This interface is implemented internally
- by the Qt WebKit navigator.qtWebChannelTransport object. Alternatively, you can also use a
- WebSocket, which also implements this interface.
+ be called when a message from the server was received. Alternatively, you can use a
+ \l{Qt WebSockets}{WebSocket} to implement the interface.
Note that the JavaScript QWebChannel object should be constructed once the transport object is
fully operational. In case of a WebSocket, that means you should create the QWebChannel in the
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp
index 8fd8e4f..66676f0 100644
--- a/src/webchannel/qmetaobjectpublisher.cpp
+++ b/src/webchannel/qmetaobjectpublisher.cpp
@@ -372,11 +372,7 @@ QVariant QMetaObjectPublisher::invokeMethod(QObject *const object, const int met
// construct converter objects of QVariant to QGenericArgument
VariantArgument arguments[10];
for (int i = 0; i < qMin(args.size(), method.parameterCount()); ++i) {
- QVariant arg = args.at(i).toVariant();
- if (method.parameterType(i) != QMetaType::QVariant && !arg.convert(method.parameterType(i))) {
- qWarning() << "Could not convert argument" << args.at(i) << "to target type" << method.parameterTypes().at(i) << '.';
- }
- arguments[i].value = arg;
+ arguments[i].value = toVariant(args.at(i), method.parameterType(i));
}
// construct QGenericReturnArgument
@@ -397,6 +393,16 @@ QVariant QMetaObjectPublisher::invokeMethod(QObject *const object, const int met
return returnValue;
}
+void QMetaObjectPublisher::setProperty(QObject *object, const int propertyIndex, const QJsonValue &value)
+{
+ QMetaProperty property = object->metaObject()->property(propertyIndex);
+ if (!property.isValid()) {
+ qWarning() << "Cannot set unknown property" << propertyIndex << "of object" << object;
+ } else if (!property.write(object, toVariant(value, property.userType()))) {
+ qWarning() << "Could not write value " << value << "to property" << property.name() << "of object" << object;
+ }
+}
+
void QMetaObjectPublisher::signalEmitted(const QObject *object, const int signalIndex, const QVariantList &arguments)
{
if (!webChannel || webChannel->d_func()->transports.isEmpty()) {
@@ -449,6 +455,46 @@ void QMetaObjectPublisher::objectDestroyed(const QObject *object)
pendingPropertyUpdates.remove(object);
}
+QObject *QMetaObjectPublisher::unwrapObject(const QString &objectId) const
+{
+ if (!objectId.isEmpty()) {
+ ObjectInfo objectInfo = wrappedObjects.value(objectId);
+ if (objectInfo.object && !objectInfo.classinfo.isEmpty())
+ return objectInfo.object;
+ }
+
+ qWarning() << "No wrapped object" << objectId;
+ return Q_NULLPTR;
+}
+
+QVariant QMetaObjectPublisher::toVariant(const QJsonValue &value, int targetType) const
+{
+ if (targetType == QMetaType::QJsonValue) {
+ return QVariant::fromValue(value);
+ } else if (targetType == QMetaType::QJsonArray) {
+ if (!value.isArray())
+ qWarning() << "Cannot not convert non-array argument" << value << "to QJsonArray.";
+ return QVariant::fromValue(value.toArray());
+ } else if (targetType == QMetaType::QJsonObject) {
+ if (!value.isObject())
+ qWarning() << "Cannot not convert non-object argument" << value << "to QJsonObject.";
+ return QVariant::fromValue(value.toObject());
+ } else if (QMetaType::typeFlags(targetType) & QMetaType::PointerToQObject) {
+ QObject *unwrappedObject = unwrapObject(value.toObject()[KEY_ID].toString());
+ if (unwrappedObject == Q_NULLPTR)
+ qWarning() << "Cannot not convert non-object argument" << value << "to QObject*.";
+ return QVariant::fromValue(unwrappedObject);
+ }
+
+ // this converts QJsonObjects to QVariantMaps, which is not desired when
+ // we want to get a QJsonObject or QJsonValue (see above)
+ QVariant variant = value.toVariant();
+ if (targetType != QMetaType::QVariant && !variant.convert(targetType)) {
+ qWarning() << "Could not convert argument" << value << "to target type" << QVariant::typeToName(targetType) << '.';
+ }
+ return variant;
+}
+
void QMetaObjectPublisher::transportRemoved(QWebChannelAbstractTransport *transport)
{
auto it = transportedWrappedObjects.find(transport);
@@ -618,14 +664,8 @@ void QMetaObjectPublisher::handleMessage(const QJsonObject &message, QWebChannel
} else if (type == TypeDisconnectFromSignal) {
signalHandler.disconnectFrom(object, message.value(KEY_SIGNAL).toInt(-1));
} else if (type == TypeSetProperty) {
- const int propertyIdx = message.value(KEY_PROPERTY).toInt(-1);
- QMetaProperty property = object->metaObject()->property(propertyIdx);
- if (!property.isValid()) {
- qWarning() << "Cannot set unknown property" << message.value(KEY_PROPERTY) << "of object" << objectName;
- } else if (!object->metaObject()->property(propertyIdx).write(object, message.value(KEY_VALUE).toVariant())) {
- qWarning() << "Could not write value " << message.value(KEY_VALUE)
- << "to property" << property.name() << "of object" << objectName;
- }
+ setProperty(object, message.value(KEY_PROPERTY).toInt(-1),
+ message.value(KEY_VALUE));
}
}
}
diff --git a/src/webchannel/qmetaobjectpublisher_p.h b/src/webchannel/qmetaobjectpublisher_p.h
index fe75656..830e510 100644
--- a/src/webchannel/qmetaobjectpublisher_p.h
+++ b/src/webchannel/qmetaobjectpublisher_p.h
@@ -156,6 +156,11 @@ public:
QVariant invokeMethod(QObject *const object, const int methodIndex, const QJsonArray &args);
/**
+ * Set the value of property @p propertyIndex on @p object to @p value.
+ */
+ void setProperty(QObject *object, const int propertyIndex, const QJsonValue &value);
+
+ /**
* Callback of the signalHandler which forwards the signal invocation to the webchannel clients.
*/
void signalEmitted(const QObject *object, const int signalIndex, const QVariantList &arguments);
@@ -167,6 +172,10 @@ public:
*/
void objectDestroyed(const QObject *object);
+ QObject *unwrapObject(const QString &objectId) const;
+
+ QVariant toVariant(const QJsonValue &value, int targetType) const;
+
/**
* Remove wrapped objects which last transport relation is with the passed transport object.
*/
diff --git a/src/webchannel/qqmlwebchannel.cpp b/src/webchannel/qqmlwebchannel.cpp
index 264bc54..d23ebef 100644
--- a/src/webchannel/qqmlwebchannel.cpp
+++ b/src/webchannel/qqmlwebchannel.cpp
@@ -69,7 +69,7 @@ QT_BEGIN_NAMESPACE
A list of transport objects, which implement QWebChannelAbstractTransport. The transports
are used to talk to the remote clients.
- \sa WebChannel::connectTo(), WebChannel::disconnectFrom()
+ \sa connectTo(), disconnectFrom()
*/
/*!
@@ -77,7 +77,7 @@ QT_BEGIN_NAMESPACE
\brief A list of objects which should be accessible to remote clients.
- The objects must have the attached WebChannel::id property set to an identifier, under which the
+ The objects must have the attached \l id property set to an identifier, under which the
object is then known on the HTML side.
Once registered, all signals and property changes are automatically propagated to the clients.
@@ -86,7 +86,7 @@ QT_BEGIN_NAMESPACE
If one needs to register objects which are not available when the component is created, use the
imperative registerObjects method.
- \sa WebChannel::registerObjects(), WebChannel::id
+ \sa registerObjects(), id
*/
class QQmlWebChannelPrivate : public QWebChannelPrivate
@@ -134,8 +134,8 @@ QQmlWebChannel::~QQmlWebChannel()
/*!
\qmlmethod void WebChannel::registerObjects(QVariantMap objects)
- Register objects to make them accessible to HTML clients. The key of the map is used as an identifier
- for the object on the client side.
+ Registers objects to make them accessible to HTML clients. The key of the
+ map is used as an identifier for the object on the client side.
Once registered, all signals and property changes are automatically propagated to the clients.
Public invokable methods, including slots, are also accessible to the clients.
@@ -143,7 +143,7 @@ QQmlWebChannel::~QQmlWebChannel()
This imperative API can be used to register objects on the fly. For static objects, the declarative
registeredObjects property should be preferred.
- \sa WebChannel::registeredObjects
+ \sa registeredObjects
*/
void QQmlWebChannel::registerObjects(const QVariantMap &objects)
{
@@ -167,11 +167,12 @@ QQmlWebChannelAttached *QQmlWebChannel::qmlAttachedProperties(QObject *obj)
/*!
\qmlmethod void WebChannel::connectTo(QWebChannelAbstractTransport transport)
- \brief Connectect to the \a transport, which represents a communication channel to a single client.
+ \brief Connects to the \a transport, which represents a communication
+ channel to a single client.
The transport object must be an implementation of QWebChannelAbstractTransport.
- \sa WebChannel::transports, WebChannel::disconnectFrom()
+ \sa transports, disconnectFrom()
*/
void QQmlWebChannel::connectTo(QObject *transport)
{
@@ -185,12 +186,12 @@ void QQmlWebChannel::connectTo(QObject *transport)
/*!
\qmlmethod void WebChannel::disconnectFrom(QWebChannelAbstractTransport transport)
- \brief Disconnect the \a transport from this WebChannel.
+ \brief Disconnects the \a transport from this WebChannel.
The client will not be able to communicate with the WebChannel anymore, nor will it receive any
signals or property updates.
- \sa WebChannel::connectTo()
+ \sa connectTo()
*/
void QQmlWebChannel::disconnectFrom(QObject *transport)
{
diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp
index 7a63af9..0e9a4c5 100644
--- a/src/webchannel/qwebchannel.cpp
+++ b/src/webchannel/qwebchannel.cpp
@@ -52,7 +52,7 @@ QT_BEGIN_NAMESPACE
\class QWebChannel
\inmodule QtWebChannel
- \brief Expose QObjects to remote HTML clients.
+ \brief Exposes QObjects to remote HTML clients.
\since 5.4
The QWebChannel fills the gap between C++ applications and HTML/JavaScript
@@ -69,7 +69,7 @@ QT_BEGIN_NAMESPACE
features used by \c{qwebchannel.js}. As such, one can interact
with basically any modern HTML browser or standalone JavaScript runtime, such as node.js.
- There also exists a declarative WebChannel API.
+ There also exists a declarative \l{Qt WebChannel QML Types}{WebChannel API}.
\sa {Qt WebChannel Standalone Example}, {Qt WebChannel JavaScript API}{JavaScript API}
*/
@@ -138,7 +138,7 @@ QWebChannel::~QWebChannel()
}
/*!
- Register a group of objects to the QWebChannel.
+ Registers a group of objects to the QWebChannel.
The properties, signals and public invokable methods of the objects are published to the remote clients.
There, an object with the identifier used as key in the \a objects map is then constructed.
@@ -168,7 +168,7 @@ QHash<QString, QObject *> QWebChannel::registeredObjects() const
}
/*!
- Register a single object to the QWebChannel.
+ Registers a single object to the QWebChannel.
The properties, signals and public methods of the \a object are published to the remote clients.
There, an object with the identifier \a id is then constructed.
@@ -184,7 +184,7 @@ void QWebChannel::registerObject(const QString &id, QObject *object)
}
/*!
- Deregister the given \a object from the QWebChannel.
+ Deregisters the given \a object from the QWebChannel.
Remote clients will receive a \c destroyed signal for the given object.
diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js
index d39e301..5b047c2 100644
--- a/src/webchannel/qwebchannel.js
+++ b/src/webchannel/qwebchannel.js
@@ -319,7 +319,7 @@ function QObject(name, data, webChannel)
this.signalEmitted = function(signalName, signalArgs)
{
- invokeSignalCallbacks(signalName, signalArgs);
+ invokeSignalCallbacks(signalName, this.unwrapQObject(signalArgs));
}
function addMethod(methodData)
@@ -330,10 +330,15 @@ function QObject(name, data, webChannel)
var args = [];
var callback;
for (var i = 0; i < arguments.length; ++i) {
- if (typeof arguments[i] === "function")
- callback = arguments[i];
+ var argument = arguments[i];
+ if (typeof argument === "function")
+ callback = argument;
+ else if (argument instanceof QObject && webChannel.objects[argument.__id__] !== undefined)
+ args.push({
+ "id": argument.__id__
+ });
else
- args.push(arguments[i]);
+ args.push(argument);
}
webChannel.exec({
@@ -387,11 +392,14 @@ function QObject(name, data, webChannel)
return;
}
object.__propertyCache__[propertyIndex] = value;
+ var valueToSend = value;
+ if (valueToSend instanceof QObject && webChannel.objects[valueToSend.__id__] !== undefined)
+ valueToSend = { "id": valueToSend.__id__ };
webChannel.exec({
"type": QWebChannelMessageTypes.setProperty,
"object": object.__id__,
"property": propertyIndex,
- "value": value
+ "value": valueToSend
});
}
});
diff --git a/src/webchannel/qwebchannelabstracttransport.cpp b/src/webchannel/qwebchannelabstracttransport.cpp
index d1b7199..86a9c18 100644
--- a/src/webchannel/qwebchannelabstracttransport.cpp
+++ b/src/webchannel/qwebchannelabstracttransport.cpp
@@ -50,9 +50,8 @@ QT_BEGIN_NAMESPACE
Users of the QWebChannel must implement this interface and connect instances of it
to the QWebChannel server for every client that should be connected to the QWebChannel.
- The \l {Qt WebChannel Standalone Example}{Standalone Example} shows how this can be done
- using Qt WebSockets. Qt WebKit implements this interface internally and uses the native
- WebKit IPC mechanism to transmit messages to HTML clients.
+ The \l{Qt WebChannel Standalone Example} shows how this can be done
+ using \l{Qt WebSockets}.
\note The JSON message protocol is considered internal and might change over time.
@@ -69,7 +68,7 @@ QT_BEGIN_NAMESPACE
/*!
\fn QWebChannelAbstractTransport::sendMessage(const QJsonObject &message)
- Send a JSON \a message to the remote client. An implementation would serialize the message and
+ Sends a JSON \a message to the remote client. An implementation would serialize the message and
transmit it to the remote JavaScript client.
*/