Truly independent web browser
LibWeb: Finish closing a Notification whose display then fails
Shannon Bootha497d385028a
diff --git a/Libraries/LibWeb/Bindings/Rust/src/notifications_api/notification.rs b/Libraries/LibWeb/Bindings/Rust/src/notifications_api/notification.rsindex 4220325fca7..fb713e83190 100644--- a/Libraries/LibWeb/Bindings/Rust/src/notifications_api/notification.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/notifications_api/notification.rs@@ -696,6 +696,11 @@ impl Notification {}fn did_fail_to_show(this: Ref<Self>, vm: &Vm) {+ // NB: The UI process forgets a notification it failed to show, so the close it was carrying out ends here.+ if this.state.get() == State::Closing {+ Self::did_close(this, vm);+ return;+ }if this.state.get() != State::Showing {return;}diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 79dbce56096..82776ec1b7f 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -2644,6 +2644,16 @@ void Internals::dismiss_notification_for_testing(NotificationsAPI::Notification&page().client().page_did_request_dismiss_notification_for_testing(notification.id_for_testing());}+void Internals::hold_next_notification_show_for_testing()+{+ page().client().page_did_request_hold_next_notification_show_for_testing();+}++void Internals::fail_held_notification_show_for_testing()+{+ page().client().page_did_request_fail_held_notification_show_for_testing();+}+bool Internals::html_parser_body_is_exhausted(DOM::Document& document){auto parser = document.parser();diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex 99ef59d757e..e6591dbc35d 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -297,6 +297,8 @@ public:void click_notification_for_testing(NotificationsAPI::Notification&);void click_notification_by_id_for_testing(WebIDL::UnsignedLongLong browser_notification_id);void dismiss_notification_for_testing(NotificationsAPI::Notification&);+ void hold_next_notification_show_for_testing();+ void fail_held_notification_show_for_testing();void set_manual_rendering_opportunities(bool enabled);void inject_rendering_opportunity(double frame_time_ms);diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex 3451fc90181..019413450c8 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -336,6 +336,10 @@ interface Internals {undefined clickNotificationForTesting(Notification notification);undefined clickNotificationByIdForTesting(unsigned long long browserNotificationId);undefined dismissNotificationForTesting(Notification notification);+ // Makes the platform hold the next notification the page shows, neither showing nor closing it, until it's told to+ // refuse it.+ undefined holdNextNotificationShowForTesting();+ undefined failHeldNotificationShowForTesting();InternalGamepad connectVirtualGamepad();diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 40901a722fb..53018f3369e 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -980,6 +980,8 @@ public:virtual void page_did_request_click_notification_for_testing([[maybe_unused]] u64 notification_id) { }virtual void page_did_request_click_notification_by_id_for_testing([[maybe_unused]] u64 browser_notification_id) { }virtual void page_did_request_dismiss_notification_for_testing([[maybe_unused]] u64 notification_id) { }+ virtual void page_did_request_hold_next_notification_show_for_testing() { }+ virtual void page_did_request_fail_held_notification_show_for_testing() { }virtual void page_did_finish_test([[maybe_unused]] Utf16String const& text) { }virtual void page_did_set_test_timeout([[maybe_unused]] double milliseconds) { }diff --git a/Libraries/LibWebView/RecordingNotificationBackend.cpp b/Libraries/LibWebView/RecordingNotificationBackend.cppindex f9feacb3ff4..3bc7402cedc 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.cpp+++ b/Libraries/LibWebView/RecordingNotificationBackend.cpp@@ -12,9 +12,8 @@ namespace WebView {void RecordingNotificationBackend::show(PlatformNotification const& notification){- if (exchange(fail_next_show, false)) {- if (on_failed)- on_failed(notification.id);+ if (m_pages_holding_next_show.remove_first_matching([&](auto page_id) { return page_id == notification.page_id; })) {+ m_held.set(notification.id, notification.page_id);return;}@@ -37,6 +36,9 @@ void RecordingNotificationBackend::show(PlatformNotification const& notificationvoid RecordingNotificationBackend::close(u64 id){+ // A held notification isn't shown, so there's nothing to close yet.+ if (m_held.contains(id))+ return;if (auto index = index_of(id); index.has_value())m_shown.remove(*index);m_closed.append({ id, m_page_of.get(id).value_or({}) });@@ -69,6 +71,20 @@ void RecordingNotificationBackend::dismiss(u64 id)on_closed(id, ByUser::Yes);}+void RecordingNotificationBackend::fail_held_shows_of(Web::PageId page_id)+{+ Vector<u64> ids;+ for (auto const& [id, held_page_id] : m_held) {+ if (held_page_id == page_id)+ ids.append(id);+ }+ for (auto id : ids) {+ m_held.remove(id);+ if (on_failed)+ on_failed(id);+ }+}+void RecordingNotificationBackend::record_focus(Web::PageId page_id){m_focused.append(page_id);@@ -85,6 +101,8 @@ void RecordingNotificationBackend::forget_page(Web::PageId page_id)m_closed.remove_all_matching([&](auto const& entry) { return entry.page_id == page_id; });m_focused.remove_all_matching([&](auto const& focused) { return focused == page_id; });m_opened.remove_all_matching([&](auto const& entry) { return entry.page_id == page_id; });+ m_pages_holding_next_show.remove_all_matching([&](auto holding_page_id) { return holding_page_id == page_id; });+ m_held.remove_all_matching([&](auto, auto held_page_id) { return held_page_id == page_id; });}String RecordingNotificationBackend::state_json(Web::PageId page_id, Vector<u64> const& abandoned) constdiff --git a/Libraries/LibWebView/RecordingNotificationBackend.h b/Libraries/LibWebView/RecordingNotificationBackend.hindex 39cb85b7c10..3d7218def9d 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.h+++ b/Libraries/LibWebView/RecordingNotificationBackend.h@@ -38,8 +38,10 @@ public:// Forgets a page's history, since test-web hands the page id to the next test it runs in the same view.void forget_page(Web::PageId);- // When set, the next show() fails instead, as a platform that refuses the notification would.- bool fail_next_show { false };+ // Holds the page's next show() unanswered, as a platform that is still deciding would, until the page's held shows+ // fail as a platform that refuses the notification would.+ void hold_next_show_of(Web::PageId page_id) { m_pages_holding_next_show.append(page_id); }+ void fail_held_shows_of(Web::PageId);private:Optional<size_t> index_of(u64 id) const;@@ -58,6 +60,8 @@ private:Vector<Web::PageId> m_focused;Vector<Opened> m_opened;HashMap<u64, Web::PageId> m_page_of;+ Vector<Web::PageId> m_pages_holding_next_show;+ HashMap<u64, Web::PageId> m_held;};}diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 9f59b094728..f663953981c 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -2082,6 +2082,18 @@ void WebContentPage::did_request_dismiss_notification_for_testing(u64 notificatirecording_backend->dismiss(*id);}+void WebContentPage::did_request_hold_next_notification_show_for_testing()+{+ if (auto* recording_backend = NotificationManager::the().backend().as_recording_backend())+ recording_backend->hold_next_show_of(id());+}++void WebContentPage::did_request_fail_held_notification_show_for_testing()+{+ if (auto* recording_backend = NotificationManager::the().backend().as_recording_backend())+ recording_backend->fail_held_shows_of(id());+}+void WebContentPage::did_cancel_geolocation_position_request(u64 request_id){if (view().on_cancel_geolocation_position_request)diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 8fffe24f172..2c69ae41d0e 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -297,6 +297,8 @@ private:virtual void did_request_click_notification_for_testing(u64 notification_id) override;virtual void did_request_click_notification_by_id_for_testing(u64 browser_notification_id) override;virtual void did_request_dismiss_notification_for_testing(u64 notification_id) override;+ virtual void did_request_hold_next_notification_show_for_testing() override;+ virtual void did_request_fail_held_notification_show_for_testing() override;virtual void did_request_file_picker(Web::HTML::FileFilter accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files) override;virtual void did_finish_handling_input_event(u64 event_id, Web::EventResult event_result) override;virtual void did_update_input_method_state(Optional<Web::DevicePixelRect> caret_rect, bool is_enabled, i32 cursor_position, i32 anchor_position, Utf16String text_before_cursor, Utf16String text_after_cursor) override;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex 641471657be..bb0cd0c5305 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -1402,6 +1402,16 @@ void PageClient::page_did_request_dismiss_notification_for_testing(u64 notificatclient().async_did_request_dismiss_notification_for_testing(m_id, notification_id);}+void PageClient::page_did_request_hold_next_notification_show_for_testing()+{+ client().async_did_request_hold_next_notification_show_for_testing(m_id);+}++void PageClient::page_did_request_fail_held_notification_show_for_testing()+{+ client().async_did_request_fail_held_notification_show_for_testing(m_id);+}+Optional<Web::Permissions::PermissionSnapshot> PageClient::page_did_request_permission_snapshot(URL::Origin const& top_level_origin){auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::DidRequestPermissionSnapshot>(m_id, top_level_origin);diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex 4cd9e5fb4f5..eb1b7f2c45b 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -390,6 +390,8 @@ private:virtual void page_did_request_click_notification_for_testing(u64 notification_id) override;virtual void page_did_request_click_notification_by_id_for_testing(u64 browser_notification_id) override;virtual void page_did_request_dismiss_notification_for_testing(u64 notification_id) override;+ virtual void page_did_request_hold_next_notification_show_for_testing() override;+ virtual void page_did_request_fail_held_notification_show_for_testing() override;virtual void page_did_finish_test(Utf16String const& text) override;virtual void page_did_set_test_timeout(double milliseconds) override;virtual void page_did_receive_reference_test_metadata(JsonValue) override;diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipcindex f15258d9c24..7244b158f24 100644--- a/Services/WebContent/WebContentClient.ipc+++ b/Services/WebContent/WebContentClient.ipc@@ -254,6 +254,8 @@ endpoint WebContentClient routed by Web::PageId page_iddid_request_click_notification_for_testing(Web::PageId page_id, u64 notification_id) =|did_request_click_notification_by_id_for_testing(Web::PageId page_id, u64 browser_notification_id) =|did_request_dismiss_notification_for_testing(Web::PageId page_id, u64 notification_id) =|+ did_request_hold_next_notification_show_for_testing(Web::PageId page_id) =|+ did_request_fail_held_notification_show_for_testing(Web::PageId page_id) =|did_request_file_picker(Web::PageId page_id, Web::HTML::FileFilter accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files) =|did_request_select_dropdown(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items) =|did_finish_handling_input_event(Web::PageId page_id, u64 event_id, Web::EventResult event_result) =|diff --git a/Tests/LibWeb/Text/expected/notifications/close-before-display-fails.txt b/Tests/LibWeb/Text/expected/notifications/close-before-display-fails.txtnew file mode 100644index 00000000000..2b67dd4898a--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/close-before-display-fails.txt@@ -0,0 +1 @@+events: [close]diff --git a/Tests/LibWeb/Text/input/notifications/close-before-display-fails.html b/Tests/LibWeb/Text/input/notifications/close-before-display-fails.htmlnew file mode 100644index 00000000000..8c27c41f250--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/close-before-display-fails.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // close() runs while the platform is still displaying the notification, and the display then fails.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ internals.holdNextNotificationShowForTesting();+ const notification = new Notification("title");+ const events = [];+ notification.onshow = () => events.push("show");+ notification.onerror = () => events.push("error");+ notification.onclose = () => {+ events.push("close");+ println(`events: [${events.join(", ")}]`);+ done();+ };+ await new Promise(resolve => setTimeout(resolve, 0));+ notification.close();+ internals.failHeldNotificationShowForTesting();+ });+</script>