Truly independent web browser
test-web+LibWeb: Drive and observe shown notifications from tests
sideshowbarker95d65cda4260
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 107757f1cd6..88cbe29e6b8 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -6564,6 +6564,8 @@ void Document::destroy()// 2. Abort document.abort();++ m_page->notifications_document_destroyed(*this);stop_compositor_animation_timers();// AD-HOC: Notify document observers that this document became inactive.diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex cec5101a999..7dcb3492d1b 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -103,6 +103,7 @@#include <LibWeb/Loader/ContentBlocker.h>#include <LibWeb/Loader/LoaderConfig.h>#include <LibWeb/Loader/ResourceLoader.h>+#include <LibWeb/NotificationsAPI/Notification.h>#include <LibWeb/Page/EventHandler.h>#include <LibWeb/Page/Page.h>#include <LibWeb/Painting/BoxViews.h>@@ -2264,6 +2265,26 @@ Vector<String> Internals::permission_cache_origins()return PermissionsAPI::PermissionStore::the().cached_origins_for_testing();}+String Internals::notifications_state_for_testing()+{+ return page().client().page_did_request_notifications_state_for_testing();+}++void Internals::click_notification_for_testing(NotificationsAPI::Notification& notification)+{+ page().client().page_did_request_click_notification_for_testing(notification.id_for_testing());+}++void Internals::click_notification_by_id_for_testing(WebIDL::UnsignedLongLong browser_notification_id)+{+ page().client().page_did_request_click_notification_by_id_for_testing(browser_notification_id);+}++void Internals::dismiss_notification_for_testing(NotificationsAPI::Notification& notification)+{+ page().client().page_did_request_dismiss_notification_for_testing(notification.id_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 6ac94a65212..bb9915d521b 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -272,6 +272,10 @@ public:void release_held_permission_prompt(WebIDL::UnsignedLong waiters);GC::Ref<WebIDL::Promise> advance_permission_clock(WebIDL::UnsignedLongLong milliseconds);Vector<String> permission_cache_origins();+ String notifications_state_for_testing();+ 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&);u64 parser_non_append_insertions();DOM::Document::StyleInvalidationCounters const& style_invalidation_counters() const;diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex cefeedcb279..463a57d9ff8 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -273,6 +273,16 @@ interface Internals {Promise<undefined> advancePermissionClock(unsigned long long milliseconds);sequence<DOMString> permissionCacheOrigins();+ // What the UI process's recording notification backend has seen, as JSON: the notifications shown (with the+ // browser's id of each), the ids closed, the pages focused by a click, and the URLs opened by a click on a+ // notification whose page was gone. "{}" outside test-web.+ DOMString notificationsStateForTesting();+ // Acts on a shown notification as the user would: by its Notification object, or by the browser's id, for a+ // notification whose page is gone.+ undefined clickNotificationForTesting(Notification notification);+ undefined clickNotificationByIdForTesting(unsigned long long browserNotificationId);+ undefined dismissNotificationForTesting(Notification notification);+InternalGamepad connectVirtualGamepad();undefined setHighlightedNode(Node? node);diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.cpp b/Libraries/LibWeb/NotificationsAPI/Notification.cppindex 39302f24af2..26eff486123 100644--- a/Libraries/LibWeb/NotificationsAPI/Notification.cpp+++ b/Libraries/LibWeb/NotificationsAPI/Notification.cpp@@ -9,6 +9,7 @@#include <LibGC/Heap.h>#include <LibJS/Runtime/Object.h>#include <LibJS/Runtime/VM.h>+#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/Event.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>#include <LibWeb/HTML/EventNames.h>@@ -330,6 +331,22 @@ void Notification::show_steps()// and a failure fires error instead.}+DOM::Document const* Notification::document() const+{+ if (auto const* window = HTML::window_from_global_object(*m_relevant_global))+ return &window->associated_document();+ return nullptr;+}++void Notification::abandon()+{+ if (m_state != State::Showing && m_state != State::Closing)+ return;+ m_state = State::Closed;+ m_page->unregister_notification(m_id);+ m_page->client().page_did_abandon_notification(m_id);+}+void Notification::did_show(){if (m_state != State::Showing)diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.h b/Libraries/LibWeb/NotificationsAPI/Notification.hindex b6f9db87060..c4614830142 100644--- a/Libraries/LibWeb/NotificationsAPI/Notification.h+++ b/Libraries/LibWeb/NotificationsAPI/Notification.h@@ -126,6 +126,14 @@ public:void set_onclose(WebIDL::CallbackType*);WebIDL::CallbackType* onclose();+ // The id this notification is known by across the Page bridge, for tests that act on it from the UI side.+ u64 id_for_testing() const { return m_id; }++ // The document the notification was created in, if a window's.+ DOM::Document const* document() const;+ // The document is gone, so no event can reach this object again; the UI process keeps showing the notification.+ void abandon();+// What the UI process reports back about the notification it shows for this object.void did_show();void did_fail_to_show();diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cppindex 1c216b828c7..fbef33d4b94 100644--- a/Libraries/LibWeb/Page/Page.cpp+++ b/Libraries/LibWeb/Page/Page.cpp@@ -1639,6 +1639,17 @@ void Page::notification_closed(u64 notification_id, bool by_user)(*notification)->did_close(by_user);}+void Page::notifications_document_destroyed(DOM::Document const& document)+{+ GC::RootVector<GC::Ref<NotificationsAPI::Notification>> abandoned;+ for (auto const& [id, notification] : m_notifications_list) {+ if (notification->document() == &document)+ abandoned.append(notification);+ }+ for (auto const& notification : abandoned)+ notification->abandon();+}+u64 Page::request_geolocation_position(HTML::CrossProcessId requesting_document_id, GeolocationPositionCallback callback, GeolocationRequestType type){// This is the browser-process bridge for the Geolocation spec's "try to acquire position data from the underlying system" step.diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 9d77dd3d14f..6afccb8ae38 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -368,6 +368,9 @@ public:void notification_show_failed(u64 notification_id);void notification_clicked(u64 notification_id);void notification_closed(u64 notification_id, bool by_user);+ // The document whose script created notifications is gone: Their objects can't get events any more, and the UI+ // process hears that, so a later click opens the site instead.+ void notifications_document_destroyed(DOM::Document const&);// https://w3c.github.io/geolocation/#dfn-emulated-position-data// NB: The top-level traversable's, which the UI process tells every page representing it.@@ -909,7 +912,12 @@ public:virtual Optional<Web::Permissions::PermissionSnapshot> page_did_advance_permission_clock_for_testing([[maybe_unused]] u64 milliseconds) { return {}; }virtual void page_did_request_show_notification([[maybe_unused]] u64 notification_id, [[maybe_unused]] NotificationsAPI::NotificationData const&, [[maybe_unused]] RefPtr<Gfx::Bitmap const> icon) { }virtual void page_did_request_close_notification([[maybe_unused]] u64 notification_id) { }+ virtual void page_did_abandon_notification([[maybe_unused]] u64 notification_id) { }virtual void page_did_handle_notification_click([[maybe_unused]] u64 notification_id, [[maybe_unused]] bool into_focus) { }+ virtual String page_did_request_notifications_state_for_testing() { return "{}"_string; }+ 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_finish_test([[maybe_unused]] Utf16String const& text) { }virtual void page_did_set_test_timeout([[maybe_unused]] double milliseconds) { }diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex 2fbc60a1fcb..9d9cfb056e1 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -162,7 +162,7 @@ public:virtual void focus_page_from_notification(WebContentPage&) { }// A click on a notification whose page is gone: Open the URL in a new foreground tab of that window, if it's// still open, or of the active window.- virtual void open_url_in_new_tab_from_notification(URL::URL const&, CanonicalTraversable*) { }+ virtual void open_url_in_new_tab_from_notification(URL::URL const&, [[maybe_unused]] Web::PageId originating_page, CanonicalTraversable*) { }void appearance_changed(Badge<ApplicationSettingsObserver>);void tab_settings_changed(Badge<ApplicationSettingsObserver>);diff --git a/Libraries/LibWebView/Forward.h b/Libraries/LibWebView/Forward.hindex ded70f907d4..af52319d335 100644--- a/Libraries/LibWebView/Forward.h+++ b/Libraries/LibWebView/Forward.h@@ -50,6 +50,7 @@ class OutOfProcessWebView;class NotificationBackend;class NotificationManager;class PictureInPictureManager;+class RecordingNotificationBackend;class PictureInPictureWindow;class ProcessManager;class RequestServerInstance;diff --git a/Libraries/LibWebView/NotificationBackend.h b/Libraries/LibWebView/NotificationBackend.hindex e076d0cfe41..679699450bc 100644--- a/Libraries/LibWebView/NotificationBackend.h+++ b/Libraries/LibWebView/NotificationBackend.h@@ -14,7 +14,9 @@#include <AK/kmalloc.h>#include <LibGfx/Bitmap.h>#include <LibWebCommon/NotificationsAPI/NotificationData.h>+#include <LibWebCommon/Page/PageId.h>#include <LibWebView/Export.h>+#include <LibWebView/Forward.h>namespace WebView {@@ -22,6 +24,8 @@ namespace WebView {struct PlatformNotification {// Names the notification for the life of the process; the backend reports every event by it.u64 id { 0 };+ // The page that asked for it, for a backend that keeps per-page state.+ Web::PageId page_id;// The notification with the same origin and tag that this one takes the place of, if any.Optional<u64> replaces;Web::NotificationsAPI::NotificationData data;@@ -32,7 +36,8 @@ struct PlatformNotification {// What a notification platform does for the browser. show() ends in on_shown() or on_failed() for its id, possibly// before it returns; close() ends in on_closed() with ByUser::No. The platform reports the user's clicks and-// dismissals through on_clicked() and on_closed(). Every callback runs on the main thread.+// dismissals through on_clicked() and on_closed(); a click takes the notification down too, so it's followed by+// on_closed() with ByUser::Yes. Every callback runs on the main thread.class WEBVIEW_API NotificationBackend {public:AK_ALLOC_WITH_KMALLOC;@@ -47,6 +52,9 @@ public:virtual void show(PlatformNotification const&) = 0;virtual void close(u64 id) = 0;+ // Non-null for the backend tests install, which keeps what it was shown.+ virtual RecordingNotificationBackend* as_recording_backend() { return nullptr; }+Function<void(u64 id)> on_shown;Function<void(u64 id)> on_failed;Function<void(u64 id)> on_clicked;diff --git a/Libraries/LibWebView/NotificationManager.cpp b/Libraries/LibWebView/NotificationManager.cppindex 81ad1735208..b2e6acc3a29 100644--- a/Libraries/LibWebView/NotificationManager.cpp+++ b/Libraries/LibWebView/NotificationManager.cpp@@ -43,13 +43,16 @@ void NotificationManager::install_backend_callbacks()void NotificationManager::show(WebContentPage& page, u64 page_notification_id, Web::NotificationsAPI::NotificationData data, RefPtr<Gfx::Bitmap const> icon){- // The notification belongs to the origin the browser knows the page by, never to one the page reports.+ // The notification belongs to the origin the browser knows the page's top-level document by, never to one the+ // page reports. (The document's origin, not the URL's: An opaque origin, which file: pages have, is the same+ // one for the life of the document, where a URL would make a fresh one on every ask.)auto url = page.view().url();- auto origin = url.origin();+ auto origin = page.traversable().active_document().origin();Record record {.id = m_next_id++,.page = page,+ .page_id = page.id(),.page_notification_id = page_notification_id,.origin = origin,.origin_url = URL::Parser::basic_parse(origin.serialize()).value_or(url),@@ -92,6 +95,7 @@ void NotificationManager::show(WebContentPage& page, u64 page_notification_id, Wm_records.append(move(record));backend().show(PlatformNotification {.id = id,+ .page_id = page.id(),.replaces = replaces,.data = move(data),.attribution = attribution_for_origin(origin),@@ -105,11 +109,23 @@ void NotificationManager::close(WebContentPage& page, u64 page_notification_id)backend().close(m_records[*index].id);}+void NotificationManager::abandon(WebContentPage& page, u64 page_notification_id)+{+ if (auto index = index_of(page, page_notification_id); index.has_value())+ m_records[*index].page = nullptr;+}+void NotificationManager::click_handled(WebContentPage& page, u64 page_notification_id, bool into_focus){- if (!into_focus)+ // Only a click the browser sent can bring the tab into focus; the notification itself may be gone by now, since+ // a click takes it down.+ auto pending = m_clicks_awaiting_reply.find_first_index_if([&](auto const& reply) {+ return reply.page == &page && reply.page_notification_id == page_notification_id;+ });+ if (!pending.has_value())return;- if (index_of(page, page_notification_id).has_value())+ m_clicks_awaiting_reply.remove(*pending);+ if (into_focus)Application::the().focus_page_from_notification(page);}@@ -189,15 +205,17 @@ void NotificationManager::backend_did_click(u64 id)return;auto const& record = m_records[*index];if (record.page && record.page->is_open()) {+ m_clicks_awaiting_reply.append({ record.page, record.page_notification_id });record.page->async_notification_clicked(record.page_notification_id);return;}// The page is gone, so the click opens the site instead; the notification has done its job.auto origin_url = record.origin_url;+ auto page_id = record.page_id;auto traversable = record.traversable;m_records.remove(*index);- Application::the().open_url_in_new_tab_from_notification(origin_url, traversable.ptr());+ Application::the().open_url_in_new_tab_from_notification(origin_url, page_id, traversable.ptr());}void NotificationManager::backend_did_close(u64 id, NotificationBackend::ByUser by_user)diff --git a/Libraries/LibWebView/NotificationManager.h b/Libraries/LibWebView/NotificationManager.hindex be82ea3352b..4f9cc160646 100644--- a/Libraries/LibWebView/NotificationManager.h+++ b/Libraries/LibWebView/NotificationManager.h@@ -17,6 +17,7 @@#include <LibURL/Origin.h>#include <LibURL/URL.h>#include <LibWebCommon/NotificationsAPI/NotificationData.h>+#include <LibWebCommon/Page/PageId.h>#include <LibWebView/Forward.h>#include <LibWebView/NotificationBackend.h>@@ -36,6 +37,8 @@ public:void show(WebContentPage&, u64 page_notification_id, Web::NotificationsAPI::NotificationData, RefPtr<Gfx::Bitmap const> icon);void close(WebContentPage&, u64 page_notification_id);+ // The page's document is gone: The notification stays up, and a click on it opens the site in a new tab.+ void abandon(WebContentPage&, u64 page_notification_id);void click_handled(WebContentPage&, u64 page_notification_id, bool into_focus);void page_did_close(WebContentPage&);@@ -48,8 +51,9 @@ public:private:struct Record {u64 id { 0 };- // Null once the page has closed; the record then only serves a later click.+ // Null once the page has closed or abandoned the notification; the record then only serves a later click.RefPtr<WebContentPage> page;+ Web::PageId page_id;u64 page_notification_id { 0 };URL::Origin origin;// What a click opens once the page is gone.@@ -71,8 +75,15 @@ private:void backend_did_click(u64 id);void backend_did_close(u64 id, NotificationBackend::ByUser);+ // A click sent to a page, until the page says whether its handler let the tab come into focus.+ struct PendingClickReply {+ RefPtr<WebContentPage> page;+ u64 page_notification_id { 0 };+ };+OwnPtr<NotificationBackend> m_backend;Vector<Record> m_records;+ Vector<PendingClickReply> m_clicks_awaiting_reply;u64 m_next_id { 1 };};diff --git a/Libraries/LibWebView/RecordingNotificationBackend.cpp b/Libraries/LibWebView/RecordingNotificationBackend.cppindex 4a1a264c6ba..b84c5d3fe92 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.cpp+++ b/Libraries/LibWebView/RecordingNotificationBackend.cpp@@ -4,6 +4,8 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/JsonArray.h>+#include <AK/JsonObject.h>#include <LibWebView/RecordingNotificationBackend.h>namespace WebView {@@ -20,6 +22,7 @@ void RecordingNotificationBackend::show(PlatformNotification const& notificationif (notification.replaces.has_value()) {if (auto index = index_of(*notification.replaces); index.has_value()) {m_shown[*index] = notification;+ m_page_of.set(notification.id, notification.page_id);if (on_shown)on_shown(notification.id);return;@@ -27,6 +30,7 @@ void RecordingNotificationBackend::show(PlatformNotification const& notification}m_shown.append(notification);+ m_page_of.set(notification.id, notification.page_id);if (on_shown)on_shown(notification.id);}@@ -35,17 +39,23 @@ void RecordingNotificationBackend::close(u64 id){if (auto index = index_of(id); index.has_value())m_shown.remove(*index);- m_closed.append(id);+ m_closed.append({ id, m_page_of.get(id).value_or({}) });if (on_closed)on_closed(id, ByUser::No);}void RecordingNotificationBackend::click(u64 id){- if (!index_of(id).has_value())+ auto index = index_of(id);+ if (!index.has_value())return;+ // A click takes the notification down, as it does on every platform.+ m_shown.remove(*index);+ m_closed.append({ id, m_page_of.get(id).value_or({}) });if (on_clicked)on_clicked(id);+ if (on_closed)+ on_closed(id, ByUser::Yes);}void RecordingNotificationBackend::dismiss(u64 id)@@ -54,11 +64,67 @@ void RecordingNotificationBackend::dismiss(u64 id)if (!index.has_value())return;m_shown.remove(*index);- m_closed.append(id);+ m_closed.append({ id, m_page_of.get(id).value_or({}) });if (on_closed)on_closed(id, ByUser::Yes);}+void RecordingNotificationBackend::record_focus(Web::PageId page_id)+{+ m_focused.append(page_id);+}++void RecordingNotificationBackend::record_open(URL::URL const& url, Web::PageId originating_page)+{+ m_opened.append({ url, originating_page });+}++String RecordingNotificationBackend::state_json(Web::PageId page_id) const+{+ JsonArray shown;+ for (auto const& notification : m_shown) {+ if (notification.page_id != page_id)+ continue;+ JsonObject entry;+ entry.set("id"sv, notification.id);+ entry.set("title"sv, notification.data.title.to_utf8());+ entry.set("body"sv, notification.data.body.to_utf8());+ entry.set("tag"sv, notification.data.tag.to_utf8());+ entry.set("attribution"sv, notification.attribution);+ entry.set("silent"sv, notification.data.silent.has_value() ? JsonValue(*notification.data.silent) : JsonValue());+ entry.set("requireInteraction"sv, notification.data.require_interaction);+ entry.set("renotify"sv, notification.data.renotify);+ entry.set("hasIcon"sv, !notification.icon.is_null());+ entry.set("replaces"sv, notification.replaces.has_value() ? JsonValue(*notification.replaces) : JsonValue());+ shown.must_append(move(entry));+ }++ JsonArray closed;+ for (auto const& entry : m_closed) {+ if (entry.page_id == page_id)+ closed.must_append(entry.id);+ }++ JsonArray focused;+ for (auto focused_page_id : m_focused) {+ if (focused_page_id == page_id)+ focused.must_append(focused_page_id.value());+ }++ JsonArray opened;+ for (auto const& entry : m_opened) {+ if (entry.page_id == page_id)+ opened.must_append(entry.url.serialize());+ }++ JsonObject state;+ state.set("shown"sv, move(shown));+ state.set("closed"sv, move(closed));+ state.set("focused"sv, move(focused));+ state.set("opened"sv, move(opened));+ return state.serialized();+}+Optional<size_t> RecordingNotificationBackend::index_of(u64 id) const{for (size_t index = 0; index < m_shown.size(); ++index) {diff --git a/Libraries/LibWebView/RecordingNotificationBackend.h b/Libraries/LibWebView/RecordingNotificationBackend.hindex 3423d3ca084..21e9c53e96f 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.h+++ b/Libraries/LibWebView/RecordingNotificationBackend.h@@ -6,7 +6,9 @@#pragma once+#include <AK/HashMap.h>#include <AK/Vector.h>+#include <LibURL/URL.h>#include <LibWebView/NotificationBackend.h>namespace WebView {@@ -21,8 +23,18 @@ public:void click(u64 id);void dismiss(u64 id);+ virtual RecordingNotificationBackend* as_recording_backend() override { return this; }+Vector<PlatformNotification> const& shown() const { return m_shown; }- Vector<u64> const& closed() const { return m_closed; }++ // What the browser did on a click: the pages it brought into focus, and the URLs it opened for notifications+ // whose page was gone. The test application records them here.+ void record_focus(Web::PageId);+ void record_open(URL::URL const&, Web::PageId originating_page);++ // Everything above, for the notifications of one page, as JSON for a test page to read. Tests run side by side in+ // one UI process, so each sees only its own.+ String state_json(Web::PageId) const;// When set, the next show() fails instead, as a platform that refuses the notification would.bool fail_next_show { false };@@ -30,8 +42,20 @@ public:private:Optional<size_t> index_of(u64 id) const;+ struct Closed {+ u64 id { 0 };+ Web::PageId page_id;+ };+ struct Opened {+ URL::URL url;+ Web::PageId page_id;+ };+Vector<PlatformNotification> m_shown;- Vector<u64> m_closed;+ Vector<Closed> m_closed;+ Vector<Web::PageId> m_focused;+ Vector<Opened> m_opened;+ HashMap<u64, Web::PageId> m_page_of;};}diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex e4911ac6c0f..5e02ce88bae 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -38,8 +38,10 @@#include <LibWebView/HelperProcess.h>#include <LibWebView/HistoryStore.h>#include <LibWebView/NavigationLoader.h>+#include <LibWebView/NotificationManager.h>#include <LibWebView/PermissionManager.h>#include <LibWebView/PictureInPictureManager.h>+#include <LibWebView/RecordingNotificationBackend.h>#include <LibWebView/RequestServerManager.h>#include <LibWebView/ViewImplementation.h>#include <LibWebView/WebContentClient.h>@@ -647,6 +649,13 @@ Messages::WebContentClient::DidRequestAllCookiesCookiestoreResponse WebContentClreturn Vector<HTTP::Cookie::Cookie> {};}+Messages::WebContentClient::DidRequestNotificationsStateForTestingResponse WebContentClient::did_request_notifications_state_for_testing(Web::PageId page_id)+{+ if (auto* recording_backend = NotificationManager::the().backend().as_recording_backend())+ return recording_backend->state_json(page_id);+ return "{}"_string;+}+Messages::WebContentClient::DidRequestNamedCookieResponse WebContentClient::did_request_named_cookie(URL::URL url, String name){if (!renderers_may_access_cookies_like_http()) {diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex e7eead909a2..a39b0a5ab0d 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -203,6 +203,7 @@ private:virtual Messages::WebContentClient::DidRequestAllCookiesCookiestoreResponse did_request_all_cookies_cookiestore(Web::PageId page_id, Web::HTML::EnvironmentId, URL::URL) override;virtual Messages::WebContentClient::DidRequestPermissionSnapshotResponse did_request_permission_snapshot(Web::PageId, URL::Origin top_level_origin) override;virtual Messages::WebContentClient::DidRequestNamedCookieResponse did_request_named_cookie(URL::URL, String) override;+ virtual Messages::WebContentClient::DidRequestNotificationsStateForTestingResponse did_request_notifications_state_for_testing(Web::PageId) override;virtual Messages::WebContentClient::DidRequestCookieResponse did_request_cookie(Web::PageId page_id, Optional<Web::HTML::EnvironmentId>, URL::URL, HTTP::Cookie::Source) override;virtual void did_close_browsing_context(Web::PageId page_id) override;virtual Messages::WebContentClient::DidRequestStorageUsageResponse did_request_storage_usage(Web::PageId page_id, Web::HTML::EnvironmentId environment_id) override;diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex eb21727cacb..6276999cef3 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -33,6 +33,7 @@#include <LibWebView/NotificationManager.h>#include <LibWebView/PermissionManager.h>#include <LibWebView/PictureInPictureManager.h>+#include <LibWebView/RecordingNotificationBackend.h>#include <LibWebView/RequestServerManager.h>#include <LibWebView/StorageAreaRegistry.h>#include <LibWebView/StorageJar.h>@@ -1875,11 +1876,40 @@ void WebContentPage::did_request_close_notification(u64 notification_id)NotificationManager::the().close(*this, notification_id);}+void WebContentPage::did_abandon_notification(u64 notification_id)+{+ NotificationManager::the().abandon(*this, notification_id);+}+void WebContentPage::did_handle_notification_click(u64 notification_id, bool into_focus){NotificationManager::the().click_handled(*this, notification_id, into_focus);}+void WebContentPage::did_request_click_notification_for_testing(u64 notification_id)+{+ auto* recording_backend = NotificationManager::the().backend().as_recording_backend();+ if (!recording_backend)+ return;+ if (auto id = NotificationManager::the().id_for(*this, notification_id); id.has_value())+ recording_backend->click(*id);+}++void WebContentPage::did_request_click_notification_by_id_for_testing(u64 browser_notification_id)+{+ if (auto* recording_backend = NotificationManager::the().backend().as_recording_backend())+ recording_backend->click(browser_notification_id);+}++void WebContentPage::did_request_dismiss_notification_for_testing(u64 notification_id)+{+ auto* recording_backend = NotificationManager::the().backend().as_recording_backend();+ if (!recording_backend)+ return;+ if (auto id = NotificationManager::the().id_for(*this, notification_id); id.has_value())+ recording_backend->dismiss(*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 e5b0f5aabba..4ead28f11e1 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -284,7 +284,11 @@ private:virtual void did_request_permission(u64 request_id, Web::HTML::CrossProcessId document_id, Utf16String name) override;virtual void did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon) override;virtual void did_request_close_notification(u64 notification_id) override;+ virtual void did_abandon_notification(u64 notification_id) override;virtual void did_handle_notification_click(u64 notification_id, bool into_focus) override;+ 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_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 50af2c0b81b..6a5ece8315d 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -1320,11 +1320,36 @@ void PageClient::page_did_request_close_notification(u64 notification_id)client().async_did_request_close_notification(m_id, notification_id);}+void PageClient::page_did_abandon_notification(u64 notification_id)+{+ client().async_did_abandon_notification(m_id, notification_id);+}+void PageClient::page_did_handle_notification_click(u64 notification_id, bool into_focus){client().async_did_handle_notification_click(m_id, notification_id, into_focus);}+String PageClient::page_did_request_notifications_state_for_testing()+{+ return client().did_request_notifications_state_for_testing(m_id);+}++void PageClient::page_did_request_click_notification_for_testing(u64 notification_id)+{+ client().async_did_request_click_notification_for_testing(m_id, notification_id);+}++void PageClient::page_did_request_click_notification_by_id_for_testing(u64 browser_notification_id)+{+ client().async_did_request_click_notification_by_id_for_testing(m_id, browser_notification_id);+}++void PageClient::page_did_request_dismiss_notification_for_testing(u64 notification_id)+{+ client().async_did_request_dismiss_notification_for_testing(m_id, notification_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 b68d2448170..d43bef7289c 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -365,7 +365,12 @@ private:virtual Optional<Web::Permissions::PermissionSnapshot> page_did_advance_permission_clock_for_testing(u64 milliseconds) override;virtual void page_did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData const&, RefPtr<Gfx::Bitmap const> icon) override;virtual void page_did_request_close_notification(u64 notification_id) override;+ virtual void page_did_abandon_notification(u64 notification_id) override;virtual void page_did_handle_notification_click(u64 notification_id, bool into_focus) override;+ virtual String page_did_request_notifications_state_for_testing() override;+ 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_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 6806704516b..f5877269474 100644--- a/Services/WebContent/WebContentClient.ipc+++ b/Services/WebContent/WebContentClient.ipc@@ -244,7 +244,12 @@ endpoint WebContentClient routed by Web::PageId page_id[NotRouted] did_request_permission_snapshot(Web::PageId page_id, URL::Origin top_level_origin) => (Web::Permissions::PermissionSnapshot snapshot)did_request_show_notification(Web::PageId page_id, u64 notification_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon) =|did_request_close_notification(Web::PageId page_id, u64 notification_id) =|+ did_abandon_notification(Web::PageId page_id, u64 notification_id) =|did_handle_notification_click(Web::PageId page_id, u64 notification_id, bool into_focus) =|+ [NotRouted] did_request_notifications_state_for_testing(Web::PageId page_id) => (String json)+ did_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_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/attribution-origin.txt b/Tests/LibWeb/Text/expected/notifications/attribution-origin.txtnew file mode 100644index 00000000000..48d01ed1057--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/attribution-origin.txt@@ -0,0 +1 @@+attribution matches: truediff --git a/Tests/LibWeb/Text/expected/notifications/click-fires-with-activation-and-focuses.txt b/Tests/LibWeb/Text/expected/notifications/click-fires-with-activation-and-focuses.txtnew file mode 100644index 00000000000..be7e3b63537--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/click-fires-with-activation-and-focuses.txt@@ -0,0 +1,2 @@+click, activation active: true+focused: 1diff --git a/Tests/LibWeb/Text/expected/notifications/click-prevent-default-no-focus.txt b/Tests/LibWeb/Text/expected/notifications/click-prevent-default-no-focus.txtnew file mode 100644index 00000000000..01d5aa62d1a--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/click-prevent-default-no-focus.txt@@ -0,0 +1,2 @@+click+focused: 0diff --git a/Tests/LibWeb/Text/expected/notifications/close-fires-close-event.txt b/Tests/LibWeb/Text/expected/notifications/close-fires-close-event.txtnew file mode 100644index 00000000000..ffa93a7d9af--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/close-fires-close-event.txt@@ -0,0 +1 @@+close, shown: 0, closed: 1diff --git a/Tests/LibWeb/Text/expected/notifications/dead-page-click-opens-origin.txt b/Tests/LibWeb/Text/expected/notifications/dead-page-click-opens-origin.txtnew file mode 100644index 00000000000..b2edcc63904--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/dead-page-click-opens-origin.txt@@ -0,0 +1,3 @@+opened this page's site: true+focused: 0+still shown: falsediff --git a/Tests/LibWeb/Text/expected/notifications/dismiss-fires-close-event.txt b/Tests/LibWeb/Text/expected/notifications/dismiss-fires-close-event.txtnew file mode 100644index 00000000000..83fe29a85db--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/dismiss-fires-close-event.txt@@ -0,0 +1 @@+closediff --git a/Tests/LibWeb/Text/expected/notifications/show-event.txt b/Tests/LibWeb/Text/expected/notifications/show-event.txtnew file mode 100644index 00000000000..ecffd4af6f8--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/show-event.txt@@ -0,0 +1,7 @@+shown: 1+title: Hello+body: World+tag: t1+hasIcon: false+silent: null+requireInteraction: falsediff --git a/Tests/LibWeb/Text/input/notifications/attribution-origin.html b/Tests/LibWeb/Text/input/notifications/attribution-origin.htmlnew file mode 100644index 00000000000..c9580afa0e4--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/attribution-origin.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The notification is attributed to the page's origin as the browser knows it, scheme omitted.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const notification = new Notification("Attributed");+ notification.onshow = () => {+ const state = JSON.parse(internals.notificationsStateForTesting());+ const expected = location.protocol === "file:" ? "this page" : location.host;+ println(`attribution matches: ${state.shown[0].attribution === expected}`);+ notification.close();+ done();+ };+ notification.onerror = () => { println("error"); done(); };+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/click-fires-with-activation-and-focuses.html b/Tests/LibWeb/Text/input/notifications/click-fires-with-activation-and-focuses.htmlnew file mode 100644index 00000000000..09f7d06e518--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/click-fires-with-activation-and-focuses.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A click on the notification fires click with transient activation, and then brings the tab into focus.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const notification = new Notification("Click me");+ notification.onshow = () => internals.clickNotificationForTesting(notification);+ notification.onclick = () => {+ println(`click, activation active: ${navigator.userActivation.isActive}`);+ // The focus request follows the click's dispatch, so give the UI process a turn to receive it.+ setTimeout(() => {+ const state = JSON.parse(internals.notificationsStateForTesting());+ println(`focused: ${state.focused.length}`);+ notification.close();+ done();+ }, 0);+ };+ notification.onerror = () => { println("error"); done(); };+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/click-prevent-default-no-focus.html b/Tests/LibWeb/Text/input/notifications/click-prevent-default-no-focus.htmlnew file mode 100644index 00000000000..15b47714b9e--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/click-prevent-default-no-focus.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A click handler that cancels the event keeps the tab from being brought into focus.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const notification = new Notification("Click me");+ notification.onshow = () => internals.clickNotificationForTesting(notification);+ notification.onclick = event => {+ event.preventDefault();+ println("click");+ setTimeout(() => {+ const state = JSON.parse(internals.notificationsStateForTesting());+ println(`focused: ${state.focused.length}`);+ notification.close();+ done();+ }, 0);+ };+ notification.onerror = () => { println("error"); done(); };+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/close-fires-close-event.html b/Tests/LibWeb/Text/input/notifications/close-fires-close-event.htmlnew file mode 100644index 00000000000..42bef98bdf9--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/close-fires-close-event.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const notification = new Notification("Close me");+ notification.onshow = () => notification.close();+ notification.onclose = () => {+ const state = JSON.parse(internals.notificationsStateForTesting());+ println(`close, shown: ${state.shown.length}, closed: ${state.closed.length}`);+ done();+ };+ notification.onerror = () => { println("error"); done(); };+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.html b/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.htmlnew file mode 100644index 00000000000..574bfbed640--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.html@@ -0,0 +1,41 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A click on a notification whose document has gone opens that document's site in a new tab instead of firing+ // click into a dead document.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ // A srcdoc frame shares this page's origin, so its notification is allowed; removing it destroys its document+ // while this page, and the notification, stay.+ const iframe = document.createElement("iframe");+ iframe.srcdoc = `<!DOCTYPE html><script>+ const notification = new Notification("From a document about to go away");+ notification.onshow = () => {+ const state = JSON.parse(window.internals.notificationsStateForTesting());+ parent.postMessage({ id: state.shown[state.shown.length - 1].id }, "*");+ };+ notification.onerror = () => parent.postMessage({ error: true }, "*");+ <\/script>`;+ const { id, error } = await new Promise(resolve => {+ addEventListener("message", event => resolve(event.data), { once: true });+ document.body.appendChild(iframe);+ });+ if (error) {+ println("error");+ done();+ return;+ }++ iframe.remove();+ // Let the frame's document go away, and the UI process hear of it, before the click.+ await new Promise(resolve => setTimeout(resolve, 100));+ internals.clickNotificationByIdForTesting(id);+ await new Promise(resolve => setTimeout(resolve, 100));++ const state = JSON.parse(internals.notificationsStateForTesting());+ println(`opened this page's site: ${state.opened.length === 1 && state.opened[0] === location.origin + "/"}`);+ println(`focused: ${state.focused.length}`);+ println(`still shown: ${state.shown.some(n => n.id === id)}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/dismiss-fires-close-event.html b/Tests/LibWeb/Text/input/notifications/dismiss-fires-close-event.htmlnew file mode 100644index 00000000000..18601851a83--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/dismiss-fires-close-event.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The user dismissing the notification fires close on the page.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const notification = new Notification("Dismiss me");+ notification.onshow = () => internals.dismissNotificationForTesting(notification);+ notification.onclose = () => {+ println("close");+ done();+ };+ notification.onerror = () => { println("error"); done(); };+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/show-event.html b/Tests/LibWeb/Text/input/notifications/show-event.htmlnew file mode 100644index 00000000000..673a3a7b416--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/show-event.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const notification = new Notification("Hello", { body: "World", tag: "t1" });+ notification.onshow = () => {+ const state = JSON.parse(internals.notificationsStateForTesting());+ println(`shown: ${state.shown.length}`);+ const shown = state.shown[0];+ println(`title: ${shown.title}`);+ println(`body: ${shown.body}`);+ println(`tag: ${shown.tag}`);+ println(`hasIcon: ${shown.hasIcon}`);+ println(`silent: ${shown.silent}`);+ println(`requireInteraction: ${shown.requireInteraction}`);+ notification.close();+ done();+ };+ notification.onerror = () => { println("error"); done(); };+ });+</script>diff --git a/Tests/LibWeb/test-web/Application.cpp b/Tests/LibWeb/test-web/Application.cppindex d3ef4b9fd8a..cc8b94140f2 100644--- a/Tests/LibWeb/test-web/Application.cpp+++ b/Tests/LibWeb/test-web/Application.cpp@@ -14,7 +14,10 @@#include <LibCore/StandardPaths.h>#include <LibCore/System.h>#include <LibWebCommon/Loader/DownloadFilename.h>+#include <LibWebView/NotificationManager.h>+#include <LibWebView/RecordingNotificationBackend.h>#include <LibWebView/TestSpellChecker.h>+#include <LibWebView/WebContentPage.h>#include <errno.h>namespace TestWeb {@@ -139,6 +142,23 @@ ErrorOr<LexicalPath> Application::default_path_for_downloaded_file(ByteString coreturn LexicalPath::join(*downloads_directory, Web::sanitize_suggested_download_filename(file));}+NonnullOwnPtr<WebView::NotificationBackend> Application::create_notification_backend()+{+ return make<WebView::RecordingNotificationBackend>();+}++void Application::focus_page_from_notification(WebView::WebContentPage& page)+{+ if (auto* recording_backend = WebView::NotificationManager::the().backend().as_recording_backend())+ recording_backend->record_focus(page.id());+}++void Application::open_url_in_new_tab_from_notification(URL::URL const& url, Web::PageId originating_page, WebView::CanonicalTraversable*)+{+ if (auto* recording_backend = WebView::NotificationManager::the().backend().as_recording_backend())+ recording_backend->record_open(url, originating_page);+}+ErrorOr<void> Application::launch_test_fixtures(){Fixture::initialize_fixtures();diff --git a/Tests/LibWeb/test-web/Application.h b/Tests/LibWeb/test-web/Application.hindex 8d8d25a1167..c97a2d21801 100644--- a/Tests/LibWeb/test-web/Application.h+++ b/Tests/LibWeb/test-web/Application.h@@ -35,6 +35,12 @@ public:virtual ErrorOr<LexicalPath> default_path_for_downloaded_file(ByteString const&) const override;+ // Notifications go to a recording backend, which tests read through Internals; what a click would do to the+ // browser's windows is recorded there too.+ virtual NonnullOwnPtr<WebView::NotificationBackend> create_notification_backend() override;+ virtual void focus_page_from_notification(WebView::WebContentPage&) override;+ virtual void open_url_in_new_tab_from_notification(URL::URL const&, Web::PageId originating_page, WebView::CanonicalTraversable*) override;+ErrorOr<void> launch_test_fixtures();static constexpr u8 VERBOSITY_LEVEL_LOG_TEST_OUTPUT = 1;