diff options
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibCore/NetworkJob.cpp | 10 | ||||
-rw-r--r-- | Libraries/LibCore/NetworkJob.h | 2 | ||||
-rw-r--r-- | Libraries/LibHTTP/HttpJob.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibHTTP/HttpsJob.cpp | 21 | ||||
-rw-r--r-- | Libraries/LibProtocol/Download.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibProtocol/Download.h | 4 |
6 files changed, 45 insertions, 12 deletions
diff --git a/Libraries/LibCore/NetworkJob.cpp b/Libraries/LibCore/NetworkJob.cpp index 6dcba639e0..901aeb3d02 100644 --- a/Libraries/LibCore/NetworkJob.cpp +++ b/Libraries/LibCore/NetworkJob.cpp @@ -78,6 +78,16 @@ void NetworkJob::did_fail(Error error) shutdown(); } +void NetworkJob::did_progress(Optional<u32> total_size, u32 downloaded) +{ + // NOTE: We protect ourselves here, since the callback may otherwise + // trigger destruction of this job somehow. + NonnullRefPtr<NetworkJob> protector(*this); + + if (on_progress) + on_progress(total_size, downloaded); +} + const char* to_string(NetworkJob::Error error) { switch (error) { diff --git a/Libraries/LibCore/NetworkJob.h b/Libraries/LibCore/NetworkJob.h index c637570ab0..8e2f57d8a1 100644 --- a/Libraries/LibCore/NetworkJob.h +++ b/Libraries/LibCore/NetworkJob.h @@ -44,6 +44,7 @@ public: virtual ~NetworkJob() override; Function<void(bool success)> on_finish; + Function<void(Optional<u32>, u32)> on_progress; bool is_cancelled() const { return m_error == Error::Cancelled; } bool has_error() const { return m_error != Error::None; } @@ -64,6 +65,7 @@ protected: NetworkJob(); void did_finish(NonnullRefPtr<NetworkResponse>&&); void did_fail(Error); + void did_progress(Optional<u32> total_size, u32 downloaded); private: RefPtr<NetworkResponse> m_response; diff --git a/Libraries/LibHTTP/HttpJob.cpp b/Libraries/LibHTTP/HttpJob.cpp index 8b1b0e8c9d..41a90b324e 100644 --- a/Libraries/LibHTTP/HttpJob.cpp +++ b/Libraries/LibHTTP/HttpJob.cpp @@ -156,11 +156,23 @@ void HttpJob::on_socket_connected() m_received_size += payload.size(); auto content_length_header = m_headers.get("Content-Length"); + Optional<u32> content_length {}; + if (content_length_header.has_value()) { bool ok; - auto content_length = content_length_header.value().to_uint(ok); - if (ok && m_received_size >= content_length) { - m_received_size = content_length; + auto length = content_length_header.value().to_uint(ok); + if (ok) + content_length = length; + } + + deferred_invoke([this, content_length](auto&) { + did_progress(content_length, m_received_size); + }); + + if (content_length.has_value()) { + auto length = content_length.value(); + if (m_received_size >= length) { + m_received_size = length; finish_up(); } } diff --git a/Libraries/LibHTTP/HttpsJob.cpp b/Libraries/LibHTTP/HttpsJob.cpp index c35575d025..aaa9944ab2 100644 --- a/Libraries/LibHTTP/HttpsJob.cpp +++ b/Libraries/LibHTTP/HttpsJob.cpp @@ -166,16 +166,25 @@ void HttpsJob::on_socket_connected() m_received_size += payload.size(); auto content_length_header = m_headers.get("Content-Length"); + Optional<u32> content_length {}; + if (content_length_header.has_value()) { bool ok; - auto content_length = content_length_header.value().to_uint(ok); - if (ok && m_received_size >= content_length) { - m_received_size = content_length; + auto length = content_length_header.value().to_uint(ok); + if (ok) + content_length = length; + } + + // This needs to be synchronous + // FIXME: Somehow enforce that this should not modify anything + did_progress(content_length, m_received_size); + + if (content_length.has_value()) { + auto length = content_length.value(); + if (m_received_size >= length) { + m_received_size = length; finish_up(); } - } else { - // no content-length, assume closed connection - finish_up(); } }; } diff --git a/Libraries/LibProtocol/Download.cpp b/Libraries/LibProtocol/Download.cpp index 1feefba3f2..37ba4a6d95 100644 --- a/Libraries/LibProtocol/Download.cpp +++ b/Libraries/LibProtocol/Download.cpp @@ -55,7 +55,7 @@ void Download::did_finish(Badge<Client>, bool success, u32 total_size, i32 shbuf on_finish(success, payload, move(shared_buffer)); } -void Download::did_progress(Badge<Client>, u32 total_size, u32 downloaded_size) +void Download::did_progress(Badge<Client>, Optional<u32> total_size, u32 downloaded_size) { if (on_progress) on_progress(total_size, downloaded_size); diff --git a/Libraries/LibProtocol/Download.h b/Libraries/LibProtocol/Download.h index b5577800ed..7296d1df06 100644 --- a/Libraries/LibProtocol/Download.h +++ b/Libraries/LibProtocol/Download.h @@ -47,10 +47,10 @@ public: bool stop(); Function<void(bool success, const ByteBuffer& payload, RefPtr<SharedBuffer> payload_storage)> on_finish; - Function<void(u32 total_size, u32 downloaded_size)> on_progress; + Function<void(Optional<u32> total_size, u32 downloaded_size)> on_progress; void did_finish(Badge<Client>, bool success, u32 total_size, i32 shbuf_id); - void did_progress(Badge<Client>, u32 total_size, u32 downloaded_size); + void did_progress(Badge<Client>, Optional<u32> total_size, u32 downloaded_size); private: explicit Download(Client&, i32 download_id); |