Truly independent web browser
LibWebView+UI/Qt: Keep tabs loaded that download or wait for an answer
Andreas Kling2af0e6e262f4
diff --git a/Libraries/LibWebView/DiscardBlockers.h b/Libraries/LibWebView/DiscardBlockers.hindex ace6cf06ab9..389a3ae873e 100644--- a/Libraries/LibWebView/DiscardBlockers.h+++ b/Libraries/LibWebView/DiscardBlockers.h@@ -15,22 +15,24 @@ namespace WebView {enum class DiscardBlockers : u32 {None = 0,NotATab = 1 << 0,- NotRestorable = 1 << 16,- Visible = 1 << 1,- Pinned = 1 << 2,- CurrentTabOfWindow = 1 << 3,- Loading = 1 << 4,- Crashed = 1 << 5,- PlayingAudio = 1 << 6,- RecentlyPlayedAudio = 1 << 7,- CapturingAudio = 1 << 8,- UsingLocation = 1 << 9,- OwnsAView = 1 << 10,- DevToolsConnected = 1 << 11,- UserEdits = 1 << 12,+ NotRestorable = 1 << 1,+ Visible = 1 << 2,+ Pinned = 1 << 3,+ CurrentTabOfWindow = 1 << 4,+ Loading = 1 << 5,+ Crashed = 1 << 6,+ PlayingAudio = 1 << 7,+ RecentlyPlayedAudio = 1 << 8,+ CapturingAudio = 1 << 9,+ UsingLocation = 1 << 10,+ OwnsAView = 1 << 11,+ DevToolsConnected = 1 << 12,+ UserEdits = 1 << 13,+ Downloading = 1 << 14,+ AwaitingPermissionPrompt = 1 << 15,// Only a tab hidden for long is kept for these, as they're work the tab does for the user in the background.- ChangedTitleOrFaviconWhileHidden = 1 << 13,- MayShowNotifications = 1 << 14,+ ChangedTitleOrFaviconWhileHidden = 1 << 16,+ MayShowNotifications = 1 << 17,};AK_ENUM_BITWISE_OPERATORS(DiscardBlockers);diff --git a/Libraries/LibWebView/TabDiscarder.cpp b/Libraries/LibWebView/TabDiscarder.cppindex 1129246db8a..14d62eb5d9b 100644--- a/Libraries/LibWebView/TabDiscarder.cpp+++ b/Libraries/LibWebView/TabDiscarder.cpp@@ -49,7 +49,7 @@ void TabDiscarder::check_tabs()});if (auto* view = least_recently_seen_ending_a_process ? least_recently_seen_ending_a_process : least_recently_seen) {dbgln("Discarding tab {}, as the system is about to run out of memory", view->url());- view->request_discard();+ view->request_discard(ViewImplementation::DiscardReason::MemoryPressure);}}@@ -66,7 +66,7 @@ void TabDiscarder::check_tabs()});for (auto& view : inactive_views) {dbgln("Discarding tab {}, as it was hidden for {} min", view.url(), minutes);- view.request_discard();+ view.request_discard(ViewImplementation::DiscardReason::Inactivity);}}diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 8d1f7f6fa79..ee1a0fb722f 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -693,6 +693,15 @@ DiscardBlockers ViewImplementation::discard_blockers(DiscardReason reason, Monotif (had_user_edits())blockers |= DiscardBlockers::UserEdits;+ // A download that a page of the tab makes itself, such as one of a blob, fails when the page's process exits. And+ // a permission prompt would go away unanswered.+ traversable().for_each_hosting_page([&](WebContentPage& page) {+ if (page.has_renderer_owned_downloads())+ blockers |= DiscardBlockers::Downloading;+ });+ if (!m_pending_permission_prompts.is_empty())+ blockers |= DiscardBlockers::AwaitingPermissionPrompt;+if (reason == DiscardReason::Inactivity) {// A tab that changes what it shows in the tab strip, such as the count of unread messages in its title, does// work for the user in the background. So does one that may show them notifications.@@ -739,9 +748,10 @@ bool ViewImplementation::discarding_ends_a_process() constreturn ends_a_process;}-void ViewImplementation::request_discard()+void ViewImplementation::request_discard(DiscardReason reason){m_discard_requested = true;+ m_discard_reason = reason;m_pages_saving_session_history_entry_states.clear();traversable().for_each_hosting_page([&](WebContentPage& page) {@@ -4500,8 +4510,17 @@ static HashMap<u64, DiscardedTabState>& discarded_tab_states()return *states;}-u64 ViewImplementation::keep_state_for_discarding()+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+ // its history. It then stays loaded, and may be discarded later.+ if (!m_discard_requested)+ return {};+ if (traversable().system_visibility_state() != Web::HTML::VisibilityState::Hidden || discard_blockers(m_discard_reason, MonotonicTime::now()) != DiscardBlockers::None) {+ m_discard_requested = false;+ return {};+ }+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 02b8e45a93a..5c3900f6c8e 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -247,7 +247,7 @@ public:void set_current_tab_of_window(bool current_tab_of_window);// When the user last stopped seeing the view, if they don't see it now.Optional<MonotonicTime> hidden_since() const { return m_hidden_since; }- void request_discard();+ void request_discard(DiscardReason);void did_save_session_history_entry_states(Badge<WebContentPage>, WebContentPage&);// Whether the view shows its frames from CPU memory for now, so that the Compositor shares them there rather than@@ -312,7 +312,7 @@ public:// 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.- u64 keep_state_for_discarding();+ 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);void update_state_of_discarded_tab();@@ -1336,6 +1336,7 @@ protected:bool m_current_tab_of_window { false };// The UI was asked to discard the view, so it isn't asked again until the user has seen it.bool m_discard_requested { false };+ DiscardReason m_discard_reason { DiscardReason::Inactivity };Optional<u64> m_discarded_tab_state_id;// NB: These are kept alive, so that no other page can take the place of one that closes before it answers.Vector<NonnullRefPtr<WebContentPage>> m_pages_saving_session_history_entry_states;diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex a024b36eb1d..5993cc128e4 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -187,6 +187,7 @@ public:Optional<PresentedBackingStores> take_presented_backing_stores();void release_presented_bitmap(i32 bitmap_id);void fail_renderer_owned_downloads();+ bool has_renderer_owned_downloads() const { return !m_renderer_owned_downloads.is_empty(); }void reset_state_for_testing();private:diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex b939d1e6493..d36ea8e6579 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -1217,13 +1217,18 @@ void BrowserWindow::discard_tab(Tab& tab)return;}+ // NB: The Hub has the final say, as the tab may have started to do something the user would miss since it asked.+ auto discarded_tab_state_id = tab.view().keep_state_for_discarding();+ if (!discarded_tab_state_id.has_value())+ return;+DeferredTab deferred_tab {.session_tab_id = *tab.session_tab_id(),.url = tab.url(),.title = tab.title(),.favicon = tab.favicon(),.history = tab.view().session_history_snapshot(),- .discarded_tab_state_id = tab.view().keep_state_for_discarding(),+ .discarded_tab_state_id = discarded_tab_state_id,};auto* discarded_tab = new Tab(this, AK::move(deferred_tab));discarded_tab->set_needs_attention(tab.needs_attention());diff --git a/UI/Qt/ProcessManagerWindow.cpp b/UI/Qt/ProcessManagerWindow.cppindex a0a738f0069..66b9c053a96 100644--- a/UI/Qt/ProcessManagerWindow.cpp+++ b/UI/Qt/ProcessManagerWindow.cpp@@ -329,6 +329,8 @@ static CanUnloadState can_unload_state(WebView::ProcessTabSnapshot const& tab)Description { DiscardBlockers::OwnsAView, QT_TRANSLATE_NOOP("QObject", "It has a Picture-in-Picture window") },Description { DiscardBlockers::DevToolsConnected, QT_TRANSLATE_NOOP("QObject", "DevTools are open for it") },Description { DiscardBlockers::UserEdits, QT_TRANSLATE_NOOP("QObject", "You typed into the page") },+ Description { DiscardBlockers::Downloading, QT_TRANSLATE_NOOP("QObject", "It's downloading a file") },+ Description { DiscardBlockers::AwaitingPermissionPrompt, QT_TRANSLATE_NOOP("QObject", "It's waiting for you to answer a permission request") },Description { DiscardBlockers::ChangedTitleOrFaviconWhileHidden, QT_TRANSLATE_NOOP("QObject", "It changed its title or icon while hidden") },Description { DiscardBlockers::MayShowNotifications, QT_TRANSLATE_NOOP("QObject", "The site may show notifications") },};