From e44702f0907328cccec165b2b95e6c68e17d7964 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Thu, 31 Oct 2024 02:38:57 +1300 Subject: [PATCH] LibWeb: Construct ResourceLoader with a GC Heap This will allow us to easily make use of HeapFunctions instead of SafeFunction for all of the callbacks in ResourceLoader. --- Ladybird/WebContent/main.cpp | 11 ++++++----- Ladybird/WebWorker/main.cpp | 10 +++++----- Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp | 9 +++++---- Userland/Libraries/LibWeb/Loader/ResourceLoader.h | 7 +++++-- Userland/Services/WebContent/main.cpp | 2 +- Userland/Services/WebWorker/main.cpp | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Ladybird/WebContent/main.cpp b/Ladybird/WebContent/main.cpp index e2ea6ef5df5..0d6c54900f7 100644 --- a/Ladybird/WebContent/main.cpp +++ b/Ladybird/WebContent/main.cpp @@ -48,7 +48,7 @@ static ErrorOr load_content_filters(StringView config_path); static ErrorOr load_autoplay_allowlist(StringView config_path); -static ErrorOr initialize_resource_loader(int request_server_socket); +static ErrorOr initialize_resource_loader(JS::Heap&, int request_server_socket); static ErrorOr initialize_image_decoder(int image_decoder_socket); static ErrorOr reinitialize_image_decoder(IPC::File const& image_decoder_socket); @@ -163,7 +163,6 @@ ErrorOr serenity_main(Main::Arguments arguments) } #endif - TRY(initialize_resource_loader(request_server_socket)); TRY(initialize_image_decoder(image_decoder_socket)); Web::HTML::Window::set_internals_object_exposed(expose_internals_object); @@ -172,6 +171,8 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Window)); + TRY(initialize_resource_loader(Web::Bindings::main_thread_vm().heap(), request_server_socket)); + if (log_all_js_exceptions) { JS::g_log_all_js_exceptions = true; } @@ -191,7 +192,7 @@ ErrorOr serenity_main(Main::Arguments arguments) static_assert(IsSame, "Need to handle other IPC transports here"); auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv)); - auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(IPC::Transport(move(webcontent_socket)))); + auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(Web::Bindings::main_thread_vm().heap(), IPC::Transport(move(webcontent_socket)))); webcontent_client->on_image_decoder_connection = [&](auto& socket_file) { auto maybe_error = reinitialize_image_decoder(socket_file); @@ -250,7 +251,7 @@ static ErrorOr load_autoplay_allowlist(StringView config_path) return {}; } -ErrorOr initialize_resource_loader(int request_server_socket) +ErrorOr initialize_resource_loader(JS::Heap& heap, int request_server_socket) { static_assert(IsSame, "Need to handle other IPC transports here"); @@ -258,7 +259,7 @@ ErrorOr initialize_resource_loader(int request_server_socket) TRY(socket->set_blocking(true)); auto request_client = TRY(try_make_ref_counted(IPC::Transport(move(socket)))); - Web::ResourceLoader::initialize(move(request_client)); + Web::ResourceLoader::initialize(heap, move(request_client)); return {}; } diff --git a/Ladybird/WebWorker/main.cpp b/Ladybird/WebWorker/main.cpp index b0b9fb4f8e1..d24c3f577c0 100644 --- a/Ladybird/WebWorker/main.cpp +++ b/Ladybird/WebWorker/main.cpp @@ -29,7 +29,7 @@ # include #endif -static ErrorOr initialize_resource_loader(int request_server_socket); +static ErrorOr initialize_resource_loader(JS::Heap&, int request_server_socket); ErrorOr serenity_main(Main::Arguments arguments) { @@ -62,16 +62,16 @@ ErrorOr serenity_main(Main::Arguments arguments) Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(false)); - TRY(initialize_resource_loader(request_server_socket)); - TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Worker)); + TRY(initialize_resource_loader(Web::Bindings::main_thread_vm().heap(), request_server_socket)); + auto client = TRY(IPC::take_over_accepted_client_from_system_server()); return event_loop.exec(); } -static ErrorOr initialize_resource_loader(int request_server_socket) +static ErrorOr initialize_resource_loader(JS::Heap& heap, int request_server_socket) { static_assert(IsSame, "Need to handle other IPC transports here"); @@ -79,7 +79,7 @@ static ErrorOr initialize_resource_loader(int request_server_socket) TRY(socket->set_blocking(true)); auto request_client = TRY(try_make_ref_counted(IPC::Transport(move(socket)))); - Web::ResourceLoader::initialize(move(request_client)); + Web::ResourceLoader::initialize(heap, move(request_client)); return {}; } diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp index 39fbb742df8..0336672fd1b 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -30,9 +30,9 @@ namespace Web { static RefPtr s_resource_loader; -void ResourceLoader::initialize(NonnullRefPtr request_client) +void ResourceLoader::initialize(JS::Heap& heap, NonnullRefPtr request_client) { - s_resource_loader = adopt_ref(*new ResourceLoader(move(request_client))); + s_resource_loader = adopt_ref(*new ResourceLoader(heap, move(request_client))); } ResourceLoader& ResourceLoader::the() @@ -44,8 +44,9 @@ ResourceLoader& ResourceLoader::the() return *s_resource_loader; } -ResourceLoader::ResourceLoader(NonnullRefPtr request_client) - : m_request_client(move(request_client)) +ResourceLoader::ResourceLoader(JS::Heap& heap, NonnullRefPtr request_client) + : m_heap(heap) + , m_request_client(move(request_client)) , m_user_agent(MUST(String::from_utf8(default_user_agent))) , m_platform(MUST(String::from_utf8(default_platform))) , m_preferred_languages({ "en-US"_string }) diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.h b/Userland/Libraries/LibWeb/Loader/ResourceLoader.h index 4dcb8d33f84..34f3d32d487 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.h +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.h @@ -22,7 +22,7 @@ namespace Web { class ResourceLoader : public Core::EventReceiver { C_OBJECT_ABSTRACT(ResourceLoader) public: - static void initialize(NonnullRefPtr); + static void initialize(JS::Heap&, NonnullRefPtr); static ResourceLoader& the(); RefPtr load_resource(Resource::Type, LoadRequest&); @@ -74,8 +74,10 @@ public: void clear_cache(); void evict_from_cache(LoadRequest const&); + JS::Heap& heap() { return m_heap; } + private: - explicit ResourceLoader(NonnullRefPtr); + explicit ResourceLoader(JS::Heap&, NonnullRefPtr); RefPtr start_network_request(LoadRequest const&); void handle_network_response_headers(LoadRequest const&, HTTP::HeaderMap const&); @@ -83,6 +85,7 @@ private: int m_pending_loads { 0 }; + JS::Heap& m_heap; NonnullRefPtr m_request_client; HashTable> m_active_requests; diff --git a/Userland/Services/WebContent/main.cpp b/Userland/Services/WebContent/main.cpp index 1cf1a482d44..14c3ef5f414 100644 --- a/Userland/Services/WebContent/main.cpp +++ b/Userland/Services/WebContent/main.cpp @@ -50,8 +50,8 @@ ErrorOr serenity_main(Main::Arguments) return Web::Platform::AudioCodecPluginAgnostic::create(move(loader)); }); - Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create())); TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Window)); + Web::ResourceLoader::initialize(Web::Bindings::main_thread_vm().heap(), TRY(WebView::RequestServerAdapter::try_create())); auto client = TRY(IPC::take_over_accepted_client_from_system_server()); return event_loop.exec(); diff --git a/Userland/Services/WebWorker/main.cpp b/Userland/Services/WebWorker/main.cpp index 12e38415e56..06dc981f087 100644 --- a/Userland/Services/WebWorker/main.cpp +++ b/Userland/Services/WebWorker/main.cpp @@ -35,8 +35,8 @@ ErrorOr serenity_main(Main::Arguments) Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity); Web::Platform::FontPlugin::install(*new Web::Platform::FontPluginSerenity); - Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create())); TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Worker)); + Web::ResourceLoader::initialize(Web::Bindings::main_thread_vm().heap(), TRY(WebView::RequestServerAdapter::try_create())); auto client = TRY(IPC::take_over_accepted_client_from_system_server());