summaryrefslogtreecommitdiff
path: root/src/webchannel/qwebchannel.js
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-03-21 16:15:47 +0100
committerPierre Rossi <pierre.rossi@gmail.com>2014-07-04 12:37:29 +0200
commit856dc072acb649be03e02ac64c81015d3f2675c2 (patch)
tree649985d553d6ea92360a66ca291af91c1114596e /src/webchannel/qwebchannel.js
parent9f78e0985d2f4fdc2588be57c5f25afdd59c6365 (diff)
downloadqtwebchannel-856dc072acb649be03e02ac64c81015d3f2675c2.tar.gz
Refactor code to use QWebChannelAbstractTransport and QtWebSockets.
This is a quite big changeset, but necessary to get the roadmap implemented that was discussed at QtCS. With this patchset landed, the QWebChannel does not depend on QtWebKit anymore, not even for the tests. Rather, we will introduce the dependency in the other way (i.e. QtWebKit will optionally use QtWebChannel if available). For the pure Qt/C++ use-case, we ship a utility implementation of a QWebChannelAbstractTransport that uses a QWebSocket for the server-client communication. This way, we can get rid of the custom WebSocket implementation. The tests are refactored to run the qwebchannel.js code directly inside QML. Integration tests for QtWebKit/QtWebEngine as well as examples will be added to these repositories. Change-Id: Icc1c1c5918ec46e31d5070937c14c4ca25a3e2d6 Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'src/webchannel/qwebchannel.js')
-rw-r--r--src/webchannel/qwebchannel.js30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js
index 278f423..291c10b 100644
--- a/src/webchannel/qwebchannel.js
+++ b/src/webchannel/qwebchannel.js
@@ -102,10 +102,8 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel)
this.socket.onmessage = this.messageReceived
setTimeout(this.initialized, 0);
} else {
- ///TODO: use ssl?
- var socketUrl = "ws://" + baseUrlOrSocket;
///TODO: use QWebChannel protocol, once custom protcols are supported by QtWebSocket
- this.socket = new WebSocket(socketUrl /*, "QWebChannel" */);
+ this.socket = new WebSocket(baseUrlOrSocket/*, "QWebChannel" */);
this.socket.onopen = this.initialized
this.socket.onclose = function()
@@ -147,7 +145,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel)
channel.send({"id": id, "data": data});
};
- this.objectMap = {};
+ this.objects = {};
this.initMetaObjectPublisher = function(doneCallback)
{
@@ -157,7 +155,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel)
channel.subscribe(
QWebChannelMessageTypes.signal,
function(payload) {
- var object = window[payload.object] || channel.objectMap[payload.object];
+ var object = channel.objects[payload.object];
if (object) {
object.signalEmitted(payload.signal, payload.args);
} else {
@@ -171,7 +169,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel)
function(payload) {
for (var i in payload) {
var data = payload[i];
- var object = window[data.object] || channel.objectMap[data.object];
+ var object = channel.objects[data.object];
if (object) {
object.propertyUpdate(data.signals, data.properties);
} else {
@@ -192,7 +190,6 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel)
for (var objectName in payload) {
var data = payload[objectName];
var object = new QObject(objectName, data, channel);
- window[objectName] = object;
}
if (doneCallback) {
doneCallback(channel);
@@ -213,7 +210,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel)
function QObject(name, data, webChannel)
{
this.__id__ = name;
- webChannel.objectMap[name] = this;
+ webChannel.objects[name] = this;
// List of callbacks that get invoked upon signal emission
this.__objectSignals__ = {};
@@ -233,18 +230,23 @@ function QObject(name, data, webChannel)
return response;
}
var objectId = response.id;
- if (webChannel.objectMap[objectId])
- return webChannel.objectMap[objectId];
+ if (webChannel.objects[objectId])
+ return webChannel.objects[objectId];
var qObject = new QObject( objectId, response.data, webChannel );
qObject.destroyed.connect(function() {
- if (webChannel.objectMap[objectId] === qObject) {
- delete webChannel.objectMap[objectId];
+ if (webChannel.objects[objectId] === qObject) {
+ delete webChannel.objects[objectId];
// reset the now deleted QObject to an empty {} object
// just assigning {} though would not have the desired effect, but the
// below also ensures all external references will see the empty map
- for (var prop in qObject) {
- delete qObject[prop];
+ // NOTE: this detour is necessary to workaround QTBUG-40021
+ var propertyNames = [];
+ for (var propertyName in qObject) {
+ propertyNames.push(propertyName);
+ }
+ for (var idx in propertyNames) {
+ delete qObject[propertyNames[idx]];
}
}
});