Truly independent web browser
UI/Qt: Reach the session store through a RemoteSession
Andreas Kling94132ad5e8ad
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 76351a953bc..64c21be6dd0 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -34,6 +34,7 @@#include <UI/Qt/ProcessManagerWindow.h>#include <UI/Qt/RemoteActions.h>#include <UI/Qt/RemoteDownloads.h>+#include <UI/Qt/RemoteSession.h>#include <UI/Qt/StringUtils.h>#include <UI/Qt/Tab.h>#include <UI/Qt/Theme.h>@@ -477,8 +478,8 @@ public:m_reopen_recently_closed_tab_action->setText("&Reopen Closed Tab");auto* active_window = Application::the().active_window_if_any();- auto& session = active_window ? active_window->session() : WebView::Application::default_session();- m_reopen_recently_closed_tab_action->setEnabled(session.session_store->has_closed_units());+ auto& session = active_window ? active_window->session() : RemoteSession::of(WebView::IsPrivate::No);+ m_reopen_recently_closed_tab_action->setEnabled(session.has_closed_units());}void rebuild_bookmarks_menu()@@ -932,7 +933,7 @@ static WindowConfiguration window_configuration_for(WebView::SessionWindowGeometBrowserWindow* Application::restore_previous_session(WindowConfiguration const& frontmost_window_configuration){- auto plan = WebView::Application::default_session().session_store->take_launch_plan();+ auto plan = RemoteSession::of(WebView::IsPrivate::No).take_launch_plan();BrowserWindow* frontmost_window = nullptr;for (size_t i = 0; i < plan.size(); ++i) {@@ -952,7 +953,7 @@ BrowserWindow* Application::restore_previous_session(WindowConfiguration const&void Application::offer_to_restore_unexpectedly_closed_tabs(BrowserWindow& window){- auto& session_store = *WebView::Application::default_session().session_store;+ auto& session_store = RemoteSession::of(WebView::IsPrivate::No);if (session_store.consecutive_unclean_exits() == 0)return;@@ -1151,8 +1152,8 @@ void Application::focus_location_editor()void Application::reopen_recently_closed_tab(BrowserWindow* target_window, Optional<WebView::SessionWindowId> unit_id, ReopenClosedWindow reopen_closed_window){auto is_private = target_window ? target_window->is_private() : WebView::IsPrivate::No;- auto& session = target_window ? target_window->session() : WebView::Application::default_session();- auto closed_unit = unit_id.has_value() ? session.session_store->take_closed_unit(*unit_id) : session.session_store->take_most_recently_closed();+ auto& session = target_window ? target_window->session() : RemoteSession::of(WebView::IsPrivate::No);+ auto closed_unit = unit_id.has_value() ? session.take_closed_unit(*unit_id) : session.take_most_recently_closed();if (closed_unit.is_error()) {dbgln("Unable to reopen the most recently closed session unit: {}", closed_unit.error());return;@@ -1327,7 +1328,7 @@ void Application::finish_quit()// The session store hears of the quit only once every window has agreed, so a canceled quit never reaches it. In// quitting mode, it keeps each window that closes as an open one, for the next launch to recover.m_quit_is_finishing = true;- WebView::Application::default_session().session_store->application_quitting();+ RemoteSession::of(WebView::IsPrivate::No).application_quitting();// Each window's pages agreed, so it closes at once; the active window goes last, so no close activates another.auto windows = visible_browser_windows_active_first(m_active_window);diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex 0730dc87d93..607107f2109 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -29,6 +29,7 @@#include <UI/Qt/Motion.h>#include <UI/Qt/Popover.h>#include <UI/Qt/RemoteDownloads.h>+#include <UI/Qt/RemoteSession.h>#include <UI/Qt/StringUtils.h>#include <UI/Qt/SystemAppearance.h>#if defined(AK_OS_MACOS)@@ -341,7 +342,6 @@ static QIcon const& app_icon(WebView::IsPrivate is_private)BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window, WebView::IsPrivate is_private, Optional<u64> hub_view_id, Optional<WebView::SessionWindowId> restored_session_window_id): WebView::SettingsObserver(Application::settings()), m_is_private(is_private)- , m_session(WebView::Application::session_for_new_view(is_private)), m_tabs_container(new TabWidget(is_private, this)), m_is_popup_window(is_popup_window){@@ -700,7 +700,7 @@ void BrowserWindow::initialize_window_context_menu()void BrowserWindow::show_window_context_menu(QPoint global_position){- m_reopen_closed_tab_action->setEnabled(m_session->session_store->has_closed_units());+ m_reopen_closed_tab_action->setEnabled(session().has_closed_units());m_always_on_top_action->setChecked(windowFlags().testFlag(Qt::WindowStaysOnTopHint));auto current_tab_layout = to_underlying(tab_layout_choice(Application::settings().tab_settings()));for (auto* action : m_tab_layout_actions->actions())@@ -760,7 +760,7 @@ void BrowserWindow::duplicate_tab(Tab& source_tab, Web::HTML::ActivateTab activaif (source_tab.is_loaded())history = source_tab.view().session_history_snapshot();else if (auto session_tab_id = source_tab.session_tab_id(); session_tab_id.has_value())- history = m_session->session_store->load_tab_history(*session_tab_id);+ history = session().load_tab_history(*session_tab_id);auto fallback_url = source_tab.url();if (history.has_value() && history_step.has_value()) {@@ -809,7 +809,7 @@ Tab& BrowserWindow::create_restored_tab(WebView::RestoredSessionTab const& resto{if (defer) {// Until the tab loads, it shows what browsing history knows about its page.- auto entry = m_session->history_store->entry_for_url(restored.url);+ auto entry = session().entry_for_url(restored.url);DeferredTab deferred_tab {.session_tab_id = restored.tab_id,.url = restored.url,@@ -834,7 +834,7 @@ Tab& BrowserWindow::create_restored_tab(WebView::RestoredSessionTab const& restom_tabs_container->insert_tab(index, tab, "New Tab");initialize_tab(tab);- auto history = m_session->session_store->load_tab_history(restored.tab_id);+ auto history = session().load_tab_history(restored.tab_id);if (!history.has_value() || tab->view().restore_session_history_from_snapshot(history.release_value()).is_error())tab->navigate(restored.url);return *tab;@@ -924,7 +924,7 @@ void BrowserWindow::initialize_tab(Tab* tab).insertion_index = insertion_index >= 0 ? Optional<i64> { insertion_index } : Optional<i64> {},.is_active = m_current_tab == tab ? WebView::SessionStore::IsActive::Yes : WebView::SessionStore::IsActive::No,};- auto session_tab_id = m_session->session_store->tab_opened(AK::move(opened));+ auto session_tab_id = session().tab_opened(AK::move(opened));if (session_tab_id.is_error())dbgln("Unable to register the new tab with the session store: {}", session_tab_id.error());else@@ -1011,12 +1011,12 @@ void BrowserWindow::adopt_tab(Tab& tab, int index).new_window_id = *m_session_window_id,.ordinal = index,};- m_session->session_store->tab_moved(AK::move(moved));+ session().tab_moved(AK::move(moved));sync_session_tab_order();} else {// The source window's reported order no longer includes this tab; it must leave// tracking rather than stay mapped there.- m_session->session_store->tab_detached(*moved_tab_id);+ session().tab_detached(*moved_tab_id);tab.clear_session_tab_id();}}@@ -1124,7 +1124,7 @@ void BrowserWindow::set_current_tab(Tab* tab)m_current_tab->view().set_system_visibility_state(Web::HTML::VisibilityState::Visible);if (auto session_tab_id = m_current_tab->session_tab_id(); session_tab_id.has_value())- m_session->session_store->active_tab_changed(*session_tab_id);+ session().active_tab_changed(*session_tab_id);}Application::the().report_active_view();@@ -1174,7 +1174,7 @@ bool BrowserWindow::close_tab(int index, WebView::SessionStore::KeepForReopening.closed_at = UnixDateTime::now(),.keep_for_reopening = keep_for_reopening,};- if (auto result = m_session->session_store->tab_closed(AK::move(closed)); result.is_error())+ if (auto result = session().tab_closed(AK::move(closed)); result.is_error())dbgln("Unable to record the closed tab in the session store: {}", result.error());}Application::the().update_reopen_recently_closed_actions();@@ -1245,7 +1245,7 @@ void BrowserWindow::pinned_state_changed(Tab& tab){// A move already reported the new order, through the tab bar's tabMoved signal.if (auto session_tab_id = tab.session_tab_id(); session_tab_id.has_value()) {- if (auto result = m_session->session_store->tab_pinned_changed(*session_tab_id, tab.is_pinned()); result.is_error())+ if (auto result = session().tab_pinned_changed(*session_tab_id, tab.is_pinned()); result.is_error())dbgln("Unable to record the tab's pin in the session store: {}", result.error());}update_tab_close_button(tab);@@ -1260,7 +1260,7 @@ void BrowserWindow::activate_first_unpinned_tab()void BrowserWindow::register_window_with_session_store(){- if (auto session_window_id = m_session->session_store->window_opened(); session_window_id.is_error())+ if (auto session_window_id = session().window_opened(); session_window_id.is_error())dbgln("Unable to register the new window with the session store: {}", session_window_id.error());elsem_session_window_id = session_window_id.value();@@ -1282,7 +1282,7 @@ void BrowserWindow::sync_session_tab_order().window_id = *m_session_window_id,.ordered_tabs = AK::move(tab_order),};- m_session->session_store->tab_order_changed(AK::move(changed));+ session().tab_order_changed(AK::move(changed));}void BrowserWindow::open_file()@@ -2074,7 +2074,7 @@ void BrowserWindow::closeEvent(QCloseEvent* event).active_tab_index = active_tab_index,.closed_at = UnixDateTime::now(),};- if (auto result = m_session->session_store->window_closing(AK::move(closing)); result.is_error())+ if (auto result = session().window_closing(AK::move(closing)); result.is_error())dbgln("Unable to record the closing window in the session store: {}", result.error());Application::the().update_reopen_recently_closed_actions();}@@ -2126,7 +2126,7 @@ Tab* BrowserWindow::tab_for_view(WebView::ViewImplementation const& view) constvoid BrowserWindow::report_activation_to_session_store(){if (m_session_window_id.has_value())- m_session->session_store->window_activated(*m_session_window_id);+ session().window_activated(*m_session_window_id);}void BrowserWindow::report_session_window_geometry()@@ -2148,7 +2148,7 @@ void BrowserWindow::report_session_window_geometry().height = rect.height(),.maximized = isMaximized(),};- m_session->session_store->window_geometry_changed(*m_session_window_id, window_geometry);+ session().window_geometry_changed(*m_session_window_id, window_geometry);}void BrowserWindow::begin_window_close_preflight(Function<void(bool approved)> on_complete)diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex a3569c327ed..70df43b8577 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -17,6 +17,7 @@#include <LibWebView/SessionStore.h>#include <LibWebView/Settings.h>#include <UI/Qt/FullscreenDebounce.h>+#include <UI/Qt/RemoteSession.h>#include <UI/Qt/Tab.h>#include <UI/Qt/TabBar.h>#include <UI/Qt/WindowCloseCoordinator.h>@@ -154,7 +155,7 @@ public:WebContentView& view() const { return m_current_tab->view(); }WebView::IsPrivate is_private() const { return m_is_private; }- WebView::BrowsingSession& session() const { return *m_session; }+ RemoteSession& session() const { return RemoteSession::of(m_is_private); }Optional<WebView::SessionWindowId> session_window_id() const { return m_session_window_id; }int tab_count() { return m_tabs_container->count(); }@@ -323,7 +324,6 @@ private:WindowScreenObserver* m_screen_observer { nullptr };WebView::IsPrivate m_is_private { WebView::IsPrivate::No };- NonnullRefPtr<WebView::BrowsingSession> m_session;TabWidget* m_tabs_container { nullptr };Tab* m_current_tab { nullptr };diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex c296ba1cb4b..fd18e4eb859 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -45,6 +45,7 @@ add_library(ladybird_impl STATICProcessManagerWindow.cppRemoteActions.cppRemoteDownloads.cpp+ RemoteSession.cppRemoteView.cppReportReview.cppRestoreSessionCard.cppdiff --git a/UI/Qt/RemoteSession.cpp b/UI/Qt/RemoteSession.cppnew file mode 100644index 00000000000..43605986c29--- /dev/null+++ b/UI/Qt/RemoteSession.cpp@@ -0,0 +1,51 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibWebView/Application.h>+#include <LibWebView/BrowsingSession.h>+#include <UI/Qt/RemoteSession.h>++namespace Ladybird {++RemoteSession& RemoteSession::of(WebView::IsPrivate is_private)+{+ static RemoteSession s_regular { WebView::IsPrivate::No };+ static RemoteSession s_private { WebView::IsPrivate::Yes };+ return is_private == WebView::IsPrivate::Yes ? s_private : s_regular;+}++WebView::SessionStore& RemoteSession::store() const+{+ return *WebView::Application::session_for_new_view(m_is_private)->session_store;+}++ErrorOr<WebView::SessionWindowId> RemoteSession::window_opened() { return store().window_opened(); }+ErrorOr<void> RemoteSession::window_closing(WebView::SessionStore::WindowClosing closing) { return store().window_closing(move(closing)); }+void RemoteSession::window_activated(WebView::SessionWindowId id) { store().window_activated(id); }+void RemoteSession::window_geometry_changed(WebView::SessionWindowId id, WebView::SessionWindowGeometry geometry) { store().window_geometry_changed(id, geometry); }+ErrorOr<WebView::SessionTabId> RemoteSession::tab_opened(WebView::SessionStore::TabOpened opened) { return store().tab_opened(move(opened)); }+ErrorOr<void> RemoteSession::tab_closed(WebView::SessionStore::TabClosed closed) { return store().tab_closed(move(closed)); }+void RemoteSession::tab_detached(WebView::SessionTabId id) { store().tab_detached(id); }+void RemoteSession::tab_moved(WebView::SessionStore::TabMoved moved) { store().tab_moved(move(moved)); }+void RemoteSession::tab_order_changed(WebView::SessionStore::TabOrderChanged changed) { store().tab_order_changed(move(changed)); }+ErrorOr<void> RemoteSession::tab_pinned_changed(WebView::SessionTabId id, bool pinned) { return store().tab_pinned_changed(id, pinned); }+void RemoteSession::active_tab_changed(WebView::SessionTabId id) { store().active_tab_changed(id); }+void RemoteSession::application_quitting() { store().application_quitting(); }+void RemoteSession::application_quit_aborted() { store().application_quit_aborted(); }+u32 RemoteSession::consecutive_unclean_exits() const { return store().consecutive_unclean_exits(); }+bool RemoteSession::has_closed_units() const { return store().has_closed_units(); }+ErrorOr<Optional<WebView::ClosedSessionUnit>> RemoteSession::take_most_recently_closed() { return store().take_most_recently_closed(); }+Vector<WebView::RecentlyClosedSessionUnit> RemoteSession::recently_closed_units(size_t limit) const { return store().recently_closed_units(limit); }+ErrorOr<Optional<WebView::ClosedSessionUnit>> RemoteSession::take_closed_unit(WebView::SessionWindowId id) { return store().take_closed_unit(id); }+Vector<WebView::RestoredSessionWindow> RemoteSession::take_launch_plan() { return store().take_launch_plan(); }+Optional<WebView::SessionHistorySnapshot> RemoteSession::load_tab_history(WebView::SessionTabId id) { return store().load_tab_history(id); }++Optional<WebView::HistoryEntry> RemoteSession::entry_for_url(URL::URL const& url) const+{+ return WebView::Application::session_for_new_view(m_is_private)->history_store->entry_for_url(url);+}++}diff --git a/UI/Qt/RemoteSession.h b/UI/Qt/RemoteSession.hnew file mode 100644index 00000000000..d81c99f9c9a--- /dev/null+++ b/UI/Qt/RemoteSession.h@@ -0,0 +1,58 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <LibWebView/BrowsingSession.h>+#include <LibWebView/Forward.h>+#include <LibWebView/HistoryStore.h>+#include <LibWebView/SessionStore.h>++namespace Ladybird {++// What the UI asks of the session of its windows, whose store lives in the Hub.+class RemoteSession {+public:+ static RemoteSession& of(WebView::IsPrivate);++ WebView::IsPrivate is_private() const { return m_is_private; }++ ErrorOr<WebView::SessionWindowId> window_opened();+ ErrorOr<void> window_closing(WebView::SessionStore::WindowClosing);+ void window_activated(WebView::SessionWindowId);+ void window_geometry_changed(WebView::SessionWindowId, WebView::SessionWindowGeometry);+ ErrorOr<WebView::SessionTabId> tab_opened(WebView::SessionStore::TabOpened);+ ErrorOr<void> tab_closed(WebView::SessionStore::TabClosed);+ void tab_detached(WebView::SessionTabId);+ void tab_moved(WebView::SessionStore::TabMoved);+ void tab_order_changed(WebView::SessionStore::TabOrderChanged);+ ErrorOr<void> tab_pinned_changed(WebView::SessionTabId, bool pinned);+ void active_tab_changed(WebView::SessionTabId);+ void application_quitting();+ void application_quit_aborted();+ u32 consecutive_unclean_exits() const;++ bool has_closed_units() const;+ ErrorOr<Optional<WebView::ClosedSessionUnit>> take_most_recently_closed();+ Vector<WebView::RecentlyClosedSessionUnit> recently_closed_units(size_t limit) const;+ ErrorOr<Optional<WebView::ClosedSessionUnit>> take_closed_unit(WebView::SessionWindowId);+ Vector<WebView::RestoredSessionWindow> take_launch_plan();+ Optional<WebView::SessionHistorySnapshot> load_tab_history(WebView::SessionTabId);++ Optional<WebView::HistoryEntry> entry_for_url(URL::URL const&) const;++private:+ explicit RemoteSession(WebView::IsPrivate is_private)+ : m_is_private(is_private)+ {+ }++ WebView::SessionStore& store() const;++ WebView::IsPrivate m_is_private { WebView::IsPrivate::No };+};++}diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex 139420a5473..1322d975220 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -52,7 +52,6 @@ public:Hub::View const& hub_view() const { return *m_view; }WebView::IsPrivate is_private() const { return m_is_private; }- WebView::BrowsingSession& session() const { return m_view->session(); }URL::Origin permission_origin() const { return *m_state.permission_origin; }Vector<WebView::SitePermissionRow> site_permission_rows() const { return m_state.site_permission_rows; }void set_site_permission(URL::Origin const& origin, Web::Permissions::PermissionName permission_name, Optional<WebView::PermissionDecision> decision) { m_view->set_site_permission(origin, permission_name, decision); }diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 38e7f67c43f..9216215db93 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -27,6 +27,7 @@#include <UI/Qt/JavaScriptDialog.h>#include <UI/Qt/LadybirdButton.h>#include <UI/Qt/RemoteActions.h>+#include <UI/Qt/RemoteSession.h>#if defined(AK_OS_MACOS)# include <UI/Qt/MacWindow.h>#endif@@ -1000,7 +1001,7 @@ void Tab::ensure_loaded()Optional<WebView::SessionHistorySnapshot> history;if (m_session_tab_id.has_value())- history = m_window->session().session_store->load_tab_history(*m_session_tab_id);+ history = m_window->session().load_tab_history(*m_session_tab_id);if (!history.has_value() || view().restore_session_history_from_snapshot(history.release_value()).is_error())navigate(deferred_url);}diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex b47fe5f7df1..cf87f0ae7a3 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -22,6 +22,7 @@#include <UI/Qt/Icon.h>#include <UI/Qt/LadybirdButton.h>#include <UI/Qt/RemoteActions.h>+#include <UI/Qt/RemoteSession.h>#if defined(AK_OS_MACOS)# include <UI/Qt/MacWindow.h>#endif@@ -4654,8 +4655,8 @@ void TabWidget::update_all_tabs_entries()if (auto* browser_window = as_if<BrowserWindow>(window())) {auto& session = browser_window->session();auto now = UnixDateTime::now();- for (auto const& unit : session.session_store->recently_closed_units(RECENTLY_CLOSED_LIMIT)) {- auto entry = session.history_store->entry_for_url(unit.active_url);+ for (auto const& unit : session.recently_closed_units(RECENTLY_CLOSED_LIMIT)) {+ auto entry = session.entry_for_url(unit.active_url);auto title = entry.has_value() && entry->title.has_value() && !entry->title->is_empty() ? qstring_from_ak_string(*entry->title) : qstring_from_ak_string(unit.active_url.serialize());if (unit.was_window)title = unit.tab_count == 1 ? QStringLiteral("Window with 1 tab") : qformatted("Window with {} tabs", unit.tab_count);