diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-07-16 19:04:39 +0200 |
---|---|---|
committer | Milian Wolff <milian.wolff@kdab.com> | 2014-08-01 13:10:32 +0200 |
commit | 30e2e336a536786cf9a9de9736b0983df02fd041 (patch) | |
tree | e5fc3c5e149e091b49e143fadc62c525b6377355 | |
parent | f2f690cc2521285706135498c809be770c19813f (diff) | |
download | qtwebchannel-30e2e336a536786cf9a9de9736b0983df02fd041.tar.gz |
Properly disconnect a transport from the QtMetaObjectPublisher.
This fixes a bug, where messages from the transport would still be
send to the QtMetaObjectPublisher even though it was previously
disconnected.
I'll refactor this code eventually to get rid of QtMetaObjectPublisher
alltogether and merge its code into QWebChannelPrivate where
appropriate.
Change-Id: Ie0c35bd81a5e633bdcb6be55b64f947d4a545a59
Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Reviewed-by: Lutz Schönemann <lutz.schoenemann@basyskom.com>
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.cpp | 7 | ||||
-rw-r--r-- | src/webchannel/qwebchannel.cpp | 1 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.cpp | 8 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.h | 6 |
4 files changed, 21 insertions, 1 deletions
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index d9aa548..7c6c6eb 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -474,7 +474,7 @@ void QMetaObjectPublisher::deleteWrappedObject(QObject *object) const void QMetaObjectPublisher::broadcastMessage(const QJsonObject &message) const { if (webChannel->d_func()->transports.isEmpty()) { - qWarning("QWebChannel is not connected to any transports, cannot send messages."); + qWarning("QWebChannel is not connected to any transports, cannot send message: %s", QJsonDocument(message).toJson().constData()); return; } @@ -485,6 +485,11 @@ void QMetaObjectPublisher::broadcastMessage(const QJsonObject &message) const void QMetaObjectPublisher::handleMessage(const QJsonObject &message, QWebChannelAbstractTransport *transport) { + if (!webChannel->d_func()->transports.contains(transport)) { + qWarning() << "Refusing to handle message of unknown transport:" << transport; + return; + } + if (!message.contains(KEY_TYPE)) { qWarning("JSON message object is missing the type property: %s", QJsonDocument(message).toJson().constData()); return; diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp index a518d26..0d096b4 100644 --- a/src/webchannel/qwebchannel.cpp +++ b/src/webchannel/qwebchannel.cpp @@ -252,6 +252,7 @@ void QWebChannel::disconnectFrom(QWebChannelAbstractTransport *transport) const int idx = d->transports.indexOf(transport); if (idx != -1) { disconnect(transport, 0, this, 0); + disconnect(transport, 0, d->publisher, 0); d->transports.remove(idx); } } diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index 900a6bb..2de34c9 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -244,6 +244,14 @@ void TestWebChannel::testInvokeMethodConversion() } } +void TestWebChannel::testDisconnect() +{ + QWebChannel channel; + channel.connectTo(m_dummyTransport); + channel.disconnectFrom(m_dummyTransport); + m_dummyTransport->emitMessageReceived(QJsonObject()); +} + static QHash<QString, QObject*> createObjects(QObject *parent) { const int num = 100; diff --git a/tests/auto/webchannel/tst_webchannel.h b/tests/auto/webchannel/tst_webchannel.h index bd403dc..57e6b5d 100644 --- a/tests/auto/webchannel/tst_webchannel.h +++ b/tests/auto/webchannel/tst_webchannel.h @@ -56,6 +56,11 @@ public: {} ~DummyTransport() {}; + void emitMessageReceived(const QJsonObject &message) + { + emit messageReceived(message, this); + } + public slots: void sendMessage(const QJsonObject &/*message*/) Q_DECL_OVERRIDE { @@ -217,6 +222,7 @@ private slots: void testRegisterObjects(); void testInfoForObject(); void testInvokeMethodConversion(); + void testDisconnect(); void benchClassInfo(); void benchInitializeClients(); |