Skip to content

Truly independent web browser

LibWebView+WebContent: Show web notifications through a platform backend

sideshowbarker492769ea58fa
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index dcfab529224..42087693701 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -62,6 +62,7 @@
#include <LibWebView/HistoryStore.h>
#include <LibWebView/Menu.h>
#include <LibWebView/PermissionManager.h>
+#include <LibWebView/NoOpNotificationBackend.h>
#include <LibWebView/PictureInPictureWindow.h>
#include <LibWebView/ProcessType.h>
#include <LibWebView/RequestServerManager.h>
@@ -2487,6 +2488,11 @@ bool Application::supports_picture_in_picture() const
return browser_options().headless_mode.has_value();
}
+NonnullOwnPtr<NotificationBackend> Application::create_notification_backend()
+{
+ return make<NoOpNotificationBackend>();
+}
+
OwnPtr<PictureInPictureWindow> Application::create_picture_in_picture_window(WebContentPage& requesting_page, CanonicalTraversable& traversable, Gfx::IntSize video_size)
{
if (!browser_options().headless_mode.has_value())
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 305b694b9c6..2fbc60a1fcb 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -156,6 +156,14 @@ public:
virtual OwnPtr<PictureInPictureWindow> create_picture_in_picture_window(WebContentPage& requesting_page, CanonicalTraversable&, Gfx::IntSize video_size);
+ // The platform's notification support; the NotificationManager asks once, the first time a page shows one.
+ virtual NonnullOwnPtr<NotificationBackend> create_notification_backend();
+ // A click on a notification brought its page's tab into focus (the page's click handler didn't cancel it).
+ 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*) { }
+
void appearance_changed(Badge<ApplicationSettingsObserver>);
void tab_settings_changed(Badge<ApplicationSettingsObserver>);
void languages_changed(Badge<ApplicationSettingsObserver>);
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index bcfcb104058..d32e136b7e4 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -47,6 +47,8 @@ set(SOURCES
NavigationLoader.cpp
Omnibox.cpp
OmniboxEngagement.cpp
+ NotificationAttribution.cpp
+ NotificationManager.cpp
PictureInPictureManager.cpp
PictureInPictureWindow.cpp
PermissionManager.cpp
@@ -58,6 +60,7 @@ set(SOURCES
ProcessMonitor.cpp
Profile.cpp
ReportSubmission.cpp
+ RecordingNotificationBackend.cpp
RequestServerManager.cpp
RequestServerSiteBindings.cpp
SearchEngine.cpp
diff --git a/Libraries/LibWebView/Forward.h b/Libraries/LibWebView/Forward.h
index 1657170f193..ded70f907d4 100644
--- a/Libraries/LibWebView/Forward.h
+++ b/Libraries/LibWebView/Forward.h
@@ -47,6 +47,8 @@ class HSTSStore;
class Menu;
class NavigationLoader;
class OutOfProcessWebView;
+class NotificationBackend;
+class NotificationManager;
class PictureInPictureManager;
class PictureInPictureWindow;
class ProcessManager;
diff --git a/Libraries/LibWebView/NoOpNotificationBackend.h b/Libraries/LibWebView/NoOpNotificationBackend.h
new file mode 100644
index 00000000000..b70e2531151
--- /dev/null
+++ b/Libraries/LibWebView/NoOpNotificationBackend.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWebView/NotificationBackend.h>
+
+namespace WebView {
+
+// The backend of a platform with no notification support: every show fails, so pages see an error event.
+class NoOpNotificationBackend final : public NotificationBackend {
+public:
+ virtual void show(PlatformNotification const& notification) override
+ {
+ if (on_failed)
+ on_failed(notification.id);
+ }
+
+ virtual void close(u64) override { }
+};
+
+}
diff --git a/Libraries/LibWebView/NotificationAttribution.cpp b/Libraries/LibWebView/NotificationAttribution.cpp
new file mode 100644
index 00000000000..7aa7b658338
--- /dev/null
+++ b/Libraries/LibWebView/NotificationAttribution.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWebView/NotificationAttribution.h>
+
+namespace WebView {
+
+String attribution_for_origin(URL::Origin const& origin)
+{
+ if (origin.is_opaque())
+ return "this page"_string;
+
+ auto serialized = origin.serialize();
+ for (auto prefix : { "https://"sv, "http://"sv }) {
+ if (serialized.starts_with_bytes(prefix))
+ return MUST(serialized.substring_from_byte_offset(prefix.length()));
+ }
+ return serialized;
+}
+
+}
diff --git a/Libraries/LibWebView/NotificationAttribution.h b/Libraries/LibWebView/NotificationAttribution.h
new file mode 100644
index 00000000000..4f05d6716d5
--- /dev/null
+++ b/Libraries/LibWebView/NotificationAttribution.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/String.h>
+#include <LibURL/Origin.h>
+#include <LibWebView/Export.h>
+
+namespace WebView {
+
+// The origin as a notification shows it to the user: the host and any explicit port, without the scheme for http(s),
+// so that "example.com" reads as the site. An opaque origin reads as "this page".
+WEBVIEW_API String attribution_for_origin(URL::Origin const&);
+
+}
diff --git a/Libraries/LibWebView/NotificationBackend.h b/Libraries/LibWebView/NotificationBackend.h
new file mode 100644
index 00000000000..e076d0cfe41
--- /dev/null
+++ b/Libraries/LibWebView/NotificationBackend.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Function.h>
+#include <AK/Optional.h>
+#include <AK/RefPtr.h>
+#include <AK/String.h>
+#include <AK/Types.h>
+#include <AK/kmalloc.h>
+#include <LibGfx/Bitmap.h>
+#include <LibWebCommon/NotificationsAPI/NotificationData.h>
+#include <LibWebView/Export.h>
+
+namespace WebView {
+
+// A notification as a platform backend shows it: the page's fields, plus what the browser adds to them.
+struct PlatformNotification {
+ // Names the notification for the life of the process; the backend reports every event by it.
+ u64 id { 0 };
+ // The notification with the same origin and tag that this one takes the place of, if any.
+ Optional<u64> replaces;
+ Web::NotificationsAPI::NotificationData data;
+ // The origin the notification is shown as coming from. See attribution_for_origin().
+ String attribution;
+ RefPtr<Gfx::Bitmap const> icon;
+};
+
+// 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.
+class WEBVIEW_API NotificationBackend {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ virtual ~NotificationBackend() = default;
+
+ enum class ByUser {
+ No,
+ Yes,
+ };
+
+ virtual void show(PlatformNotification const&) = 0;
+ virtual void close(u64 id) = 0;
+
+ Function<void(u64 id)> on_shown;
+ Function<void(u64 id)> on_failed;
+ Function<void(u64 id)> on_clicked;
+ Function<void(u64 id, ByUser)> on_closed;
+};
+
+}
diff --git a/Libraries/LibWebView/NotificationManager.cpp b/Libraries/LibWebView/NotificationManager.cpp
new file mode 100644
index 00000000000..81ad1735208
--- /dev/null
+++ b/Libraries/LibWebView/NotificationManager.cpp
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibURL/Parser.h>
+#include <LibWebView/Application.h>
+#include <LibWebView/CanonicalTraversable.h>
+#include <LibWebView/NotificationAttribution.h>
+#include <LibWebView/NotificationManager.h>
+#include <LibWebView/ViewImplementation.h>
+#include <LibWebView/WebContentPage.h>
+
+namespace WebView {
+
+NotificationManager& NotificationManager::the()
+{
+ static auto& manager = *new NotificationManager;
+ return manager;
+}
+
+NotificationBackend& NotificationManager::backend()
+{
+ if (!m_backend)
+ set_backend(Application::the().create_notification_backend());
+ return *m_backend;
+}
+
+void NotificationManager::set_backend(NonnullOwnPtr<NotificationBackend> backend)
+{
+ m_backend = move(backend);
+ install_backend_callbacks();
+}
+
+void NotificationManager::install_backend_callbacks()
+{
+ m_backend->on_shown = [this](u64 id) { backend_did_show(id); };
+ m_backend->on_failed = [this](u64 id) { backend_did_fail(id); };
+ m_backend->on_clicked = [this](u64 id) { backend_did_click(id); };
+ m_backend->on_closed = [this](u64 id, NotificationBackend::ByUser by_user) { backend_did_close(id, by_user); };
+}
+
+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.
+ auto url = page.view().url();
+ auto origin = url.origin();
+
+ Record record {
+ .id = m_next_id++,
+ .page = page,
+ .page_notification_id = page_notification_id,
+ .origin = origin,
+ .origin_url = URL::Parser::basic_parse(origin.serialize()).value_or(url),
+ .traversable = page.traversable().make_weak_ptr<CanonicalTraversable>(),
+ .tag = data.tag,
+ };
+
+ // Same origin and same tag take the place of the earlier notification (the spec's oldNotification): Its page
+ // hears that it closed, and the platform replaces it in place.
+ Optional<u64> replaces;
+ if (!data.tag.is_empty()) {
+ for (size_t index = 0; index < m_records.size(); ++index) {
+ auto const& existing = m_records[index];
+ if (existing.tag == data.tag && existing.origin.is_same_origin(origin)) {
+ replaces = existing.id;
+ report_closed(existing, false);
+ m_records.remove(index);
+ break;
+ }
+ }
+ }
+
+ // An origin shows a bounded number of notifications at a time; the oldest makes room for a new one.
+ size_t live_for_origin = 0;
+ for (auto const& existing : m_records) {
+ if (existing.origin.is_same_origin(origin))
+ ++live_for_origin;
+ }
+ while (live_for_origin >= max_live_notifications_per_origin) {
+ for (size_t index = 0; index < m_records.size(); ++index) {
+ if (m_records[index].origin.is_same_origin(origin)) {
+ close_record_from_browser(index);
+ --live_for_origin;
+ break;
+ }
+ }
+ }
+
+ auto id = record.id;
+ m_records.append(move(record));
+ backend().show(PlatformNotification {
+ .id = id,
+ .replaces = replaces,
+ .data = move(data),
+ .attribution = attribution_for_origin(origin),
+ .icon = move(icon),
+ });
+}
+
+void NotificationManager::close(WebContentPage& page, u64 page_notification_id)
+{
+ if (auto index = index_of(page, page_notification_id); index.has_value())
+ backend().close(m_records[*index].id);
+}
+
+void NotificationManager::click_handled(WebContentPage& page, u64 page_notification_id, bool into_focus)
+{
+ if (!into_focus)
+ return;
+ if (index_of(page, page_notification_id).has_value())
+ Application::the().focus_page_from_notification(page);
+}
+
+void NotificationManager::page_did_close(WebContentPage& page)
+{
+ // The notifications stay up; only their way back to the page is gone.
+ for (auto& record : m_records) {
+ if (record.page == &page)
+ record.page = nullptr;
+ }
+}
+
+Optional<u64> NotificationManager::id_for(WebContentPage const& page, u64 page_notification_id) const
+{
+ if (auto index = index_of(page, page_notification_id); index.has_value())
+ return m_records[*index].id;
+ return {};
+}
+
+Optional<size_t> NotificationManager::index_of(u64 id) const
+{
+ for (size_t index = 0; index < m_records.size(); ++index) {
+ if (m_records[index].id == id)
+ return index;
+ }
+ return {};
+}
+
+Optional<size_t> NotificationManager::index_of(WebContentPage const& page, u64 page_notification_id) const
+{
+ for (size_t index = 0; index < m_records.size(); ++index) {
+ auto const& record = m_records[index];
+ if (record.page == &page && record.page_notification_id == page_notification_id)
+ return index;
+ }
+ return {};
+}
+
+// Withdraws a notification the browser itself decided to drop, and tells its page.
+void NotificationManager::close_record_from_browser(size_t index)
+{
+ auto record = m_records.take(index);
+ backend().close(record.id);
+ report_closed(record, false);
+}
+
+void NotificationManager::report_closed(Record const& record, bool by_user)
+{
+ if (record.page && record.page->is_open())
+ record.page->async_notification_closed(record.page_notification_id, by_user);
+}
+
+void NotificationManager::backend_did_show(u64 id)
+{
+ auto index = index_of(id);
+ if (!index.has_value())
+ return;
+ auto const& record = m_records[*index];
+ if (record.page && record.page->is_open())
+ record.page->async_notification_shown(record.page_notification_id);
+}
+
+void NotificationManager::backend_did_fail(u64 id)
+{
+ auto index = index_of(id);
+ if (!index.has_value())
+ return;
+ auto record = m_records.take(*index);
+ if (record.page && record.page->is_open())
+ record.page->async_notification_show_failed(record.page_notification_id);
+}
+
+void NotificationManager::backend_did_click(u64 id)
+{
+ auto index = index_of(id);
+ if (!index.has_value())
+ return;
+ auto const& record = m_records[*index];
+ if (record.page && record.page->is_open()) {
+ 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 traversable = record.traversable;
+ m_records.remove(*index);
+ Application::the().open_url_in_new_tab_from_notification(origin_url, traversable.ptr());
+}
+
+void NotificationManager::backend_did_close(u64 id, NotificationBackend::ByUser by_user)
+{
+ auto index = index_of(id);
+ if (!index.has_value())
+ return;
+ auto record = m_records.take(*index);
+ report_closed(record, by_user == NotificationBackend::ByUser::Yes);
+}
+
+}
diff --git a/Libraries/LibWebView/NotificationManager.h b/Libraries/LibWebView/NotificationManager.h
new file mode 100644
index 00000000000..be82ea3352b
--- /dev/null
+++ b/Libraries/LibWebView/NotificationManager.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullOwnPtr.h>
+#include <AK/Optional.h>
+#include <AK/RefPtr.h>
+#include <AK/Utf16String.h>
+#include <AK/Vector.h>
+#include <AK/WeakPtr.h>
+#include <AK/kmalloc.h>
+#include <LibGfx/Bitmap.h>
+#include <LibURL/Origin.h>
+#include <LibURL/URL.h>
+#include <LibWebCommon/NotificationsAPI/NotificationData.h>
+#include <LibWebView/Forward.h>
+#include <LibWebView/NotificationBackend.h>
+
+namespace WebView {
+
+// Owns the notifications the browser has shown, each with the page that asked for it and the origin it belongs to,
+// and drives the platform backend. A notification outlives its page: a click on one whose page is gone opens the
+// origin in a new tab. Same origin and same tag replace an earlier notification, and an origin keeps at most
+// max_live_notifications_per_origin at a time.
+class WEBVIEW_API NotificationManager {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ static NotificationManager& the();
+
+ static constexpr size_t max_live_notifications_per_origin = 8;
+
+ void show(WebContentPage&, u64 page_notification_id, Web::NotificationsAPI::NotificationData, RefPtr<Gfx::Bitmap const> icon);
+ void close(WebContentPage&, u64 page_notification_id);
+ void click_handled(WebContentPage&, u64 page_notification_id, bool into_focus);
+ void page_did_close(WebContentPage&);
+
+ NotificationBackend& backend();
+ void set_backend(NonnullOwnPtr<NotificationBackend>);
+
+ // The manager's id of a page's notification, for tests that act on it through the backend.
+ Optional<u64> id_for(WebContentPage const&, u64 page_notification_id) const;
+
+private:
+ struct Record {
+ u64 id { 0 };
+ // Null once the page has closed; the record then only serves a later click.
+ RefPtr<WebContentPage> page;
+ u64 page_notification_id { 0 };
+ URL::Origin origin;
+ // What a click opens once the page is gone.
+ URL::URL origin_url;
+ WeakPtr<CanonicalTraversable> traversable;
+ Utf16String tag;
+ };
+
+ NotificationManager() = default;
+
+ void install_backend_callbacks();
+ Optional<size_t> index_of(u64 id) const;
+ Optional<size_t> index_of(WebContentPage const&, u64 page_notification_id) const;
+ void close_record_from_browser(size_t index);
+ void report_closed(Record const&, bool by_user);
+
+ void backend_did_show(u64 id);
+ void backend_did_fail(u64 id);
+ void backend_did_click(u64 id);
+ void backend_did_close(u64 id, NotificationBackend::ByUser);
+
+ OwnPtr<NotificationBackend> m_backend;
+ Vector<Record> m_records;
+ u64 m_next_id { 1 };
+};
+
+}
diff --git a/Libraries/LibWebView/RecordingNotificationBackend.cpp b/Libraries/LibWebView/RecordingNotificationBackend.cpp
new file mode 100644
index 00000000000..4a1a264c6ba
--- /dev/null
+++ b/Libraries/LibWebView/RecordingNotificationBackend.cpp
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWebView/RecordingNotificationBackend.h>
+
+namespace WebView {
+
+void RecordingNotificationBackend::show(PlatformNotification const& notification)
+{
+ if (exchange(fail_next_show, false)) {
+ if (on_failed)
+ on_failed(notification.id);
+ return;
+ }
+
+ // A replacement takes the replaced notification's place, as a platform that replaces in place would show it.
+ if (notification.replaces.has_value()) {
+ if (auto index = index_of(*notification.replaces); index.has_value()) {
+ m_shown[*index] = notification;
+ if (on_shown)
+ on_shown(notification.id);
+ return;
+ }
+ }
+
+ m_shown.append(notification);
+ if (on_shown)
+ on_shown(notification.id);
+}
+
+void RecordingNotificationBackend::close(u64 id)
+{
+ if (auto index = index_of(id); index.has_value())
+ m_shown.remove(*index);
+ m_closed.append(id);
+ if (on_closed)
+ on_closed(id, ByUser::No);
+}
+
+void RecordingNotificationBackend::click(u64 id)
+{
+ if (!index_of(id).has_value())
+ return;
+ if (on_clicked)
+ on_clicked(id);
+}
+
+void RecordingNotificationBackend::dismiss(u64 id)
+{
+ auto index = index_of(id);
+ if (!index.has_value())
+ return;
+ m_shown.remove(*index);
+ m_closed.append(id);
+ if (on_closed)
+ on_closed(id, ByUser::Yes);
+}
+
+Optional<size_t> RecordingNotificationBackend::index_of(u64 id) const
+{
+ for (size_t index = 0; index < m_shown.size(); ++index) {
+ if (m_shown[index].id == id)
+ return index;
+ }
+ return {};
+}
+
+}
diff --git a/Libraries/LibWebView/RecordingNotificationBackend.h b/Libraries/LibWebView/RecordingNotificationBackend.h
new file mode 100644
index 00000000000..3423d3ca084
--- /dev/null
+++ b/Libraries/LibWebView/RecordingNotificationBackend.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Vector.h>
+#include <LibWebView/NotificationBackend.h>
+
+namespace WebView {
+
+// A backend that keeps the notifications it was asked to show, for tests to read and to act on as the user would.
+class WEBVIEW_API RecordingNotificationBackend final : public NotificationBackend {
+public:
+ virtual void show(PlatformNotification const&) override;
+ virtual void close(u64 id) override;
+
+ // What the user would do with a shown notification.
+ void click(u64 id);
+ void dismiss(u64 id);
+
+ Vector<PlatformNotification> const& shown() const { return m_shown; }
+ Vector<u64> const& closed() const { return m_closed; }
+
+ // When set, the next show() fails instead, as a platform that refuses the notification would.
+ bool fail_next_show { false };
+
+private:
+ Optional<size_t> index_of(u64 id) const;
+
+ Vector<PlatformNotification> m_shown;
+ Vector<u64> m_closed;
+};
+
+}
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 7f8f456833a..eb21727cacb 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -30,6 +30,7 @@
#include <LibWebView/GamepadManager.h>
#include <LibWebView/HistoryStore.h>
#include <LibWebView/NavigationLoader.h>
+#include <LibWebView/NotificationManager.h>
#include <LibWebView/PermissionManager.h>
#include <LibWebView/PictureInPictureManager.h>
#include <LibWebView/RequestServerManager.h>
@@ -644,6 +645,7 @@ void WebContentPage::fail_renderer_owned_downloads()
void WebContentPage::close()
{
PictureInPictureManager::the().close_window(*this);
+ NotificationManager::the().page_did_close(*this);
m_is_open = false;
// NB: Nothing finishes the input this page held, including a page discarded while its process hosts other pages.
if (m_traversable) {
@@ -1860,6 +1862,24 @@ Web::Permissions::PermissionSnapshot WebContentPage::advance_permission_clock_fo
return manager.snapshot_for(client().permission_origins());
}
+void WebContentPage::did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon)
+{
+ RefPtr<Gfx::Bitmap const> icon_bitmap;
+ if (icon.has_value() && icon->is_valid())
+ icon_bitmap = icon->bitmap();
+ NotificationManager::the().show(*this, notification_id, move(data), move(icon_bitmap));
+}
+
+void WebContentPage::did_request_close_notification(u64 notification_id)
+{
+ NotificationManager::the().close(*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_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.h
index 1bd3e14897c..e5b0f5aabba 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -282,6 +282,9 @@ private:
virtual void did_start_geolocation_position_watch(u64 request_id, Web::HTML::CrossProcessId document_id) override;
virtual void did_stop_geolocation_position_watch(u64 request_id) override;
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_handle_notification_click(u64 notification_id, bool into_focus) 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/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 302836aa55d..f8421655e16 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3285,6 +3285,30 @@ void ConnectionFromClient::permission_request_completed(Web::PageId page_id, u64
page->permission_request_completed(request_id, state);
}
+void ConnectionFromClient::notification_shown(Web::PageId page_id, u64 notification_id)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().notification_shown(notification_id);
+}
+
+void ConnectionFromClient::notification_show_failed(Web::PageId page_id, u64 notification_id)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().notification_show_failed(notification_id);
+}
+
+void ConnectionFromClient::notification_clicked(Web::PageId page_id, u64 notification_id)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().notification_clicked(notification_id);
+}
+
+void ConnectionFromClient::notification_closed(Web::PageId page_id, u64 notification_id, bool by_user)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().notification_closed(notification_id, by_user);
+}
+
void ConnectionFromClient::set_has_focus(Web::PageId page_id, bool has_focus)
{
if (auto page = this->page(page_id); page.has_value())
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index d71cd846f21..ec0cefa1766 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -259,6 +259,10 @@ private:
virtual void set_geolocation_emulated_position(Web::PageId page_id, WebView::GeolocationPositionData, Optional<u16> error_code) override;
virtual void geolocation_position_response(Web::PageId page_id, u64 request_id, WebView::GeolocationPositionData, Optional<u16> error_code) override;
virtual void permission_request_completed(Web::PageId page_id, u64 request_id, Web::Permissions::EffectiveState state) override;
+ virtual void notification_shown(Web::PageId page_id, u64 notification_id) override;
+ virtual void notification_show_failed(Web::PageId page_id, u64 notification_id) override;
+ virtual void notification_clicked(Web::PageId page_id, u64 notification_id) override;
+ virtual void notification_closed(Web::PageId page_id, u64 notification_id, bool by_user) override;
virtual void set_has_focus(Web::PageId page_id, bool) override;
virtual void set_focused_navigable(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) override;
virtual void consume_user_activation(Web::PageId page_id, Web::HTML::UserActivationConsumption) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 33af9678ab2..50af2c0b81b 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1307,6 +1307,24 @@ void PageClient::page_did_request_permission(u64 request_id, Web::HTML::CrossPro
client().async_did_request_permission(m_id, request_id, requesting_document_id, name);
}
+void PageClient::page_did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData const& data, RefPtr<Gfx::Bitmap const> icon)
+{
+ Optional<Gfx::ShareableBitmap> shareable_icon;
+ if (icon)
+ shareable_icon = icon->to_shareable_bitmap();
+ client().async_did_request_show_notification(m_id, notification_id, data, move(shareable_icon));
+}
+
+void PageClient::page_did_request_close_notification(u64 notification_id)
+{
+ client().async_did_request_close_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);
+}
+
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.h
index b989bad0b03..b68d2448170 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -363,6 +363,9 @@ private:
virtual void page_did_set_next_permission_prompt_outcome_for_testing(String const& name, String const& outcome, bool held) override;
virtual void page_did_release_held_permission_prompt_for_testing(u32 waiters) override;
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_handle_notification_click(u64 notification_id, bool into_focus) 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.ipc
index 7e8fcb5508a..6806704516b 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -38,6 +38,7 @@
#include <LibWebCommon/HTML/PreparedNavigationDescriptor.h>
#include <LibWebCommon/HTML/ReplicatedNavigableState.h>
#include <LibWebCommon/HTML/SameDocumentNavigationEntry.h>
+#include <LibWebCommon/NotificationsAPI/NotificationData.h>
#include <LibWebCommon/HTML/SandboxingFlagSet.h>
#include <LibWebCommon/HTML/Scripting/ScriptRegistryTypes.h>
#include <LibWebCommon/HTML/SelectItem.h>
@@ -241,6 +242,9 @@ endpoint WebContentClient routed by Web::PageId page_id
did_stop_geolocation_position_watch(Web::PageId page_id, u64 request_id) =|
did_request_permission(Web::PageId page_id, u64 request_id, Web::HTML::CrossProcessId document_id, Utf16String name) =|
[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_handle_notification_click(Web::PageId page_id, u64 notification_id, bool into_focus) =|
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/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index 383defc313b..211cdebe9b4 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -267,6 +267,10 @@ endpoint WebContentServer routed by Web::PageId page_id
set_geolocation_emulated_position(Web::PageId page_id, WebView::GeolocationPositionData position, Optional<u16> error_code) =|
geolocation_position_response(Web::PageId page_id, u64 request_id, WebView::GeolocationPositionData position, Optional<u16> error_code) =|
permission_request_completed(Web::PageId page_id, u64 request_id, Web::Permissions::EffectiveState state) =|
+ notification_shown(Web::PageId page_id, u64 notification_id) =|
+ notification_show_failed(Web::PageId page_id, u64 notification_id) =|
+ notification_clicked(Web::PageId page_id, u64 notification_id) =|
+ notification_closed(Web::PageId page_id, u64 notification_id, bool by_user) =|
set_has_focus(Web::PageId page_id, bool has_focus) =|
set_focused_navigable(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) =|
consume_user_activation(Web::PageId page_id, Web::HTML::UserActivationConsumption consumption) =|
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 46438492974..4ef5a71c0a7 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -175,6 +175,7 @@ set(TEST_SOURCES
TestHistoryStore.cpp
TestHSTSStore.cpp
TestNavigationLoader.cpp
+ TestNotificationAttribution.cpp
TestOmnibox.cpp
TestPermissionManager.cpp
TestPictureInPictureWindow.cpp
diff --git a/Tests/LibWebView/TestNotificationAttribution.cpp b/Tests/LibWebView/TestNotificationAttribution.cpp
new file mode 100644
index 00000000000..ef416918e38
--- /dev/null
+++ b/Tests/LibWebView/TestNotificationAttribution.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+#include <LibURL/Origin.h>
+#include <LibURL/Parser.h>
+#include <LibWebView/NotificationAttribution.h>
+
+static URL::Origin origin_of(StringView url)
+{
+ return URL::Parser::basic_parse(url)->origin();
+}
+
+TEST_CASE(https_origin_shows_its_host_without_the_scheme)
+{
+ EXPECT_EQ(WebView::attribution_for_origin(origin_of("https://example.com/mail/inbox"sv)), "example.com"_string);
+}
+
+TEST_CASE(http_origin_shows_its_host_without_the_scheme)
+{
+ EXPECT_EQ(WebView::attribution_for_origin(origin_of("http://example.com/"sv)), "example.com"_string);
+}
+
+TEST_CASE(non_default_port_stays)
+{
+ EXPECT_EQ(WebView::attribution_for_origin(origin_of("http://example.com:8080/"sv)), "example.com:8080"_string);
+}
+
+TEST_CASE(ipv6_host_keeps_its_brackets_and_port)
+{
+ EXPECT_EQ(WebView::attribution_for_origin(origin_of("https://[::1]:8443/"sv)), "[::1]:8443"_string);
+}
+
+TEST_CASE(other_schemes_keep_their_scheme)
+{
+ EXPECT_EQ(WebView::attribution_for_origin(origin_of("ftp://files.example.com/"sv)), "ftp://files.example.com"_string);
+}
+
+TEST_CASE(opaque_origin_is_this_page)
+{
+ EXPECT_EQ(WebView::attribution_for_origin(URL::Origin::create_opaque()), "this page"_string);
+}