Truly independent web browser
LibWebView: Close notifications the platform hasn’t acknowledged yet
sideshowbarker6eed044510e8
diff --git a/Libraries/LibWebView/NotificationBackendFreedesktop.cpp b/Libraries/LibWebView/NotificationBackendFreedesktop.cppindex 07ddb087437..3770e4b1452 100644--- a/Libraries/LibWebView/NotificationBackendFreedesktop.cpp+++ b/Libraries/LibWebView/NotificationBackendFreedesktop.cpp@@ -197,6 +197,9 @@ void NotificationBackendFreedesktop::show(PlatformNotification const& notification_failed(notification.id);return;}+ // A replaced notification that never reached the daemon is dropped here; one that did is replaced by send_notify().+ if (notification.replaces.has_value())+ m_waiting_for_capabilities.remove_first_matching([&](auto const& waiting) { return waiting.id == *notification.replaces; });if (!m_capabilities.has_value()) {m_waiting_for_capabilities.append(notification);return;@@ -262,6 +265,10 @@ void NotificationBackendFreedesktop::send_notify(PlatformNotification const& notif (auto daemon_id = m_daemon_id_for.take(*notification.replaces); daemon_id.has_value()) {replaces_daemon_id = *daemon_id;m_id_for_daemon_id.remove(replaces_daemon_id);+ } else if (m_awaiting_reply.contains(*notification.replaces)) {+ // The one being replaced hasn't been acknowledged yet, so it can't be replaced in place: It comes down+ // as soon as it can.+ m_close_when_acknowledged.set(*notification.replaces);}}auto arguments = build_notify_arguments(notification, *m_capabilities, replaces_daemon_id);@@ -297,6 +304,7 @@ void NotificationBackendFreedesktop::send_notify(PlatformNotification const& notreturn;}dbus_message_unref(message);+ m_awaiting_reply.set(notification.id);struct Context {AK_ALLOC_WITH_KMALLOC;@@ -314,16 +322,35 @@ void NotificationBackendFreedesktop::send_notify(PlatformNotification const& notdbus_pending_call_unref(pending);}+void NotificationBackendFreedesktop::send_close_notification(u32 daemon_id)+{+ auto* message = dbus_message_new_method_call(bus_name, object_path, interface_name, "CloseNotification");+ dbus_message_append_args(message, DBUS_TYPE_UINT32, &daemon_id, DBUS_TYPE_INVALID);+ dbus_message_set_no_reply(message, TRUE);+ dbus_connection_send(m_connection, message, nullptr);+ dbus_message_unref(message);+}+void NotificationBackendFreedesktop::did_receive_notify_reply(u64 id, DBusMessage* reply){+ m_awaiting_reply.remove(id);+ bool closed_meanwhile = m_close_when_acknowledged.remove(id);+u32 daemon_id = 0;if (!reply || dbus_message_get_type(reply) != DBUS_MESSAGE_TYPE_METHOD_RETURN || !dbus_message_get_args(reply, nullptr, DBUS_TYPE_UINT32, &daemon_id, DBUS_TYPE_INVALID)) {if (reply && dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)dbgln("Notifications: The notification daemon refused the request: {}", dbus_message_get_error_name(reply));- if (on_failed)+ if (!closed_meanwhile && on_failed)on_failed(id);return;}++ // Closed on this side while the daemon was still answering: It comes down now, and its close was reported then.+ if (closed_meanwhile) {+ send_close_notification(daemon_id);+ return;+ }+m_daemon_id_for.set(id, daemon_id);m_id_for_daemon_id.set(daemon_id, id);if (on_shown)@@ -333,14 +360,18 @@ void NotificationBackendFreedesktop::did_receive_notify_reply(u64 id, DBusMessagvoid NotificationBackendFreedesktop::close(u64 id){auto daemon_id = m_daemon_id_for.take(id);- if (!daemon_id.has_value())- return;- m_id_for_daemon_id.remove(*daemon_id);- auto* message = dbus_message_new_method_call(bus_name, object_path, interface_name, "CloseNotification");- dbus_message_append_args(message, DBUS_TYPE_UINT32, &*daemon_id, DBUS_TYPE_INVALID);- dbus_message_set_no_reply(message, TRUE);- dbus_connection_send(m_connection, message, nullptr);- dbus_message_unref(message);+ if (daemon_id.has_value()) {+ m_id_for_daemon_id.remove(*daemon_id);+ send_close_notification(*daemon_id);+ } else if (m_awaiting_reply.contains(id)) {+ // The daemon hasn't answered the Notify yet; its answer takes the notification down.+ m_close_when_acknowledged.set(id);+ } else {+ // Not sent yet (still waiting for the capabilities), or unknown: Nothing to take down.+ bool was_waiting = m_waiting_for_capabilities.remove_first_matching([&](auto const& waiting) { return waiting.id == id; });+ if (!was_waiting)+ return;+ }// The daemon's NotificationClosed for this id finds no mapping any more, so the close is reported once, here.if (on_closed)on_closed(id, ByUser::No);diff --git a/Libraries/LibWebView/NotificationBackendFreedesktop.h b/Libraries/LibWebView/NotificationBackendFreedesktop.hindex 7c457d68718..e670c1acf2c 100644--- a/Libraries/LibWebView/NotificationBackendFreedesktop.h+++ b/Libraries/LibWebView/NotificationBackendFreedesktop.h@@ -7,6 +7,7 @@#pragma once#include <AK/HashMap.h>+#include <AK/HashTable.h>#include <AK/NonnullRefPtr.h>#include <AK/Optional.h>#include <AK/Vector.h>@@ -38,6 +39,7 @@ private:void request_capabilities();void did_receive_capabilities(DBusMessage* reply);void send_notify(PlatformNotification const&);+ void send_close_notification(u32 daemon_id);void did_receive_notify_reply(u64 id, DBusMessage* reply);void did_receive_signal(DBusMessage*);void dispatch();@@ -59,6 +61,11 @@ private:bool m_capabilities_requested { false };Vector<PlatformNotification> m_waiting_for_capabilities;+ // Notifications whose Notify the daemon hasn't answered yet, and among them the ones already closed on this side:+ // Those are taken down as soon as the daemon's id for them arrives.+ HashTable<u64> m_awaiting_reply;+ HashTable<u64> m_close_when_acknowledged;+struct Watch {DBusWatch* watch { nullptr };RefPtr<Core::Notifier> notifier;diff --git a/Libraries/LibWebView/NotificationBackendMacOS.h b/Libraries/LibWebView/NotificationBackendMacOS.hindex f0b7da6a0b0..b976d73c492 100644--- a/Libraries/LibWebView/NotificationBackendMacOS.h+++ b/Libraries/LibWebView/NotificationBackendMacOS.h@@ -7,6 +7,7 @@#pragma once#include <AK/HashMap.h>+#include <AK/HashTable.h>#include <AK/NonnullOwnPtr.h>#include <AK/String.h>#include <AK/kmalloc.h>@@ -57,6 +58,12 @@ private:OwnPtr<Detail::NotificationCenterBridge> m_bridge;bool m_bundled { false };++ // Identifiers carry this launch's token, so a click on a notification left over from an earlier run of the browser+ // isn't delivered to whatever page holds that number now.+ String m_launch_token;+ // Closed by the browser before the center was asked to show them (authorization still pending).+ HashTable<u64> m_closed_before_shown;};}diff --git a/Libraries/LibWebView/NotificationBackendMacOS.mm b/Libraries/LibWebView/NotificationBackendMacOS.mmindex 9b7255604da..f31e66bcbbd 100644--- a/Libraries/LibWebView/NotificationBackendMacOS.mm+++ b/Libraries/LibWebView/NotificationBackendMacOS.mm@@ -8,6 +8,7 @@#include <AK/Format.h>#include <LibCore/EventLoop.h>#include <LibGfx/ImageFormats/PNGWriter.h>+#include <LibWebView/NotificationAttribution.h>#include <LibWebView/NotificationBackendMacOS.h>#import <Foundation/Foundation.h>@@ -45,11 +46,6 @@ static NSString* ns_string(String const& string)return [NSString stringWithUTF8String:string.to_byte_string().characters()];}-static NSString* ns_string(Utf16String const& string)-{- return ns_string(string.to_utf8());-}-NotificationBackendMacOS::NotificationBackendMacOS(): m_bridge(make<Detail::NotificationCenterBridge>()){@@ -61,6 +57,14 @@ NotificationBackendMacOS::NotificationBackendMacOS()m_bridge->center = (__bridge_retained void*)[UNUserNotificationCenter currentNotificationCenter];m_bridge->delegate = (__bridge_retained void*)[[LadybirdNotificationCenterDelegate alloc] initWithBackend:this];center_of(*m_bridge).delegate = delegate_of(*m_bridge);+ m_launch_token = MUST(String::from_utf8(StringView { [[[NSUUID UUID] UUIDString] UTF8String], strlen([[[NSUUID UUID] UUIDString] UTF8String]) }));++ // The center reports a user's dismissal only for a category that asks for it.+ auto* category = [UNNotificationCategory categoryWithIdentifier:@"org.ladybird.Ladybird.web-notification"+ actions:@[]+ intentIdentifiers:@[]+ options:UNNotificationCategoryOptionCustomDismissAction];+ [center_of(*m_bridge) setNotificationCategories:[NSSet setWithObject:category]];}NotificationBackendMacOS::~NotificationBackendMacOS()@@ -113,6 +117,9 @@ void NotificationBackendMacOS::show(PlatformNotification const& notification)}request_authorization_then([this, notification](bool granted) {+ // Closed while the authorization was pending: Its close was reported then.+ if (m_closed_before_shown.remove(notification.id))+ return;if (!granted) {if (on_failed)on_failed(notification.id);@@ -120,10 +127,11 @@ void NotificationBackendMacOS::show(PlatformNotification const& notification)}auto* content = [[UNMutableNotificationContent alloc] init];- content.title = ns_string(notification.data.title);+ content.title = ns_string(platform_text(notification.data.title));content.subtitle = ns_string(notification.attribution);- content.body = ns_string(notification.data.body);+ content.body = ns_string(platform_text(notification.data.body));content.threadIdentifier = ns_string(notification.attribution);+ content.categoryIdentifier = @"org.ladybird.Ladybird.web-notification";if (notification.data.silent != true)content.sound = [UNNotificationSound defaultSound];@@ -156,7 +164,7 @@ void NotificationBackendMacOS::show(PlatformNotification const& notification)identifier = existing.release_value();}if (identifier.is_empty())- identifier = String::number(notification.id);+ identifier = MUST(String::formatted("{}-{}", m_launch_token, notification.id));m_identifier_for_id.set(notification.id, identifier);m_id_for_identifier.set(identifier, notification.id);@@ -186,8 +194,16 @@ void NotificationBackendMacOS::show(PlatformNotification const& notification)void NotificationBackendMacOS::close(u64 id){auto identifier = m_identifier_for_id.take(id);- if (!identifier.has_value())+ if (!identifier.has_value()) {+ // Not handed to the center yet (authorization pending): The show, when it gets its turn, finds this and+ // does nothing; the close is reported now.+ if (m_authorization == Authorization::Pending) {+ m_closed_before_shown.set(id);+ if (on_closed)+ on_closed(id, ByUser::No);+ }return;+ }m_id_for_identifier.remove(*identifier);NSArray* identifiers = @[ ns_string(*identifier) ];[center_of(*m_bridge) removeDeliveredNotificationsWithIdentifiers:identifiers];