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 
45 QT_BEGIN_NAMESPACE
46 
47 using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
48 
QQmlEngineControlServiceImpl(QObject * parent)49 QQmlEngineControlServiceImpl::QQmlEngineControlServiceImpl(QObject *parent) :
50     QQmlEngineControlService(1, parent)
51 {
52     blockingMode = QQmlDebugConnector::instance()->blockingMode();
53 }
54 
messageReceived(const QByteArray & message)55 void 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 *>(objectForId(engineId));
63     if (command == StartWaitingEngine && startingEngines.contains(engine)) {
64         startingEngines.removeOne(engine);
65         emit attachedToEngine(engine);
66     } else if (command == StopWaitingEngine && stoppingEngines.contains(engine)) {
67         stoppingEngines.removeOne(engine);
68         emit detachedFromEngine(engine);
69     }
70 }
71 
engineAboutToBeAdded(QJSEngine * engine)72 void 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(engine);
79         sendMessage(EngineAboutToBeAdded, engine);
80     } else {
81         emit attachedToEngine(engine);
82     }
83 }
84 
engineAboutToBeRemoved(QJSEngine * engine)85 void 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(engine);
92         sendMessage(EngineAboutToBeRemoved, engine);
93     } else {
94         emit detachedFromEngine(engine);
95     }
96 }
97 
engineAdded(QJSEngine * engine)98 void 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(EngineAdded, engine);
105     }
106 }
107 
engineRemoved(QJSEngine * engine)108 void 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(EngineRemoved, engine);
115     }
116 }
117 
sendMessage(QQmlEngineControlServiceImpl::MessageType type,QJSEngine * engine)118 void QQmlEngineControlServiceImpl::sendMessage(QQmlEngineControlServiceImpl::MessageType type,
119                                                QJSEngine *engine)
120 {
121     QQmlDebugPacket d;
122     d << static_cast<qint32>(type) << idForObject(engine);
123     emit messageToClient(name(), d.data());
124 }
125 
stateChanged(State)126 void 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(startingEngines))
131         emit attachedToEngine(engine);
132     startingEngines.clear();
133     for (QJSEngine *engine : qAsConst(stoppingEngines))
134         emit detachedFromEngine(engine);
135     stoppingEngines.clear();
136 }
137 
138 QT_END_NAMESPACE
139