Skip to content

Truly independent web browser

LibWebView+Hub+UI/Qt: Answer about:downloads in the UI

Andreas Kling50e0d7e03b69
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index ed566d51a0e..fb2a0a6fc85 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -94,7 +94,6 @@ set(SOURCES
WebUI.cpp
WebUI/ApplicationWebUI.cpp
WebUI/CrashReportsUI.cpp
- WebUI/DownloadsUI.cpp
WebUI/SettingsUI.cpp
WebWorkerClient.cpp
WorkerAgentManager.cpp
diff --git a/Libraries/LibWebView/WebUI.cpp b/Libraries/LibWebView/WebUI.cpp
index a9c9fc819a8..441ee7f89b8 100644
--- a/Libraries/LibWebView/WebUI.cpp
+++ b/Libraries/LibWebView/WebUI.cpp
@@ -12,7 +12,6 @@
#include <LibWebView/WebUI.h>
#include <LibWebView/WebUI/ApplicationWebUI.h>
#include <LibWebView/WebUI/CrashReportsUI.h>
-#include <LibWebView/WebUI/DownloadsUI.h>
#include <LibWebView/WebUI/SettingsUI.h>
namespace WebView {
@@ -21,7 +20,6 @@ static constexpr auto s_pages = to_array<WebUI::Page>({
{ "about"sv, "About URLs"sv, WebUI::PageType::Static },
{ "blocking"sv, "Blocking"sv, WebUI::PageType::Static },
{ "crashes"sv, "Crash Reports"sv, WebUI::PageType::Dynamic },
- { "downloads"sv, "Downloads"sv, WebUI::PageType::Dynamic },
{ "newtab"sv, "New Tab"sv, WebUI::PageType::Static },
{ "settings"sv, "Settings"sv, WebUI::PageType::Dynamic },
{ "services"sv, "Services"sv, WebUI::PageType::Static },
@@ -95,8 +93,6 @@ ErrorOr<RefPtr<WebUI>> WebUI::create(WebContentClient& client, Web::PageId page_
web_ui = TRY(create_web_ui<ApplicationWebUI>(client, page_id, move(host)));
else if (page->host == "crashes"sv)
web_ui = TRY(create_web_ui<CrashReportsUI>(client, page_id, move(host)));
- else if (page->host == "downloads"sv)
- web_ui = TRY(create_web_ui<DownloadsUI>(client, page_id, move(host)));
else if (page->host.is_one_of("settings"sv, "welcome"sv))
web_ui = TRY(create_web_ui<SettingsUI>(client, page_id, move(host)));
diff --git a/Libraries/LibWebView/WebUI/ApplicationWebUI.cpp b/Libraries/LibWebView/WebUI/ApplicationWebUI.cpp
index 3f5c4e74058..70f7f8c510f 100644
--- a/Libraries/LibWebView/WebUI/ApplicationWebUI.cpp
+++ b/Libraries/LibWebView/WebUI/ApplicationWebUI.cpp
@@ -6,6 +6,8 @@
#include <AK/HashMap.h>
#include <AK/NeverDestroyed.h>
+#include <LibWebView/WebContentClient.h>
+#include <LibWebView/WebContentPage.h>
#include <LibWebView/WebUI/ApplicationWebUI.h>
namespace WebView {
@@ -46,7 +48,7 @@ void ApplicationWebUI::register_interfaces()
m_id = s_next_id++;
web_uis().set(m_id, this);
if (callbacks().on_open)
- callbacks().on_open(m_id, host());
+ callbacks().on_open(m_id, host(), client().is_private());
}
void ApplicationWebUI::received_message(String name, JsonValue data)
@@ -55,6 +57,14 @@ void ApplicationWebUI::received_message(String name, JsonValue data)
callbacks().on_message(m_id, move(name), move(data));
}
+bool ApplicationWebUI::consume_user_activation()
+{
+ if (!has_client())
+ return false;
+ auto* page = client().page(page_id());
+ return page && page->consume_transient_user_activation();
+}
+
void ApplicationWebUI::send_to_page(String const& name, JsonValue const& data)
{
if (has_client())
diff --git a/Libraries/LibWebView/WebUI/ApplicationWebUI.h b/Libraries/LibWebView/WebUI/ApplicationWebUI.h
index 5f3b8b6deb5..02746d8a64c 100644
--- a/Libraries/LibWebView/WebUI/ApplicationWebUI.h
+++ b/Libraries/LibWebView/WebUI/ApplicationWebUI.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/Function.h>
+#include <LibWebView/BrowsingSession.h>
#include <LibWebView/Export.h>
#include <LibWebView/WebUI.h>
@@ -19,7 +20,7 @@ class WEBVIEW_API ApplicationWebUI final : public WebUI {
public:
struct Callbacks {
- Function<void(u64 id, String const& host)> on_open;
+ Function<void(u64 id, String const& host, IsPrivate)> on_open;
Function<void(u64 id, String name, JsonValue data)> on_message;
Function<void(u64 id)> on_close;
};
@@ -31,6 +32,9 @@ public:
void send_to_page(String const& name, JsonValue const& data);
+ // NB: Some actions of a page, such as opening a download, need the user's own click in it.
+ bool consume_user_activation();
+
private:
virtual void register_interfaces() override;
virtual void received_message(String name, JsonValue data) override;
diff --git a/Libraries/LibWebView/WebUI/DownloadsUI.h b/Libraries/LibWebView/WebUI/DownloadsUI.h
deleted file mode 100644
index 76a5550e895..00000000000
--- a/Libraries/LibWebView/WebUI/DownloadsUI.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWebView/FileDownloader.h>
-#include <LibWebView/WebUI.h>
-
-namespace WebView {
-
-class DownloadsUI final
- : public WebUI
- , public FileDownloaderObserver {
- WEB_UI(DownloadsUI);
-
-private:
- virtual void register_interfaces() override;
-
- virtual void download_added(FileDownloader::Download const&) override;
- virtual void download_updated(FileDownloader::Download const&) override;
- virtual void download_removed(u64, IsPrivate) override;
-
- void load_downloads();
- void prune_inactive_downloads();
- void cancel_download(JsonValue const&);
- void pause_download(JsonValue const&);
- void resume_download(JsonValue const&);
- void open_download(JsonValue const&);
- void show_download_in_folder(JsonValue const&);
-};
-
-}
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index ba047e8ebd9..150cc2680ee 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -33,7 +33,7 @@ ConnectionFromUI::ConnectionFromUI(NonnullOwnPtr<IPC::Transport> transport)
async_settings_changed(settings.serialized().to_byte_string(), m_last_applied_settings_change_id);
};
WebView::ApplicationWebUI::set_callbacks({
- .on_open = [this](u64 id, String const& host) { async_web_ui_opened(id, host); },
+ .on_open = [this](u64 id, String const& host, WebView::IsPrivate is_private) { async_web_ui_opened(id, host, is_private); },
.on_message = [this](u64 id, String name, JsonValue data) { async_web_ui_message(id, move(name), move(data)); },
.on_close = [this](u64 id) { async_web_ui_closed(id); },
});
@@ -187,6 +187,7 @@ static DownloadState state_of_download(WebView::FileDownloader::Download const&
state.bytes_per_second = download.bytes_per_second;
state.is_waiting_to_retry = download.is_waiting_to_retry;
state.created_time = download.created_time;
+ state.segments = Application::the().file_downloader().segment_progress(download.id);
return state;
}
@@ -228,6 +229,13 @@ void ConnectionFromUI::resume_download(u64 id)
Application::the().file_downloader().resume_download(id);
}
+void ConnectionFromUI::open_download(u64 id)
+{
+ auto& application = static_cast<WebView::Application&>(Application::the());
+ if (auto download = application.file_downloader().download(id); download.has_value())
+ application.open_download(*download);
+}
+
void ConnectionFromUI::show_download_in_folder(u64 id)
{
auto& application = static_cast<WebView::Application&>(Application::the());
@@ -484,6 +492,12 @@ void ConnectionFromUI::send_web_ui_message(u64 web_ui_id, String name, JsonValue
web_ui->send_to_page(name, data);
}
+Messages::HubServer::WebUiConsumeUserActivationResponse ConnectionFromUI::web_ui_consume_user_activation(u64 web_ui_id)
+{
+ auto* web_ui = WebView::ApplicationWebUI::find(web_ui_id);
+ return web_ui && web_ui->consume_user_activation();
+}
+
Messages::HubServer::InitializeResponse ConnectionFromUI::initialize()
{
auto const& options = Application::browser_options();
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index 56562e18101..2d76b49eda9 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -62,6 +62,7 @@ private:
virtual void ui_did_start() override;
virtual void add_web_ui_page(String host, String title) override;
virtual void send_web_ui_message(u64 web_ui_id, String name, JsonValue data) override;
+ virtual Messages::HubServer::WebUiConsumeUserActivationResponse web_ui_consume_user_activation(u64 web_ui_id) override;
virtual void start_request(u64 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> headers, ByteString body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private) override;
virtual void stop_request(u64 request_id) override;
virtual void toggle_devtools_enabled() override;
@@ -122,6 +123,7 @@ private:
virtual void cancel_download(u64 id) override;
virtual void pause_download(u64 id) override;
virtual void resume_download(u64 id) override;
+ virtual void open_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;
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index 0773ed0a18e..404ac4e2bb2 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -81,6 +81,13 @@ struct WebView::ViewImplementation::InputMethodState {
Optional<Web::DevicePixelRect> caret_rect;
}
+[ExistingCpp]
+struct WebView::FileDownloader::SegmentProgress {
+ u64 start_offset;
+ u64 end_offset;
+ u64 next_offset;
+}
+
struct Hub::DownloadState {
u64 id;
WebView::IsPrivate is_private;
@@ -95,6 +102,7 @@ struct Hub::DownloadState {
Optional<u64> bytes_per_second;
bool is_waiting_to_retry;
UnixDateTime created_time;
+ Vector<WebView::FileDownloader::SegmentProgress> segments;
}
// What only the UI can tell, which the Hub needs from the start.
@@ -261,7 +269,7 @@ endpoint HubClient
app_read_clipboard_item(u64 request_id) =|
app_ask_user_for_download_path(u64 request_id, ByteString file) =|
settings_changed(ByteString settings_json, u64 last_applied_change_id) =|
- web_ui_opened(u64 web_ui_id, String host) =|
+ web_ui_opened(u64 web_ui_id, String host, WebView::IsPrivate is_private) =|
web_ui_message(u64 web_ui_id, String name, JsonValue data) =|
web_ui_closed(u64 web_ui_id) =|
view_request_context_menu(u64 view_id, WebView::ContextMenuRequest request) =|
diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipc
index 9bd99f88bd1..9458643f11a 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -117,6 +117,7 @@ endpoint HubServer
ui_did_start() =|
add_web_ui_page(String host, String title) =|
send_web_ui_message(u64 web_ui_id, String name, JsonValue data) =|
+ web_ui_consume_user_activation(u64 web_ui_id) => (bool consumed)
start_request(u64 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> headers, ByteString body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private) =|
stop_request(u64 request_id) =|
@@ -190,6 +191,7 @@ endpoint HubServer
cancel_download(u64 id) =|
pause_download(u64 id) =|
resume_download(u64 id) =|
+ open_download(u64 id) =|
show_download_in_folder(u64 id) =|
prune_inactive_downloads(WebView::IsPrivate is_private) =|
pause_active_downloads() =|
diff --git a/Tests/LibWebView/TestApplicationDestruction.cpp b/Tests/LibWebView/TestApplicationDestruction.cpp
index 15854f69f65..8f5ad21205d 100644
--- a/Tests/LibWebView/TestApplicationDestruction.cpp
+++ b/Tests/LibWebView/TestApplicationDestruction.cpp
@@ -137,7 +137,7 @@ TEST_CASE(application_destruction_with_pending_web_ui_callbacks)
{
auto app = TRY_OR_FAIL(environment.create_application());
auto theme = TRY_OR_FAIL(load_test_theme());
- for (auto host : { "settings"sv, "crashes"sv, "downloads"sv }) {
+ for (auto host : { "settings"sv, "crashes"sv, "welcome"sv }) {
for (bool retained_by_callback : { false, true }) {
auto view = WebView::HeadlessWebView::create(theme, { 1000, 700 });
auto& page = view->page();
diff --git a/Tests/LibWebView/TestOmnibox.cpp b/Tests/LibWebView/TestOmnibox.cpp
index e99ea3b599e..9e8e01ce1c8 100644
--- a/Tests/LibWebView/TestOmnibox.cpp
+++ b/Tests/LibWebView/TestOmnibox.cpp
@@ -204,7 +204,6 @@ TEST_CASE(all_web_ui_pages_are_suggested)
"about:about"sv,
"about:blocking"sv,
"about:crashes"sv,
- "about:downloads"sv,
"about:newtab"sv,
"about:settings"sv,
"about:services"sv,
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 4b5b1c979ae..5c9e8758cc7 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -51,6 +51,7 @@
#include <UI/Qt/BookmarkDialog.h>
#include <UI/Qt/Bookmarks.h>
#include <UI/Qt/ChromeLayout.h>
+#include <UI/Qt/DownloadsPage.h>
#include <UI/Qt/EventLoopImplementationQt.h>
#include <UI/Qt/ExternalURLHandler.h>
#include <UI/Qt/HistoryPage.h>
@@ -1028,10 +1029,13 @@ void Application::create_platform_actions()
};
m_web_ui_pages = make<WebUIPages>();
m_bookmarks = make<Bookmarks>(*m_bookmark_store);
- m_web_ui_pages->register_page("history"_string, "History"_string, [](u64 web_ui_id) -> NonnullOwnPtr<WebUIPage> {
+ m_web_ui_pages->register_page("history"_string, "History"_string, [](u64 web_ui_id, WebView::IsPrivate) -> NonnullOwnPtr<WebUIPage> {
return make<HistoryPage>(web_ui_id);
});
- m_web_ui_pages->register_page("version"_string, "Version"_string, [](u64 web_ui_id) -> NonnullOwnPtr<WebUIPage> {
+ m_web_ui_pages->register_page("downloads"_string, "Downloads"_string, [](u64 web_ui_id, WebView::IsPrivate is_private) -> NonnullOwnPtr<WebUIPage> {
+ return make<DownloadsPage>(web_ui_id, is_private);
+ });
+ m_web_ui_pages->register_page("version"_string, "Version"_string, [](u64 web_ui_id, WebView::IsPrivate) -> NonnullOwnPtr<WebUIPage> {
return make<VersionPage>(web_ui_id);
});
diff --git a/UI/Qt/Bookmarks.cpp b/UI/Qt/Bookmarks.cpp
index d7cf529ae8f..f5a617e318d 100644
--- a/UI/Qt/Bookmarks.cpp
+++ b/UI/Qt/Bookmarks.cpp
@@ -32,7 +32,7 @@ Bookmarks::Bookmarks(BookmarkStore& store)
create_menus();
create_context_menus();
- Application::web_ui_pages().register_page("bookmarks"_string, "Bookmarks"_string, [](u64 web_ui_id) -> NonnullOwnPtr<WebUIPage> {
+ Application::web_ui_pages().register_page("bookmarks"_string, "Bookmarks"_string, [](u64 web_ui_id, WebView::IsPrivate) -> NonnullOwnPtr<WebUIPage> {
return make<BookmarksPage>(web_ui_id);
});
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 8e4de2d2208..489a0c8075a 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -23,6 +23,7 @@ add_library(ladybird_impl STATIC
Bookmarks.cpp
BookmarksBar.cpp
BookmarksPage.cpp
+ DownloadsPage.cpp
BrowserWindow.cpp
ChromeStyle.cpp
ContextMenus.cpp
diff --git a/Libraries/LibWebView/WebUI/DownloadsUI.cpp b/UI/Qt/DownloadsPage.cpp
similarity index 56%
rename from Libraries/LibWebView/WebUI/DownloadsUI.cpp
rename to UI/Qt/DownloadsPage.cpp
index c220b1bafc2..7f4f154a94f 100644
--- a/Libraries/LibWebView/WebUI/DownloadsUI.cpp
+++ b/UI/Qt/DownloadsPage.cpp
@@ -6,12 +6,11 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
-#include <LibWebView/Application.h>
-#include <LibWebView/WebContentClient.h>
-#include <LibWebView/WebContentPage.h>
-#include <LibWebView/WebUI/DownloadsUI.h>
+#include <UI/Qt/DownloadsPage.h>
-namespace WebView {
+namespace Ladybird {
+
+using WebView::FileDownloader;
static StringView status_to_string(FileDownloader::DownloadStatus status)
{
@@ -55,7 +54,7 @@ static JsonObject serialize_download(FileDownloader::Download const& download)
serialized.set("secondsRemaining"sv, time_remaining.has_value() ? JsonValue { time_remaining->to_seconds() } : JsonValue {});
JsonArray segments;
- for (auto const& segment : Application::the().file_downloader().segment_progress(download.id)) {
+ for (auto const& segment : RemoteDownloads::the().segment_progress(download.id)) {
JsonObject serialized_segment;
serialized_segment.set("start"sv, segment.start_offset);
serialized_segment.set("end"sv, segment.end_offset);
@@ -70,75 +69,66 @@ static JsonObject serialize_download(FileDownloader::Download const& download)
// The page lists and acts on the downloads of its own browsing session only, so those of a private session stay out of
// the pages of every other session.
-static bool is_download_of_session(FileDownloader::Download const& download, IsPrivate is_private)
+static bool is_download_of_session(FileDownloader::Download const& download, WebView::IsPrivate is_private)
{
return download.is_private == is_private;
}
-void DownloadsUI::register_interfaces()
+DownloadsPage::DownloadsPage(u64 web_ui_id, WebView::IsPrivate is_private)
+ : WebUIPage(web_ui_id)
+ , m_is_private(is_private)
+{
+}
+
+void DownloadsPage::received_message(String const& name, JsonValue const& data)
{
- register_interface("loadDownloads"sv, [this](auto const&) {
+ if (name == "loadDownloads"sv)
load_downloads();
- });
- register_interface("pruneInactiveDownloads"sv, [this](auto const&) {
- prune_inactive_downloads();
- });
- register_interface("cancelDownload"sv, [this](auto const& data) {
+ else if (name == "pruneInactiveDownloads"sv)
+ RemoteDownloads::the().prune_inactive_downloads(m_is_private);
+ else if (name == "cancelDownload"sv)
cancel_download(data);
- });
- register_interface("pauseDownload"sv, [this](auto const& data) {
+ else if (name == "pauseDownload"sv)
pause_download(data);
- });
- register_interface("resumeDownload"sv, [this](auto const& data) {
+ else if (name == "resumeDownload"sv)
resume_download(data);
- });
- register_interface("openDownload"sv, [this](auto const& data) {
+ else if (name == "openDownload"sv)
open_download(data);
- });
- register_interface("showDownloadInFolder"sv, [this](auto const& data) {
+ else if (name == "showDownloadInFolder"sv)
show_download_in_folder(data);
- });
}
-void DownloadsUI::download_added(FileDownloader::Download const& download)
+void DownloadsPage::download_added(FileDownloader::Download const& download)
{
- if (!has_client() || !is_download_of_session(download, client().is_private()))
- return;
- async_send_message("downloadAdded"sv, serialize_download(download));
+ if (is_download_of_session(download, m_is_private))
+ send_message("downloadAdded"_string, serialize_download(download));
}
-void DownloadsUI::download_updated(FileDownloader::Download const& download)
+void DownloadsPage::download_updated(FileDownloader::Download const& download)
{
- if (!has_client() || !is_download_of_session(download, client().is_private()))
- return;
- async_send_message("downloadUpdated"sv, serialize_download(download));
+ if (is_download_of_session(download, m_is_private))
+ send_message("downloadUpdated"_string, serialize_download(download));
}
-void DownloadsUI::download_removed(u64 id, IsPrivate is_private)
+void DownloadsPage::download_removed(u64 id, WebView::IsPrivate is_private)
{
- if (!has_client() || is_private != client().is_private())
- return;
- async_send_message("downloadRemoved"sv, JsonValue { id });
+ if (is_private == m_is_private)
+ send_message("downloadRemoved"_string, JsonValue { id });
}
-void DownloadsUI::load_downloads()
+void DownloadsPage::load_downloads()
{
- auto downloads = Application::the().file_downloader().downloads_of_session(client().is_private());
+ auto downloads = RemoteDownloads::the().downloads_of_session(m_is_private);
JsonArray serialized_downloads;
serialized_downloads.ensure_capacity(downloads.size());
for (auto const& download : downloads)
serialized_downloads.must_append(serialize_download(download));
- async_send_message("loadDownloads"sv, move(serialized_downloads));
+ send_message("loadDownloads"_string, move(serialized_downloads));
}
-void DownloadsUI::prune_inactive_downloads()
-{
- (void)Application::the().file_downloader().prune_inactive_downloads(client().is_private());
-}
-
-static Optional<FileDownloader::Download const&> download_from_message(JsonValue const& data, IsPrivate is_private)
+static Optional<FileDownloader::Download const&> download_from_message(JsonValue const& data, WebView::IsPrivate is_private)
{
if (!data.is_object())
return {};
@@ -147,81 +137,75 @@ static Optional<FileDownloader::Download const&> download_from_message(JsonValue
if (!id.has_value())
return {};
- auto download = Application::the().file_downloader().download(*id);
+ auto download = RemoteDownloads::the().download(*id);
if (!download.has_value() || !is_download_of_session(*download, is_private))
return {};
return download;
}
-void DownloadsUI::cancel_download(JsonValue const& data)
+void DownloadsPage::cancel_download(JsonValue const& data)
{
- auto download = download_from_message(data, client().is_private());
+ auto download = download_from_message(data, m_is_private);
if (!download.has_value())
return;
if (!FileDownloader::status_is_active(download->status))
return;
- Application::the().file_downloader().cancel_download(download->id);
+ RemoteDownloads::the().cancel_download(download->id);
}
-void DownloadsUI::pause_download(JsonValue const& data)
+void DownloadsPage::pause_download(JsonValue const& data)
{
- auto download = download_from_message(data, client().is_private());
+ auto download = download_from_message(data, m_is_private);
if (!download.has_value())
return;
if (download->status != FileDownloader::DownloadStatus::InProgress || !download->can_resume)
return;
- Application::the().file_downloader().pause_download(download->id);
+ RemoteDownloads::the().pause_download(download->id);
}
-void DownloadsUI::resume_download(JsonValue const& data)
+void DownloadsPage::resume_download(JsonValue const& data)
{
- auto download = download_from_message(data, client().is_private());
+ auto download = download_from_message(data, m_is_private);
if (!download.has_value())
return;
if (download->status != FileDownloader::DownloadStatus::Paused)
return;
- Application::the().file_downloader().resume_download(download->id);
+ RemoteDownloads::the().resume_download(download->id);
}
// Opening a download can run it, so only the user's own click in the page opens one or shows it in its folder.
-static bool consume_user_activation_of_page(WebContentClient& client, Web::PageId page_id)
-{
- auto* page = client.page(page_id);
- return page && page->consume_transient_user_activation();
-}
-
-void DownloadsUI::open_download(JsonValue const& data)
+void DownloadsPage::open_download(JsonValue const& data)
{
- auto download = download_from_message(data, client().is_private());
+ auto download = download_from_message(data, m_is_private);
if (!download.has_value())
return;
if (download->status != FileDownloader::DownloadStatus::Completed)
return;
- if (!consume_user_activation_of_page(client(), page_id()))
+ if (!consume_user_activation())
return;
- Application::the().open_download(*download);
+ RemoteDownloads::the().open(download->id);
}
-void DownloadsUI::show_download_in_folder(JsonValue const& data)
+void DownloadsPage::show_download_in_folder(JsonValue const& data)
{
- auto download = download_from_message(data, client().is_private());
+ auto download = download_from_message(data, m_is_private);
if (!download.has_value())
return;
if (download->status != FileDownloader::DownloadStatus::Completed)
return;
- if (!consume_user_activation_of_page(client(), page_id()))
+ if (!consume_user_activation())
return;
- Application::the().show_download_in_folder(*download);
+ RemoteDownloads::the().show_in_folder(download->id);
}
}
diff --git a/UI/Qt/DownloadsPage.h b/UI/Qt/DownloadsPage.h
new file mode 100644
index 00000000000..a747d21fabb
--- /dev/null
+++ b/UI/Qt/DownloadsPage.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <UI/Qt/RemoteDownloads.h>
+#include <UI/Qt/WebUIPage.h>
+
+namespace Ladybird {
+
+class DownloadsPage final
+ : public WebUIPage
+ , public RemoteDownloadsObserver {
+public:
+ DownloadsPage(u64 web_ui_id, WebView::IsPrivate);
+
+ virtual void received_message(String const& name, JsonValue const& data) override;
+
+private:
+ virtual void download_added(RemoteDownloads::Download const&) override;
+ virtual void download_updated(RemoteDownloads::Download const&) override;
+ virtual void download_removed(u64, WebView::IsPrivate) override;
+
+ void load_downloads();
+ void cancel_download(JsonValue const&);
+ void pause_download(JsonValue const&);
+ void resume_download(JsonValue const&);
+ void open_download(JsonValue const&);
+ void show_download_in_folder(JsonValue const&);
+
+ WebView::IsPrivate m_is_private { WebView::IsPrivate::No };
+};
+
+}
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index f170f6201cc..0c47a2c88b0 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -590,9 +590,9 @@ void HubConnection::app_open_new_window(Vector<URL::URL> urls)
application.on_new_window(urls);
}
-void HubConnection::web_ui_opened(u64 web_ui_id, String host)
+void HubConnection::web_ui_opened(u64 web_ui_id, String host, WebView::IsPrivate is_private)
{
- Application::web_ui_pages().page_opened(web_ui_id, host);
+ Application::web_ui_pages().page_opened(web_ui_id, host, is_private);
}
void HubConnection::web_ui_message(u64 web_ui_id, String name, JsonValue data)
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index bc112f22fd9..306a571d4b9 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -120,7 +120,7 @@ private:
virtual void app_read_clipboard_item(u64 request_id) override;
virtual void app_ask_user_for_download_path(u64 request_id, ByteString file) override;
virtual void settings_changed(ByteString settings_json, u64 last_applied_change_id) override;
- virtual void web_ui_opened(u64 web_ui_id, String host) override;
+ virtual void web_ui_opened(u64 web_ui_id, String host, WebView::IsPrivate is_private) override;
virtual void web_ui_message(u64 web_ui_id, String name, JsonValue data) override;
virtual void web_ui_closed(u64 web_ui_id) override;
virtual void view_request_context_menu(u64 view_id, WebView::ContextMenuRequest request) override;
diff --git a/UI/Qt/RemoteDownloads.cpp b/UI/Qt/RemoteDownloads.cpp
index 1006cf41756..6a2901d6f04 100644
--- a/UI/Qt/RemoteDownloads.cpp
+++ b/UI/Qt/RemoteDownloads.cpp
@@ -37,13 +37,17 @@ static RemoteDownloads::Download download_from(Hub::DownloadState const& state)
void RemoteDownloads::set_downloads(ReadonlySpan<Hub::DownloadState> downloads)
{
m_downloads.clear();
- for (auto const& download : downloads)
+ m_segments.clear();
+ for (auto const& download : downloads) {
m_downloads.append(download_from(download));
+ m_segments.set(download.id, download.segments);
+ }
}
void RemoteDownloads::did_add(Hub::DownloadState const& state)
{
m_downloads.append(download_from(state));
+ m_segments.set(state.id, state.segments);
for (auto& observer : m_observers)
observer.download_added(m_downloads.last());
}
@@ -54,6 +58,7 @@ void RemoteDownloads::did_update(Hub::DownloadState const& state)
if (download.id != state.id)
continue;
download = download_from(state);
+ m_segments.set(state.id, state.segments);
for (auto& observer : m_observers)
observer.download_updated(download);
return;
@@ -63,6 +68,7 @@ void RemoteDownloads::did_update(Hub::DownloadState const& state)
void RemoteDownloads::did_remove(u64 id, WebView::IsPrivate is_private)
{
m_downloads.remove_first_matching([&](auto const& download) { return download.id == id; });
+ m_segments.remove(id);
for (auto& observer : m_observers)
observer.download_removed(id, is_private);
}
@@ -111,6 +117,11 @@ void RemoteDownloads::resume_download(u64 id)
HubConnection::the().async_resume_download(id);
}
+void RemoteDownloads::open(u64 id)
+{
+ HubConnection::the().async_open_download(id);
+}
+
void RemoteDownloads::show_in_folder(u64 id)
{
HubConnection::the().async_show_download_in_folder(id);
diff --git a/UI/Qt/RemoteDownloads.h b/UI/Qt/RemoteDownloads.h
index 5b9b0f400b3..415ba19427f 100644
--- a/UI/Qt/RemoteDownloads.h
+++ b/UI/Qt/RemoteDownloads.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/HashMap.h>
#include <AK/Vector.h>
#include <Hub/HubClientEndpoint.h>
#include <LibWebView/FileDownloader.h>
@@ -28,12 +29,14 @@ public:
Vector<Download> downloads_of_session(WebView::IsPrivate) const;
Optional<Download const&> download(u64 id) const;
+ Vector<WebView::FileDownloader::SegmentProgress> segment_progress(u64 id) const { return m_segments.get(id).value_or({}); }
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 open(u64 id);
void show_in_folder(u64 id);
void prune_inactive_downloads(WebView::IsPrivate);
void pause_active_downloads();
@@ -43,6 +46,7 @@ private:
friend class RemoteDownloadsObserver;
Vector<Download> m_downloads;
+ HashMap<u64, Vector<WebView::FileDownloader::SegmentProgress>> m_segments;
Vector<RemoteDownloadsObserver&> m_observers;
};
diff --git a/UI/Qt/WebUIPage.cpp b/UI/Qt/WebUIPage.cpp
index 817e195533c..4c84d8b3fee 100644
--- a/UI/Qt/WebUIPage.cpp
+++ b/UI/Qt/WebUIPage.cpp
@@ -5,6 +5,7 @@
*/
#include <LibWebView/WebUI.h>
+#include <LibWebView/WebUI/ApplicationWebUI.h>
#include <UI/Qt/HubConnection.h>
#include <UI/Qt/WebUIPage.h>
@@ -16,6 +17,17 @@ void WebUIPage::send_message(String name, JsonValue data) const
hub->async_send_web_ui_message(m_web_ui_id, move(name), move(data));
}
+bool WebUIPage::consume_user_activation() const
+{
+#ifdef LADYBIRD_HUB_IN_OWN_PROCESS
+ auto* hub = HubConnection::the_if_connected();
+ return hub && hub->web_ui_consume_user_activation(m_web_ui_id);
+#else
+ auto* web_ui = WebView::ApplicationWebUI::find(m_web_ui_id);
+ return web_ui && web_ui->consume_user_activation();
+#endif
+}
+
void WebUIPages::register_page(String host, String title, Factory factory)
{
// NB: The omnibox of this process suggests the pages too.
@@ -25,10 +37,10 @@ void WebUIPages::register_page(String host, String title, Factory factory)
m_factories.set(move(host), move(factory));
}
-void WebUIPages::page_opened(u64 web_ui_id, String const& host)
+void WebUIPages::page_opened(u64 web_ui_id, String const& host, WebView::IsPrivate is_private)
{
if (auto factory = m_factories.get(host); factory.has_value())
- m_pages.set(web_ui_id, (*factory)(web_ui_id));
+ m_pages.set(web_ui_id, (*factory)(web_ui_id, is_private));
}
void WebUIPages::page_received_message(u64 web_ui_id, String const& name, JsonValue const& data)
diff --git a/UI/Qt/WebUIPage.h b/UI/Qt/WebUIPage.h
index 46775ecb8cb..7e608c9fcef 100644
--- a/UI/Qt/WebUIPage.h
+++ b/UI/Qt/WebUIPage.h
@@ -11,6 +11,7 @@
#include <AK/JsonValue.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/String.h>
+#include <LibWebView/Forward.h>
namespace Ladybird {
@@ -33,6 +34,9 @@ protected:
u64 web_ui_id() const { return m_web_ui_id; }
void send_message(String name, JsonValue data) const;
+ // Whether the user clicked in the page just now. This uses up the click, so only one action follows from it.
+ bool consume_user_activation() const;
+
private:
u64 m_web_ui_id { 0 };
};
@@ -42,12 +46,12 @@ class WebUIPages {
public:
AK_ALLOC_WITH_KMALLOC;
- using Factory = Function<NonnullOwnPtr<WebUIPage>(u64 web_ui_id)>;
+ using Factory = Function<NonnullOwnPtr<WebUIPage>(u64 web_ui_id, WebView::IsPrivate)>;
// Makes about:<host> a page of the UI, which opens with a page made by the factory.
void register_page(String host, String title, Factory);
- void page_opened(u64 web_ui_id, String const& host);
+ void page_opened(u64 web_ui_id, String const& host, WebView::IsPrivate);
void page_received_message(u64 web_ui_id, String const& name, JsonValue const& data);
void page_closed(u64 web_ui_id);