QmlProfiler: Avoid overhead if profiling is not enabled
[profile/ivi/qtdeclarative.git] / src / qml / debugger / qqmldebugclient.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qqmldebugclient_p.h"
43
44 #include "qpacketprotocol_p.h"
45
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
48 #include <QtNetwork/qnetworkproxy.h>
49
50 #include <private/qobject_p.h>
51
52 QT_BEGIN_NAMESPACE
53
54 const int protocolVersion = 1;
55 const QString serverId = QLatin1String("QDeclarativeDebugServer");
56 const QString clientId = QLatin1String("QDeclarativeDebugClient");
57
58 class QQmlDebugClientPrivate : public QObjectPrivate
59 {
60     Q_DECLARE_PUBLIC(QQmlDebugClient)
61 public:
62     QQmlDebugClientPrivate();
63
64     QString name;
65     QQmlDebugConnection *connection;
66 };
67
68 class QQmlDebugConnectionPrivate : public QObject
69 {
70     Q_OBJECT
71 public:
72     QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
73     QQmlDebugConnection *q;
74     QPacketProtocol *protocol;
75     QIODevice *device;
76
77     bool gotHello;
78     QHash <QString, float> serverPlugins;
79     QHash<QString, QQmlDebugClient *> plugins;
80
81     void advertisePlugins();
82     void connectDeviceSignals();
83
84 public Q_SLOTS:
85     void connected();
86     void readyRead();
87     void deviceAboutToClose();
88 };
89
90 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
91     : QObject(c), q(c), protocol(0), device(0), gotHello(false)
92 {
93     protocol = new QPacketProtocol(q, this);
94     QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
95     QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
96 }
97
98 void QQmlDebugConnectionPrivate::advertisePlugins()
99 {
100     if (!q->isConnected())
101         return;
102
103     QPacket pack;
104     pack << serverId << 1 << plugins.keys();
105     protocol->send(pack);
106     q->flush();
107 }
108
109 void QQmlDebugConnectionPrivate::connected()
110 {
111     QPacket pack;
112     pack << serverId << 0 << protocolVersion << plugins.keys();
113     protocol->send(pack);
114     q->flush();
115 }
116
117 void QQmlDebugConnectionPrivate::readyRead()
118 {
119     if (!gotHello) {
120         QPacket pack = protocol->read();
121         QString name;
122
123         pack >> name;
124
125         bool validHello = false;
126         if (name == clientId) {
127             int op = -1;
128             pack >> op;
129             if (op == 0) {
130                 int version = -1;
131                 pack >> version;
132                 if (version == protocolVersion) {
133                     QStringList pluginNames;
134                     QList<float> pluginVersions;
135                     pack >> pluginNames;
136                     if (!pack.isEmpty())
137                         pack >> pluginVersions;
138
139                     const int pluginNamesSize = pluginNames.size();
140                     const int pluginVersionsSize = pluginVersions.size();
141                     for (int i = 0; i < pluginNamesSize; ++i) {
142                         float pluginVersion = 1.0;
143                         if (i < pluginVersionsSize)
144                             pluginVersion = pluginVersions.at(i);
145                         serverPlugins.insert(pluginNames.at(i), pluginVersion);
146                     }
147
148                     validHello = true;
149                 }
150             }
151         }
152
153         if (!validHello) {
154             qWarning("QQmlDebugConnection: Invalid hello message");
155             QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
156             return;
157         }
158         gotHello = true;
159
160         QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
161         for (; iter != plugins.end(); ++iter) {
162             QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
163             if (serverPlugins.contains(iter.key()))
164                 newState = QQmlDebugClient::Enabled;
165             iter.value()->stateChanged(newState);
166         }
167     }
168
169     while (protocol->packetsAvailable()) {
170         QPacket pack = protocol->read();
171         QString name;
172         pack >> name;
173
174         if (name == clientId) {
175             int op = -1;
176             pack >> op;
177
178             if (op == 1) {
179                 // Service Discovery
180                 QHash<QString, float> oldServerPlugins = serverPlugins;
181                 serverPlugins.clear();
182
183                 QStringList pluginNames;
184                 QList<float> pluginVersions;
185                 pack >> pluginNames;
186                 if (!pack.isEmpty())
187                     pack >> pluginVersions;
188
189                 const int pluginNamesSize = pluginNames.size();
190                 const int pluginVersionsSize = pluginVersions.size();
191                 for (int i = 0; i < pluginNamesSize; ++i) {
192                     float pluginVersion = 1.0;
193                     if (i < pluginVersionsSize)
194                         pluginVersion = pluginVersions.at(i);
195                     serverPlugins.insert(pluginNames.at(i), pluginVersion);
196                 }
197
198                 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
199                 for (; iter != plugins.end(); ++iter) {
200                     const QString pluginName = iter.key();
201                     QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
202                     if (serverPlugins.contains(pluginName))
203                         newSate = QQmlDebugClient::Enabled;
204
205                     if (oldServerPlugins.contains(pluginName)
206                             != serverPlugins.contains(pluginName)) {
207                         iter.value()->stateChanged(newSate);
208                     }
209                 }
210             } else {
211                 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
212             }
213         } else {
214             QByteArray message;
215             pack >> message;
216
217             QHash<QString, QQmlDebugClient *>::Iterator iter =
218                     plugins.find(name);
219             if (iter == plugins.end()) {
220                 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
221             } else {
222                 (*iter)->messageReceived(message);
223             }
224         }
225     }
226 }
227
228 void QQmlDebugConnectionPrivate::deviceAboutToClose()
229 {
230     // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
231     // without calling the underlying device close fn as that would cause an infinite loop
232     q->QIODevice::close();
233 }
234
235 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
236     : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
237 {
238 }
239
240 QQmlDebugConnection::~QQmlDebugConnection()
241 {
242     QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
243     for (; iter != d->plugins.end(); ++iter) {
244         iter.value()->d_func()->connection = 0;
245         iter.value()->stateChanged(QQmlDebugClient::NotConnected);
246     }
247 }
248
249 bool QQmlDebugConnection::isConnected() const
250 {
251     return state() == QAbstractSocket::ConnectedState;
252 }
253
254 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
255 {
256     return d->device->read(data, maxSize);
257 }
258
259 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
260 {
261     return d->device->write(data, maxSize);
262 }
263
264 qint64 QQmlDebugConnection::bytesAvailable() const
265 {
266     return d->device->bytesAvailable();
267 }
268
269 bool QQmlDebugConnection::isSequential() const
270 {
271     return true;
272 }
273
274 void QQmlDebugConnection::close()
275 {
276     if (isOpen()) {
277         QIODevice::close();
278         d->device->close();
279         emit stateChanged(QAbstractSocket::UnconnectedState);
280
281         QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
282         for (; iter != d->plugins.end(); ++iter) {
283             iter.value()->stateChanged(QQmlDebugClient::NotConnected);
284         }
285     }
286 }
287
288 bool QQmlDebugConnection::waitForConnected(int msecs)
289 {
290     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
291     if (socket)
292         return socket->waitForConnected(msecs);
293     return false;
294 }
295
296 QAbstractSocket::SocketState QQmlDebugConnection::state() const
297 {
298     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
299     if (socket)
300         return socket->state();
301
302     return QAbstractSocket::UnconnectedState;
303 }
304
305 void QQmlDebugConnection::flush()
306 {
307     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
308     if (socket) {
309         socket->flush();
310         return;
311     }
312 }
313
314 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
315 {
316     QTcpSocket *socket = new QTcpSocket(d);
317     socket->setProxy(QNetworkProxy::NoProxy);
318     d->device = socket;
319     d->connectDeviceSignals();
320     d->gotHello = false;
321     connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
322     connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
323     connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
324     socket->connectToHost(hostName, port);
325     QIODevice::open(ReadWrite | Unbuffered);
326 }
327
328 void QQmlDebugConnectionPrivate::connectDeviceSignals()
329 {
330     connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
331     connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
332     connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
333 }
334
335 //
336
337 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
338     : connection(0)
339 {
340 }
341
342 QQmlDebugClient::QQmlDebugClient(const QString &name, 
343                                                  QQmlDebugConnection *parent)
344     : QObject(*(new QQmlDebugClientPrivate), parent)
345 {
346     Q_D(QQmlDebugClient);
347     d->name = name;
348     d->connection = parent;
349
350     if (!d->connection)
351         return;
352
353     if (d->connection->d->plugins.contains(name)) {
354         qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
355         d->connection = 0;
356     } else {
357         d->connection->d->plugins.insert(name, this);
358         d->connection->d->advertisePlugins();
359     }
360 }
361
362 QQmlDebugClient::~QQmlDebugClient()
363 {
364     Q_D(QQmlDebugClient);
365     if (d->connection && d->connection->d) {
366         d->connection->d->plugins.remove(d->name);
367         d->connection->d->advertisePlugins();
368     }
369 }
370
371 QString QQmlDebugClient::name() const
372 {
373     Q_D(const QQmlDebugClient);
374     return d->name;
375 }
376
377 float QQmlDebugClient::serviceVersion() const
378 {
379     Q_D(const QQmlDebugClient);
380     if (d->connection->d->serverPlugins.contains(d->name))
381         return d->connection->d->serverPlugins.value(d->name);
382     return -1;
383 }
384
385 QQmlDebugClient::State QQmlDebugClient::state() const
386 {
387     Q_D(const QQmlDebugClient);
388     if (!d->connection
389             || !d->connection->isConnected()
390             || !d->connection->d->gotHello)
391         return NotConnected;
392
393     if (d->connection->d->serverPlugins.contains(d->name))
394         return Enabled;
395
396     return Unavailable;
397 }
398
399 void QQmlDebugClient::sendMessage(const QByteArray &message)
400 {
401     Q_D(QQmlDebugClient);
402     if (state() != Enabled)
403         return;
404
405     QPacket pack;
406     pack << d->name << message;
407     d->connection->d->protocol->send(pack);
408     d->connection->flush();
409 }
410
411 void QQmlDebugClient::stateChanged(State)
412 {
413 }
414
415 void QQmlDebugClient::messageReceived(const QByteArray &)
416 {
417 }
418
419 QT_END_NAMESPACE
420
421 #include <qqmldebugclient.moc>