5#include <private/qqmldatablob_p.h>
6#include <private/qqmlglobal_p.h>
7#include <private/qqmlprofiler_p.h>
8#include <private/qqmlsourcecoordinate_p.h>
9#include <private/qqmltypedata_p.h>
10#include <private/qqmltypeloader_p.h>
11#include <private/qqmltypeloaderthread_p.h>
13#include <QtQml/qqmlengine.h>
15#include <QtCore/qcryptographichash.h>
17#include <qtqml_tracepoints_p.h>
24
25
26
27
28
29
30
31
34
35
36
37
38
39
40
41
42
43
44
45
46
49
50
51
52
53
54
55
56
59
60
61QQmlDataBlob::QQmlDataBlob(
const QUrl &url, Type type, QQmlTypeLoader *manager)
62 : m_typeLoader(manager)
68 , m_url(manager->interceptUncachedUrl(url, (QQmlAbstractUrlInterceptor::DataType)type))
78QQmlDataBlob::~QQmlDataBlob()
80 Q_ASSERT(m_waitingOnMe.isEmpty());
97 cancelAllWaitingFor();
101
102
103void QQmlDataBlob::startLoading()
107 Q_ASSERT(status() == QQmlDataBlob::Null);
108 setStatus(QQmlDataBlob::Loading);
112
113
114QQmlDataBlob::Type QQmlDataBlob::type()
const
120
121
122QQmlDataBlob::Status QQmlDataBlob::status()
const
124 return m_data.status();
128
129
130bool QQmlDataBlob::isNull()
const
132 return status() == Null;
136
137
138bool QQmlDataBlob::isLoading()
const
140 return status() == Loading;
144
145
146bool QQmlDataBlob::isWaiting()
const
148 return status() == WaitingForDependencies ||
149 status() == ResolvingDependencies;
153
154
155bool QQmlDataBlob::isComplete()
const
157 return status() == Complete;
161
162
163bool QQmlDataBlob::isError()
const
165 return status() == Error;
169
170
171bool QQmlDataBlob::isCompleteOrError()
const
174 return s == Error || s == Complete;
177bool QQmlDataBlob::isAsync()
const
179 return m_data.isAsync();
183
184
185qreal QQmlDataBlob::progress()
const
187 return m_data.progress();
191
192
193
194
195
196
197QUrl QQmlDataBlob::url()
const
202QString QQmlDataBlob::urlString()
const
205 if (m_urlString.isEmpty())
206 m_urlString = m_url.toString();
212
213
214
215
216
217
218
219
220
221
222
223
224QUrl QQmlDataBlob::finalUrl()
const
230
231
232QString QQmlDataBlob::finalUrlString()
const
236 if (m_finalUrlString.isEmpty())
237 m_finalUrlString = m_finalUrl.toString();
239 return m_finalUrlString;
243
244
245
246
247QList<QQmlError> QQmlDataBlob::errors()
const
249 Q_ASSERT(isCompleteOrError()
251 || !m_typeLoader->thread()
252 || m_typeLoader->thread()->isThisThread());
257
258
259
260
261
262
263
264void QQmlDataBlob::setError(
const QQmlError &errors)
266 assertTypeLoaderThread();
274
275
276void QQmlDataBlob::setError(
const QList<QQmlError> &errors)
278 assertTypeLoaderThread();
280 Q_ASSERT(status() != Error);
281 Q_ASSERT(m_errors.isEmpty());
284 m_errors.reserve(errors.size());
285 for (
const QQmlError &error : errors) {
286 if (error.url().isEmpty()) {
287 QQmlError mutableError = error;
288 mutableError.setUrl(url());
289 m_errors.append(mutableError);
291 m_errors.append(error);
295 cancelAllWaitingFor();
299 qWarning().nospace() <<
"Errors for " << urlString();
300 for (
int ii = 0; ii < errors.size(); ++ii)
301 qWarning().nospace() <<
" " << qPrintable(errors.at(ii).toString());
308void QQmlDataBlob::setError(
const QQmlJS::DiagnosticMessage &error)
310 assertTypeLoaderThread();
312 e.setColumn(qmlConvertSourceCoordinate<quint32,
int>(error.loc.startColumn));
313 e.setLine(qmlConvertSourceCoordinate<quint32,
int>(error.loc.startLine));
314 e.setDescription(error.message);
319void QQmlDataBlob::setError(
const QString &description)
321 assertTypeLoaderThread();
323 e.setDescription(description);
329
330
331
332
333
334void QQmlDataBlob::addDependency(
const QQmlDataBlob::Ptr &blob)
336 assertTypeLoaderThread();
338 Q_ASSERT(status() != Null);
341 blob->status() == Error || blob->status() == Complete ||
342 status() == Error || status() == Complete || m_isDone)
345 for (
const auto &existingDep: std::as_const(m_waitingFor)) {
346 if (existingDep.data() == blob)
350 m_waitingFor.append(blob);
351 blob->m_waitingOnMe.append(
this);
353 setStatus(WaitingForDependencies);
356 if (m_waitingOnMe.indexOf(blob.data()) >= 0) {
357 qCWarning(lcCycle) <<
"Cyclic dependency detected between" <<
this->url().toString()
358 <<
"and" << blob->url().toString();
361 error.setDescription(QString::fromLatin1(
"Cyclic dependency detected between \"%1\" and \"%2\"")
362 .arg(url().toString(), blob->url().toString()));
368
369
370
371
372
373
376
377
378
379
380
381
382
383
384
385
386
387void QQmlDataBlob::done()
389 assertTypeLoaderThread();
392#if QT_CONFIG(qml_network)
394
395
396
397
398void QQmlDataBlob::networkError(QNetworkReply::NetworkError networkError)
400 assertTypeLoaderThread();
402 Q_UNUSED(networkError);
407 const char *errorString =
nullptr;
408 switch (networkError) {
410 errorString =
"Network error";
412 case QNetworkReply::ConnectionRefusedError:
413 errorString =
"Connection refused";
415 case QNetworkReply::RemoteHostClosedError:
416 errorString =
"Remote host closed the connection";
418 case QNetworkReply::HostNotFoundError:
419 errorString =
"Host not found";
421 case QNetworkReply::TimeoutError:
422 errorString =
"Timeout";
424 case QNetworkReply::ProxyConnectionRefusedError:
425 case QNetworkReply::ProxyConnectionClosedError:
426 case QNetworkReply::ProxyNotFoundError:
427 case QNetworkReply::ProxyTimeoutError:
428 case QNetworkReply::ProxyAuthenticationRequiredError:
429 case QNetworkReply::UnknownProxyError:
430 errorString =
"Proxy error";
432 case QNetworkReply::ContentAccessDenied:
433 errorString =
"Access denied";
435 case QNetworkReply::ContentNotFoundError:
436 errorString =
"File not found";
438 case QNetworkReply::AuthenticationRequiredError:
439 errorString =
"Authentication required";
443 error.setDescription(QLatin1String(errorString));
450
451
452
453
454void QQmlDataBlob::dependencyError(
const QQmlDataBlob::Ptr &blob)
456 assertTypeLoaderThread();
461
462
463
464
465void QQmlDataBlob::dependencyComplete(
const QQmlDataBlob::Ptr &blob)
467 assertTypeLoaderThread();
472
473
474
475
476
477void QQmlDataBlob::allDependenciesDone()
479 assertTypeLoaderThread();
480 setStatus(QQmlDataBlob::ResolvingDependencies);
484
485
486
487
488
489
490
491
492
493
494void QQmlDataBlob::downloadProgressChanged(qreal progress)
497 assertEngineThread();
501
502
503
504
505
506
507
508
509
510
511
512
513
514void QQmlDataBlob::completed()
516 assertEngineThread();
519void QQmlDataBlob::tryDone()
521 assertTypeLoaderThread();
523 if (status() != Loading && m_waitingFor.isEmpty() && !m_isDone) {
528 qWarning(
"QQmlDataBlob::done() %s", qPrintable(urlString()));
532 if (status() != Error)
535 notifyAllWaitingOnMe();
540 qWarning(
"QQmlDataBlob: Dispatching completed");
542 m_typeLoader->thread()->callCompleted(
this);
548void QQmlDataBlob::cancelAllWaitingFor()
550 while (m_waitingFor.size()) {
555 assertTypeLoaderThreadIfRunning();
557 QQmlRefPointer<QQmlDataBlob> blob = m_waitingFor.takeLast();
559 Q_ASSERT(blob->m_waitingOnMe.contains(
this));
561 blob->m_waitingOnMe.removeOne(
this);
565void QQmlDataBlob::notifyAllWaitingOnMe()
567 assertTypeLoaderThread();
569 while (m_waitingOnMe.size()) {
570 QQmlDataBlob::Ptr blob = m_waitingOnMe.takeLast();
572 Q_ASSERT(std::any_of(blob->m_waitingFor.constBegin(), blob->m_waitingFor.constEnd(),
573 [
this](
const QQmlRefPointer<QQmlDataBlob> &waiting) {
return waiting.data() ==
this; }));
575 blob->notifyComplete(
this);
579void QQmlDataBlob::notifyComplete(
const QQmlDataBlob::Ptr &blob)
581 assertTypeLoaderThread();
583 Q_ASSERT(blob->status() == Error || blob->status() == Complete);
584 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
585 QQmlCompilingProfiler prof(typeLoader()->profiler(), blob.data());
589 QQmlRefPointer<QQmlDataBlob> blobRef;
590 for (
int i = 0; i < m_waitingFor.size(); ++i) {
591 if (m_waitingFor.at(i).data() == blob) {
592 blobRef = m_waitingFor.takeAt(i);
598 if (blob->status() == Error) {
599 dependencyError(blob);
600 }
else if (blob->status() == Complete) {
601 dependencyComplete(blob);
604 if (!isError() && m_waitingFor.isEmpty())
605 allDependenciesDone();
607 m_inCallback =
false;
612QString QQmlDataBlob::SourceCodeData::readAll(QString *error)
const
615 if (hasInlineSourceCode)
616 return inlineSourceCode;
618 QFile f(fileInfo.filePath());
619 if (!f.open(QIODevice::ReadOnly)) {
620 *error = f.errorString();
624 const qint64 fileSize = f.size();
626 if (uchar *mappedData = f.map(0, fileSize)) {
627 QString source = QString::fromUtf8(
reinterpret_cast<
const char *>(mappedData), fileSize);
632 QByteArray data(fileSize, Qt::Uninitialized);
633 if (f.read(data.data(), data.size()) != data.size()) {
634 *error = f.errorString();
637 return QString::fromUtf8(data);
640QDateTime QQmlDataBlob::SourceCodeData::sourceTimeStamp()
const
642 if (hasInlineSourceCode)
650 if (QQmlTypeLoader::isResource(fileInfo.path()))
653 return fileInfo.lastModified();
656QByteArray QQmlDataBlob::SourceCodeData::checksum()
const
658 QCryptographicHash hash(QCryptographicHash::Md5);
659 if (hasInlineSourceCode) {
660 hash.addData(inlineSourceCode.toUtf8());
661 return hash.result();
664 QFile f(fileInfo.filePath());
665 if (!f.open(QIODevice::ReadOnly))
667 if (!hash.addData(&f))
669 return hash.result();
672bool QQmlDataBlob::setStatus(Status status)
677 case WaitingForDependencies:
678 Q_ASSERT(!m_waitingFor.isEmpty());
681 case ResolvingDependencies:
684 Q_ASSERT(m_waitingFor.isEmpty());
688 return m_data.setStatus(status);
DEFINE_BOOL_CONFIG_OPTION(forceDiskCache, QML_FORCE_DISK_CACHE)