diff --git a/src/corelib/io/qsettings_wasm.cpp b/src/corelib/io/qsettings_wasm.cpp index 9b206e26d53..da51fe68364 100644 --- a/src/corelib/io/qsettings_wasm.cpp +++ b/src/corelib/io/qsettings_wasm.cpp @@ -18,7 +18,9 @@ #include #include -#include +# include +# include +# include QT_BEGIN_NAMESPACE @@ -58,7 +60,6 @@ public: QString fileName() const final; private: - val m_localStorage = val::global("window")["localStorage"]; QStringList m_keyPrefixes; }; @@ -108,89 +109,106 @@ void QWasmLocalStorageSettingsPrivate::remove(const QString &key) { const std::string removed = QString(m_keyPrefixes.first() + key).toStdString(); - std::vector children = { removed }; - const int length = m_localStorage["length"].as(); - for (int i = 0; i < length; ++i) { - const QString storedKeyWithPrefix = - QString::fromStdString(m_localStorage.call("key", i).as()); + qstdweb::runTaskOnMainThread([this, &removed, &key]() { + std::vector children = { removed }; + const int length = val::global("window")["localStorage"]["length"].as(); + for (int i = 0; i < length; ++i) { + const QString storedKeyWithPrefix = QString::fromStdString( + val::global("window")["localStorage"].call("key", i).as()); - const QStringView storedKey = keyNameFromPrefixedStorageName( - m_keyPrefixes.first(), QStringView(storedKeyWithPrefix)); - if (storedKey.isEmpty() || !storedKey.startsWith(key)) - continue; + const QStringView storedKey = keyNameFromPrefixedStorageName( + m_keyPrefixes.first(), QStringView(storedKeyWithPrefix)); + if (storedKey.isEmpty() || !storedKey.startsWith(key)) + continue; - children.push_back(storedKeyWithPrefix.toStdString()); - } + children.push_back(storedKeyWithPrefix.toStdString()); + } - for (const auto &child : children) - m_localStorage.call("removeItem", child); + for (const auto &child : children) + val::global("window")["localStorage"].call("removeItem", child); + }); } void QWasmLocalStorageSettingsPrivate::set(const QString &key, const QVariant &value) { - const std::string keyString = QString(m_keyPrefixes.first() + key).toStdString(); - const std::string valueString = QSettingsPrivate::variantToString(value).toStdString(); - m_localStorage.call("setItem", keyString, valueString); + qstdweb::runTaskOnMainThread([this, &key, &value]() { + const std::string keyString = QString(m_keyPrefixes.first() + key).toStdString(); + const std::string valueString = QSettingsPrivate::variantToString(value).toStdString(); + val::global("window")["localStorage"].call("setItem", keyString, valueString); + }); } std::optional QWasmLocalStorageSettingsPrivate::get(const QString &key) const { - for (const auto &prefix : m_keyPrefixes) { - const std::string keyString = QString(prefix + key).toStdString(); - const emscripten::val value = m_localStorage.call("getItem", keyString); - if (!value.isNull()) - return QSettingsPrivate::stringToVariant( - QString::fromStdString(value.as())); - if (!fallbacks) - return std::nullopt; - } - return std::nullopt; + return qstdweb::runTaskOnMainThread>( + [this, &key]() -> std::optional { + for (const auto &prefix : m_keyPrefixes) { + const std::string keyString = QString(prefix + key).toStdString(); + const emscripten::val value = + val::global("window")["localStorage"].call("getItem", keyString); + if (!value.isNull()) { + return QSettingsPrivate::stringToVariant( + QString::fromStdString(value.as())); + } + if (!fallbacks) { + return std::nullopt; + } + } + return std::nullopt; + }); } QStringList QWasmLocalStorageSettingsPrivate::children(const QString &prefix, ChildSpec spec) const { - QSet nodes; - // Loop through all keys on window.localStorage, return Qt keys belonging to - // this application, with the correct prefix, and according to ChildSpec. - QStringList children; - const int length = m_localStorage["length"].as(); - for (int i = 0; i < length; ++i) { - for (const auto &storagePrefix : m_keyPrefixes) { - const QString keyString = - QString::fromStdString(m_localStorage.call("key", i).as()); + return qstdweb::runTaskOnMainThread([this, &prefix, &spec]() -> QStringList { + QSet nodes; + // Loop through all keys on window.localStorage, return Qt keys belonging to + // this application, with the correct prefix, and according to ChildSpec. + QStringList children; + const int length = val::global("window")["localStorage"]["length"].as(); + for (int i = 0; i < length; ++i) { + for (const auto &storagePrefix : m_keyPrefixes) { + const QString keyString = + QString::fromStdString(val::global("window")["localStorage"] + .call("key", i) + .as()); - const QStringView key = - keyNameFromPrefixedStorageName(storagePrefix, QStringView(keyString)); - if (!key.isEmpty() && key.startsWith(prefix)) { - QStringList children; - QSettingsPrivate::processChild(key.sliced(prefix.length()), spec, children); - if (!children.isEmpty()) - nodes.insert(children.first()); + const QStringView key = + keyNameFromPrefixedStorageName(storagePrefix, QStringView(keyString)); + if (!key.isEmpty() && key.startsWith(prefix)) { + QStringList children; + QSettingsPrivate::processChild(key.sliced(prefix.length()), spec, children); + if (!children.isEmpty()) + nodes.insert(children.first()); + } + if (!fallbacks) + break; } - if (!fallbacks) - break; } - } - return QStringList(nodes.begin(), nodes.end()); + return QStringList(nodes.begin(), nodes.end()); + }); } void QWasmLocalStorageSettingsPrivate::clear() { - // Get all Qt keys from window.localStorage - const int length = m_localStorage["length"].as(); - QStringList keys; - keys.reserve(length); - for (int i = 0; i < length; ++i) - keys.append(QString::fromStdString((m_localStorage.call("key", i).as()))); + qstdweb::runTaskOnMainThread([this]() { + // Get all Qt keys from window.localStorage + const int length = val::global("window")["localStorage"]["length"].as(); + QStringList keys; + keys.reserve(length); + for (int i = 0; i < length; ++i) + keys.append(QString::fromStdString( + (val::global("window")["localStorage"].call("key", i).as()))); - // Remove all Qt keys. Note that localStorage does not guarantee a stable - // iteration order when the storage is mutated, which is why removal is done - // in a second step after getting all keys. - for (const QString &key : keys) { - if (!keyNameFromPrefixedStorageName(m_keyPrefixes.first(), key).isEmpty()) - m_localStorage.call("removeItem", key.toStdString()); - } + // Remove all Qt keys. Note that localStorage does not guarantee a stable + // iteration order when the storage is mutated, which is why removal is done + // in a second step after getting all keys. + for (const QString &key : keys) { + if (!keyNameFromPrefixedStorageName(m_keyPrefixes.first(), key).isEmpty()) + val::global("window")["localStorage"].call("removeItem", key.toStdString()); + } + }); } void QWasmLocalStorageSettingsPrivate::sync() { } @@ -361,9 +379,12 @@ QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format, QSettings:: format = QSettings::WebLocalStorageFormat; // Check if cookies are enabled (required for using persistent storage) - const bool cookiesEnabled = val::global("navigator")["cookieEnabled"].as(); - constexpr QLatin1StringView cookiesWarningMessage - ("QSettings::%1 requires cookies, falling back to IniFormat with temporary file"); + + const bool cookiesEnabled = qstdweb::runTaskOnMainThread( + []() { return val::global("navigator")["cookieEnabled"].as(); }); + + constexpr QLatin1StringView cookiesWarningMessage( + "QSettings::%1 requires cookies, falling back to IniFormat with temporary file"); if (!cookiesEnabled) { if (format == QSettings::WebLocalStorageFormat) { qWarning() << cookiesWarningMessage.arg("WebLocalStorageFormat"); diff --git a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp index 0661ac17ca7..74784fb3c24 100644 --- a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp +++ b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,7 @@ #if defined(Q_OS_WASM) #include +#include "emscripten/threading.h" #include "emscripten/val.h" #endif @@ -2136,6 +2138,12 @@ void tst_QSettings::testThreadSafety() #if !QT_CONFIG(thread) QSKIP("This test requires threads to be enabled."); #endif // !QT_CONFIG(thread) +#if defined(Q_OS_WASM) + if (!qstdweb::haveJspi()) + QSKIP("Test needs jspi on WASM. Calls are proxied to the main thread from SettingsThreads, " + "which necessitates the use of an event loop to yield to the main loop. Event loops " + "require jspi."); +#endif SettingsThread threads[NumThreads]; int i, j; @@ -2144,6 +2152,19 @@ void tst_QSettings::testThreadSafety() for (i = 0; i < NumThreads; ++i) threads[i].start(i + 1); + +#if defined(Q_OS_WASM) && QT_CONFIG(thread) + QEventLoop loop; + int remaining = NumThreads; + for (int i = 0; i < NumThreads; ++i) { + QObject::connect(&threads[i], &QThread::finished, this, [&remaining, &loop]() { + if (!--remaining) + loop.quit(); + }); + } + loop.exec(); +#endif // defined(Q_OS_WASM) && QT_CONFIG(thread) + for (i = 0; i < NumThreads; ++i) threads[i].wait();