Skip to content

Truly independent web browser

LibWebView: Release the memory of tabs that were hidden for a while

Andreas Kling0820eadd0d6f
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index daee5abb6d8..c14aadcb76e 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -58,6 +58,7 @@ void CanonicalTraversable::set_system_visibility_state(Web::HTML::VisibilityStat
m_system_visibility_state = visibility_state;
for_each_hosting_page([&](WebContentPage& page) {
page.async_set_system_visibility_state(visibility_state);
+ page.client().did_change_visibility_of_a_tab();
});
// When a user agent determines that the system visibility state for
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 8d20947addd..3c20c531adf 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -240,6 +240,70 @@ void WebContentClient::die()
did_lose_process();
}
+// A tab the user left is likely to stay in the background for long, and its process keeps much that only makes it
+// faster: compiled code, decoded images, caches. Once every tab of a process was hidden for this long, the process is
+// asked to let go of all that. The wait keeps a user who switches back and forth between tabs from paying for it.
+static constexpr auto time_hidden_before_releasing_memory = AK::Duration::from_seconds(10);
+
+bool WebContentClient::all_tabs_are_hidden() const
+{
+ // NB: A process without pages has no tabs to speak of, and little to release.
+ bool has_open_page = false;
+ for (auto const& [page_id, page] : m_pages) {
+ if (!page->is_open() || !page->has_traversable())
+ continue;
+ if (page->traversable().system_visibility_state() != Web::HTML::VisibilityState::Hidden)
+ return false;
+ has_open_page = true;
+ }
+ return has_open_page;
+}
+
+void WebContentClient::did_change_visibility_of_a_tab()
+{
+ if (!all_tabs_are_hidden()) {
+ if (m_release_memory_of_hidden_tabs_timer)
+ m_release_memory_of_hidden_tabs_timer->stop();
+ return;
+ }
+ if (!m_release_memory_of_hidden_tabs_timer) {
+ m_release_memory_of_hidden_tabs_timer = Core::Timer::create_single_shot(time_hidden_before_releasing_memory, [this] {
+ release_memory_of_hidden_tabs();
+ });
+ }
+ // NB: A timer that already runs keeps its time, so that more of these calls while the tabs stay hidden do not
+ // put the release off.
+ if (!m_release_memory_of_hidden_tabs_timer->is_active())
+ m_release_memory_of_hidden_tabs_timer->start();
+}
+
+void WebContentClient::release_memory_of_hidden_tabs()
+{
+ if (!is_open() || !all_tabs_are_hidden())
+ return;
+ // NB: What the process lets go of, it has to make again when it needs it, which a tab that plays or records sound
+ // in the background could not do without the user hearing it. A page that only hosts a frame of a tab counts
+ // too, as the sound of the tab may come from that frame. The process is asked once the sound has stopped.
+ for (auto const& [page_id, page] : m_pages) {
+ if (!page->is_open() || !page->has_traversable())
+ continue;
+ auto view = page->traversable().view();
+ if (!view.has_value())
+ continue;
+ auto plays_or_records_sound = view->is_playing_audio() || view->is_capturing_audio();
+ // NB: A view that another view owns has no tab of its own, and its capture shows on its owner.
+ if (auto owner_view_id = view->owner_view_id(); owner_view_id.has_value()) {
+ if (auto owner = ViewImplementation::find_view_by_id(*owner_view_id); owner.has_value())
+ plays_or_records_sound |= owner->is_playing_audio() || owner->is_capturing_audio();
+ }
+ if (plays_or_records_sound) {
+ m_release_memory_of_hidden_tabs_timer->restart();
+ return;
+ }
+ }
+ async_release_memory();
+}
+
void WebContentClient::did_misbehave(StringView message_name, StringView reason)
{
m_rejected_ipc = true;
@@ -452,6 +516,8 @@ WebContentPage& WebContentClient::open_page(Web::PageId page_id, CanonicalTraver
{
auto page = adopt_ref(*new WebContentPage(*this, page_id, traversable));
m_pages.set(page_id, page);
+ // NB: A tab that starts hidden, or that moves to this process while hidden, never tells of a change.
+ did_change_visibility_of_a_tab();
return page;
}
diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.h
index d362f09c08f..56c15383096 100644
--- a/Libraries/LibWebView/WebContentClient.h
+++ b/Libraries/LibWebView/WebContentClient.h
@@ -124,6 +124,8 @@ public:
virtual void did_misbehave(StringView message_name, StringView reason) override;
// The process is about to be ended for using more memory than the limit.
void did_exceed_memory_limit() { m_exceeded_memory_limit = true; }
+ // A tab that a page of this process belongs to was shown or hidden.
+ void did_change_visibility_of_a_tab();
enum class HTTPCookieAccess : u8 {
Allowed,
// A WebDriver cookie command runs, but for another document, which the renderer may not have heard of yet.
@@ -338,6 +340,10 @@ private:
};
RefPtr<Core::Timer> m_detached_page_close_timer;
+ bool all_tabs_are_hidden() const;
+ void release_memory_of_hidden_tabs();
+ RefPtr<Core::Timer> m_release_memory_of_hidden_tabs_timer;
+
// When this process last got a WebRTCClient, so that it can't make us spawn them in a loop.
Vector<MonotonicTime> m_webrtc_client_launch_times;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 4db9155b2d5..79302fb20c6 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -670,8 +670,11 @@ void WebContentPage::close()
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)
+ if (m_client) {
WorkerAgentManager::the().remove_web_content_page_owner(*m_client, m_id);
+ // NB: This may have been the only page of the process with a tab that is shown.
+ m_client->did_change_visibility_of_a_tab();
+ }
// 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())
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index ac6c77706a5..1ed293402fb 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -84,6 +84,8 @@ public:
virtual Web::PageId routed_page_id() const override { return m_id; }
CanonicalTraversable& traversable() const;
+ // NB: The tab of a page can go away before the page is closed.
+ bool has_traversable() const { return !m_traversable.is_null(); }
ViewImplementation& view() const;
bool displays_tab() const;
String dump_process_tree() const;