From e3e4d6a18d63537459f0e616360e53e816927f76 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 5 Feb 2014 16:03:20 +0100 Subject: 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 Reviewed-by: Frederik Gladhorn --- src/webchannel/qmetaobjectpublisher.cpp | 61 ++++++++++++++++++++++----------- src/webchannel/qwebchannel.js | 44 ++++++++++++++++++------ 2 files changed, 74 insertions(+), 31 deletions(-) (limited to 'src') 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(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 () { -- cgit v1.2.1