Skip to content

Truly independent web browser

LibWebView+Base: Let the user choose which tabs share a process

Andreas Kling4403c4887577
diff --git a/Base/res/ladybird/about-pages/settings.html b/Base/res/ladybird/about-pages/settings.html
index 9d08b6ebd64..416c1963b45 100644
--- a/Base/res/ladybird/about-pages/settings.html
+++ b/Base/res/ladybird/about-pages/settings.html
@@ -1241,6 +1241,21 @@
<option value="480">After 8 hours</option>
</select>
</div>
+ <div class="card-group inline-container control-row">
+ <label for="tab-processes">
+ Let tabs share a process
+ <p class="description">
+ Tabs that share a process use less memory, but if one of them crashes, the others
+ crash with it. Tabs of the same subdomain are on mail.example.com, and tabs of the
+ same site are anywhere on example.com.
+ </p>
+ </label>
+ <select id="tab-processes">
+ <option value="perTab">Never</option>
+ <option value="perOrigin">Same subdomain</option>
+ <option value="perSite">Same site</option>
+ </select>
+ </div>
</div>
</div>
</div>
diff --git a/Base/res/ladybird/about-pages/settings/tabs.js b/Base/res/ladybird/about-pages/settings/tabs.js
index 49114f7d3bb..a9d41841562 100644
--- a/Base/res/ladybird/about-pages/settings/tabs.js
+++ b/Base/res/ladybird/about-pages/settings/tabs.js
@@ -4,6 +4,7 @@ const verticalTabsExpandOnHover = document.querySelector("#vertical-tabs-expand-
const verticalTabsPosition = document.querySelector("#vertical-tabs-position");
const showBookmarksBar = document.querySelector("#show-bookmarks-bar");
const discardHiddenTabsAfter = document.querySelector("#discard-hidden-tabs-after");
+const tabProcesses = document.querySelector("#tab-processes");
let TAB_SETTINGS = {};
@@ -52,6 +53,7 @@ const loadSettings = settings => {
discardHiddenTabsAfter.add(new Option(`After ${discardMinutes} minutes`, discardMinutes));
}
discardHiddenTabsAfter.value = discardMinutes;
+ tabProcesses.value = TAB_SETTINGS.tabProcesses || "perOrigin";
updateVerticalTabsDependentSettings();
};
@@ -84,6 +86,11 @@ discardHiddenTabsAfter.addEventListener("change", () => {
saveTabSettings();
});
+tabProcesses.addEventListener("change", () => {
+ TAB_SETTINGS.tabProcesses = tabProcesses.value;
+ saveTabSettings();
+});
+
document.addEventListener("WebUIMessage", event => {
if (event.detail.name === "loadFeatures") {
loadFeatures(event.detail.data);
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 75fb5ccc4f8..1294b1f84cf 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -1730,11 +1730,12 @@ void Application::crash_compositor_process()
m_compositor_client->async_crash();
}
-bool Application::same_origin_tabs_may_share_a_process() const
+bool Application::tabs_may_share_processes() 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;
+ && m_web_content_options.is_test_mode == IsTestMode::No
+ && m_settings->tab_settings().tab_processes != TabProcesses::PerTab;
}
ErrorOr<NonnullRefPtr<WebContentPage>> Application::open_page_for_new_tab(IsPrivate is_private)
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index dff6b3fac7b..cd069b83ab2 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -242,7 +242,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;
+ bool tabs_may_share_processes() const;
ErrorOr<NonnullRefPtr<WebContentPage>> open_page_for_new_tab(IsPrivate);
struct ChildFrameWebContentProcess {
NonnullRefPtr<WebContentClient> client;
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index cbdf8afa032..7175486ef3b 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -929,14 +929,14 @@ 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 that are origin-keyed just as it is, 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.
+ // A tab goes to a process that has hosted only tabs of its origin that are origin-keyed just as it is, or of its
+ // site if the user chose so, 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()) {
+ && Application::the().tabs_may_share_processes()) {
if (auto process = lowest_process_hosting_only_tabs_like(document, process_holding_navigable->session()))
return process;
}
diff --git a/Libraries/LibWebView/Settings.cpp b/Libraries/LibWebView/Settings.cpp
index 41aca49bcfb..6cadb1144c9 100644
--- a/Libraries/LibWebView/Settings.cpp
+++ b/Libraries/LibWebView/Settings.cpp
@@ -62,6 +62,7 @@ static constexpr auto VERTICAL_TABS_POSITION_KEY = "verticalTabsPosition"sv;
static constexpr auto VERTICAL_TABS_EXPANDED_WIDTH_KEY = "verticalTabsExpandedWidth"sv;
static constexpr auto EVERYTHING_IN_SIDEBAR_KEY = "everythingInSidebar"sv;
static constexpr auto DISCARD_HIDDEN_TABS_AFTER_MINUTES_KEY = "discardHiddenTabsAfterMinutes"sv;
+static constexpr auto TAB_PROCESSES_KEY = "tabProcesses"sv;
static constexpr auto BROWSING_BEHAVIOR_KEY = "browsingBehavior"sv;
static constexpr auto ENABLE_AUTOSCROLL_KEY = "enableAutoscroll"sv;
@@ -347,6 +348,30 @@ static constexpr Optional<VerticalTabsPosition> vertical_tabs_position_from_stri
return {};
}
+static constexpr StringView tab_processes_to_string(TabProcesses tab_processes)
+{
+ switch (tab_processes) {
+ case TabProcesses::PerTab:
+ return "perTab"sv;
+ case TabProcesses::PerOrigin:
+ return "perOrigin"sv;
+ case TabProcesses::PerSite:
+ return "perSite"sv;
+ }
+ VERIFY_NOT_REACHED();
+}
+
+static constexpr Optional<TabProcesses> tab_processes_from_string(StringView tab_processes)
+{
+ if (tab_processes == "perTab"sv)
+ return TabProcesses::PerTab;
+ if (tab_processes == "perOrigin"sv)
+ return TabProcesses::PerOrigin;
+ if (tab_processes == "perSite"sv)
+ return TabProcesses::PerSite;
+ return {};
+}
+
static bool json_value_matches_type(JsonValue const& value, JsonValue::Type type)
{
switch (type) {
@@ -842,6 +867,7 @@ JsonValue Settings::serialize_json() const
tab_settings.set(VERTICAL_TABS_EXPANDED_WIDTH_KEY, *m_tab_settings.vertical_tabs_expanded_width);
tab_settings.set(EVERYTHING_IN_SIDEBAR_KEY, m_tab_settings.everything_in_sidebar);
tab_settings.set(DISCARD_HIDDEN_TABS_AFTER_MINUTES_KEY, m_tab_settings.discard_hidden_tabs_after_minutes);
+ tab_settings.set(TAB_PROCESSES_KEY, tab_processes_to_string(m_tab_settings.tab_processes));
settings.set(TAB_SETTINGS_KEY, move(tab_settings));
JsonObject browsing_behavior;
@@ -1265,6 +1291,10 @@ TabSettings Settings::parse_tab_settings(JsonValue const& settings)
tab_settings.everything_in_sidebar = *everything_in_sidebar;
if (auto minutes = settings.as_object().get_integer<u32>(DISCARD_HIDDEN_TABS_AFTER_MINUTES_KEY); minutes.has_value())
tab_settings.discard_hidden_tabs_after_minutes = *minutes;
+ if (auto tab_processes = settings.as_object().get_string(TAB_PROCESSES_KEY); tab_processes.has_value()) {
+ if (auto parsed_tab_processes = tab_processes_from_string(*tab_processes); parsed_tab_processes.has_value())
+ tab_settings.tab_processes = *parsed_tab_processes;
+ }
return tab_settings;
}
diff --git a/Libraries/LibWebView/Settings.h b/Libraries/LibWebView/Settings.h
index 622ceb3627a..ff82a1f5999 100644
--- a/Libraries/LibWebView/Settings.h
+++ b/Libraries/LibWebView/Settings.h
@@ -85,6 +85,13 @@ struct ContentSettings {
HashMap<String, bool> force_dark_per_host;
};
+// Which tabs may share a WebContent process. Sharing one uses less memory, but a crash then takes all its tabs down.
+enum class TabProcesses : u8 {
+ PerTab,
+ PerOrigin,
+ PerSite,
+};
+
enum class VerticalTabsPosition : u8 {
Left,
Right,
@@ -102,6 +109,7 @@ struct TabSettings {
// A tab that was hidden for this many minutes is discarded, unless it does something the user would notice. Zero
// means that tabs are kept however long they are hidden.
u32 discard_hidden_tabs_after_minutes { 120 };
+ TabProcesses tab_processes { TabProcesses::PerOrigin };
};
struct SearchEngineSettings {
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 52f8e1eaaf0..175d6139d8e 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -92,8 +92,11 @@ void WebContentClient::note_hosted_document(CanonicalDocument const& document)
m_origin_of_hosted_tabs = origin;
m_hosted_tabs_are_origin_keyed = document.relevant_global_object().agent().agent_cluster_is_origin_keyed();
}
- if (has_hosted_only_tabs_like(document))
+ if (has_hosted_only_tabs_of_the_site_of(document)) {
+ if (origin != *m_origin_of_hosted_tabs)
+ m_has_hosted_tabs_of_more_than_one_origin_of_a_site = true;
return;
+ }
} else if (!document.is_top_level() && m_origin_of_hosted_tabs.has_value()) {
// NB: A data: URL iframe is hosted in the process of the document that made it, and holds only what that
// document already had.
@@ -109,17 +112,39 @@ void WebContentClient::note_hosted_document(CanonicalDocument const& document)
m_origin_of_hosted_tabs.clear();
}
+static bool tabs_of_a_site_share_processes()
+{
+ return Application::settings().tab_settings().tab_processes == TabProcesses::PerSite;
+}
+
bool WebContentClient::has_hosted_only_tabs_like(CanonicalDocument const& document) const
{
- return m_origin_of_hosted_tabs == document.origin()
+ if (tabs_of_a_site_share_processes())
+ return has_hosted_only_tabs_of_the_site_of(document);
+ return !m_has_hosted_tabs_of_more_than_one_origin_of_a_site && m_origin_of_hosted_tabs == document.origin()
&& m_hosted_tabs_are_origin_keyed == document.relevant_global_object().agent().agent_cluster_is_origin_keyed();
}
bool WebContentClient::has_hosted_only_tabs_unlike(CanonicalDocument const& document) const
{
+ // NB: The user may want no tabs to share a process, which makes every other tab unlike the tabs of a process.
+ if (Application::settings().tab_settings().tab_processes == TabProcesses::PerTab)
+ return m_origin_of_hosted_tabs.has_value();
+ if (tabs_of_a_site_share_processes())
+ return m_origin_of_hosted_tabs.has_value() && !has_hosted_only_tabs_of_the_site_of(document);
return m_origin_of_hosted_tabs.has_value() && !has_hosted_only_tabs_like(document);
}
+// NB: An origin-keyed tab keeps apart from the other origins of its site, so its site is just its origin.
+bool WebContentClient::has_hosted_only_tabs_of_the_site_of(CanonicalDocument const& document) const
+{
+ if (!m_origin_of_hosted_tabs.has_value() || m_hosted_tabs_are_origin_keyed != document.relevant_global_object().agent().agent_cluster_is_origin_keyed())
+ return false;
+ if (m_hosted_tabs_are_origin_keyed)
+ return *m_origin_of_hosted_tabs == document.origin();
+ return m_origin_of_hosted_tabs->is_same_site(document.origin());
+}
+
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.h
index 56c15383096..48a1ae4d202 100644
--- a/Libraries/LibWebView/WebContentClient.h
+++ b/Libraries/LibWebView/WebContentClient.h
@@ -152,7 +152,7 @@ public:
// 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 and data: URL iframes. An origin-keyed tab keeps apart from the other origins of
// its site, so it only joins origin-keyed tabs of its origin, in a process that has not hosted iframes of those
- // other origins.
+ // other origins. The user may let the tabs of all origins of a site share processes, which then go by site.
void note_hosted_document(CanonicalDocument const&);
bool has_hosted_only_tabs_like(CanonicalDocument const&) const;
bool has_hosted_only_tabs_unlike(CanonicalDocument const&) const;
@@ -286,8 +286,11 @@ private:
Optional<URL::Origin> m_origin_of_hosted_tabs;
bool m_hosted_tabs_are_origin_keyed { false };
bool m_has_hosted_more_than_tabs_of_one_origin { false };
+ // NB: Such a process takes in more tabs only while the tabs of a site share processes.
+ bool m_has_hosted_tabs_of_more_than_one_origin_of_a_site { false };
Vector<u64> m_crashed_view_ids;
+ bool has_hosted_only_tabs_of_the_site_of(CanonicalDocument const&) const;
WebContentPage& open_page(Web::PageId, CanonicalTraversable&);
WebContentPage* find_page(Web::PageId) const;