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 | #include <private/qqmlengine_p.h> |
5 | #include <private/qqmlextensionplugin_p.h> |
6 | #include <private/qqmltypeloaderthread_p.h> |
7 | |
8 | #if QT_CONFIG(qml_network) |
9 | #include <private/qqmltypeloadernetworkreplyproxy_p.h> |
10 | #endif |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | QQmlTypeLoaderThread::QQmlTypeLoaderThread(QQmlTypeLoader *loader) |
15 | : m_loader(loader) |
16 | { |
17 | // Do that after initializing all the members. |
18 | startup(); |
19 | } |
20 | |
21 | #if QT_CONFIG(qml_network) |
22 | QNetworkAccessManager *QQmlTypeLoaderThread::networkAccessManager() const |
23 | { |
24 | Q_ASSERT(isThisThread()); |
25 | if (!m_networkAccessManager) { |
26 | m_networkAccessManager = QQmlEnginePrivate::get(e: m_loader->engine())->createNetworkAccessManager(parent: nullptr); |
27 | QObject::connect(sender: thread(), signal: &QThread::finished, context: m_networkAccessManager, slot: &QObject::deleteLater); |
28 | m_networkReplyProxy = new QQmlTypeLoaderNetworkReplyProxy(m_loader); |
29 | QObject::connect(sender: thread(), signal: &QThread::finished, context: m_networkReplyProxy, slot: &QObject::deleteLater); |
30 | } |
31 | |
32 | return m_networkAccessManager; |
33 | } |
34 | |
35 | QQmlTypeLoaderNetworkReplyProxy *QQmlTypeLoaderThread::networkReplyProxy() const |
36 | { |
37 | Q_ASSERT(isThisThread()); |
38 | Q_ASSERT(m_networkReplyProxy); // Must call networkAccessManager() first |
39 | return m_networkReplyProxy; |
40 | } |
41 | #endif // qml_network |
42 | |
43 | void QQmlTypeLoaderThread::load(const QQmlDataBlob::Ptr &b) |
44 | { |
45 | callMethodInThread(method: &This::loadThread, args: b); |
46 | } |
47 | |
48 | void QQmlTypeLoaderThread::loadAsync(const QQmlDataBlob::Ptr &b) |
49 | { |
50 | postMethodToThread(method: &This::loadThread, args: b); |
51 | } |
52 | |
53 | void QQmlTypeLoaderThread::loadWithStaticData(const QQmlDataBlob::Ptr &b, const QByteArray &d) |
54 | { |
55 | callMethodInThread(method: &This::loadWithStaticDataThread, args: b, args: d); |
56 | } |
57 | |
58 | void QQmlTypeLoaderThread::loadWithStaticDataAsync(const QQmlDataBlob::Ptr &b, const QByteArray &d) |
59 | { |
60 | postMethodToThread(method: &This::loadWithStaticDataThread, args: b, args: d); |
61 | } |
62 | |
63 | void QQmlTypeLoaderThread::loadWithCachedUnit(const QQmlDataBlob::Ptr &b, const QQmlPrivate::CachedQmlUnit *unit) |
64 | { |
65 | callMethodInThread(method: &This::loadWithCachedUnitThread, args: b, args&: unit); |
66 | } |
67 | |
68 | void QQmlTypeLoaderThread::loadWithCachedUnitAsync(const QQmlDataBlob::Ptr &b, const QQmlPrivate::CachedQmlUnit *unit) |
69 | { |
70 | postMethodToThread(method: &This::loadWithCachedUnitThread, args: b, args&: unit); |
71 | } |
72 | |
73 | void QQmlTypeLoaderThread::callCompleted(const QQmlDataBlob::Ptr &b) |
74 | { |
75 | #if !QT_CONFIG(thread) |
76 | if (!isThisThread()) |
77 | postMethodToThread(&This::callCompletedMain, b); |
78 | #else |
79 | postMethodToMain(method: &This::callCompletedMain, args: b); |
80 | #endif |
81 | } |
82 | |
83 | void QQmlTypeLoaderThread::callDownloadProgressChanged(const QQmlDataBlob::Ptr &b, qreal p) |
84 | { |
85 | #if !QT_CONFIG(thread) |
86 | if (!isThisThread()) |
87 | postMethodToThread(&This::callDownloadProgressChangedMain, b, p); |
88 | #else |
89 | postMethodToMain(method: &This::callDownloadProgressChangedMain, args: b, args&: p); |
90 | #endif |
91 | } |
92 | |
93 | void QQmlTypeLoaderThread::initializeEngine(QQmlExtensionInterface *iface, |
94 | const char *uri) |
95 | { |
96 | callMethodInMain(method: &This::initializeExtensionMain, args&: iface, args&: uri); |
97 | } |
98 | |
99 | void QQmlTypeLoaderThread::initializeEngine(QQmlEngineExtensionInterface *iface, |
100 | const char *uri) |
101 | { |
102 | callMethodInMain(method: &This::initializeEngineExtensionMain, args&: iface, args&: uri); |
103 | } |
104 | |
105 | void QQmlTypeLoaderThread::loadThread(const QQmlDataBlob::Ptr &b) |
106 | { |
107 | m_loader->loadThread(b); |
108 | } |
109 | |
110 | void QQmlTypeLoaderThread::loadWithStaticDataThread(const QQmlDataBlob::Ptr &b, const QByteArray &d) |
111 | { |
112 | m_loader->loadWithStaticDataThread(b, d); |
113 | } |
114 | |
115 | void QQmlTypeLoaderThread::loadWithCachedUnitThread(const QQmlDataBlob::Ptr &b, const QQmlPrivate::CachedQmlUnit *unit) |
116 | { |
117 | m_loader->loadWithCachedUnitThread(blob: b, unit); |
118 | } |
119 | |
120 | void QQmlTypeLoaderThread::callCompletedMain(const QQmlDataBlob::Ptr &b) |
121 | { |
122 | #ifdef DATABLOB_DEBUG |
123 | qWarning("QQmlTypeLoaderThread: %s completed() callback" , qPrintable(b->urlString())); |
124 | #endif |
125 | b->completed(); |
126 | } |
127 | |
128 | void QQmlTypeLoaderThread::callDownloadProgressChangedMain(const QQmlDataBlob::Ptr &b, qreal p) |
129 | { |
130 | #ifdef DATABLOB_DEBUG |
131 | qWarning("QQmlTypeLoaderThread: %s downloadProgressChanged(%f) callback" , |
132 | qPrintable(b->urlString()), p); |
133 | #endif |
134 | b->downloadProgressChanged(p); |
135 | } |
136 | |
137 | void QQmlTypeLoaderThread::initializeExtensionMain(QQmlExtensionInterface *iface, |
138 | const char *uri) |
139 | { |
140 | Q_ASSERT(m_loader->engine()->thread() == QThread::currentThread()); |
141 | iface->initializeEngine(engine: m_loader->engine(), uri); |
142 | } |
143 | |
144 | void QQmlTypeLoaderThread::initializeEngineExtensionMain(QQmlEngineExtensionInterface *iface, |
145 | const char *uri) |
146 | { |
147 | Q_ASSERT(m_loader->engine()->thread() == QThread::currentThread()); |
148 | iface->initializeEngine(engine: m_loader->engine(), uri); |
149 | } |
150 | |
151 | QT_END_NAMESPACE |
152 | |