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 QtDeclarative 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 "qdeclarativedebugserver_p.h"
43 #include "qdeclarativedebugservice_p.h"
44 #include "qdeclarativedebugservice_p_p.h"
45 #include <private/qdeclarativeengine_p.h>
47 #include <QtCore/QDir>
48 #include <QtCore/QPluginLoader>
49 #include <QtCore/QStringList>
50 #include <QtCore/qwaitcondition.h>
52 #include <private/qobject_p.h>
53 #include <private/qcoreapplication_p.h>
58 QDeclarativeDebug Protocol (Version 1):
62 "QDeclarativeDebugServer" 0 version pluginNames
63 version: an int representing the highest protocol version the client knows
64 pluginNames: plugins available on client side
66 "QDeclarativeDebugClient" 0 version pluginNames pluginVersions
67 version: an int representing the highest protocol version the client & server know
68 pluginNames: plugins available on server side. plugins both in the client and server message are enabled.
69 client plugin advertisement
71 "QDeclarativeDebugServer" 1 pluginNames
72 server plugin advertisement
74 "QDeclarativeDebugClient" 1 pluginNames pluginVersions
76 Everything send with a header different to "QDeclarativeDebugServer" is sent to the appropriate plugin.
79 const int protocolVersion = 1;
81 // print detailed information about loading of plugins
82 DEFINE_BOOL_CONFIG_OPTION(qmlDebugVerbose, QML_DEBUGGER_VERBOSE)
84 class QDeclarativeDebugServerThread;
86 class QDeclarativeDebugServerPrivate : public QObjectPrivate
88 Q_DECLARE_PUBLIC(QDeclarativeDebugServer)
90 QDeclarativeDebugServerPrivate();
92 void advertisePlugins();
93 QDeclarativeDebugServerConnection *loadConnectionPlugin(const QString &pluginName);
95 QDeclarativeDebugServerConnection *connection;
96 QHash<QString, QDeclarativeDebugService *> plugins;
97 mutable QReadWriteLock pluginsLock;
98 QStringList clientPlugins;
101 QMutex messageArrivedMutex;
102 QWaitCondition messageArrivedCondition;
103 QStringList waitingForMessageNames;
104 QDeclarativeDebugServerThread *thread;
105 QPluginLoader loader;
109 void _q_sendMessages(const QList<QByteArray> &messages);
112 class QDeclarativeDebugServerThread : public QThread
115 void setPluginName(const QString &pluginName) {
116 m_pluginName = pluginName;
119 void setPort(int port, bool block) {
127 QString m_pluginName;
132 QDeclarativeDebugServerPrivate::QDeclarativeDebugServerPrivate() :
137 // used in _q_sendMessages
138 qRegisterMetaType<QList<QByteArray> >("QList<QByteArray>");
141 void QDeclarativeDebugServerPrivate::advertisePlugins()
143 Q_Q(QDeclarativeDebugServer);
150 QDataStream out(&message, QIODevice::WriteOnly);
151 QStringList pluginNames;
152 QList<float> pluginVersions;
153 foreach (QDeclarativeDebugService *service, plugins.values()) {
154 pluginNames << service->name();
155 pluginVersions << service->version();
157 out << QString(QLatin1String("QDeclarativeDebugClient")) << 1 << pluginNames << pluginVersions;
160 QMetaObject::invokeMethod(q, "_q_sendMessages", Qt::QueuedConnection, Q_ARG(QList<QByteArray>, QList<QByteArray>() << message));
163 QDeclarativeDebugServerConnection *QDeclarativeDebugServerPrivate::loadConnectionPlugin(
164 const QString &pluginName)
166 #ifndef QT_NO_LIBRARY
167 QStringList pluginCandidates;
168 const QStringList paths = QCoreApplication::libraryPaths();
169 foreach (const QString &libPath, paths) {
170 const QDir dir(libPath + QLatin1String("/qmltooling"));
172 QStringList plugins(dir.entryList(QDir::Files));
173 foreach (const QString &pluginPath, plugins) {
174 if (QFileInfo(pluginPath).fileName().contains(pluginName))
175 pluginCandidates << dir.absoluteFilePath(pluginPath);
180 foreach (const QString &pluginPath, pluginCandidates) {
181 if (qmlDebugVerbose())
182 qDebug() << "QDeclarativeDebugServer: Trying to load plugin " << pluginPath << "...";
184 loader.setFileName(pluginPath);
185 if (!loader.load()) {
186 if (qmlDebugVerbose())
187 qDebug() << "QDeclarativeDebugServer: Error while loading: " << loader.errorString();
190 if (QObject *instance = loader.instance())
191 connection = qobject_cast<QDeclarativeDebugServerConnection*>(instance);
194 if (qmlDebugVerbose())
195 qDebug() << "QDeclarativeDebugServer: Plugin successfully loaded.";
200 if (qmlDebugVerbose())
201 qDebug() << "QDeclarativeDebugServer: Plugin does not implement interface QDeclarativeDebugServerConnection.";
209 void QDeclarativeDebugServerThread::run()
211 QDeclarativeDebugServer *server = QDeclarativeDebugServer::instance();
212 QDeclarativeDebugServerConnection *connection
213 = server->d_func()->loadConnectionPlugin(m_pluginName);
215 connection->setServer(QDeclarativeDebugServer::instance());
216 connection->setPort(m_port, m_block);
218 QCoreApplicationPrivate *appD = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp));
219 qWarning() << QString::fromAscii("QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
220 "Remote debugger plugin has not been found.").arg(appD->qmljsDebugArgumentsString());
225 // make sure events still waiting are processed
226 QEventLoop eventLoop;
227 eventLoop.processEvents(QEventLoop::AllEvents);
230 bool QDeclarativeDebugServer::hasDebuggingClient() const
232 Q_D(const QDeclarativeDebugServer);
234 && d->connection->isConnected()
238 static QDeclarativeDebugServer *qDeclarativeDebugServer = 0;
241 static void cleanup()
243 delete qDeclarativeDebugServer;
244 qDeclarativeDebugServer = 0;
247 QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
249 static bool commandLineTested = false;
251 if (!commandLineTested) {
252 commandLineTested = true;
254 QCoreApplicationPrivate *appD = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp));
255 #ifndef QDECLARATIVE_NO_DEBUG_PROTOCOL
256 // ### remove port definition when protocol is changed
261 // format: qmljsdebugger=port:3768[,block] OR qmljsdebugger=ost[,block]
262 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
263 if (!QDeclarativeEnginePrivate::qml_debugging_enabled) {
264 qWarning() << QString::fromLatin1(
265 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
266 "Debugging has not been enabled.").arg(
267 appD->qmljsDebugArgumentsString());
272 if (appD->qmljsDebugArgumentsString().indexOf(QLatin1String("port:")) == 0) {
273 int separatorIndex = appD->qmljsDebugArgumentsString().indexOf(QLatin1Char(','));
274 port = appD->qmljsDebugArgumentsString().mid(5, separatorIndex - 5).toInt(&ok);
275 pluginName = QLatin1String("qmldbg_tcp");
276 } else if (appD->qmljsDebugArgumentsString().contains(QLatin1String("ost"))) {
277 pluginName = QLatin1String("qmldbg_ost");
281 block = appD->qmljsDebugArgumentsString().contains(QLatin1String("block"));
284 qDeclarativeDebugServer = new QDeclarativeDebugServer();
285 QDeclarativeDebugServerThread *thread = new QDeclarativeDebugServerThread;
286 qDeclarativeDebugServer->d_func()->thread = thread;
287 qDeclarativeDebugServer->moveToThread(thread);
288 thread->setPluginName(pluginName);
289 thread->setPort(port, block);
293 QDeclarativeDebugServerPrivate *d = qDeclarativeDebugServer->d_func();
294 d->messageArrivedMutex.lock();
295 d->messageArrivedCondition.wait(&d->messageArrivedMutex);
296 d->messageArrivedMutex.unlock();
300 qWarning() << QString::fromLatin1(
301 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
302 "Format is -qmljsdebugger=port:<port>[,block]").arg(
303 appD->qmljsDebugArgumentsString());
307 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
308 qWarning() << QString::fromLatin1(
309 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
310 "QtDeclarative is not configured for debugging.").arg(
311 appD->qmljsDebugArgumentsString());
316 return qDeclarativeDebugServer;
319 QDeclarativeDebugServer::QDeclarativeDebugServer()
320 : QObject(*(new QDeclarativeDebugServerPrivate))
322 qAddPostRoutine(cleanup);
325 QDeclarativeDebugServer::~QDeclarativeDebugServer()
327 Q_D(QDeclarativeDebugServer);
329 QReadLocker(&d->pluginsLock);
331 foreach (QDeclarativeDebugService *service, d->plugins.values()) {
332 service->stateAboutToBeChanged(QDeclarativeDebugService::NotConnected);
333 service->d_func()->server = 0;
334 service->d_func()->state = QDeclarativeDebugService::NotConnected;
335 service->stateChanged(QDeclarativeDebugService::NotConnected);
344 delete d->connection;
347 void QDeclarativeDebugServer::receiveMessage(const QByteArray &message)
349 Q_D(QDeclarativeDebugServer);
351 QDataStream in(message);
356 if (name == QLatin1String("QDeclarativeDebugServer")) {
361 in >> version >> d->clientPlugins;
363 // Send the hello answer immediately, since it needs to arrive before
364 // the plugins below start sending messages.
365 QByteArray helloAnswer;
367 QDataStream out(&helloAnswer, QIODevice::WriteOnly);
368 QStringList pluginNames;
369 QList<float> pluginVersions;
370 foreach (QDeclarativeDebugService *service, d->plugins.values()) {
371 pluginNames << service->name();
372 pluginVersions << service->version();
375 out << QString(QLatin1String("QDeclarativeDebugClient")) << 0 << protocolVersion << pluginNames << pluginVersions;
377 d->connection->send(QList<QByteArray>() << helloAnswer);
381 QReadLocker(&d->pluginsLock);
382 QHash<QString, QDeclarativeDebugService*>::ConstIterator iter = d->plugins.constBegin();
383 for (; iter != d->plugins.constEnd(); ++iter) {
384 QDeclarativeDebugService::State newState = QDeclarativeDebugService::Unavailable;
385 if (d->clientPlugins.contains(iter.key()))
386 newState = QDeclarativeDebugService::Enabled;
387 iter.value()->d_func()->state = newState;
388 iter.value()->stateChanged(newState);
391 qWarning("QDeclarativeDebugServer: Connection established");
392 d->messageArrivedCondition.wakeAll();
394 } else if (op == 1) {
397 QStringList oldClientPlugins = d->clientPlugins;
398 in >> d->clientPlugins;
400 QReadLocker(&d->pluginsLock);
401 QHash<QString, QDeclarativeDebugService*>::ConstIterator iter = d->plugins.constBegin();
402 for (; iter != d->plugins.constEnd(); ++iter) {
403 const QString pluginName = iter.key();
404 QDeclarativeDebugService::State newState = QDeclarativeDebugService::Unavailable;
405 if (d->clientPlugins.contains(pluginName))
406 newState = QDeclarativeDebugService::Enabled;
408 if (oldClientPlugins.contains(pluginName)
409 != d->clientPlugins.contains(pluginName)) {
410 iter.value()->d_func()->state = newState;
411 iter.value()->stateChanged(newState);
416 qWarning("QDeclarativeDebugServer: Invalid control message %d", op);
417 d->connection->disconnect();
426 QReadLocker(&d->pluginsLock);
427 QHash<QString, QDeclarativeDebugService *>::Iterator iter = d->plugins.find(name);
428 if (iter == d->plugins.end()) {
429 qWarning() << "QDeclarativeDebugServer: Message received for missing plugin" << name;
431 (*iter)->messageReceived(message);
433 if (d->waitingForMessageNames.removeOne(name))
434 d->messageArrivedCondition.wakeAll();
437 qWarning("QDeclarativeDebugServer: Invalid hello message");
443 void QDeclarativeDebugServerPrivate::_q_sendMessages(const QList<QByteArray> &messages)
446 connection->send(messages);
449 QList<QDeclarativeDebugService*> QDeclarativeDebugServer::services() const
451 const Q_D(QDeclarativeDebugServer);
452 QReadLocker(&d->pluginsLock);
453 return d->plugins.values();
456 QStringList QDeclarativeDebugServer::serviceNames() const
458 const Q_D(QDeclarativeDebugServer);
459 QReadLocker(&d->pluginsLock);
460 return d->plugins.keys();
463 bool QDeclarativeDebugServer::addService(QDeclarativeDebugService *service)
465 Q_D(QDeclarativeDebugServer);
467 QWriteLocker(&d->pluginsLock);
468 if (!service || d->plugins.contains(service->name()))
470 d->plugins.insert(service->name(), service);
473 QReadLocker(&d->pluginsLock);
474 d->advertisePlugins();
475 QDeclarativeDebugService::State newState = QDeclarativeDebugService::Unavailable;
476 if (d->clientPlugins.contains(service->name()))
477 newState = QDeclarativeDebugService::Enabled;
478 service->d_func()->state = newState;
483 bool QDeclarativeDebugServer::removeService(QDeclarativeDebugService *service)
485 Q_D(QDeclarativeDebugServer);
487 QWriteLocker(&d->pluginsLock);
488 if (!service || !d->plugins.contains(service->name()))
490 d->plugins.remove(service->name());
493 QReadLocker(&d->pluginsLock);
494 QDeclarativeDebugService::State newState = QDeclarativeDebugService::NotConnected;
495 service->stateAboutToBeChanged(newState);
496 d->advertisePlugins();
497 service->d_func()->server = 0;
498 service->d_func()->state = newState;
499 service->stateChanged(newState);
505 void QDeclarativeDebugServer::sendMessages(QDeclarativeDebugService *service,
506 const QList<QByteArray> &messages)
508 QList<QByteArray> prefixedMessages;
509 foreach (const QByteArray &message, messages) {
511 QDataStream out(&prefixed, QIODevice::WriteOnly);
512 out << service->name() << message;
513 prefixedMessages << prefixed;
516 QMetaObject::invokeMethod(this, "_q_sendMessages", Qt::QueuedConnection, Q_ARG(QList<QByteArray>, prefixedMessages));
519 bool QDeclarativeDebugServer::waitForMessage(QDeclarativeDebugService *service)
521 Q_D(QDeclarativeDebugServer);
522 QReadLocker(&d->pluginsLock);
525 || !d->plugins.contains(service->name()))
528 d->messageArrivedMutex.lock();
529 d->waitingForMessageNames << service->name();
531 d->messageArrivedCondition.wait(&d->messageArrivedMutex);
532 } while (d->waitingForMessageNames.contains(service->name()));
533 d->messageArrivedMutex.unlock();
539 #include "moc_qdeclarativedebugserver_p.cpp"