aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldebug/qqmlenginecontrolclient.cpp
blob: 6034076745893bb9feeab49a8024745d449ef871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmlenginecontrolclient_p.h"
#include "qqmlenginecontrolclient_p_p.h"
#include "qqmldebugconnection_p.h"

#include <private/qpacket_p.h>

QT_BEGIN_NAMESPACE

QQmlEngineControlClient::QQmlEngineControlClient(QQmlDebugConnection *connection) :
    QQmlDebugClient(*(new QQmlEngineControlClientPrivate(connection)))
{
}

QQmlEngineControlClient::QQmlEngineControlClient(QQmlEngineControlClientPrivate &dd) :
    QQmlDebugClient(dd)
{
}

/*!
 * Block the starting or stopping of the engine with id \a engineId for now. By calling
 * releaseEngine later the block can be lifted again. In the debugged application the engine control
 * server waits until a message is received before continuing. So by not sending a message here we
 * delay the process. Blocks add up. You have to call releaseEngine() as often as you've called
 * blockEngine before. The intention of that is to allow different debug clients to use the same
 * engine control and communicate with their respective counterparts before the QML engine starts or
 * shuts down.
 */
void QQmlEngineControlClient::blockEngine(int engineId)
{
    Q_D(QQmlEngineControlClient);
    Q_ASSERT(d->blockedEngines.contains(engineId));
    d->blockedEngines[engineId].blockers++;
}

/*!
 * Release the engine with id \a engineId. If no other blocks are present, depending on what the
 * engine is waiting for, the start or stop command is sent to the process being debugged.
 */
void QQmlEngineControlClient::releaseEngine(int engineId)
{
    Q_D(QQmlEngineControlClient);
    Q_ASSERT(d->blockedEngines.contains(engineId));

    QQmlEngineControlClientPrivate::EngineState &state = d->blockedEngines[engineId];
    if (--state.blockers == 0) {
        Q_ASSERT(state.releaseCommand != QQmlEngineControlClientPrivate::InvalidCommand);
        d->sendCommand(state.releaseCommand, engineId);
        d->blockedEngines.remove(engineId);
    }
}

QList<int> QQmlEngineControlClient::blockedEngines() const
{
    Q_D(const QQmlEngineControlClient);
    return d->blockedEngines.keys();
}

void QQmlEngineControlClient::messageReceived(const QByteArray &data)
{
    Q_D(QQmlEngineControlClient);
    QPacket stream(d->connection->currentDataStreamVersion(), data);
    qint32 message;
    qint32 id;
    QString name;

    stream >> message >> id;

    if (!stream.atEnd())
        stream >> name;

    auto handleWaiting = [&](
            QQmlEngineControlClientPrivate::CommandType command, std::function<void()> emitter) {
        QQmlEngineControlClientPrivate::EngineState &state = d->blockedEngines[id];
        Q_ASSERT(state.blockers == 0);
        Q_ASSERT(state.releaseCommand == QQmlEngineControlClientPrivate::InvalidCommand);
        state.releaseCommand = command;
        emitter();
        if (state.blockers == 0) {
            d->sendCommand(state.releaseCommand, id);
            d->blockedEngines.remove(id);
        }
    };

    switch (message) {
    case QQmlEngineControlClientPrivate::EngineAboutToBeAdded:
        handleWaiting(QQmlEngineControlClientPrivate::StartWaitingEngine, [&](){
            emit engineAboutToBeAdded(id, name);
        });
        break;
    case QQmlEngineControlClientPrivate::EngineAdded:
        emit engineAdded(id, name);
        break;
    case QQmlEngineControlClientPrivate::EngineAboutToBeRemoved:
        handleWaiting(QQmlEngineControlClientPrivate::StopWaitingEngine, [&](){
            emit engineAboutToBeRemoved(id, name);
        });
        break;
    case QQmlEngineControlClientPrivate::EngineRemoved:
        emit engineRemoved(id, name);
        break;
    }
}

QQmlEngineControlClientPrivate::QQmlEngineControlClientPrivate(QQmlDebugConnection *connection) :
    QQmlDebugClientPrivate(QLatin1String("EngineControl"), connection)
{
}

void QQmlEngineControlClientPrivate::sendCommand(
        QQmlEngineControlClientPrivate::CommandType command, int engineId)
{
    Q_Q(QQmlEngineControlClient);
    QPacket stream(connection->currentDataStreamVersion());
    stream << static_cast<qint32>(command) << engineId;
    q->sendMessage(stream.data());
}

QT_END_NAMESPACE

#include "moc_qqmlenginecontrolclient_p.cpp"