Skip to content

Truly independent web browser

LibWeb: Give each agent a ResourceLoader of its own

Andreas Kling914a422d95b0
diff --git a/Libraries/LibWeb/HTML/NavigatorID.cpp b/Libraries/LibWeb/HTML/NavigatorID.cpp
index a44afd5c9c8..42f69e3da35 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.cpp
+++ b/Libraries/LibWeb/HTML/NavigatorID.cpp
@@ -30,7 +30,7 @@ Utf16String NavigatorIDMixin::app_version() const
auto trail = MUST(user_agent.substring_from_byte_offset("Mozilla/"sv.length()));
// 4. -> If the navigator compatibility mode is Chrome or WebKit
- auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
+ auto navigator_compatibility_mode = ResourceLoader::navigator_compatibility_mode();
if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome || navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) {
// Return trail.
return Utf16String::from_ascii_without_validation(trail.bytes());
@@ -63,7 +63,7 @@ Utf16String NavigatorIDMixin::platform() const
// platform.
// FIXME: Use some portion of the user agent string to make spoofing work 100%
- return Utf16String::from_ascii_without_validation(ResourceLoader::the().platform().bytes());
+ return Utf16String::from_ascii_without_validation(default_platform.bytes());
}
// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-oscpu
@@ -95,7 +95,7 @@ Utf16String NavigatorIDMixin::oscpu() const
// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-productsub
Utf16FlyString NavigatorIDMixin::product_sub() const
{
- auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
+ auto navigator_compatibility_mode = ResourceLoader::navigator_compatibility_mode();
// Must return the appropriate string from the following list:
@@ -125,7 +125,7 @@ Utf16String NavigatorIDMixin::user_agent() const
// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-vendor
Utf16FlyString NavigatorIDMixin::vendor() const
{
- auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
+ auto navigator_compatibility_mode = ResourceLoader::navigator_compatibility_mode();
// Must return the appropriate string from the following list:
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index 45a025a1867..9734b5d8710 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -7,6 +7,7 @@
#include <AK/AllOf.h>
#include <AK/Debug.h>
+#include <AK/NeverDestroyed.h>
#include <LibCore/Directory.h>
#include <LibCore/MimeData.h>
#include <LibCore/Resource.h>
@@ -17,7 +18,6 @@
#include <LibRequests/Request.h>
#include <LibRequests/RequestClient.h>
#include <LibURL/Parser.h>
-#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Fetch/Engine/NetworkTransportHandle.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
@@ -35,10 +35,11 @@ namespace Web {
static RefPtr<ResourceLoader>& resource_loader()
{
- static RefPtr<ResourceLoader>& resource_loader = *new RefPtr<ResourceLoader>;
- return resource_loader;
+ static thread_local NeverDestroyed<RefPtr<ResourceLoader>> resource_loader;
+ return *resource_loader;
}
+// NB: This is set from the command line, before any agent starts.
static Optional<URL::URL>& proxy_autoconfig_url_storage()
{
static Optional<URL::URL>& proxy_autoconfig_url = *new Optional<URL::URL>;
@@ -89,11 +90,10 @@ ResourceLoader& ResourceLoader::the()
ResourceLoader::ResourceLoader(GC::Heap& heap)
: m_heap(heap)
- , m_platform(MUST(String::from_utf8(default_platform)))
{
}
-NavigatorCompatibilityMode ResourceLoader::s_navigator_compatibility_mode = default_navigator_compatibility_mode;
+Atomic<NavigatorCompatibilityMode, AK::MemoryOrder::memory_order_relaxed> ResourceLoader::s_navigator_compatibility_mode { default_navigator_compatibility_mode };
void ResourceLoader::set_navigator_compatibility_mode(NavigatorCompatibilityMode mode)
{
@@ -101,11 +101,10 @@ void ResourceLoader::set_navigator_compatibility_mode(NavigatorCompatibilityMode
// A script reads two strings to tell engines apart that no navigator attribute covers: the frames of an error's
// stack, and the source text of a built-in function. Both follow the engine the navigator claims.
- auto& vm = Bindings::main_thread_vm();
if (mode == NavigatorCompatibilityMode::Chrome)
- vm.set_source_text_syntaxes(JS::VM::StackFrameSyntax::Indented, JS::VM::NativeFunctionSourceSyntax::SingleLine);
+ JS::VM::set_source_text_syntaxes(JS::VM::StackFrameSyntax::Indented, JS::VM::NativeFunctionSourceSyntax::SingleLine);
else
- vm.set_source_text_syntaxes(JS::VM::StackFrameSyntax::AtSign, JS::VM::NativeFunctionSourceSyntax::MultiLine);
+ JS::VM::set_source_text_syntaxes(JS::VM::StackFrameSyntax::AtSign, JS::VM::NativeFunctionSourceSyntax::MultiLine);
}
void ResourceLoader::set_client(Optional<Utf16String> const& top_level_site, NonnullRefPtr<Requests::RequestClient> request_client)
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.h b/Libraries/LibWeb/Loader/ResourceLoader.h
index 083e0a5c5f3..f5e8bbc8d4c 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.h
+++ b/Libraries/LibWeb/Loader/ResourceLoader.h
@@ -7,6 +7,7 @@
#pragma once
+#include <AK/Atomic.h>
#include <AK/ByteString.h>
#include <AK/HashMap.h>
#include <AK/HashTable.h>
@@ -26,6 +27,8 @@
namespace Web {
+// Each agent has a loader of its own, which holds its connections to the RequestServer and its requests, and only its
+// thread can use it. The static settings are the process's.
class WEB_API ResourceLoader : public Core::EventReceiver {
C_OBJECT_ABSTRACT(ResourceLoader)
@@ -33,7 +36,7 @@ public:
static void initialize(GC::Heap&);
static bool is_initialized();
- // Forgets the loader, once its requests are stopped.
+ // Forgets the loader of this agent, once its requests are stopped.
static void shut_down();
static ResourceLoader& the();
@@ -66,9 +69,6 @@ public:
static bool is_known_hsts_host(Page&, String const& host);
- String const& platform() const { return m_platform; }
- void set_platform(String platform) { m_platform = move(platform); }
-
// 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; }
@@ -99,8 +99,7 @@ private:
HashMap<Utf16String, RefPtr<Requests::RequestClient>> m_site_request_clients;
HashTable<NonnullRefPtr<Requests::Request>> m_active_requests;
- String m_platform;
- static NavigatorCompatibilityMode s_navigator_compatibility_mode;
+ static Atomic<NavigatorCompatibilityMode, AK::MemoryOrder::memory_order_relaxed> s_navigator_compatibility_mode;
};
}