diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-02-05 16:03:20 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-03-21 15:03:34 +0100 |
commit | e3e4d6a18d63537459f0e616360e53e816927f76 (patch) | |
tree | c52e2d7d402dd1cea731065a673301821e5a72d7 | |
parent | 3a85e592c050e73e61dc54d22e133bcf08d2f3c5 (diff) | |
download | qtwebchannel-e3e4d6a18d63537459f0e616360e53e816927f76.tar.gz |
Use an enum for message types instead of strings.
This further reduces the network traffic and thus leads to a small
performance boost.
Personally, I also think this code is a bit nicer to read and grasp.
Change-Id: I943c621142e9982f0e52d24e3a0976428856541b
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.cpp | 61 | ||||
-rw-r--r-- | src/webchannel/qwebchannel.js | 44 | ||||
-rw-r--r-- | tests/qml/WebChannelTest.qml | 16 | ||||
-rw-r--r-- | tests/qml/data/grouping.html | 2 | ||||
-rw-r--r-- | tests/qml/tst_metaobjectpublisher.qml | 22 |
5 files changed, 100 insertions, 45 deletions
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index 1ac3ad9..d06fe98 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -56,6 +56,36 @@ QT_BEGIN_NAMESPACE namespace { + +// NOTE: keep in sync with corresponding maps in qwebchannel.js and WebChannelTest.qml +enum Type { + TypeInvalid = 0, + + TYPES_FIRST_VALUE = 1, + + TypeSignal = 1, + TypePropertyUpdate = 2, + TypeInit = 3, + TypeIdle = 4, + TypeDebug = 5, + TypeInvokeMethod = 6, + TypeConnectToSignal = 7, + TypeDisconnectFromSignal = 8, + TypeSetProperty = 9, + + TYPES_LAST_VALUE = 9 +}; + +Type toType(const QJsonValue &value) +{ + int i = value.toInt(-1); + if (i >= TYPES_FIRST_VALUE && i <= TYPES_LAST_VALUE) { + return static_cast<Type>(i); + } else { + return TypeInvalid; + } +} + const QString KEY_SIGNALS = QStringLiteral("signals"); const QString KEY_METHODS = QStringLiteral("methods"); const QString KEY_PROPERTIES = QStringLiteral("properties"); @@ -73,15 +103,6 @@ const QString KEY_ARGS = QStringLiteral("args"); const QString KEY_PROPERTY = QStringLiteral("property"); const QString KEY_VALUE = QStringLiteral("value"); -const QString TYPE_SIGNAL = QStringLiteral("Qt.signal"); -const QString TYPE_PROPERTY_UPDATE = QStringLiteral("Qt.propertyUpdate"); -const QString TYPE_INIT = QStringLiteral("Qt.init"); -const QString TYPE_IDLE = QStringLiteral("Qt.idle"); -const QString TYPE_DEBUG = QStringLiteral("Qt.debug"); -const QString TYPE_INVOKE_METHOD = QStringLiteral("Qt.invokeMethod"); -const QString TYPE_CONNECT_TO_SIGNAL = QStringLiteral("Qt.connectToSignal"); -const QString TYPE_DISCONNECT_FROM_SIGNAL = QStringLiteral("Qt.disconnectFromSignal"); -const QString TYPE_SET_PROPERTY = QStringLiteral("Qt.setProperty"); QString objectId(const QObject *object) { @@ -238,7 +259,7 @@ void QMetaObjectPublisher::initializeClients() objectInfos[it.key()] = info; } } - webChannel->sendMessage(TYPE_INIT, objectInfos); + webChannel->sendMessage(TypeInit, objectInfos); propertyUpdatesInitialized = true; pendingInit = false; } @@ -311,7 +332,7 @@ void QMetaObjectPublisher::sendPendingPropertyUpdates() } pendingPropertyUpdates.clear(); - webChannel->sendMessage(TYPE_PROPERTY_UPDATE, data); + webChannel->sendMessage(TypePropertyUpdate, data); setClientIsIdle(false); } @@ -400,7 +421,7 @@ void QMetaObjectPublisher::signalEmitted(const QObject *object, const int signal #endif data[KEY_ARGS] = args; } - webChannel->sendMessage(TYPE_SIGNAL, data); + webChannel->sendMessage(TypeSignal, data); if (signalIndex == s_destroyedSignalIndex) { objectDestroyed(object); @@ -476,18 +497,18 @@ bool QMetaObjectPublisher::handleRequest(const QJsonObject &message) return false; } - const QString &type = payload.value(KEY_TYPE).toString(); - if (type == TYPE_IDLE) { + const Type type = toType(payload.value(KEY_TYPE)); + if (type == TypeIdle) { setClientIsIdle(true); return true; - } else if (type == TYPE_INIT) { + } else if (type == TypeInit) { if (!blockUpdates) { initializeClients(); } else { pendingInit = true; } return true; - } else if (type == TYPE_DEBUG) { + } else if (type == TypeDebug) { static QTextStream out(stdout); out << "DEBUG: " << payload.value(KEY_MESSAGE).toString() << endl; return true; @@ -499,15 +520,15 @@ bool QMetaObjectPublisher::handleRequest(const QJsonObject &message) return false; } - if (type == TYPE_INVOKE_METHOD) { + if (type == TypeInvokeMethod) { return invokeMethod(object, payload.value(KEY_METHOD).toInt(-1), payload.value(KEY_ARGS).toArray(), message.value(KEY_ID)); - } else if (type == TYPE_CONNECT_TO_SIGNAL) { + } else if (type == TypeConnectToSignal) { signalHandler.connectTo(object, payload.value(KEY_SIGNAL).toInt(-1)); return true; - } else if (type == TYPE_DISCONNECT_FROM_SIGNAL) { + } else if (type == TypeDisconnectFromSignal) { signalHandler.disconnectFrom(object, payload.value(KEY_SIGNAL).toInt(-1)); return true; - } else if (type == TYPE_SET_PROPERTY) { + } else if (type == TypeSetProperty) { const int propertyIdx = payload.value(KEY_PROPERTY).toInt(-1); QMetaProperty property = object->metaObject()->property(propertyIdx); if (!property.isValid()) { diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js index 9aa174b..33f4cf5 100644 --- a/src/webchannel/qwebchannel.js +++ b/src/webchannel/qwebchannel.js @@ -42,6 +42,18 @@ "use strict"; +var QWebChannelMessageTypes = { + signal: 1, + propertyUpdate: 2, + init: 3, + idle: 4, + debug: 5, + invokeMethod: 6, + connectToSignal: 7, + disconnectFromSignal: 8, + setProperty: 9, +}; + var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) { var channel = this; @@ -152,7 +164,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) var initialized = false; channel.subscribe( - "Qt.signal", + QWebChannelMessageTypes.signal, function(payload) { var object = window[payload.object] || channel.objectMap[payload.object]; if (object) { @@ -164,7 +176,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) ); channel.subscribe( - "Qt.propertyUpdate", + QWebChannelMessageTypes.propertyUpdate, function(payload) { for (var i in payload) { var data = payload[i]; @@ -175,12 +187,12 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) console.warn("Unhandled property update: " + data.object + "::" + data.signal); } } - setTimeout(function() { channel.exec({type: "Qt.idle"}); }, 0); + setTimeout(function() { channel.exec({type: QWebChannelMessageTypes.idle}); }, 0); } ); channel.subscribe( - "Qt.init", + QWebChannelMessageTypes.init, function(payload) { if (initialized) { return; @@ -194,16 +206,16 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) if (doneCallback) { doneCallback(channel); } - setTimeout(function() { channel.exec({type: "Qt.idle"}); }, 0); + setTimeout(function() { channel.exec({type: QWebChannelMessageTypes.idle}); }, 0); } ); channel.debug = function(message) { - channel.send({"data" : {"type" : "Qt.Debug", "message" : message}}); + channel.send({"data" : {"type" : QWebChannelMessageTypes.debug, "message" : message}}); }; - channel.exec({type:"Qt.init"}); + channel.exec({type: QWebChannelMessageTypes.init}); } }; @@ -265,7 +277,7 @@ function QObject(name, data, webChannel) if (!isPropertyNotifySignal) { // only required for "pure" signals, handled separately for properties in propertyUpdate webChannel.exec({ - type: "Qt.connectToSignal", + type: QWebChannelMessageTypes.connectToSignal, object: object.__id__, signal: signalIndex }); @@ -286,7 +298,7 @@ function QObject(name, data, webChannel) if (!isPropertyNotifySignal && object.__objectSignals__[signalIndex].length === 0) { // only required for "pure" signals, handled separately for properties in propertyUpdate webChannel.exec({ - type: "Qt.disconnectFromSignal", + type: QWebChannelMessageTypes.disconnectFromSignal, object: object.__id__, signal: signalIndex }); @@ -342,7 +354,12 @@ function QObject(name, data, webChannel) args.push(arguments[i]); } - webChannel.exec({"type": "Qt.invokeMethod", "object": object.__id__, "method": methodIdx, "args": args}, function(response) { + webChannel.exec({ + "type": QWebChannelMessageTypes.invokeMethod, + "object": object.__id__, + "method": methodIdx, + "args": args + }, function(response) { if ( (response !== undefined) && callback ) { (callback)(unwrapQObject(response)); } @@ -372,7 +389,12 @@ function QObject(name, data, webChannel) return; } object.__propertyCache__[propertyIndex] = value; - webChannel.exec({"type": "Qt.setProperty", "object": object.__id__, "property": propertyIndex, "value": value }); + webChannel.exec({ + "type": QWebChannelMessageTypes.setProperty, + "object": object.__id__, + "property": propertyIndex, + "value": value + }); }); object.__defineGetter__(propertyName, function () { diff --git a/tests/qml/WebChannelTest.qml b/tests/qml/WebChannelTest.qml index 59a0cab..9a0baa9 100644 --- a/tests/qml/WebChannelTest.qml +++ b/tests/qml/WebChannelTest.qml @@ -131,7 +131,7 @@ TestCase { verify(msg); verify(msg.data); verify(msg.data.type); - compare(msg.data.type, "Qt.init"); + compare(msg.data.type, qWebChannelMessageTypes.init); } function awaitIdle() @@ -139,7 +139,19 @@ TestCase { var msg = awaitMessage(); verify(msg); verify(msg.data); - compare(msg.data.type, "Qt.idle"); + compare(msg.data.type, qWebChannelMessageTypes.idle); verify(webChannel.test_clientIsIdle()) } + + property var qWebChannelMessageTypes: ({ + signal: 1, + propertyUpdate: 2, + init: 3, + idle: 4, + debug: 5, + invokeMethod: 6, + connectToSignal: 7, + disconnectFromSignal: 8, + setProperty: 9, + }); } diff --git a/tests/qml/data/grouping.html b/tests/qml/data/grouping.html index 60fa0f8..f2d7aec 100644 --- a/tests/qml/data/grouping.html +++ b/tests/qml/data/grouping.html @@ -5,7 +5,7 @@ <script type="text/javascript"> //BEGIN SETUP window.channel = createWebChannel(function(channel) { - channel.subscribe("Qt.propertyUpdate", function() { + channel.subscribe(QWebChannelMessageTypes.propertyUpdate, function() { channel.exec({label: "gotPropertyUpdate", values: [myObj.myProperty(), myOtherObj.foo(), myOtherObj.bar()]}); }); }); diff --git a/tests/qml/tst_metaobjectpublisher.qml b/tests/qml/tst_metaobjectpublisher.qml index fed52d8..e07f105 100644 --- a/tests/qml/tst_metaobjectpublisher.qml +++ b/tests/qml/tst_metaobjectpublisher.qml @@ -102,7 +102,7 @@ WebChannelTest { msg = awaitMessage(); verify(msg); verify(msg.data); - } while (msg.data.type === "Qt.idle"); + } while (msg.data.type === qWebChannelMessageTypes.idle); return msg; } @@ -145,7 +145,7 @@ WebChannelTest { webChannel.sendMessage("invokeMethod", "test"); var msg = awaitMessage(); - compare(msg.data.type, "Qt.invokeMethod"); + compare(msg.data.type, qWebChannelMessageTypes.invokeMethod); compare(msg.data.object, "myObj"); compare(msg.data.args, ["test"]); @@ -158,7 +158,7 @@ WebChannelTest { awaitInit(); var msg = awaitMessage(); - compare(msg.data.type, "Qt.connectToSignal"); + compare(msg.data.type, qWebChannelMessageTypes.connectToSignal); compare(msg.data.object, "myObj"); awaitIdle(); @@ -197,27 +197,27 @@ WebChannelTest { awaitInit(); var msg = awaitMessageSkipIdle(); - compare(msg.data.type, "Qt.invokeMethod"); + compare(msg.data.type, qWebChannelMessageTypes.invokeMethod); compare(msg.data.object, "myFactory"); verify(myFactory.lastObj); compare(myFactory.lastObj.objectName, "testObj"); msg = awaitMessageSkipIdle(); - compare(msg.data.type, "Qt.connectToSignal"); + compare(msg.data.type, qWebChannelMessageTypes.connectToSignal); verify(msg.data.object); var objId = msg.data.object; msg = awaitMessageSkipIdle(); - compare(msg.data.type, "Qt.connectToSignal"); + compare(msg.data.type, qWebChannelMessageTypes.connectToSignal); compare(msg.data.object, objId); msg = awaitMessageSkipIdle(); - compare(msg.data.type, "Qt.setProperty"); + compare(msg.data.type, qWebChannelMessageTypes.setProperty); compare(msg.data.object, objId); compare(myFactory.lastObj.myProperty, 42); msg = awaitMessageSkipIdle(); - compare(msg.data.type, "Qt.invokeMethod"); + compare(msg.data.type, qWebChannelMessageTypes.invokeMethod); compare(msg.data.object, objId); compare(msg.data.args, ["foobar"]); @@ -229,7 +229,7 @@ WebChannelTest { webChannel.sendMessage("triggerDelete"); msg = awaitMessageSkipIdle(); - compare(msg.data.type, "Qt.invokeMethod"); + compare(msg.data.type, qWebChannelMessageTypes.invokeMethod); compare(msg.data.object, objId); webChannel.sendMessage("report"); @@ -245,7 +245,7 @@ WebChannelTest { awaitInit(); var msg = awaitMessage(); - compare(msg.data.type, "Qt.connectToSignal"); + compare(msg.data.type, qWebChannelMessageTypes.connectToSignal); compare(msg.data.object, "myObj"); awaitIdle(); @@ -257,7 +257,7 @@ WebChannelTest { compare(msg.data.args, [42]); msg = awaitMessage(); - compare(msg.data.type, "Qt.disconnectFromSignal"); + compare(msg.data.type, qWebChannelMessageTypes.disconnectFromSignal); compare(msg.data.object, "myObj"); myObj.mySignal(0); |