Skip to content

Truly independent web browser

LibWeb: Add a fetch controller registry

Luke Wilde22a76767c556
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 97eb439eb1f..0d6b5d9e398 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -369,6 +369,7 @@ set(SOURCES
Fetch/HeadersIterator.cpp
Fetch/Infrastructure/FetchAlgorithms.cpp
Fetch/Infrastructure/FetchController.cpp
+ Fetch/Infrastructure/FetchControllerRegistry.cpp
Fetch/Infrastructure/FetchParams.cpp
Fetch/Infrastructure/FetchRecord.cpp
Fetch/Infrastructure/FetchTimingInfo.cpp
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index d4314a8c13b..0be1ded8615 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -58,6 +58,7 @@
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/PreloadEntry.h>
+#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
@@ -323,8 +324,10 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
// process early hints response is processEarlyHintsResponse, process response is processResponse, process
// response consume body is processResponseConsumeBody, process response end-of-body is processResponseEndOfBody,
// task destination is taskDestination, and cross-origin isolated capability is crossOriginIsolatedCapability.
- auto fetch_params = Infrastructure::FetchParams::create(request, algorithms, timing_info, current_loader_config());
- fetch_params->controller()->set_task_destination(task_destination);
+ auto& controller_registry = HTML::relevant_agent(realm.global_object()).fetch_controller_registry;
+ auto controller = Infrastructure::FetchController::create(controller_registry);
+ auto fetch_params = Infrastructure::FetchParams::create(controller, request, algorithms, timing_info, current_loader_config());
+ controller->set_task_destination(task_destination);
fetch_params->set_cross_origin_isolated_capability(cross_origin_isolated_capability);
fetch_params->set_has_response_body_transfer_lease(create_response_body_transfer_lease == CreateResponseBodyTransferLease::Yes);
@@ -465,7 +468,7 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
// 14. If request is a subresource request, then:
if (request.is_subresource_request()) {
// 1. Let record be a new fetch record whose request is request and controller is fetchParams’s controller.
- auto record = Infrastructure::FetchRecord::create(request, fetch_params->controller());
+ auto record = Infrastructure::FetchRecord::create(request, controller);
// 2. Append record to request’s client’s fetch group’s fetch records.
// AD-HOC: A client another process hosts has its fetch group there.
@@ -490,7 +493,7 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
(void)main_fetch(realm, fetch_params);
// 16. Return fetchParams’s controller.
- return fetch_params->controller();
+ return controller;
}
// https://fetch.spec.whatwg.org/#populate-request-from-client
@@ -3101,13 +3104,16 @@ GC::Ref<PendingResponse> cors_preflight_fetch(JS::Realm& realm, Infrastructure::
// 6. Let response be the result of running HTTP-network-or-cache fetch given a new fetch params whose request is preflight.
// FIXME: The spec doesn't say anything about timing_info here, but FetchParams requires a non-null FetchTimingInfo object.
auto timing_info = Infrastructure::FetchTimingInfo::create();
- auto preflight_fetch_params = Infrastructure::FetchParams::create(preflight, Infrastructure::FetchAlgorithms::create(Infrastructure::FetchAlgorithms::Input { Engine::BodyIntent::DrainAndDiscard }), timing_info, fetch_params.loader_config_snapshot());
+ auto& controller_registry = HTML::relevant_agent(realm.global_object()).fetch_controller_registry;
+ auto preflight_controller = Infrastructure::FetchController::create(controller_registry);
+ auto preflight_algorithms = Infrastructure::FetchAlgorithms::create(Infrastructure::FetchAlgorithms::Input { Engine::BodyIntent::DrainAndDiscard });
+ auto preflight_fetch_params = Infrastructure::FetchParams::create(preflight_controller, preflight, preflight_algorithms, timing_info, fetch_params.loader_config_snapshot());
auto returned_pending_response = PendingResponse::create(fetch_params);
auto preflight_response = http_network_or_cache_fetch(realm, preflight_fetch_params);
- preflight_response->when_loaded([request = fetch_params.request(), returned_pending_response, preflight_controller = preflight_fetch_params->controller()](NonnullRefPtr<Infrastructure::Response> response) {
+ preflight_response->when_loaded([request = fetch_params.request(), returned_pending_response, preflight_controller](NonnullRefPtr<Infrastructure::Response> response) {
dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'CORS-preflight fetch' preflight_response load callback");
if (WebDriver::BiDi::g_network_events_enabled) {
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
index 50d88c35524..65d76e09105 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
@@ -23,18 +23,15 @@ namespace Web::Fetch::Infrastructure {
GC_DEFINE_ALLOCATOR(FetchController);
-FetchController::FetchController() = default;
-
-void FetchController::finalize()
+FetchController::FetchController(FetchControllerRegistry& registry)
+ : m_registry(registry)
+ , m_token(registry.add({}, *this))
{
- // A discarded response can leave an ongoing controller without a stop or end-of-body task.
- end_rendering_pending_operation(this);
- Base::finalize();
}
-GC::Ref<FetchController> FetchController::create()
+GC::Ref<FetchController> FetchController::create(FetchControllerRegistry& registry)
{
- return GC::Heap::the().allocate<FetchController>();
+ return GC::Heap::the().allocate<FetchController>(registry);
}
void FetchController::visit_edges(JS::Cell::Visitor& visitor)
@@ -52,6 +49,21 @@ void FetchController::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_pending_responses);
}
+void FetchController::finalize()
+{
+ // A discarded response can leave an ongoing controller without a stop or end-of-body task.
+ end_rendering_pending_operation(this);
+ Base::finalize();
+ if (m_registry)
+ m_registry->remove({}, m_token);
+}
+
+void FetchController::set_algorithms(Badge<FetchParams>, GC::Ref<FetchAlgorithms const> algorithms)
+{
+ VERIFY(!m_algorithms);
+ m_algorithms = algorithms;
+}
+
GC::Ref<FetchAlgorithms const> FetchController::algorithms() const
{
VERIFY(m_algorithms);
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h
index 9395a6488c4..9cc62652e9f 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h
@@ -17,6 +17,7 @@
#include <LibWeb/Export.h>
#include <LibWeb/Fetch/Engine/FetchBodyDeliveryGate.h>
#include <LibWeb/Fetch/Engine/FetchByteChannel.h>
+#include <LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h>
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
#include <LibWeb/Fetch/Infrastructure/Task.h>
#include <LibWeb/Forward.h>
@@ -44,7 +45,9 @@ public:
Stopped,
};
- [[nodiscard]] static GC::Ref<FetchController> create();
+ [[nodiscard]] static GC::Ref<FetchController> create(FetchControllerRegistry&);
+
+ [[nodiscard]] FetchControllerToken token() const { return m_token; }
void set_full_timing_info(NonnullRefPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = move(full_timing_info); }
void set_report_timing_steps(Function<void(JS::Object&)> report_timing_steps);
@@ -68,7 +71,7 @@ public:
void set_fetch_params(Badge<FetchParams>, GC::Ref<FetchParams> fetch_params) { m_fetch_params = fetch_params; }
[[nodiscard]] GC::Ptr<FetchParams> fetch_params() const { return m_fetch_params; }
- void set_algorithms(Badge<FetchParams>, GC::Ref<FetchAlgorithms const> algorithms) { m_algorithms = algorithms; }
+ void set_algorithms(Badge<FetchParams>, GC::Ref<FetchAlgorithms const>);
[[nodiscard]] GC::Ref<FetchAlgorithms const> algorithms() const;
[[nodiscard]] TaskDestination const& task_destination() const { return m_task_destination; }
@@ -103,11 +106,14 @@ public:
void remove_pending_response(Badge<Fetching::PendingResponse>, GC::Ref<Fetching::PendingResponse>);
private:
- FetchController();
+ explicit FetchController(FetchControllerRegistry&);
virtual void visit_edges(JS::Cell::Visitor&) override;
virtual void finalize() override;
+ WeakPtr<FetchControllerRegistry> m_registry;
+ FetchControllerToken m_token;
+
// https://fetch.spec.whatwg.org/#fetch-controller-state
// state (default "ongoing")
// "ongoing", "terminated", or "aborted"
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerRegistry.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerRegistry.cpp
new file mode 100644
index 00000000000..5aa01cf7bdc
--- /dev/null
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerRegistry.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Atomic.h>
+#include <LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h>
+
+namespace Web::Fetch::Infrastructure {
+
+static Atomic<u64> s_next_registry_id { 1 };
+static Atomic<u64> s_next_serial { 1 };
+
+FetchControllerRegistry::FetchControllerRegistry()
+ : m_id(s_next_registry_id.fetch_add(1))
+{
+}
+
+FetchControllerToken FetchControllerRegistry::add(Badge<FetchController>, FetchController& controller)
+{
+ FetchControllerToken token { m_id, s_next_serial.fetch_add(1) };
+ m_controllers.set(token.m_serial, &controller);
+ return token;
+}
+
+void FetchControllerRegistry::remove(Badge<FetchController>, FetchControllerToken token)
+{
+ VERIFY(token.m_registry_id == m_id);
+ m_controllers.remove(token.m_serial);
+}
+
+GC::Ptr<FetchController> FetchControllerRegistry::find(FetchControllerToken token) const
+{
+ VERIFY(token.m_registry_id == m_id);
+ return m_controllers.get(token.m_serial).value_or(nullptr);
+}
+
+}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h
new file mode 100644
index 00000000000..4b410d343f0
--- /dev/null
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Badge.h>
+#include <AK/HashMap.h>
+#include <AK/Noncopyable.h>
+#include <AK/Weakable.h>
+#include <LibGC/Ptr.h>
+#include <LibWeb/Export.h>
+#include <LibWeb/Fetch/Infrastructure/FetchControllerToken.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::Fetch::Infrastructure {
+
+// The fetch controllers of one agent, by token. It keeps none of them alive.
+class WEB_API FetchControllerRegistry : public Weakable<FetchControllerRegistry> {
+ AK_MAKE_NONCOPYABLE(FetchControllerRegistry);
+ AK_MAKE_NONMOVABLE(FetchControllerRegistry);
+
+public:
+ FetchControllerRegistry();
+
+ [[nodiscard]] FetchControllerToken add(Badge<FetchController>, FetchController&);
+ void remove(Badge<FetchController>, FetchControllerToken);
+
+ [[nodiscard]] GC::Ptr<FetchController> find(FetchControllerToken) const;
+
+private:
+ u64 m_id { 0 };
+ HashMap<u64, GC::RawPtr<FetchController>> m_controllers;
+};
+
+}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerToken.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerToken.h
new file mode 100644
index 00000000000..6892da3bb8a
--- /dev/null
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchControllerToken.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Types.h>
+
+namespace Web::Fetch::Infrastructure {
+
+// Names one fetch controller, and only within the registry that issued it.
+class FetchControllerToken {
+public:
+ bool operator==(FetchControllerToken const&) const = default;
+
+private:
+ friend class FetchControllerRegistry;
+
+ FetchControllerToken(u64 registry_id, u64 serial)
+ : m_registry_id(registry_id)
+ , m_serial(serial)
+ {
+ }
+
+ u64 m_registry_id { 0 };
+ u64 m_serial { 0 };
+};
+
+}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
index 3352a92b17e..95905cd2e77 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
@@ -37,9 +37,8 @@ FetchParams::FetchParams(FetchParams const& params)
{
}
-GC::Ref<FetchParams> FetchParams::create(NonnullRefPtr<Request> request, GC::Ref<FetchAlgorithms const> algorithms, NonnullRefPtr<FetchTimingInfo> timing_info, NonnullRefPtr<LoaderConfigSnapshot const> loader_config)
+GC::Ref<FetchParams> FetchParams::create(FetchController& controller, NonnullRefPtr<Request> request, GC::Ref<FetchAlgorithms const> algorithms, NonnullRefPtr<FetchTimingInfo> timing_info, NonnullRefPtr<LoaderConfigSnapshot const> loader_config)
{
- auto controller = Infrastructure::FetchController::create();
return GC::Heap::the().allocate<FetchParams>(move(request), algorithms, controller, move(timing_info), move(loader_config));
}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h
index 8ba0adc83cd..1d8dc29c2c4 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h
@@ -30,7 +30,7 @@ public:
struct PreloadedResponseCandidatePendingTag { };
using PreloadedResponseCandidate = Variant<Empty, PreloadedResponseCandidatePendingTag, NonnullRefPtr<Response>>;
- [[nodiscard]] static GC::Ref<FetchParams> create(NonnullRefPtr<Request>, GC::Ref<FetchAlgorithms const>, NonnullRefPtr<FetchTimingInfo>, NonnullRefPtr<LoaderConfigSnapshot const>);
+ [[nodiscard]] static GC::Ref<FetchParams> create(FetchController&, NonnullRefPtr<Request>, GC::Ref<FetchAlgorithms const>, NonnullRefPtr<FetchTimingInfo>, NonnullRefPtr<LoaderConfigSnapshot const>);
[[nodiscard]] static GC::Ref<FetchParams> copy(FetchParams const&);
[[nodiscard]] NonnullRefPtr<Request> request() const { return m_request; }
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 8fd9abf19e4..51b8a6fe5df 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -577,6 +577,7 @@ namespace Web::Fetch::Infrastructure {
class Body;
class FetchAlgorithms;
class FetchController;
+class FetchControllerRegistry;
class FetchParams;
class FetchRecord;
class FetchTimingInfo;
diff --git a/Libraries/LibWeb/HTML/Scripting/Agent.h b/Libraries/LibWeb/HTML/Scripting/Agent.h
index 6533f654cab..2a932333edc 100644
--- a/Libraries/LibWeb/HTML/Scripting/Agent.h
+++ b/Libraries/LibWeb/HTML/Scripting/Agent.h
@@ -10,6 +10,7 @@
#include <LibGC/Root.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Agent.h>
+#include <LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h>
#include <LibWeb/Forward.h>
namespace JS {
@@ -33,6 +34,8 @@ struct WEB_API Agent : public JS::Agent {
// Rooted by Bindings::create_global_object_wrapper() until Bindings::cache_global_object_wrapper() runs.
Vector<GC::Root<JS::HostObject>> global_object_wrappers_of_realms_being_set_up;
+ Fetch::Infrastructure::FetchControllerRegistry fetch_controller_registry;
+
virtual void spin_event_loop_until(GC::Root<GC::Function<bool()>> goal_condition) override;
protected:
diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
index a8c87667c67..34c35f9b92f 100644
--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
@@ -48,6 +48,7 @@
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Parser/HTMLEncodingDetection.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
+#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
@@ -90,7 +91,9 @@ GC::Ref<XMLHttpRequest> XMLHttpRequest::create(GC::Ref<DOM::EventTarget> relevan
auto upload_object = GC::Heap::the().allocate<XMLHttpRequestUpload>();
auto author_request_headers = HTTP::HeaderList::create();
auto response = Fetch::Infrastructure::ResponseView::network_error("Not sent yet"_string);
- auto fetch_controller = Fetch::Infrastructure::FetchController::create();
+ auto& global_scope = HTML::relevant_window_or_worker_global_scope(*relevant_global_object);
+ auto& relevant_agent = HTML::relevant_agent(HTML::relevant_global_object(global_scope));
+ auto fetch_controller = Fetch::Infrastructure::FetchController::create(relevant_agent.fetch_controller_registry);
auto xhr = GC::Heap::the().allocate<XMLHttpRequest>(relevant_global_object, *upload_object, move(author_request_headers), *response, *fetch_controller);
live_xml_http_requests().append(xhr);
prune_live_xml_http_requests();
diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt
index 99ed60fcd3b..f2b2089c189 100644
--- a/Tests/LibWeb/CMakeLists.txt
+++ b/Tests/LibWeb/CMakeLists.txt
@@ -30,6 +30,7 @@ set(TEST_SOURCES
TestFetchBodyDeliveryGate.cpp
TestFetchBodyStream.cpp
TestFetchByteChannel.cpp
+ TestFetchControllerRegistry.cpp
TestFetchEngineMessages.cpp
TestFetchExecutor.cpp
TestFetchResponse.cpp
@@ -97,6 +98,7 @@ target_link_libraries(TestFetchAlgorithms PRIVATE LibCore LibGC LibJS LibURL)
target_link_libraries(TestFetchBodyDeliveryGate PRIVATE LibCore LibThreading)
target_link_libraries(TestFetchBodyStream PRIVATE LibCore LibGC LibJS)
target_link_libraries(TestFetchByteChannel PRIVATE LibThreading)
+target_link_libraries(TestFetchControllerRegistry PRIVATE LibGC)
target_link_libraries(TestFetchResponse PRIVATE LibGC LibHTTP LibJS LibRequests LibURL)
target_link_libraries(TestFetchURL PRIVATE LibURL)
target_link_libraries(TestImageData PRIVATE LibGC LibJS)
diff --git a/Tests/LibWeb/TestFetchBodyStream.cpp b/Tests/LibWeb/TestFetchBodyStream.cpp
index 1878455dda0..826459f52c8 100644
--- a/Tests/LibWeb/TestFetchBodyStream.cpp
+++ b/Tests/LibWeb/TestFetchBodyStream.cpp
@@ -17,6 +17,7 @@
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchParams.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
+#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/Loader/LoaderConfig.h>
@@ -69,12 +70,12 @@ private:
GC_DEFINE_ALLOCATOR(RecordingReadRequest);
-GC::Ref<FetchParams> create_sync_xhr_fetch_params()
+GC::Ref<FetchParams> create_sync_xhr_fetch_params(Web::Fetch::Infrastructure::FetchController& controller)
{
auto algorithms = FetchAlgorithms::create(FetchAlgorithms::Input { Web::Fetch::Engine::BodyIntent::SyncAccumulate });
auto request = Web::Fetch::Infrastructure::Request::create();
auto timing_info = Web::Fetch::Infrastructure::FetchTimingInfo::create();
- return FetchParams::create(move(request), algorithms, move(timing_info), Web::current_loader_config());
+ return FetchParams::create(controller, move(request), algorithms, move(timing_info), Web::current_loader_config());
}
struct BodyStreamHarness {
@@ -228,7 +229,8 @@ TEST_CASE(bytes_reach_a_stream_while_the_event_loop_is_paused)
Core::EventLoop event_loop;
install_font_plugin();
BodyStreamHarness harness;
- harness.fetch_params = create_sync_xhr_fetch_params();
+ auto& controller_registry = static_cast<Web::HTML::Agent&>(*harness.realm->vm().agent()).fetch_controller_registry;
+ harness.fetch_params = create_sync_xhr_fetch_params(Web::Fetch::Infrastructure::FetchController::create(controller_registry));
auto pause = Web::HTML::main_thread_event_loop().pause(Web::HTML::EventLoop::UpdateTheRendering::No);
harness.create_stream();
@@ -475,7 +477,8 @@ TEST_CASE(network_completion_runs_only_the_steps_awaited_for_its_body)
install_font_plugin();
BodyStreamHarness harness;
auto& heap = harness.realm->heap();
- auto controller = Web::Fetch::Infrastructure::FetchController::create();
+ auto& controller_registry = static_cast<Web::HTML::Agent&>(*harness.realm->vm().agent()).fetch_controller_registry;
+ auto controller = Web::Fetch::Infrastructure::FetchController::create(controller_registry);
auto earlier = FetchByteChannel::create({ .low = 16, .high = 64 }, adopt_ref(*new IgnoringProducerSink));
auto later = FetchByteChannel::create({ .low = 16, .high = 64 }, adopt_ref(*new IgnoringProducerSink));
diff --git a/Tests/LibWeb/TestFetchControllerRegistry.cpp b/Tests/LibWeb/TestFetchControllerRegistry.cpp
new file mode 100644
index 00000000000..b4e0075a023
--- /dev/null
+++ b/Tests/LibWeb/TestFetchControllerRegistry.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/NeverDestroyed.h>
+#include <LibGC/Heap.h>
+#include <LibGC/Root.h>
+#include <LibTest/TestCase.h>
+#include <LibWeb/Fetch/Infrastructure/FetchController.h>
+#include <LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h>
+
+using Web::Fetch::Infrastructure::FetchController;
+using Web::Fetch::Infrastructure::FetchControllerRegistry;
+using Web::Fetch::Infrastructure::FetchControllerToken;
+
+static GC::Heap& test_heap()
+{
+ static AK::NeverDestroyed<GC::Heap> heap([](auto&) { });
+ return *heap;
+}
+
+static NEVER_INLINE FetchControllerToken create_unreferenced_controller(FetchControllerRegistry& registry)
+{
+ return FetchController::create(registry)->token();
+}
+
+TEST_SETUP
+{
+ GC::Heap::set_default_heap_for_testing(test_heap());
+}
+
+TEST_CASE(each_registry_finds_only_its_own_controllers_by_unique_tokens)
+{
+ FetchControllerRegistry registry;
+ FetchControllerRegistry other_registry;
+ auto first_controller = GC::make_root(FetchController::create(registry));
+ auto second_controller = GC::make_root(FetchController::create(registry));
+ auto other_controller = GC::make_root(FetchController::create(other_registry));
+
+ EXPECT(first_controller->token() != second_controller->token());
+ EXPECT(first_controller->token() != other_controller->token());
+ EXPECT(second_controller->token() != other_controller->token());
+
+ EXPECT_EQ(registry.find(first_controller->token()).ptr(), first_controller.ptr());
+ EXPECT_EQ(registry.find(second_controller->token()).ptr(), second_controller.ptr());
+ EXPECT_EQ(other_registry.find(other_controller->token()).ptr(), other_controller.ptr());
+}
+
+TEST_CASE(a_collected_controller_is_not_found)
+{
+ FetchControllerRegistry registry;
+ auto token = create_unreferenced_controller(registry);
+
+ test_heap().collect_garbage(GC::Heap::CollectionType::CollectEverything);
+
+ EXPECT(!registry.find(token));
+}
+
+TEST_CASE(a_controller_can_be_collected_after_its_registry_is_gone)
+{
+ {
+ FetchControllerRegistry registry;
+ (void)create_unreferenced_controller(registry);
+ }
+
+ test_heap().collect_garbage(GC::Heap::CollectionType::CollectEverything);
+}
+
+TEST_CASE(a_token_is_rejected_by_a_registry_that_did_not_issue_it)
+{
+ FetchControllerRegistry registry;
+ FetchControllerRegistry other_registry;
+ auto controller = GC::make_root(FetchController::create(registry));
+
+ EXPECT_DEATH("Finding a token in another registry", (void)other_registry.find(controller->token()));
+}
diff --git a/Tests/LibWeb/TestFetchResponse.cpp b/Tests/LibWeb/TestFetchResponse.cpp
index 49ceb8580f7..3ce55a36a5f 100644
--- a/Tests/LibWeb/TestFetchResponse.cpp
+++ b/Tests/LibWeb/TestFetchResponse.cpp
@@ -69,7 +69,9 @@ TEST_CASE(http_redirect_fetch_releases_intermediate_response_transfer_lease)
request->set_redirect_count(20);
auto algorithms = Web::Fetch::Infrastructure::FetchAlgorithms::create(Web::Fetch::Infrastructure::FetchAlgorithms::Input { Web::Fetch::Engine::BodyIntent::DeliverStreamToConsumerAgent });
- auto fetch_params = Web::Fetch::Infrastructure::FetchParams::create(request, algorithms, Web::Fetch::Infrastructure::FetchTimingInfo::create(), Web::current_loader_config());
+ Web::Fetch::Infrastructure::FetchControllerRegistry controller_registry;
+ auto controller = Web::Fetch::Infrastructure::FetchController::create(controller_registry);
+ auto fetch_params = Web::Fetch::Infrastructure::FetchParams::create(controller, request, algorithms, Web::Fetch::Infrastructure::FetchTimingInfo::create(), Web::current_loader_config());
auto response = TransferTrackingResponse::create();
response->set_status(302);
response->set_header_list(HTTP::HeaderList::create({ { "Location"sv, "/redirected"sv } }));