1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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
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
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_sendMessage(const QByteArray &message);
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() :
139 void QDeclarativeDebugServerPrivate::advertisePlugins()
141 Q_Q(QDeclarativeDebugServer);
148 QDataStream out(&message, QIODevice::WriteOnly);
149 out << QString(QLatin1String("QDeclarativeDebugClient")) << 1 << plugins.keys();
152 QMetaObject::invokeMethod(q, "_q_sendMessage", Qt::QueuedConnection, Q_ARG(QByteArray, message));
155 QDeclarativeDebugServerConnection *QDeclarativeDebugServerPrivate::loadConnectionPlugin(
156 const QString &pluginName)
158 #ifndef QT_NO_LIBRARY
159 QStringList pluginCandidates;
160 const QStringList paths = QCoreApplication::libraryPaths();
161 foreach (const QString &libPath, paths) {
162 const QDir dir(libPath + QLatin1String("/qmltooling"));
164 QStringList plugins(dir.entryList(QDir::Files));
165 foreach (const QString &pluginPath, plugins) {
166 if (QFileInfo(pluginPath).fileName().contains(pluginName))
167 pluginCandidates << dir.absoluteFilePath(pluginPath);
172 foreach (const QString &pluginPath, pluginCandidates) {
173 if (qmlDebugVerbose())
174 qDebug() << "QDeclarativeDebugServer: Trying to load plugin " << pluginPath << "...";
176 loader.setFileName(pluginPath);
177 if (!loader.load()) {
178 if (qmlDebugVerbose())
179 qDebug() << "QDeclarativeDebugServer: Error while loading: " << loader.errorString();
182 if (QObject *instance = loader.instance())
183 connection = qobject_cast<QDeclarativeDebugServerConnection*>(instance);
186 if (qmlDebugVerbose())
187 qDebug() << "QDeclarativeDebugServer: Plugin successfully loaded.";
192 if (qmlDebugVerbose())
193 qDebug() << "QDeclarativeDebugServer: Plugin does not implement interface QDeclarativeDebugServerConnection.";
201 void QDeclarativeDebugServerThread::run()
203 QDeclarativeDebugServer *server = QDeclarativeDebugServer::instance();
204 QDeclarativeDebugServerConnection *connection
205 = server->d_func()->loadConnectionPlugin(m_pluginName);
207 connection->setServer(QDeclarativeDebugServer::instance());
208 connection->setPort(m_port, m_block);
210 QCoreApplicationPrivate *appD = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp));
211 qWarning() << QString::fromAscii("QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
212 "Remote debugger plugin has not been found.").arg(appD->qmljsDebugArgumentsString());
218 bool QDeclarativeDebugServer::hasDebuggingClient() const
220 Q_D(const QDeclarativeDebugServer);
222 && d->connection->isConnected()
226 static QDeclarativeDebugServer *qDeclarativeDebugServer = 0;
228 QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
230 static bool commandLineTested = false;
232 if (!commandLineTested) {
233 commandLineTested = true;
235 QCoreApplicationPrivate *appD = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp));
236 #ifndef QDECLARATIVE_NO_DEBUG_PROTOCOL
237 // ### remove port definition when protocol is changed
242 // format: qmljsdebugger=port:3768[,block] OR qmljsdebugger=ost[,block]
243 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
244 if (!QDeclarativeEnginePrivate::qml_debugging_enabled) {
245 qWarning() << QString::fromLatin1(
246 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
247 "Debugging has not been enabled.").arg(
248 appD->qmljsDebugArgumentsString());
253 if (appD->qmljsDebugArgumentsString().indexOf(QLatin1String("port:")) == 0) {
254 int separatorIndex = appD->qmljsDebugArgumentsString().indexOf(QLatin1Char(','));
255 port = appD->qmljsDebugArgumentsString().mid(5, separatorIndex - 5).toInt(&ok);
256 pluginName = QLatin1String("qmldbg_tcp");
257 } else if (appD->qmljsDebugArgumentsString().contains(QLatin1String("ost"))) {
258 pluginName = QLatin1String("qmldbg_ost");
262 block = appD->qmljsDebugArgumentsString().contains(QLatin1String("block"));
265 qDeclarativeDebugServer = new QDeclarativeDebugServer();
267 QDeclarativeDebugServerThread *thread = new QDeclarativeDebugServerThread;
269 qDeclarativeDebugServer = new QDeclarativeDebugServer();
270 qDeclarativeDebugServer->d_func()->thread = thread;
271 qDeclarativeDebugServer->moveToThread(thread);
272 thread->setPluginName(pluginName);
273 thread->setPort(port, block);
277 QDeclarativeDebugServerPrivate *d = qDeclarativeDebugServer->d_func();
278 d->messageArrivedMutex.lock();
279 d->messageArrivedCondition.wait(&d->messageArrivedMutex);
280 d->messageArrivedMutex.unlock();
284 qWarning() << QString::fromLatin1(
285 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
286 "Format is -qmljsdebugger=port:<port>[,block]").arg(
287 appD->qmljsDebugArgumentsString());
291 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
292 qWarning() << QString::fromLatin1(
293 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
294 "QtDeclarative is not configured for debugging.").arg(
295 appD->qmljsDebugArgumentsString());
300 return qDeclarativeDebugServer;
303 QDeclarativeDebugServer::QDeclarativeDebugServer()
304 : QObject(*(new QDeclarativeDebugServerPrivate))
308 void QDeclarativeDebugServer::receiveMessage(const QByteArray &message)
310 Q_D(QDeclarativeDebugServer);
312 QDataStream in(message);
317 if (name == QLatin1String("QDeclarativeDebugServer")) {
322 in >> version >> d->clientPlugins;
324 // Send the hello answer immediately, since it needs to arrive before
325 // the plugins below start sending messages.
326 QByteArray helloAnswer;
328 QDataStream out(&helloAnswer, QIODevice::WriteOnly);
329 out << QString(QLatin1String("QDeclarativeDebugClient")) << 0 << protocolVersion << d->plugins.keys();
331 d->connection->send(helloAnswer);
335 QReadLocker(&d->pluginsLock);
336 QHash<QString, QDeclarativeDebugService*>::ConstIterator iter = d->plugins.constBegin();
337 for (; iter != d->plugins.constEnd(); ++iter) {
338 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
339 if (d->clientPlugins.contains(iter.key()))
340 newStatus = QDeclarativeDebugService::Enabled;
341 iter.value()->d_func()->status = newStatus;
342 iter.value()->statusChanged(newStatus);
345 qWarning("QDeclarativeDebugServer: Connection established");
346 d->messageArrivedCondition.wakeAll();
348 } else if (op == 1) {
351 QStringList oldClientPlugins = d->clientPlugins;
352 in >> d->clientPlugins;
354 QReadLocker(&d->pluginsLock);
355 QHash<QString, QDeclarativeDebugService*>::ConstIterator iter = d->plugins.constBegin();
356 for (; iter != d->plugins.constEnd(); ++iter) {
357 const QString pluginName = iter.key();
358 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
359 if (d->clientPlugins.contains(pluginName))
360 newStatus = QDeclarativeDebugService::Enabled;
362 if (oldClientPlugins.contains(pluginName)
363 != d->clientPlugins.contains(pluginName)) {
364 iter.value()->d_func()->status = newStatus;
365 iter.value()->statusChanged(newStatus);
370 qWarning("QDeclarativeDebugServer: Invalid control message %d", op);
371 d->connection->disconnect();
380 QReadLocker(&d->pluginsLock);
381 QHash<QString, QDeclarativeDebugService *>::Iterator iter = d->plugins.find(name);
382 if (iter == d->plugins.end()) {
383 qWarning() << "QDeclarativeDebugServer: Message received for missing plugin" << name;
385 (*iter)->messageReceived(message);
387 if (d->waitingForMessageNames.removeOne(name))
388 d->messageArrivedCondition.wakeAll();
391 qWarning("QDeclarativeDebugServer: Invalid hello message");
397 void QDeclarativeDebugServerPrivate::_q_sendMessage(const QByteArray &message)
400 connection->send(message);
403 QList<QDeclarativeDebugService*> QDeclarativeDebugServer::services() const
405 const Q_D(QDeclarativeDebugServer);
406 QReadLocker(&d->pluginsLock);
407 return d->plugins.values();
410 QStringList QDeclarativeDebugServer::serviceNames() const
412 const Q_D(QDeclarativeDebugServer);
413 QReadLocker(&d->pluginsLock);
414 return d->plugins.keys();
417 bool QDeclarativeDebugServer::addService(QDeclarativeDebugService *service)
419 Q_D(QDeclarativeDebugServer);
421 QWriteLocker(&d->pluginsLock);
422 if (!service || d->plugins.contains(service->name()))
424 d->plugins.insert(service->name(), service);
427 QReadLocker(&d->pluginsLock);
428 d->advertisePlugins();
429 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
430 if (d->clientPlugins.contains(service->name()))
431 newStatus = QDeclarativeDebugService::Enabled;
432 service->d_func()->status = newStatus;
437 bool QDeclarativeDebugServer::removeService(QDeclarativeDebugService *service)
439 Q_D(QDeclarativeDebugServer);
441 QWriteLocker(&d->pluginsLock);
442 if (!service || !d->plugins.contains(service->name()))
444 d->plugins.remove(service->name());
447 QReadLocker(&d->pluginsLock);
448 d->advertisePlugins();
449 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::NotConnected;
450 service->d_func()->server = 0;
451 service->d_func()->status = newStatus;
452 service->statusChanged(newStatus);
454 // Last service? Then stop thread & delete instance
455 if (d->plugins.isEmpty()) {
459 delete d->connection;
461 qDeclarativeDebugServer = 0;
469 void QDeclarativeDebugServer::sendMessage(QDeclarativeDebugService *service,
470 const QByteArray &message)
474 QDataStream out(&msg, QIODevice::WriteOnly);
475 out << service->name() << message;
478 QMetaObject::invokeMethod(this, "_q_sendMessage", Qt::QueuedConnection, Q_ARG(QByteArray, msg));
481 bool QDeclarativeDebugServer::waitForMessage(QDeclarativeDebugService *service)
483 Q_D(QDeclarativeDebugServer);
484 QReadLocker(&d->pluginsLock);
487 || !d->plugins.contains(service->name()))
490 d->messageArrivedMutex.lock();
491 d->waitingForMessageNames << service->name();
493 d->messageArrivedCondition.wait(&d->messageArrivedMutex);
494 } while (d->waitingForMessageNames.contains(service->name()));
495 d->messageArrivedMutex.unlock();
501 #include "moc_qdeclarativedebugserver_p.cpp"