Truly independent web browser
UI/Qt: Quit only once every window agrees to close
sideshowbarker14d6c73e07ab
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 568a0a0025b..77d49048dce 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -258,6 +258,19 @@ public:break;}+#if defined(AK_OS_MACOS)+ case QEvent::Quit:+ // The system asks the application to quit (from the Dock, or at logout) with a spontaneous Quit event, and+ // so does QApplication::quit(), which on macOS terminates through the system. So until every window's pages+ // have agreed, the termination is canceled and quit() asks the pages instead — and then finishes by itself.+ if (event->spontaneous() && !application.is_finishing_quit()) {+ application.quit();+ event->ignore();+ return true;+ }+ break;+#endif+default:break;}@@ -819,25 +832,111 @@ void Application::open_file()m_active_window->open_file();}+static Vector<BrowserWindow*> visible_browser_windows_active_first(BrowserWindow* active_window)+{+ Vector<BrowserWindow*> windows;+ if (active_window && active_window->isVisible())+ windows.append(active_window);+ for (auto* widget : QApplication::topLevelWidgets()) {+ if (auto* window = as_if<BrowserWindow>(widget); window && window != active_window && window->isVisible())+ windows.append(window);+ }+ return windows;+}+void Application::quit(){- if (!confirm_stop_active_downloads(active_window_if_any()))+ // A quit is already asking the pages, so a repeated one does nothing.+ if (m_quit_in_progress)return;- WebView::Application::default_session().session_store->application_quitting();+ m_quit_in_progress = true;+ m_windows_awaiting_quit_preflight.clear();++ // The window the user is looking at gets asked first.+ for (auto* window : visible_browser_windows_active_first(m_active_window))+ m_windows_awaiting_quit_preflight.append(window);+ ask_next_window_to_close_for_quit();+}- QApplication::closeAllWindows();+void Application::ask_next_window_to_close_for_quit()+{+ while (!m_windows_awaiting_quit_preflight.is_empty()) {+ auto window = m_windows_awaiting_quit_preflight.take_first();+ if (!window)+ continue;- for (auto* widget : QApplication::topLevelWidgets()) {- if (as_if<BrowserWindow>(widget) && widget->isVisible()) {- WebView::Application::default_session().session_store->application_quit_aborted();- return;- }+ window->request_close_preflight_for_quit([this](bool approved) {+ if (approved)+ ask_next_window_to_close_for_quit();+ else+ cancel_quit();+ });+ return;+ }++ // The quit finishes on a later turn of the event loop, so that it never terminates the application from inside a+ // page's reply, or from inside the system's own request to quit.+ QTimer::singleShot(0, m_application.ptr(), [this] {+ finish_quit();+ });+}++bool Application::ask_windows_yet_to_agree_to_quit()+{+ for (auto* window : visible_browser_windows_active_first(m_active_window)) {+ if (!window->is_window_close_approved())+ m_windows_awaiting_quit_preflight.append(window);+ }+ if (m_windows_awaiting_quit_preflight.is_empty())+ return false;++ ask_next_window_to_close_for_quit();+ return true;+}++void Application::finish_quit()+{+ // A window can open while the quit asks the others, and a navigation or a closed tab withdraws a window's approval,+ // so any window that hasn't agreed yet gets asked before anything closes.+ if (ask_windows_yet_to_agree_to_quit())+ return;++ // Downloads are stopped only once every page has agreed, so a page that keeps the windows open leaves them running.+ if (!confirm_stop_active_downloads(active_window_if_any())) {+ cancel_quit();+ return;}+ // The downloads prompt runs an event loop of its own, in which windows can open or lose their approval too.+ if (ask_windows_yet_to_agree_to_quit())+ return;++ // 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();++ // 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);+ for (auto* window : windows.in_reverse())+ window->close();+QApplication::quit();}+void Application::cancel_quit()+{+ m_quit_in_progress = false;+ m_windows_awaiting_quit_preflight.clear();++ // The windows whose pages already agreed go back to needing their own close check.+ for (auto* widget : QApplication::topLevelWidgets()) {+ if (auto* window = as_if<BrowserWindow>(widget))+ window->cancel_window_close_preflight();+ }+}+bool Application::confirm_stop_active_downloads(QWidget* parent){auto& downloader = file_downloader();diff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex 2161d0c1fe0..d8695e11182 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -17,6 +17,7 @@#include <UI/Qt/CrashReportNotifier.h>#include <QApplication>+#include <QPointer>class QAction;class QMenu;@@ -60,7 +61,10 @@ public:void restart_private_browsing_session();void focus_location_editor();void show_process_manager();+ // Asks every window's pages if they may unload, one window at a time. Once all of them agree, it confirms stopping+ // the downloads and quits; if a page refuses or the downloads are kept, the quit is canceled and no window closes.void quit();+ bool is_finishing_quit() const { return m_quit_is_finishing; }bool confirm_stop_active_downloads(QWidget* parent = nullptr);CrashReportNotifier& crash_report_notifier() { return m_crash_report_notifier; }@@ -105,6 +109,11 @@ public:private:explicit Application();+ void ask_next_window_to_close_for_quit();+ bool ask_windows_yet_to_agree_to_quit();+ void finish_quit();+ void cancel_quit();+virtual void create_platform_options(WebView::BrowserOptions&, WebView::RequestServerOptions&, WebView::WebContentOptions&) override;virtual Core::EventLoop& create_platform_event_loop() override;virtual void create_platform_actions() override;@@ -176,6 +185,10 @@ private:BrowserWindow* m_active_window { nullptr };OwnPtr<ProcessManagerWindow> m_process_manager_window;CrashReportNotifier m_crash_report_notifier;++ bool m_quit_in_progress { false };+ bool m_quit_is_finishing { false };+ Vector<QPointer<BrowserWindow>> m_windows_awaiting_quit_preflight;};}diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex ba117b81e34..7bffd3e0361 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -1663,7 +1663,35 @@ void BrowserWindow::closeEvent(QCloseEvent* event)}}-void BrowserWindow::begin_window_close_preflight()+void BrowserWindow::request_close_preflight_for_quit(Function<void(bool approved)> on_complete)+{+ if (m_window_close_approved) {+ on_complete(true);+ return;+ }++ // A close the user already started on this window gives way to the quit's check, which asks every page again.+ if (m_window_close_coordinator.is_running())+ cancel_window_close_preflight();++ begin_window_close_preflight(AK::move(on_complete));+}++void BrowserWindow::raise_for_quit_prompt()+{+ // Only a quit's check carries a completion; a close the user started on this window is already in front.+ if (!m_window_close_coordinator.is_running() || !m_window_close_preflight_on_complete)+ return;++ // A page's beforeunload prompt shows inside this window, which may sit behind other windows or in the Dock while a+ // quit asks it, so the window comes to the front first, where the user can see the prompt.+ if (isMinimized())+ setWindowState(windowState() & ~Qt::WindowMinimized);+ raise();+ activateWindow();+}++void BrowserWindow::begin_window_close_preflight(Function<void(bool approved)> on_complete){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;@@ -1710,6 +1738,10 @@ void BrowserWindow::begin_window_close_preflight()});}+ // With a completion, approval only marks the window as approved (so that a later close() goes straight through),+ // and the caller decides when to close it.+ m_window_close_preflight_on_complete = AK::move(on_complete);+auto weak_this = QPointer<BrowserWindow>(this);m_window_close_coordinator.start(AK::move(preflights),@@ -1717,6 +1749,17 @@ void BrowserWindow::begin_window_close_preflight()if (!weak_this)return;weak_this->m_window_close_approved = true;+ if (auto on_complete = AK::move(weak_this->m_window_close_preflight_on_complete)) {+ // The window stays open until the caller closes it, so it goes back to the tab it showed before the+ // check, which the session store then keeps as its active tab.+ 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);+ }+ on_complete(true);+ return;+ }QTimer::singleShot(0, weak_this, [weak_this] {if (weak_this && weak_this->m_window_close_approved)weak_this->close();@@ -1737,6 +1780,8 @@ void BrowserWindow::begin_window_close_preflight()weak_this->for_each_tab([](auto& tab) {tab.cancel_window_close_preflight();});+ if (auto on_complete = AK::move(weak_this->m_window_close_preflight_on_complete))+ on_complete(false);});}diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex 3e5a5baafd2..61a2333e2fa 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -151,6 +151,12 @@ public:void duplicate_tab(Tab&);Tab* current_tab() const { return m_current_tab; }bool activate_tab_with_url(URL::URL const&);+ // Asks every tab's page if the window may close, without closing it, and reports the answer. Once the pages agree,+ // a close() goes straight through.+ void request_close_preflight_for_quit(Function<void(bool approved)> on_complete);+ // Brings the window forward for a page's beforeunload prompt, when the prompt is part of a quit's check.+ void raise_for_quit_prompt();+ bool is_window_close_approved() const { return m_window_close_approved; }void cancel_window_close_preflight();void on_tab_load_start(Tab&);FullscreenMode& fullscreen_mode();@@ -219,7 +225,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 begin_window_close_preflight(Function<void(bool approved)> on_complete = {});void set_current_tab(Tab* tab);Qt::Edges resize_edges_for_position(QPoint const&) const;@@ -288,6 +294,7 @@ private:QPointer<Tab> m_window_close_current_preflight_tab;Vector<QPointer<Tab>> m_window_close_approved_tabs;WindowCloseCoordinator m_window_close_coordinator;+ Function<void(bool approved)> m_window_close_preflight_on_complete;bool m_window_close_approved { false };ExitFullscreenButton* m_exit_button { nullptr };diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 216adb0ec3a..b16550c5870 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -834,6 +834,7 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversview().on_request_before_unload = [this](auto const& source, auto on_complete) {hide_hover_label();m_before_unload_completion = AK::move(on_complete);+ m_window->raise_for_quit_prompt();m_javascript_dialog->show_before_unload(qstring_from_ak_string(source));};