Truly independent web browser
LibWebView: Host same-origin tabs in one process
Aliaksandr Kalenik662158a110c7
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex df324cb2a44..653760eabcc 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -389,6 +389,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)bool validate_dnssec_locally = false;bool log_all_js_exceptions = false;auto site_isolation_mode = default_site_isolation_mode();+ bool disable_same_origin_tab_sharing = false;Optional<size_t> maximum_site_request_servers;bool disable_http_memory_cache = false;bool disable_http_disk_cache = false;@@ -498,6 +499,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)return true;},});+ args_parser.add_option(disable_same_origin_tab_sharing, "Disable hosting tabs of the same origin in one WebContent process", "disable-same-origin-tab-sharing");args_parser.add_option(disable_http_memory_cache, "Disable HTTP memory cache", "disable-http-memory-cache");args_parser.add_option(disable_http_disk_cache, "Disable HTTP disk cache", "disable-http-disk-cache");args_parser.add_option(maximum_site_request_servers, "How many RequestServers a session may have for the sites it visits, 0 to share one", "max-site-request-servers", 0, "count");@@ -646,6 +648,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments).is_test_mode = enable_test_mode ? IsTestMode::Yes : IsTestMode::No,.log_all_js_exceptions = log_all_js_exceptions ? LogAllJSExceptions::Yes : LogAllJSExceptions::No,.site_isolation_mode = site_isolation_mode,+ .share_process_between_same_origin_tabs = disable_same_origin_tab_sharing ? ShareProcessBetweenSameOriginTabs::No : ShareProcessBetweenSameOriginTabs::Yes,.enable_http_memory_cache = disable_http_memory_cache ? EnableMemoryHTTPCache::No : EnableMemoryHTTPCache::Yes,.expose_experimental_interfaces = expose_experimental_interfaces ? ExposeExperimentalInterfaces::Yes : ExposeExperimentalInterfaces::No,.expose_internals_object = expose_internals_object ? ExposeInternalsObject::Yes : ExposeInternalsObject::No,@@ -1644,6 +1647,13 @@ void Application::crash_compositor_process()m_compositor_client->async_crash();}+bool Application::same_origin_tabs_may_share_a_process() const+{+ // NB: Tests run side by side, and must not take each other's state with them.+ return m_web_content_options.share_process_between_same_origin_tabs == ShareProcessBetweenSameOriginTabs::Yes+ && m_web_content_options.is_test_mode == IsTestMode::No;+}+ErrorOr<NonnullRefPtr<WebContentPage>> Application::open_page_for_new_tab(IsPrivate is_private){RefPtr<WebContentClient> web_content_client;diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex 6a47aacbcd1..2ebf0ddeaa4 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -259,6 +259,7 @@ public:ErrorOr<Core::GeolocationProvider::WatchId, Core::GeolocationError> start_watching_geolocation_position(Core::GeolocationProvider::SuccessCallback on_success, Core::GeolocationProvider::ErrorCallback on_error);void stop_watching_geolocation_position(Core::GeolocationProvider::WatchId);+ bool same_origin_tabs_may_share_a_process() const;ErrorOr<NonnullRefPtr<WebContentPage>> open_page_for_new_tab(IsPrivate);struct ChildFrameWebContentProcess {NonnullRefPtr<WebContentClient> client;diff --git a/Libraries/LibWebView/CanonicalDocument.cpp b/Libraries/LibWebView/CanonicalDocument.cppindex 8c933344256..c55bdf4d142 100644--- a/Libraries/LibWebView/CanonicalDocument.cpp+++ b/Libraries/LibWebView/CanonicalDocument.cpp@@ -68,6 +68,26 @@ Optional<URL::Origin> CanonicalDocument::top_level_origin() constreturn top_level_browsing_context.active_document()->origin();}+bool CanonicalDocument::is_top_level() const+{+ return &m_browsing_context->top_level_browsing_context() == m_browsing_context.ptr();+}++bool CanonicalDocument::may_share_a_process_with_tabs_of_its_origin() const+{+ if (!is_top_level() || m_origin.is_opaque() || m_web_ui_host.has_value() || m_is_local_file_content)+ return false;++ auto const& scheme = m_origin.scheme();+ if (!scheme.has_value() || !scheme->is_one_of("http"sv, "https"sv))+ return false;++ auto const& agent = m_relevant_global_object->agent();+ return !agent.agent_cluster_is_origin_keyed()+ && agent.agent_cluster_cross_origin_isolation_mode() == Web::HTML::CrossOriginIsolationMode::None+ && m_opener_policy.value == Web::HTML::OpenerPolicyValue::UnsafeNone;+}+void CanonicalDocument::set_host(RefPtr<WebContentPage> host){m_host = move(host);@@ -80,6 +100,7 @@ void CanonicalDocument::set_host(RefPtr<WebContentPage> host)auto is_pdf_viewer = !m_origin.is_opaque() && m_origin.scheme() == "resource"sv;if (auto site = URL::Site::serialize_for_partitioning(m_origin); site.has_value() && !is_pdf_viewer)m_host->client().did_host_document_of_site(site.release_value());+ m_host->client().note_hosted_document(*this);}}diff --git a/Libraries/LibWebView/CanonicalDocument.h b/Libraries/LibWebView/CanonicalDocument.hindex 88673396dcd..f15556395e9 100644--- a/Libraries/LibWebView/CanonicalDocument.h+++ b/Libraries/LibWebView/CanonicalDocument.h@@ -93,6 +93,12 @@ public:RefPtr<WebContentPage> const& host() const { return m_host; }void set_host(RefPtr<WebContentPage>);+ bool is_top_level() const;++ // Whether this top-level document may be hosted by one process with the tabs of its origin. A document that asked+ // to be kept apart from others, by its opener policy or by being origin-keyed, may not.+ bool may_share_a_process_with_tabs_of_its_origin() const;+// Whether this is local file content, whose process may read local files: a document created from a file: URL, or// from a blob: URL whose entry local file content added.bool is_local_file_content() const { return m_is_local_file_content; }diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex 76457bcf59a..0dff9d5e02f 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -893,6 +893,17 @@ static Optional<String> origin_of_sandboxed_document(CanonicalNavigable const& nreturn {};}+static RefPtr<WebContentClient> lowest_process_hosting_only_tabs_of(URL::Origin const& origin, BrowsingSession const& session)+{+ RefPtr<WebContentClient> process;+ WebContentClient::for_each_client([&](WebContentClient& client) {+ if (&client.session() == &session && client.is_open() && !client.has_requested_close() && client.has_hosted_only_tabs_of(origin) && (!process || client.child_id() < process->child_id()))+ process = client;+ return IterationDecision::Continue;+ });+ return process;+}+RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(CanonicalDocument const& document, Optional<URL::Origin> const& initiator_origin) const{RefPtr<WebContentPage> page_holding_navigable = parent() ? reporting_page() : top_level_traversable().page_hosting(*this);@@ -918,6 +929,17 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(return nullptr;}+ // A tab goes to a process that has hosted only tabs of its origin, to share what the process holds. Nothing else can+ // reach a browsing context group of one, whereas an opener could make a process it can connect to host the tab. A+ // tab recovering from a crash gets a process of its own, so that it can't take the tabs it joins down with it again.+ if (!parent() && process_holding_navigable && document.may_share_a_process_with_tabs_of_its_origin()+ && document.browsing_context().top_level_browsing_context().group()->browsing_context_set().size() == 1+ && !top_level_traversable().has_page_standing_in_for_lost_document()+ && Application::the().same_origin_tabs_may_share_a_process()) {+ if (auto process = lowest_process_hosting_only_tabs_of(document.origin(), process_holding_navigable->session()))+ return process;+ }+// Any other opaque origin keys an agent cluster of its own, which nothing but the documents it was created from can// address: it goes with the initiator's agent. A traversable's document of an opaque origin that no such agent// hosts, as a file's opened from the browser's UI is, keeps the process of a document of an opaque origin.@@ -934,9 +956,13 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(// A traversable's first document takes the process its initial about:blank came with, unless that document// inherited its creator's origin and shares the creator's process, or a browsing context group switch leaves the- // initial about:blank's group.- if (!parent() && active_document().is_initial_about_blank() && active_document().origin().is_opaque() && !active_browsing_context().is_auxiliary() && &document.browsing_context() == &active_browsing_context())+ // initial about:blank's group. A noopener popup's initial about:blank came with the process of the document that+ // opened it, which a document of another origin stays out of if that process hosts tabs of one origin.+ if (!parent() && active_document().is_initial_about_blank() && active_document().origin().is_opaque() && !active_browsing_context().is_auxiliary() && &document.browsing_context() == &active_browsing_context()) {+ if (process_holding_navigable && process_holding_navigable->has_hosted_only_tabs_of_an_origin_other_than(document.origin()))+ return nullptr;return process_holding_navigable;+ }return nullptr;}diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.hindex 0993eab3475..2f67b3e29d2 100644--- a/Libraries/LibWebView/CanonicalTraversable.h+++ b/Libraries/LibWebView/CanonicalTraversable.h@@ -130,6 +130,7 @@ public:RefPtr<WebContentPage> display_page() const;ErrorOr<NonnullRefPtr<WebContentPage>> obtain_page_to_host_traversable(RefPtr<WebContentClient> host);void set_page_standing_in_for_lost_document(Badge<ViewImplementation>, NonnullRefPtr<WebContentPage>);+ bool has_page_standing_in_for_lost_document() const { return m_page_standing_in_for_lost_document; }void did_activate_document_in(Badge<CanonicalNavigable>, WebContentPage& host);void remove_child_navigables_of(CanonicalNavigable&, CanonicalDocument const&);void did_lose_page(WebContentPage&, WebContentProcessLost);diff --git a/Libraries/LibWebView/Options.h b/Libraries/LibWebView/Options.hindex b460461f5a5..e310b5ee982 100644--- a/Libraries/LibWebView/Options.h+++ b/Libraries/LibWebView/Options.h@@ -148,6 +148,11 @@ enum class EnableMemoryHTTPCache {Yes,};+enum class ShareProcessBetweenSameOriginTabs {+ No,+ Yes,+};+enum class ExposeExperimentalInterfaces {No,Yes,@@ -189,6 +194,7 @@ struct WebContentOptions {IsTestMode is_test_mode { IsTestMode::No };LogAllJSExceptions log_all_js_exceptions { LogAllJSExceptions::No };SiteIsolationMode site_isolation_mode { SiteIsolationMode::IFrame };+ ShareProcessBetweenSameOriginTabs share_process_between_same_origin_tabs { ShareProcessBetweenSameOriginTabs::Yes };EnableMemoryHTTPCache enable_http_memory_cache { EnableMemoryHTTPCache::No };ExposeExperimentalInterfaces expose_experimental_interfaces { ExposeExperimentalInterfaces::No };ExposeInternalsObject expose_internals_object { ExposeInternalsObject::No };diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex 7e4a1eec2cd..432d00c23d3 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -80,6 +80,27 @@ void WebContentClient::close_worker_agent(Web::PageId page_id, Web::HTML::WorkerWorkerAgentManager::the().close_worker_agent(*this, agent_id, owner_token);}+void WebContentClient::note_hosted_document(CanonicalDocument const& document)+{+ // NB: An about:blank document starts out empty, and its origin is that of a document the process already hosts or+ // an opaque one that nothing else can script.+ if (m_has_hosted_more_than_tabs_of_one_origin || Web::HTML::url_matches_about_blank(document.creation_url()))+ return;++ auto const& origin = document.origin();+ if (document.may_share_a_process_with_tabs_of_its_origin()) {+ if (!m_origin_of_hosted_tabs.has_value())+ m_origin_of_hosted_tabs = origin;+ if (m_origin_of_hosted_tabs == origin)+ return;+ } else if (!document.is_top_level() && m_origin_of_hosted_tabs.has_value() && !origin.is_opaque() && origin.is_same_site(*m_origin_of_hosted_tabs)) {+ return;+ }++ m_has_hosted_more_than_tabs_of_one_origin = true;+ m_origin_of_hosted_tabs.clear();+}+bool WebContentClient::hosts_an_environment_with_storage_key(Web::StorageAPI::StorageKey const& storage_key){bool hosts_one = false;diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex db3ca5e7ad8..f9b6de99c8f 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -138,6 +138,11 @@ public:// documents processes of their own, which host nothing else.Optional<String> const& hosted_web_ui() const { return m_hosted_web_ui; }void set_hosted_web_ui(String host) { m_hosted_web_ui = move(host); }+ // A process can take in a tab of an origin only if all it has ever hosted, and so may still hold, is tabs of that+ // origin with their same-site iframes.+ void note_hosted_document(CanonicalDocument const&);+ bool has_hosted_only_tabs_of(URL::Origin const& origin) const { return m_origin_of_hosted_tabs == origin; }+ bool has_hosted_only_tabs_of_an_origin_other_than(URL::Origin const& origin) const { return m_origin_of_hosted_tabs.has_value() && m_origin_of_hosted_tabs != origin; }void register_embedded_page(Web::PageId page_id, CanonicalTraversable&);void unregister_embedded_page(Web::PageId page_id);Optional<Web::PageId> page_id_for_traversable(CanonicalTraversable const&) const;@@ -260,6 +265,8 @@ private:bool m_has_hosted_documents_of_several_sites { false };bool m_has_hosted_local_file_content { false };Optional<String> m_hosted_web_ui;+ Optional<URL::Origin> m_origin_of_hosted_tabs;+ bool m_has_hosted_more_than_tabs_of_one_origin { false };Vector<u64> m_crashed_view_ids;WebContentPage& open_page(Web::PageId, CanonicalTraversable&);