Truly independent web browser
LibWebView+WebContent+WebWorker: Run shared workers on threads
Andreas Kling30c21469961e
diff --git a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cppindex fb0fbc4379e..789c0aa4726 100644--- a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp@@ -311,8 +311,8 @@ void LocalTraversableNavigable::run_ui_traversable_close_unload_task()queue_a_task(Task::Source::NavigationAndTraversal, nullptr, nullptr,GC::create_function(heap(), [this] {// NB: The UI process discards the page of a tab it let go of without waiting for this task, and the page- // goes without the unload then.- if (page().has_been_discarded())+ // goes without the unload then. A tab can also go while this task is queued.+ if (page().has_been_discarded() || has_been_destroyed())return;// 2. Unload document, passing along newDocument if it is not null.diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 310bcf5b320..151fbdaf795 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -1448,6 +1448,7 @@ void Application::reset_private_browsing_session()// NB: Those views own the session until they are deleted, so let go of it here. The private window opened next// starts a new one rather than picking up what this was asked to clear.m_private_session.clear();+ WorkerProcessManager::the().remove_private_workers_without_owners();}Web::CompositorContextId Application::allocate_compositor_context_id()@@ -1659,6 +1660,11 @@ ErrorOr<NonnullRefPtr<WebContentPage>> Application::open_page_for_new_tab(IsPrivreturn web_content_client->open_initial_page_for_new_top_level_traversable();}+ErrorOr<NonnullRefPtr<WebContentClient>> Application::launch_web_content_process_without_page(IsPrivate is_private)+{+ return create_web_content_client(is_private, allocate_page_id());+}+ErrorOr<Application::ChildFrameWebContentProcess> Application::launch_child_frame_web_content_process(IsPrivate is_private, Vector<Web::HTML::RemoteNavigableDescriptor> remote_navigables, Web::HTML::CrossProcessId root_navigable_id, Web::HTML::SessionHistoryEntryDescriptor initial_history_entry, Web::HTML::VisibilityState system_visibility_state){auto page_id = allocate_page_id();diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex d7c72914ca1..6a47aacbcd1 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -264,6 +264,8 @@ public:NonnullRefPtr<WebContentClient> client;Web::PageId page_id { 0 };};+ // For what may share a process with no page. It ends once nothing keeps it.+ ErrorOr<NonnullRefPtr<WebContentClient>> launch_web_content_process_without_page(IsPrivate);ErrorOr<ChildFrameWebContentProcess> launch_child_frame_web_content_process(IsPrivate, Vector<Web::HTML::RemoteNavigableDescriptor> remote_navigables, Web::HTML::CrossProcessId root_navigable_id, Web::HTML::SessionHistoryEntryDescriptor initial_history_entry, Web::HTML::VisibilityState system_visibility_state);Web::PageId allocate_page_id();Web::HTML::CrossProcessIdAllocator allocate_cross_process_id_allocator();diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex c8771f21976..02d5b9c30c0 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -321,6 +321,10 @@ void WebContentClient::close_page_of_closed_tab(Web::PageId page_id)// e.g. because the user requested a forced close.if (page->needs_beforeunload_check())page->set_detached_close_pending(false);+ // NB: The process can stay alive for other pages or for a shared worker, and the document must not go on in it.+ // Its close steps run through the traversable that is going away, so it cannot close by itself.+ if (!page->detached_close_pending() || WorkerProcessManager::the().hosts_shared_worker(*this))+ page->async_discard_embedded_page();PictureInPictureManager::the().close_window_and_its_page(*page);// NB: Other tabs can keep the process alive, so its page has to go as well.page->async_discard_embedded_page();@@ -450,6 +454,9 @@ Optional<CanonicalNavigable&> WebContentClient::hosted_navigable(Web::HTML::Crosvoid WebContentClient::close_server_if_unused(){+ if (m_requested_close)+ return;+bool any_detached_close_pending = false;for (auto const& page : m_pages) {if (page.value->is_open())@@ -457,6 +464,9 @@ void WebContentClient::close_server_if_unused()any_detached_close_pending |= page.value->detached_close_pending();}+ if (WorkerProcessManager::the().hosts_shared_worker(*this))+ return;+if (!any_detached_close_pending) {if (m_detached_page_close_timer)m_detached_page_close_timer->stop();diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex 18ed288b8ad..a0c9056a18b 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -191,13 +191,13 @@ public:Web::CompositorContextId allocate_compositor_context(Web::PageId page_id, Web::PagePresentationRegistration);Optional<Web::PageId> page_id_for_compositor_context_id(Web::CompositorContextId) const;bool has_requested_close() const { return m_requested_close; }+ void close_server_if_unused();pid_t pid() const { return m_child_identity.pid; }u64 child_id() const { return m_child_identity.child_id; }void set_child_identity(ChildIdentity identity) { m_child_identity = identity; }private:- void close_server_if_unused();bool forget_compositor_context(Web::CompositorContextId);void destroy_all_compositor_contexts();void cancel_navigation_transactions();diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 2a255075c27..ae668c48e77 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -650,6 +650,9 @@ void WebContentPage::close()PictureInPictureManager::the().close_window(*this);NotificationManager::the().page_did_close(*this);m_is_open = false;+ // NB: The process can outlive the page, for the shared workers it runs, or for other pages.+ if (m_client)+ WorkerProcessManager::the().remove_web_content_page_owner(*m_client, m_id);// NB: Nothing finishes the input this page held, including a page discarded while its process hosts other pages.if (m_traversable) {if (auto view = m_traversable->view(); view.has_value())@@ -3561,6 +3564,14 @@ void WebContentPage::did_remove_blob_url_entries(Web::HTML::EnvironmentId enviroclient().session().blob_url_store->remove_entries(urls, environment->origin(), WeakPtr<WebContentClient> { client() });}+Optional<bool> WebContentPage::hosted_environment_is_cross_origin_isolated(Web::HTML::EnvironmentId const& environment_id) const+{+ auto document = document_with_hosted_environment(environment_id);+ if (!document.has_value())+ return {};+ return document->relevant_global_object().agent().agent_cluster_cross_origin_isolation_mode() != Web::HTML::CrossOriginIsolationMode::None;+}+Optional<bool> WebContentPage::hosted_environment_has_cross_site_ancestor(Web::HTML::EnvironmentId const& environment_id) const{auto is_of_environment = [&](CanonicalDocument const& document) {diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 4cfee8c6c42..bdc67b3869c 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -94,6 +94,7 @@ public:// Whether the document of an environment the page hosts has a cross-site ancestor, or nothing if the page hosts no// such environment.Optional<bool> hosted_environment_has_cross_site_ancestor(Web::HTML::EnvironmentId const& environment_id) const;+ Optional<bool> hosted_environment_is_cross_origin_isolated(Web::HTML::EnvironmentId const& environment_id) const;bool hosts_an_environment_with_storage_key(Web::StorageAPI::StorageKey const&) const;void spoof_document_origin_for_testing(Web::HTML::EnvironmentId const& environment_id, URL::Origin);// Clears the bottle an environment of this page names, the way the browser's own clearing does: through the jar.diff --git a/Libraries/LibWebView/WebWorkerClient.h b/Libraries/LibWebView/WebWorkerClient.hindex a087fb7a45c..7f8f04d9fa2 100644--- a/Libraries/LibWebView/WebWorkerClient.h+++ b/Libraries/LibWebView/WebWorkerClient.h@@ -53,7 +53,7 @@ public:void terminate_process();// The WebContent process that runs this worker on a thread. A worker with a process of its own has none.- RefPtr<WebContentClient> host() const { return m_host.strong_ref(); }+ WebContentClient* host() const { return m_host.ptr(); }void set_host(WebContentClient&);bool runs_on_thread() const { return m_runs_on_thread; }diff --git a/Libraries/LibWebView/WorkerProcessManager.cpp b/Libraries/LibWebView/WorkerProcessManager.cppindex 24477a8d814..87d3dde2d77 100644--- a/Libraries/LibWebView/WorkerProcessManager.cpp+++ b/Libraries/LibWebView/WorkerProcessManager.cpp@@ -10,6 +10,7 @@#include <LibIPC/File.h>#include <LibURL/Parser.h>#include <LibURL/Site.h>+#include <LibWebCommon/WebView/SiteIsolation.h>#include <LibWebView/Application.h>#include <LibWebView/CanonicalEnvironmentSettingsObject.h>#include <LibWebView/CanonicalNavigable.h>@@ -19,6 +20,7 @@#include <LibWebView/Utilities.h>#include <LibWebView/ViewImplementation.h>#include <LibWebView/WebContentClient.h>+#include <LibWebView/WebContentPage.h>#include <LibWebView/WebWorkerClient.h>#include <LibWebView/WorkerProcessManager.h>@@ -192,7 +194,8 @@ WorkerProcessManager::StartedWorkerAgent WorkerProcessManager::start_worker_agen// 11.6. Otherwise, in parallel, run a worker given worker, urlRecord, outsideSettings, outsidePort,// and options.// AD-HOC: For DedicatedWorker there is no shared worker manager step. It always gets a fresh thread in the process- // of its owner, or a fresh process if its owner has none to give.+ // of its owner. A shared worker's thread is in the process of the owner that starts it, if that process may+ // host it, and otherwise in a process of its own.auto agent_id = ++m_next_agent_id;RefPtr<WebContentClient> host;if (request.agent_type == Web::HTML::AgentType::DedicatedWorker) {@@ -203,14 +206,22 @@ WorkerProcessManager::StartedWorkerAgent WorkerProcessManager::start_worker_agen[](WebWorkerOwner const& web_worker_owner) -> RefPtr<WebContentClient> {return web_worker_owner.client->host();});+ } else if (auto const* web_content_owner = owner.client.get_pointer<WebContentOwner>(); web_content_owner && owner_process_may_host_shared_worker(*web_content_owner, *outside_settings)) {+ host = web_content_owner->client.strong_ref();}+ bool const host_has_only_this_worker = !host && request.agent_type == Web::HTML::AgentType::SharedWorker;Optional<IPC::TransportHandle> thread_transport;auto client_or_error = [&]() -> ErrorOr<NonnullRefPtr<WebWorkerClient>> {auto client = TRY([&]() -> ErrorOr<NonnullRefPtr<WebWorkerClient>> {- if (!host)+ if (!host && !host_has_only_this_worker)return launch_web_worker_process(request.agent_type, is_private, agent_id);auto transport = TRY(IPC::Transport::create_paired());- thread_transport = move(transport.remote_handle);+ if (host_has_only_this_worker) {+ host = TRY(Application::the().launch_web_content_process_without_page(is_private));+ host->async_start_worker_thread(move(transport.remote_handle), request.agent_type);+ } else {+ thread_transport = move(transport.remote_handle);+ }auto client = TRY(try_make_ref_counted<WebWorkerClient>(move(transport.local), is_private, agent_id));client->set_host(*host);return client;@@ -248,6 +259,8 @@ WorkerProcessManager::StartedWorkerAgent WorkerProcessManager::start_worker_agen// A worker the system cannot give a thread or a process fails to load, as one whose script cannot be fetched does.if (client_or_error.is_error()) {dbgln("Unable to start a worker: {}", client_or_error.error());+ if (host_has_only_this_worker && host)+ host->close_server_if_unused();notify_worker_script_load_failure(owner);return {};}@@ -271,17 +284,9 @@ WorkerProcessManager::StartedWorkerAgent WorkerProcessManager::start_worker_agenauto environment_id = inside_settings->id();client->set_site(URL::Site::serialize_for_partitioning(inside_settings->origin()));- // NB: The task manager lists the worker's process with the process that started it, and once that one is gone,- // with the site of the worker's origin. A worker on a thread is part of a process that it lists already.- if (auto process = Application::process_manager().find_child(client->child_id()); !host && process.has_value()) {- owner.client.visit(- [&](WebContentOwner const& web_content_owner) {- if (web_content_owner.client)- process->set_owner_child_id(web_content_owner.client->child_id());- },- [&](WebWorkerOwner const& web_worker_owner) {- process->set_owner_child_id(web_worker_owner.client->child_id());- });+ // NB: The task manager lists a process with the site of its pages. One that has none left only runs on for its shared+ // workers, so it goes by the site of their origin.+ if (auto process = Application::process_manager().find_child(client->child_id()); request.agent_type == Web::HTML::AgentType::SharedWorker && process.has_value()) {if (auto const& worker_origin = inside_settings->origin(); !worker_origin.is_opaque() && !worker_origin.is_file_origin())process->set_site_url(URL::Parser::basic_parse(worker_origin.serialize()));}@@ -343,11 +348,41 @@ WorkerProcessManager::StartedWorkerAgent WorkerProcessManager::start_worker_agenauto inside_origin = agent.inside_settings->origin();m_agents.set(agent_id, move(agent));- client->async_start_worker(request.url, request.type, request.credentials, request.name, move(request.outside_port), request.outside_settings, request.agent_type, move(inside_origin), move(environment_id));+ client->async_start_worker(request.url, request.type, request.credentials, request.name, move(request.outside_port), request.outside_settings, request.agent_type, move(inside_origin), move(environment_id), host_has_only_this_worker);return { agent_id, move(thread_transport) };}+// Sites decide what runs together in a process, as for the documents of CanonicalNavigable::process_to_host(). The worker's+// origin is that of the document starting it, so the process of that document already holds the worker's site.+bool WorkerProcessManager::owner_process_may_host_shared_worker(WebContentOwner const& owner, CanonicalEnvironmentSettingsObject const& outside_settings)+{+ if (!owner.client || owner.client->has_requested_close())+ return false;+ auto* page = owner.client->page(owner.page_id);+ if (!page)+ return false;++ // NB: Nothing in a cross-origin isolated process may have been loaded without the consent of its origin. A shared+ // worker is in an agent cluster of its own, and needs no such consent unless its response says so.+ if (page->hosted_environment_is_cross_origin_isolated(outside_settings.id()).value_or(true))+ return false;++ // NB: Isolating only top-level documents leaves a cross-site frame with the process of its parent's site.+ if (site_isolation_mode() == SiteIsolationMode::TopLevel) {+ auto top_level_origin = outside_settings.top_level_origin();+ return top_level_origin.has_value() && top_level_origin->is_same_site(outside_settings.origin());+ }+ return true;+}++bool WorkerProcessManager::hosts_shared_worker(WebContentClient const& process) const+{+ return any_of(m_agents, [&](auto const& entry) {+ return entry.value.agent_type == Web::HTML::AgentType::SharedWorker && entry.value.client->host() == &process;+ });+}+void WorkerProcessManager::update_permission_snapshot(PermissionManager const& manager){for (auto const& agent : m_agents) {@@ -425,6 +460,38 @@ void WorkerProcessManager::remove_web_content_owner(WebContentClient& client)remove_agent(agent_id, AgentRemovalCause::OwnerSetEmptied);}+void WorkerProcessManager::remove_web_content_page_owner(WebContentClient& client, Web::PageId page_id)+{+ Vector<Web::HTML::WorkerAgentId> agents_to_close;+ for (auto& entry : m_agents) {+ auto& agent = entry.value;+ auto did_remove_owner = agent.owners.remove_all_matching([&](Owner const& owner) {+ auto const* web_content_owner = owner.client.get_pointer<WebContentOwner>();+ return web_content_owner && web_content_owner->client.ptr() == &client && web_content_owner->page_id == page_id;+ });+ if (!did_remove_owner)+ continue;+ forget_removed_outside_settings_owner(agent);+ if (agent.owners.is_empty())+ agents_to_close.append(agent.id);+ }++ for (auto agent_id : agents_to_close)+ remove_agent(agent_id, AgentRemovalCause::OwnerSetEmptied);+}++void WorkerProcessManager::remove_private_workers_without_owners()+{+ Vector<Web::HTML::WorkerAgentId> agents_to_close;+ for (auto const& entry : m_agents) {+ if (entry.value.is_private == IsPrivate::Yes && entry.value.owners.is_empty())+ agents_to_close.append(entry.key);+ }++ for (auto agent_id : agents_to_close)+ remove_agent(agent_id, AgentRemovalCause::AgentTerminated);+}+void WorkerProcessManager::remove_web_worker_owner(WebWorkerClient& client){Vector<Web::HTML::WorkerAgentId> agents_to_close;@@ -784,6 +851,15 @@ void WorkerProcessManager::remove_agent(Web::HTML::WorkerAgentId agent_id, Agentif (agent.shared_worker_key.has_value())m_shared_workers.remove(*agent.shared_worker_key);+ // NB: The owner of a page can be removing the agent from the destructor of the host, which nothing can take a+ // reference to any more.+ if (agent.agent_type == Web::HTML::AgentType::SharedWorker && agent.client->host()) {+ Core::deferred_invoke([host = agent.client->host()->make_weak_ptr<WebContentClient>()] {+ if (auto process = host.strong_ref())+ process->close_server_if_unused();+ });+ }+agent.closing = true;if (!agent.client->is_open())return;diff --git a/Libraries/LibWebView/WorkerProcessManager.h b/Libraries/LibWebView/WorkerProcessManager.hindex 7321ecf9f4c..4cd87f180a6 100644--- a/Libraries/LibWebView/WorkerProcessManager.h+++ b/Libraries/LibWebView/WorkerProcessManager.h@@ -74,6 +74,7 @@ public:void rtc_transform_teardown(WebContentClient&, Web::HTML::WorkerAgentId, u64 transform_id);void rtc_transform_encoded_audio_frame(WebContentClient&, Web::HTML::WorkerAgentId, u64 transform_id, ByteBuffer payload, u32 ssrc, u8 payload_type, u32 rtp_timestamp, u16 sequence_number);void remove_web_content_owner(WebContentClient&);+ void remove_web_content_page_owner(WebContentClient&, Web::PageId);void remove_web_worker_owner(WebWorkerClient&);// The agent behind the source connection has delivered the message to its own channels. Every other agent gets it@@ -82,6 +83,13 @@ public:void for_each_request_server_site_bindings(Function<IterationDecision(RequestServerSiteBindings&)> const&);ErrorOr<void> simulate_request_server_connection_loss_for_testing(WebContentClient&, Web::PageId page_id);+ // Those of a private session that is over, whose workers only their extended lifetime keeps, are not to meet the next+ // one's documents.+ void remove_private_workers_without_owners();++ // A process goes on without pages for as long as it runs a shared worker, as the worker's owners can be anywhere.+ bool hosts_shared_worker(WebContentClient const&) const;+Optional<u64> exclusive_performance_owner(pid_t) const;size_t client_count() const { return m_agents.size(); }@@ -118,6 +126,7 @@ private:};StartedWorkerAgent start_worker_agent(Owner, Optional<CanonicalEnvironmentSettingsObject const&> outside_settings, Web::HTML::WorkerAgentStartRequest, IsPrivate);+ static bool owner_process_may_host_shared_worker(WebContentOwner const&, CanonicalEnvironmentSettingsObject const& outside_settings);void notify_worker_script_load_failure(Owner const&);void notify_worker_exception(Owner const&, Utf16String const& message, Utf16String const& filename, u32 lineno, u32 colno);diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex 7caaf95d618..0e349ae49ab 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -383,6 +383,13 @@ void ConnectionFromClient::close_server()shutdown();}+void ConnectionFromClient::start_worker_thread(IPC::TransportHandle worker_transport, Web::HTML::AgentType agent_type)+{+ // NB: The UI process notices a transport that nobody takes up, and tells the owners that their worker is gone.+ if (auto result = WebWorker::start_worker_thread(move(worker_transport), agent_type); result.is_error())+ dbgln("Unable to start a thread for a worker: {}", result.error());+}+Messages::WebContentServer::GetWindowHandleResponse ConnectionFromClient::get_window_handle(Web::PageId page_id){if (auto page = this->page(page_id); page.has_value())diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.hindex 1db9b7e021d..d4a27298064 100644--- a/Services/WebContent/ConnectionFromClient.h+++ b/Services/WebContent/ConnectionFromClient.h@@ -105,6 +105,7 @@ private:virtual void content_navigable_completely_finished_loading(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) override;virtual void update_local_root_container_state(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::ReplicatedContainerState) override;virtual void close_server() override;+ virtual void start_worker_thread(IPC::TransportHandle worker_transport, Web::HTML::AgentType agent_type) override;virtual Messages::WebContentServer::GetWindowHandleResponse get_window_handle(Web::PageId page_id) override;virtual void set_window_handle(Web::PageId page_id, String handle) override;virtual void run_webdriver_command(Web::PageId page_id, u64 command_id, Optional<Web::HTML::CrossProcessId> navigable_id, String name, JsonValue payload, Vector<String> arguments) override;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex 682250e69df..0def3523a65 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -2264,6 +2264,7 @@ void PageClient::reset_gamepad_access_for_testing()Web::HTML::WorkerAgentId PageClient::start_worker_agent(Web::HTML::WorkerAgentStartRequest&& request){+ auto agent_type = request.agent_type;auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::StartWorkerAgent>(m_id, move(request));if (!response) {dbgln("WebContent client disconnected during StartWorkerAgent. Exiting peacefully.");@@ -2272,7 +2273,7 @@ Web::HTML::WorkerAgentId PageClient::start_worker_agent(Web::HTML::WorkerAgentSt// NB: The UI process notices a transport that nobody takes up, and tells the owner that its worker is gone.if (auto transport = response->take_thread_transport(); transport.has_value()) {- if (auto result = WebWorker::start_worker_thread(transport.release_value()); result.is_error())+ if (auto result = WebWorker::start_worker_thread(transport.release_value(), agent_type); result.is_error())dbgln("Unable to start a thread for a worker: {}", result.error());}diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipcindex 6bc49eead9a..06046a7cb79 100644--- a/Services/WebContent/WebContentServer.ipc+++ b/Services/WebContent/WebContentServer.ipc@@ -40,6 +40,7 @@#include <LibWebCommon/HTML/VisibilityState.h>#include <LibWebCommon/HTML/WorkerAgentForward.h>#include <LibWebCommon/HTML/WorkerAgentTypes.h>+#include <LibWebCommon/HTML/WorkerTypes.h>#include <LibWebCommon/Page/ContextMenuForInputEventsTarget.h>#include <LibWebCommon/Page/DragEvent.h>#include <LibWebCommon/Page/InputEvent.h>@@ -80,6 +81,7 @@ endpoint WebContentServer routed by Web::PageId page_idcontent_navigable_completely_finished_loading(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) =|update_local_root_container_state(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::ReplicatedContainerState state) =|close_server() =|+ [NotRouted] start_worker_thread(IPC::TransportHandle worker_transport, Web::HTML::AgentType agent_type) =|[NotRouted] get_window_handle(Web::PageId page_id) => (String handle)set_window_handle(Web::PageId page_id, String handle) =|diff --git a/Services/WebWorker/ConnectionFromClient.cpp b/Services/WebWorker/ConnectionFromClient.cppindex 24fb6bf3e35..95a2db84b5c 100644--- a/Services/WebWorker/ConnectionFromClient.cpp+++ b/Services/WebWorker/ConnectionFromClient.cpp@@ -303,7 +303,7 @@ Web::Page const& ConnectionFromClient::page() constreturn m_page_host->page();}-void ConnectionFromClient::start_worker(URL::URL url, Web::HTML::WorkerType type, Web::HTML::RequestCredentials credentials, String name, Web::HTML::TransferDataEncoder implicit_port, Web::HTML::SerializedEnvironmentSettingsObject outside_settings, Web::HTML::AgentType agent_type, URL::Origin origin, Web::HTML::EnvironmentId environment_id)+void ConnectionFromClient::start_worker(URL::URL url, Web::HTML::WorkerType type, Web::HTML::RequestCredentials credentials, String name, Web::HTML::TransferDataEncoder implicit_port, Web::HTML::SerializedEnvironmentSettingsObject outside_settings, Web::HTML::AgentType agent_type, URL::Origin origin, Web::HTML::EnvironmentId environment_id, bool has_process_to_itself){m_worker_host = make_ref_counted<WorkerHost>(move(url), type, move(name));@@ -318,7 +318,7 @@ void ConnectionFromClient::start_worker(URL::URL url, Web::HTML::WorkerType typerun_transform_init(entry.key, move(entry.value));});- m_worker_host->run(page(), move(implicit_port), outside_settings, credentials, is_shared, move(origin), move(environment_id));+ m_worker_host->run(page(), move(implicit_port), outside_settings, credentials, is_shared, move(origin), move(environment_id), has_process_to_itself);}void ConnectionFromClient::connect_shared_worker(Web::HTML::TransferDataEncoder message_port, Web::HTML::SerializedEnvironmentSettingsObject outside_settings)diff --git a/Services/WebWorker/ConnectionFromClient.h b/Services/WebWorker/ConnectionFromClient.hindex b4e233d35c2..633cbecd6ff 100644--- a/Services/WebWorker/ConnectionFromClient.h+++ b/Services/WebWorker/ConnectionFromClient.h@@ -79,7 +79,7 @@ private:virtual void set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode mode) override;virtual void set_preferred_languages(Vector<String> preferred_languages) override;virtual void set_system_font_family(String family) override;- virtual void start_worker(URL::URL url, Web::HTML::WorkerType type, Web::HTML::RequestCredentials credentials, String name, Web::HTML::TransferDataEncoder, Web::HTML::SerializedEnvironmentSettingsObject, Web::HTML::AgentType, URL::Origin origin, Web::HTML::EnvironmentId environment_id) override;+ virtual void start_worker(URL::URL url, Web::HTML::WorkerType type, Web::HTML::RequestCredentials credentials, String name, Web::HTML::TransferDataEncoder, Web::HTML::SerializedEnvironmentSettingsObject, Web::HTML::AgentType, URL::Origin origin, Web::HTML::EnvironmentId environment_id, bool has_process_to_itself) override;virtual void connect_shared_worker(Web::HTML::TransferDataEncoder, Web::HTML::SerializedEnvironmentSettingsObject) override;virtual void handle_file_return(i32 error, Optional<IPC::File> file, i32 request_id) override;virtual void blob_url_entry_removed(Utf16String url) override;diff --git a/Services/WebWorker/PageHost.cpp b/Services/WebWorker/PageHost.cppindex 1d0f28c6f0f..a8f4013aefe 100644--- a/Services/WebWorker/PageHost.cpp+++ b/Services/WebWorker/PageHost.cpp@@ -158,13 +158,14 @@ Optional<Web::FileAPI::SerializedBlobURLEntry> PageHost::page_did_request_blob_uWeb::HTML::WorkerAgentId PageHost::start_worker_agent(Web::HTML::WorkerAgentStartRequest&& request){+ auto agent_type = request.agent_type;auto response = m_client.send_sync_but_allow_failure<Messages::WebWorkerClient::StartWorkerAgent>(move(request));if (!response)return 0;// NB: The UI process notices a transport that nobody takes up, and tells the owner that its worker is gone.if (auto transport = response->take_thread_transport(); transport.has_value()) {- if (auto result = start_worker_thread(transport.release_value()); result.is_error())+ if (auto result = start_worker_thread(transport.release_value(), agent_type); result.is_error())dbgln("Unable to start a thread for a worker: {}", result.error());}return response->agent_id();diff --git a/Services/WebWorker/WebWorkerServer.ipc b/Services/WebWorker/WebWorkerServer.ipcindex 8aa3d9a83a2..c444cf496ba 100644--- a/Services/WebWorker/WebWorkerServer.ipc+++ b/Services/WebWorker/WebWorkerServer.ipc@@ -33,7 +33,8 @@ endpoint WebWorkerServer {Web::HTML::SerializedEnvironmentSettingsObject outside_settings,Web::HTML::AgentType agent_type,URL::Origin origin,- Web::HTML::EnvironmentId environment_id) =|+ Web::HTML::EnvironmentId environment_id,+ bool has_process_to_itself) =|connect_shared_worker(Web::HTML::TransferDataEncoder message_port,Web::HTML::SerializedEnvironmentSettingsObject outside_settings) =|diff --git a/Services/WebWorker/WorkerHost.cpp b/Services/WebWorker/WorkerHost.cppindex f056c284273..4ab9901a832 100644--- a/Services/WebWorker/WorkerHost.cpp+++ b/Services/WebWorker/WorkerHost.cpp@@ -97,7 +97,7 @@ static Web::HTML::WorkerGlobalScope::Owner relevant_owner_to_add(Web::HTML::Seri}// https://html.spec.whatwg.org/multipage/workers.html#run-a-worker-void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const& outside_settings_snapshot, Web::HTML::RequestCredentials credentials, bool is_shared, URL::Origin origin, Web::HTML::EnvironmentId environment_id)+void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const& outside_settings_snapshot, Web::HTML::RequestCredentials credentials, bool is_shared, URL::Origin origin, Web::HTML::EnvironmentId environment_id, bool has_process_to_itself){m_is_shared = is_shared;@@ -191,7 +191,7 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes: Web::Fetch::Infrastructure::Request::Destination::Worker;// In both cases, let performFetch be the following perform the fetch hook given request, isTopLevel, and processCustomFetchResponse:- auto perform_fetch_function = [inside_settings, outside_settings, worker_global_scope, is_shared](NonnullRefPtr<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {+ auto perform_fetch_function = [inside_settings, outside_settings, worker_global_scope, is_shared, has_process_to_itself](NonnullRefPtr<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {auto& realm = inside_settings->realm();Web::Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input { Web::Fetch::Engine::BodyIntent::ConsumeForCallback };@@ -213,7 +213,7 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes// 3. Fetch request with processResponseConsumeBody set to the following steps given response response and// null, failure, or a byte sequence bodyBytes:- fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function, inside_settings, outside_settings, is_shared](auto response, auto body_bytes) {+ fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function, inside_settings, outside_settings, is_shared, has_process_to_itself](auto response, auto body_bytes) {// 1. Set worker global scope's url to response's URL.worker_global_scope->set_url(response->url().value_or({}));@@ -235,9 +235,11 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes// "concrete". The one chosen is implementation-defined.// AD-HOC: Nothing sets worker global scope's embedder policy. This reads its policy container's embedder// policy instead. See https://github.com/whatwg/html/issues/11316.- // NB: This chooses "concrete", as the UI process does for a browsing context group.+ // NB: This chooses "concrete", as the UI process does for a browsing context group, for a worker that has a+ // process to itself. The response comes too late to move a worker, and a process with anything else in it+ // cannot give this worker what "concrete" stands for.if (Web::HTML::is_compatible_with_cross_origin_isolation(worker_global_scope->policy_container()->embedder_policy.value) && is_shared)- inside_settings->set_agent_cluster_cross_origin_isolation_mode(Web::HTML::CrossOriginIsolationMode::Concrete);+ inside_settings->set_agent_cluster_cross_origin_isolation_mode(has_process_to_itself ? Web::HTML::CrossOriginIsolationMode::Concrete : Web::HTML::CrossOriginIsolationMode::Logical);// 6. If the result of checking a global object's embedder policy with worker global scope, outside settings,// and response is false, then set response to a network error.diff --git a/Services/WebWorker/WorkerHost.h b/Services/WebWorker/WorkerHost.hindex e082028e276..baa605a0afb 100644--- a/Services/WebWorker/WorkerHost.h+++ b/Services/WebWorker/WorkerHost.h@@ -30,7 +30,7 @@ public:explicit WorkerHost(URL::URL url, Web::HTML::WorkerType type, String name);~WorkerHost();- void run(GC::Ref<Web::Page>, Web::HTML::TransferDataEncoder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const&, Web::HTML::RequestCredentials, bool is_shared, URL::Origin origin, Web::HTML::EnvironmentId environment_id);+ void run(GC::Ref<Web::Page>, Web::HTML::TransferDataEncoder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const&, Web::HTML::RequestCredentials, bool is_shared, URL::Origin origin, Web::HTML::EnvironmentId environment_id, bool has_process_to_itself);void connect_shared_worker(Web::HTML::TransferDataEncoder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject);void terminate();diff --git a/Services/WebWorker/WorkerThread.cpp b/Services/WebWorker/WorkerThread.cppindex 74ae31c10dd..3ef17f905a7 100644--- a/Services/WebWorker/WorkerThread.cpp+++ b/Services/WebWorker/WorkerThread.cpp@@ -37,7 +37,7 @@ bool is_worker_thread()return s_is_worker_thread;}-static intptr_t run_worker_thread(IPC::TransportHandle const& worker_handle)+static intptr_t run_worker_thread(IPC::TransportHandle const& worker_handle, Web::HTML::AgentType agent_type){s_is_worker_thread = true;@@ -48,7 +48,7 @@ static intptr_t run_worker_thread(IPC::TransportHandle const& worker_handle)}Core::EventLoop event_loop;- Web::Bindings::initialize_agent(Web::HTML::AgentType::DedicatedWorker);+ Web::Bindings::initialize_agent(agent_type);auto event_loop_reference = Core::EventLoop::current_weak();{@@ -70,10 +70,10 @@ static intptr_t run_worker_thread(IPC::TransportHandle const& worker_handle)return exit_code;}-ErrorOr<void> start_worker_thread(IPC::TransportHandle worker_handle)+ErrorOr<void> start_worker_thread(IPC::TransportHandle worker_handle, Web::HTML::AgentType agent_type){- auto thread = TRY(Threading::Thread::try_create("WebWorker"sv, [worker_handle = move(worker_handle)] {- return run_worker_thread(worker_handle);+ auto thread = TRY(Threading::Thread::try_create("WebWorker"sv, [worker_handle = move(worker_handle), agent_type] {+ return run_worker_thread(worker_handle, agent_type);}));thread->set_stack_size(JS::VM::MINIMUM_THREAD_STACK_SIZE);diff --git a/Services/WebWorker/WorkerThread.h b/Services/WebWorker/WorkerThread.hindex 64365b2f624..720468a9d48 100644--- a/Services/WebWorker/WorkerThread.h+++ b/Services/WebWorker/WorkerThread.h@@ -8,13 +8,14 @@#include <AK/Error.h>#include <LibIPC/Forward.h>+#include <LibWebCommon/HTML/WorkerTypes.h>namespace WebWorker {-// Starts a thread that hosts a dedicated worker agent, which talks to the UI process over the given transport as a worker-// process does. Nothing ever waits for the thread: it ends once its worker has closed, and otherwise with the process. A-// process that exits takes running workers with it, so it has to leave by Core::Process::terminate_immediately().-ErrorOr<void> start_worker_thread(IPC::TransportHandle);+// Starts a thread that hosts a worker agent, which talks to the UI process over the given transport as a worker process+// does. Nothing ever waits for the thread: it ends once its worker has closed, and otherwise with the process. A process+// that exits takes running workers with it, so it has to leave by Core::Process::terminate_immediately().+ErrorOr<void> start_worker_thread(IPC::TransportHandle, Web::HTML::AgentType);// Whether the calling thread is one start_worker_thread() started. This can be asked from a signal handler.bool is_worker_thread();