Skip to content

Truly independent web browser

LibRequests: Notify request owners when delivery tears down early

Luke Wilde7b4fb13ba2e3
diff --git a/Libraries/LibRequests/Request.cpp b/Libraries/LibRequests/Request.cpp
index b5743e7ede9..3207eddd342 100644
--- a/Libraries/LibRequests/Request.cpp
+++ b/Libraries/LibRequests/Request.cpp
@@ -77,11 +77,14 @@ bool Request::stop()
if (!had_active_request)
release_transfer_lease();
auto on_stop = move(m_on_stop);
+ auto teardown = move(on_teardown);
defer_teardown();
if (had_active_request && on_stop)
on_stop();
+ if (teardown)
+ teardown();
return had_active_request;
}
@@ -275,11 +278,14 @@ void Request::did_transfer(Badge<RequestClient>)
{
m_transfer_lease.clear();
auto on_stop = move(m_on_stop);
+ auto teardown = move(on_teardown);
defer_teardown();
if (on_stop)
on_stop();
+ if (teardown)
+ teardown();
}
void Request::defer_teardown()
@@ -335,6 +341,7 @@ void Request::set_up_internal_stream_data(DataReceived on_data_available)
m_internal_stream_data->read_stream = nullptr;
// The request has finished for its owner, so a stop or transfer that follows has nothing left to report.
m_on_stop = nullptr;
+ on_teardown = nullptr;
user_on_finish(m_internal_stream_data->total_size, m_internal_stream_data->timing_info, m_internal_stream_data->network_error);
defer_teardown();
}
diff --git a/Libraries/LibRequests/Request.h b/Libraries/LibRequests/Request.h
index 7a802ffb6c6..53a61697024 100644
--- a/Libraries/LibRequests/Request.h
+++ b/Libraries/LibRequests/Request.h
@@ -120,6 +120,9 @@ public:
Function<void()> on_requires_network;
+ // Invoked at most once, when the request drops its delivery callbacks without a finish: a stop() or a transfer.
+ Function<void()> on_teardown;
+
void did_finish(Badge<RequestClient>, u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> const& network_error);
void did_receive_headers(Badge<RequestClient>, NonnullRefPtr<HTTP::HeaderList> response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase, Optional<Core::ImmutableBytes> javascript_bytecode, Optional<u64> javascript_bytecode_cache_vary_key, CacheState cache_state);
void did_transfer(Badge<RequestClient>);