Truly independent web browser
Hub+UI/Qt: Offer a newer release in a card over the page
Jelle Raaijmakersc85f01518776
diff --git a/Services/Hub/Application.cpp b/Services/Hub/Application.cppindex 1ad1f480678..91ae263bb77 100644--- a/Services/Hub/Application.cpp+++ b/Services/Hub/Application.cpp@@ -13,6 +13,7 @@#include <LibCore/ArgsParser.h>#include <LibCore/EventLoop.h>#include <LibIPC/Decoder.h>+#include <LibWebView/UpdateCheck.h>#include <LibWebView/WebContentPage.h>namespace Hub {@@ -230,6 +231,21 @@ void Application::display_error_dialog(StringView error_message) constWebView::Application::display_error_dialog(error_message);}+void Application::display_update_check_result(WebView::UpdateCheckResult const& result, WebView::UpdateCheckTrigger trigger)+{+ auto* ui = ConnectionFromUI::the();+ if (!ui)+ return;++ Optional<String> version;+ auto install_method = WebView::InstallMethod::Source;+ if (result.update.has_value()) {+ version = result.update->version;+ install_method = result.update->install_method;+ }+ ui->async_app_display_update_check_result(result.kind, move(version), install_method, trigger);+}+void Application::set_clipboard_text(String text, ClipboardType type){if (auto* ui = ConnectionFromUI::the())diff --git a/Services/Hub/Application.h b/Services/Hub/Application.hindex 2004bb12084..188ccaa9606 100644--- a/Services/Hub/Application.h+++ b/Services/Hub/Application.h@@ -88,6 +88,7 @@ private:virtual NonnullRefPtr<Core::Promise<ByteString>> ask_user_for_download_path(ByteString const& file) const override;virtual void display_download_confirmation_dialog(StringView download_name, LexicalPath const& path) const override;virtual void display_error_dialog(StringView error_message) const override;+ virtual void display_update_check_result(WebView::UpdateCheckResult const&, WebView::UpdateCheckTrigger) override;virtual void set_clipboard_text(String, ClipboardType) override;virtual void insert_clipboard_item(Web::Clipboard::SystemClipboardItem) override;virtual void read_clipboard_text(ClipboardType, Function<void(Utf16String)>) const override;diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex bd24d6357bd..40717486984 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -22,6 +22,7 @@#include <LibWebView/ReportSubmission.h>#include <LibWebView/Settings.h>#include <LibWebView/TabPerformanceMonitor.h>+#include <LibWebView/UpdateCheck.h>#include <LibWebView/WebCompatReport.h>#include <LibWebView/WebUI/ApplicationWebUI.h>@@ -670,6 +671,18 @@ void ConnectionFromUI::stop_request(u64 request_id)(*request)->stop();}+void ConnectionFromUI::check_for_updates(WebView::UpdateCheckTrigger trigger)+{+ if (auto* update_checker = Application::the().update_checker())+ update_checker->check(trigger);+}++void ConnectionFromUI::skip_update_version(String version)+{+ if (auto* update_checker = Application::the().update_checker())+ update_checker->skip_version(version);+}+void ConnectionFromUI::toggle_devtools_enabled(){if (auto result = Application::the().toggle_devtools_enabled(); result.is_error())diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex 04ea3e809d7..8f1232ba600 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -68,6 +68,8 @@ private:void start_request(u64 request_id, ByteString const& method, URL::URL const&, HTTP::HeaderList const& headers, ByteString const& body, HTTP::CacheMode, HTTP::Cookie::IncludeCredentials, WebView::IsPrivate, ReadonlySpan<StringView> response_header_names);virtual void stop_request(u64 request_id) override;virtual void toggle_devtools_enabled() override;+ virtual void check_for_updates(WebView::UpdateCheckTrigger) override;+ virtual void skip_update_version(String) override;virtual void set_preferred_contrast(Web::CSS::PreferredContrast) override;virtual void set_preferred_motion(Web::CSS::PreferredMotion) override;virtual void set_user_agent_string(String user_agent) override;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex 0bb24133d9d..f8b51b78c89 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -29,6 +29,7 @@#include <LibWebView/MicrophoneAuthorizer.h>#include <LibWebView/SessionHistory.h>#include <LibWebView/TabPerformanceStats.h>+#include <LibWebView/UpdateCheck.h>#include <LibWebCommon/Clipboard/SystemClipboard.h>#include <LibWebCommon/HTML/PreparedNavigationDescriptor.h>#include <LibWebView/Application.h>@@ -295,6 +296,7 @@ endpoint HubClientapp_activate_or_open_tab_with_url(URL::URL url) =|app_open_view_in_new_tab(u64 view_id, Optional<u64> source_view_id, WebView::IsPrivate is_private, Web::HTML::ActivateTab activate_tab) =|app_display_error_dialog(String message) =|+ app_display_update_check_result(WebView::UpdateCheckResult::Kind kind, Optional<String> version, WebView::InstallMethod install_method, WebView::UpdateCheckTrigger trigger) =|app_display_download_confirmation_dialog(String download_name, ByteString path) =|app_read_clipboard_text(u64 request_id, WebView::Application::ClipboardType type) =|app_read_clipboard_item(u64 request_id) =|diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex 444f8ae1c5e..d866baf77df 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -27,6 +27,7 @@#include <LibWebView/ProcessSnapshot.h>#include <LibWebView/SessionHistory.h>#include <LibWebView/Translator.h>+#include <LibWebView/UpdateCheck.h>#include <LibWebView/ViewImplementation.h>[ExistingCpp]@@ -132,6 +133,8 @@ endpoint HubServerstop_request(u64 request_id) =|toggle_devtools_enabled() =|+ check_for_updates(WebView::UpdateCheckTrigger trigger) =|+ skip_update_version(String version) =|set_preferred_contrast(Web::CSS::PreferredContrast contrast) =|set_preferred_motion(Web::CSS::PreferredMotion motion) =|set_user_agent_string(String user_agent) =|diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 04fc5f30d82..1228e396e19 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -4,8 +4,10 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/GenericShorthands.h>#include <AK/Hex.h>#include <LibCore/ArgsParser.h>+#include <LibCore/BrowserVersion.h>#include <LibCore/Environment.h>#include <LibCore/EventLoop.h>#include <LibCore/GeolocationProvider.h>@@ -17,6 +19,7 @@#include <LibIPC/Encoder.h>#include <LibIPC/Transport.h>#include <LibURL/InternalURLs.h>+#include <LibURL/Parser.h>#include <LibURL/URL.h>#include <LibWebCommon/MimeSniff/MimeType.h>#include <LibWebCommon/WebView/Utilities.h>@@ -520,24 +523,20 @@ public:m_help_menu->addSeparator();m_help_menu->addAction(create_application_action(*m_application_widget, application.open_about_page_action(), IncludeActionIcon::No));-#if defined(AK_OS_MACOS)- if (auto* software_updater = Application::the().software_updater()) {- // NB: On macOS, this moves the item into the application menu, below About Ladybird.- auto* check_for_updates_action = make_action("Check for Updates…");- check_for_updates_action->setMenuRole(QAction::ApplicationSpecificRole);- QObject::connect(check_for_updates_action, &QAction::triggered, this, [software_updater]() {- software_updater->check_for_updates();- });- m_help_menu->addAction(check_for_updates_action);+ auto* check_for_updates_action = make_action("Check for Updates…");+ // NB: On macOS, this moves the item into the application menu, below About Ladybird.+ check_for_updates_action->setMenuRole(QAction::ApplicationSpecificRole);+ QObject::connect(check_for_updates_action, &QAction::triggered, this, [] {+ HubConnection::the().async_check_for_updates(WebView::UpdateCheckTrigger::UserInitiated);+ });+ m_help_menu->addAction(check_for_updates_action);- auto* show_update_dialog_action = make_action("Show Update Dialog for Latest Version");- QObject::connect(show_update_dialog_action, &QAction::triggered, this, [software_updater]() {- software_updater->show_update_dialog_for_latest_version();- });- m_debug_menu->addSeparator();- m_debug_menu->addAction(show_update_dialog_action);- }-#endif+ auto* simulate_update_action = make_action("Simulate Available Update");+ QObject::connect(simulate_update_action, &QAction::triggered, this, [] {+ HubConnection::the().async_check_for_updates(WebView::UpdateCheckTrigger::Debug);+ });+ m_debug_menu->addSeparator();+ m_debug_menu->addAction(simulate_update_action);}#if defined(AK_OS_MACOS)@@ -2309,6 +2308,70 @@ void Application::display_error_dialog(StringView error_message) constcreate_message_box(QMessageBox::Warning, error_message, active_window_if_any())->open();}+void Application::display_update_check_result(WebView::UpdateCheckResult::Kind kind, Optional<String> const& version, WebView::InstallMethod install_method, WebView::UpdateCheckTrigger trigger)+{+ static constexpr auto current_version = BROWSER_VERSION ""sv;++ switch (kind) {+ case WebView::UpdateCheckResult::Kind::UpToDate:+ create_message_box(QMessageBox::Information, MUST(String::formatted("Ladybird {} is the newest version.", current_version)), active_window_if_any())->open();+ return;+ case WebView::UpdateCheckResult::Kind::DevelopmentBuild:+ create_message_box(QMessageBox::Information, "Development builds of Ladybird do not check for updates."sv, active_window_if_any())->open();+ return;+ case WebView::UpdateCheckResult::Kind::Failed:+ display_error_dialog("Ladybird could not check for updates. Check your internet connection, and try again."sv);+ return;+ case WebView::UpdateCheckResult::Kind::UpdateAvailable:+ break;+ }++ if (!version.has_value())+ return;++ // A later automatic check finds the update again, but one that the user asked for answers now, in a new window if+ // no window is open. On macOS, the menu bar outlives the last window.+ auto* window = active_window_if_any();+ if (!window && trigger == WebView::UpdateCheckTrigger::Automatic)+ return;+ if (!window)+ window = &new_window({ Application::settings().new_tab_page_url() }, configuration_for_new_window());++ Vector<PageCard::Button> buttons;+ // A version skipped for the debug action would also be skipped when it is offered for real.+ buttons.append({ "Skip", PageCard::ButtonRole::Secondary, [version = *version, trigger] {+ if (trigger != WebView::UpdateCheckTrigger::Debug)+ HubConnection::the().async_skip_update_version(version);+ } });++ auto text = qformatted("You have Ladybird {}.", current_version);+ if (first_is_one_of(install_method, WebView::InstallMethod::DEB, WebView::InstallMethod::RPM, WebView::InstallMethod::Flatpak)) {+ text = qformatted("You have Ladybird {}. Update it with your system's software updater.", current_version);+ buttons.append({ "Remind me later", PageCard::ButtonRole::Primary, {} });+ } else if (install_method == WebView::InstallMethod::MacOSApp) {+ buttons.append({ "Remind me later", PageCard::ButtonRole::Secondary, {} });+#if defined(AK_OS_MACOS)+ buttons.append({ "Install update", PageCard::ButtonRole::Primary, [trigger] {+ auto* software_updater = Application::the().software_updater();+ if (!software_updater)+ return;+ if (trigger == WebView::UpdateCheckTrigger::Debug)+ software_updater->install_latest_version();+ else+ software_updater->install_update();+ } });+#endif+ } else {+ buttons.append({ "Remind me later", PageCard::ButtonRole::Secondary, {} });+ buttons.append({ "View release", PageCard::ButtonRole::Primary, [url = WebView::release_page_url(*version)] {+ if (auto release_page = URL::Parser::basic_parse(url); release_page.has_value())+ Application::the().open_url_in_new_tab(*release_page, Web::HTML::ActivateTab::Yes, {}, WebView::IsPrivate::No);+ } });+ }++ window->show_page_card(BrowserWindow::PageCardSlot::Update, qformatted("Ladybird {} is available", *version), text, move(buttons));+}+void Application::open_file(String const& path) const{QDesktopServices::openUrl(QUrl::fromLocalFile(qstring_from_ak_string(path)));diff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex cd68ccb31bd..a9190a6b848 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -19,6 +19,7 @@#include <LibWebView/BrowsingSession.h>#include <LibWebView/CrashReportOutbox.h>#include <LibWebView/ProcessSnapshot.h>+#include <LibWebView/UpdateCheck.h>#include <UI/Qt/ApplicationActions.h>#include <UI/Qt/BrowserWindow.h>#include <UI/Qt/HubConnection.h>@@ -258,6 +259,7 @@ private:void ask_user_for_download_path(u64 request_id, ByteString const& file) const;void display_download_confirmation_dialog(StringView download_name, ByteString const& path) const;void display_error_dialog(StringView error_message) const;+ void display_update_check_result(WebView::UpdateCheckResult::Kind, Optional<String> const& version, WebView::InstallMethod, WebView::UpdateCheckTrigger);void open_file(String const& path) const;virtual void crash_reports_changed() override;diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex 5ec872847c7..fb50cf59baa 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -188,6 +188,7 @@ public:enum class PageCardSlot : u8 {RestoreSession,+ Update,};void show_page_card(PageCardSlot, QString const& title, QString const& text, Vector<PageCard::Button>);void reposition_page_cards();@@ -339,7 +340,7 @@ private:TabWidget* m_tabs_container { nullptr };Tab* m_current_tab { nullptr };- Array<QPointer<PageCard>, 1> m_page_cards;+ Array<QPointer<PageCard>, 2> m_page_cards;QPointer<WebContentView> m_page_cards_view;Vector<QPointer<Tab>> m_background_tabs_from_current_tab;DevToolsBanner* m_devtools_banner { nullptr };diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cppindex abe8e8086dd..bbd79a8e285 100644--- a/UI/Qt/HubConnection.cpp+++ b/UI/Qt/HubConnection.cpp@@ -341,6 +341,11 @@ void HubConnection::app_display_error_dialog(String message)Application::the().display_error_dialog(message);}+void HubConnection::app_display_update_check_result(WebView::UpdateCheckResult::Kind kind, Optional<String> version, WebView::InstallMethod install_method, WebView::UpdateCheckTrigger trigger)+{+ Application::the().display_update_check_result(kind, version, install_method, trigger);+}+void HubConnection::app_display_download_confirmation_dialog(String download_name, ByteString path){Application::the().display_download_confirmation_dialog(download_name, path);diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.hindex d58bf5f27de..353e445f565 100644--- a/UI/Qt/HubConnection.h+++ b/UI/Qt/HubConnection.h@@ -139,6 +139,7 @@ private:virtual void app_activate_or_open_tab_with_url(URL::URL url) override;virtual void app_open_view_in_new_tab(u64 view_id, Optional<u64> source_view_id, WebView::IsPrivate is_private, Web::HTML::ActivateTab activate_tab) override;virtual void app_display_error_dialog(String message) override;+ virtual void app_display_update_check_result(WebView::UpdateCheckResult::Kind, Optional<String> version, WebView::InstallMethod, WebView::UpdateCheckTrigger) override;virtual void app_display_download_confirmation_dialog(String download_name, ByteString path) override;virtual void app_read_clipboard_text(u64 request_id, WebView::Application::ClipboardType type) override;virtual void app_read_clipboard_item(u64 request_id) override;diff --git a/UI/Qt/SoftwareUpdaterMac.h b/UI/Qt/SoftwareUpdaterMac.hindex a3d6056e15b..b255e54b7cb 100644--- a/UI/Qt/SoftwareUpdaterMac.h+++ b/UI/Qt/SoftwareUpdaterMac.h@@ -27,8 +27,8 @@ public:static NonnullOwnPtr<SoftwareUpdater> create();~SoftwareUpdater();- void check_for_updates();- void show_update_dialog_for_latest_version();+ void install_update();+ void install_latest_version();private:explicit SoftwareUpdater(NonnullOwnPtr<Detail::SparkleUpdater>);diff --git a/UI/Qt/SoftwareUpdaterMac.mm b/UI/Qt/SoftwareUpdaterMac.mmindex de5d0a93e3a..c15f0f06112 100644--- a/UI/Qt/SoftwareUpdaterMac.mm+++ b/UI/Qt/SoftwareUpdaterMac.mm@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Format.h>#include <UI/Qt/SoftwareUpdaterMac.h>#import <Sparkle/Sparkle.h>@@ -54,6 +55,119 @@@end+// Sparkle only runs once the user chose to install an update in Ladybird's own notice, so it installs the update and+// relaunches without asking again. Its progress and its alerts show in Sparkle's own windows. The window that says+// Sparkle is checking for updates is left out: Sparkle's update window would replace it, but that window is skipped.+@interface LadybirdUserDriver : NSObject <SPUUserDriver>++- (instancetype)initWithStandardUserDriver:(SPUStandardUserDriver*)standardUserDriver;++@end++@implementation LadybirdUserDriver+{+ SPUStandardUserDriver* _standard;+}++- (instancetype)initWithStandardUserDriver:(SPUStandardUserDriver*)standardUserDriver+{+ if (self = [super init])+ _standard = standardUserDriver;+ return self;+}++- (void)showUpdatePermissionRequest:(SPUUpdatePermissionRequest*)request reply:(void (^)(SUUpdatePermissionResponse*))reply+{+ [_standard showUpdatePermissionRequest:request reply:reply];+}++- (void)showUserInitiatedUpdateCheckWithCancellation:(void (^)(void))cancellation+{+}++- (void)showUpdateFoundWithAppcastItem:(SUAppcastItem*)appcastItem state:(SPUUserUpdateState*)state reply:(void (^)(SPUUserUpdateChoice))reply+{+ // Sparkle cannot install an update that only informs, so the user sees what it says instead. A check that the user did+ // not start, such as one that a user default scheduled, asks the user as Sparkle normally does.+ if (appcastItem.informationOnlyUpdate || !state.userInitiated) {+ [_standard showUpdateFoundWithAppcastItem:appcastItem state:state reply:reply];+ return;+ }+ reply(SPUUserUpdateChoiceInstall);+}++- (void)showUpdateReleaseNotesWithDownloadData:(SPUDownloadData*)downloadData+{+ [_standard showUpdateReleaseNotesWithDownloadData:downloadData];+}++- (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError*)error+{+ [_standard showUpdateReleaseNotesFailedToDownloadWithError:error];+}++- (void)showUpdateNotFoundWithError:(NSError*)error acknowledgement:(void (^)(void))acknowledgement+{+ [_standard showUpdateNotFoundWithError:error acknowledgement:acknowledgement];+}++- (void)showUpdaterError:(NSError*)error acknowledgement:(void (^)(void))acknowledgement+{+ [_standard showUpdaterError:error acknowledgement:acknowledgement];+}++- (void)showDownloadInitiatedWithCancellation:(void (^)(void))cancellation+{+ [_standard showDownloadInitiatedWithCancellation:cancellation];+}++- (void)showDownloadDidReceiveExpectedContentLength:(uint64_t)expectedContentLength+{+ [_standard showDownloadDidReceiveExpectedContentLength:expectedContentLength];+}++- (void)showDownloadDidReceiveDataOfLength:(uint64_t)length+{+ [_standard showDownloadDidReceiveDataOfLength:length];+}++- (void)showDownloadDidStartExtractingUpdate+{+ [_standard showDownloadDidStartExtractingUpdate];+}++- (void)showExtractionReceivedProgress:(double)progress+{+ [_standard showExtractionReceivedProgress:progress];+}++- (void)showReadyToInstallAndRelaunch:(void (^)(SPUUserUpdateChoice))reply+{+ reply(SPUUserUpdateChoiceInstall);+}++- (void)showInstallingUpdateWithApplicationTerminated:(BOOL)applicationTerminated retryTerminatingApplication:(void (^)(void))retryTerminatingApplication+{+ [_standard showInstallingUpdateWithApplicationTerminated:applicationTerminated retryTerminatingApplication:retryTerminatingApplication];+}++- (void)showUpdateInstalledAndRelaunched:(BOOL)relaunched acknowledgement:(void (^)(void))acknowledgement+{+ [_standard showUpdateInstalledAndRelaunched:relaunched acknowledgement:acknowledgement];+}++- (void)showUpdateInFocus+{+ [_standard showUpdateInFocus];+}++- (void)dismissUpdateInstallation+{+ [_standard dismissUpdateInstallation];+}++@end+namespace Ladybird {namespace Detail {@@ -61,9 +175,10 @@ namespace Detail {struct SparkleUpdater {AK_ALLOC_WITH_KMALLOC;- // NB: Sparkle holds its delegate weakly.+ // Sparkle holds its delegates weakly.LadybirdUpdaterDelegate* delegate { nil };- SPUStandardUpdaterController* controller { nil };+ LadybirdUserDriver* user_driver { nil };+ SPUUpdater* updater { nil };};}@@ -74,7 +189,13 @@ NonnullOwnPtr<SoftwareUpdater> SoftwareUpdater::create(){auto sparkle = make<Detail::SparkleUpdater>();sparkle->delegate = [[LadybirdUpdaterDelegate alloc] init];- sparkle->controller = [[SPUStandardUpdaterController alloc] initWithStartingUpdater:YES updaterDelegate:sparkle->delegate userDriverDelegate:sparkle->delegate];+ auto* standard_user_driver = [[SPUStandardUserDriver alloc] initWithHostBundle:NSBundle.mainBundle delegate:sparkle->delegate];+ sparkle->user_driver = [[LadybirdUserDriver alloc] initWithStandardUserDriver:standard_user_driver];+ sparkle->updater = [[SPUUpdater alloc] initWithHostBundle:NSBundle.mainBundle applicationBundle:NSBundle.mainBundle userDriver:sparkle->user_driver delegate:sparkle->delegate];++ NSError* error = nil;+ if (![sparkle->updater startUpdater:&error])+ warnln("Unable to start Sparkle: {}", error.localizedDescription.UTF8String);return adopt_own(*new SoftwareUpdater(move(sparkle)));}@@ -85,15 +206,15 @@ SoftwareUpdater::SoftwareUpdater(NonnullOwnPtr<Detail::SparkleUpdater> sparkle)SoftwareUpdater::~SoftwareUpdater() = default;-void SoftwareUpdater::check_for_updates()+void SoftwareUpdater::install_update(){- [m_sparkle->controller checkForUpdates:nil];+ [m_sparkle->updater checkForUpdates];}-void SoftwareUpdater::show_update_dialog_for_latest_version()+void SoftwareUpdater::install_latest_version(){m_sparkle->delegate.offersLatestVersion = YES;- [m_sparkle->controller checkForUpdates:nil];+ [m_sparkle->updater checkForUpdates];}}