Truly independent web browser
UI/Qt: Honor beforeunload when closing browser windows
Sam Atkins92dac0c11c1c
diff --git a/Tests/UI/Qt/CMakeLists.txt b/Tests/UI/Qt/CMakeLists.txtindex f2f0444af25..5a73ea7c72a 100644--- a/Tests/UI/Qt/CMakeLists.txt+++ b/Tests/UI/Qt/CMakeLists.txt@@ -138,5 +138,8 @@ target_sources(TestJavaScriptDialog PRIVATE"${LADYBIRD_SOURCE_DIR}/UI/Qt/StringUtils.cpp")+ladybird_test("TestWindowCloseCoordinator.cpp" UI/Qt LIBS Qt::Core)+target_sources(TestWindowCloseCoordinator PRIVATE "${LADYBIRD_SOURCE_DIR}/UI/Qt/WindowCloseCoordinator.cpp")+ladybird_test("TestStringUtils.cpp" UI/Qt NAME TestQtStringUtils LIBS LibURL Qt::Core)target_sources(TestQtStringUtils PRIVATE "${LADYBIRD_SOURCE_DIR}/UI/Qt/StringUtils.cpp")diff --git a/Tests/UI/Qt/TestWindowCloseCoordinator.cpp b/Tests/UI/Qt/TestWindowCloseCoordinator.cppnew file mode 100644index 00000000000..23a220d87b3--- /dev/null+++ b/Tests/UI/Qt/TestWindowCloseCoordinator.cpp@@ -0,0 +1,87 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>+#include <UI/Qt/WindowCloseCoordinator.h>++TEST_CASE(preflights_run_sequentially)+{+ Ladybird::WindowCloseCoordinator coordinator;+ Vector<size_t> started;+ Vector<Function<void(bool)>> completions;+ Vector<Ladybird::WindowCloseCoordinator::Preflight> preflights;++ for (size_t index = 0; index < 3; ++index) {+ preflights.append([&, index](auto on_complete) {+ started.append(index);+ completions.append(move(on_complete));+ });+ }++ bool approved = false;+ bool canceled = false;+ EXPECT(coordinator.start(move(preflights), [&] { approved = true; }, [&] { canceled = true; }));+ EXPECT(coordinator.is_running());+ EXPECT_EQ(started, Vector<size_t> { 0 });++ completions[0](true);+ EXPECT_EQ(started, (Vector<size_t> { 0, 1 }));+ completions[1](true);+ EXPECT_EQ(started, (Vector<size_t> { 0, 1, 2 }));+ completions[2](true);++ EXPECT(approved);+ EXPECT(!canceled);+ EXPECT(!coordinator.is_running());+}++TEST_CASE(cancellation_stops_the_sequence)+{+ Ladybird::WindowCloseCoordinator coordinator;+ Vector<Function<void(bool)>> completions;+ Vector<Ladybird::WindowCloseCoordinator::Preflight> preflights;+ preflights.append([&](auto on_complete) { completions.append(move(on_complete)); });+ preflights.append([&](auto on_complete) { completions.append(move(on_complete)); });+ preflights.append([&](auto on_complete) { completions.append(move(on_complete)); });++ bool approved = false;+ bool canceled = false;+ EXPECT(coordinator.start(move(preflights), [&] { approved = true; }, [&] { canceled = true; }));+ completions[0](true);+ completions[1](false);++ EXPECT(!approved);+ EXPECT(canceled);+ EXPECT_EQ(completions.size(), 2u);+ EXPECT(!coordinator.is_running());+}++TEST_CASE(repeated_start_and_late_completions_are_ignored)+{+ Ladybird::WindowCloseCoordinator coordinator;+ Function<void(bool)> completion;+ Vector<Ladybird::WindowCloseCoordinator::Preflight> preflights;+ preflights.append([&](auto on_complete) { completion = move(on_complete); });++ size_t canceled_count = 0;+ EXPECT(coordinator.start(move(preflights), [] {}, [&] { ++canceled_count; }));+ EXPECT(!coordinator.start({}, [] { }, [] { }));+ coordinator.cancel();+ EXPECT_EQ(canceled_count, 1u);++ completion(true);+ EXPECT_EQ(canceled_count, 1u);+ EXPECT(!coordinator.is_running());+}++TEST_CASE(an_empty_preflight_is_immediately_approved)+{+ Ladybird::WindowCloseCoordinator coordinator;+ bool approved = false;+ EXPECT(coordinator.start({}, [&] { approved = true; }, [] {}));+ EXPECT(approved);+ EXPECT(!coordinator.is_running());+}diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex ad44700e8f6..ba117b81e34 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -511,6 +511,7 @@ void BrowserWindow::duplicate_tab(Tab& source_tab)Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, Optional<WebView::CanonicalTraversable&> traversable){+ cancel_window_close_preflight();if (!traversable.has_value())return create_new_tab(activate_tab, TabLocation::end());@@ -546,6 +547,8 @@ bool BrowserWindow::has_chrome_in_titlebar()Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, TabLocation location){+ cancel_window_close_preflight();+auto* tab = new Tab(this);if (m_current_tab == nullptr) {@@ -643,6 +646,8 @@ void BrowserWindow::uninitialize_tab(Tab* tab)void BrowserWindow::adopt_tab(Tab& tab, int index){+ cancel_window_close_preflight();+index = clamp(index, 0, m_tabs_container->count());tab.set_window(*this);@@ -693,6 +698,8 @@ void BrowserWindow::move_tab_to_window(int index, BrowserWindow& target_window,if (is_private() != target_window.is_private())return;+ cancel_window_close_preflight();+auto* tab = m_tabs_container->tab(index);tab->view().prepare_for_window_move();uninitialize_tab(tab);@@ -772,6 +779,8 @@ bool BrowserWindow::activate_tab_with_url(URL::URL const& url)bool BrowserWindow::definitely_close_tab(int index){+ cancel_window_close_preflight();+if (m_tabs_container->count() == 1 && Application::the().file_downloader().has_active_downloads() && visible_browser_window_count() <= 1) {if (!Application::the().confirm_stop_active_downloads(this))return false;@@ -1599,18 +1608,36 @@ void BrowserWindow::wheelEvent(QWheelEvent* event)void BrowserWindow::closeEvent(QCloseEvent* event){- if (Application::the().file_downloader().has_active_downloads()) {- if (visible_browser_window_count() <= 1) {- if (!Application::the().confirm_stop_active_downloads(this)) {- event->ignore();- return;+ if (!m_window_close_approved) {+ if (m_window_close_coordinator.is_running()) {+ event->ignore();+ return;+ }++ if (Application::the().file_downloader().has_active_downloads()) {+ if (visible_browser_window_count() <= 1) {+ if (!Application::the().confirm_stop_active_downloads(this)) {+ event->ignore();+ return;+ }}}++ begin_window_close_preflight();+ event->ignore();+ return;}+ Vector<Function<void()>> close_requests;+ for_each_tab([&](auto& tab) {+ close_requests.append(tab.prepare_for_window_close());+ });+ for (auto& close_request : close_requests)+ close_request();+clear_resize_cursor();- auto active_tab_index = static_cast<i64>(max(m_tabs_container->current_index(), 0));+ auto active_tab_index = m_window_close_active_tab_index.value_or(static_cast<i64>(max(m_tabs_container->current_index(), 0)));if (m_is_private == WebView::IsPrivate::No) {if (m_is_popup_window == IsPopupWindow::No) {@@ -1636,4 +1663,115 @@ void BrowserWindow::closeEvent(QCloseEvent* event)}}+void BrowserWindow::begin_window_close_preflight()+{+ m_window_close_active_tab_index = static_cast<i64>(max(m_tabs_container->current_index(), 0));+ m_window_close_original_active_tab = m_current_tab;+ m_window_close_current_preflight_tab = nullptr;+ m_window_close_approved_tabs.clear();++ Vector<QPointer<Tab>> tabs;+ if (m_current_tab)+ tabs.append(m_current_tab);+ for_each_tab([&](auto& tab) {+ if (&tab != m_current_tab)+ tabs.append(&tab);+ });++ Vector<WindowCloseCoordinator::Preflight> preflights;+ preflights.ensure_capacity(tabs.size());+ for (auto tab : tabs) {+ preflights.append([this, tab](Function<void(bool)> on_complete) mutable {+ if (!tab) {+ on_complete(true);+ return;+ }++ auto index = tab_index(tab);+ if (index < 0) {+ on_complete(true);+ return;+ }++ activate_tab(index);+ m_window_close_current_preflight_tab = tab;+ auto weak_this = QPointer<BrowserWindow>(this);+ tab->request_window_close_preflight([weak_this, tab, on_complete = AK::move(on_complete)](bool approved) mutable {+ if (!weak_this) {+ on_complete(approved);+ return;+ }+ if (weak_this->m_window_close_current_preflight_tab == tab)+ weak_this->m_window_close_current_preflight_tab = nullptr;+ if (approved && tab && !weak_this->m_window_close_approved_tabs.contains([&](auto const& approved_tab) { return approved_tab == tab; }))+ weak_this->m_window_close_approved_tabs.append(tab);+ on_complete(approved);+ });+ });+ }++ auto weak_this = QPointer<BrowserWindow>(this);+ m_window_close_coordinator.start(+ AK::move(preflights),+ [weak_this] {+ if (!weak_this)+ return;+ weak_this->m_window_close_approved = true;+ QTimer::singleShot(0, weak_this, [weak_this] {+ if (weak_this && weak_this->m_window_close_approved)+ weak_this->close();+ });+ },+ [weak_this] {+ if (!weak_this)+ return;+ if (auto* original_tab = weak_this->m_window_close_original_active_tab.data()) {+ auto index = weak_this->tab_index(original_tab);+ if (index >= 0)+ weak_this->activate_tab(index);+ }+ weak_this->m_window_close_active_tab_index.clear();+ weak_this->m_window_close_original_active_tab = nullptr;+ weak_this->m_window_close_current_preflight_tab = nullptr;+ weak_this->m_window_close_approved_tabs.clear();+ weak_this->for_each_tab([](auto& tab) {+ tab.cancel_window_close_preflight();+ });+ });+}++void BrowserWindow::cancel_window_close_preflight()+{+ if (m_window_close_coordinator.is_running()) {+ m_window_close_coordinator.cancel();+ return;+ }++ if (!m_window_close_approved)+ return;++ if (auto* original_tab = m_window_close_original_active_tab.data()) {+ auto index = tab_index(original_tab);+ if (index >= 0)+ activate_tab(index);+ }+ m_window_close_approved = false;+ m_window_close_active_tab_index.clear();+ m_window_close_original_active_tab = nullptr;+ m_window_close_current_preflight_tab = nullptr;+ m_window_close_approved_tabs.clear();+ for_each_tab([](auto& tab) {+ tab.cancel_window_close_preflight();+ });+}++void BrowserWindow::on_tab_load_start(Tab& tab)+{+ if (!m_window_close_coordinator.is_running() && !m_window_close_approved)+ return;++ if (m_window_close_current_preflight_tab == &tab || m_window_close_approved_tabs.contains([&](auto const& approved_tab) { return approved_tab == &tab; }))+ cancel_window_close_preflight();+}+}diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex 67fc218d4cb..3e5a5baafd2 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -19,6 +19,7 @@#include <UI/Qt/FullscreenDebounce.h>#include <UI/Qt/Tab.h>#include <UI/Qt/TabBar.h>+#include <UI/Qt/WindowCloseCoordinator.h>#include <QMainWindow>#include <QPushButton>@@ -150,6 +151,8 @@ public:void duplicate_tab(Tab&);Tab* current_tab() const { return m_current_tab; }bool activate_tab_with_url(URL::URL const&);+ void cancel_window_close_preflight();+ void on_tab_load_start(Tab&);FullscreenMode& fullscreen_mode();QMenu& hamburger_menu() const { return *m_hamburger_menu; }@@ -216,6 +219,7 @@ private:void update_window_title(QString const&);void register_window_with_session_store();void sync_session_tab_order();+ void begin_window_close_preflight();void set_current_tab(Tab* tab);Qt::Edges resize_edges_for_position(QPoint const&) const;@@ -279,6 +283,12 @@ private:IsPopupWindow m_is_popup_window { IsPopupWindow::No };Optional<WebView::SessionWindowId> m_session_window_id;+ Optional<i64> m_window_close_active_tab_index;+ QPointer<Tab> m_window_close_original_active_tab;+ QPointer<Tab> m_window_close_current_preflight_tab;+ Vector<QPointer<Tab>> m_window_close_approved_tabs;+ WindowCloseCoordinator m_window_close_coordinator;+ bool m_window_close_approved { false };ExitFullscreenButton* m_exit_button { nullptr };FullscreenMode* m_fullscreen_mode { nullptr };diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex 224648aaa29..ffb59efcb0f 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -53,6 +53,7 @@ target_sources(ladybird PRIVATETab.cppTabBar.cppWebContentView.cpp+ WindowCloseCoordinator.cppWindowControlButton.cppladybird.qrc)diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex bc8f7ca8b76..216adb0ec3a 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -738,7 +738,8 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversif (auto index_to_activate = m_window->tab_index(m_tab_to_activate_on_close); index_to_activate >= 0)m_window->activate_tab(index_to_activate);}- m_window->definitely_close_tab(index);+ if (!m_window_is_closing)+ m_window->definitely_close_tab(index);};view().on_link_hover = [this](auto const& url) {@@ -755,12 +756,12 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversview().on_loading_state_change = [this](bool is_loading) {set_loading(is_loading);};- set_loading(view().is_loading());-view().on_load_start = [this] {+ m_window->on_tab_load_start(*this);if (m_content_blocking_popover)m_content_blocking_popover->close();};+ set_loading(view().is_loading());view().on_load_finish = [this](auto const&) {m_suppress_javascript_dialogs_until_navigation = false;@@ -868,7 +869,8 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversVERIFY(on_complete);on_complete(accepted);if (!accepted) {- m_already_requested_close = false;+ if (!m_preserve_close_retry_state_on_dialog_dismissal)+ m_already_requested_close = false;m_suppress_javascript_dialogs_until_navigation = false;}break;@@ -1765,8 +1767,11 @@ void Tab::find_next()void Tab::request_close(){+ m_window->cancel_window_close_preflight();m_suppress_javascript_dialogs_until_navigation = true;+ m_preserve_close_retry_state_on_dialog_dismissal = m_already_requested_close;m_javascript_dialog->dismiss();+ m_preserve_close_retry_state_on_dialog_dismissal = false;if (!view().needs_beforeunload_check()) {auto request_close = view().prepare_for_immediate_close();@@ -1812,12 +1817,15 @@ void Tab::request_window_close_preflight(Function<void(bool)> on_complete)void Tab::cancel_window_close_preflight(){+ if (m_javascript_dialog->type() == JavaScriptDialog::Type::BeforeUnload)+ m_javascript_dialog->dismiss();m_already_requested_close = false;m_suppress_javascript_dialogs_until_navigation = false;}Function<void()> Tab::prepare_for_window_close(){+ m_window_is_closing = true;m_suppress_javascript_dialogs_until_navigation = true;m_javascript_dialog->dismiss();return view().prepare_for_close_without_prompting();diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex 2eefa631b4d..2ff15a52802 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -228,6 +228,8 @@ private:bool m_suppress_javascript_dialogs_until_navigation { false };bool m_already_requested_close { false };+ bool m_preserve_close_retry_state_on_dialog_dismissal { false };+ bool m_window_is_closing { false };};}diff --git a/UI/Qt/WindowCloseCoordinator.cpp b/UI/Qt/WindowCloseCoordinator.cppnew file mode 100644index 00000000000..6aedffec535--- /dev/null+++ b/UI/Qt/WindowCloseCoordinator.cpp@@ -0,0 +1,64 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <UI/Qt/WindowCloseCoordinator.h>++namespace Ladybird {++bool WindowCloseCoordinator::start(Vector<Preflight> preflights, Function<void()> on_approved, Function<void()> on_canceled)+{+ if (m_is_running)+ return false;++ m_preflights = move(preflights);+ m_on_approved = move(on_approved);+ m_on_canceled = move(on_canceled);+ m_next_preflight_index = 0;+ m_is_running = true;+ ++m_generation;+ run_next_preflight();+ return true;+}++void WindowCloseCoordinator::cancel()+{+ if (m_is_running)+ finish(false);+}++void WindowCloseCoordinator::run_next_preflight()+{+ if (m_next_preflight_index >= m_preflights.size()) {+ finish(true);+ return;+ }++ auto preflight = move(m_preflights[m_next_preflight_index++]);+ auto generation = m_generation;+ preflight([this, generation](bool approved) {+ if (!m_is_running || generation != m_generation)+ return;+ if (!approved) {+ finish(false);+ return;+ }+ run_next_preflight();+ });+}++void WindowCloseCoordinator::finish(bool approved)+{+ auto on_complete = approved ? move(m_on_approved) : move(m_on_canceled);+ m_is_running = false;+ ++m_generation;+ m_preflights.clear();+ m_on_approved = {};+ m_on_canceled = {};+ if (on_complete)+ on_complete();+}++}diff --git a/UI/Qt/WindowCloseCoordinator.h b/UI/Qt/WindowCloseCoordinator.hnew file mode 100644index 00000000000..1835330c434--- /dev/null+++ b/UI/Qt/WindowCloseCoordinator.h@@ -0,0 +1,35 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Function.h>+#include <AK/Vector.h>++namespace Ladybird {++class WindowCloseCoordinator {+public:+ using Preflight = Function<void(Function<void(bool)>)>;++ bool start(Vector<Preflight>, Function<void()> on_approved, Function<void()> on_canceled);+ void cancel();++ bool is_running() const { return m_is_running; }++private:+ void run_next_preflight();+ void finish(bool approved);++ Vector<Preflight> m_preflights;+ Function<void()> m_on_approved;+ Function<void()> m_on_canceled;+ size_t m_next_preflight_index { 0 };+ u64 m_generation { 0 };+ bool m_is_running { false };+};++}