Skip to content

Truly independent web browser

LibWebView: Give a worker the browsing session of its owner

Luke Wilde0f07cda833a3
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 21d48290393..d47c4d7b5bc 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -1093,18 +1093,15 @@ void Application::open_urls_in_new_tabs(ReadonlySpan<URL::URL> urls) const
open_url_in_new_tab(url, Web::HTML::ActivateTab::No);
}
-ErrorOr<NonnullRefPtr<WebContentClient>> Application::create_web_content_client(IsPrivate is_private, Web::PageId initial_page_id, Optional<Web::HTML::CrossProcessId> navigable_to_adopt, Optional<Web::HTML::CrossProcessId> initial_document_state_id, Vector<Web::HTML::RemoteNavigableDescriptor> remote_navigables, Optional<Web::HTML::SessionHistoryEntryDescriptor> canonical_initial_history_entry, Web::HTML::VisibilityState system_visibility_state)
+ErrorOr<NonnullRefPtr<WebContentClient>> Application::create_web_content_client(BrowsingSession& session, Web::PageId initial_page_id, Optional<Web::HTML::CrossProcessId> navigable_to_adopt, Optional<Web::HTML::CrossProcessId> initial_document_state_id, Vector<Web::HTML::RemoteNavigableDescriptor> remote_navigables, Optional<Web::HTML::SessionHistoryEntryDescriptor> canonical_initial_history_entry, Web::HTML::VisibilityState system_visibility_state)
{
- // The client's WebContentClient picks up this same session when it is created.
- auto session = session_for_new_view(is_private);
-
auto cross_process_id_allocator = allocate_cross_process_id_allocator();
auto root_navigable_id = navigable_to_adopt.has_value() ? *navigable_to_adopt : cross_process_id_allocator.allocate();
if (!initial_document_state_id.has_value())
initial_document_state_id = cross_process_id_allocator.allocate();
auto initial_document_id = cross_process_id_allocator.allocate();
- auto client = TRY(WebView::launch_web_content_process(is_private, initial_page_id, root_navigable_id));
+ auto client = TRY(WebView::launch_web_content_process(session, initial_page_id, root_navigable_id));
TRY(Application::the().connect_web_content_to_compositor(*client));
// A tab's first process creates its traversable from this entry. A process hosting a navigable of an existing tab
@@ -1246,11 +1243,6 @@ RefPtr<BrowsingSession> Application::session_for_request_server_client(int clien
return {};
}
-RefPtr<BrowsingSession> Application::existing_session(IsPrivate is_private)
-{
- return is_private == IsPrivate::Yes ? the().m_private_session.strong_ref() : the().m_default_session;
-}
-
WebDriverBrowserConnection* Application::webdriver_browser_connection()
{
if (!m_browser_options.webdriver_browser_endpoint.has_value() || m_webdriver_browser_connection_failed)
@@ -1547,27 +1539,27 @@ ErrorOr<NonnullRefPtr<WebContentPage>> Application::open_page_for_new_tab(IsPriv
RefPtr<WebContentClient> web_content_client;
if (is_private == IsPrivate::Yes) {
- web_content_client = TRY(create_web_content_client(IsPrivate::Yes, allocate_page_id()));
+ web_content_client = TRY(create_web_content_client(session_for_new_view(IsPrivate::Yes), allocate_page_id()));
} else if (m_spare_web_content_process) {
web_content_client = m_spare_web_content_process.release_nonnull();
launch_spare_web_content_process();
} else {
launch_spare_web_content_process();
- web_content_client = TRY(create_web_content_client(IsPrivate::No, allocate_page_id()));
+ web_content_client = TRY(create_web_content_client(default_session(), allocate_page_id()));
}
return web_content_client->open_initial_page_for_new_top_level_traversable();
}
-ErrorOr<NonnullRefPtr<WebContentClient>> Application::launch_web_content_process_without_page(IsPrivate is_private)
+ErrorOr<NonnullRefPtr<WebContentClient>> Application::launch_web_content_process_without_page(BrowsingSession& session)
{
- return create_web_content_client(is_private, allocate_page_id());
+ return create_web_content_client(session, 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();
auto initial_document_state_id = initial_history_entry.document_state.id;
- auto client = TRY(create_web_content_client(is_private, page_id, root_navigable_id, initial_document_state_id, move(remote_navigables), move(initial_history_entry), system_visibility_state));
+ auto client = TRY(create_web_content_client(session_for_new_view(is_private), page_id, root_navigable_id, initial_document_state_id, move(remote_navigables), move(initial_history_entry), system_visibility_state));
return ChildFrameWebContentProcess {
.client = move(client),
.page_id = page_id,
@@ -1595,7 +1587,7 @@ void Application::launch_spare_web_content_process()
Core::deferred_invoke([this]() {
m_has_queued_task_to_launch_spare_web_content_process = false;
- auto web_content_client = create_web_content_client(IsPrivate::No, allocate_page_id());
+ auto web_content_client = create_web_content_client(default_session(), allocate_page_id());
if (web_content_client.is_error()) {
dbgln("Unable to create spare web content client: {}", web_content_client.error());
return;
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 246f57c0256..646495aa81e 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -199,8 +199,6 @@ public:
static NonnullRefPtr<BrowsingSession> session_for_new_view(IsPrivate);
- static RefPtr<BrowsingSession> existing_session(IsPrivate);
-
// Whether the windows and tabs of this run are kept for the next one. A headless run, a browser that WebDriver drives,
// and one that logs process statistics never join a browser already running on their profile, so they keep none.
bool persists_sessions() const { return m_persists_sessions; }
@@ -238,7 +236,7 @@ public:
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<NonnullRefPtr<WebContentClient>> launch_web_content_process_without_page(BrowsingSession&);
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();
@@ -419,7 +417,7 @@ private:
ErrorOr<void> create_profile(Optional<StringView> profile_name, Optional<StringView> profile_path, bool force_new_process, bool temporary_profile);
void create_mach_port_server(ByteString const& profile_identity);
void update_website_color_scheme();
- ErrorOr<NonnullRefPtr<WebContentClient>> create_web_content_client(IsPrivate, Web::PageId initial_page_id, Optional<Web::HTML::CrossProcessId> navigable_to_adopt = {}, Optional<Web::HTML::CrossProcessId> initial_document_state_id = {}, Vector<Web::HTML::RemoteNavigableDescriptor> remote_navigables = {}, Optional<Web::HTML::SessionHistoryEntryDescriptor> canonical_initial_history_entry = {}, Web::HTML::VisibilityState system_visibility_state = Web::HTML::VisibilityState::Hidden);
+ ErrorOr<NonnullRefPtr<WebContentClient>> create_web_content_client(BrowsingSession&, Web::PageId initial_page_id, Optional<Web::HTML::CrossProcessId> navigable_to_adopt = {}, Optional<Web::HTML::CrossProcessId> initial_document_state_id = {}, Vector<Web::HTML::RemoteNavigableDescriptor> remote_navigables = {}, Optional<Web::HTML::SessionHistoryEntryDescriptor> canonical_initial_history_entry = {}, Web::HTML::VisibilityState system_visibility_state = Web::HTML::VisibilityState::Hidden);
ErrorOr<void> launch_services();
void launch_spare_web_content_process();
diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cpp
index de231f0f3d1..4cfad72cd30 100644
--- a/Libraries/LibWebView/HelperProcess.cpp
+++ b/Libraries/LibWebView/HelperProcess.cpp
@@ -332,7 +332,7 @@ static ErrorOr<void> connect_to_font_service(ClientType& client)
return {};
}
-ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(IsPrivate is_private, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id)
+ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(BrowsingSession& session, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id)
{
auto const& browser_options = WebView::Application::browser_options();
auto const& web_content_options = WebView::Application::web_content_options();
@@ -383,7 +383,7 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(IsP
arguments.append(server.value());
}
- auto client = TRY(launch_server_process<WebView::WebContentClient>("WebContent"sv, move(arguments), is_private, initial_page_id, root_navigable_id));
+ auto client = TRY(launch_server_process<WebView::WebContentClient>("WebContent"sv, move(arguments), session, initial_page_id, root_navigable_id));
TRY(connect_to_font_service(*client));
// The test-only messages live on their own endpoint pair, over a transport that only exists in test mode.
diff --git a/Libraries/LibWebView/HelperProcess.h b/Libraries/LibWebView/HelperProcess.h
index 37c562ca485..866fa9c2834 100644
--- a/Libraries/LibWebView/HelperProcess.h
+++ b/Libraries/LibWebView/HelperProcess.h
@@ -22,7 +22,7 @@
namespace WebView {
-WEBVIEW_API ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(IsPrivate, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id);
+WEBVIEW_API ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(BrowsingSession&, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id);
WEBVIEW_API ErrorOr<NonnullRefPtr<MediaClient::Client>> launch_media_server_process(u64 owner_child_id);
WEBVIEW_API ErrorOr<NonnullRefPtr<AudioServerControlClient>> launch_audio_server_process(u64 owner_child_id);
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 3bbd808cfbd..63e5c97dda0 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -185,10 +185,10 @@ static constexpr auto detached_page_close_timeout_ms = 1000;
static constexpr auto close_server_exit_timeout_ms = 5000;
static constexpr auto detached_page_forced_exit_timeout_ms = detached_page_close_timeout_ms + close_server_exit_timeout_ms;
-WebContentClient::WebContentClient(NonnullOwnPtr<IPC::Transport> transport, IsPrivate is_private, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id)
+WebContentClient::WebContentClient(NonnullOwnPtr<IPC::Transport> transport, BrowsingSession& session, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id)
: IPC::ConnectionToServer<WebContentClientEndpoint, WebContentServerEndpoint, WebContentClientPageRoutingStub>(*this, move(transport))
- , m_is_private(is_private)
- , m_session(Application::existing_session(is_private))
+ , m_is_private(session.is_private())
+ , m_session(session)
, m_request_server_site_bindings([this](RequestServerClientConnection& connection, Optional<Utf16String> const& top_level_site) {
async_connect_to_request_server(move(connection.handle), connection.client_id, top_level_site);
})
@@ -197,7 +197,6 @@ WebContentClient::WebContentClient(NonnullOwnPtr<IPC::Transport> transport, IsPr
, m_audio_tab_id(allocate_audio_tab_id())
{
VERIFY(initial_page_id > 0);
- VERIFY(m_session);
clients().set(this);
}
diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.h
index c1e2d43ee06..5e37f843a0f 100644
--- a/Libraries/LibWebView/WebContentClient.h
+++ b/Libraries/LibWebView/WebContentClient.h
@@ -94,7 +94,7 @@ public:
virtual void did_retain_blob_url_token(Web::HTML::CrossProcessId navigable_id, URL::BlobURLEntry::Token token) override;
virtual Messages::WebContentClient::DidRequestBlobUrlEntryResponse did_request_blob_url_entry(Utf16String url, Optional<URL::BlobURLEntry::Token> token) override;
- WebContentClient(NonnullOwnPtr<IPC::Transport>, IsPrivate, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id);
+ WebContentClient(NonnullOwnPtr<IPC::Transport>, BrowsingSession&, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id);
~WebContentClient();
IsPrivate is_private() const { return m_is_private; }
diff --git a/Libraries/LibWebView/WebWorkerClient.cpp b/Libraries/LibWebView/WebWorkerClient.cpp
index abe5edf658f..50607c7bfa9 100644
--- a/Libraries/LibWebView/WebWorkerClient.cpp
+++ b/Libraries/LibWebView/WebWorkerClient.cpp
@@ -21,17 +21,16 @@
namespace WebView {
-WebWorkerClient::WebWorkerClient(NonnullOwnPtr<IPC::Transport> transport, IsPrivate is_private, Web::HTML::WorkerAgentId agent_id)
+WebWorkerClient::WebWorkerClient(NonnullOwnPtr<IPC::Transport> transport, BrowsingSession& session, Web::HTML::WorkerAgentId agent_id)
: IPC::ConnectionToServer<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(transport))
- , m_is_private(is_private)
+ , m_is_private(session.is_private())
+ , m_session(session.make_weak_ptr())
, m_request_server_site_bindings([this](RequestServerClientConnection& connection, Optional<Utf16String> const& top_level_site) {
async_connect_to_request_server(move(connection.handle), connection.client_id, top_level_site);
})
, m_agent_id(agent_id)
, m_audio_tab_id(allocate_audio_tab_id())
{
- if (auto session = Application::existing_session(is_private))
- m_session = session->make_weak_ptr();
}
WebWorkerClient::~WebWorkerClient()
diff --git a/Libraries/LibWebView/WebWorkerClient.h b/Libraries/LibWebView/WebWorkerClient.h
index d975bdfdb45..07a86f79fb6 100644
--- a/Libraries/LibWebView/WebWorkerClient.h
+++ b/Libraries/LibWebView/WebWorkerClient.h
@@ -34,7 +34,7 @@ class WEBVIEW_API WebWorkerClient final
C_OBJECT_ABSTRACT(WebWorkerClient);
public:
- WebWorkerClient(NonnullOwnPtr<IPC::Transport>, IsPrivate, Web::HTML::WorkerAgentId agent_id);
+ WebWorkerClient(NonnullOwnPtr<IPC::Transport>, BrowsingSession&, Web::HTML::WorkerAgentId agent_id);
~WebWorkerClient();
RefPtr<BrowsingSession> session() const { return m_session.strong_ref(); }
diff --git a/Libraries/LibWebView/WorkerAgentManager.cpp b/Libraries/LibWebView/WorkerAgentManager.cpp
index 1c830661284..a755630a999 100644
--- a/Libraries/LibWebView/WorkerAgentManager.cpp
+++ b/Libraries/LibWebView/WorkerAgentManager.cpp
@@ -137,12 +137,12 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(We
return IPC::Transport::create_paired();
}());
if (host_has_only_this_worker) {
- host = TRY(Application::the().launch_web_content_process_without_page(is_private));
+ host = TRY(Application::the().launch_web_content_process_without_page(owner_client.session()));
host->async_start_worker_thread(move(transport.remote_handle), request.agent_type);
} else {
thread_transport = move(transport.remote_handle);
}
- return create_worker_client(move(transport.local), is_private, agent_id, *host, owner_client.request_server_site_bindings());
+ return create_worker_client(move(transport.local), agent_id, *host, owner_client.request_server_site_bindings());
}();
// A worker the system cannot give a thread fails to load, as one whose script cannot be fetched does.
if (client_or_error.is_error()) {
@@ -231,7 +231,7 @@ void WorkerAgentManager::register_dedicated_worker(DedicatedWorkerOwner const& o
IPC::ScopedSharedIOThread shared_io_thread;
return worker_transport.create_transport();
}());
- return create_worker_client(move(transport), owner.is_private, 0, owner.process, owner.request_server_site_bindings);
+ return create_worker_client(move(transport), 0, owner.process, owner.request_server_site_bindings);
}();
if (client_or_error.is_error()) {
dbgln("Unable to start a dedicated worker: {}", client_or_error.error());
@@ -265,22 +265,20 @@ bool WorkerAgentManager::has_reached_worker_limit(IsPrivate is_private, Canonica
return worker_count_of_top_level_site >= Web::HTML::maximum_worker_count;
}
-ErrorOr<NonnullRefPtr<WebWorkerClient>> WorkerAgentManager::create_worker_client(NonnullOwnPtr<IPC::Transport> transport, IsPrivate is_private, Web::HTML::WorkerAgentId agent_id, WebContentClient& host, RequestServerSiteBindings const& owner_request_server_site_bindings)
+ErrorOr<NonnullRefPtr<WebWorkerClient>> WorkerAgentManager::create_worker_client(NonnullOwnPtr<IPC::Transport> transport, Web::HTML::WorkerAgentId agent_id, WebContentClient& host, RequestServerSiteBindings const& owner_request_server_site_bindings)
{
- auto client = TRY(try_make_ref_counted<WebWorkerClient>(move(transport), is_private, agent_id));
+ // A worker belongs to the browsing session of the process its thread runs in.
+ auto& session = host.session();
+ auto client = TRY(try_make_ref_counted<WebWorkerClient>(move(transport), session, agent_id));
client->set_host(host);
- // The worker's RequestServer client uses the cookies of the session the worker itself belongs to.
- auto session = client->session();
- if (!session)
- session = Application::session_for_new_view(is_private);
// The worker makes its requests for the sites its owner makes them for.
client->request_server_site_bindings().bind_sites_of(owner_request_server_site_bindings);
// The worker's requests go through the RequestServers of its owner's sites.
- TRY(client->request_server_site_bindings().connect(*session));
+ TRY(client->request_server_site_bindings().connect(session));
client->async_set_site_compatibility_data(Application::the().site_compatibility_data());
- client->async_set_permission_snapshot(session->permission_manager->snapshot_for({}));
+ client->async_set_permission_snapshot(session.permission_manager->snapshot_for({}));
// The User-Agent string, the navigator compatibility mode, and the preferred languages live in this process.
// Pages get them through their view; a worker gets the same values here, so its navigator never contradicts
// the page's.
diff --git a/Libraries/LibWebView/WorkerAgentManager.h b/Libraries/LibWebView/WorkerAgentManager.h
index cd1c75ae134..27642d19651 100644
--- a/Libraries/LibWebView/WorkerAgentManager.h
+++ b/Libraries/LibWebView/WorkerAgentManager.h
@@ -117,7 +117,7 @@ private:
static bool owner_process_may_host_shared_worker(Owner const&, CanonicalEnvironmentSettingsObject const& outside_settings);
static bool page_environment_has_cross_site_ancestor(WebContentClient&, Web::PageId, CanonicalEnvironmentSettingsObject const&);
bool has_reached_worker_limit(IsPrivate, CanonicalEnvironmentSettingsObject const& outside_settings) const;
- ErrorOr<NonnullRefPtr<WebWorkerClient>> create_worker_client(NonnullOwnPtr<IPC::Transport>, IsPrivate, Web::HTML::WorkerAgentId, WebContentClient& host, RequestServerSiteBindings const& owner_request_server_site_bindings);
+ ErrorOr<NonnullRefPtr<WebWorkerClient>> create_worker_client(NonnullOwnPtr<IPC::Transport>, Web::HTML::WorkerAgentId, WebContentClient& host, RequestServerSiteBindings const& owner_request_server_site_bindings);
static void start_worker(WebWorkerClient&, Web::HTML::WorkerAgentStartRequest, CanonicalEnvironmentSettingsObject const& outside_settings, bool has_cross_site_ancestor, bool owner_may_read_local_files, bool has_process_to_itself);
void notify_worker_script_load_failure(Owner const&);
diff --git a/Tests/LibWebView/TestBrowserHistoryTraversal.cpp b/Tests/LibWebView/TestBrowserHistoryTraversal.cpp
index d2d28467579..d2d0a2827f5 100644
--- a/Tests/LibWebView/TestBrowserHistoryTraversal.cpp
+++ b/Tests/LibWebView/TestBrowserHistoryTraversal.cpp
@@ -353,7 +353,7 @@ TEST_CASE(browser_history_traversal)
{
auto transport = TRY_OR_FAIL(IPC::Transport::create_paired());
auto initial_page_id = app->allocate_page_id();
- auto initial_client = adopt_ref(*new WebView::WebContentClient(move(transport.local), client.is_private(), initial_page_id, app->allocate_ui_process_cross_process_id()));
+ auto initial_client = adopt_ref(*new WebView::WebContentClient(move(transport.local), client.session(), initial_page_id, app->allocate_ui_process_cross_process_id()));
VERIFY(!initial_client->page(initial_page_id));
auto& initial_stub = static_cast<WebContentClientStub&>(*initial_client);
VERIFY(initial_stub.did_request_cookie(initial_page_id, {}, cookie_url, HTTP::Cookie::Source::Http).cookie().cookie == "page-lifecycle=preserved"sv);
diff --git a/Tests/LibWebView/TestRequestServerClientSessions.cpp b/Tests/LibWebView/TestRequestServerClientSessions.cpp
index 06e541b97b9..d2f82a8dc49 100644
--- a/Tests/LibWebView/TestRequestServerClientSessions.cpp
+++ b/Tests/LibWebView/TestRequestServerClientSessions.cpp
@@ -12,12 +12,15 @@
#include <LibRequests/RequestClient.h>
#include <LibRequests/RequestControlClient.h>
#include <LibTest/TestCase.h>
+#include <LibURL/Parser.h>
#include <LibWebView/Application.h>
#include <LibWebView/BrowsingSession.h>
#include <LibWebView/HeadlessWebView.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/ProcessManager.h>
+#include <LibWebView/RequestServerSiteBindings.h>
#include <LibWebView/Utilities.h>
+#include <LibWebView/WorkerAgentManager.h>
namespace {
@@ -133,5 +136,46 @@ TEST_CASE(request_server_client_sessions)
VERIFY(app->session_for_request_server_client(disconnecting_client));
s_clients.clear();
Core::EventLoop::current().spin_until([&] { return !app->session_for_request_server_client(disconnecting_client); });
+
+ // A worker uses the cookies of the browsing session of the process it runs in, also one that a page's worker
+ // starts after the page's private session was restarted, whether or not the next private session has begun.
+ auto worker_owner_view = WebView::HeadlessWebView::create(theme, { 800, 600 }, WebView::IsPrivate::Yes);
+ worker_owner_view->load(URL::Parser::basic_parse("data:text/html,<title>workers</title>"sv).release_value());
+ Core::EventLoop::current().spin_until([&] { return worker_owner_view->title() == "workers"sv; });
+ auto& worker_owner_session = worker_owner_view->page().client().session();
+ worker_owner_view->run_javascript(R"js(
+ const script = `
+ const nestedWorkers = [];
+ onmessage = () => {
+ const nestedWorker = new Worker("data:text/javascript,postMessage(0)");
+ nestedWorker.onmessage = () => postMessage(nestedWorkers.length);
+ nestedWorkers.push(nestedWorker);
+ };
+ postMessage(0);
+ `;
+ window.worker = new Worker(`data:text/javascript,${encodeURIComponent(script)}`);
+ worker.onmessage = event => document.title = `nested workers: ${event.data}`;
+ )js"_string);
+ auto wait_for_nested_workers = [&](size_t count) {
+ auto title = ByteString::formatted("nested workers: {}", count);
+ Core::EventLoop::current().spin_until([&] { return worker_owner_view->title() == title.view(); });
+ };
+ wait_for_nested_workers(0);
+
+ app->reset_private_browsing_session();
+ worker_owner_view->run_javascript("worker.postMessage(null);"_string);
+ wait_for_nested_workers(1);
+ auto next_private_view = WebView::HeadlessWebView::create(theme, { 800, 600 }, WebView::IsPrivate::Yes);
+ VERIFY(&next_private_view->page().client().session() != &worker_owner_session);
+ worker_owner_view->run_javascript("worker.postMessage(null);"_string);
+ wait_for_nested_workers(2);
+
+ size_t worker_count = 0;
+ WebView::WorkerAgentManager::the().for_each_request_server_site_bindings([&](WebView::RequestServerSiteBindings& bindings) {
+ VERIFY(app->session_for_request_server_client(*bindings.client_id()).ptr() == &worker_owner_session);
+ ++worker_count;
+ return IterationDecision::Continue;
+ });
+ VERIFY(worker_count == 3);
outln("PASS: RequestServer clients use the cookies of the browsing session they were created for");
}
diff --git a/Tests/LibWebView/TestSharedWorkerHost.cpp b/Tests/LibWebView/TestSharedWorkerHost.cpp
index c2ff75f052b..e79b119bda0 100644
--- a/Tests/LibWebView/TestSharedWorkerHost.cpp
+++ b/Tests/LibWebView/TestSharedWorkerHost.cpp
@@ -402,6 +402,30 @@ static void cross_origin_isolated_owner_gets_a_process_without_a_page_for_its_wo
browser.expect_clean_exit(*host_pid);
}
+static void process_without_a_page_for_a_worker_has_the_session_of_its_owner(Browser& browser)
+{
+ auto tab = browser.open(browser.server().url("127.0.0.1"sv, "/isolated-page?name=isolated-private&script=/isolated-worker.js"sv), WebView::IsPrivate::Yes);
+ auto& session = tab->page().client().session();
+
+ // The page starts another worker once the private session it belongs to has been restarted.
+ WebView::Application::the().reset_private_browsing_session();
+ tab->run_javascript("window.laterWorker = new SharedWorker('/isolated-worker.js', { name: 'isolated-private-later' });"_string);
+ Core::EventLoop::current().spin_until([] { return worker_count() == 2; });
+
+ size_t host_count = 0;
+ WebView::WebContentClient::for_each_client([&](WebView::WebContentClient& client) {
+ if (WebView::WorkerAgentManager::the().hosts_shared_worker(client)) {
+ EXPECT_EQ(&client.session(), &session);
+ ++host_count;
+ }
+ return IterationDecision::Continue;
+ });
+ EXPECT_EQ(host_count, 2u);
+
+ browser.close(move(tab));
+ browser.wait_for_no_workers();
+}
+
TEST_CASE(shared_worker_host)
{
Test::HeadlessTestEnvironment environment { "TestSharedWorkerHost"sv, 120 };
@@ -416,6 +440,7 @@ TEST_CASE(shared_worker_host)
same_url_stays_apart_in_private_and_normal_sessions(browser);
cross_site_pages_never_share_a_process(browser);
cross_origin_isolated_owner_gets_a_process_without_a_page_for_its_worker(browser);
+ process_without_a_page_for_a_worker_has_the_session_of_its_owner(browser);
extended_lifetime_ends_with_the_private_session(browser);
extended_lifetime_outlives_the_last_tab(browser);
}
diff --git a/Tests/LibWebView/TestWebWorkerClient.cpp b/Tests/LibWebView/TestWebWorkerClient.cpp
index bb7f2c559cd..8b4a0bca120 100644
--- a/Tests/LibWebView/TestWebWorkerClient.cpp
+++ b/Tests/LibWebView/TestWebWorkerClient.cpp
@@ -7,25 +7,17 @@
#include <LibCore/EventLoop.h>
#include <LibIPC/Transport.h>
#include <LibTest/TestCase.h>
-#include <LibWebView/Application.h>
+#include <LibWebView/BrowsingSession.h>
#include <LibWebView/WebWorkerClient.h>
-namespace {
-
-class SessionlessApplication final : public WebView::Application {
-public:
- SessionlessApplication() = default;
-};
-
-}
-
TEST_CASE(every_host_is_a_known_hsts_host_to_a_worker_without_a_browsing_session)
{
Core::EventLoop event_loop;
- SessionlessApplication application;
+ RefPtr<WebView::BrowsingSession> session = WebView::BrowsingSession::create(WebView::IsPrivate::Yes);
auto pair = MUST(IPC::Transport::create_paired());
- auto client = make_ref_counted<WebView::WebWorkerClient>(move(pair.local), WebView::IsPrivate::Yes, 1);
+ auto client = make_ref_counted<WebView::WebWorkerClient>(move(pair.local), *session, 1);
+ session = nullptr;
EXPECT(!client->session());
EXPECT(client->did_is_known_hsts_host("example.test"_string).result());
}