Truly independent web browser
LibWeb: Give WebContent's agents a fetch executor
Luke Wilde877fc945c146
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cppindex fa706d8579e..660dff4d857 100644--- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp+++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp@@ -789,7 +789,8 @@ void shut_down_agent()// NB: A window agent lives as long as its process does.VERIFY(&vm != main_thread_vm_ptr().ptr());- static_cast<HTML::Agent*>(vm.agent())->detach_script_abort_handle();+ auto& agent = static_cast<HTML::Agent&>(*vm.agent());+ agent.detach_script_abort_handle();// What the agent keeps outside its heap refers to cells in it, so that goes while the heap is alive.Platform::discard_thread_pool_completions();@@ -804,8 +805,10 @@ void shut_down_agent()ServiceWorker::Registration::forget_registrations_of_this_agent();// An event that is still queued can hold roots as well, and a stopped request only lets go of its roots in one. As- // the task queues discard everything by now, no script runs from these. What a deferred invocation of LibWeb is for- // has to go undone, as the worker that it was for has run its closing steps.+ // the task queues discard everything by now, no script runs from these. What a deferred invocation of LibWeb or a+ // command to the agent's fetch executor is for has to go undone, as the worker that it was for has run its closing+ // steps.+ agent.fetch_executor->shutdown();Platform::EventLoopPlugin::set_discard_deferred_invocations_on_this_thread(true);auto& event_queue = Core::ThreadEventQueue::current();for (;;) {diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex a1d56ce3880..7d2181c60c1 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -306,6 +306,7 @@ set(SOURCESFetch/Fetching/Checks.cppFetch/Fetching/ClientContextSnapshots.cppFetch/Fetching/NetworkBodyPump.cpp+ Fetch/Fetching/FetchEngine.cppFetch/Fetching/Fetching.cppFetch/Fetching/PendingResponse.cppFetch/Fetching/RefCountedFlag.cppdiff --git a/Libraries/LibWeb/Fetch/Fetching/FetchEngine.cpp b/Libraries/LibWeb/Fetch/Fetching/FetchEngine.cppnew file mode 100644index 00000000000..782de109798--- /dev/null+++ b/Libraries/LibWeb/Fetch/Fetching/FetchEngine.cpp@@ -0,0 +1,24 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCore/EventLoop.h>+#include <LibWeb/Fetch/Engine/InlineFetchExecutor.h>+#include <LibWeb/Fetch/Fetching/FetchEngine.h>++namespace Web::Fetch::Fetching {++void FetchEngine::handle_command(Engine::EngineCommand)+{+ VERIFY_NOT_REACHED();+}++NonnullRefPtr<Engine::FetchExecutor> create_inline_fetch_executor()+{+ Engine::FetchExecutorServices services { .event_loop = Core::EventLoop::current_weak() };+ return Engine::InlineFetchExecutor::create(move(services), make<FetchEngine>());+}++}diff --git a/Libraries/LibWeb/Fetch/Fetching/FetchEngine.h b/Libraries/LibWeb/Fetch/Fetching/FetchEngine.hnew file mode 100644index 00000000000..eb4ea39ea13--- /dev/null+++ b/Libraries/LibWeb/Fetch/Fetching/FetchEngine.h@@ -0,0 +1,25 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/NonnullRefPtr.h>+#include <LibWeb/Export.h>+#include <LibWeb/Fetch/Engine/EngineCommandSink.h>+#include <LibWeb/Fetch/Engine/FetchExecutor.h>++namespace Web::Fetch::Fetching {++// Handles the commands posted for the fetches of the agent on whose thread it runs.+class FetchEngine final : public Engine::EngineCommandSink {+public:+ virtual void handle_command(Engine::EngineCommand) override;+};++// Creates an executor that runs a fetch engine on the current thread's event loop.+WEB_API NonnullRefPtr<Engine::FetchExecutor> create_inline_fetch_executor();++}diff --git a/Libraries/LibWeb/HTML/Scripting/Agent.h b/Libraries/LibWeb/HTML/Scripting/Agent.hindex 515d2099970..380d3e4ef3c 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/Engine/FetchExecutor.h>#include <LibWeb/Fetch/Infrastructure/FetchControllerRegistry.h>#include <LibWeb/Forward.h>#include <LibWeb/HTML/EventLoop/Task.h>@@ -33,6 +34,9 @@ struct WEB_API Agent : public JS::Agent {// And the event loop of a worklet agent is known as a worklet event loop.GC::Root<HTML::EventLoop> event_loop;+ // Set by whatever runs the agent's thread, before the agent fetches anything.+ RefPtr<Fetch::Engine::FetchExecutor> fetch_executor;+// 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;diff --git a/Services/WebContent/Worker/WorkerThread.cpp b/Services/WebContent/Worker/WorkerThread.cppindex 4926ed9c446..67865d45915 100644--- a/Services/WebContent/Worker/WorkerThread.cpp+++ b/Services/WebContent/Worker/WorkerThread.cpp@@ -15,6 +15,8 @@#include <LibJS/Runtime/VM.h>#include <LibThreading/Thread.h>#include <LibWeb/Bindings/MainThreadVM.h>+#include <LibWeb/Fetch/Fetching/FetchEngine.h>+#include <LibWeb/HTML/Scripting/Agent.h>#include <LibWebCommon/HTML/WorkerAgentTypes.h>#include <WebContent/Worker/ConnectionFromClient.h>#include <WebContent/Worker/DedicatedWorkerThreadLink.h>@@ -60,6 +62,7 @@ static intptr_t run_worker_thread(IPC::TransportHandle const& worker_handle, WebCore::EventLoop event_loop;Web::Bindings::initialize_agent(agent_type);+ Web::HTML::surrounding_agent().fetch_executor = Web::Fetch::Fetching::create_inline_fetch_executor();auto event_loop_reference = Core::EventLoop::current_weak();{diff --git a/Services/WebContent/main.cpp b/Services/WebContent/main.cppindex ea129945bdc..74ee5530358 100644--- a/Services/WebContent/main.cpp+++ b/Services/WebContent/main.cpp@@ -30,8 +30,10 @@#include <LibUnicode/TimeZone.h>#include <LibWeb/Bindings/MainThreadVM.h>#include <LibWeb/DOM/Document.h>+#include <LibWeb/Fetch/Fetching/FetchEngine.h>#include <LibWeb/Fetch/Fetching/Fetching.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>+#include <LibWeb/HTML/Scripting/Agent.h>#include <LibWeb/HTML/Window.h>#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>#include <LibWeb/Internals/Internals.h>@@ -281,6 +283,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)Web::Bindings::set_collect_garbage_on_every_allocation_in_new_agents(collect_garbage_on_every_allocation);Web::Bindings::initialize_agent(Web::HTML::AgentType::SimilarOriginWindow);+ Web::HTML::surrounding_agent().fetch_executor = Web::Fetch::Fetching::create_inline_fetch_executor();if (stats_log)Web::Bindings::main_thread_vm().heap().enable_stats();diff --git a/Tests/LibWeb/TestAgentThreads.cpp b/Tests/LibWeb/TestAgentThreads.cppindex 2bc20f86879..9d036f414c5 100644--- a/Tests/LibWeb/TestAgentThreads.cpp+++ b/Tests/LibWeb/TestAgentThreads.cpp@@ -21,6 +21,7 @@#include <LibWeb/Bindings/MainThreadVM.h>#include <LibWeb/Bindings/PlatformObject.h>#include <LibWeb/CSS/PropertyID.h>+#include <LibWeb/Fetch/Fetching/FetchEngine.h>#include <LibWeb/FileAPI/BlobURLStore.h>#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>@@ -394,6 +395,7 @@ public:TestAgent(){Web::Bindings::initialize_agent(Web::HTML::AgentType::DedicatedWorker);+ Web::HTML::surrounding_agent().fetch_executor = Web::Fetch::Fetching::create_inline_fetch_executor();Web::HTML::current_event_loop().set_on_destroyed([] {Core::EventLoop::current().quit(0);});diff --git a/UI/Android/src/main/cpp/WebContentService.cpp b/UI/Android/src/main/cpp/WebContentService.cppindex 3eda68e0604..cb6a9ef6cbf 100644--- a/UI/Android/src/main/cpp/WebContentService.cpp+++ b/UI/Android/src/main/cpp/WebContentService.cpp@@ -16,6 +16,8 @@#include <LibMedia/Audio/Loader.h>#include <LibRequests/RequestClient.h>#include <LibWeb/Bindings/MainThreadVM.h>+#include <LibWeb/Fetch/Fetching/FetchEngine.h>+#include <LibWeb/HTML/Scripting/Agent.h>#include <LibWeb/HTML/Window.h>#include <LibWeb/Loader/GeneratedPagesLoader.h>#include <LibWeb/Loader/ResourceLoader.h>@@ -47,6 +49,7 @@ ErrorOr<int> service_main(int ipc_socket)Web::Platform::ImageCodecPlugin::install(*new Web::Platform::RemoteImageCodecPlugin(bind_image_decoder_service));Web::Bindings::initialize_agent(Web::HTML::AgentType::SimilarOriginWindow);+ Web::HTML::surrounding_agent().fetch_executor = Web::Fetch::Fetching::create_inline_fetch_executor();auto request_server_client = TRY(bind_request_server_service());Web::ResourceLoader::initialize(Web::Bindings::main_thread_vm().heap());