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
qqmlpreviewfileloader.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
7
8#include <QtQml/qqmlfile.h>
9
10#include <QtCore/qlibraryinfo.h>
11#include <QtCore/qstandardpaths.h>
12
14
15QQmlPreviewFileLoader::QQmlPreviewFileLoader(QQmlPreviewServiceImpl *service)
16{
17 // This is created from stateChanged() on the debug server thread, but that's not where we
18 // want it to live. It logically belongs to the service. Mind that the service does not live
19 // on the debug server thread, but does process messageReceived() on the debug server thread.
20 moveToThread(service->thread());
21
22 // Exclude some resource paths used by Qt itself. There is no point in loading those from the
23 // client as the client will not have the files (or even worse, it may have different ones).
24 m_blacklist.blacklist(":/qt-project.org");
25 m_blacklist.blacklist(":/QtQuick/Controls/Styles");
26 m_blacklist.blacklist(":/ExtrasImports/QtQuick/Controls/Styles");
27
28 // Target specific configuration should not replaced with files from the host.
29 m_blacklist.blacklist("/etc");
30
31 for (int loc = QLibraryInfo::PrefixPath; loc < QLibraryInfo::TestsPath; ++loc) {
32 m_blacklist.blacklist(QLibraryInfo::paths(
33 static_cast<QLibraryInfo::LibraryPath>(loc)));
34 }
35 m_blacklist.blacklist(QLibraryInfo::paths(QLibraryInfo::SettingsPath));
36
37 static const QStandardPaths::StandardLocation blackListLocations[] = {
38 QStandardPaths::CacheLocation,
39 QStandardPaths::GenericDataLocation,
40 QStandardPaths::ConfigLocation,
41 QStandardPaths::GenericCacheLocation,
42 QStandardPaths::GenericConfigLocation,
43 QStandardPaths::AppDataLocation,
44 QStandardPaths::AppConfigLocation
45 };
46
47 for (auto locationType : blackListLocations) {
48 const QStringList locations = QStandardPaths::standardLocations(locationType);
49 for (const QString &location : locations)
50 m_blacklist.blacklist(location);
51 }
52
53 m_blacklist.whitelist(QLibraryInfo::paths(QLibraryInfo::TestsPath));
54
55 // On Windows, GenericDataLocation (AppData/Local) is a parent of the temp directory
56 // (AppData/Local/Temp). Whitelist the temp path since we commonly want to preview
57 // files in the temp location.
58 const QStringList tempLocations
59 = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
60 for (const QString &tempLocation : tempLocations)
61 m_blacklist.whitelist(tempLocation);
62
63 // We absolutely want those signals to be connected directly. The service's (directly connected)
64 // load() signal should not be able to sneak in between a file() signal submission and its
65 // arrival. All the file loader's methods are protected by the contentMutex. So we can connect
66 // directly.
67 connect(this, &QQmlPreviewFileLoader::request, service, &QQmlPreviewServiceImpl::forwardRequest,
68 Qt::DirectConnection);
69 connect(service, &QQmlPreviewServiceImpl::directory, this, &QQmlPreviewFileLoader::directory,
70 Qt::DirectConnection);
71 connect(service, &QQmlPreviewServiceImpl::file, this, &QQmlPreviewFileLoader::file,
72 Qt::DirectConnection);
73 connect(service, &QQmlPreviewServiceImpl::error, this, &QQmlPreviewFileLoader::error,
74 Qt::DirectConnection);
75 connect(service, &QQmlPreviewServiceImpl::clearCache, this, &QQmlPreviewFileLoader::clearCache,
76 Qt::DirectConnection);
77}
78
80{
81 QMutexLocker locker(&m_contentMutex);
82 m_path = path;
83
84 auto fileIterator = m_fileCache.constFind(path);
85 if (fileIterator != m_fileCache.constEnd())
86 return Data(fileIterator->contents, fileIterator->permissions);
87
88 auto dirIterator = m_directoryCache.constFind(path);
89 if (dirIterator != m_directoryCache.constEnd())
90 return Data(dirIterator->entries, dirIterator->permissions);
91
92 m_requestedData = Data();
93 emit request(path);
94 m_waitCondition.wait(&m_contentMutex);
95 return std::move(m_requestedData);
96}
97
98void QQmlPreviewFileLoader::whitelist(const QUrl &url)
99{
100 const QString path = QQmlFile::urlToLocalFileOrQrc(url);
101 if (!path.isEmpty()) {
102 QMutexLocker locker(&m_contentMutex);
103 m_blacklist.whitelist(path);
104 }
105}
106
107bool QQmlPreviewFileLoader::isBlacklisted(const QString &path)
108{
109 QMutexLocker locker(&m_contentMutex);
110 return m_blacklist.isBlacklisted(path);
111}
112
113void QQmlPreviewFileLoader::file(const QString &path, const QByteArray &contents,
114 QFileDevice::Permissions permissions)
115{
116 QMutexLocker locker(&m_contentMutex);
117 m_blacklist.whitelist(path);
118 m_fileCache[path] = { contents, permissions };
119 if (path == m_path) {
120 m_requestedData = Data(contents, permissions);
121 m_waitCondition.wakeOne();
122 }
123}
124
125void QQmlPreviewFileLoader::directory(const QString &path, const QStringList &entries,
126 QFileDevice::Permissions permissions)
127{
128 QMutexLocker locker(&m_contentMutex);
129 m_blacklist.whitelist(path);
130 m_directoryCache[path] = { entries, permissions };
131 if (path == m_path) {
132 m_requestedData = Data(entries, permissions);
133 m_waitCondition.wakeOne();
134 }
135}
136
137void QQmlPreviewFileLoader::error(const QString &path)
138{
139 QMutexLocker locker(&m_contentMutex);
140 m_blacklist.blacklist(path);
141 if (path == m_path) {
142 m_requestedData = Data(Fallback);
143 m_waitCondition.wakeOne();
144 }
145}
146
147void QQmlPreviewFileLoader::clearCache()
148{
149 QMutexLocker locker(&m_contentMutex);
150 m_fileCache.clear();
151 m_directoryCache.clear();
152}
153
154QT_END_NAMESPACE
155
156#include "moc_qqmlpreviewfileloader.cpp"
bool isBlacklisted(const QString &file)
Data load(const QString &file)
void whitelist(const QUrl &url)
Combined button and popup list for selecting options.
connect(manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished)