summaryrefslogtreecommitdiffstats
path: root/src/network/access/qnetworkreplywasmimpl_p.h
diff options
context:
space:
mode:
authorPiotr Wierciński <piotr.wiercinski@qt.io>2024-04-02 16:55:07 +0200
committerPiotr Wierciński <piotr.wiercinski@qt.io>2024-05-10 09:55:19 +0000
commit9907ef0d64f743fbf269967a65005d490ba0a432 (patch)
tree2b918e0fe6752f6206eef4267d9f0af7a5f94c55 /src/network/access/qnetworkreplywasmimpl_p.h
parent0ae44ccc6f3017213ba8a66d703b59c5fcb336a5 (diff)
wasm: Allow fetching from background thread
Allow network request from background thread by proxing it to main thread if needed. Introduce "fetchHelper" which is stored on heap and owns "outgoingData" which must be valid during entire fetch operation. It is also used for synchronization between thread that has scheduled fetch operation and the one that is executing it. Enable the test that was skipped before fix. Fixes: QTBUG-124111 Change-Id: Ifafa4c40fa435122639fa861a61fbf96340a7747 Reviewed-by: Piotr Wierciński <piotr.wiercinski@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io> Reviewed-by: Jøger Hansegård <joger.hansegard@qt.io>
Diffstat (limited to 'src/network/access/qnetworkreplywasmimpl_p.h')
-rw-r--r--src/network/access/qnetworkreplywasmimpl_p.h26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/network/access/qnetworkreplywasmimpl_p.h b/src/network/access/qnetworkreplywasmimpl_p.h
index ae167799d7..4b00bb09ea 100644
--- a/src/network/access/qnetworkreplywasmimpl_p.h
+++ b/src/network/access/qnetworkreplywasmimpl_p.h
@@ -28,6 +28,7 @@
#include <emscripten/fetch.h>
#include <memory>
+#include <mutex>
QT_BEGIN_NAMESPACE
@@ -63,6 +64,29 @@ private:
QByteArray methodName() const;
};
+class QNetworkReplyWasmImplPrivate;
+
+/*!
+ The FetchContext class ensures the requestData object remains valid
+ while a fetch operation is pending. Since Emscripten fetch is asynchronous,
+ requestData must persist until one of the final callbacks is invoked.
+ Additionally, there's a potential race condition between the thread
+ scheduling the fetch operation and the one executing it. Since fetch must
+ occur on the main thread due to browser limitations,
+ a mutex safeguards the FetchContext to ensure atomic state transitions.
+*/
+struct FetchContext
+{
+ enum class State { SCHEDULED, SENT, FINISHED, CANCELED, TO_BE_DESTROYED };
+
+ FetchContext(QNetworkReplyWasmImplPrivate *networkReply) : reply(networkReply) { }
+
+ QNetworkReplyWasmImplPrivate *reply{ nullptr };
+ std::mutex mutex;
+ QByteArray requestData;
+ State state{ State::SCHEDULED };
+};
+
class QNetworkReplyWasmImplPrivate: public QNetworkReplyPrivate
{
public:
@@ -101,7 +125,6 @@ public:
QIODevice *outgoingData;
std::shared_ptr<QRingBuffer> outgoingDataBuffer;
- QByteArray requestData;
static void downloadProgress(emscripten_fetch_t *fetch);
static void downloadFailed(emscripten_fetch_t *fetch);
@@ -111,6 +134,7 @@ public:
static QNetworkReply::NetworkError statusCodeFromHttp(int httpStatusCode, const QUrl &url);
emscripten_fetch_t *m_fetch;
+ FetchContext *m_fetchContext;
void setReplyFinished();
void setCanceled();