Files
qt-creator/src/plugins/qmljsinspector/qmljsclientproxy.cpp

707 lines
20 KiB
C++
Raw Normal View History

2010-06-28 18:07:12 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
2010-06-28 18:07:12 +02:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2010-06-28 18:07:12 +02:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** 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.
2010-06-28 18:07:12 +02:00
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
2010-06-28 18:07:12 +02:00
**
**************************************************************************/
2010-06-28 18:05:24 +02:00
#include "qmljsclientproxy.h"
#include "qmljsprivateapi.h"
#include "qmljsinspectorclient.h"
#include "qmljsinspector.h"
2010-06-28 18:05:24 +02:00
#include <debugger/debuggerplugin.h>
#include <debugger/debuggerrunner.h>
#include <debugger/qml/qmlengine.h>
#include <debugger/qml/qmladapter.h>
#include <extensionsystem/pluginmanager.h>
2010-06-28 18:05:24 +02:00
#include <utils/qtcassert.h>
#include <projectexplorer/project.h>
2010-06-28 18:05:24 +02:00
#include <QUrl>
#include <QAbstractSocket>
2010-10-06 17:24:39 +02:00
2010-06-28 18:05:24 +02:00
using namespace QmlJSInspector::Internal;
ClientProxy::ClientProxy(Debugger::QmlAdapter *adapter, QObject *parent)
: QObject(parent)
, m_adapter(adapter)
, m_engineClient(0)
, m_inspectorClient(0)
, m_engineQueryId(0)
, m_contextQueryId(0)
, m_isConnected(false)
{
connectToServer();
}
ClientProxy::~ClientProxy()
{
m_adapter.data()->setEngineDebugClient(0);
m_adapter.data()->setCurrentSelectedDebugInfo(-1);
}
void ClientProxy::connectToServer()
{
QmlEngineDebugClient *client1 = new QDeclarativeEngineClient(
m_adapter.data()->connection());
QmlEngineDebugClient *client2 = new QmlDebuggerClient(
m_adapter.data()->connection());
connect(client1, SIGNAL(newStatus(QDeclarativeDebugClient::Status)),
SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
connect(client1, SIGNAL(newStatus(QDeclarativeDebugClient::Status)),
SLOT(engineClientStatusChanged(QDeclarativeDebugClient::Status)));
connect(client2, SIGNAL(newStatus(QDeclarativeDebugClient::Status)),
SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
connect(client2, SIGNAL(newStatus(QDeclarativeDebugClient::Status)),
SLOT(engineClientStatusChanged(QDeclarativeDebugClient::Status)));
m_inspectorClient =
new QmlJSInspectorClient(m_adapter.data()->connection(), this);
connect(m_inspectorClient,
SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
connect(m_inspectorClient, SIGNAL(currentObjectsChanged(QList<int>)),
SLOT(onCurrentObjectsChanged(QList<int>)));
connect(m_inspectorClient, SIGNAL(zoomToolActivated()),
SIGNAL(zoomToolActivated()));
connect(m_inspectorClient, SIGNAL(selectToolActivated()),
SIGNAL(selectToolActivated()));
connect(m_inspectorClient, SIGNAL(selectMarqueeToolActivated()),
SIGNAL(selectMarqueeToolActivated()));
connect(m_inspectorClient, SIGNAL(animationSpeedChanged(qreal)),
SIGNAL(animationSpeedChanged(qreal)));
connect(m_inspectorClient, SIGNAL(animationPausedChanged(bool)),
SIGNAL(animationPausedChanged(bool)));
connect(m_inspectorClient, SIGNAL(designModeBehaviorChanged(bool)),
SIGNAL(designModeBehaviorChanged(bool)));
connect(m_inspectorClient, SIGNAL(showAppOnTopChanged(bool)),
SIGNAL(showAppOnTopChanged(bool)));
connect(m_inspectorClient, SIGNAL(reloaded()), this,
SIGNAL(serverReloaded()));
connect(m_inspectorClient, SIGNAL(logActivity(QString,QString)),
m_adapter.data(), SLOT(logServiceActivity(QString,QString)));
updateConnected();
}
void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
{
QString serviceName;
if (sender()) {
serviceName = sender()->objectName();
}
if (m_adapter)
m_adapter.data()->logServiceStatusChange(serviceName, status);
updateConnected();
}
void ClientProxy::engineClientStatusChanged(QDeclarativeDebugClient::Status status)
{
if (status == QDeclarativeDebugClient::Enabled) {
m_engineClient = qobject_cast<QmlEngineDebugClient *>(sender());
connect(m_engineClient, SIGNAL(newObjects()), this, SLOT(newObjects()));
connect(m_engineClient, SIGNAL(result(quint32,QVariant,QByteArray)),
SLOT(onResult(quint32,QVariant,QByteArray)));
connect(m_engineClient, SIGNAL(valueChanged(int,QByteArray,QVariant)),
SLOT(objectWatchTriggered(int,QByteArray,QVariant)));
m_adapter.data()->setEngineDebugClient(m_engineClient);
updateConnected();
}
}
void ClientProxy::refreshObjectTree()
{
if (!m_contextQueryId) {
m_objectTreeQueryIds.clear();
queryEngineContext(m_engines.value(0).debugId());
}
}
void ClientProxy::onCurrentObjectsChanged(const QList<int> &debugIds,
bool requestIfNeeded)
{
QList<QmlDebugObjectReference> selectedItems;
2010-08-12 09:25:40 +02:00
foreach (int debugId, debugIds) {
QmlDebugObjectReference ref = objectReferenceForId(debugId);
if (ref.debugId() != -1) {
selectedItems << ref;
} else if (requestIfNeeded) {
// ### FIXME right now, there's no way in the protocol to
2010-08-12 09:25:40 +02:00
// a) get some item and know its parent (although that's possible
// by adding it to a separate plugin)
// b) add children to part of an existing tree.
2010-08-12 09:25:40 +02:00
// So the only choice that remains is to update the complete
// tree when we have an unknown debug id.
// break;
}
}
emit selectedItemsChanged(selectedItems);
}
void ClientProxy::setSelectedItemsByDebugId(const QList<int> &debugIds)
{
if (!isConnected())
return;
m_inspectorClient->setCurrentObjects(debugIds);
}
void ClientProxy::setSelectedItemsByObjectId(
const QList<QmlDebugObjectReference> &objectRefs)
{
if (isConnected()) {
QList<int> debugIds;
foreach (const QmlDebugObjectReference &ref, objectRefs) {
debugIds << ref.debugId();
}
m_inspectorClient->setCurrentObjects(debugIds);
}
}
void ClientProxy::addObjectToTree(const QmlDebugObjectReference &obj)
{
int count = m_rootObjects.count();
for (int i = 0; i < count; i++) {
if (m_rootObjects[i].insertObjectInTree(obj)) {
buildDebugIdHashRecursive(obj);
emit objectTreeUpdated();
break;
}
}
}
QmlDebugObjectReference ClientProxy::objectReferenceForId(int debugId) const
{
foreach (const QmlDebugObjectReference& it, m_rootObjects) {
QmlDebugObjectReference result = objectReferenceForId(debugId, it);
if (result.debugId() == debugId)
return result;
}
return QmlDebugObjectReference();
}
void ClientProxy::log(LogDirection direction, const QString &message)
{
QString msg;
if (direction == LogSend) {
msg += " sending ";
} else {
msg += " receiving ";
}
msg += message;
if (m_adapter)
m_adapter.data()->logServiceActivity("QDeclarativeDebug", msg);
}
QList<QmlDebugObjectReference>
QmlJSInspector::Internal::ClientProxy::rootObjectReference() const
{
return m_rootObjects;
}
QmlDebugObjectReference
ClientProxy::objectReferenceForId(int debugId,
const QmlDebugObjectReference &objectRef) const
2010-06-28 18:05:24 +02:00
{
if (objectRef.debugId() == debugId)
return objectRef;
foreach (const QmlDebugObjectReference &child, objectRef.children()) {
QmlDebugObjectReference result = objectReferenceForId(debugId, child);
if (result.debugId() == debugId)
return result;
}
return QmlDebugObjectReference();
2010-06-28 18:05:24 +02:00
}
QmlDebugObjectReference ClientProxy::objectReferenceForId(
const QString &objectId) const
{
if (!objectId.isEmpty() && objectId[0].isLower()) {
const QList<QmlDebugObjectReference> refs = objectReferences();
foreach (const QmlDebugObjectReference &ref, refs) {
if (ref.idString() == objectId)
return ref;
}
}
return QmlDebugObjectReference();
}
QmlDebugObjectReference ClientProxy::objectReferenceForLocation(
const int line, const int column) const
{
const QList<QmlDebugObjectReference> refs = objectReferences();
foreach (const QmlDebugObjectReference &ref, refs) {
if (ref.source().lineNumber() == line && ref.source().columnNumber() == column)
return ref;
}
return QmlDebugObjectReference();
}
QList<QmlDebugObjectReference> ClientProxy::objectReferences() const
2010-06-28 18:05:24 +02:00
{
QList<QmlDebugObjectReference> result;
foreach (const QmlDebugObjectReference &it, m_rootObjects) {
2010-08-30 18:40:56 +02:00
result.append(objectReferences(it));
}
return result;
2010-06-28 18:05:24 +02:00
}
QList<QmlDebugObjectReference>
ClientProxy::objectReferences(const QmlDebugObjectReference &objectRef) const
2010-06-28 18:05:24 +02:00
{
QList<QmlDebugObjectReference> result;
2010-08-30 18:40:56 +02:00
result.append(objectRef);
2010-06-28 18:05:24 +02:00
foreach (const QmlDebugObjectReference &child, objectRef.children()) {
2010-08-30 18:40:56 +02:00
result.append(objectReferences(child));
2010-06-28 18:05:24 +02:00
}
return result;
}
quint32 ClientProxy::setBindingForObject(int objectDebugId,
2010-07-08 15:44:35 +02:00
const QString &propertyName,
const QVariant &value,
bool isLiteralValue,
QString source,
int line)
2010-06-28 18:05:24 +02:00
{
if (objectDebugId == -1)
2010-07-08 16:51:25 +02:00
return false;
if (propertyName == QLatin1String("id"))
return false; // Crashes the QMLViewer.
2010-06-28 18:05:24 +02:00
if (!isConnected())
return false;
log(LogSend, QString("SET_BINDING %1 %2 %3 %4").arg(
QString::number(objectDebugId), propertyName, value.toString(),
QString(isLiteralValue ? "true" : "false")));
quint32 queryId = m_engineClient->setBindingForObject(
objectDebugId, propertyName, value.toString(), isLiteralValue,
source, line);
if (!queryId)
log(LogSend, QString("failed!"));
return queryId;
2010-06-28 18:05:24 +02:00
}
quint32 ClientProxy::setMethodBodyForObject(int objectDebugId,
const QString &methodName,
const QString &methodBody)
2010-07-08 16:51:25 +02:00
{
if (objectDebugId == -1)
return false;
if (!isConnected())
return false;
log(LogSend, QString("SET_METHOD_BODY %1 %2 %3").arg(
QString::number(objectDebugId), methodName, methodBody));
quint32 queryId = m_engineClient->setMethodBody(
objectDebugId, methodName, methodBody);
if (!queryId)
log(LogSend, QString("failed!"));
return queryId;
2010-07-08 16:51:25 +02:00
}
quint32 ClientProxy::resetBindingForObject(int objectDebugId,
const QString& propertyName)
{
if (objectDebugId == -1)
return false;
if (!isConnected())
return false;
log(LogSend, QString("RESET_BINDING %1 %2").arg(
QString::number(objectDebugId), propertyName));
quint32 queryId = m_engineClient->resetBindingForObject(
objectDebugId, propertyName);
if (!queryId)
log(LogSend, QString("failed!"));
return queryId;
}
quint32 ClientProxy::queryExpressionResult(int objectDebugId,
const QString &expr)
{
if (objectDebugId == -1)
return 0;
if (!isConnected())
return 0;
bool block = false;
if (m_adapter)
block = m_adapter.data()->disableJsDebugging(true);
log(LogSend, QString("EVAL_EXPRESSION %1 %2").arg(
QString::number(objectDebugId), expr));
quint32 queryId = m_engineClient->queryExpressionResult(objectDebugId, expr);
if (m_adapter)
m_adapter.data()->disableJsDebugging(block);
return queryId;
}
void ClientProxy::clearComponentCache()
{
if (isConnected())
m_inspectorClient->clearComponentCache();
}
bool ClientProxy::addObjectWatch(int objectDebugId)
{
if (objectDebugId == -1)
return false;
if (!isConnected())
return false;
// already set
if (m_objectWatches.contains(objectDebugId))
return true;
QmlDebugObjectReference ref = objectReferenceForId(objectDebugId);
if (ref.debugId() != objectDebugId)
return false;
// is flooding the debugging output log!
// log(LogSend, QString("WATCH_PROPERTY %1").arg(objectDebugId));
if (m_engineClient->addWatch(ref))
m_objectWatches.append(objectDebugId);
return false;
}
bool ClientProxy::isObjectBeingWatched(int objectDebugId)
{
return m_objectWatches.contains(objectDebugId);
}
void ClientProxy::objectWatchTriggered(int objectDebugId,
const QByteArray &propertyName,
const QVariant &propertyValue)
{
if (m_objectWatches.contains(objectDebugId))
emit propertyChanged(objectDebugId, propertyName, propertyValue);
}
bool ClientProxy::removeObjectWatch(int objectDebugId)
{
if (objectDebugId == -1)
return false;
if (!m_objectWatches.contains(objectDebugId))
return false;
if (!isConnected())
return false;
m_objectWatches.removeOne(objectDebugId);
return true;
}
void ClientProxy::removeAllObjectWatches()
{
foreach (int watchedObject, m_objectWatches)
removeObjectWatch(watchedObject);
}
2010-06-28 18:05:24 +02:00
void ClientProxy::queryEngineContext(int id)
{
if (id < 0)
return;
if (!isConnected())
return;
if (m_contextQueryId)
m_contextQueryId = 0;
2010-06-28 18:05:24 +02:00
log(LogSend, QString("LIST_OBJECTS %1").arg(QString::number(id)));
m_contextQueryId = m_engineClient->queryRootContexts(QmlDebugEngineReference(id));
2010-06-28 18:05:24 +02:00
}
void ClientProxy::contextChanged(const QVariant &value)
2010-06-28 18:05:24 +02:00
{
if (m_contextQueryId) {
m_contextQueryId = 0;
emit rootContext(value);
2010-06-28 18:05:24 +02:00
}
}
2010-06-28 18:05:24 +02:00
quint32 ClientProxy::fetchContextObject(const QmlDebugObjectReference& obj)
{
log(LogSend, QString("FETCH_OBJECT %1").arg(obj.idString()));
return m_engineClient->queryObject(obj);
}
void ClientProxy::fetchContextObjectRecursive(
const QmlDebugContextReference& context, bool clear)
{
if (!isConnected())
return;
if (clear) {
m_rootObjects.clear();
m_objectTreeQueryIds.clear();
}
foreach (const QmlDebugObjectReference & obj, context.objects()) {
quint32 queryId = fetchContextObject(obj);
if (queryId)
m_objectTreeQueryIds << queryId;
}
foreach (const QmlDebugContextReference& child, context.contexts()) {
fetchContextObjectRecursive(child, false);
}
2010-06-28 18:05:24 +02:00
}
void ClientProxy::onResult(quint32 queryId, const QVariant &value, const QByteArray &type)
2010-06-28 18:05:24 +02:00
{
if (type == "FETCH_OBJECT_R") {
log(LogReceive, QString("FETCH_OBJECT_R %1").arg(
qvariant_cast<QmlDebugObjectReference>(value).idString()));
} else {
log(LogReceive, QLatin1String(type));
}
if (m_objectTreeQueryIds.contains(queryId))
objectTreeFetched(queryId, value);
else if (queryId == m_engineQueryId)
updateEngineList(value);
else if (queryId == m_contextQueryId)
contextChanged(value);
else
emit result(queryId, value);
}
2010-06-28 18:05:24 +02:00
void ClientProxy::objectTreeFetched(quint32 queryId, const QVariant &result)
{
QmlDebugObjectReference obj = qvariant_cast<QmlDebugObjectReference>(result);
m_rootObjects.append(obj);
2010-06-28 18:05:24 +02:00
m_objectTreeQueryIds.removeOne(queryId);
if (m_objectTreeQueryIds.isEmpty()) {
int old_count = m_debugIdHash.count();
m_debugIdHash.clear();
m_debugIdHash.reserve(old_count + 1);
foreach (const QmlDebugObjectReference &it, m_rootObjects)
buildDebugIdHashRecursive(it);
emit objectTreeUpdated();
if (isConnected()) {
if (!m_inspectorClient->currentObjects().isEmpty())
onCurrentObjectsChanged(m_inspectorClient->currentObjects(), false);
m_inspectorClient->setObjectIdList(m_rootObjects);
}
}
2010-06-28 18:05:24 +02:00
}
void ClientProxy::buildDebugIdHashRecursive(const QmlDebugObjectReference& ref)
{
QUrl fileUrl = ref.source().url();
int lineNum = ref.source().lineNumber();
int colNum = ref.source().columnNumber();
int rev = 0;
2010-08-30 18:40:56 +02:00
// handle the case where the url contains the revision number encoded.
//(for object created by the debugger)
static QRegExp rx("(.*)_(\\d+):(\\d+)$");
if (rx.exactMatch(fileUrl.path())) {
fileUrl.setPath(rx.cap(1));
rev = rx.cap(2).toInt();
lineNum += rx.cap(3).toInt() - 1;
}
const QString filePath = InspectorUi::instance()->findFileInProject(fileUrl);
2010-08-30 18:40:56 +02:00
// append the debug ids in the hash
m_debugIdHash[qMakePair<QString, int>(filePath, rev)][qMakePair<int, int>(
lineNum, colNum)].append(ref.debugId());
foreach (const QmlDebugObjectReference &it, ref.children())
buildDebugIdHashRecursive(it);
}
void ClientProxy::reloadQmlViewer()
2010-06-28 18:05:24 +02:00
{
if (isConnected())
m_inspectorClient->reloadViewer();
}
void ClientProxy::setDesignModeBehavior(bool inDesignMode)
{
if (isConnected())
m_inspectorClient->setDesignModeBehavior(inDesignMode);
}
void ClientProxy::setAnimationSpeed(qreal slowDownFactor)
{
if (isConnected())
m_inspectorClient->setAnimationSpeed(slowDownFactor);
}
void ClientProxy::setAnimationPaused(bool paused)
{
if (isConnected())
m_inspectorClient->setAnimationPaused(paused);
}
void ClientProxy::changeToZoomTool()
{
if (isConnected())
m_inspectorClient->changeToZoomTool();
}
void ClientProxy::changeToSelectTool()
{
if (isConnected())
m_inspectorClient->changeToSelectTool();
2010-06-28 18:05:24 +02:00
}
void ClientProxy::changeToSelectMarqueeTool()
{
if (isConnected())
m_inspectorClient->changeToSelectMarqueeTool();
}
void ClientProxy::showAppOnTop(bool showOnTop)
{
if (isConnected())
m_inspectorClient->showAppOnTop(showOnTop);
}
void ClientProxy::createQmlObject(const QString &qmlText, int parentDebugId,
const QStringList &imports,
const QString &filename, int order)
{
if (isConnected())
m_inspectorClient->createQmlObject(qmlText, parentDebugId, imports,
filename, order);
}
void ClientProxy::destroyQmlObject(int debugId)
{
if (isConnected())
m_inspectorClient->destroyQmlObject(debugId);
}
void ClientProxy::reparentQmlObject(int debugId, int newParent)
{
if (isConnected())
m_inspectorClient->reparentQmlObject(debugId, newParent);
}
void ClientProxy::updateConnected()
{
bool isConnected = m_inspectorClient &&
m_inspectorClient->status() == QDeclarativeDebugClient::Enabled &&
m_engineClient &&
m_engineClient->status() == QDeclarativeDebugClient::Enabled;
if (isConnected != m_isConnected) {
m_isConnected = isConnected;
if (isConnected) {
emit connected();
reloadEngines();
} else {
emit disconnected();
}
}
}
2010-06-28 18:05:24 +02:00
void ClientProxy::reloadEngines()
{
if (!isConnected())
return;
2010-06-28 18:05:24 +02:00
emit aboutToReloadEngines();
log(LogSend, QString("LIST_ENGINES"));
m_engineQueryId = m_engineClient->queryAvailableEngines();
2010-06-28 18:05:24 +02:00
}
QList<QmlDebugEngineReference> ClientProxy::engines() const
2010-06-28 18:05:24 +02:00
{
return m_engines;
}
void ClientProxy::updateEngineList(const QVariant &value)
2010-06-28 18:05:24 +02:00
{
m_engines = qvariant_cast<QmlDebugEngineReferenceList>(value);
m_engineQueryId = 0;
2010-06-28 18:05:24 +02:00
emit enginesChanged();
}
Debugger::QmlAdapter *ClientProxy::qmlAdapter() const
{
return m_adapter.data();
}
bool ClientProxy::isConnected() const
{
return m_isConnected;
}
void ClientProxy::newObjects()
{
log(LogReceive, QString("OBJECT_CREATED"));
refreshObjectTree();
}