summaryrefslogtreecommitdiff
path: root/src/webchannel/qmetaobjectpublisher.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/webchannel/qmetaobjectpublisher.cpp')
-rw-r--r--src/webchannel/qmetaobjectpublisher.cpp61
1 files changed, 41 insertions, 20 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()) {