Skip to content

Truly independent web browser

LibWeb+LibWebCommon: Move the loader configuration out of ResourceLoader

Luke Wilde9c49a694c27e
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 5b303b7dfb1..b7c4c9a8a63 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -736,6 +736,7 @@ set(SOURCES
Loader/ContentBlocker.cpp
Loader/FileRequest.cpp
Loader/GeneratedPagesLoader.cpp
+ Loader/LoaderConfig.cpp
Loader/ResourceLoader.cpp
Loader/SourceHighlighter.cpp
MathML/AttributeNames.cpp
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 5fc55cf8d04..0278c662d16 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -63,6 +63,7 @@
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Infra/SerializedURL.h>
#include <LibWeb/Loader/LoadRequest.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/MixedContent/AbstractOperations.h>
#include <LibWeb/Page/Page.h>
@@ -305,7 +306,7 @@ 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);
+ auto fetch_params = Infrastructure::FetchParams::create(request, algorithms, timing_info, current_loader_config());
fetch_params->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);
@@ -432,10 +433,7 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
// (`Accept-Language, an appropriate header value) to request’s header list.
if (!request.header_list()->contains("Accept-Language"sv)) {
StringBuilder accept_language;
- if (ResourceLoader::is_initialized())
- accept_language.join(","sv, ResourceLoader::the().preferred_languages());
- else
- accept_language.append("en-US"sv);
+ accept_language.join(","sv, fetch_params->loader_config().preferred_languages);
auto header = HTTP::Header::isomorphic_encode("Accept-Language"sv, accept_language.string_view());
request.header_list()->append(move(header));
@@ -2119,10 +2117,15 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
// 14. FIXME: If httpRequest’s initiator is "prefetch", then set a structured field value
// given (`Sec-Purpose`, the token prefetch) in httpRequest’s header list.
- // 15. If httpRequest’s header list does not contain `User-Agent`, then user agents should append
- // (`User-Agent`, default `User-Agent` value) to httpRequest’s header list.
- if (!http_request->header_list()->contains("User-Agent"sv))
- http_request->header_list()->append({ "User-Agent"sv, Infrastructure::default_user_agent_value(http_request->current_url()) });
+ // 15. If httpRequest’s header list does not contain `User-Agent`, then user agents should:
+ if (!http_request->header_list()->contains("User-Agent"sv)) {
+ // 1. Let userAgent be httpRequest’s client’s environment default `User-Agent` value.
+ // FIXME: Honor WebDriver BiDi emulated User-Agent.
+ auto user_agent = Infrastructure::default_user_agent_value(fetch_params.loader_config(), http_request->current_url());
+
+ // 2. Append (`User-Agent`, userAgent) to httpRequest’s header list.
+ http_request->header_list()->append({ "User-Agent"sv, move(user_agent) });
+ }
// 16. If httpRequest’s cache mode is "default" and httpRequest’s header list contains `If-Modified-Since`,
// `If-None-Match`, `If-Unmodified-Since`, `If-Match`, or `If-Range`, then set httpRequest’s cache mode to
@@ -2184,7 +2187,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
// more details.
//
// https://w3c.github.io/gpc/#the-sec-gpc-header-field-for-http-requests
- if (ResourceLoader::is_initialized() && ResourceLoader::the().enable_global_privacy_control() && !http_request->header_list()->contains("Sec-GPC"sv))
+ if (fetch_params.loader_config().enable_global_privacy_control && !http_request->header_list()->contains("Sec-GPC"sv))
http_request->header_list()->append({ "Sec-GPC"sv, "1"sv });
append_user_agent_client_hints_for_request(*http_request);
@@ -2895,7 +2898,7 @@ 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);
+ 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 returned_pending_response = PendingResponse::create(fetch_params);
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
index 9741e2e5c39..666aafe77e8 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp
@@ -13,11 +13,12 @@ namespace Web::Fetch::Infrastructure {
GC_DEFINE_ALLOCATOR(FetchParams);
-FetchParams::FetchParams(NonnullRefPtr<Request> request, GC::Ref<FetchAlgorithms const> algorithms, GC::Ref<FetchController> controller, NonnullRefPtr<FetchTimingInfo> timing_info)
+FetchParams::FetchParams(NonnullRefPtr<Request> request, GC::Ref<FetchAlgorithms const> algorithms, GC::Ref<FetchController> controller, NonnullRefPtr<FetchTimingInfo> timing_info, NonnullRefPtr<LoaderConfigSnapshot const> loader_config)
: m_request(move(request))
, m_algorithms(algorithms)
, m_controller(controller)
, m_timing_info(move(timing_info))
+ , m_loader_config(move(loader_config))
{
m_controller->set_fetch_params({}, *this);
}
@@ -32,13 +33,14 @@ FetchParams::FetchParams(FetchParams const& params)
, m_preloaded_response_candidate(params.m_preloaded_response_candidate)
, m_has_response_body_transfer_lease(params.m_has_response_body_transfer_lease)
, m_blob_url_entry(params.m_blob_url_entry)
+ , m_loader_config(params.m_loader_config)
{
}
-GC::Ref<FetchParams> FetchParams::create(NonnullRefPtr<Request> request, GC::Ref<FetchAlgorithms const> algorithms, NonnullRefPtr<FetchTimingInfo> timing_info)
+GC::Ref<FetchParams> FetchParams::create(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));
+ return GC::Heap::the().allocate<FetchParams>(move(request), algorithms, controller, move(timing_info), move(loader_config));
}
GC::Ref<FetchParams> FetchParams::copy(FetchParams const& params)
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h
index 3a978a9166a..e5d0b4c27d9 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h
@@ -17,6 +17,7 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/Task.h>
#include <LibWebCommon/FileAPI/SerializedBlobURLEntry.h>
+#include <LibWebCommon/Loader/LoaderConfigSnapshot.h>
namespace Web::Fetch::Infrastructure {
@@ -29,13 +30,15 @@ public:
struct PreloadedResponseCandidatePendingTag { };
using PreloadedResponseCandidate = Variant<Empty, PreloadedResponseCandidatePendingTag, GC::Ref<Response>>;
- [[nodiscard]] static GC::Ref<FetchParams> create(NonnullRefPtr<Request>, GC::Ref<FetchAlgorithms const>, NonnullRefPtr<FetchTimingInfo>);
+ [[nodiscard]] static GC::Ref<FetchParams> create(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; }
void set_request(NonnullRefPtr<Request> request) { m_request = move(request); }
[[nodiscard]] GC::Ref<FetchController> controller() const { return m_controller; }
[[nodiscard]] NonnullRefPtr<FetchTimingInfo> timing_info() const { return m_timing_info; }
+ [[nodiscard]] LoaderConfig const& loader_config() const { return m_loader_config->config; }
+ [[nodiscard]] NonnullRefPtr<LoaderConfigSnapshot const> const& loader_config_snapshot() const { return m_loader_config; }
[[nodiscard]] GC::Ref<FetchAlgorithms const> algorithms() const { return m_algorithms; }
void set_algorithms(GC::Ref<FetchAlgorithms const> algorithms) { m_algorithms = algorithms; }
@@ -64,7 +67,7 @@ public:
void set_blob_url_entry(Optional<FileAPI::SerializedBlobURLEntry> blob_url_entry) { m_blob_url_entry = move(blob_url_entry); }
private:
- FetchParams(NonnullRefPtr<Request>, GC::Ref<FetchAlgorithms const>, GC::Ref<FetchController>, NonnullRefPtr<FetchTimingInfo>);
+ FetchParams(NonnullRefPtr<Request>, GC::Ref<FetchAlgorithms const>, GC::Ref<FetchController>, NonnullRefPtr<FetchTimingInfo>, NonnullRefPtr<LoaderConfigSnapshot const>);
FetchParams(FetchParams const&);
virtual void visit_edges(JS::Cell::Visitor&) override;
@@ -122,6 +125,9 @@ private:
// The blob URL entry of the request's URL, when that is a blob: URL, as it was when the fetch started.
Optional<FileAPI::SerializedBlobURLEntry> m_blob_url_entry;
+
+ // The loader configuration as it was when the fetch started.
+ NonnullRefPtr<LoaderConfigSnapshot const> m_loader_config;
};
}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
index 7e343c02fa7..b20d37998f0 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
@@ -4,16 +4,17 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ByteString.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
-#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWebCommon/Loader/LoaderConfigSnapshot.h>
namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#default-user-agent-value
-ByteString default_user_agent_value(URL::URL const& url)
+ByteString default_user_agent_value(LoaderConfig const& loader_config, URL::URL const& url)
{
// A default `User-Agent` value is an implementation-defined header value for the `User-Agent` header.
- return ResourceLoader::the().user_agent_for_url(url).to_byte_string();
+ return loader_config.user_agent_for_url(url).to_byte_string();
}
}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h
index 50e3bdc2272..4244e0a3e96 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h
@@ -10,9 +10,10 @@
#include <AK/Forward.h>
#include <LibURL/Forward.h>
#include <LibWebCommon/Fetch/Infrastructure/RedirectTaint.h>
+#include <LibWebCommon/Forward.h>
namespace Web::Fetch::Infrastructure {
-[[nodiscard]] ByteString default_user_agent_value(URL::URL const&);
+[[nodiscard]] ByteString default_user_agent_value(LoaderConfig const&, URL::URL const&);
}
diff --git a/Libraries/LibWeb/GPC/GlobalPrivacyControl.cpp b/Libraries/LibWeb/GPC/GlobalPrivacyControl.cpp
index a06f4fb1139..450dc5acca4 100644
--- a/Libraries/LibWeb/GPC/GlobalPrivacyControl.cpp
+++ b/Libraries/LibWeb/GPC/GlobalPrivacyControl.cpp
@@ -5,7 +5,7 @@
*/
#include <LibWeb/GPC/GlobalPrivacyControl.h>
-#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/Loader/LoaderConfig.h>
namespace Web::GlobalPrivacyControl {
@@ -15,7 +15,7 @@ GlobalPrivacyControlMixin::~GlobalPrivacyControlMixin() = default;
bool GlobalPrivacyControlMixin::global_privacy_control() const
{
// The value is false if no Sec-GPC header field would be sent; otherwise, the value is true.
- return ResourceLoader::the().enable_global_privacy_control();
+ return current_loader_config()->config.enable_global_privacy_control;
}
}
diff --git a/Libraries/LibWeb/HTML/NavigatorID.cpp b/Libraries/LibWeb/HTML/NavigatorID.cpp
index cd0686c0e3e..a44afd5c9c8 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.cpp
+++ b/Libraries/LibWeb/HTML/NavigatorID.cpp
@@ -9,6 +9,7 @@
#include <AK/StringBuilder.h>
#include <AK/Utf16StringBuilder.h>
#include <LibWeb/HTML/NavigatorID.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWebCommon/Loader/UserAgent.h>
@@ -19,7 +20,7 @@ Utf16String NavigatorIDMixin::app_version() const
{
// 1. Let userAgent be this's relevant settings object's environment default `User-Agent` value.
// FIXME: Store that on the settings object?
- auto user_agent = ResourceLoader::the().user_agent_for_url(navigator_id_url());
+ auto user_agent = current_loader_config()->config.user_agent_for_url(navigator_id_url());
// 2. If userAgent does not start with `Mozilla/5.0 (`, then return the empty string.
if (!user_agent.starts_with_bytes("Mozilla/5.0 ("sv))
@@ -72,7 +73,7 @@ Utf16String NavigatorIDMixin::oscpu() const
// the browser is executing, e.g. "Windows NT 10.0; Win64; x64", "Linux x86_64".
// The platform tokens of the User-Agent string say what it claims to run on: the parenthesized list without the
// "Macintosh" and "X11" markers and without the "rv:" version, so "Intel Mac OS X 10.15" or "Linux x86_64".
- auto user_agent = ResourceLoader::the().user_agent_for_url(navigator_id_url());
+ auto user_agent = current_loader_config()->config.user_agent_for_url(navigator_id_url());
auto open = user_agent.find_byte_offset('(');
auto close = user_agent.find_byte_offset(')');
if (!open.has_value() || !close.has_value() || *close <= *open)
@@ -117,7 +118,7 @@ Utf16FlyString NavigatorIDMixin::product_sub() const
Utf16String NavigatorIDMixin::user_agent() const
{
// Must return the default `User-Agent` value.
- auto user_agent = ResourceLoader::the().user_agent_for_url(navigator_id_url());
+ auto user_agent = current_loader_config()->config.user_agent_for_url(navigator_id_url());
return Utf16String::from_ascii_without_validation(user_agent.bytes());
}
diff --git a/Libraries/LibWeb/HTML/NavigatorLanguage.h b/Libraries/LibWeb/HTML/NavigatorLanguage.h
index 7b303efa565..0f99b0233bf 100644
--- a/Libraries/LibWeb/HTML/NavigatorLanguage.h
+++ b/Libraries/LibWeb/HTML/NavigatorLanguage.h
@@ -9,7 +9,7 @@
#include <AK/Utf16String.h>
#include <AK/Vector.h>
-#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/Loader/LoaderConfig.h>
namespace Web::HTML {
@@ -19,14 +19,15 @@ public:
// FIXME: Honor WebDriver BiDi emulated language.
Utf16String language() const
{
- return Utf16String::from_ascii_without_validation(ResourceLoader::the().preferred_languages()[0].bytes());
+ return Utf16String::from_ascii_without_validation(current_loader_config()->config.preferred_languages[0].bytes());
}
// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-languages
// FIXME: Honor WebDriver BiDi emulated language.
Vector<Utf16String> languages() const
{
- auto const& preferred_languages = ResourceLoader::the().preferred_languages();
+ auto loader_config = current_loader_config();
+ auto const& preferred_languages = loader_config->config.preferred_languages;
Vector<Utf16String> languages;
languages.ensure_capacity(preferred_languages.size());
for (auto const& preferred_language : preferred_languages)
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index 6e6b640c288..3f651948cc7 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -65,7 +65,7 @@
#include <LibWeb/IndexedDB/Internal/Algorithms.h>
#include <LibWeb/Infra/SerializedURL.h>
#include <LibWeb/LargestContentfulPaint/LargestContentfulPaint.h>
-#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/NavigationTiming/PerformanceNavigationTiming.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Page/RenderingStats.h>
@@ -1751,11 +1751,7 @@ bool WindowOrWorkerGlobalScopeMixin::expose_experimental_interface(EnvironmentSe
if (s_experimental_interfaces_exposed)
return true;
- // The first global of a page is set up before the loader exists, and holds nothing a rule could match anyway.
- if (!ResourceLoader::is_initialized())
- return false;
-
- return ResourceLoader::the().site_compatibility_exposes_experimental_interface(settings.creation_url, name);
+ return current_loader_config()->config.exposes_experimental_interface(settings.creation_url, name);
}
}
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 7a155ac6e88..6d8f92c8234 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -97,6 +97,7 @@
#include <LibWeb/Layout/TreeBuilderRustFFI.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Loader/ContentBlocker.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Page/Page.h>
@@ -1369,7 +1370,7 @@ WebIDL::ExceptionOr<void> Internals::set_site_compatibility_data(Utf16String con
if (data.is_error())
return window().principal_realm().vm().throw_completion<JS::InternalError>(Utf16String::formatted("Could not set site compatibility data: {}", data.error()));
- ResourceLoader::the().set_site_compatibility_data(data.release_value());
+ Web::set_site_compatibility_data(data.release_value());
return {};
}
diff --git a/Libraries/LibWeb/Loader/LoaderConfig.cpp b/Libraries/LibWeb/Loader/LoaderConfig.cpp
new file mode 100644
index 00000000000..fcb70078fea
--- /dev/null
+++ b/Libraries/LibWeb/Loader/LoaderConfig.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/RefPtr.h>
+#include <LibWeb/Loader/LoaderConfig.h>
+#include <LibWebCommon/Loader/UserAgent.h>
+
+namespace Web {
+
+static NonnullRefPtr<LoaderConfigSnapshot const> default_loader_config()
+{
+ LoaderConfig config {
+ .user_agent = MUST(String::from_utf8(default_user_agent)),
+ .site_compatibility = make_ref_counted<SharedSiteCompatibilityData>(SiteCompatibilityData {}),
+ .preferred_languages = default_preferred_languages(),
+ };
+ return make_ref_counted<LoaderConfigSnapshot>(move(config));
+}
+
+static RefPtr<LoaderConfigSnapshot const>& loader_config()
+{
+ static auto& loader_config = *new RefPtr<LoaderConfigSnapshot const>(default_loader_config());
+ return loader_config;
+}
+
+NonnullRefPtr<LoaderConfigSnapshot const> current_loader_config()
+{
+ return *loader_config();
+}
+
+void update_loader_config(Function<void(LoaderConfig&)> const& change)
+{
+ auto config = loader_config()->config;
+ change(config);
+ VERIFY(!config.preferred_languages.is_empty());
+ loader_config() = make_ref_counted<LoaderConfigSnapshot>(move(config));
+}
+
+void set_site_compatibility_data(SiteCompatibilityData data)
+{
+ update_loader_config([&](auto& config) {
+ config.site_compatibility = make_ref_counted<SharedSiteCompatibilityData>(move(data));
+ });
+}
+
+}
diff --git a/Libraries/LibWeb/Loader/LoaderConfig.h b/Libraries/LibWeb/Loader/LoaderConfig.h
new file mode 100644
index 00000000000..32edec12933
--- /dev/null
+++ b/Libraries/LibWeb/Loader/LoaderConfig.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Function.h>
+#include <AK/NonnullRefPtr.h>
+#include <LibWeb/Export.h>
+#include <LibWebCommon/Loader/LoaderConfigSnapshot.h>
+
+namespace Web {
+
+// The process's loader configuration, read and changed on the main thread. A snapshot already taken never changes.
+WEB_API NonnullRefPtr<LoaderConfigSnapshot const> current_loader_config();
+WEB_API void update_loader_config(Function<void(LoaderConfig&)> const& change);
+WEB_API void set_site_compatibility_data(SiteCompatibilityData);
+
+}
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index a88cecdcda3..b501416c1c2 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -75,9 +75,7 @@ ResourceLoader& ResourceLoader::the()
ResourceLoader::ResourceLoader(GC::Heap& heap)
: m_heap(heap)
- , m_user_agent(MUST(String::from_utf8(default_user_agent)))
, m_platform(MUST(String::from_utf8(default_platform)))
- , m_preferred_languages(default_preferred_languages())
{
}
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.h b/Libraries/LibWeb/Loader/ResourceLoader.h
index b3f89ccfb1a..e3bbe2fad5b 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.h
+++ b/Libraries/LibWeb/Loader/ResourceLoader.h
@@ -23,7 +23,6 @@
#include <LibURL/URL.h>
#include <LibWeb/Forward.h>
#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
-#include <LibWebCommon/Loader/SiteCompatibility.h>
namespace Web {
@@ -61,31 +60,14 @@ public:
static bool is_known_hsts_host(Page&, String const& host);
- String const& user_agent() const { return m_user_agent; }
- String user_agent_for_url(URL::URL const& url) const { return m_site_compatibility_data.user_agent_for_url(url, m_user_agent); }
- String user_agent_for_websocket_url(URL::URL const& url) const { return m_site_compatibility_data.user_agent_for_websocket_url(url, m_user_agent); }
- void set_user_agent(String user_agent) { m_user_agent = move(user_agent); }
- void set_site_compatibility_data(SiteCompatibilityData data) { m_site_compatibility_data = move(data); }
- bool site_compatibility_exposes_experimental_interface(URL::URL const& url, StringView name) const { return m_site_compatibility_data.exposes_experimental_interface(url, name); }
-
String const& platform() const { return m_platform; }
void set_platform(String platform) { m_platform = move(platform); }
- Vector<String> const& preferred_languages() const { return m_preferred_languages; }
- void set_preferred_languages(Vector<String> preferred_languages)
- {
- m_preferred_languages = move(preferred_languages);
- VERIFY(!m_preferred_languages.is_empty());
- }
-
// The engine the navigator claims to be. A process-wide setting, readable before the loader exists: the bindings
// consult it while they build a realm's global object.
static NavigatorCompatibilityMode navigator_compatibility_mode() { return s_navigator_compatibility_mode; }
static void set_navigator_compatibility_mode(NavigatorCompatibilityMode);
- bool enable_global_privacy_control() const { return m_enable_global_privacy_control; }
- void set_enable_global_privacy_control(bool enable) { m_enable_global_privacy_control = enable; }
-
private:
explicit ResourceLoader(GC::Heap&);
@@ -111,12 +93,8 @@ private:
HashMap<Utf16String, RefPtr<Requests::RequestClient>> m_site_request_clients;
HashTable<NonnullRefPtr<Requests::Request>> m_active_requests;
- String m_user_agent;
- SiteCompatibilityData m_site_compatibility_data;
String m_platform;
- Vector<String> m_preferred_languages = { "en"_string };
static NavigatorCompatibilityMode s_navigator_compatibility_mode;
- bool m_enable_global_privacy_control { false };
};
}
diff --git a/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp b/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp
index e7749315c36..7530b36334b 100644
--- a/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp
@@ -18,7 +18,7 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/Viewport.h>
-#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Painting/BoxViews.h>
@@ -102,7 +102,8 @@ static bool system_language_evaluates_to_true(Utf16View value)
// preferences is a case-insensitive prefix of one of the language tags given in the value of this parameter such
// that the first tag character following the prefix is "-".
auto language_tags = SVGStringList::parse(value, SVGStringList::Separator::Comma);
- for (auto const& preferred_language : ResourceLoader::the().preferred_languages()) {
+ auto loader_config = current_loader_config();
+ for (auto const& preferred_language : loader_config->config.preferred_languages) {
auto preferred_tag = Utf16String::from_utf8(preferred_language);
for (auto const& language_tag : language_tags) {
if (language_tag.equals_ignoring_ascii_case(preferred_tag))
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp
index dd5cd39bc1d..7e27a871022 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp
+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp
@@ -36,6 +36,7 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/MixedContent/AbstractOperations.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
@@ -229,7 +230,8 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
auto additional_headers = HTTP::HeaderList::create();
- additional_headers->append({ "User-Agent"sv, ResourceLoader::the().user_agent_for_websocket_url(connection_url).to_byte_string() });
+ auto user_agent = current_loader_config()->config.user_agent_for_websocket_url(connection_url);
+ additional_headers->append({ "User-Agent"sv, user_agent.to_byte_string() });
if (ResourceLoader::proxy_autoconfig_url().has_value()) {
dbgln("FIXME: Evaluate the PAC file to find the proxy for {}; failing the WebSocket connection", url_record);
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index f97e95349f2..24967e76030 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -45,6 +45,7 @@ set(SOURCES
Infra/Strings.cpp
Loader/ContentBlocking.cpp
Loader/DownloadFilename.cpp
+ Loader/LoaderConfigSnapshot.cpp
Loader/SiteCompatibility.cpp
MimeSniff/MimeType.cpp
MimeSniff/Resource.cpp
diff --git a/Libraries/LibWebCommon/Forward.h b/Libraries/LibWebCommon/Forward.h
index 2d25a0f5e4f..9e47cb3ae4d 100644
--- a/Libraries/LibWebCommon/Forward.h
+++ b/Libraries/LibWebCommon/Forward.h
@@ -21,6 +21,8 @@ enum class WheelDeltaPrecision : u8;
struct InitiatorSourceSnapshot;
struct KeyEvent;
+struct LoaderConfig;
+struct LoaderConfigSnapshot;
struct MouseEvent;
struct PinchEvent;
diff --git a/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.cpp b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.cpp
new file mode 100644
index 00000000000..71ca435e329
--- /dev/null
+++ b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.cpp
@@ -0,0 +1,13 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWebCommon/Loader/LoaderConfigSnapshot.h>
+
+namespace Web {
+
+SharedSiteCompatibilityData::~SharedSiteCompatibilityData() = default;
+
+}
diff --git a/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
new file mode 100644
index 00000000000..c00e7f9f031
--- /dev/null
+++ b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/AtomicRefCounted.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/String.h>
+#include <AK/Vector.h>
+#include <LibURL/URL.h>
+#include <LibWebCommon/Export.h>
+#include <LibWebCommon/Loader/SiteCompatibility.h>
+
+namespace Web {
+
+// Site compatibility data, whose URL patterns may only be matched on one thread.
+struct WEBCOMMON_API SharedSiteCompatibilityData final : public AtomicRefCounted<SharedSiteCompatibilityData> {
+ explicit SharedSiteCompatibilityData(SiteCompatibilityData data)
+ : data(move(data))
+ {
+ }
+ ~SharedSiteCompatibilityData();
+
+ SiteCompatibilityData const data;
+};
+
+// The user agent's settings that a fetch reads.
+struct LoaderConfig {
+ String user_agent;
+ NonnullRefPtr<SharedSiteCompatibilityData const> site_compatibility;
+ Vector<String> preferred_languages;
+ bool enable_global_privacy_control { false };
+
+ String user_agent_for_url(URL::URL const& url) const { return site_compatibility->data.user_agent_for_url(url, user_agent); }
+ String user_agent_for_websocket_url(URL::URL const& url) const { return site_compatibility->data.user_agent_for_websocket_url(url, user_agent); }
+ bool exposes_experimental_interface(URL::URL const& url, StringView name) const { return site_compatibility->data.exposes_experimental_interface(url, name); }
+};
+
+// A loader configuration as it was at one moment, which a fetch keeps for its lifetime.
+struct LoaderConfigSnapshot final : public AtomicRefCounted<LoaderConfigSnapshot> {
+ explicit LoaderConfigSnapshot(LoaderConfig config)
+ : config(move(config))
+ {
+ }
+
+ LoaderConfig const config;
+};
+
+}
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 2bc7634fd5f..0c3dd25e7b4 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -80,6 +80,7 @@
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Loader/ContentBlocker.h>
+#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Loader/SourceHighlighter.h>
#include <LibWeb/Namespace.h>
@@ -527,7 +528,7 @@ void ConnectionFromClient::set_site_compatibility_data(JsonValue data)
warnln("Ignoring invalid site compatibility data: {}", parsed_data.error());
return;
}
- Web::ResourceLoader::the().set_site_compatibility_data(parsed_data.release_value());
+ Web::set_site_compatibility_data(parsed_data.release_value());
}
void ConnectionFromClient::update_screen_rects(Web::PageId page_id, Vector<Web::DevicePixelRect> rects, Vector<Web::DevicePixelRect> available_rects, u32 main_screen)
@@ -1156,7 +1157,7 @@ void ConnectionFromClient::debug_request(Web::PageId page_id, ByteString request
}
if (request == "spoof-user-agent") {
- Web::ResourceLoader::the().set_user_agent(MUST(String::from_byte_string(argument)));
+ Web::update_loader_config([&](auto& config) { config.user_agent = MUST(String::from_byte_string(argument)); });
return;
}
@@ -3187,7 +3188,7 @@ void ConnectionFromClient::set_preferred_languages(Web::PageId, Vector<String> p
// object global return a new set of language tags, the user agent must queue a global task on the DOM manipulation
// task source given global to fire an event named languagechange at global, and wait until that task begins to be
// executed before actually returning a new value.
- Web::ResourceLoader::the().set_preferred_languages(move(preferred_languages));
+ Web::update_loader_config([&](auto& config) { config.preferred_languages = move(preferred_languages); });
}
void ConnectionFromClient::set_browsing_behavior(Web::PageId page_id, WebView::BrowsingBehavior browsing_behavior)
@@ -3206,7 +3207,7 @@ void ConnectionFromClient::consume_user_activation(Web::PageId page_id, Web::HTM
void ConnectionFromClient::set_enable_global_privacy_control(Web::PageId, bool enable)
{
- Web::ResourceLoader::the().set_enable_global_privacy_control(enable);
+ Web::update_loader_config([&](auto& config) { config.enable_global_privacy_control = enable; });
}
void ConnectionFromClient::set_geolocation_emulated_position(Web::PageId page_id, WebView::GeolocationPositionData position, Optional<u16> error_code)
diff --git a/Services/WebWorker/ConnectionFromClient.cpp b/Services/WebWorker/ConnectionFromClient.cpp
index f085f828667..a9b9b06ccab 100644
--- a/Services/WebWorker/ConnectionFromClient.cpp
+++ b/Services/WebWorker/ConnectionFromClient.cpp
@@ -11,6 +11,8 @@
#include <LibWeb/FileAPI/BlobURLStore.h>
#include <LibWeb/HTML/BroadcastChannel.h>
#include <LibWeb/HTML/WorkerAgentParent.h>
+#include <LibWeb/Loader/LoaderConfig.h>
+#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Platform/FontPlugin.h>
#include <WebWorker/ConnectionFromClient.h>
#include <WebWorker/PageHost.h>
@@ -81,12 +83,12 @@ void ConnectionFromClient::set_site_compatibility_data(JsonValue data)
warnln("Ignoring invalid site compatibility data: {}", parsed_data.error());
return;
}
- Web::ResourceLoader::the().set_site_compatibility_data(parsed_data.release_value());
+ Web::set_site_compatibility_data(parsed_data.release_value());
}
void ConnectionFromClient::set_user_agent(String user_agent)
{
- Web::ResourceLoader::the().set_user_agent(move(user_agent));
+ Web::update_loader_config([&](auto& config) { config.user_agent = move(user_agent); });
}
void ConnectionFromClient::set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode mode)
@@ -96,7 +98,7 @@ void ConnectionFromClient::set_navigator_compatibility_mode(Web::NavigatorCompat
void ConnectionFromClient::set_preferred_languages(Vector<String> preferred_languages)
{
- Web::ResourceLoader::the().set_preferred_languages(move(preferred_languages));
+ Web::update_loader_config([&](auto& config) { config.preferred_languages = move(preferred_languages); });
}
void ConnectionFromClient::close_worker()
diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt
index cf68573bd85..67ab6c75799 100644
--- a/Tests/LibWeb/CMakeLists.txt
+++ b/Tests/LibWeb/CMakeLists.txt
@@ -23,6 +23,7 @@ set(TEST_SOURCES
TestImageData.cpp
TestInitialLoadTracker.cpp
TestInlineStyle.cpp
+ TestLoaderConfig.cpp
TestMicrosyntax.cpp
TestMimeSniff.cpp
TestNumbers.cpp
diff --git a/Tests/LibWeb/TestFetchResponse.cpp b/Tests/LibWeb/TestFetchResponse.cpp
index 223030fec55..0051db16e0b 100644
--- a/Tests/LibWeb/TestFetchResponse.cpp
+++ b/Tests/LibWeb/TestFetchResponse.cpp
@@ -14,6 +14,7 @@
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
+#include <LibWeb/Loader/LoaderConfig.h>
namespace {
@@ -77,7 +78,7 @@ 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());
+ auto fetch_params = Web::Fetch::Infrastructure::FetchParams::create(request, algorithms, Web::Fetch::Infrastructure::FetchTimingInfo::create(), Web::current_loader_config());
auto response = TransferTrackingResponse::create(*vm);
response->set_status(302);
response->set_header_list(HTTP::HeaderList::create({ { "Location"sv, "/redirected"sv } }));
diff --git a/Tests/LibWeb/TestLoaderConfig.cpp b/Tests/LibWeb/TestLoaderConfig.cpp
new file mode 100644
index 00000000000..f81794b7ea1
--- /dev/null
+++ b/Tests/LibWeb/TestLoaderConfig.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+#include <LibWeb/Loader/LoaderConfig.h>
+
+TEST_CASE(a_change_leaves_a_snapshot_already_taken_as_it_was)
+{
+ auto before = Web::current_loader_config();
+ Web::update_loader_config([](auto& config) { config.preferred_languages = { "fr-CA"_string, "fr"_string }; });
+ auto after = Web::current_loader_config();
+
+ EXPECT_EQ(before->config.preferred_languages, (Vector { "en-US"_string, "en"_string }));
+ EXPECT_EQ(after->config.preferred_languages, (Vector { "fr-CA"_string, "fr"_string }));
+}
+
+TEST_CASE(a_change_keeps_the_rest_of_the_configuration)
+{
+ auto before = Web::current_loader_config();
+ Web::update_loader_config([](auto& config) { config.enable_global_privacy_control = !config.enable_global_privacy_control; });
+ auto after = Web::current_loader_config();
+
+ EXPECT_NE(after->config.enable_global_privacy_control, before->config.enable_global_privacy_control);
+ EXPECT_EQ(after->config.user_agent, before->config.user_agent);
+ EXPECT_EQ(after->config.preferred_languages, before->config.preferred_languages);
+ EXPECT_EQ(after->config.site_compatibility.ptr(), before->config.site_compatibility.ptr());
+}
diff --git a/Tests/LibWeb/Text/expected/Fetch/site-compatibility-user-agent-change-during-fetch.txt b/Tests/LibWeb/Text/expected/Fetch/site-compatibility-user-agent-change-during-fetch.txt
new file mode 100644
index 00000000000..9a32b591095
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/site-compatibility-user-agent-change-during-fetch.txt
@@ -0,0 +1 @@
+User-Agent as at the fetch's start: PASS
diff --git a/Tests/LibWeb/Text/input/Fetch/site-compatibility-user-agent-change-during-fetch.html b/Tests/LibWeb/Text/input/Fetch/site-compatibility-user-agent-change-during-fetch.html
new file mode 100644
index 00000000000..7c380a088cc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/site-compatibility-user-agent-change-during-fetch.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const url = await server.createEcho("GET", "/site-compatibility-user-agent-change-during-fetch", {
+ status: 200,
+ headers: {
+ "Access-Control-Allow-Origin": "*",
+ "Content-Type": "application/json",
+ },
+ reflect_headers_in_body: true,
+ });
+
+ try {
+ const responsePromise = fetch(url);
+ internals.setSiteCompatibilityData(JSON.stringify([{
+ matches: [url],
+ user_agent: ["hide_Ladybird"],
+ }]));
+
+ const userAgent = (await (await responsePromise).json())["User-Agent"][0];
+ println(`User-Agent as at the fetch's start: ${userAgent.includes("Ladybird/") ? "PASS" : "FAIL"}`);
+ } catch (error) {
+ println(`FAIL: ${error}`);
+ } finally {
+ internals.setSiteCompatibilityData("[]");
+ }
+ });
+</script>