Skip to content

Truly independent web browser

LibWeb: Implement Notification.permission over the permission store

sideshowbarker2399639d2146
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index e3b4661982a..061efeb618c 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -757,6 +757,7 @@ set(SOURCES
NavigationTiming/PerformanceNavigationTiming.cpp
NavigationTiming/PerformanceTiming.cpp
NotificationsAPI/Notification.cpp
+ NotificationsAPI/NotificationPermission.cpp
Page/AutoScrollHandler.cpp
Page/DragAndDropEventHandler.cpp
Page/ElementResizeAction.cpp
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.cpp b/Libraries/LibWeb/NotificationsAPI/Notification.cpp
index 69eb36345d1..e5302084edc 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.cpp
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.cpp
@@ -12,6 +12,7 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/NotificationsAPI/Notification.h>
+#include <LibWeb/NotificationsAPI/NotificationPermission.h>
#include <LibWeb/ServiceWorker/ServiceWorkerGlobalScope.h>
namespace Web::NotificationsAPI {
@@ -243,6 +244,27 @@ WebIDL::ExceptionOr<GC::Ref<Notification>> Notification::create_with_global_scop
return this_notification;
}
+// https://notifications.spec.whatwg.org/#dom-notification-permission
+Bindings::NotificationPermission Notification::permission()
+{
+ // The static permission getter steps are to return the result of getting the notifications permission state.
+ // https://notifications.spec.whatwg.org/#permissions-integration
+ // 1. Let permissionState be the result of getting the current permission state with "notifications".
+ auto permission_state = get_the_notifications_permission_state(HTML::current_settings_object());
+
+ // 2. If permissionState is "prompt", then return "default".
+ // 3. Return permissionState.
+ switch (permission_state) {
+ case PermissionsAPI::PermissionState::Prompt:
+ return Bindings::NotificationPermission::Default;
+ case PermissionsAPI::PermissionState::Granted:
+ return Bindings::NotificationPermission::Granted;
+ case PermissionsAPI::PermissionState::Denied:
+ return Bindings::NotificationPermission::Denied;
+ }
+ VERIFY_NOT_REACHED();
+}
+
// https://notifications.spec.whatwg.org/#dom-notification-actions
Vector<NotificationAction> Notification::actions() const
{
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.h b/Libraries/LibWeb/NotificationsAPI/Notification.h
index c6808b83ca0..a6795b7176a 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.h
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.h
@@ -108,6 +108,8 @@ public:
URL::URL base_url,
HighResolutionTime::EpochTimeStamp fallback_timestamp);
+ static Bindings::NotificationPermission permission();
+
static unsigned long max_actions()
{
// FIXME: Change the number of max_actions supported when actions will actually be supported
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.idl b/Libraries/LibWeb/NotificationsAPI/Notification.idl
index 06d5462dc49..4598f96c090 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.idl
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.idl
@@ -3,7 +3,7 @@
interface Notification : EventTarget {
constructor(DOMString title, optional NotificationOptions options = {});
- [FIXME] static readonly attribute NotificationPermission permission;
+ static readonly attribute NotificationPermission permission;
[FIXME, Exposed=Window] static Promise<NotificationPermission> requestPermission(optional NotificationPermissionCallback deprecatedCallback);
static readonly attribute unsigned long maxActions;
diff --git a/Libraries/LibWeb/NotificationsAPI/NotificationPermission.cpp b/Libraries/LibWeb/NotificationsAPI/NotificationPermission.cpp
new file mode 100644
index 00000000000..ebe5b7aa98a
--- /dev/null
+++ b/Libraries/LibWeb/NotificationsAPI/NotificationPermission.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/NotificationsAPI/NotificationPermission.h>
+#include <LibWeb/PermissionsAPI/Permissions.h>
+
+namespace Web::NotificationsAPI {
+
+// https://notifications.spec.whatwg.org/#permissions-integration
+PermissionsAPI::PermissionState get_the_notifications_permission_state(HTML::EnvironmentSettingsObject& settings)
+{
+ // To get the notifications permission state, run these steps:
+
+ // 1. Let permissionState be the result of getting the current permission state with "notifications".
+ return PermissionsAPI::permission_state({ "notifications"_utf16 }, settings);
+
+ // NB: Steps 2 and 3, which turn "prompt" into "default", belong to the permission getter: Every other caller wants
+ // the raw state.
+}
+
+}
diff --git a/Libraries/LibWeb/NotificationsAPI/NotificationPermission.h b/Libraries/LibWeb/NotificationsAPI/NotificationPermission.h
new file mode 100644
index 00000000000..378ed04f5c9
--- /dev/null
+++ b/Libraries/LibWeb/NotificationsAPI/NotificationPermission.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <LibWeb/Export.h>
+#include <LibWeb/Forward.h>
+#include <LibWeb/PermissionsAPI/Permissions.h>
+
+namespace Web::NotificationsAPI {
+
+// https://notifications.spec.whatwg.org/#permissions-integration
+WEB_API PermissionsAPI::PermissionState get_the_notifications_permission_state(HTML::EnvironmentSettingsObject&);
+
+}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 68ce0d997b7..cf05a8c7c97 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -10,6 +10,8 @@ Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-c
Text/input/PermissionsAPI/
Text/input/wpt-import/geolocation/
Text/input/wpt-import/permissions/
+; A stored notifications grant needs a site origin.
+Text/input/notifications/
; Their harness scripts are served from the WPT root, which a file: page cannot reach.
Crash/wpt-import/permissions/
; Render-blocking tests need delayed HTTP responses and module imports.
diff --git a/Tests/LibWeb/Text/expected/notifications/permission-getter.txt b/Tests/LibWeb/Text/expected/notifications/permission-getter.txt
new file mode 100644
index 00000000000..64feaf32f49
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/permission-getter.txt
@@ -0,0 +1,3 @@
+granted: granted / granted
+denied: denied / denied
+prompt: default / prompt
diff --git a/Tests/LibWeb/Text/expected/notifications/worker-permission-reads-store.txt b/Tests/LibWeb/Text/expected/notifications/worker-permission-reads-store.txt
new file mode 100644
index 00000000000..21dd43061e8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/worker-permission-reads-store.txt
@@ -0,0 +1 @@
+worker: granted
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 38600dadd44..6aca35cede3 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,8 +2,8 @@ Harness status: OK
Found 67 tests
-49 Pass
-18 Fail
+50 Pass
+17 Fail
Fail idl_test setup
Pass idl_test validation
Pass Partial interface ServiceWorkerRegistration: original interface defined
@@ -17,7 +17,7 @@ Pass Notification interface object name
Pass Notification interface: existence and properties of interface prototype object
Pass Notification interface: existence and properties of interface prototype object's "constructor" property
Pass Notification interface: existence and properties of interface prototype object's @@unscopables property
-Fail Notification interface: attribute permission
+Pass Notification interface: attribute permission
Fail Notification interface: operation requestPermission(optional NotificationPermissionCallback)
Pass Notification interface: attribute maxActions
Fail Notification interface: attribute onclick
diff --git a/Tests/LibWeb/Text/input/notifications/permission-getter.html b/Tests/LibWeb/Text/input/notifications/permission-getter.html
new file mode 100644
index 00000000000..db217c7da28
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/permission-getter.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Notification.permission reads the same store as the Permissions API, with "prompt" reported as "default".
+ asyncTest(async done => {
+ for (const state of ["granted", "denied", "prompt"]) {
+ await internals.setPermission("notifications", state);
+ const queried = (await navigator.permissions.query({ name: "notifications" })).state;
+ println(`${state}: ${Notification.permission} / ${queried}`);
+ }
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/worker-permission-reads-store.html b/Tests/LibWeb/Text/input/notifications/worker-permission-reads-store.html
new file mode 100644
index 00000000000..f83f18ce9fd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/worker-permission-reads-store.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script type="worker">
+ postMessage(Notification.permission);
+</script>
+<script src="../include.js"></script>
+<script>
+ // A dedicated worker reads the permission its page's site was granted.
+ 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>