Truly independent web browser
LibWebView: Retire the RequestServers of a discarded tab right away
Andreas Klingcfd44c21c013
diff --git a/Libraries/LibWebView/RequestServerManager.cpp b/Libraries/LibWebView/RequestServerManager.cppindex d08cfc52dd9..e74ff4cceb7 100644--- a/Libraries/LibWebView/RequestServerManager.cpp+++ b/Libraries/LibWebView/RequestServerManager.cpp@@ -184,16 +184,30 @@ ErrorOr<RequestServerClientConnection> RequestServerManager::connect_new_client(return Error::from_string_literal("RequestServer keeps handing out client IDs that are in use");}+void RequestServerManager::retire_without_delay_once_disconnected(int client_id)+{+ for (auto& instance : m_instances) {+ if (instance->site().has_value() && instance->has_client(client_id)) {+ instance->m_client_ids_retiring_without_delay.set(client_id);+ return;+ }+ }+}+void RequestServerManager::client_disconnected(RequestServerInstance& instance, int client_id){Application::the().did_disconnect_request_server_client(client_id);instance.m_client_ids.remove(client_id);+ auto retires_without_delay = instance.m_client_ids_retiring_without_delay.remove(client_id);if (!instance.site().has_value() || !instance.m_client_ids.is_empty())return;+ // NB: This runs inside a message handler of the instance's control connection, which retiring the instance drops.+ // So even an instance that retires without delay is retired from the event loop.+ auto idle_timeout = retires_without_delay ? AK::Duration::zero() : Application::request_server_options().idle_site_request_server_timeout;auto weak_instance = instance.make_weak_ptr();- instance.m_idle_timer = Core::Timer::create_single_shot(Application::request_server_options().idle_site_request_server_timeout, [weak_instance] {+ instance.m_idle_timer = Core::Timer::create_single_shot(idle_timeout, [weak_instance] {auto instance = weak_instance.strong_ref();if (instance && instance->m_client_ids.is_empty())the().retire(*instance);diff --git a/Libraries/LibWebView/RequestServerManager.h b/Libraries/LibWebView/RequestServerManager.hindex 5f897057e08..6e2c5d99ac0 100644--- a/Libraries/LibWebView/RequestServerManager.h+++ b/Libraries/LibWebView/RequestServerManager.h@@ -60,6 +60,7 @@ private:RefPtr<Requests::RequestControlClient> m_control_client;RefPtr<Requests::RequestClient> m_ui_client;HashTable<int> m_client_ids;+ HashTable<int> m_client_ids_retiring_without_delay;RefPtr<Core::Timer> m_idle_timer;bool m_retired { false };@@ -101,6 +102,10 @@ public:bool uses_site_request_servers() const { return m_maximum_site_instances > 0; }+ // The process of the client is about to exit for good, such as for a discarded tab. If no other client of its+ // RequestServer is left then, nothing is likely to need that server soon, so it is retired right away.+ void retire_without_delay_once_disconnected(int client_id);+private:ErrorOr<NonnullRefPtr<RequestServerInstance>> launch(NonnullRefPtr<BrowsingSession>, Optional<Utf16String> site);ErrorOr<void> launch_process(RequestServerInstance&);diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex bf0d5c265a4..acc35c04242 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -38,6 +38,8 @@#include <LibWebView/HistoryDebug.h>#include <LibWebView/PageTranslation.h>#include <LibWebView/PermissionManager.h>+#include <LibWebView/RequestServerManager.h>+#include <LibWebView/RequestServerSiteBindings.h>#include <LibWebView/SessionHistory.h>#include <LibWebView/TabDiscarder.h>#include <LibWebView/TabPerformanceMonitor.h>@@ -753,23 +755,33 @@ static constexpr auto time_to_save_session_history_entry_states = AK::Duration::// NB: A process may host pages of several tabs, such as tabs of one origin, or the cross-site frames of one site in// several tabs. A page of another tab, even one only representing a frame, keeps the process alive.-bool ViewImplementation::discarding_ends_a_process() const+void ViewImplementation::for_each_process_that_discarding_ends(Function<void(WebContentClient&)> const& callback) const{if (!m_top_level_traversable)- return false;+ return;- auto ends_a_process = false;+ HashTable<WebContentClient*> clients;traversable().for_each_hosting_page([&](WebContentPage& page) {- if (ends_a_process)- return;+ clients.set(&page.client());+ });+ for (auto* client : clients) {auto hosts_only_this_view = true;- page.client().for_each_page([&](WebContentPage& other_page) {+ client->for_each_page([&](WebContentPage& other_page) {if (other_page.has_traversable() && &other_page.traversable() == &traversable())return IterationDecision::Continue;hosts_only_this_view = false;return IterationDecision::Break;});- ends_a_process = hosts_only_this_view;+ if (hosts_only_this_view)+ callback(*client);+ }+}++bool ViewImplementation::discarding_ends_a_process() const+{+ auto ends_a_process = false;+ for_each_process_that_discarding_ends([&](WebContentClient&) {+ ends_a_process = true;});return ends_a_process;}@@ -4555,6 +4567,7 @@ static HashMap<u64, DiscardedTabState>& discarded_tab_states()return *states;}+// NB: The UI asks for this once it discards the tab, right before it closes the view.Optional<u64> ViewImplementation::keep_state_for_discarding(){// NB: The tab may have started to do something the user would miss, such as playing audio, while its pages saved@@ -4567,6 +4580,17 @@ Optional<u64> ViewImplementation::keep_state_for_discarding()return {};}+ // A RequestServer that served only this tab keeps the connections and cache entries a page of the tab would use+ // again soon. The tab doesn't load its pages again until the user comes back to it, so the server can go with the+ // processes of the tab.+ for_each_process_that_discarding_ends([](WebContentClient& client) {+ auto const& bindings = client.request_server_site_bindings();+ for (auto const& top_level_site : bindings.top_level_sites()) {+ if (auto client_id = bindings.client_id_for(top_level_site); client_id.has_value())+ RequestServerManager::the().retire_without_delay_once_disconnected(*client_id);+ }+ });+static u64 s_next_discarded_tab_state_id = 1;auto discarded_tab_state_id = s_next_discarded_tab_state_id++;diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex ce96230f963..a86041acdcf 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -313,8 +313,8 @@ public:[[nodiscard]] SessionHistorySnapshot session_history_snapshot() const;[[nodiscard]] ErrorOr<void> restore_session_history_from_snapshot(SessionHistorySnapshot);- // The UI keeps the session history of a discarded tab. The Hub keeps what else the tab should have once it is loaded- // again, until the UI restores or forgets it.+ // The UI keeps the session history of a discarded tab. The Hub keeps what else the tab should have once it is+ // loaded again, until the UI restores or forgets it. The UI asks the Hub to keep it as it discards the tab.Optional<u64> keep_state_for_discarding();void restore_state_of_discarded_tab(u64 discarded_tab_state_id);static void forget_state_of_discarded_tab(u64 discarded_tab_state_id);@@ -852,6 +852,7 @@ protected:void finish_discard_request();void stop_counting_as_owned_view();void start_being_hidden();+ void for_each_process_that_discarding_ends(Function<void(WebContentClient&)> const&) const;Optional<URL::URL const&> browser_ui_load_url() const;void update_displayed_url();void did_start_navigation();