Skip to content

Truly independent web browser

LibWeb: Run the notification show, click, and close steps

sideshowbarkerdf2edb7945ed
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index b3bf33e0f48..9f6677c5236 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -951,6 +951,7 @@ class PerformanceTiming;
namespace Web::NotificationsAPI {
class Notification;
+struct NotificationData;
}
diff --git a/Libraries/LibWeb/HTML/EventNames.h b/Libraries/LibWeb/HTML/EventNames.h
index 2ef43bd823d..0a0c31c92ff 100644
--- a/Libraries/LibWeb/HTML/EventNames.h
+++ b/Libraries/LibWeb/HTML/EventNames.h
@@ -126,6 +126,7 @@ namespace Web::HTML::EventNames {
__ENUMERATE_HTML_EVENT(seeked) \
__ENUMERATE_HTML_EVENT(seeking) \
__ENUMERATE_HTML_EVENT(select) \
+ __ENUMERATE_HTML_EVENT(show) \
__ENUMERATE_HTML_EVENT(selectionchange) \
__ENUMERATE_HTML_EVENT(slotchange) \
__ENUMERATE_HTML_EVENT(soundend) \
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.cpp b/Libraries/LibWeb/NotificationsAPI/Notification.cpp
index 70f8da087ef..39302f24af2 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.cpp
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.cpp
@@ -9,10 +9,16 @@
#include <LibGC/Heap.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/VM.h>
+#include <LibWeb/DOM/Event.h>
+#include <LibWeb/HTML/EventLoop/EventLoop.h>
+#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/StructuredSerialize.h>
+#include <LibWeb/HTML/Window.h>
+#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/NotificationsAPI/Notification.h>
#include <LibWeb/NotificationsAPI/NotificationPermission.h>
+#include <LibWeb/Page/Page.h>
#include <LibWeb/ServiceWorker/ServiceWorkerGlobalScope.h>
namespace Web::NotificationsAPI {
@@ -24,6 +30,13 @@ Notification::Notification()
{
}
+void Notification::visit_edges(Cell::Visitor& visitor)
+{
+ Base::visit_edges(visitor);
+ visitor.visit(m_relevant_global);
+ visitor.visit(m_page);
+}
+
WebIDL::ExceptionOr<NotificationOptions> Notification::options_from_bindings(JS::VM& vm, Bindings::NotificationOptions const& options)
{
Vector<NotificationAction> actions;
@@ -66,7 +79,7 @@ WebIDL::ExceptionOr<GC::Ref<Notification>> Notification::create_for_constructor(
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Options `action` is not empty"_utf16 };
auto title_utf8 = title.to_utf8();
- return create_with_global_scope(*global_scope, title_utf8, TRY(options_from_bindings(relevant_global_object.shape().realm().vm(), options)));
+ return create_with_global_scope(relevant_global_object, *global_scope, title_utf8, TRY(options_from_bindings(relevant_global_object.shape().realm().vm(), options)));
}
// https://notifications.spec.whatwg.org/#create-a-notification
@@ -217,6 +230,7 @@ WebIDL::ExceptionOr<ConceptNotification> Notification::create_a_notification_wit
// https://notifications.spec.whatwg.org/#constructors
WebIDL::ExceptionOr<GC::Ref<Notification>> Notification::create_with_global_scope(
+ JS::Object& relevant_global_object,
HTML::WindowOrWorkerGlobalScopeMixin& global_scope,
String const& title,
NotificationOptions options)
@@ -237,17 +251,199 @@ WebIDL::ExceptionOr<GC::Ref<Notification>> Notification::create_with_global_scop
// 4. Associate this with notification.
auto this_notification = GC::Heap::the().allocate<Notification>();
this_notification->m_notification = notification;
+ this_notification->m_relevant_global = relevant_global_object;
+ if (auto* window = HTML::window_from_global_object(relevant_global_object))
+ this_notification->m_page = window->page();
+
+ // 5. Run these steps in parallel:
+ // AD-HOC: The steps run from a task on the relevant global instead of in parallel: Showing crosses to the UI
+ // process asynchronously anyway, and a task keeps the permission read and the event firing on the main
+ // thread, after the constructor's caller has had its turn to attach event handlers.
+ HTML::queue_global_task(HTML::Task::Source::Unspecified, relevant_global_object, GC::create_function(GC::Heap::the(), [this_notification] {
+ this_notification->run_steps_in_parallel_after_construction();
+ }));
- // FIXME: 5. Run these steps in parallel:
+ return this_notification;
+}
- // FIXME: 1. If the result of getting the notifications permission state is not "granted",
- // then queue a task to fire an event named error on this, and abort these steps.
+// Step 5 of https://notifications.spec.whatwg.org/#constructors
+void Notification::run_steps_in_parallel_after_construction()
+{
+ // 1. If the result of getting the notifications permission state is not "granted", then queue a task to fire an
+ // event named error on this, and abort these steps.
+ if (get_the_notifications_permission_state(HTML::relevant_settings_object(*m_relevant_global)) != PermissionsAPI::PermissionState::Granted) {
+ queue_task_to_fire_event(HTML::EventNames::error);
+ return;
+ }
- // FIXME: 2. Run the notification show steps for notification.
+ // AD-HOC: The spec lets any document or worker show a notification. We refuse two cases until they're supported:
+ // A worker has no page to route the notification's events through, and a document whose origin differs
+ // from its top-level origin would have its notification attributed to the top-level site. Both get an
+ // error event, as if permission had been denied.
+ auto* window = HTML::window_from_global_object(*m_relevant_global);
+ if (!window || !m_page) {
+ queue_task_to_fire_event(HTML::EventNames::error);
+ return;
+ }
+ auto& settings = HTML::relevant_settings_object(*m_relevant_global);
+ if (!settings.top_level_origin.has_value() || !settings.top_level_origin->is_same_origin(settings.origin())) {
+ queue_task_to_fire_event(HTML::EventNames::error);
+ return;
+ }
- return this_notification;
+ // 2. Run the notification show steps for notification.
+ show_steps();
+}
+
+// https://notifications.spec.whatwg.org/#notification-show-steps
+void Notification::show_steps()
+{
+ // FIXME: 1. Run the fetch steps given notification and settings.
+ // FIXME: 2. Wait for any fetches to complete and notification's image resource, icon resource, and badge resource
+ // to be set (if any), as well as the icon resources for the notification's actions (if any).
+
+ // 3. Let shown be false.
+ // 4. Let oldNotification be the notification in the list of notifications whose tag is not the empty string and is
+ // notification's tag, and whose origin is same origin with notification's origin, if any, and null otherwise.
+ // 5. If oldNotification is non-null:
+ // 1. Handle close events with oldNotification.
+ // 2. If the notification platform supports replacement:
+ // 1. Replace oldNotification with notification, in the list of notifications.
+ // 2. Set shown to true.
+ // 3. Otherwise, remove oldNotification from the list of notifications.
+ // 6. If shown is false:
+ // 1. Append notification to the list of notifications.
+ // 2. Display notification on the device (e.g., by calling the appropriate notification platform API).
+ // 7. If shown is false or oldNotification is non-null, and notification's renotify preference is true, then
+ // optionally alert the end user in a user-agent-defined manner (e.g., by playing a sound).
+ // NB: Steps 4, 5 and 7 are the UI process's: It sees every page's notifications, so it finds oldNotification
+ // across pages, replaces it on the platform, and reports its close back through notification_closed().
+ // Here, the notification joins this page's part of the list and is sent off for display.
+ m_id = m_page->allocate_notification_id();
+ m_page->register_showing_notification(m_id, *this);
+ m_state = State::Showing;
+ m_page->client().page_did_request_show_notification(m_id, data(), {});
+
+ // 8. If notification is a non-persistent notification, then queue a task to fire an event named show on the
+ // Notification object representing notification.
+ // NB: Fired from did_show(), once the platform has shown the notification: Until then the display can still fail,
+ // and a failure fires error instead.
+}
+
+void Notification::did_show()
+{
+ if (m_state != State::Showing)
+ return;
+ queue_task_to_fire_event(HTML::EventNames::show);
+}
+
+void Notification::did_fail_to_show()
+{
+ if (m_state != State::Showing)
+ return;
+ m_state = State::Closed;
+ m_page->unregister_notification(m_id);
+ queue_task_to_fire_event(HTML::EventNames::error);
+}
+
+// Step 2 of the non-persistent branch of https://notifications.spec.whatwg.org/#activating-a-notification
+void Notification::did_click()
+{
+ if (m_state != State::Showing && m_state != State::Closing)
+ return;
+
+ // Otherwise, queue a task to run these steps:
+ HTML::queue_global_task(HTML::Task::Source::UserInteraction, *m_relevant_global, GC::create_function(GC::Heap::the(), [this] {
+ // 1. Let intoFocus be the result of firing an event named click on the Notification object representing
+ // notification, with its cancelable attribute initialized to true.
+ // NB: The user activated the notification, so the window gets transient activation first: That's what lets
+ // focus() work from within the event listener, as the spec encourages.
+ if (auto* window = HTML::window_from_global_object(*m_relevant_global))
+ window->set_last_activation_timestamp(HighResolutionTime::current_high_resolution_time(*m_relevant_global));
+ auto into_focus = dispatch_event(DOM::Event::create(*m_relevant_global, HTML::EventNames::click, { .cancelable = true }));
+
+ // 2. If intoFocus is true, then the user agent should bring the notification's related browsing context's
+ // viewport into focus.
+ if (m_page)
+ m_page->client().page_did_handle_notification_click(m_id, into_focus);
+ }));
+}
+
+// https://notifications.spec.whatwg.org/#dom-notification-close
+void Notification::close()
+{
+ // The close() method steps are to run the close steps for this's notification.
+ close_steps();
+}
+
+// https://notifications.spec.whatwg.org/#close-steps
+void Notification::close_steps()
+{
+ // 1. If the list of notifications does not contain notification, then abort these steps.
+ // NB: A notification that's already on its way out (Closing) is left to the close the UI process is carrying out.
+ if (m_state != State::Showing)
+ return;
+
+ // 2. Handle close events with notification.
+ // 3. Remove notification from the list of notifications.
+ // NB: Both happen in did_close(), once the UI process has withdrawn the notification from the platform.
+ m_state = State::Closing;
+ m_page->client().page_did_request_close_notification(m_id);
+}
+
+// Steps 2 and 3 of https://notifications.spec.whatwg.org/#close-steps
+void Notification::did_close(bool)
+{
+ if (m_state != State::Showing && m_state != State::Closing)
+ return;
+
+ // 2. Handle close events with notification.
+ // https://notifications.spec.whatwg.org/#handle-close-events
+ // 2. If notification is a non-persistent notification, then queue a task to fire an event named close on the
+ // Notification object representing notification.
+ queue_task_to_fire_event(HTML::EventNames::close);
+
+ // 3. Remove notification from the list of notifications.
+ m_state = State::Closed;
+ m_page->unregister_notification(m_id);
+}
+
+void Notification::queue_task_to_fire_event(Utf16FlyString const& event_name)
+{
+ HTML::queue_global_task(HTML::Task::Source::Unspecified, *m_relevant_global, GC::create_function(GC::Heap::the(), [this, event_name] {
+ dispatch_event(DOM::Event::create(*m_relevant_global, event_name));
+ }));
+}
+
+NotificationData Notification::data() const
+{
+ return NotificationData {
+ .title = Utf16String::from_utf8(m_notification.title),
+ .body = m_notification.body,
+ .tag = m_notification.tag,
+ .language = m_notification.language,
+ .direction = m_notification.direction,
+ .silent = m_notification.silent_preference,
+ .require_interaction = m_notification.require_interaction_preference,
+ .renotify = m_notification.renotify_preference,
+ };
}
+#define __ENUMERATE(attribute_name, event_name) \
+ void Notification::set_##attribute_name(WebIDL::CallbackType* value) \
+ { \
+ set_event_handler_attribute(HTML::EventNames::event_name, value); \
+ } \
+ WebIDL::CallbackType* Notification::attribute_name() \
+ { \
+ return event_handler_attribute(HTML::EventNames::event_name); \
+ }
+__ENUMERATE(onclick, click)
+__ENUMERATE(onshow, show)
+__ENUMERATE(onerror, error)
+__ENUMERATE(onclose, close)
+#undef __ENUMERATE
+
// https://notifications.spec.whatwg.org/#dom-notification-permission
Bindings::NotificationPermission Notification::permission()
{
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.h b/Libraries/LibWeb/NotificationsAPI/Notification.h
index dd1a983be66..b6f9db87060 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.h
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.h
@@ -16,6 +16,7 @@
#include <LibWeb/Export.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HighResolutionTime/EpochTimeStamp.h>
+#include <LibWebCommon/NotificationsAPI/NotificationData.h>
#include <LibWebCommon/WebIDL/Types.h>
namespace Web::NotificationsAPI {
@@ -91,6 +92,7 @@ class WEB_API Notification final : public DOM::EventTarget {
public:
[[nodiscard]] static WebIDL::ExceptionOr<GC::Ref<Notification>> create_with_global_scope(
+ JS::Object& relevant_global_object,
HTML::WindowOrWorkerGlobalScopeMixin&,
String const& title,
NotificationOptions options);
@@ -113,6 +115,23 @@ public:
static Bindings::NotificationPermission permission();
+ void close();
+
+ void set_onclick(WebIDL::CallbackType*);
+ WebIDL::CallbackType* onclick();
+ void set_onshow(WebIDL::CallbackType*);
+ WebIDL::CallbackType* onshow();
+ void set_onerror(WebIDL::CallbackType*);
+ WebIDL::CallbackType* onerror();
+ void set_onclose(WebIDL::CallbackType*);
+ WebIDL::CallbackType* onclose();
+
+ // What the UI process reports back about the notification it shows for this object.
+ void did_show();
+ void did_fail_to_show();
+ void did_click();
+ void did_close(bool by_user);
+
// https://notifications.spec.whatwg.org/#maximum-number-of-actions-supported
// Two is what both desktop notification platforms show room for; persistent notifications, the only kind that
// carries actions, aren't supported yet.
@@ -139,9 +158,32 @@ public:
private:
Notification();
+ virtual void visit_edges(Cell::Visitor&) override;
+
static Utf16String serialize_url_for_bindings(Optional<URL::URL> const&);
+ void run_steps_in_parallel_after_construction();
+ void show_steps();
+ void close_steps();
+ void queue_task_to_fire_event(Utf16FlyString const& event_name);
+ NotificationData data() const;
+
ConceptNotification m_notification;
+
+ // The global the constructor ran in: Where the events fire, and whose window the notification belongs to.
+ GC::Ptr<JS::Object> m_relevant_global;
+ GC::Ptr<Page> m_page;
+
+ // Where the notification is on its way through the UI process. Showing and Closing mean it's in the page's list of
+ // notifications; Closed means it has left it for good.
+ enum class State {
+ Idle,
+ Showing,
+ Closing,
+ Closed,
+ };
+ State m_state { State::Idle };
+ u64 m_id { 0 };
};
}
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.idl b/Libraries/LibWeb/NotificationsAPI/Notification.idl
index ae20149eeb6..a0ea97964b3 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.idl
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.idl
@@ -8,10 +8,10 @@ interface Notification : EventTarget {
static readonly attribute unsigned long maxActions;
- [FIXME] attribute EventHandler onclick;
- [FIXME] attribute EventHandler onshow;
- [FIXME] attribute EventHandler onerror;
- [FIXME] attribute EventHandler onclose;
+ attribute EventHandler onclick;
+ attribute EventHandler onshow;
+ attribute EventHandler onerror;
+ attribute EventHandler onclose;
readonly attribute DOMString title;
[ImplementedAs=notification_direction] readonly attribute NotificationDirection dir;
@@ -30,7 +30,7 @@ interface Notification : EventTarget {
[SameObject] readonly attribute any data;
[SameObject] readonly attribute FrozenArray<NotificationAction> actions;
- [FIXME] undefined close();
+ undefined close();
};
dictionary NotificationOptions {
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 1a33c40507a..1c216b828c7 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -41,6 +41,7 @@
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Loader/ContentBlocker.h>
+#include <LibWeb/NotificationsAPI/Notification.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Page/RenderingStats.h>
#include <LibWeb/Painting/BoxViews.h>
@@ -158,6 +159,7 @@ void Page::visit_edges(JS::Cell::Visitor& visitor)
for (auto const& request : m_pending_geolocation_requests)
visitor.visit(request.value.callback);
visitor.visit(m_pending_permission_requests);
+ visitor.visit(m_notifications_list);
m_pending_fullscreen_operations.for_each([&](auto const& operation) {
operation.visit([&](PendingFullscreenEnter const& enter_operation) {
visitor.visit(enter_operation.element);
@@ -1603,6 +1605,40 @@ void Page::permission_request_completed(u64 request_id, Web::Permissions::Effect
callback.value()->function()(state);
}
+void Page::register_showing_notification(u64 notification_id, GC::Ref<NotificationsAPI::Notification> notification)
+{
+ m_notifications_list.set(notification_id, notification);
+}
+
+void Page::unregister_notification(u64 notification_id)
+{
+ m_notifications_list.remove(notification_id);
+}
+
+void Page::notification_shown(u64 notification_id)
+{
+ if (auto notification = m_notifications_list.get(notification_id); notification.has_value())
+ (*notification)->did_show();
+}
+
+void Page::notification_show_failed(u64 notification_id)
+{
+ if (auto notification = m_notifications_list.get(notification_id); notification.has_value())
+ (*notification)->did_fail_to_show();
+}
+
+void Page::notification_clicked(u64 notification_id)
+{
+ if (auto notification = m_notifications_list.get(notification_id); notification.has_value())
+ (*notification)->did_click();
+}
+
+void Page::notification_closed(u64 notification_id, bool by_user)
+{
+ if (auto notification = m_notifications_list.get(notification_id); notification.has_value())
+ (*notification)->did_close(by_user);
+}
+
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.h
index 1a15e661acb..9d77dd3d14f 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -359,6 +359,16 @@ public:
void request_permission(HTML::CrossProcessId requesting_document_id, Utf16String const& name, PermissionPromptCallback);
void permission_request_completed(u64 request_id, Web::Permissions::EffectiveState);
+ // The list of notifications (https://notifications.spec.whatwg.org/#list-of-notifications) this page has asked
+ // the UI process to show, by the id that names each one across the bridge.
+ u64 allocate_notification_id() { return m_next_notification_id++; }
+ void register_showing_notification(u64 notification_id, GC::Ref<NotificationsAPI::Notification>);
+ void unregister_notification(u64 notification_id);
+ void notification_shown(u64 notification_id);
+ void notification_show_failed(u64 notification_id);
+ void notification_clicked(u64 notification_id);
+ void notification_closed(u64 notification_id, bool by_user);
+
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
// NB: The top-level traversable's, which the UI process tells every page representing it.
Geolocation::EmulatedPositionData const& emulated_position_data() const { return m_emulated_position_data; }
@@ -594,6 +604,9 @@ private:
HashMap<u64, PermissionPromptCallback> m_pending_permission_requests;
u64 m_next_permission_request_id { 0 };
+ HashMap<u64, GC::Ref<NotificationsAPI::Notification>> m_notifications_list;
+ u64 m_next_notification_id { 0 };
+
// AD-HOC: Denied until the UI process sends the browser-wide setting, so a request cannot observe the test
// position in the short window before that arrives.
Geolocation::EmulatedPositionData m_emulated_position_data { Geolocation::GeolocationPositionError::ErrorCode::PermissionDenied };
@@ -894,6 +907,9 @@ public:
virtual void page_did_set_next_permission_prompt_outcome_for_testing([[maybe_unused]] String const& name, [[maybe_unused]] String const& outcome, [[maybe_unused]] bool held) { }
virtual void page_did_release_held_permission_prompt_for_testing([[maybe_unused]] u32 waiters) { }
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_handle_notification_click([[maybe_unused]] u64 notification_id, [[maybe_unused]] bool into_focus) { }
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/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index 298ac4d1f95..749b47537a4 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -15,6 +15,7 @@ set(SOURCES
Gamepad/GamepadSnapshot.cpp
Gamepad/GamepadStateBuffer.cpp
HTML/AutoplayPolicy.cpp
+ NotificationsAPI/NotificationData.cpp
HTML/BroadcastChannelMessage.cpp
HTML/BrowsingContext.cpp
HTML/CrossOrigin/OpenerPolicy.cpp
diff --git a/Libraries/LibWebCommon/NotificationsAPI/NotificationData.cpp b/Libraries/LibWebCommon/NotificationsAPI/NotificationData.cpp
new file mode 100644
index 00000000000..e6f9b41f924
--- /dev/null
+++ b/Libraries/LibWebCommon/NotificationsAPI/NotificationData.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibIPC/Decoder.h>
+#include <LibIPC/Encoder.h>
+#include <LibWebCommon/NotificationsAPI/NotificationData.h>
+
+template<>
+ErrorOr<void> IPC::encode(Encoder& encoder, Web::NotificationsAPI::NotificationData const& data)
+{
+ TRY(encoder.encode(data.title));
+ TRY(encoder.encode(data.body));
+ TRY(encoder.encode(data.tag));
+ TRY(encoder.encode(data.language));
+ TRY(encoder.encode(data.direction));
+ TRY(encoder.encode(data.silent));
+ TRY(encoder.encode(data.require_interaction));
+ TRY(encoder.encode(data.renotify));
+ return {};
+}
+
+template<>
+ErrorOr<Web::NotificationsAPI::NotificationData> IPC::decode(Decoder& decoder)
+{
+ Web::NotificationsAPI::NotificationData data;
+ data.title = TRY(decoder.decode<Utf16String>());
+ data.body = TRY(decoder.decode<Utf16String>());
+ data.tag = TRY(decoder.decode<Utf16String>());
+ data.language = TRY(decoder.decode<Utf16String>());
+ data.direction = TRY(decoder.decode<Web::Bindings::NotificationDirection>());
+ data.silent = TRY(decoder.decode<Optional<bool>>());
+ data.require_interaction = TRY(decoder.decode<bool>());
+ data.renotify = TRY(decoder.decode<bool>());
+ return data;
+}
diff --git a/Libraries/LibWebCommon/NotificationsAPI/NotificationData.h b/Libraries/LibWebCommon/NotificationsAPI/NotificationData.h
new file mode 100644
index 00000000000..b27f26c4576
--- /dev/null
+++ b/Libraries/LibWebCommon/NotificationsAPI/NotificationData.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/Utf16String.h>
+#include <LibIPC/Forward.h>
+#include <LibWebCommon/Bindings/Notification.h>
+#include <LibWebCommon/Export.h>
+
+namespace Web::NotificationsAPI {
+
+// What the UI process needs to show a notification: the page-supplied fields of a notification (see the "notification"
+// concept in https://notifications.spec.whatwg.org/#notifications). The origin isn't among them, since the UI
+// process attributes a notification to the origin it knows the page by, never to one the page reports.
+struct NotificationData {
+ Utf16String title;
+ Utf16String body;
+ Utf16String tag;
+ Utf16String language;
+ Bindings::NotificationDirection direction { Bindings::NotificationDirection::Auto };
+ Optional<bool> silent;
+ bool require_interaction { false };
+ bool renotify { false };
+};
+
+}
+
+namespace IPC {
+
+template<>
+WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::NotificationsAPI::NotificationData const&);
+
+template<>
+WEBCOMMON_API ErrorOr<Web::NotificationsAPI::NotificationData> decode(Decoder&);
+
+}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index cf05a8c7c97..08d7f5479a5 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1230,6 +1230,7 @@ Text/input/visual-viewport-horizontal-scroll-hit-test.html
Text/input/visual-viewport-scrollend-after-pan.html
[LoadFromInsecureOrigin]
+Text/input/notifications/permission-insecure-context.html
Text/input/wpt-import/html/browsers/origin/origin-keyed-agent-clusters/insecure-http.sub.html
Text/input/HTML/page-from-an-insecure-origin-is-not-a-secure-context.html
; Geolocation refuses a page that is not a secure context, which localhost would be.
diff --git a/Tests/LibWeb/Text/expected/notifications/cross-origin-iframe-errors.txt b/Tests/LibWeb/Text/expected/notifications/cross-origin-iframe-errors.txt
new file mode 100644
index 00000000000..bc3004b513e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/cross-origin-iframe-errors.txt
@@ -0,0 +1 @@
+cross-origin iframe: error
diff --git a/Tests/LibWeb/Text/expected/notifications/permission-insecure-context.txt b/Tests/LibWeb/Text/expected/notifications/permission-insecure-context.txt
new file mode 100644
index 00000000000..dd275eb3423
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/permission-insecure-context.txt
@@ -0,0 +1,3 @@
+secure context: false
+permission: denied
+constructor: error
diff --git a/Tests/LibWeb/Text/expected/notifications/show-error-when-not-granted.txt b/Tests/LibWeb/Text/expected/notifications/show-error-when-not-granted.txt
new file mode 100644
index 00000000000..0701b124e84
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/show-error-when-not-granted.txt
@@ -0,0 +1,2 @@
+denied: error
+prompt: error
diff --git a/Tests/LibWeb/Text/expected/notifications/worker-errors.txt b/Tests/LibWeb/Text/expected/notifications/worker-errors.txt
new file mode 100644
index 00000000000..8f5ac538244
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/worker-errors.txt
@@ -0,0 +1 @@
+worker: error
diff --git a/Tests/LibWeb/Text/expected/wpt-import/notifications/idlharness.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/notifications/idlharness.https.any.txt
index 6aca35cede3..9e3d2c2574a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/notifications/idlharness.https.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/notifications/idlharness.https.any.txt
@@ -2,9 +2,9 @@ Harness status: OK
Found 67 tests
-50 Pass
-17 Fail
-Fail idl_test setup
+61 Pass
+6 Fail
+Pass idl_test setup
Pass idl_test validation
Pass Partial interface ServiceWorkerRegistration: original interface defined
Pass Partial interface ServiceWorkerRegistration: member names are unique
@@ -20,10 +20,10 @@ Pass Notification interface: existence and properties of interface prototype obj
Pass Notification interface: attribute permission
Fail Notification interface: operation requestPermission(optional NotificationPermissionCallback)
Pass Notification interface: attribute maxActions
-Fail Notification interface: attribute onclick
-Fail Notification interface: attribute onshow
-Fail Notification interface: attribute onerror
-Fail Notification interface: attribute onclose
+Pass Notification interface: attribute onclick
+Pass Notification interface: attribute onshow
+Pass Notification interface: attribute onerror
+Pass Notification interface: attribute onclose
Pass Notification interface: attribute title
Pass Notification interface: attribute dir
Pass Notification interface: attribute lang
@@ -40,17 +40,17 @@ Pass Notification interface: attribute silent
Pass Notification interface: attribute requireInteraction
Pass Notification interface: attribute data
Pass Notification interface: attribute actions
-Fail Notification interface: operation close()
+Pass Notification interface: operation close()
Pass Notification must be primary interface of notification
Pass Stringification of notification
Pass Notification interface: notification must inherit property "permission" with the proper type
Pass Notification interface: notification must inherit property "requestPermission(optional NotificationPermissionCallback)" with the proper type
Fail Notification interface: calling requestPermission(optional NotificationPermissionCallback) on notification with too few arguments must throw TypeError
Pass Notification interface: notification must inherit property "maxActions" with the proper type
-Fail Notification interface: notification must inherit property "onclick" with the proper type
-Fail Notification interface: notification must inherit property "onshow" with the proper type
-Fail Notification interface: notification must inherit property "onerror" with the proper type
-Fail Notification interface: notification must inherit property "onclose" with the proper type
+Pass Notification interface: notification must inherit property "onclick" with the proper type
+Pass Notification interface: notification must inherit property "onshow" with the proper type
+Pass Notification interface: notification must inherit property "onerror" with the proper type
+Pass Notification interface: notification must inherit property "onclose" with the proper type
Pass Notification interface: notification must inherit property "title" with the proper type
Pass Notification interface: notification must inherit property "dir" with the proper type
Pass Notification interface: notification must inherit property "lang" with the proper type
@@ -67,7 +67,7 @@ Pass Notification interface: notification must inherit property "silent" with th
Pass Notification interface: notification must inherit property "requireInteraction" with the proper type
Pass Notification interface: notification must inherit property "data" with the proper type
Pass Notification interface: notification must inherit property "actions" with the proper type
-Fail Notification interface: notification must inherit property "close()" with the proper type
+Pass Notification interface: notification must inherit property "close()" with the proper type
Pass NotificationEvent interface: existence and properties of interface object
Fail ServiceWorkerRegistration interface: operation showNotification(DOMString, optional NotificationOptions)
Fail ServiceWorkerRegistration interface: operation getNotifications(optional GetNotificationOptions)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/notifications/cross-origin-iframe-errors.html b/Tests/LibWeb/Text/input/notifications/cross-origin-iframe-errors.html
new file mode 100644
index 00000000000..8ccf8014862
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/cross-origin-iframe-errors.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A document whose origin differs from its top-level origin gets error, not a shown notification.
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ const server = httpTestServer();
+ const frameURL = new URL(await server.createEcho("GET", "/notifications/cross-origin-frame.html", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><script>
+ const notification = new Notification("from a cross-origin frame");
+ const timeout = setTimeout(() => parent.postMessage("no event", "*"), 1000);
+ notification.onshow = () => { clearTimeout(timeout); parent.postMessage("show", "*"); };
+ notification.onerror = () => { clearTimeout(timeout); parent.postMessage("error", "*"); };
+ <\/script>`,
+ }));
+ frameURL.hostname = uniqueLocalhostHostname("notifications-cross-origin");
+
+ const iframe = document.createElement("iframe");
+ iframe.src = frameURL.href;
+ const result = await new Promise(resolve => {
+ addEventListener("message", event => resolve(event.data), { once: true });
+ document.body.appendChild(iframe);
+ });
+ println(`cross-origin iframe: ${result}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/permission-insecure-context.html b/Tests/LibWeb/Text/input/notifications/permission-insecure-context.html
new file mode 100644
index 00000000000..8fab76cbdea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/permission-insecure-context.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A page that isn't a secure context reads "denied" and can't show a notification, whatever the store holds.
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ println(`secure context: ${isSecureContext}`);
+ println(`permission: ${Notification.permission}`);
+ const notification = new Notification("title");
+ const timeout = setTimeout(() => { println("constructor: no event"); done(); }, 1000);
+ notification.onshow = () => { clearTimeout(timeout); println("constructor: show"); done(); };
+ notification.onerror = () => { clearTimeout(timeout); println("constructor: error"); done(); };
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/show-error-when-not-granted.html b/Tests/LibWeb/Text/input/notifications/show-error-when-not-granted.html
new file mode 100644
index 00000000000..57051c86efa
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/show-error-when-not-granted.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A notification created while the permission state isn't "granted" fires error and never show.
+ asyncTest(async done => {
+ const outcome = async state => {
+ await internals.setPermission("notifications", state);
+ return new Promise(resolve => {
+ const notification = new Notification("title");
+ const timeout = setTimeout(() => resolve("no event"), 1000);
+ notification.onshow = () => { clearTimeout(timeout); resolve("show"); };
+ notification.onerror = () => { clearTimeout(timeout); resolve("error"); };
+ });
+ };
+ println(`denied: ${await outcome("denied")}`);
+ println(`prompt: ${await outcome("prompt")}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/worker-errors.html b/Tests/LibWeb/Text/input/notifications/worker-errors.html
new file mode 100644
index 00000000000..40f4c537c09
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/worker-errors.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<script type="worker">
+ const notification = new Notification("from a worker");
+ const timeout = setTimeout(() => postMessage("no event"), 1000);
+ notification.onshow = () => { clearTimeout(timeout); postMessage("show"); };
+ notification.onerror = () => { clearTimeout(timeout); postMessage("error"); };
+</script>
+<script src="../include.js"></script>
+<script>
+ // A dedicated worker can construct a Notification, but showing it from a worker isn't supported yet: error fires.
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ const workerSource = document.querySelector("[type=worker]").innerText;
+ const worker = new Worker(URL.createObjectURL(new Blob([workerSource], { type: "text/javascript" })));
+ worker.onmessage = event => {
+ println(`worker: ${event.data}`);
+ done();
+ };
+ });
+</script>