summaryrefslogtreecommitdiff
path: root/src/MetaObjectPublisher.qml
blob: 5c3cfca9c86e38ce055aff1bad100cfe58c14e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Copyright (C) 2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
** Contact: http://www.qt-project.org/legal
**
** 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

MetaObjectPublisherImpl
{
    id: publisher

    // The web channel this publisher works on.
    property var webChannel

    /**
     * This map contains the registered objects indexed by their name.
     */
    property variant registeredObjects: ({})
    /**
     * Tracks how many connections are active to object signals.
     *
     * Maps objectName -> signalName -> {handler: functor, subscribers: int}.
     */
    property var subscriberCountMap: ({})

    // Map of object names to maps of signal names to an array of all their properties.
    // The last value is an array as a signal can be the notify signal of multiple properties.
    property var signalToPropertyMap: ({})

    // Objects that changed their properties and are waiting for idle client.
    // map of object name to map of signal name to arguments
    property var pendingPropertyUpdates: ({})

    // true when the client is idle, false otherwise
    property bool clientIsIdle: false

    // true when no property updates should be sent, false otherwise
    property bool blockUpdates: false

    // true when at least one client needs to be initialized,
    // i.e. when a Qt.init came in which was not handled yet.
    property bool pendingInit: false

    // true when at least one client was initialized and thus
    // the property updates have been initialized and the
    // object info map set.
    property bool propertyUpdatesInitialized: false

    /**
     *    Wrap a result value if it's a Qt QObject
     *
     *    @return object info for wrapped Qt Object,
     *     or the same value if no wrapping needed
     *
     */
    function wrapResult(result)
    {
        if (typeof(result) === "object"
            && result["objectName"] !== undefined)
        {
            var ret = wrapObject(result);
            initializePropertyUpdates(ret.id, ret.data, result, webChannel);
            return ret;
        }
        return result;
    }

    function convertQMLArgsToJSArgs(qmlArgs)
    {
        // NOTE: QML arguments is a map not an array it seems...
        // so do the conversion manually
        var args = [];
        for (var i = 0; i < qmlArgs.length; ++i) {
            args.push(qmlArgs[i]);
        }
        return args;
    }

    /**
     * Handle the given WebChannel client request and potentially give a response.
     *
     * @return true if the request was handled, false otherwise.
     */
    function handleRequest(data)
    {
        var message = typeof(data) === "string" ? JSON.parse(data) : data;
        if (!message.data) {
            return false;
        }
        var payload = message.data;
        if (!payload.type) {
            return false;
        }

        if (payload.object) {
            var isWrapped = false;
            var object = registeredObjects[payload.object];
            if (!object) {
                object = unwrapObject(payload.object);
                if (object)
                    isWrapped = true;
                else
                    return false
            }

            if (payload.type === "Qt.invokeMethod") {
                var method = object[payload.method];
                if (method !== undefined) {
                    webChannel.respond(message.id,
                        wrapResult(method.apply(method, payload.args)));
                    return true;
                }
                if (isWrapped && payload.method === "deleteLater") {
                    // invoke `deleteLater` on wrapped QObject indirectly
                    deleteWrappedObject(object);
                    return true;
                }
                return false;
            }
            if (payload.type === "Qt.connectToSignal") {
                if (object.hasOwnProperty(payload.signal)) {
                    subscriberCountMap =  subscriberCountMap || {};
                    subscriberCountMap[payload.object] = subscriberCountMap[payload.object] || {};

                    // if no one is connected, connect.
                    if (!subscriberCountMap[payload.object].hasOwnProperty(payload.signal)) {
                        subscriberCountMap[payload.object][payload.signal] = {
                            subscribers: 1,
                            handler: function() {
                                var args = convertQMLArgsToJSArgs(arguments);
                                webChannel.sendMessage("Qt.signal", {
                                    object: payload.object,
                                    signal: payload.signal,
                                    args: args
                                });
                            }
                        };
                        object[payload.signal].connect(subscriberCountMap[payload.object][payload.signal].handler);
                    } else {
                        ++subscriberCountMap[payload.object][payload.signal].subscribers;
                    }
                    return true;
                }
                // connecting to `destroyed` signal of wrapped QObject
                if (isWrapped && payload.signal === "destroyed") {
                    // is a no-op on this side
                    return true;
                }
                return false;
            }
            if (payload.type === "Qt.disconnectFromSignal") {
                if (!object.hasOwnProperty(payload.signal)) {
                    return false;
                }
                subscriberCountMap = subscriberCountMap || {};
                subscriberCountMap[payload.object] = subscriberCountMap[payload.object] || {};

                if (!subscriberCountMap[payload.object].hasOwnProperty(payload.signal)) {
                    return false
                }
                if (--subscriberCountMap[payload.object][payload.signal].subscribers === 0) {
                    object[payload.signal].disconnect(subscriberCountMap[payload.object][payload.signal].handler);
                    delete subscriberCountMap[payload.object][payload.signal];
                }
                return true;
            }
            if (payload.type === "Qt.setProperty") {
                object[payload.property] = payload.value;
                return true;
            }
        }
        if (payload.type === "Qt.idle") {
            clientIsIdle = true;
            return true;
        }
        if (payload.type === "Qt.init") {
            if (!blockUpdates) {
                initializeClients();
            } else {
                pendingInit = true;
            }
            return true;
        }
        if (payload.type === "Qt.Debug") {
            console.log("DEBUG: ", payload.message);
            return true;
        }
        return false;
    }

    function registerObjects(objects)
    {
        if (propertyUpdatesInitialized) {
            console.error("Registered new object after initialization. This does not work!");
        }
        // joining a JS map and a QML one is not as easy as one would assume...
        // NOTE: the extra indirection via "merged" is required, using registeredObjects directly
        //       does not work! this looks like a QML/v8 bug to me, but I could not find a
        //       standalone testcase which reproduces this behavior :(
        var merged = registeredObjects;
        for (var name in objects) {
            if (!merged.hasOwnProperty(name)) {
                merged[name] = objects[name];
            }
        }
        registeredObjects = merged;
    }

    function initializeClients()
    {
        var objectInfos = classInfoForObjects(registeredObjects);
        webChannel.sendMessage("Qt.init", objectInfos);
        if (!propertyUpdatesInitialized) {
            for (var objectName in objectInfos) {
                var objectInfo = objectInfos[objectName];
                var object = registeredObjects[objectName];
                initializePropertyUpdates(objectName, objectInfo, object);
            }
            propertyUpdatesInitialized = true;
        }
        pendingInit = false;
    }

    // This function goes through all properties of all objects and connects against
    // their notify signal.
    // When receiving a notify signal, it will send a Qt.propertyUpdate message to the
    // server.
    function initializePropertyUpdates(objectName, objectInfo, object)
    {
        for (var propertyIndex in objectInfo.properties) {
            var propertyInfo = objectInfo.properties[propertyIndex];
            var propertyName = propertyInfo[0];
            var signalName = propertyInfo[1];

            if (!signalName) // Property without NOTIFY signal
                continue;

            if (signalName === 1) {
                /// signal name is optimized away, reconstruct the actual name
                signalName = propertyName + "Changed";
            }

            signalToPropertyMap[objectName] = signalToPropertyMap[objectName] || {};
            signalToPropertyMap[objectName][signalName] = signalToPropertyMap[objectName][signalName] || [];
            var connectedProperties = signalToPropertyMap[objectName][signalName];
            var numConnectedProperties = connectedProperties === undefined ? 0 : connectedProperties.length;

            // Only connect for a property update once
            if (numConnectedProperties === 0) {
                (function(signalName) {
                    object[signalName].connect(function() {
                        pendingPropertyUpdates[objectName] = pendingPropertyUpdates[objectName] || {};
                        pendingPropertyUpdates[objectName][signalName] = arguments;
                    });
                })(signalName);
            }

            if (connectedProperties.indexOf(propertyName) === -1) {
                /// TODO: this ensures that a given property is only once in
                ///       the list of connected properties.
                ///       This happens when multiple clients are connected to
                ///       a single webchannel. A better place for the initialization
                ///       should be found.
                connectedProperties.push(propertyName);
            }
        }
    }

    function sendPendingPropertyUpdates()
    {
        if (blockUpdates || !clientIsIdle) {
            return;
        }

        var data = [];
        for (var objectName in pendingPropertyUpdates) {
            var object = registeredObjects[objectName];
            if (!object) {
                object = unwrapObject(objectName);
                if (!object) {
                    console.error("Got property update for unknown object " + objectName);
                    continue;
                }
            }
            var signals = pendingPropertyUpdates[objectName];
            var propertyMap = {};
            for (var signalName in signals) {
                var propertyList = signalToPropertyMap[objectName][signalName];
                for (var propertyIndex in propertyList) {
                    var propertyName = propertyList[propertyIndex];
                    var propertyValue = object[propertyName];
                    propertyMap[propertyName] = propertyValue;
                }
                signals[signalName] = convertQMLArgsToJSArgs(signals[signalName]);
            }
            data.push({
                object: objectName,
                signals: signals,
                propertyMap: propertyMap
            });
        }
        pendingPropertyUpdates = {};
        if (data.length > 0) {
            webChannel.sendMessage("Qt.propertyUpdate", data);
            clientIsIdle = false;
        }
    }

    onBlockUpdatesChanged: {
        if (blockUpdates) {
            return;
        }

        if (pendingInit) {
            initializeClients();
        } else {
            sendPendingPropertyUpdates();
        }
    }

    onWrappedObjectDestroyed: { // (const QString& id)
        // act as if object had sent `destroyed` signal
        webChannel.sendMessage("Qt.signal", {
            object: id,
            signal: "destroyed",
            args: []
        });
        delete subscriberCountMap[id];
        delete pendingPropertyUpdates[id];
        delete signalToPropertyMap[id]
    }

    /**
     * Aggregate property updates since we get multiple Qt.idle message when we have multiple
     * clients. They all share the same QWebProcess though so we must take special care to
     * prevent message flooding.
     */
    Timer {
        id: propertyUpdateTimer
        /// TODO: what is the proper value here?
        interval: 50;
        running: !publisher.blockUpdates && publisher.clientIsIdle;
        repeat: true;
        onTriggered: publisher.sendPendingPropertyUpdates()
    }
}