Skip to content

Truly independent web browser

LibWeb: Finish the notification create steps

sideshowbarker647bbfde48a7
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.cpp b/Libraries/LibWeb/NotificationsAPI/Notification.cpp
index e5302084edc..70f8da087ef 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.cpp
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.cpp
@@ -46,6 +46,7 @@ WebIDL::ExceptionOr<NotificationOptions> Notification::options_from_bindings(JS:
.image = options.image,
.icon = options.icon,
.badge = options.badge,
+ .vibrate = options.vibrate,
.timestamp = options.timestamp,
.renotify = options.renotify,
.silent = options.silent,
@@ -79,7 +80,9 @@ WebIDL::ExceptionOr<ConceptNotification> Notification::create_a_notification(
// 1. Let notification be a new notification.
ConceptNotification notification;
- // FIXME: 2. If options["silent"] is true and options["vibrate"] exists, then throw a TypeError.
+ // 2. If options["silent"] is true and options["vibrate"] exists, then throw a TypeError.
+ if (options.silent == true && options.vibrate.has_value())
+ return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "options[\"vibrate\"] cannot be set when options[\"silent\"] is true."_utf16 };
// 3. If options["renotify"] is true and options["tag"] is the empty string, then throw a TypeError.
if (options.renotify && options.tag.is_empty())
@@ -158,7 +161,8 @@ WebIDL::ExceptionOr<ConceptNotification> Notification::create_a_notification(
// 21. For each entry in options["actions"], up to the maximum number of actions supported (skip any excess entries):
for (auto const& entry : options.actions) {
- // FIXME: stop the loop at the max number of actions supported
+ if (notification.actions.size() >= max_actions())
+ break;
// 1. Let action be a new notification action.
ConceptNotification::Action action;
@@ -297,7 +301,8 @@ Vector<NotificationAction> Notification::actions() const
frozen_actions.append(action);
}
- // FIXME: 3. Return the result of create a frozen array from frozenActions.
+ // 3. Return the result of create a frozen array from frozenActions.
+ // NB: The bindings freeze the array they build from this sequence.
return frozen_actions;
}
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.h b/Libraries/LibWeb/NotificationsAPI/Notification.h
index a6795b7176a..dd1a983be66 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/WebIDL/Types.h>
namespace Web::NotificationsAPI {
@@ -37,6 +38,8 @@ struct NotificationOptions {
Optional<Utf16String> image;
Optional<Utf16String> icon;
Optional<Utf16String> badge;
+ // FIXME: Validate and normalize the pattern (create step 15); only its presence matters until then.
+ Optional<Variant<WebIDL::UnsignedLong, Vector<WebIDL::UnsignedLong>>> vibrate;
Optional<HighResolutionTime::EpochTimeStamp> timestamp;
bool renotify { false };
Optional<bool> silent;
@@ -110,12 +113,10 @@ public:
static Bindings::NotificationPermission permission();
- static unsigned long max_actions()
- {
- // FIXME: Change the number of max_actions supported when actions will actually be supported
- // It seems like Chrome is 2, Firefox is undefined, Safari is undefined
- return 0;
- }
+ // 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.
+ static unsigned long max_actions() { return 2; }
String const& title() const { return m_notification.title; }
NotificationDirection direction() const { return m_notification.direction; }
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.idl b/Libraries/LibWeb/NotificationsAPI/Notification.idl
index 4598f96c090..ae20149eeb6 100644
--- a/Libraries/LibWeb/NotificationsAPI/Notification.idl
+++ b/Libraries/LibWeb/NotificationsAPI/Notification.idl
@@ -42,7 +42,7 @@ dictionary NotificationOptions {
USVString image;
USVString icon;
USVString badge;
- // FIXME: VibratePattern vibrate;
+ VibratePattern vibrate;
EpochTimeStamp timestamp;
boolean renotify = false;
boolean? silent = null;
@@ -51,6 +51,9 @@ dictionary NotificationOptions {
sequence<NotificationAction> actions = [];
};
+// https://w3c.github.io/vibration/#idl-def-vibratepattern
+typedef (unsigned long or sequence<unsigned long>) VibratePattern;
+
enum NotificationPermission {
"default",
"denied",
diff --git a/Tests/LibWeb/Text/expected/notifications/constructor-create-steps.txt b/Tests/LibWeb/Text/expected/notifications/constructor-create-steps.txt
new file mode 100644
index 00000000000..eb83f25f7e2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/constructor-create-steps.txt
@@ -0,0 +1,6 @@
+maxActions: 2
+silent with vibrate: TypeError
+silent alone: created, actions frozen: true
+vibrate alone: created, actions frozen: true
+renotify without tag: TypeError
+one action: TypeError
diff --git a/Tests/LibWeb/Text/input/notifications/constructor-create-steps.html b/Tests/LibWeb/Text/input/notifications/constructor-create-steps.html
new file mode 100644
index 00000000000..d51fc1fd8c9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/constructor-create-steps.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ println(`maxActions: ${Notification.maxActions}`);
+
+ const attempt = (label, options) => {
+ try {
+ const notification = new Notification("title", options);
+ println(`${label}: created, actions frozen: ${Object.isFrozen(notification.actions)}`);
+ } catch (e) {
+ println(`${label}: ${e.name}`);
+ }
+ };
+ attempt("silent with vibrate", { silent: true, vibrate: [100] });
+ attempt("silent alone", { silent: true });
+ attempt("vibrate alone", { vibrate: [100] });
+ attempt("renotify without tag", { renotify: true });
+ attempt("one action", { actions: [{ action: "a", title: "A" }] });
+ });
+</script>