summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2013-11-20 16:25:10 +0100
committerMilian Wolff <milian.wolff@kdab.com>2013-12-05 16:23:13 +0100
commitb6158dc3525c1c906d4040b2e88cd20feb21a2b2 (patch)
tree8facd266a1cf306445dc6a6ae6e9e932865286c8 /src
parentf75499f5fa5605183d37ddcf1532f7a22e602f60 (diff)
downloadqtwebchannel-b6158dc3525c1c906d4040b2e88cd20feb21a2b2.tar.gz
Optimize and cleanup code by moving WebChannel.qml logic into C++.
By handling more logic on the C++ side, such as serializing the JSON data to QByteArray, we can save up to 6% in the propertyUpdates benchmark. Furthermore, this allows for some more code cleanup and obsoletes the WebChannel.qml file. Also, it is a first step towards removing the QML dependency and making it optional alltogether. Change-Id: Id610b5f2652da4a7ad867aef576fabcc40d3d92c Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/WebChannel.qml63
-rw-r--r--src/qmldir1
-rw-r--r--src/qwebchannel.cpp34
-rw-r--r--src/qwebchannel.h7
-rw-r--r--src/qwebchannel_plugin.cpp2
-rw-r--r--src/qwebsocketserver.cpp8
-rw-r--r--src/qwebsocketserver.h8
-rw-r--r--src/src.pro6
8 files changed, 47 insertions, 82 deletions
diff --git a/src/WebChannel.qml b/src/WebChannel.qml
deleted file mode 100644
index e00926a..0000000
--- a/src/WebChannel.qml
+++ /dev/null
@@ -1,63 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
-**
-** This file is part of the QWebChannel module on Qt labs.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the Technology Preview License Agreement accompanying
-** this package.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 2.1 requirements
-** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
-**
-** In addition, as a special exception, Nokia gives you certain additional
-** rights. These rights are described in the Nokia Qt LGPL Exception
-** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
-**
-** If you have questions regarding the use of this file, please contact
-** Nokia at qt-info@nokia.com.
-**
-**
-**
-**
-**
-**
-**
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-import QtQuick 2.0
-import Qt.labs.WebChannel 1.0
-
-WebChannelImpl
-{
- function respond(messageId, data)
- {
- sendRawMessage(JSON.stringify({
- response: true,
- id: messageId,
- data: data
- }));
- }
-
- function sendMessage(id, data)
- {
- sendRawMessage(JSON.stringify({
- id: id,
- data: data
- }));
- }
-}
diff --git a/src/qmldir b/src/qmldir
index 0b5a511..f0b0992 100644
--- a/src/qmldir
+++ b/src/qmldir
@@ -1,4 +1,3 @@
module Qt.labs.WebChannel
plugin qwebchannel
MetaObjectPublisher 1.0 MetaObjectPublisher.qml
-WebChannel 1.0 WebChannel.qml \ No newline at end of file
diff --git a/src/qwebchannel.cpp b/src/qwebchannel.cpp
index fd52e7d..ac5f8f9 100644
--- a/src/qwebchannel.cpp
+++ b/src/qwebchannel.cpp
@@ -47,6 +47,8 @@
#include <QUuid>
#include <QStringList>
#include <QDebug>
+#include <QJsonDocument>
+#include <QJsonObject>
#include "qwebsocketserver.h"
@@ -77,6 +79,8 @@ public:
m_starting = true;
}
+ void sendJSONMessage(const QJsonValue& id, const QJsonValue& data, bool response) const;
+
signals:
void failed(const QString& reason);
void initialized();
@@ -125,6 +129,20 @@ void QWebChannelPrivate::socketError()
emit failed(errorString());
}
+void QWebChannelPrivate::sendJSONMessage(const QJsonValue& id, const QJsonValue& data, bool response) const
+{
+ QJsonObject obj;
+ if (response) {
+ obj[QStringLiteral("response")] = true;
+ }
+ obj[QStringLiteral("id")] = id;
+ if (!data.isNull()) {
+ obj[QStringLiteral("data")] = data;
+ }
+ QJsonDocument doc(obj);
+ sendMessage(doc.toJson(QJsonDocument::Compact));
+}
+
QWebChannel::QWebChannel(QObject *parent)
: QObject(parent)
, d(new QWebChannelPrivate(this))
@@ -168,12 +186,22 @@ void QWebChannel::onInitialized()
emit baseUrlChanged(d->m_baseUrl);
}
-void QWebChannel::sendRawMessage(const QString& message)
+void QWebChannel::respond(const QJsonValue& messageId, const QJsonValue& data) const
+{
+ d->sendJSONMessage(messageId, data, true);
+}
+
+void QWebChannel::sendMessage(const QJsonValue& id, const QJsonValue& data) const
+{
+ d->sendJSONMessage(id, data, false);
+}
+
+void QWebChannel::sendRawMessage(const QString& message) const
{
- d->sendMessage(message);
+ d->sendMessage(message.toUtf8());
}
-void QWebChannel::ping()
+void QWebChannel::ping() const
{
d->ping();
}
diff --git a/src/qwebchannel.h b/src/qwebchannel.h
index f8fa6a1..7f3ff13 100644
--- a/src/qwebchannel.h
+++ b/src/qwebchannel.h
@@ -46,6 +46,7 @@
#define QWEBCHANNEL_H
#include <QObject>
+#include <QJsonValue>
class QWebChannelPrivate;
@@ -74,8 +75,10 @@ signals:
void failed(const QString& reason);
public slots:
- void sendRawMessage(const QString& rawMessage);
- void ping();
+ void sendMessage(const QJsonValue& id, const QJsonValue& data = QJsonValue()) const;
+ void respond(const QJsonValue& messageId, const QJsonValue& data = QJsonValue()) const;
+ void sendRawMessage(const QString& rawMessage) const;
+ void ping() const;
private slots:
void onInitialized();
diff --git a/src/qwebchannel_plugin.cpp b/src/qwebchannel_plugin.cpp
index 131aee7..a5834ba 100644
--- a/src/qwebchannel_plugin.cpp
+++ b/src/qwebchannel_plugin.cpp
@@ -48,7 +48,7 @@
void QWebChannelPlugin::registerTypes(const char *uri)
{
- qmlRegisterType<QWebChannel>(uri, 1, 0, "WebChannelImpl");
+ qmlRegisterType<QWebChannel>(uri, 1, 0, "WebChannel");
qmlRegisterType<QtMetaObjectPublisher>(uri, 1, 0, "MetaObjectPublisherImpl");
}
diff --git a/src/qwebsocketserver.cpp b/src/qwebsocketserver.cpp
index 1364ed0..6e9858b 100644
--- a/src/qwebsocketserver.cpp
+++ b/src/qwebsocketserver.cpp
@@ -377,12 +377,12 @@ void QWebSocketServer::upgrade(QTcpSocket* socket, HeaderData& header)
header.wasUpgraded = true;
}
-void QWebSocketServer::sendMessage(const QString& message)
+void QWebSocketServer::sendMessage(const QByteArray& message) const
{
- sendFrame(Frame::TextFrame, message.toUtf8());
+ sendFrame(Frame::TextFrame, message);
}
-void QWebSocketServer::sendFrame(Frame::Opcode opcode, const QByteArray& data)
+void QWebSocketServer::sendFrame(Frame::Opcode opcode, const QByteArray& data) const
{
const QByteArray& header = frameHeader(opcode, data.size());
QHash< QTcpSocket*, Connection >::const_iterator it = m_connections.constBegin();
@@ -416,7 +416,7 @@ QByteArray QWebSocketServer::frameHeader(QWebSocketServer::Frame::Opcode opcode,
return header;
}
-void QWebSocketServer::ping()
+void QWebSocketServer::ping() const
{
sendFrame(Frame::Ping, QByteArray());
}
diff --git a/src/qwebsocketserver.h b/src/qwebsocketserver.h
index 4a5b281..3a57d2e 100644
--- a/src/qwebsocketserver.h
+++ b/src/qwebsocketserver.h
@@ -71,8 +71,8 @@ signals:
void pongReceived();
public slots:
- void sendMessage(const QString& message);
- void ping();
+ void sendMessage(const QByteArray& message) const;
+ void ping() const;
private slots:
void newConnection();
@@ -147,8 +147,8 @@ private:
bool readFrameData(QTcpSocket* socket, Frame& frame);
void handleFrame(QTcpSocket* socket, Frame& frame);
- void sendFrame(Frame::Opcode opcode, const QByteArray& data);
- void sendFrame(QTcpSocket* socket, Frame::Opcode opcode, const QByteArray& data);
+ void sendFrame(Frame::Opcode opcode, const QByteArray& data) const;
+ void sendFrame(QTcpSocket* socket, Frame::Opcode opcode, const QByteArray& data) const;
QByteArray frameHeader(Frame::Opcode opcode, const int dataSize) const;
QTcpServer* m_server;
diff --git a/src/src.pro b/src/src.pro
index bc61f5b..5b860ba 100644
--- a/src/src.pro
+++ b/src/src.pro
@@ -18,16 +18,14 @@ RESOURCES += \
OTHER_FILES = qmldir \
webchannel.js \
qobject.js \
- MetaObjectPublisher.qml \
- WebChannel.qml
+ MetaObjectPublisher.qml
target.path = $$[QT_INSTALL_QML]/$$TARGETPATH
# extra files that need to be deployed to $$TARGETPATH
DEPLOY_FILES = \
qmldir \
- MetaObjectPublisher.qml \
- WebChannel.qml
+ MetaObjectPublisher.qml
for(FILE, DEPLOY_FILES): qmldir.files += $$PWD/$$FILE
qmldir.path += $$[QT_INSTALL_QML]/$$TARGETPATH