1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmlenginedebugservice_p.h"
44 #include "qqmldebugstatesdelegate_p.h"
45 #include <private/qqmlboundsignal_p.h>
46 #include <qqmlengine.h>
47 #include <private/qqmlmetatype_p.h>
48 #include <qqmlproperty.h>
49 #include <private/qqmlproperty_p.h>
50 #include <private/qqmlbinding_p.h>
51 #include <private/qqmlcontext_p.h>
52 #include <private/qqmlwatcher_p.h>
53 #include <private/qqmlvaluetype_p.h>
54 #include <private/qqmlvmemetaobject_p.h>
55 #include <private/qqmlexpression_p.h>
57 #include <QtCore/qdebug.h>
58 #include <QtCore/qmetaobject.h>
62 Q_GLOBAL_STATIC(QQmlEngineDebugService, qmlEngineDebugService)
64 QQmlEngineDebugService *QQmlEngineDebugService::instance()
66 return qmlEngineDebugService();
69 QQmlEngineDebugService::QQmlEngineDebugService(QObject *parent)
70 : QQmlDebugService(QLatin1String("QDeclarativeEngine"), 1, parent),
71 m_watch(new QQmlWatcher(this)),
74 QObject::connect(m_watch, SIGNAL(propertyChanged(int,int,QMetaProperty,QVariant)),
75 this, SLOT(propertyChanged(int,int,QMetaProperty,QVariant)));
80 QQmlEngineDebugService::~QQmlEngineDebugService()
82 delete m_statesDelegate;
85 QDataStream &operator<<(QDataStream &ds,
86 const QQmlEngineDebugService::QQmlObjectData &data)
88 ds << data.url << data.lineNumber << data.columnNumber << data.idString
89 << data.objectName << data.objectType << data.objectId << data.contextId;
93 QDataStream &operator>>(QDataStream &ds,
94 QQmlEngineDebugService::QQmlObjectData &data)
96 ds >> data.url >> data.lineNumber >> data.columnNumber >> data.idString
97 >> data.objectName >> data.objectType >> data.objectId >> data.contextId;
101 QDataStream &operator<<(QDataStream &ds,
102 const QQmlEngineDebugService::QQmlObjectProperty &data)
104 ds << (int)data.type << data.name << data.value << data.valueTypeName
105 << data.binding << data.hasNotifySignal;
109 QDataStream &operator>>(QDataStream &ds,
110 QQmlEngineDebugService::QQmlObjectProperty &data)
113 ds >> type >> data.name >> data.value >> data.valueTypeName
114 >> data.binding >> data.hasNotifySignal;
115 data.type = (QQmlEngineDebugService::QQmlObjectProperty::Type)type;
119 static inline bool isSignalPropertyName(const QString &signalName)
121 // see QmlCompiler::isSignalPropertyName
122 return signalName.length() >= 3 && signalName.startsWith(QLatin1String("on")) &&
123 signalName.at(2).isLetter() && signalName.at(2).isUpper();
126 static bool hasValidSignal(QObject *object, const QString &propertyName)
128 if (!isSignalPropertyName(propertyName))
131 QString signalName = propertyName.mid(2);
132 signalName[0] = signalName.at(0).toLower();
134 int sigIdx = QQmlPropertyPrivate::findSignalByName(object->metaObject(), signalName.toLatin1()).methodIndex();
142 QQmlEngineDebugService::QQmlObjectProperty
143 QQmlEngineDebugService::propertyData(QObject *obj, int propIdx)
145 QQmlObjectProperty rv;
147 QMetaProperty prop = obj->metaObject()->property(propIdx);
149 rv.type = QQmlObjectProperty::Unknown;
150 rv.valueTypeName = QString::fromUtf8(prop.typeName());
151 rv.name = QString::fromUtf8(prop.name());
152 rv.hasNotifySignal = prop.hasNotifySignal();
153 QQmlAbstractBinding *binding =
154 QQmlPropertyPrivate::binding(QQmlProperty(obj, rv.name));
156 rv.binding = binding->expression();
158 if (QQmlValueTypeFactory::isValueType(prop.userType())) {
159 rv.type = QQmlObjectProperty::Basic;
160 } else if (QQmlMetaType::isQObject(prop.userType())) {
161 rv.type = QQmlObjectProperty::Object;
162 } else if (QQmlMetaType::isList(prop.userType())) {
163 rv.type = QQmlObjectProperty::List;
167 if (rv.type != QQmlObjectProperty::Unknown && prop.userType() != 0) {
168 value = prop.read(obj);
170 rv.value = valueContents(value);
175 QVariant QQmlEngineDebugService::valueContents(const QVariant &value) const
177 int userType = value.userType();
179 //QObject * is not streamable.
180 //Convert all such instances to a String value
182 if (value.type() == QVariant::List) {
183 QVariantList contents;
184 QVariantList list = value.toList();
185 int count = list.size();
186 for (int i = 0; i < count; i++)
187 contents << valueContents(list.at(i));
191 if (value.type() == QVariant::Map) {
192 QVariantMap contents;
193 QMapIterator<QString, QVariant> i(value.toMap());
194 while (i.hasNext()) {
196 contents.insert(i.key(), valueContents(i.value()));
201 if (QQmlValueTypeFactory::isValueType(userType))
204 if (QQmlMetaType::isQObject(userType)) {
205 QObject *o = QQmlMetaType::toQObject(value);
207 QString name = o->objectName();
209 name = QLatin1String("<unnamed object>");
214 return QLatin1String("<unknown value>");
217 void QQmlEngineDebugService::buildObjectDump(QDataStream &message,
218 QObject *object, bool recur, bool dumpProperties)
220 message << objectData(object);
222 QObjectList children = object->children();
224 int childrenCount = children.count();
225 for (int ii = 0; ii < children.count(); ++ii) {
226 if (qobject_cast<QQmlContext*>(children[ii]) || QQmlBoundSignal::cast(children[ii]))
230 message << childrenCount << recur;
232 QList<QQmlObjectProperty> fakeProperties;
234 for (int ii = 0; ii < children.count(); ++ii) {
235 QObject *child = children.at(ii);
236 if (qobject_cast<QQmlContext*>(child))
238 QQmlBoundSignal *signal = QQmlBoundSignal::cast(child);
242 QQmlObjectProperty prop;
243 prop.type = QQmlObjectProperty::SignalProperty;
244 prop.hasNotifySignal = false;
245 QQmlExpression *expr = signal->expression();
247 prop.value = expr->expression();
248 QObject *scope = expr->scopeObject();
250 QString methodName = QLatin1String(scope->metaObject()->method(signal->index()).name().constData());
251 if (!methodName.isEmpty()) {
252 prop.name = QLatin1String("on") + methodName[0].toUpper()
257 fakeProperties << prop;
260 buildObjectDump(message, child, recur, dumpProperties);
262 message << objectData(child);
266 if (!dumpProperties) {
271 QList<int> propertyIndexes;
272 for (int ii = 0; ii < object->metaObject()->propertyCount(); ++ii) {
273 if (object->metaObject()->property(ii).isScriptable())
274 propertyIndexes << ii;
277 message << propertyIndexes.size() + fakeProperties.count();
279 for (int ii = 0; ii < propertyIndexes.size(); ++ii)
280 message << propertyData(object, propertyIndexes.at(ii));
282 for (int ii = 0; ii < fakeProperties.count(); ++ii)
283 message << fakeProperties[ii];
286 void QQmlEngineDebugService::prepareDeferredObjects(QObject *obj)
288 qmlExecuteDeferred(obj);
290 QObjectList children = obj->children();
291 for (int ii = 0; ii < children.count(); ++ii) {
292 QObject *child = children.at(ii);
293 prepareDeferredObjects(child);
298 void QQmlEngineDebugService::buildObjectList(QDataStream &message, QQmlContext *ctxt)
300 QQmlContextData *p = QQmlContextData::get(ctxt);
302 QString ctxtName = ctxt->objectName();
303 int ctxtId = QQmlDebugService::idForObject(ctxt);
305 message << ctxtName << ctxtId;
309 QQmlContextData *child = p->childContexts;
312 child = child->nextChild;
317 child = p->childContexts;
319 buildObjectList(message, child->asQQmlContext());
320 child = child->nextChild;
323 // Clean deleted objects
324 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(ctxt);
325 for (int ii = 0; ii < ctxtPriv->instances.count(); ++ii) {
326 if (!ctxtPriv->instances.at(ii)) {
327 ctxtPriv->instances.removeAt(ii);
332 message << ctxtPriv->instances.count();
333 for (int ii = 0; ii < ctxtPriv->instances.count(); ++ii) {
334 message << objectData(ctxtPriv->instances.at(ii));
338 void QQmlEngineDebugService::buildStatesList(QQmlContext *ctxt, bool cleanList)
340 if (m_statesDelegate)
341 m_statesDelegate->buildStatesList(ctxt, cleanList);
344 QQmlEngineDebugService::QQmlObjectData
345 QQmlEngineDebugService::objectData(QObject *object)
347 QQmlData *ddata = QQmlData::get(object);
349 if (ddata && ddata->outerContext) {
350 rv.url = ddata->outerContext->url;
351 rv.lineNumber = ddata->lineNumber;
352 rv.columnNumber = ddata->columnNumber;
355 rv.columnNumber = -1;
358 QQmlContext *context = qmlContext(object);
360 QQmlContextData *cdata = QQmlContextData::get(context);
362 rv.idString = cdata->findObjectId(object);
365 rv.objectName = object->objectName();
366 rv.objectId = QQmlDebugService::idForObject(object);
367 rv.contextId = QQmlDebugService::idForObject(qmlContext(object));
369 QQmlType *type = QQmlMetaType::qmlType(object->metaObject());
371 QString typeName = type->qmlTypeName();
372 int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
373 rv.objectType = lastSlash < 0 ? typeName : typeName.mid(lastSlash+1);
375 rv.objectType = QString::fromUtf8(object->metaObject()->className());
376 int marker = rv.objectType.indexOf(QLatin1String("_QMLTYPE_"));
378 rv.objectType = rv.objectType.left(marker);
384 void QQmlEngineDebugService::messageReceived(const QByteArray &message)
386 QMetaObject::invokeMethod(this, "processMessage", Qt::QueuedConnection, Q_ARG(QByteArray, message));
389 void QQmlEngineDebugService::processMessage(const QByteArray &message)
391 QDataStream ds(message);
396 if (type == "LIST_ENGINES") {
401 QDataStream rs(&reply, QIODevice::WriteOnly);
402 rs << QByteArray("LIST_ENGINES_R");
403 rs << queryId << m_engines.count();
405 for (int ii = 0; ii < m_engines.count(); ++ii) {
406 QQmlEngine *engine = m_engines.at(ii);
408 QString engineName = engine->objectName();
409 int engineId = QQmlDebugService::idForObject(engine);
411 rs << engineName << engineId;
415 } else if (type == "LIST_OBJECTS") {
418 ds >> queryId >> engineId;
421 qobject_cast<QQmlEngine *>(QQmlDebugService::objectForId(engineId));
424 QDataStream rs(&reply, QIODevice::WriteOnly);
425 rs << QByteArray("LIST_OBJECTS_R") << queryId;
428 buildObjectList(rs, engine->rootContext());
429 buildStatesList(engine->rootContext(), true);
433 } else if (type == "FETCH_OBJECT") {
437 bool dumpProperties = true;
439 ds >> queryId >> objectId >> recurse >> dumpProperties;
441 QObject *object = QQmlDebugService::objectForId(objectId);
444 QDataStream rs(&reply, QIODevice::WriteOnly);
445 rs << QByteArray("FETCH_OBJECT_R") << queryId;
449 prepareDeferredObjects(object);
450 buildObjectDump(rs, object, recurse, dumpProperties);
454 } else if (type == "WATCH_OBJECT") {
458 ds >> queryId >> objectId;
459 bool ok = m_watch->addWatch(queryId, objectId);
462 QDataStream rs(&reply, QIODevice::WriteOnly);
463 rs << QByteArray("WATCH_OBJECT_R") << queryId << ok;
466 } else if (type == "WATCH_PROPERTY") {
471 ds >> queryId >> objectId >> property;
472 bool ok = m_watch->addWatch(queryId, objectId, property);
475 QDataStream rs(&reply, QIODevice::WriteOnly);
476 rs << QByteArray("WATCH_PROPERTY_R") << queryId << ok;
479 } else if (type == "WATCH_EXPR_OBJECT") {
484 ds >> queryId >> debugId >> expr;
485 bool ok = m_watch->addWatch(queryId, debugId, expr);
488 QDataStream rs(&reply, QIODevice::WriteOnly);
489 rs << QByteArray("WATCH_EXPR_OBJECT_R") << queryId << ok;
491 } else if (type == "NO_WATCH") {
495 m_watch->removeWatch(queryId);
496 } else if (type == "EVAL_EXPRESSION") {
501 ds >> queryId >> objectId >> expr;
503 QObject *object = QQmlDebugService::objectForId(objectId);
504 QQmlContext *context = qmlContext(object);
506 if (object && context) {
507 QQmlExpression exprObj(context, object, expr);
508 bool undefined = false;
509 QVariant value = exprObj.evaluate(&undefined);
511 result = QLatin1String("<undefined>");
513 result = valueContents(value);
515 result = QLatin1String("<unknown context>");
519 QDataStream rs(&reply, QIODevice::WriteOnly);
520 rs << QByteArray("EVAL_EXPRESSION_R") << queryId << result;
523 } else if (type == "SET_BINDING") {
525 QString propertyName;
530 ds >> objectId >> propertyName >> expr >> isLiteralValue;
531 if (!ds.atEnd()) { // backward compatibility from 2.1, 2.2
532 ds >> filename >> line;
534 setBinding(objectId, propertyName, expr, isLiteralValue, filename, line);
535 } else if (type == "RESET_BINDING") {
537 QString propertyName;
538 ds >> objectId >> propertyName;
539 resetBinding(objectId, propertyName);
540 } else if (type == "SET_METHOD_BODY") {
544 ds >> objectId >> methodName >> methodBody;
545 setMethodBody(objectId, methodName, methodBody);
549 void QQmlEngineDebugService::setBinding(int objectId,
550 const QString &propertyName,
551 const QVariant &expression,
557 QObject *object = objectForId(objectId);
558 QQmlContext *context = qmlContext(object);
560 if (object && context) {
561 QQmlProperty property(object, propertyName, context);
562 if (property.isValid()) {
564 bool inBaseState = true;
565 if (m_statesDelegate) {
566 m_statesDelegate->updateBinding(context, property, expression, isLiteralValue,
567 filename, line, column, &inBaseState);
571 if (isLiteralValue) {
572 property.write(expression);
573 } else if (hasValidSignal(object, propertyName)) {
574 QQmlExpression *qmlExpression = new QQmlExpression(context, object, expression.toString());
575 QQmlPropertyPrivate::setSignalExpression(property, qmlExpression);
576 qmlExpression->setSourceLocation(filename, line, column);
577 } else if (property.isProperty()) {
578 QQmlBinding *binding = new QQmlBinding(expression.toString(), object, context);
579 binding->setTarget(property);
580 binding->setSourceLocation(filename, line, column);
581 binding->setNotifyOnValueChanged(true);
582 QQmlAbstractBinding *oldBinding = QQmlPropertyPrivate::setBinding(property, binding);
584 oldBinding->destroy();
587 qWarning() << "QQmlEngineDebugService::setBinding: unable to set property" << propertyName << "on object" << object;
592 // not a valid property
594 if (m_statesDelegate)
595 ok = m_statesDelegate->setBindingForInvalidProperty(object, propertyName, expression, isLiteralValue);
597 qWarning() << "QQmlEngineDebugService::setBinding: unable to set property" << propertyName << "on object" << object;
602 void QQmlEngineDebugService::resetBinding(int objectId, const QString &propertyName)
604 QObject *object = objectForId(objectId);
605 QQmlContext *context = qmlContext(object);
607 if (object && context) {
608 if (object->property(propertyName.toLatin1()).isValid()) {
609 QQmlProperty property(object, propertyName);
610 QQmlAbstractBinding *oldBinding = QQmlPropertyPrivate::binding(property);
612 QQmlAbstractBinding *oldBinding = QQmlPropertyPrivate::setBinding(property, 0);
614 oldBinding->destroy();
616 if (property.isResettable()) {
617 // Note: this will reset the property in any case, without regard to states
618 // Right now almost no QQuickItem has reset methods for its properties (with the
619 // notable exception of QQuickAnchors), so this is not a big issue
620 // later on, setBinding does take states into account
623 // overwrite with default value
624 if (QQmlType *objType = QQmlMetaType::qmlType(object->metaObject())) {
625 if (QObject *emptyObject = objType->create()) {
626 if (emptyObject->property(propertyName.toLatin1()).isValid()) {
627 QVariant defaultValue = QQmlProperty(emptyObject, propertyName).read();
628 if (defaultValue.isValid()) {
629 setBinding(objectId, propertyName, defaultValue, true);
636 } else if (hasValidSignal(object, propertyName)) {
637 QQmlProperty property(object, propertyName, context);
638 QQmlPropertyPrivate::setSignalExpression(property, 0);
640 if (m_statesDelegate)
641 m_statesDelegate->resetBindingForInvalidProperty(object, propertyName);
646 void QQmlEngineDebugService::setMethodBody(int objectId, const QString &method, const QString &body)
648 QObject *object = objectForId(objectId);
649 QQmlContext *context = qmlContext(object);
650 if (!object || !context || !context->engine())
652 QQmlContextData *contextData = QQmlContextData::get(context);
656 QQmlPropertyData dummy;
657 QQmlPropertyData *prop =
658 QQmlPropertyCache::property(context->engine(), object, method, dummy);
660 if (!prop || !prop->isVMEFunction())
663 QMetaMethod metaMethod = object->metaObject()->method(prop->coreIndex);
664 QList<QByteArray> paramNames = metaMethod.parameterNames();
667 for (int ii = 0; ii < paramNames.count(); ++ii) {
668 if (ii != 0) paramStr.append(QLatin1String(","));
669 paramStr.append(QString::fromUtf8(paramNames.at(ii)));
672 QString jsfunction = QLatin1String("(function ") + method + QLatin1String("(") + paramStr +
673 QLatin1String(") {");
675 jsfunction += QLatin1String("\n})");
677 QQmlVMEMetaObject *vmeMetaObject =
678 static_cast<QQmlVMEMetaObject*>(QObjectPrivate::get(object)->metaObject);
679 Q_ASSERT(vmeMetaObject); // the fact we found the property above should guarentee this
681 int lineNumber = vmeMetaObject->vmeMethodLineNumber(prop->coreIndex);
682 vmeMetaObject->setVmeMethod(prop->coreIndex, QQmlExpressionPrivate::evalFunction(contextData, object, jsfunction, contextData->url.toString(), lineNumber));
685 void QQmlEngineDebugService::propertyChanged(int id, int objectId, const QMetaProperty &property, const QVariant &value)
688 QDataStream rs(&reply, QIODevice::WriteOnly);
690 rs << QByteArray("UPDATE_WATCH") << id << objectId << QByteArray(property.name()) << valueContents(value);
695 void QQmlEngineDebugService::addEngine(QQmlEngine *engine)
698 Q_ASSERT(!m_engines.contains(engine));
700 m_engines.append(engine);
703 void QQmlEngineDebugService::remEngine(QQmlEngine *engine)
706 Q_ASSERT(m_engines.contains(engine));
708 m_engines.removeAll(engine);
711 void QQmlEngineDebugService::objectCreated(QQmlEngine *engine, QObject *object)
714 Q_ASSERT(m_engines.contains(engine));
716 int engineId = QQmlDebugService::idForObject(engine);
717 int objectId = QQmlDebugService::idForObject(object);
720 QDataStream rs(&reply, QIODevice::WriteOnly);
722 rs << QByteArray("OBJECT_CREATED") << engineId << objectId;
726 void QQmlEngineDebugService::setStatesDelegate(QQmlDebugStatesDelegate *delegate)
728 m_statesDelegate = delegate;