baseenginedebugclient.cpp 11.9 KB
Newer Older
hjk's avatar
hjk committed
1
/**************************************************************************
2
**
hjk's avatar
hjk committed
3
** This file is part of Qt Creator
4
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
10 11 12 13 14 15 16
**
** 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.
17 18
**
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20 21
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
22
** Other Usage
23
**
hjk's avatar
hjk committed
24 25
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
26
**
hjk's avatar
hjk committed
27
** If you have questions regarding the use of this file, please contact
28
** Nokia at qt-info@nokia.com.
29
**
hjk's avatar
hjk committed
30
**************************************************************************/
31

32 33
#include "baseenginedebugclient.h"
#include "qmldebugconstants.h"
34

35
namespace QmlDebug {
36

37
struct QmlObjectData {
38 39 40 41 42 43 44 45 46
    QUrl url;
    int lineNumber;
    int columnNumber;
    QString idString;
    QString objectName;
    QString objectType;
    int objectId;
    int contextId;
};
47

48
QDataStream &operator>>(QDataStream &ds, QmlObjectData &data)
49 50
{
    ds >> data.url >> data.lineNumber >> data.columnNumber >> data.idString
51
       >> data.objectName >> data.objectType >> data.objectId >> data.contextId;
52 53
    return ds;
}
54

55
struct QmlObjectProperty {
56 57 58 59 60 61 62 63
    enum Type { Unknown, Basic, Object, List, SignalProperty };
    Type type;
    QString name;
    QVariant value;
    QString valueTypeName;
    QString binding;
    bool hasNotifySignal;
};
64

65
QDataStream &operator>>(QDataStream &ds, QmlObjectProperty &data)
66 67 68 69
{
    int type;
    ds >> type >> data.name >> data.value >> data.valueTypeName
       >> data.binding >> data.hasNotifySignal;
70
    data.type = (QmlObjectProperty::Type)type;
71 72 73
    return ds;
}

74
void BaseEngineDebugClient::decode(QDataStream &ds,
Kai Koehne's avatar
Kai Koehne committed
75
                                   ObjectReference &o,
76
                                   bool simple)
77
{
78
    QmlObjectData data;
79
    ds >> data;
80 81
    int parentId = -1;
    if (objectName() == QLatin1String("QmlDebugger") &&
82
            serviceVersion() >= Constants::CURRENT_SUPPORTED_VERSION )
83
        ds >> parentId;
84
    o.m_debugId = data.objectId;
85
    o.m_className = data.objectType;
86 87 88 89 90 91
    o.m_idString = data.idString;
    o.m_name = data.objectName;
    o.m_source.m_url = data.url;
    o.m_source.m_lineNumber = data.lineNumber;
    o.m_source.m_columnNumber = data.columnNumber;
    o.m_contextDebugId = data.contextId;
92
    o.m_needsMoreData = simple;
93
    o.m_parentId = parentId;
94 95 96 97 98 99 100 101 102

    if (simple)
        return;

    int childCount;
    bool recur;
    ds >> childCount >> recur;

    for (int ii = 0; ii < childCount; ++ii) {
Kai Koehne's avatar
Kai Koehne committed
103
        o.m_children.append(ObjectReference());
104 105 106 107 108 109 110
        decode(ds, o.m_children.last(), !recur);
    }

    int propCount;
    ds >> propCount;

    for (int ii = 0; ii < propCount; ++ii) {
111
        QmlObjectProperty data;
112
        ds >> data;
Kai Koehne's avatar
Kai Koehne committed
113
        PropertyReference prop;
114 115 116 117 118 119
        prop.m_objectDebugId = o.m_debugId;
        prop.m_name = data.name;
        prop.m_binding = data.binding;
        prop.m_hasNotifySignal = data.hasNotifySignal;
        prop.m_valueTypeName = data.valueTypeName;
        switch (data.type) {
120 121 122
        case QmlObjectProperty::Basic:
        case QmlObjectProperty::List:
        case QmlObjectProperty::SignalProperty:
123 124 125 126
        {
            prop.m_value = data.value;
            break;
        }
127
        case QmlObjectProperty::Object:
128
        {
Kai Koehne's avatar
Kai Koehne committed
129
            ObjectReference obj;
130 131 132 133
            obj.m_debugId = prop.m_value.toInt();
            prop.m_value = qVariantFromValue(obj);
            break;
        }
134
        case QmlObjectProperty::Unknown:
135
            break;
136 137 138 139 140
        }
        o.m_properties << prop;
    }
}

141
void BaseEngineDebugClient::decode(QDataStream &ds,
Kai Koehne's avatar
Kai Koehne committed
142
                                   ContextReference &c)
143 144 145 146 147 148 149
{
    ds >> c.m_name >> c.m_debugId;

    int contextCount;
    ds >> contextCount;

    for (int ii = 0; ii < contextCount; ++ii) {
Kai Koehne's avatar
Kai Koehne committed
150
        c.m_contexts.append(ContextReference());
151 152 153 154 155 156 157
        decode(ds, c.m_contexts.last());
    }

    int objectCount;
    ds >> objectCount;

    for (int ii = 0; ii < objectCount; ++ii) {
Kai Koehne's avatar
Kai Koehne committed
158
        ObjectReference obj;
159 160 161 162 163 164
        decode(ds, obj, true);
        obj.m_contextDebugId = c.m_debugId;
        c.m_objects << obj;
    }
}

Kai Koehne's avatar
Kai Koehne committed
165
void BaseEngineDebugClient::statusChanged(ClientStatus status)
166
{
167
    emit newStatus(status);
168 169
}

170
void BaseEngineDebugClient::messageReceived(const QByteArray &data)
171 172
{
    QDataStream ds(data);
173
    int queryId;
174 175 176
    QByteArray type;
    ds >> type;

177 178 179 180
    if (type == "OBJECT_CREATED") {
        emit newObjects();
        return;
    }
181

182
    ds >> queryId;
183

184
    if (type == "LIST_ENGINES_R") {
185 186
        int count;
        ds >> count;
Kai Koehne's avatar
Kai Koehne committed
187
        QList<EngineReference> engines;
188
        for (int ii = 0; ii < count; ++ii) {
Kai Koehne's avatar
Kai Koehne committed
189
            EngineReference eng;
190 191 192
            ds >> eng.m_name;
            ds >> eng.m_debugId;
            engines << eng;
193
        }
194
        emit result(queryId, QVariant::fromValue(engines), type);
195
    } else if (type == "LIST_OBJECTS_R") {
Kai Koehne's avatar
Kai Koehne committed
196
        ContextReference rootContext;
197
        if (!ds.atEnd())
198
            decode(ds, rootContext);
199
        emit result(queryId, QVariant::fromValue(rootContext), type);
200
    } else if (type == "FETCH_OBJECT_R") {
Kai Koehne's avatar
Kai Koehne committed
201
        ObjectReference object;
202
        if (!ds.atEnd())
203
            decode(ds, object, false);
204
        emit result(queryId, QVariant::fromValue(object), type);
205 206 207
    } else if (type == "EVAL_EXPRESSION_R") {;
        QVariant exprResult;
        ds >> exprResult;
208
        emit result(queryId, exprResult, type);
209 210 211 212 213
    } else if (type == "WATCH_PROPERTY_R" ||
               type == "WATCH_OBJECT_R" ||
               type == "WATCH_EXPR_OBJECT_R") {
        bool valid;
        ds >> valid;
214
        emit result(queryId, valid, type);
215 216 217 218
    } else if (type == "UPDATE_WATCH") {
        int debugId;
        QByteArray name;
        QVariant value;
219 220
        ds >> debugId >> name >> value;
        emit valueChanged(debugId, name, value);
221 222 223
    }
}

224 225 226
BaseEngineDebugClient::BaseEngineDebugClient(const QString &clientName,
                     QmlDebugConnection *conn)
    : QmlDebugClient(clientName, conn),
227
      m_nextId(1)
228
{
229
    setObjectName(clientName);
230
}
231

Kai Koehne's avatar
Kai Koehne committed
232
quint32 BaseEngineDebugClient::addWatch(const PropertyReference &property)
233
{
234
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
235
    if (status() == Enabled) {
236
        id = getId();
237 238
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
239 240
        ds << QByteArray("WATCH_PROPERTY") << id << property.m_objectDebugId
           << property.m_name.toUtf8();
241
        sendMessage(message);
242
    }
243
    return id;
244 245
}

Kai Koehne's avatar
Kai Koehne committed
246
quint32 BaseEngineDebugClient::addWatch(const ContextReference &/*context*/,
247
                                       const QString &/*id*/)
248
{
249
    qWarning("QmlEngineDebugClient::addWatch(): Not implemented");
250 251 252
    return 0;
}

Kai Koehne's avatar
Kai Koehne committed
253
quint32 BaseEngineDebugClient::addWatch(const ObjectReference &object,
254
                                       const QString &expr)
255
{
256
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
257
    if (status() == Enabled) {
258
        id = getId();
259 260
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
261
        ds << QByteArray("WATCH_EXPR_OBJECT") << id << object.m_debugId << expr;
262
        sendMessage(message);
263
    }
264
    return id;
265 266
}

Kai Koehne's avatar
Kai Koehne committed
267
quint32 BaseEngineDebugClient::addWatch(const ObjectReference &object)
268
{
269
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
270
    if (status() == Enabled) {
271
        id = getId();
272 273
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
274
        ds << QByteArray("WATCH_OBJECT") << id << object.m_debugId;
275
        sendMessage(message);
276
    }
277
    return id;
278 279
}

Kai Koehne's avatar
Kai Koehne committed
280
quint32 BaseEngineDebugClient::addWatch(const FileReference &/*file*/)
281
{
282
    qWarning("QmlEngineDebugClient::addWatch(): Not implemented");
283 284 285
    return 0;
}

286
void BaseEngineDebugClient::removeWatch(quint32 id)
287
{
Kai Koehne's avatar
Kai Koehne committed
288
    if (status() == Enabled) {
289 290
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
291
        ds << QByteArray("NO_WATCH") << id;
292
        sendMessage(message);
293 294 295
    }
}

296
quint32 BaseEngineDebugClient::queryAvailableEngines()
297
{
298
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
299
    if (status() == Enabled) {
300
        id = getId();
301 302
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
303
        ds << QByteArray("LIST_ENGINES") << id;
304
        sendMessage(message);
305
    }
306
    return id;
307 308
}

Kai Koehne's avatar
Kai Koehne committed
309
quint32 BaseEngineDebugClient::queryRootContexts(const EngineReference &engine)
310
{
311
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
312
    if (status() == Enabled && engine.m_debugId != -1) {
313
        id = getId();
314 315
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
316
        ds << QByteArray("LIST_OBJECTS") << id << engine.m_debugId;
317
        sendMessage(message);
318
    }
319
    return id;
320 321
}

Kai Koehne's avatar
Kai Koehne committed
322
quint32 BaseEngineDebugClient::queryObject(const ObjectReference &object)
323
{
324
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
325
    if (status() == Enabled && object.m_debugId != -1) {
326
        id = getId();
327 328
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
329 330
        ds << QByteArray("FETCH_OBJECT") << id << object.m_debugId << false <<
              true;
331
        sendMessage(message);
332
    }
333
    return id;
334 335
}

Kai Koehne's avatar
Kai Koehne committed
336
quint32 BaseEngineDebugClient::queryObjectRecursive(const ObjectReference &object)
337
{
338
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
339
    if (status() == Enabled && object.m_debugId != -1) {
340
        id = getId();
341 342
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
343 344
        ds << QByteArray("FETCH_OBJECT") << id << object.m_debugId << true <<
              true;
345
        sendMessage(message);
346
    }
347
    return id;
348 349
}

350
quint32 BaseEngineDebugClient::queryExpressionResult(int objectDebugId,
351 352
                                                     const QString &expr,
                                                     int engineId)
353
{
354
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
355
    if (status() == Enabled && objectDebugId != -1) {
356
        id = getId();
357 358
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
359 360
        ds << QByteArray("EVAL_EXPRESSION") << id << objectDebugId << expr
           << engineId;
361
        sendMessage(message);
362
    }
363
    return id;
364 365
}

366
quint32 BaseEngineDebugClient::setBindingForObject(
367 368 369 370 371
        int objectDebugId,
        const QString &propertyName,
        const QVariant &bindingExpression,
        bool isLiteralValue,
        QString source, int line)
372
{
373
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
374
    if (status() == Enabled && objectDebugId != -1) {
375
        id = getId();
376 377
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
378 379
        ds << QByteArray("SET_BINDING") << objectDebugId << propertyName
           << bindingExpression << isLiteralValue << source << line;
380
        sendMessage(message);
381
    }
382
    return id;
383 384
}

385
quint32 BaseEngineDebugClient::resetBindingForObject(
386 387
        int objectDebugId,
        const QString &propertyName)
388
{
389
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
390
    if (status() == Enabled && objectDebugId != -1) {
391
        id = getId();
392 393 394
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
        ds << QByteArray("RESET_BINDING") << objectDebugId << propertyName;
395
        sendMessage(message);
396
    }
397
    return id;
398 399
}

400
quint32 BaseEngineDebugClient::setMethodBody(
401 402
        int objectDebugId, const QString &methodName,
        const QString &methodBody)
403
{
404
    quint32 id = 0;
Kai Koehne's avatar
Kai Koehne committed
405
    if (status() == Enabled && objectDebugId != -1) {
406
        id = getId();
407 408
        QByteArray message;
        QDataStream ds(&message, QIODevice::WriteOnly);
409 410
        ds << QByteArray("SET_METHOD_BODY") << objectDebugId
           << methodName << methodBody;
411
        sendMessage(message);
412
    }
413
    return id;
414 415
}

416
} // namespace QmlDebug