Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmldatablob_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 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
4#ifndef QQMLDATABLOB_P_H
5#define QQMLDATABLOB_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <private/qqmlrefcount_p.h>
19#include <private/qqmljsdiagnosticmessage_p.h>
20
21#if QT_CONFIG(qml_network)
22#include <QtNetwork/qnetworkreply.h>
23#endif
24
25#include <QtQml/qqmlprivate.h>
26#include <QtQml/qqmlerror.h>
27#include <QtQml/qqmlabstracturlinterceptor.h>
28#include <QtQml/qqmlprivate.h>
29
30#include <QtCore/qdatetime.h>
31#include <QtCore/qfileinfo.h>
32#include <QtCore/qurl.h>
33
35
36class QQmlTypeLoader;
37class Q_QML_EXPORT QQmlDataBlob : public QQmlRefCounted<QQmlDataBlob>
38{
39public:
40 using Ptr = QQmlRefPointer<QQmlDataBlob>;
41
42 enum Status {
43 Null, // Prior to QQmlTypeLoader::load()
44 Loading, // Prior to data being received and dataReceived() being called
45 WaitingForDependencies, // While there are outstanding addDependency()s
46 ResolvingDependencies, // While resolving outstanding dependencies, to detect cycles
47 Complete, // Finished
48 Error // Error
49 };
50
51 enum Type { //Matched in QQmlAbstractUrlInterceptor
55 };
56
58 virtual ~QQmlDataBlob();
59
60 void startLoading();
61
62 QQmlTypeLoader *typeLoader() const { return m_typeLoader; }
63
64 Type type() const;
65
66 Status status() const;
67 bool isNull() const;
68 bool isLoading() const;
69 bool isWaiting() const;
70 bool isComplete() const;
71 bool isError() const;
72 bool isCompleteOrError() const;
73
74 qreal progress() const;
75
76 QUrl url() const;
77 QString urlString() const;
78 QUrl finalUrl() const;
79 QString finalUrlString() const;
80
81 QList<QQmlError> errors() const;
82
84 public:
85 QString readAll(QString *error) const;
86 QDateTime sourceTimeStamp() const;
87 bool exists() const;
88 bool isEmpty() const;
89 bool isValid() const
90 {
91 return hasInlineSourceCode || !fileInfo.filePath().isEmpty();
92 }
93
94 private:
95 friend class QQmlDataBlob;
96 friend class QQmlTypeLoader;
97 QString inlineSourceCode;
98 QFileInfo fileInfo;
99 bool hasInlineSourceCode = false;
100 };
101
102protected:
103 // Can be called from within callbacks
104 void setError(const QQmlError &);
105 void setError(const QList<QQmlError> &errors);
107 void setError(const QString &description);
108 void addDependency(QQmlDataBlob *);
109
110 // Callbacks made in load thread
111 virtual void dataReceived(const SourceCodeData &) = 0;
113 virtual void done();
114#if QT_CONFIG(qml_network)
115 virtual void networkError(QNetworkReply::NetworkError);
116#endif
117 virtual void dependencyError(QQmlDataBlob *);
118 virtual void dependencyComplete(QQmlDataBlob *);
119 virtual void allDependenciesDone();
120
121 // Callbacks made in main thread
122 virtual void downloadProgressChanged(qreal);
123 virtual void completed();
124
125protected:
126 // Manager that is currently fetching data for me
128
129private:
130 friend class QQmlTypeLoader;
132
133 void tryDone();
134 void cancelAllWaitingFor();
135 void notifyAllWaitingOnMe();
136 void notifyComplete(QQmlDataBlob *);
137
138 struct ThreadData {
139 private:
140 enum {
141 StatusMask = 0x0000FFFF,
142 StatusShift = 0,
143 ProgressMask = 0x00FF0000,
144 ProgressShift = 16,
145 AsyncMask = 0x80000000,
146 NoMask = 0
147 };
148
149 public:
150 inline ThreadData()
151 : _p(0)
152 {
153 }
154
155 inline QQmlDataBlob::Status status() const
156 {
157 return QQmlDataBlob::Status((_p.loadRelaxed() & StatusMask) >> StatusShift);
158 }
159
160 inline void setStatus(QQmlDataBlob::Status status)
161 {
162 while (true) {
163 int d = _p.loadRelaxed();
164 int nd = (d & ~StatusMask) | ((status << StatusShift) & StatusMask);
165 if (d == nd || _p.testAndSetOrdered(d, nd)) return;
166 }
167 }
168
169 inline bool isAsync() const
170 {
171 return _p.loadRelaxed() & AsyncMask;
172 }
173
174 inline void setIsAsync(bool v)
175 {
176 while (true) {
177 int d = _p.loadRelaxed();
178 int nd = (d & ~AsyncMask) | (v ? AsyncMask : NoMask);
179 if (d == nd || _p.testAndSetOrdered(d, nd)) return;
180 }
181 }
182
183 inline qreal progress() const
184 {
185 return quint8((_p.loadRelaxed() & ProgressMask) >> ProgressShift) / float(0xFF);
186 }
187
188 inline void setProgress(qreal progress)
189 {
190 quint8 v = 0xFF * progress;
191 while (true) {
192 int d = _p.loadRelaxed();
193 int nd = (d & ~ProgressMask) | ((v << ProgressShift) & ProgressMask);
194 if (d == nd || _p.testAndSetOrdered(d, nd)) return;
195 }
196 }
197
198 private:
199 QAtomicInt _p;
200 };
201 ThreadData m_data;
202
203 // m_errors should *always* be written before the status is set to Error.
204 // We use the status change as a memory fence around m_errors so that locking
205 // isn't required. Once the status is set to Error (or Complete), m_errors
206 // cannot be changed.
207 QList<QQmlError> m_errors;
208
209 Type m_type;
210
211 QUrl m_url;
212 QUrl m_finalUrl;
213 mutable QString m_urlString;
214 mutable QString m_finalUrlString;
215
216 // List of QQmlDataBlob's that are waiting for me to complete.
217protected:
218 QList<QQmlDataBlob *> m_waitingOnMe;
219private:
220
221 // List of QQmlDataBlob's that I am waiting for to complete.
222 QVector<QQmlRefPointer<QQmlDataBlob>> m_waitingFor;
223
224 int m_redirectCount:30;
225 bool m_inCallback:1;
226 bool m_isDone:1;
227};
228
230
231#endif // QQMLDATABLOB_P_H
NSData * m_data
\inmodule QtCore
Definition qatomic.h:112
\inmodule QtCore\reentrant
Definition qdatetime.h:283
NetworkError
Indicates all possible error conditions found during the processing of the request.
The QQmlDataBlob encapsulates a data request that can be issued to a QQmlTypeLoader.
virtual void dataReceived(const SourceCodeData &)=0
Invoked when data for the blob is received.
QQmlTypeLoader * typeLoader() const
virtual void initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit *)=0
QList< QQmlDataBlob * > m_waitingOnMe
QQmlTypeLoader * m_typeLoader
Status
This enum describes the status of the data blob.
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
The QQmlTypeLoader class abstracts loading files and their dependencies over the network.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qurl.h:94
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
GLsizei const GLfloat * v
[13]
GLenum type
static void setError(QJsonObject *response, const QString &msg)
double qreal
Definition qtypes.h:187
unsigned char quint8
Definition qtypes.h:46
QUrl url("example.com")
[constructor-url-reference]
QNetworkAccessManager manager
Definition moc.h:23