1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qqmlenginecontrolclient_p.h"
41#include "qqmlenginecontrolclient_p_p.h"
42#include "qqmldebugconnection_p.h"
43
44#include <private/qpacket_p.h>
45
46QT_BEGIN_NAMESPACE
47
48QQmlEngineControlClient::QQmlEngineControlClient(QQmlDebugConnection *connection) :
49 QQmlDebugClient(*(new QQmlEngineControlClientPrivate(connection)))
50{
51}
52
53QQmlEngineControlClient::QQmlEngineControlClient(QQmlEngineControlClientPrivate &dd) :
54 QQmlDebugClient(dd)
55{
56}
57
58/*!
59 * Block the starting or stopping of the engine with id \a engineId for now. By calling
60 * releaseEngine later the block can be lifted again. In the debugged application the engine control
61 * server waits until a message is received before continuing. So by not sending a message here we
62 * delay the process. Blocks add up. You have to call releaseEngine() as often as you've called
63 * blockEngine before. The intention of that is to allow different debug clients to use the same
64 * engine control and communicate with their respective counterparts before the QML engine starts or
65 * shuts down.
66 */
67void QQmlEngineControlClient::blockEngine(int engineId)
68{
69 Q_D(QQmlEngineControlClient);
70 Q_ASSERT(d->blockedEngines.contains(engineId));
71 d->blockedEngines[engineId].blockers++;
72}
73
74/*!
75 * Release the engine with id \a engineId. If no other blocks are present, depending on what the
76 * engine is waiting for, the start or stop command is sent to the process being debugged.
77 */
78void QQmlEngineControlClient::releaseEngine(int engineId)
79{
80 Q_D(QQmlEngineControlClient);
81 Q_ASSERT(d->blockedEngines.contains(engineId));
82
83 QQmlEngineControlClientPrivate::EngineState &state = d->blockedEngines[engineId];
84 if (--state.blockers == 0) {
85 Q_ASSERT(state.releaseCommand != QQmlEngineControlClientPrivate::InvalidCommand);
86 d->sendCommand(command: state.releaseCommand, engineId);
87 d->blockedEngines.remove(key: engineId);
88 }
89}
90
91QList<int> QQmlEngineControlClient::blockedEngines() const
92{
93 Q_D(const QQmlEngineControlClient);
94 return d->blockedEngines.keys();
95}
96
97void QQmlEngineControlClient::messageReceived(const QByteArray &data)
98{
99 Q_D(QQmlEngineControlClient);
100 QPacket stream(d->connection->currentDataStreamVersion(), data);
101 qint32 message;
102 qint32 id;
103 QString name;
104
105 stream >> message >> id;
106
107 if (!stream.atEnd())
108 stream >> name;
109
110 auto handleWaiting = [&](
111 QQmlEngineControlClientPrivate::CommandType command, std::function<void()> emitter) {
112 QQmlEngineControlClientPrivate::EngineState &state = d->blockedEngines[id];
113 Q_ASSERT(state.blockers == 0);
114 Q_ASSERT(state.releaseCommand == QQmlEngineControlClientPrivate::InvalidCommand);
115 state.releaseCommand = command;
116 emitter();
117 if (state.blockers == 0) {
118 d->sendCommand(command: state.releaseCommand, engineId: id);
119 d->blockedEngines.remove(key: id);
120 }
121 };
122
123 switch (message) {
124 case QQmlEngineControlClientPrivate::EngineAboutToBeAdded:
125 handleWaiting(QQmlEngineControlClientPrivate::StartWaitingEngine, [&](){
126 emit engineAboutToBeAdded(engineId: id, name);
127 });
128 break;
129 case QQmlEngineControlClientPrivate::EngineAdded:
130 emit engineAdded(engineId: id, name);
131 break;
132 case QQmlEngineControlClientPrivate::EngineAboutToBeRemoved:
133 handleWaiting(QQmlEngineControlClientPrivate::StopWaitingEngine, [&](){
134 emit engineAboutToBeRemoved(engineId: id, name);
135 });
136 break;
137 case QQmlEngineControlClientPrivate::EngineRemoved:
138 emit engineRemoved(engineId: id, name);
139 break;
140 }
141}
142
143QQmlEngineControlClientPrivate::QQmlEngineControlClientPrivate(QQmlDebugConnection *connection) :
144 QQmlDebugClientPrivate(QLatin1String("EngineControl"), connection)
145{
146}
147
148void QQmlEngineControlClientPrivate::sendCommand(
149 QQmlEngineControlClientPrivate::CommandType command, int engineId)
150{
151 Q_Q(QQmlEngineControlClient);
152 QPacket stream(connection->currentDataStreamVersion());
153 stream << static_cast<qint32>(command) << engineId;
154 q->sendMessage(message: stream.data());
155}
156
157QT_END_NAMESPACE
158
159#include "moc_qqmlenginecontrolclient_p.cpp"
160

source code of qtdeclarative/src/qmldebug/qqmlenginecontrolclient.cpp