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 "qqmlenginecontrolservice.h"
41#include <private/qqmldebugconnector_p.h>
42#include <private/qversionedpacket_p.h>
43#include <QJSEngine>
44
45QT_BEGIN_NAMESPACE
46
47using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
48
49QQmlEngineControlServiceImpl::QQmlEngineControlServiceImpl(QObject *parent) :
50 QQmlEngineControlService(1, parent)
51{
52 blockingMode = QQmlDebugConnector::instance()->blockingMode();
53}
54
55void QQmlEngineControlServiceImpl::messageReceived(const QByteArray &message)
56{
57 QMutexLocker lock(&dataMutex);
58 QQmlDebugPacket d(message);
59 qint32 command;
60 qint32 engineId;
61 d >> command >> engineId;
62 QJSEngine *engine = qobject_cast<QJSEngine *>(object: objectForId(id: engineId));
63 if (command == StartWaitingEngine && startingEngines.contains(t: engine)) {
64 startingEngines.removeOne(t: engine);
65 emit attachedToEngine(engine);
66 } else if (command == StopWaitingEngine && stoppingEngines.contains(t: engine)) {
67 stoppingEngines.removeOne(t: engine);
68 emit detachedFromEngine(engine);
69 }
70}
71
72void QQmlEngineControlServiceImpl::engineAboutToBeAdded(QJSEngine *engine)
73{
74 QMutexLocker lock(&dataMutex);
75 if (blockingMode && state() == Enabled) {
76 Q_ASSERT(!stoppingEngines.contains(engine));
77 Q_ASSERT(!startingEngines.contains(engine));
78 startingEngines.append(t: engine);
79 sendMessage(type: EngineAboutToBeAdded, engine);
80 } else {
81 emit attachedToEngine(engine);
82 }
83}
84
85void QQmlEngineControlServiceImpl::engineAboutToBeRemoved(QJSEngine *engine)
86{
87 QMutexLocker lock(&dataMutex);
88 if (blockingMode && state() == Enabled) {
89 Q_ASSERT(!stoppingEngines.contains(engine));
90 Q_ASSERT(!startingEngines.contains(engine));
91 stoppingEngines.append(t: engine);
92 sendMessage(type: EngineAboutToBeRemoved, engine);
93 } else {
94 emit detachedFromEngine(engine);
95 }
96}
97
98void QQmlEngineControlServiceImpl::engineAdded(QJSEngine *engine)
99{
100 if (state() == Enabled) {
101 QMutexLocker lock(&dataMutex);
102 Q_ASSERT(!startingEngines.contains(engine));
103 Q_ASSERT(!stoppingEngines.contains(engine));
104 sendMessage(type: EngineAdded, engine);
105 }
106}
107
108void QQmlEngineControlServiceImpl::engineRemoved(QJSEngine *engine)
109{
110 if (state() == Enabled) {
111 QMutexLocker lock(&dataMutex);
112 Q_ASSERT(!startingEngines.contains(engine));
113 Q_ASSERT(!stoppingEngines.contains(engine));
114 sendMessage(type: EngineRemoved, engine);
115 }
116}
117
118void QQmlEngineControlServiceImpl::sendMessage(QQmlEngineControlServiceImpl::MessageType type,
119 QJSEngine *engine)
120{
121 QQmlDebugPacket d;
122 d << static_cast<qint32>(type) << idForObject(engine);
123 emit messageToClient(name: name(), message: d.data());
124}
125
126void QQmlEngineControlServiceImpl::stateChanged(State)
127{
128 // We flush everything for any kind of state change, to avoid complicated timing issues.
129 QMutexLocker lock(&dataMutex);
130 for (QJSEngine *engine : qAsConst(t&: startingEngines))
131 emit attachedToEngine(engine);
132 startingEngines.clear();
133 for (QJSEngine *engine : qAsConst(t&: stoppingEngines))
134 emit detachedFromEngine(engine);
135 stoppingEngines.clear();
136}
137
138QT_END_NAMESPACE
139

source code of qtdeclarative/src/plugins/qmltooling/qmldbg_profiler/qqmlenginecontrolservice.cpp