Truly independent web browser
Hub+UI/Qt: Mirror downloads in the UI
Andreas Kling55cd8ffec2a8
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex c22c8134bbd..085466e0cfb 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -142,6 +142,85 @@ void ConnectionFromUI::answer_download_path(u64 request_id, Optional<ByteString>Application::the().did_get_download_path(request_id, move(path));}+static DownloadState state_of_download(WebView::FileDownloader::Download const& download)+{+ DownloadState state;+ state.id = download.id;+ state.is_private = download.is_private;+ state.url = download.url;+ state.destination = download.destination.string();+ state.status = download.status;+ state.downloaded_size = download.downloaded_size;+ state.total_size = download.total_size;+ state.error = download.error;+ state.can_resume = download.can_resume;+ state.connection_count = download.connection_count;+ state.bytes_per_second = download.bytes_per_second;+ state.is_waiting_to_retry = download.is_waiting_to_retry;+ state.created_time = download.created_time;+ return state;+}++Vector<DownloadState> ConnectionFromUI::downloads()+{+ Vector<DownloadState> downloads;+ for (auto const& download : Application::the().file_downloader().downloads())+ downloads.append(state_of_download(download));+ return downloads;+}++void ConnectionFromUI::download_added(WebView::FileDownloader::Download const& download)+{+ async_download_added(state_of_download(download));+}++void ConnectionFromUI::download_updated(WebView::FileDownloader::Download const& download)+{+ async_download_updated(state_of_download(download));+}++void ConnectionFromUI::download_removed(u64 id, WebView::IsPrivate is_private)+{+ async_download_removed(id, is_private);+}++void ConnectionFromUI::cancel_download(u64 id)+{+ Application::the().file_downloader().cancel_download(id);+}++void ConnectionFromUI::pause_download(u64 id)+{+ Application::the().file_downloader().pause_download(id);+}++void ConnectionFromUI::resume_download(u64 id)+{+ Application::the().file_downloader().resume_download(id);+}++void ConnectionFromUI::show_download_in_folder(u64 id)+{+ auto& application = static_cast<WebView::Application&>(Application::the());+ if (auto download = application.file_downloader().download(id); download.has_value())+ application.show_download_in_folder(*download);+}++void ConnectionFromUI::prune_inactive_downloads(WebView::IsPrivate is_private)+{+ (void)Application::the().file_downloader().prune_inactive_downloads(is_private);+}++void ConnectionFromUI::pause_active_downloads()+{+ Application::the().file_downloader().pause_active_downloads();+}++void ConnectionFromUI::cancel_unresumable_downloads()+{+ Application::the().file_downloader().cancel_unresumable_downloads();+}+void ConnectionFromUI::update_settings(ByteString settings_json){auto settings = JsonValue::from_string(settings_json);diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex 7a611ac08ca..85551a2f24f 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -10,11 +10,14 @@#include <Hub/HubServerEndpoint.h>#include <Hub/View.h>#include <LibIPC/ConnectionFromClient.h>+#include <LibWebView/FileDownloader.h>namespace Hub {// The Hub's end of its connection to the UI.-class ConnectionFromUI final : public IPC::ConnectionFromClient<HubClientEndpoint, HubServerEndpoint> {+class ConnectionFromUI final+ : public IPC::ConnectionFromClient<HubClientEndpoint, HubServerEndpoint>+ , public WebView::FileDownloaderObserver {C_OBJECT(ConnectionFromUI);public:@@ -22,6 +25,8 @@ public:static ConnectionFromUI* the();+ static Vector<DownloadState> downloads();+// The ID that the UI gave an input event, from the browser data of the event.static u64 input_id_of(Web::BrowserInputData const&);@@ -32,7 +37,19 @@ private:static View* find_view(u64 view_id);+ // ^WebView::FileDownloaderObserver+ virtual void download_added(WebView::FileDownloader::Download const&) override;+ virtual void download_updated(WebView::FileDownloader::Download const&) override;+ virtual void download_removed(u64 id, WebView::IsPrivate) override;+// ^HubServerEndpoint+ virtual void cancel_download(u64 id) override;+ virtual void pause_download(u64 id) override;+ virtual void resume_download(u64 id) override;+ virtual void show_download_in_folder(u64 id) override;+ virtual void prune_inactive_downloads(WebView::IsPrivate is_private) override;+ virtual void pause_active_downloads() override;+ virtual void cancel_unresumable_downloads() override;virtual void active_view_changed(Optional<u64> view_id) override;virtual void answer_clipboard_text(u64 request_id, Utf16String text) override;virtual void answer_clipboard_item(u64 request_id, Web::Clipboard::SystemClipboardItem item) override;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex fe12e087b84..4988f855203 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -12,6 +12,7 @@#include <LibWebCommon/HTML/FileFilter.h>#include <LibWebCommon/HTML/SelectItem.h>#include <LibWebView/AccessibilityTreeManager.h>+#include <LibWebView/FileDownloader.h>#include <LibWebView/Menu.h>#include <LibWebView/TabPerformanceStats.h>#include <LibWebCommon/Clipboard/SystemClipboard.h>@@ -147,6 +148,22 @@ struct Hub::ViewActions {Vector<Hub::MenuEntry> bookmark_folder_context_menu;}+struct Hub::DownloadState {+ u64 id;+ WebView::IsPrivate is_private;+ URL::URL url;+ ByteString destination;+ WebView::FileDownloader::DownloadStatus status;+ u64 downloaded_size;+ Optional<u64> total_size;+ Optional<String> error;+ bool can_resume;+ u32 connection_count;+ Optional<u64> bytes_per_second;+ bool is_waiting_to_retry;+ UnixDateTime created_time;+}+// What the UI needs to know about a view at any time, without having to ask.struct Hub::ViewState {URL::URL url;@@ -178,6 +195,9 @@ struct Hub::ViewState {// What the Hub tells the UI.endpoint HubClient{+ download_added(Hub::DownloadState download) =|+ download_updated(Hub::DownloadState download) =|+ download_removed(u64 id, WebView::IsPrivate is_private) =|app_open_url_in_new_tab(URL::URL url, Web::HTML::ActivateTab activate_tab) =|app_open_url_in_new_window(URL::URL url, WebView::IsPrivate is_private) =|app_open_navigation_in_new_tab(Web::HTML::PreparedNavigationDescriptor navigation, Web::HTML::ActivateTab activate_tab) =|diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex 107d86da76c..fadae50c741 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -20,6 +20,13 @@ struct WebView::ViewImplementation::ScreenRects {// What the UI asks of the Hub.endpoint HubServer{+ cancel_download(u64 id) =|+ pause_download(u64 id) =|+ resume_download(u64 id) =|+ show_download_in_folder(u64 id) =|+ prune_inactive_downloads(WebView::IsPrivate is_private) =|+ pause_active_downloads() =|+ cancel_unresumable_downloads() =|active_view_changed(Optional<u64> view_id) =|answer_clipboard_text(u64 request_id, Utf16String text) =|answer_clipboard_item(u64 request_id, Web::Clipboard::SystemClipboardItem item) =|diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex fc0c5ce4389..d70df5dd511 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -33,6 +33,7 @@#include <UI/Qt/PlainText.h>#include <UI/Qt/ProcessManagerWindow.h>#include <UI/Qt/RemoteActions.h>+#include <UI/Qt/RemoteDownloads.h>#include <UI/Qt/StringUtils.h>#include <UI/Qt/Tab.h>#include <UI/Qt/Theme.h>@@ -689,6 +690,7 @@ void Application::create_platform_actions()};RemoteActions::the().set_application_actions(Hub::ActionMirror::the().application_actions());+ RemoteDownloads::the().set_downloads(Hub::ConnectionFromUI::downloads());RemoteActions::the().inspect_menu().add_action(WebView::Action::create("Open Task Manager"sv, WebView::ActionID::OpenTaskManager, [this]() {show_process_manager();@@ -1351,7 +1353,7 @@ void Application::cancel_quit()bool Application::confirm_stop_active_downloads(QWidget* parent){- auto& downloader = hub().file_downloader();+ auto& downloader = RemoteDownloads::the();if (downloader.has_unresumable_downloads()) {QMessageBox dialog(parent ? parent : active_window_if_any());diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex 7ae44cb3ce9..0730dc87d93 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -28,6 +28,7 @@#include <UI/Qt/Menu.h>#include <UI/Qt/Motion.h>#include <UI/Qt/Popover.h>+#include <UI/Qt/RemoteDownloads.h>#include <UI/Qt/StringUtils.h>#include <UI/Qt/SystemAppearance.h>#if defined(AK_OS_MACOS)@@ -1160,7 +1161,7 @@ bool BrowserWindow::close_tab(int index, WebView::SessionStore::KeepForReopening{cancel_window_close_preflight();- if (m_tabs_container->count() == 1 && Application::hub().file_downloader().has_active_downloads() && visible_browser_window_count() <= 1) {+ if (m_tabs_container->count() == 1 && RemoteDownloads::the().has_active_downloads() && visible_browser_window_count() <= 1) {if (!Application::the().confirm_stop_active_downloads(this))return false;}@@ -2028,7 +2029,7 @@ void BrowserWindow::closeEvent(QCloseEvent* event)return;}- if (Application::hub().file_downloader().has_active_downloads()) {+ if (RemoteDownloads::the().has_active_downloads()) {if (visible_browser_window_count() <= 1) {if (!Application::the().confirm_stop_active_downloads(this)) {event->ignore();diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex 8d404fa3707..c296ba1cb4b 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -44,6 +44,7 @@ add_library(ladybird_impl STATICPopover.cppProcessManagerWindow.cppRemoteActions.cpp+ RemoteDownloads.cppRemoteView.cppReportReview.cppRestoreSessionCard.cppdiff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cppindex f7439023677..f5655ffca79 100644--- a/UI/Qt/HubConnection.cpp+++ b/UI/Qt/HubConnection.cpp@@ -8,6 +8,7 @@#include <UI/Qt/Application.h>#include <UI/Qt/HubConnection.h>#include <UI/Qt/RemoteActions.h>+#include <UI/Qt/RemoteDownloads.h>#include <UI/Qt/RemoteView.h>namespace Ladybird {@@ -280,6 +281,21 @@ void HubConnection::app_ask_user_for_download_path(u64 request_id, ByteString fiApplication::the().ask_user_for_download_path(request_id, file);}+void HubConnection::download_added(Hub::DownloadState download)+{+ RemoteDownloads::the().did_add(download);+}++void HubConnection::download_updated(Hub::DownloadState download)+{+ RemoteDownloads::the().did_update(download);+}++void HubConnection::download_removed(u64 id, WebView::IsPrivate is_private)+{+ RemoteDownloads::the().did_remove(id, is_private);+}+void HubConnection::settings_changed(ByteString settings_json){auto settings = JsonValue::from_string(settings_json);diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.hindex adfd1c186b5..a61c0a694a9 100644--- a/UI/Qt/HubConnection.h+++ b/UI/Qt/HubConnection.h@@ -29,6 +29,9 @@ private:virtual void die() override;// ^HubClientEndpoint+ virtual void download_added(Hub::DownloadState download) override;+ virtual void download_updated(Hub::DownloadState download) override;+ virtual void download_removed(u64 id, WebView::IsPrivate is_private) override;virtual void app_open_url_in_new_tab(URL::URL url, Web::HTML::ActivateTab activate_tab) override;virtual void app_open_url_in_new_window(URL::URL url, WebView::IsPrivate is_private) override;virtual void app_open_navigation_in_new_tab(Web::HTML::PreparedNavigationDescriptor navigation, Web::HTML::ActivateTab activate_tab) override;diff --git a/UI/Qt/RemoteDownloads.cpp b/UI/Qt/RemoteDownloads.cppnew file mode 100644index 00000000000..1006cf41756--- /dev/null+++ b/UI/Qt/RemoteDownloads.cpp@@ -0,0 +1,144 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <UI/Qt/HubConnection.h>+#include <UI/Qt/RemoteDownloads.h>++namespace Ladybird {++RemoteDownloads& RemoteDownloads::the()+{+ static RemoteDownloads s_the;+ return s_the;+}++static RemoteDownloads::Download download_from(Hub::DownloadState const& state)+{+ return {+ .id = state.id,+ .is_private = state.is_private,+ .url = state.url,+ .destination = LexicalPath { state.destination },+ .status = state.status,+ .downloaded_size = state.downloaded_size,+ .total_size = state.total_size,+ .error = state.error,+ .can_resume = state.can_resume,+ .connection_count = state.connection_count,+ .bytes_per_second = state.bytes_per_second,+ .is_waiting_to_retry = state.is_waiting_to_retry,+ .created_time = state.created_time,+ };+}++void RemoteDownloads::set_downloads(ReadonlySpan<Hub::DownloadState> downloads)+{+ m_downloads.clear();+ for (auto const& download : downloads)+ m_downloads.append(download_from(download));+}++void RemoteDownloads::did_add(Hub::DownloadState const& state)+{+ m_downloads.append(download_from(state));+ for (auto& observer : m_observers)+ observer.download_added(m_downloads.last());+}++void RemoteDownloads::did_update(Hub::DownloadState const& state)+{+ for (auto& download : m_downloads) {+ if (download.id != state.id)+ continue;+ download = download_from(state);+ for (auto& observer : m_observers)+ observer.download_updated(download);+ return;+ }+}++void RemoteDownloads::did_remove(u64 id, WebView::IsPrivate is_private)+{+ m_downloads.remove_first_matching([&](auto const& download) { return download.id == id; });+ for (auto& observer : m_observers)+ observer.download_removed(id, is_private);+}++Vector<RemoteDownloads::Download> RemoteDownloads::downloads_of_session(WebView::IsPrivate is_private) const+{+ Vector<Download> downloads;+ for (auto const& download : m_downloads) {+ if (download.is_private == is_private)+ downloads.append(download);+ }+ return downloads;+}++Optional<RemoteDownloads::Download const&> RemoteDownloads::download(u64 id) const+{+ for (auto const& download : m_downloads) {+ if (download.id == id)+ return download;+ }+ return {};+}++bool RemoteDownloads::has_active_downloads() const+{+ return any_of(m_downloads, [](auto const& download) { return WebView::FileDownloader::status_is_active(download.status); });+}++bool RemoteDownloads::has_unresumable_downloads() const+{+ return any_of(m_downloads, [](auto const& download) { return WebView::FileDownloader::status_is_active(download.status) && !download.can_resume; });+}++void RemoteDownloads::cancel_download(u64 id)+{+ HubConnection::the().async_cancel_download(id);+}++void RemoteDownloads::pause_download(u64 id)+{+ HubConnection::the().async_pause_download(id);+}++void RemoteDownloads::resume_download(u64 id)+{+ HubConnection::the().async_resume_download(id);+}++void RemoteDownloads::show_in_folder(u64 id)+{+ HubConnection::the().async_show_download_in_folder(id);+}++void RemoteDownloads::prune_inactive_downloads(WebView::IsPrivate is_private)+{+ HubConnection::the().async_prune_inactive_downloads(is_private);+}++void RemoteDownloads::pause_active_downloads()+{+ HubConnection::the().async_pause_active_downloads();+}++void RemoteDownloads::cancel_unresumable_downloads()+{+ HubConnection::the().async_cancel_unresumable_downloads();+}++RemoteDownloadsObserver::RemoteDownloadsObserver()+{+ RemoteDownloads::the().m_observers.append(*this);+}++RemoteDownloadsObserver::~RemoteDownloadsObserver()+{+ RemoteDownloads::the().m_observers.remove_first_matching([&](auto& observer) { return &observer == this; });+}++}diff --git a/UI/Qt/RemoteDownloads.h b/UI/Qt/RemoteDownloads.hnew file mode 100644index 00000000000..5b9b0f400b3--- /dev/null+++ b/UI/Qt/RemoteDownloads.h@@ -0,0 +1,59 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Vector.h>+#include <Hub/HubClientEndpoint.h>+#include <LibWebView/FileDownloader.h>++namespace Ladybird {++class RemoteDownloadsObserver;++// The Hub's downloads, as the UI shows them.+class RemoteDownloads {+public:+ using Download = WebView::FileDownloader::Download;++ static RemoteDownloads& the();++ void set_downloads(ReadonlySpan<Hub::DownloadState>);+ void did_add(Hub::DownloadState const&);+ void did_update(Hub::DownloadState const&);+ void did_remove(u64 id, WebView::IsPrivate);++ Vector<Download> downloads_of_session(WebView::IsPrivate) const;+ Optional<Download const&> download(u64 id) const;+ bool has_active_downloads() const;+ bool has_unresumable_downloads() const;++ void cancel_download(u64 id);+ void pause_download(u64 id);+ void resume_download(u64 id);+ void show_in_folder(u64 id);+ void prune_inactive_downloads(WebView::IsPrivate);+ void pause_active_downloads();+ void cancel_unresumable_downloads();++private:+ friend class RemoteDownloadsObserver;++ Vector<Download> m_downloads;+ Vector<RemoteDownloadsObserver&> m_observers;+};++class RemoteDownloadsObserver {+public:+ RemoteDownloadsObserver();+ virtual ~RemoteDownloadsObserver();++ virtual void download_added(RemoteDownloads::Download const&) { }+ virtual void download_updated(RemoteDownloads::Download const&) { }+ virtual void download_removed(u64, WebView::IsPrivate) { }+};++}diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cppindex 651e45eaec4..c216f13d2bd 100644--- a/UI/Qt/WindowToolbar.cpp+++ b/UI/Qt/WindowToolbar.cpp@@ -30,6 +30,7 @@#include <UI/Qt/PerformanceMonitorWidget.h>#include <UI/Qt/Popover.h>#include <UI/Qt/RemoteActions.h>+#include <UI/Qt/RemoteDownloads.h>#include <UI/Qt/StringUtils.h>#include <UI/Qt/SystemAppearance.h>#include <UI/Qt/Theme.h>@@ -1947,7 +1948,7 @@ void WindowToolbar::recreate_icons()void WindowToolbar::update_downloads_button(){- auto downloads = WebView::Application::the().file_downloader().downloads_of_session(m_is_private);+ auto downloads = RemoteDownloads::the().downloads_of_session(m_is_private);auto button_state = WebView::downloads_button_state(downloads);if (!button_state.has_downloads && m_downloads_popover)@@ -1977,7 +1978,7 @@ void WindowToolbar::update_downloads_popover()if (!m_downloads_popover || !m_downloads_popover->isVisible())return;- if (m_downloads_popover->set_downloads(WebView::Application::the().file_downloader().downloads_of_session(m_is_private)))+ if (m_downloads_popover->set_downloads(RemoteDownloads::the().downloads_of_session(m_is_private)))position_downloads_popover();}@@ -2041,7 +2042,7 @@ void WindowToolbar::show_feedback_popover()// The downloads hang from the downloads button. Without any, the downloads page opens instead.void WindowToolbar::show_downloads_popover(){- auto downloads = WebView::Application::the().file_downloader().downloads_of_session(m_is_private);+ auto downloads = RemoteDownloads::the().downloads_of_session(m_is_private);if (!WebView::downloads_button_state(downloads).has_downloads) {m_open_downloads_page_action->trigger();return;@@ -2054,25 +2055,25 @@ void WindowToolbar::show_downloads_popover()m_downloads_button->setChecked(visible);};m_downloads_popover->on_cancel_download = [this](u64 id) {- auto& file_downloader = WebView::Application::the().file_downloader();+ auto& file_downloader = RemoteDownloads::the();if (auto download = file_downloader.download(id); download.has_value() && WebView::FileDownloader::status_is_active(download->status))file_downloader.cancel_download(id);update_downloads_popover();};m_downloads_popover->on_pause_download = [this](u64 id) {- WebView::Application::the().file_downloader().pause_download(id);+ RemoteDownloads::the().pause_download(id);update_downloads_popover();};m_downloads_popover->on_resume_download = [this](u64 id) {- WebView::Application::the().file_downloader().resume_download(id);+ RemoteDownloads::the().resume_download(id);update_downloads_popover();};m_downloads_popover->on_show_in_folder = [](u64 id) {- if (auto download = WebView::Application::the().file_downloader().download(id); download.has_value())- WebView::Application::the().show_download_in_folder(*download);+ if (auto download = RemoteDownloads::the().download(id); download.has_value())+ RemoteDownloads::the().show_in_folder(id);};m_downloads_popover->on_clear_finished = [this] {- (void)WebView::Application::the().file_downloader().prune_inactive_downloads(m_is_private);+ RemoteDownloads::the().prune_inactive_downloads(m_is_private);update_downloads_popover();};m_downloads_popover->on_open_all_downloads = [this] {@@ -2083,7 +2084,7 @@ void WindowToolbar::show_downloads_popover()}m_downloads_popover->update_chrome_style(palette());- (void)m_downloads_popover->set_downloads(WebView::Application::the().file_downloader().downloads_of_session(m_is_private));+ (void)m_downloads_popover->set_downloads(RemoteDownloads::the().downloads_of_session(m_is_private));position_downloads_popover();m_downloads_popover->show();position_downloads_popover();diff --git a/UI/Qt/WindowToolbar.h b/UI/Qt/WindowToolbar.hindex 593325e4eea..42d7d3726ac 100644--- a/UI/Qt/WindowToolbar.h+++ b/UI/Qt/WindowToolbar.h@@ -12,6 +12,7 @@#include <LibWebView/Forward.h>#include <LibWebView/Settings.h>#include <UI/Qt/Menu.h>+#include <UI/Qt/RemoteDownloads.h>#include <QPointer>#include <QRect>@@ -47,7 +48,7 @@ enum class ToolbarArrangement : u8 {// toolbar holds while that tab is shown.class WindowToolbar final: public QWidget- , public WebView::FileDownloaderObserver+ , public RemoteDownloadsObserver, public WebView::SettingsObserver {Q_OBJECT