Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlcontextdata_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLCONTEXTDATA_P_H
6#define QQMLCONTEXTDATA_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQml/private/qtqmlglobal_p.h>
20#include <QtQml/private/qqmlcontext_p.h>
21#include <QtQml/private/qqmlguard_p.h>
22#include <QtQml/private/qqmltypenamecache_p.h>
23#include <QtQml/private/qqmlnotifier_p.h>
24#include <QtQml/private/qv4identifierhash_p.h>
25#include <QtQml/private/qv4executablecompilationunit_p.h>
26
27QT_BEGIN_NAMESPACE
28
29class QQmlComponentAttached;
30class QQmlGuardedContextData;
31class QQmlJavaScriptExpression;
33
34class Q_QML_EXPORT QQmlContextData
35{
36public:
37 static QQmlRefPointer<QQmlContextData> createRefCounted(
38 const QQmlRefPointer<QQmlContextData> &parent)
39 {
40 return QQmlRefPointer<QQmlContextData>(new QQmlContextData(RefCounted, nullptr, parent),
41 QQmlRefPointer<QQmlContextData>::Adopt);
42 }
43
44 // Owned by the parent. When the parent is reset to nullptr, it will be deref'd.
45 static QQmlRefPointer<QQmlContextData> createChild(
46 const QQmlRefPointer<QQmlContextData> &parent)
47 {
48 Q_ASSERT(!parent.isNull());
49 return QQmlRefPointer<QQmlContextData>(new QQmlContextData(OwnedByParent, nullptr, parent));
50 }
51
52 void addref() const { ++m_refCount; }
53 void release() const { if (--m_refCount == 0) delete this; }
54 int count() const { return m_refCount; }
55 int refCount() const { return m_refCount; }
56
57 QQmlRefPointer<QV4::ExecutableCompilationUnit> typeCompilationUnit() const
58 {
59 return m_typeCompilationUnit;
60 }
61 void setTypeCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit);
62
63 void initFromTypeCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
64 int subComponentIndex);
65
66 static QQmlRefPointer<QQmlContextData> get(QQmlContext *context) {
67 return QQmlContextPrivate::get(context)->m_data;
68 }
69
70 void emitDestruction();
71 void clearExpressions();
72 void clearContextRecursively();
73 void clearChildrenAndSiblings();
74 void clearImportedScripts();
75 void clearOwnedObjects();
76 void clearContextGuards();
77 void clearIdValues();
78 void invalidate();
79
80 bool isValid() const
81 {
82 return m_engine && (!m_isInternal || !m_contextObject
83 || !QObjectPrivate::get(m_contextObject)->wasDeleted);
84 }
85
86 bool isInternal() const { return m_isInternal; }
87 void setInternal(bool isInternal) { m_isInternal = isInternal; }
88
89 bool isJSContext() const { return m_isJSContext; }
90 void setJSContext(bool isJSContext) { m_isJSContext = isJSContext; }
91
92 bool isPragmaLibraryContext() const { return m_isPragmaLibraryContext; }
93 void setPragmaLibraryContext(bool library) { m_isPragmaLibraryContext = library; }
94
95 QQmlRefPointer<QQmlContextData> parent() const { return m_parent; }
96 void clearParent()
97 {
98 if (!m_parent)
99 return;
100
101 m_parent = nullptr;
102 if (m_ownedByParent) {
103 m_ownedByParent = false;
104 release();
105 }
106 }
107
108 void refreshExpressions();
109
110 void addOwnedObject(QQmlData *ownedObject);
111 QQmlData *ownedObjects() const { return m_ownedObjects; }
112 void setOwnedObjects(QQmlData *ownedObjects) { m_ownedObjects = ownedObjects; }
113
114 enum QmlObjectKind {
115 OrdinaryObject,
116 DocumentRoot,
117 };
118 void installContext(QQmlData *ddata, QmlObjectKind kind);
119
120 QUrl resolvedUrl(const QUrl &) const;
121
122 // My containing QQmlContext. If isInternal is true this owns publicContext.
123 // If internal is false publicContext owns this.
124 QQmlContext *asQQmlContext()
125 {
126 if (!m_publicContext)
127 m_publicContext = new QQmlContext(*new QQmlContextPrivate(this));
128 return m_publicContext;
129 }
130
131 QQmlContextPrivate *asQQmlContextPrivate()
132 {
133 return QQmlContextPrivate::get(asQQmlContext());
134 }
135
136 QObject *contextObject() const { return m_contextObject; }
137 void setContextObject(QObject *contextObject) { m_contextObject = contextObject; }
138
139 template<typename HandleSelf, typename HandleLinked>
140 void deepClearContextObject(
141 QObject *contextObject, HandleSelf &&handleSelf, HandleLinked &&handleLinked) {
142 for (QQmlContextData *lc = m_linkedContext.data(); lc; lc = lc->m_linkedContext.data()) {
143 handleLinked(lc);
144 if (lc->m_contextObject == contextObject)
145 lc->m_contextObject = nullptr;
146 }
147
148 handleSelf(this);
149 if (m_contextObject == contextObject)
150 m_contextObject = nullptr;
151 }
152
153 void deepClearContextObject(QObject *contextObject)
154 {
155 deepClearContextObject(
156 contextObject,
157 [](QQmlContextData *self) { self->emitDestruction(); },
158 [](QQmlContextData *){});
159 }
160
161 QQmlEngine *engine() const { return m_engine; }
162 void setEngine(QQmlEngine *engine) { m_engine = engine; }
163
164 QQmlContext *publicContext() const { return m_publicContext; }
165 void clearPublicContext()
166 {
167 if (!m_publicContext)
168 return;
169
170 m_publicContext = nullptr;
171 if (m_ownedByPublicContext) {
172 m_ownedByPublicContext = false;
173 release();
174 }
175 }
176
177 int propertyIndex(const QString &name) const
178 {
179 ensurePropertyNames();
180 return m_propertyNameCache.value(name);
181 }
182
183 int propertyIndex(QV4::Heap::String *name) const
184 {
185 ensurePropertyNames();
186 return m_propertyNameCache.value(name);
187 }
188
189 QString propertyName(int index) const
190 {
191 ensurePropertyNames();
192 return m_propertyNameCache.key<QString>(index);
193 }
194
195 void addPropertyNameAndIndex(const QString &name, int index)
196 {
197 Q_ASSERT(m_propertyNameCache.isValid());
198 m_propertyNameCache.add(name, index);
199 }
200
201 void setExpressions(QQmlJavaScriptExpression *expressions) { m_expressions = expressions; }
202 QQmlJavaScriptExpression *takeExpressions()
203 {
204 QQmlJavaScriptExpression *expressions = m_expressions;
205 m_expressions = nullptr;
206 return expressions;
207 }
208
209 void setChildContexts(const QQmlRefPointer<QQmlContextData> &childContexts)
210 {
211 m_childContexts = childContexts.data();
212 }
213 QQmlRefPointer<QQmlContextData> childContexts() const { return m_childContexts; }
214 QQmlRefPointer<QQmlContextData> takeChildContexts()
215 {
216 QQmlRefPointer<QQmlContextData> childContexts = m_childContexts;
217 m_childContexts = nullptr;
218 return childContexts;
219 }
220 QQmlRefPointer<QQmlContextData> nextChild() const { return m_nextChild; }
221
222 int numIdValues() const { return m_idValueCount; }
223 void setIdValue(int index, QObject *idValue);
224 bool isIdValueSet(int index) const { return m_idValues[index].wasSet(); }
225 QQmlNotifier *idValueBindings(int index) const { return m_idValues[index].bindings(); }
226 QObject *idValue(int index) const { return m_idValues[index].data(); }
227
228 // Return the outermost id for obj, if any.
229 QString findObjectId(const QObject *obj) const;
230
231 // url() and urlString() prefer the CU's URL over explicitly set baseUrls. They
232 // don't search the context hierarchy.
233 // baseUrl() and baseUrlString() search the context hierarchy and prefer explicit
234 // base URLs over CU Urls.
235
236 QUrl url() const;
237 QString urlString() const;
238
239 void setBaseUrlString(const QString &baseUrlString) { m_baseUrlString = baseUrlString; }
240 QString baseUrlString() const
241 {
242 for (const QQmlContextData *data = this; data; data = data->m_parent) {
243 if (!data->m_baseUrlString.isEmpty())
244 return data->m_baseUrlString;
245 if (data->m_typeCompilationUnit)
246 return data->m_typeCompilationUnit->finalUrlString();
247 }
248 return QString();
249 }
250
251 void setBaseUrl(const QUrl &baseUrl) { m_baseUrl = baseUrl; }
252 QUrl baseUrl() const
253 {
254 for (const QQmlContextData *data = this; data; data = data->m_parent) {
255 if (!data->m_baseUrl.isEmpty())
256 return data->m_baseUrl;
257 if (data->m_typeCompilationUnit)
258 return data->m_typeCompilationUnit->finalUrl();
259 }
260 return QUrl();
261 }
262
263 QQmlRefPointer<QQmlTypeNameCache> imports() const { return m_imports; }
264 void setImports(const QQmlRefPointer<QQmlTypeNameCache> &imports) { m_imports = imports; }
265
266 QQmlIncubatorPrivate *incubator() const { return m_hasExtraObject ? nullptr : m_incubator; }
267 void setIncubator(QQmlIncubatorPrivate *incubator)
268 {
269 Q_ASSERT(!m_hasExtraObject || m_extraObject == nullptr);
270 m_hasExtraObject = false;
271 m_incubator = incubator;
272 }
273
274 QObject *extraObject() const { return m_hasExtraObject ? m_extraObject : nullptr; }
275 void setExtraObject(QObject *extraObject)
276 {
277 Q_ASSERT(m_hasExtraObject || m_incubator == nullptr);
278 m_hasExtraObject = true;
279 m_extraObject = extraObject;
280 }
281
282 bool isRootObjectInCreation() const { return m_isRootObjectInCreation; }
283 void setRootObjectInCreation(bool rootInCreation) { m_isRootObjectInCreation = rootInCreation; }
284
285 QV4::ReturnedValue importedScripts() const
286 {
287 if (m_hasWeakImportedScripts)
288 return m_weakImportedScripts.value();
289 else
290 return m_importedScripts.value();
291 }
292 void setImportedScripts(QV4::ExecutionEngine *engine, QV4::Value scripts) {
293 // setImportedScripts should not be called on an invalidated context
294 Q_ASSERT(!m_hasWeakImportedScripts);
295 m_importedScripts.set(engine, scripts);
296 }
297
298 /*
299 we can safely pass a ReturnedValue here, as setImportedScripts will directly store
300 scripts in a persistentValue, without any intermediate allocation that could trigger
301 a gc run
302 */
303 void setImportedScripts(QV4::ExecutionEngine *engine, QV4::ReturnedValue scripts) {
304 // setImportedScripts should not be called on an invalidated context
305 Q_ASSERT(!m_hasWeakImportedScripts);
306 m_importedScripts.set(engine, scripts);
307 }
308
309 QQmlRefPointer<QQmlContextData> linkedContext() const { return m_linkedContext; }
310 void setLinkedContext(const QQmlRefPointer<QQmlContextData> &context) { m_linkedContext = context; }
311
312 bool hasUnresolvedNames() const { return m_unresolvedNames; }
313 void setUnresolvedNames(bool hasUnresolvedNames) { m_unresolvedNames = hasUnresolvedNames; }
314
315 QQmlComponentAttached *componentAttacheds() const { return m_componentAttacheds; }
316 void addComponentAttached(QQmlComponentAttached *attached);
317
318 void addExpression(QQmlJavaScriptExpression *expression);
319
320 bool valueTypesAreAddressable() const {
321 return m_typeCompilationUnit && m_typeCompilationUnit->valueTypesAreAddressable();
322 }
323
324 bool valueTypesAreAssertable() const {
325 return m_typeCompilationUnit && m_typeCompilationUnit->valueTypesAreAssertable();
326 }
327
328private:
329 friend class QQmlGuardedContextData;
330 friend class QQmlContextPrivate;
331
332 enum Ownership {
333 RefCounted,
334 OwnedByParent,
335 OwnedByPublicContext
336 };
337
338 // id guards
339 struct ContextGuard : public QQmlGuard<QObject>
340 {
341 enum Tag {
342 NoTag,
343 ObjectWasSet
344 };
345
346 inline ContextGuard() : QQmlGuard<QObject>(&ContextGuard::objectDestroyedImpl, nullptr), m_context(nullptr) {}
347 inline ContextGuard &operator=(QObject *obj);
348
349 inline bool wasSet() const;
350
351 QQmlNotifier *bindings() { return &m_bindings; }
352 void setContext(const QQmlRefPointer<QQmlContextData> &context)
353 {
354 m_context = context.data();
355 }
356
357 private:
358 inline static void objectDestroyedImpl(QQmlGuardImpl *);
359 // Not refcounted, as it always belongs to the QQmlContextData.
360 QTaggedPointer<QQmlContextData, Tag> m_context;
361 QQmlNotifier m_bindings;
362 };
363
364 // It's OK to pass a half-created publicContext here. We will not dereference it during
365 // construction.
366 QQmlContextData(
367 Ownership ownership, QQmlContext *publicContext,
368 const QQmlRefPointer<QQmlContextData> &parent, QQmlEngine *engine = nullptr)
369 : m_parent(parent.data()),
370 m_engine(engine ? engine : (parent.isNull() ? nullptr : parent->engine())),
371 m_isInternal(false), m_isJSContext(false), m_isPragmaLibraryContext(false),
372 m_unresolvedNames(false), m_hasEmittedDestruction(false), m_isRootObjectInCreation(false),
373 m_ownedByParent(ownership == OwnedByParent),
374 m_ownedByPublicContext(ownership == OwnedByPublicContext), m_hasExtraObject(false),
375 m_hasWeakImportedScripts(false), m_dummy(0), m_publicContext(publicContext), m_incubator(nullptr)
376 {
377 Q_ASSERT(!m_ownedByParent || !m_ownedByPublicContext);
378 if (!m_parent)
379 return;
380
381 m_nextChild = m_parent->m_childContexts;
382 if (m_nextChild)
383 m_nextChild->m_prevChild = &m_nextChild;
384 m_prevChild = &m_parent->m_childContexts;
385 m_parent->m_childContexts = this;
386 }
387
388 ~QQmlContextData();
389
390 bool hasExpressionsToRun(bool isGlobalRefresh) const
391 {
392 return m_expressions && (!isGlobalRefresh || m_unresolvedNames);
393 }
394
395 void refreshExpressionsRecursive(bool isGlobal);
396 void refreshExpressionsRecursive(QQmlJavaScriptExpression *);
397 void initPropertyNames() const;
398
399 void ensurePropertyNames() const
400 {
401 if (!m_propertyNameCache.isValid())
402 initPropertyNames();
403 Q_ASSERT(m_propertyNameCache.isValid());
404 }
405
406 // My parent context and engine
407 QQmlContextData *m_parent = nullptr;
408 QQmlEngine *m_engine = nullptr;
409
410 mutable quint32 m_refCount = 1;
411 quint32 m_isInternal:1;
412 quint32 m_isJSContext:1;
413 quint32 m_isPragmaLibraryContext:1;
414 quint32 m_unresolvedNames:1; // True if expressions in this context failed to resolve a toplevel name
415 quint32 m_hasEmittedDestruction:1;
416 quint32 m_isRootObjectInCreation:1;
417 quint32 m_ownedByParent:1;
418 quint32 m_ownedByPublicContext:1;
419 quint32 m_hasExtraObject:1; // used in QQmlDelegateModelItem::dataForObject to find the corresponding QQmlDelegateModelItem of an object
420 quint32 m_hasWeakImportedScripts:1;
421 Q_DECL_UNUSED_MEMBER quint32 m_dummy:22;
422 QQmlContext *m_publicContext = nullptr;
423
424 union {
425 // The incubator that is constructing this context if any
426 QQmlIncubatorPrivate *m_incubator;
427 // a pointer to extra data, currently only used in QQmlDelegateModel
428 QObject *m_extraObject;
429 };
430
431 // Compilation unit for contexts that belong to a compiled type.
432 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_typeCompilationUnit;
433
434 // object index in CompiledData::Unit to component that created this context
435 int m_componentObjectIndex = -1;
436
437 // flag indicates whether the context owns the cache (after mutation) or not.
438 mutable QV4::IdentifierHash m_propertyNameCache;
439
440 // Context object
441 QObject *m_contextObject = nullptr;
442
443 // Any script blocks that exist on this context
444 union {
445 /* an invalidated context transitions from a strong reference to the scripts
446 to a weak one, so that the context doesn't needlessly holds on to the scripts,
447 but closures can still access them if needed
448 */
449 QV4::PersistentValue m_importedScripts = {}; // This is a JS Array
450 QV4::WeakValue m_weakImportedScripts;
451 };
452
453 QUrl m_baseUrl;
454 QString m_baseUrlString;
455
456 // List of imports that apply to this context
457 QQmlRefPointer<QQmlTypeNameCache> m_imports;
458
459 // My children, not refcounted as that would create cyclic references
460 QQmlContextData *m_childContexts = nullptr;
461
462 // My peers in parent's childContexts list; not refcounted
463 QQmlContextData *m_nextChild = nullptr;
464 QQmlContextData **m_prevChild = nullptr;
465
466 // Expressions that use this context
467 QQmlJavaScriptExpression *m_expressions = nullptr;
468
469 // Doubly-linked list of objects that are owned by this context
470 QQmlData *m_ownedObjects = nullptr;
471
472 // Doubly-linked list of context guards (XXX merge with contextObjects)
473 QQmlGuardedContextData *m_contextGuards = nullptr;
474
475 ContextGuard *m_idValues = nullptr;
476 int m_idValueCount = 0;
477
478 // Linked contexts. this owns linkedContext.
479 QQmlRefPointer<QQmlContextData> m_linkedContext;
480
481 // Linked list of uses of the Component attached property in this context
482 QQmlComponentAttached *m_componentAttacheds = nullptr;
483};
484
485QQmlContextData::ContextGuard &QQmlContextData::ContextGuard::operator=(QObject *obj)
486{
487 QQmlGuard<QObject>::operator=(obj);
488 m_context.setTag(ObjectWasSet);
489 m_bindings.notify(); // For alias connections
490 return *this;
491}
492
493 void QQmlContextData::ContextGuard::objectDestroyedImpl(QQmlGuardImpl *impl)
494{
495 auto This = static_cast<QQmlContextData::ContextGuard *>(impl);
496 if (QObject *contextObject = This->m_context->contextObject()) {
497 if (!QObjectPrivate::get(contextObject)->wasDeleted)
498 This->m_bindings.notify();
499 }
500}
501
502bool QQmlContextData::ContextGuard::wasSet() const
503{
504 return m_context.tag() == ObjectWasSet;
505}
506
507QT_END_NAMESPACE
508
509#endif // QQMLCONTEXTDATA_P_H
friend class QQmlIncubatorPrivate
Combined button and popup list for selecting options.