1// Copyright (C) 2016 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 QQMLTYPELOADER_P_H
5#define QQMLTYPELOADER_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/qqmldatablob_p.h>
19#include <private/qqmlimport_p.h>
20#include <private/qqmlmetatype_p.h>
21#include <private/qv4compileddata_p.h>
22
23#include <QtQml/qtqmlglobal.h>
24#include <QtQml/qqmlerror.h>
25
26#include <QtCore/qcache.h>
27#include <QtCore/qmutex.h>
28
29#include <memory>
30
31QT_BEGIN_NAMESPACE
32
33class QQmlScriptBlob;
34class QQmlQmldirData;
35class QQmlTypeData;
36class QQmlEngineExtensionInterface;
37class QQmlExtensionInterface;
38class QQmlProfiler;
39class QQmlTypeLoaderThread;
40class QQmlEngine;
41
42class Q_QML_EXPORT QQmlTypeLoader
43{
44 Q_DECLARE_TR_FUNCTIONS(QQmlTypeLoader)
45public:
46 using ChecksumCache = QHash<quintptr, QByteArray>;
47 enum Mode { PreferSynchronous, Asynchronous, Synchronous };
48
49 class Q_QML_EXPORT Blob : public QQmlDataBlob
50 {
51 public:
52 Blob(const QUrl &url, QQmlDataBlob::Type type, QQmlTypeLoader *loader);
53 ~Blob() override;
54
55 const QQmlImports *imports() const { return m_importCache.data(); }
56
57 void setCachedUnitStatus(QQmlMetaType::CachedUnitLookupError status) { m_cachedUnitStatus = status; }
58
59 struct PendingImport
60 {
61 QString uri;
62 QString qualifier;
63
64 QV4::CompiledData::Import::ImportType type
65 = QV4::CompiledData::Import::ImportType::ImportLibrary;
66 QV4::CompiledData::Location location;
67
68 QQmlImports::ImportFlags flags;
69 quint8 precedence = 0;
70 int priority = 0;
71
72 QTypeRevision version;
73
74 PendingImport() = default;
75 PendingImport(Blob *blob, const QV4::CompiledData::Import *import,
76 QQmlImports::ImportFlags flags);
77 };
78 using PendingImportPtr = std::shared_ptr<PendingImport>;
79
80 void importQmldirScripts(const PendingImportPtr &import, const QQmlTypeLoaderQmldirContent &qmldir, const QUrl &qmldirUrl);
81
82 protected:
83 bool addImport(const QV4::CompiledData::Import *import, QQmlImports::ImportFlags,
84 QList<QQmlError> *errors);
85 bool addImport(PendingImportPtr import, QList<QQmlError> *errors);
86
87 bool fetchQmldir(const QUrl &url, PendingImportPtr import, int priority, QList<QQmlError> *errors);
88 bool updateQmldir(const QQmlRefPointer<QQmlQmldirData> &data, const PendingImportPtr &import, QList<QQmlError> *errors);
89
90 private:
91 bool addScriptImport(const PendingImportPtr &import);
92 bool addFileImport(const PendingImportPtr &import, QList<QQmlError> *errors);
93 bool addLibraryImport(const PendingImportPtr &import, QList<QQmlError> *errors);
94
95 virtual bool qmldirDataAvailable(const QQmlRefPointer<QQmlQmldirData> &, QList<QQmlError> *);
96
97 virtual void scriptImported(const QQmlRefPointer<QQmlScriptBlob> &, const QV4::CompiledData::Location &, const QString &, const QString &) {}
98
99 void dependencyComplete(QQmlDataBlob *) override;
100
101 bool loadImportDependencies(
102 const PendingImportPtr &currentImport, const QString &qmldirUri,
103 QQmlImports::ImportFlags flags, QList<QQmlError> *errors);
104
105 protected:
106 bool loadDependentImports(
107 const QList<QQmlDirParser::Import> &imports, const QString &qualifier,
108 QTypeRevision version, quint16 precedence, QQmlImports::ImportFlags flags,
109 QList<QQmlError> *errors);
110 virtual QString stringAt(int) const { return QString(); }
111
112 bool isDebugging() const;
113 bool readCacheFile() const;
114 bool writeCacheFile() const;
115 QQmlMetaType::CacheMode aotCacheMode() const;
116
117 QQmlRefPointer<QQmlImports> m_importCache;
118 QVector<PendingImportPtr> m_unresolvedImports;
119 QVector<QQmlRefPointer<QQmlQmldirData>> m_qmldirs;
120 QQmlMetaType::CachedUnitLookupError m_cachedUnitStatus = QQmlMetaType::CachedUnitLookupError::NoError;
121 };
122
123 QQmlTypeLoader(QQmlEngine *);
124 ~QQmlTypeLoader();
125
126 template<
127 typename Engine,
128 typename EnginePrivate = QQmlEnginePrivate,
129 typename = std::enable_if_t<std::is_same_v<Engine, QQmlEngine>>>
130 static QQmlTypeLoader *get(Engine *engine)
131 {
132 return get(EnginePrivate::get(engine));
133 }
134
135 template<
136 typename Engine,
137 typename = std::enable_if_t<std::is_same_v<Engine, QQmlEnginePrivate>>>
138 static QQmlTypeLoader *get(Engine *engine)
139 {
140 return &engine->typeLoader;
141 }
142
143 QQmlImportDatabase *importDatabase() const;
144 ChecksumCache *checksumCache() { return &m_checksumCache; }
145 const ChecksumCache *checksumCache() const { return &m_checksumCache; }
146
147 static QUrl normalize(const QUrl &unNormalizedUrl);
148
149 QQmlRefPointer<QQmlTypeData> getType(const QUrl &unNormalizedUrl, Mode mode = PreferSynchronous);
150 QQmlRefPointer<QQmlTypeData> getType(const QByteArray &, const QUrl &url, Mode mode = PreferSynchronous);
151
152 void injectScript(const QUrl &relativeUrl);
153 QQmlRefPointer<QQmlScriptBlob> injectedScript(const QUrl &relativeUrl);
154
155 QQmlRefPointer<QQmlScriptBlob> getScript(const QUrl &unNormalizedUrl);
156 QQmlRefPointer<QQmlQmldirData> getQmldir(const QUrl &);
157
158 QString absoluteFilePath(const QString &path);
159 bool fileExists(const QString &path, const QString &file);
160 bool directoryExists(const QString &path);
161
162 const QQmlTypeLoaderQmldirContent qmldirContent(const QString &filePath);
163 void setQmldirContent(const QString &filePath, const QString &content);
164
165 void clearCache();
166 void trimCache();
167
168 bool isTypeLoaded(const QUrl &url) const;
169 bool isScriptLoaded(const QUrl &url) const;
170
171 void lock() { m_mutex.lock(); }
172 void unlock() { m_mutex.unlock(); }
173
174 void load(QQmlDataBlob *, Mode = PreferSynchronous);
175 void loadWithStaticData(QQmlDataBlob *, const QByteArray &, Mode = PreferSynchronous);
176 void loadWithCachedUnit(QQmlDataBlob *blob, const QQmlPrivate::CachedQmlUnit *unit, Mode mode = PreferSynchronous);
177 void drop(const QQmlDataBlob::Ptr &blob);
178
179 QQmlEngine *engine() const;
180 void initializeEngine(QQmlEngineExtensionInterface *, const char *);
181 void initializeEngine(QQmlExtensionInterface *, const char *);
182 void invalidate();
183
184#if !QT_CONFIG(qml_debug)
185 quintptr profiler() const { return 0; }
186 void setProfiler(quintptr) {}
187#else
188 QQmlProfiler *profiler() const { return m_profiler.data(); }
189 void setProfiler(QQmlProfiler *profiler);
190#endif // QT_CONFIG(qml_debug)
191
192
193private:
194 friend class QQmlDataBlob;
195 friend class QQmlTypeLoaderThread;
196#if QT_CONFIG(qml_network)
197 friend class QQmlTypeLoaderNetworkReplyProxy;
198#endif // qml_network
199
200 void shutdownThread();
201
202 void loadThread(const QQmlDataBlob::Ptr &);
203 void loadWithStaticDataThread(const QQmlDataBlob::Ptr &, const QByteArray &);
204 void loadWithCachedUnitThread(const QQmlDataBlob::Ptr &blob, const QQmlPrivate::CachedQmlUnit *unit);
205#if QT_CONFIG(qml_network)
206 void networkReplyFinished(QNetworkReply *);
207 void networkReplyProgress(QNetworkReply *, qint64, qint64);
208
209 typedef QHash<QNetworkReply *, QQmlDataBlob::Ptr> NetworkReplies;
210#endif
211
212 void setData(const QQmlDataBlob::Ptr &, const QByteArray &);
213 void setData(const QQmlDataBlob::Ptr &, const QString &fileName);
214 void setData(const QQmlDataBlob::Ptr &, const QQmlDataBlob::SourceCodeData &);
215 void setCachedUnit(const QQmlDataBlob::Ptr &blob, const QQmlPrivate::CachedQmlUnit *unit);
216
217 typedef QHash<QUrl, QQmlTypeData *> TypeCache;
218 typedef QHash<QUrl, QQmlScriptBlob *> ScriptCache;
219 typedef QHash<QUrl, QQmlQmldirData *> QmldirCache;
220 typedef QCache<QString, QCache<QString, bool> > ImportDirCache;
221 typedef QStringHash<QQmlTypeLoaderQmldirContent *> ImportQmlDirCache;
222
223 QQmlEngine *m_engine;
224 QQmlTypeLoaderThread *m_thread;
225 QMutex &m_mutex;
226
227#if QT_CONFIG(qml_debug)
228 QScopedPointer<QQmlProfiler> m_profiler;
229#endif
230
231#if QT_CONFIG(qml_network)
232 NetworkReplies m_networkReplies;
233#endif
234 TypeCache m_typeCache;
235 int m_typeCacheTrimThreshold;
236 ScriptCache m_scriptCache;
237 QmldirCache m_qmldirCache;
238 ImportDirCache m_importDirCache;
239 ImportQmlDirCache m_importQmlDirCache;
240 ChecksumCache m_checksumCache;
241
242 template<typename Loader>
243 void doLoad(const Loader &loader, QQmlDataBlob *blob, Mode mode);
244 void updateTypeCacheTrimThreshold();
245
246 friend struct PlainLoader;
247 friend struct CachedLoader;
248 friend struct StaticLoader;
249};
250
251QT_END_NAMESPACE
252
253#endif // QQMLTYPELOADER_P_H
254

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtdeclarative/src/qml/qml/qqmltypeloader_p.h