source: trunk/src/declarative/debugger/qdeclarativedebugserver.cpp@ 930

Last change on this file since 930 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 12.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtDeclarative module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "private/qdeclarativedebugserver_p.h"
43#include "private/qdeclarativedebugservice_p.h"
44#include "private/qdeclarativedebugservice_p_p.h"
45#include "private/qdeclarativeengine_p.h"
46
47#include <QtCore/QDir>
48#include <QtCore/QPluginLoader>
49#include <QtCore/QStringList>
50
51#include <private/qobject_p.h>
52#include <private/qapplication_p.h>
53
54QT_BEGIN_NAMESPACE
55
56/*
57 QDeclarativeDebug Protocol (Version 1):
58
59 handshake:
60 1. Client sends
61 "QDeclarativeDebugServer" 0 version pluginNames
62 version: an int representing the highest protocol version the client knows
63 pluginNames: plugins available on client side
64 2. Server sends
65 "QDeclarativeDebugClient" 0 version pluginNames
66 version: an int representing the highest protocol version the client & server know
67 pluginNames: plugins available on server side. plugins both in the client and server message are enabled.
68 client plugin advertisement
69 1. Client sends
70 "QDeclarativeDebugServer" 1 pluginNames
71 server plugin advertisement
72 1. Server sends
73 "QDeclarativeDebugClient" 1 pluginNames
74 plugin communication:
75 Everything send with a header different to "QDeclarativeDebugServer" is sent to the appropriate plugin.
76 */
77
78const int protocolVersion = 1;
79
80
81class QDeclarativeDebugServerPrivate : public QObjectPrivate
82{
83 Q_DECLARE_PUBLIC(QDeclarativeDebugServer)
84public:
85 QDeclarativeDebugServerPrivate();
86
87 void advertisePlugins();
88
89 QDeclarativeDebugServerConnection *connection;
90 QHash<QString, QDeclarativeDebugService *> plugins;
91 QStringList clientPlugins;
92 bool gotHello;
93
94 static QDeclarativeDebugServerConnection *loadConnectionPlugin();
95};
96
97QDeclarativeDebugServerPrivate::QDeclarativeDebugServerPrivate() :
98 connection(0),
99 gotHello(false)
100{
101}
102
103void QDeclarativeDebugServerPrivate::advertisePlugins()
104{
105 if (!gotHello)
106 return;
107
108 QByteArray message;
109 {
110 QDataStream out(&message, QIODevice::WriteOnly);
111 out << QString(QLatin1String("QDeclarativeDebugClient")) << 1 << plugins.keys();
112 }
113 connection->send(message);
114}
115
116QDeclarativeDebugServerConnection *QDeclarativeDebugServerPrivate::loadConnectionPlugin()
117{
118 QStringList pluginCandidates;
119 const QStringList paths = QCoreApplication::libraryPaths();
120 foreach (const QString &libPath, paths) {
121 const QDir dir(libPath + QLatin1String("/qmltooling"));
122 if (dir.exists()) {
123 QStringList plugins(dir.entryList(QDir::Files));
124 foreach (const QString &pluginPath, plugins) {
125 pluginCandidates << dir.absoluteFilePath(pluginPath);
126 }
127 }
128 }
129
130 foreach (const QString &pluginPath, pluginCandidates) {
131 QPluginLoader loader(pluginPath);
132 if (!loader.load()) {
133 continue;
134 }
135 QDeclarativeDebugServerConnection *connection = 0;
136 if (QObject *instance = loader.instance())
137 connection = qobject_cast<QDeclarativeDebugServerConnection*>(instance);
138
139 if (connection)
140 return connection;
141 loader.unload();
142 }
143 return 0;
144}
145
146bool QDeclarativeDebugServer::hasDebuggingClient() const
147{
148 Q_D(const QDeclarativeDebugServer);
149 return d->connection
150 && d->connection->isConnected()
151 && d->gotHello;
152}
153
154QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
155{
156 static bool commandLineTested = false;
157 static QDeclarativeDebugServer *server = 0;
158
159 if (!commandLineTested) {
160 commandLineTested = true;
161
162#ifndef QDECLARATIVE_NO_DEBUG_PROTOCOL
163 QApplicationPrivate *appD = static_cast<QApplicationPrivate*>(QObjectPrivate::get(qApp));
164 // ### remove port definition when protocol is changed
165 int port = 0;
166 bool block = false;
167 bool ok = false;
168
169 // format: qmljsdebugger=port:3768[,block]
170 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
171 if (!QDeclarativeEnginePrivate::qml_debugging_enabled) {
172 const QString message =
173 QString::fromAscii("QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
174 "Debugging has not been enabled.").arg(
175 appD->qmljsDebugArgumentsString());
176 qWarning("%s", qPrintable(message));
177 return 0;
178 }
179
180 if (appD->qmljsDebugArgumentsString().indexOf(QLatin1String("port:")) == 0) {
181 int separatorIndex = appD->qmljsDebugArgumentsString().indexOf(QLatin1Char(','));
182 port = appD->qmljsDebugArgumentsString().mid(5, separatorIndex - 5).toInt(&ok);
183 }
184 block = appD->qmljsDebugArgumentsString().contains(QLatin1String("block"));
185
186 if (ok) {
187 server = new QDeclarativeDebugServer();
188
189 QDeclarativeDebugServerConnection *connection
190 = QDeclarativeDebugServerPrivate::loadConnectionPlugin();
191 if (connection) {
192 server->d_func()->connection = connection;
193
194 connection->setServer(server);
195 connection->setPort(port, block);
196 } else {
197 qWarning() << QString::fromAscii("QDeclarativeDebugServer: Ignoring\"-qmljsdebugger=%1\". "
198 "Remote debugger plugin has not been found.").arg(appD->qmljsDebugArgumentsString());
199 }
200
201 } else {
202 qWarning(QString::fromAscii("QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
203 "Format is -qmljsdebugger=port:<port>[,block]").arg(
204 appD->qmljsDebugArgumentsString()).toAscii().constData());
205 }
206 }
207#endif
208 }
209
210 return server;
211}
212
213QDeclarativeDebugServer::QDeclarativeDebugServer()
214: QObject(*(new QDeclarativeDebugServerPrivate))
215{
216}
217
218void QDeclarativeDebugServer::receiveMessage(const QByteArray &message)
219{
220 Q_D(QDeclarativeDebugServer);
221
222 QDataStream in(message);
223 if (!d->gotHello) {
224
225 QString name;
226 int op;
227 in >> name >> op;
228
229 if (name != QLatin1String("QDeclarativeDebugServer")
230 || op != 0) {
231 qWarning("QDeclarativeDebugServer: Invalid hello message");
232 d->connection->disconnect();
233 return;
234 }
235
236 int version;
237 in >> version >> d->clientPlugins;
238
239 QHash<QString, QDeclarativeDebugService*>::Iterator iter = d->plugins.begin();
240 for (; iter != d->plugins.end(); ++iter) {
241 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
242 if (d->clientPlugins.contains(iter.key()))
243 newStatus = QDeclarativeDebugService::Enabled;
244 iter.value()->d_func()->status = newStatus;
245 iter.value()->statusChanged(newStatus);
246 }
247
248 QByteArray helloAnswer;
249 {
250 QDataStream out(&helloAnswer, QIODevice::WriteOnly);
251 out << QString(QLatin1String("QDeclarativeDebugClient")) << 0 << protocolVersion << d->plugins.keys();
252 }
253 d->connection->send(helloAnswer);
254
255 d->gotHello = true;
256 qWarning("QDeclarativeDebugServer: Connection established");
257 } else {
258
259 QString debugServer(QLatin1String("QDeclarativeDebugServer"));
260
261 QString name;
262 in >> name;
263
264 if (name == debugServer) {
265 int op = -1;
266 in >> op;
267
268 if (op == 1) {
269 // Service Discovery
270 QStringList oldClientPlugins = d->clientPlugins;
271 in >> d->clientPlugins;
272
273 QHash<QString, QDeclarativeDebugService*>::Iterator iter = d->plugins.begin();
274 for (; iter != d->plugins.end(); ++iter) {
275 const QString pluginName = iter.key();
276 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
277 if (d->clientPlugins.contains(pluginName))
278 newStatus = QDeclarativeDebugService::Enabled;
279
280 if (oldClientPlugins.contains(pluginName)
281 != d->clientPlugins.contains(pluginName)) {
282 iter.value()->d_func()->status = newStatus;
283 iter.value()->statusChanged(newStatus);
284 }
285 }
286 } else {
287 qWarning("QDeclarativeDebugServer: Invalid control message %d", op);
288 }
289 } else {
290 QByteArray message;
291 in >> message;
292
293 QHash<QString, QDeclarativeDebugService *>::Iterator iter =
294 d->plugins.find(name);
295 if (iter == d->plugins.end()) {
296 qWarning() << "QDeclarativeDebugServer: Message received for missing plugin" << name;
297 } else {
298 (*iter)->messageReceived(message);
299 }
300 }
301 }
302}
303
304QList<QDeclarativeDebugService*> QDeclarativeDebugServer::services() const
305{
306 const Q_D(QDeclarativeDebugServer);
307 return d->plugins.values();
308}
309
310QStringList QDeclarativeDebugServer::serviceNames() const
311{
312 const Q_D(QDeclarativeDebugServer);
313 return d->plugins.keys();
314}
315
316bool QDeclarativeDebugServer::addService(QDeclarativeDebugService *service)
317{
318 Q_D(QDeclarativeDebugServer);
319 if (!service || d->plugins.contains(service->name()))
320 return false;
321
322 d->plugins.insert(service->name(), service);
323 d->advertisePlugins();
324
325 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
326 if (d->clientPlugins.contains(service->name()))
327 newStatus = QDeclarativeDebugService::Enabled;
328 service->d_func()->status = newStatus;
329 service->statusChanged(newStatus);
330 return true;
331}
332
333bool QDeclarativeDebugServer::removeService(QDeclarativeDebugService *service)
334{
335 Q_D(QDeclarativeDebugServer);
336 if (!service || !d->plugins.contains(service->name()))
337 return false;
338
339 d->plugins.remove(service->name());
340 d->advertisePlugins();
341
342 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::NotConnected;
343 service->d_func()->server = 0;
344 service->d_func()->status = newStatus;
345 service->statusChanged(newStatus);
346 return true;
347}
348
349void QDeclarativeDebugServer::sendMessage(QDeclarativeDebugService *service,
350 const QByteArray &message)
351{
352 Q_D(QDeclarativeDebugServer);
353 QByteArray msg;
354 {
355 QDataStream out(&msg, QIODevice::WriteOnly);
356 out << service->name() << message;
357 }
358 d->connection->send(msg);
359}
360
361QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.