1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "qquickdeferredexecute_p_p.h"
38 
39 #include <QtCore/qhash.h>
40 #include <QtQml/qqmlengine.h>
41 #include <QtQml/private/qqmldata_p.h>
42 #include <QtQml/private/qqmlcomponent_p.h>
43 #include <QtQml/private/qqmlobjectcreator_p.h>
44 
45 QT_BEGIN_NAMESPACE
46 
47 namespace QtQuickPrivate {
48 
49 typedef QHash<uint, QQmlComponentPrivate::DeferredState *> DeferredStates;
50 
qHash(QObject * object,const QString & propertyName)51 static inline uint qHash(QObject *object, const QString &propertyName)
52 {
53     return ::qHash(object) + ::qHash(propertyName);
54 }
55 
Q_GLOBAL_STATIC(DeferredStates,deferredStates)56 Q_GLOBAL_STATIC(DeferredStates, deferredStates)
57 
58 static void cancelDeferred(QQmlData *ddata, int propertyIndex)
59 {
60     auto dit = ddata->deferredData.rbegin();
61     while (dit != ddata->deferredData.rend()) {
62         (*dit)->bindings.remove(propertyIndex);
63         ++dit;
64     }
65 }
66 
beginDeferred(QQmlEnginePrivate * enginePriv,const QQmlProperty & property,QQmlComponentPrivate::DeferredState * deferredState)67 static bool beginDeferred(QQmlEnginePrivate *enginePriv, const QQmlProperty &property, QQmlComponentPrivate::DeferredState *deferredState)
68 {
69     QObject *object = property.object();
70     QQmlData *ddata = QQmlData::get(object);
71     Q_ASSERT(!ddata->deferredData.isEmpty());
72 
73     int propertyIndex = property.index();
74     int wasInProgress = enginePriv->inProgressCreations;
75 
76     for (auto dit = ddata->deferredData.rbegin(); dit != ddata->deferredData.rend(); ++dit) {
77         QQmlData::DeferredData *deferData = *dit;
78 
79         auto bindings = deferData->bindings;
80         auto range = bindings.equal_range(propertyIndex);
81         if (range.first == bindings.end())
82             continue;
83 
84         QQmlComponentPrivate::ConstructionState *state = new QQmlComponentPrivate::ConstructionState;
85         state->completePending = true;
86 
87         QQmlContextData *creationContext = nullptr;
88         state->creator.reset(new QQmlObjectCreator(deferData->context->parent, deferData->compilationUnit, creationContext));
89 
90         enginePriv->inProgressCreations++;
91 
92         typedef QMultiHash<int, const QV4::CompiledData::Binding *> QV4PropertyBindingHash;
93         auto it = std::reverse_iterator<QV4PropertyBindingHash::iterator>(range.second);
94         auto last = std::reverse_iterator<QV4PropertyBindingHash::iterator>(range.first);
95 #if Q_QML_PRIVATE_API_VERSION < 7
96         while (it != last) {
97             if (!state->creator->populateDeferredBinding(property, deferData, *it))
98                 state->errors << state->creator->errors;
99             ++it;
100         }
101 #else
102         state->creator->beginPopulateDeferred(deferData->context);
103         while (it != last) {
104             state->creator->populateDeferredBinding(property, deferData->deferredIdx, *it);
105             ++it;
106         }
107         state->creator->finalizePopulateDeferred();
108         state->errors << state->creator->errors;
109 #endif
110 
111         deferredState->constructionStates += state;
112 
113         // Cleanup any remaining deferred bindings for this property, also in inner contexts,
114         // to avoid executing them later and overriding the property that was just populated.
115         cancelDeferred(ddata, propertyIndex);
116         break;
117     }
118 
119     return enginePriv->inProgressCreations > wasInProgress;
120 }
121 
beginDeferred(QObject * object,const QString & property)122 void beginDeferred(QObject *object, const QString &property)
123 {
124     QQmlData *data = QQmlData::get(object);
125     if (data && !data->deferredData.isEmpty() && !data->wasDeleted(object)) {
126         QQmlEnginePrivate *ep = QQmlEnginePrivate::get(data->context->engine);
127 
128         QQmlComponentPrivate::DeferredState *state = new QQmlComponentPrivate::DeferredState;
129         if (beginDeferred(ep, QQmlProperty(object, property), state))
130             deferredStates()->insert(qHash(object, property), state);
131         else
132             delete state;
133 
134         // Release deferred data for those compilation units that no longer have deferred bindings
135         data->releaseDeferredData();
136     }
137 }
138 
cancelDeferred(QObject * object,const QString & property)139 void cancelDeferred(QObject *object, const QString &property)
140 {
141     QQmlData *data = QQmlData::get(object);
142     if (data)
143         cancelDeferred(data, QQmlProperty(object, property).index());
144 }
145 
completeDeferred(QObject * object,const QString & property)146 void completeDeferred(QObject *object, const QString &property)
147 {
148     QQmlData *data = QQmlData::get(object);
149     QQmlComponentPrivate::DeferredState *state = deferredStates()->take(qHash(object, property));
150     if (data && state && !data->wasDeleted(object)) {
151         QQmlEnginePrivate *ep = QQmlEnginePrivate::get(data->context->engine);
152         QQmlComponentPrivate::completeDeferred(ep, state);
153     }
154     delete state;
155 }
156 
157 } // QtQuickPrivate
158 
159 QT_END_NAMESPACE
160