Skip to content

Truly independent web browser

LibWeb: Move the fetch state checks onto FetchControlBlock

Luke Wilde4a325a386f35
diff --git a/Libraries/LibWeb/Fetch/Fetching/FetchEventSink.cpp b/Libraries/LibWeb/Fetch/Fetching/FetchEventSink.cpp
index 00ebcf9e3c1..06abe192472 100644
--- a/Libraries/LibWeb/Fetch/Fetching/FetchEventSink.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/FetchEventSink.cpp
@@ -32,21 +32,13 @@ GC::Ref<Infrastructure::FetchController> FetchEventSink::controller() const
return *controller;
}
-// https://fetch.spec.whatwg.org/#fetch-params-canceled
-static bool is_canceled(Infrastructure::FetchController const& controller)
-{
- // A fetch params fetchParams is canceled if its controller’s state is "aborted" or "terminated".
- auto state = controller.state();
- return state == Infrastructure::FetchController::State::Aborted || state == Infrastructure::FetchController::State::Terminated;
-}
-
// https://fetch.spec.whatwg.org/#concept-http-network-fetch
// The processBodyChunk steps of transmitting request's body.
// NB: The steps read only bytes's length, so they are given it.
static void process_body_chunk(Infrastructure::FetchController& controller, u64 length)
{
// 1. If fetchParams is canceled, then abort these steps.
- if (is_canceled(controller))
+ if (controller.control_block().is_canceled())
return;
// 2. Run this step in parallel: transmit bytes.
@@ -63,7 +55,7 @@ static void process_body_chunk(Infrastructure::FetchController& controller, u64
static void process_end_of_body(Infrastructure::FetchController& controller)
{
// 1. If fetchParams is canceled, then abort these steps.
- if (is_canceled(controller))
+ if (controller.control_block().is_canceled())
return;
// 2. If fetchParams’s process request end-of-body is non-null, then run fetchParams’s process request
@@ -99,7 +91,7 @@ static void read_request_body_stream(GC::Ref<Infrastructure::FetchController> co
// 3. Let processBodyError given e be these steps:
auto process_body_error = GC::create_function(GC::Heap::the(), GC::captures(realm, controller), [](auto& realm, auto& controller, JS::Value error) {
// 1. If fetchParams is canceled, then abort these steps.
- if (is_canceled(controller))
+ if (controller->control_block().is_canceled())
return;
// 2. If e is an "AbortError" DOMException, then abort fetchParams’s controller.
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 69530ca343b..19d071ae1b0 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -1256,7 +1256,7 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const
auto algorithms = controller->algorithms();
// NB: Nobody reads the body of a fetch stopped before its handover.
- auto body_intent = fetch_params.control_block().state() == Infrastructure::FetchControlBlock::State::Stopped
+ auto body_intent = fetch_params.control_block().is_stopped()
? Engine::BodyIntent::DrainAndDiscard
: fetch_params.control_block().body_intent();
@@ -2296,7 +2296,7 @@ static GC::Ref<PendingResponse> continue_http_network_or_cache_fetch(JS::Object&
}
// AD-HOC: If the controller is already in the non-spec Stopped state, we should cancel the network request immediately.
- if (http_fetch_params->control_block().state() == Infrastructure::FetchControlBlock::State::Stopped)
+ if (http_fetch_params->control_block().is_stopped())
http_fetch_params->controller()->stop_fetch();
auto returned_pending_response = PendingResponse::create(fetch_params);
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchControlBlock.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchControlBlock.h
index 94df00ac676..0a7985999e1 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchControlBlock.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchControlBlock.h
@@ -32,6 +32,23 @@ public:
[[nodiscard]] State state() const { return m_state; }
void set_state(Badge<FetchController>, State state) { m_state = state; }
+ // https://fetch.spec.whatwg.org/#fetch-params-aborted
+ [[nodiscard]] bool is_aborted() const
+ {
+ // A fetch params fetchParams is aborted if its controller’s state is "aborted".
+ return m_state == State::Aborted;
+ }
+
+ // https://fetch.spec.whatwg.org/#fetch-params-canceled
+ [[nodiscard]] bool is_canceled() const
+ {
+ // A fetch params fetchParams is canceled if its controller’s state is "aborted" or "terminated".
+ return m_state == State::Aborted || m_state == State::Terminated;
+ }
+
+ [[nodiscard]] bool is_ongoing() const { return m_state == State::Ongoing; }
+ [[nodiscard]] bool is_stopped() const { return m_state == State::Stopped; }
+
[[nodiscard]] RefPtr<FetchTimingInfo> full_timing_info() const { return m_full_timing_info; }
void set_full_timing_info(NonnullRefPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = move(full_timing_info); }
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
index b313a6131be..c1815d93b39 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
@@ -237,7 +237,7 @@ void FetchController::terminate()
void FetchController::stop_fetch()
{
- if (state() == State::Aborted || state() == State::Terminated)
+ if (m_control_block->is_canceled())
return;
m_control_block->set_state({}, State::Stopped);
@@ -276,7 +276,7 @@ void FetchController::stop_fetch()
void FetchController::stop_request()
{
end_rendering_pending_operation(this);
- VERIFY(state() != State::Ongoing);
+ VERIFY(!m_control_block->is_ongoing());
m_awaited_network_completion.clear();
stop_body_transport();
}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
index 27a3c868dc2..bd1e5f14637 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
@@ -65,16 +65,13 @@ GC::Ref<FetchController> FetchParams::controller() const
// https://fetch.spec.whatwg.org/#fetch-params-aborted
bool FetchParams::is_aborted() const
{
- // A fetch params fetchParams is aborted if its controller’s state is "aborted".
- return m_control_block->state() == FetchControlBlock::State::Aborted;
+ return m_control_block->is_aborted();
}
// https://fetch.spec.whatwg.org/#fetch-params-canceled
bool FetchParams::is_canceled() const
{
- // A fetch params fetchParams is canceled if its controller’s state is "aborted" or "terminated".
- auto state = m_control_block->state();
- return state == FetchControlBlock::State::Aborted || state == FetchControlBlock::State::Terminated;
+ return m_control_block->is_canceled();
}
}
diff --git a/Libraries/LibWeb/WebDriver/BiDi/Network.cpp b/Libraries/LibWeb/WebDriver/BiDi/Network.cpp
index aa6aac0ea48..db8a8b4cde2 100644
--- a/Libraries/LibWeb/WebDriver/BiDi/Network.cpp
+++ b/Libraries/LibWeb/WebDriver/BiDi/Network.cpp
@@ -275,7 +275,7 @@ JsonArray get_network_intercepts(FetchParams const& fetch_params, StringView pha
// NB: An existing request keeps its original context. A request first observed after instrumentation was
// enabled can still discover its context, and blocking must use that same context.
auto navigable = request_navigable(state.ptr(), *request);
- if (!navigable || fetch_params.is_canceled() || fetch_params.control_block().state() == FetchControlBlock::State::Stopped)
+ if (!navigable || !fetch_params.control_block().is_ongoing())
return intercepts;
if (!state)
initialize_network_request(fetch_params, navigable);
@@ -559,7 +559,7 @@ void block_network_request(FetchParams const& fetch_params, RefPtr<Fetch::Infras
{
initialize_network_request(fetch_params);
auto state = network_request_of(fetch_params);
- if (fetch_params.is_canceled() || fetch_params.control_block().state() == FetchControlBlock::State::Stopped) {
+ if (!fetch_params.control_block().is_ongoing()) {
if (response)
stop_response_transport(*fetch_params.controller(), *response);
continuation->invoke(NetworkContinuationAction::Fail, Response::network_error("Intercepted fetch was stopped"_string));