Skip to content

Truly independent web browser

LibWebView: Add a Freedesktop notification backend

sideshowbarkerbdcbdddbc1fc
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index 659f8095abf..35ca4f05b53 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -48,6 +48,7 @@ set(SOURCES
Omnibox.cpp
OmniboxEngagement.cpp
NotificationAttribution.cpp
+ NotificationFreedesktopArguments.cpp
NotificationManager.cpp
PictureInPictureManager.cpp
PictureInPictureWindow.cpp
@@ -108,6 +109,12 @@ elseif (LINUX)
list(APPEND SOURCES AudioDaemonSocket.cpp CrashReportPOSIX.cpp)
endif()
+if (LINUX OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+ # Web notifications reach the desktop's notification daemon over the session bus.
+ pkg_check_modules(DBUS REQUIRED IMPORTED_TARGET dbus-1)
+ list(APPEND SOURCES NotificationBackendFreedesktop.cpp)
+endif()
+
set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED})
compile_ipc(UIProcessServer.ipc UIProcessServerEndpoint.h)
@@ -170,6 +177,10 @@ if (APPLE)
target_link_libraries(LibWebView PRIVATE "-framework Carbon" "-framework UserNotifications")
endif()
+if (LINUX OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+ target_link_libraries(LibWebView PRIVATE PkgConfig::DBUS)
+endif()
+
# Third-party
if (HAS_FONTCONFIG)
target_link_libraries(LibWebView PRIVATE Fontconfig::Fontconfig)
diff --git a/Libraries/LibWebView/NotificationBackendFreedesktop.cpp b/Libraries/LibWebView/NotificationBackendFreedesktop.cpp
new file mode 100644
index 00000000000..07ddb087437
--- /dev/null
+++ b/Libraries/LibWebView/NotificationBackendFreedesktop.cpp
@@ -0,0 +1,375 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteString.h>
+#include <AK/kmalloc.h>
+#include <LibCore/EventLoop.h>
+#include <LibCore/Notifier.h>
+#include <LibCore/Timer.h>
+#include <LibWebView/NotificationBackendFreedesktop.h>
+
+#include <dbus/dbus.h>
+
+namespace WebView {
+
+static constexpr auto bus_name = "org.freedesktop.Notifications";
+static constexpr auto object_path = "/org/freedesktop/Notifications";
+static constexpr auto interface_name = "org.freedesktop.Notifications";
+
+// Reasons the daemon gives in NotificationClosed.
+static constexpr u32 closed_reason_dismissed_by_user = 2;
+
+NotificationBackendFreedesktop::NotificationBackendFreedesktop()
+{
+ if (!connect())
+ return;
+ request_capabilities();
+}
+
+NotificationBackendFreedesktop::~NotificationBackendFreedesktop()
+{
+ if (!m_connection)
+ return;
+ dbus_connection_set_watch_functions(m_connection, nullptr, nullptr, nullptr, nullptr, nullptr);
+ dbus_connection_set_timeout_functions(m_connection, nullptr, nullptr, nullptr, nullptr, nullptr);
+ dbus_connection_close(m_connection);
+ dbus_connection_unref(m_connection);
+}
+
+bool NotificationBackendFreedesktop::connect()
+{
+ DBusError error;
+ dbus_error_init(&error);
+ m_connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
+ if (!m_connection) {
+ dbgln("Notifications: No session bus to reach a notification daemon over: {}", error.message ? error.message : "unknown error");
+ dbus_error_free(&error);
+ return false;
+ }
+ dbus_connection_set_exit_on_disconnect(m_connection, FALSE);
+
+ dbus_connection_set_watch_functions(m_connection, add_watch, remove_watch, toggle_watch, this, nullptr);
+ dbus_connection_set_timeout_functions(m_connection, add_timeout, remove_timeout, toggle_timeout, this, nullptr);
+ dbus_connection_set_dispatch_status_function(
+ m_connection, [](DBusConnection*, DBusDispatchStatus status, void* data) {
+ if (status == DBUS_DISPATCH_DATA_REMAINS)
+ Core::deferred_invoke([backend = static_cast<NotificationBackendFreedesktop*>(data)] { backend->dispatch(); });
+ },
+ this, nullptr);
+
+ dbus_connection_add_filter(
+ m_connection, [](DBusConnection*, DBusMessage* message, void* data) -> DBusHandlerResult {
+ static_cast<NotificationBackendFreedesktop*>(data)->did_receive_signal(message);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ },
+ this, nullptr);
+ dbus_bus_add_match(m_connection, "type='signal',interface='org.freedesktop.Notifications'", nullptr);
+ return true;
+}
+
+void NotificationBackendFreedesktop::dispatch()
+{
+ while (dbus_connection_dispatch(m_connection) == DBUS_DISPATCH_DATA_REMAINS) { }
+}
+
+unsigned NotificationBackendFreedesktop::add_watch(DBusWatch* watch, void* data)
+{
+ auto& backend = *static_cast<NotificationBackendFreedesktop*>(data);
+ auto flags = dbus_watch_get_flags(watch);
+ auto type = Core::Notifier::Type::None;
+ if (flags & DBUS_WATCH_READABLE)
+ type |= Core::Notifier::Type::Read;
+ if (flags & DBUS_WATCH_WRITABLE)
+ type |= Core::Notifier::Type::Write;
+ auto notifier = Core::Notifier::construct(dbus_watch_get_unix_fd(watch), type);
+ notifier->on_activation = [watch, &backend, flags] {
+ dbus_watch_handle(watch, flags & (DBUS_WATCH_READABLE | DBUS_WATCH_WRITABLE));
+ backend.dispatch();
+ };
+ notifier->set_enabled(dbus_watch_get_enabled(watch));
+ backend.m_watches.append({ watch, move(notifier) });
+ return TRUE;
+}
+
+void NotificationBackendFreedesktop::remove_watch(DBusWatch* watch, void* data)
+{
+ auto& backend = *static_cast<NotificationBackendFreedesktop*>(data);
+ backend.m_watches.remove_all_matching([&](auto const& entry) { return entry.watch == watch; });
+}
+
+void NotificationBackendFreedesktop::toggle_watch(DBusWatch* watch, void* data)
+{
+ auto& backend = *static_cast<NotificationBackendFreedesktop*>(data);
+ for (auto& entry : backend.m_watches) {
+ if (entry.watch == watch)
+ entry.notifier->set_enabled(dbus_watch_get_enabled(watch));
+ }
+}
+
+unsigned NotificationBackendFreedesktop::add_timeout(DBusTimeout* timeout, void* data)
+{
+ auto& backend = *static_cast<NotificationBackendFreedesktop*>(data);
+ auto timer = Core::Timer::create_repeating(AK::Duration::from_milliseconds(dbus_timeout_get_interval(timeout)), [timeout, &backend] {
+ dbus_timeout_handle(timeout);
+ backend.dispatch();
+ });
+ if (dbus_timeout_get_enabled(timeout))
+ timer->start();
+ backend.m_timeouts.append({ timeout, move(timer) });
+ return TRUE;
+}
+
+void NotificationBackendFreedesktop::remove_timeout(DBusTimeout* timeout, void* data)
+{
+ auto& backend = *static_cast<NotificationBackendFreedesktop*>(data);
+ backend.m_timeouts.remove_all_matching([&](auto const& entry) { return entry.timeout == timeout; });
+}
+
+void NotificationBackendFreedesktop::toggle_timeout(DBusTimeout* timeout, void* data)
+{
+ auto& backend = *static_cast<NotificationBackendFreedesktop*>(data);
+ for (auto& entry : backend.m_timeouts) {
+ if (entry.timeout != timeout)
+ continue;
+ if (dbus_timeout_get_enabled(timeout))
+ entry.timer->start(AK::Duration::from_milliseconds(dbus_timeout_get_interval(timeout)));
+ else
+ entry.timer->stop();
+ }
+}
+
+void NotificationBackendFreedesktop::request_capabilities()
+{
+ m_capabilities_requested = true;
+ auto* message = dbus_message_new_method_call(bus_name, object_path, interface_name, "GetCapabilities");
+ DBusPendingCall* pending = nullptr;
+ if (!dbus_connection_send_with_reply(m_connection, message, &pending, -1) || !pending) {
+ dbus_message_unref(message);
+ m_capabilities = FreedesktopCapabilities {};
+ return;
+ }
+ dbus_message_unref(message);
+ dbus_pending_call_set_notify(
+ pending, [](DBusPendingCall* pending, void* data) {
+ auto* reply = dbus_pending_call_steal_reply(pending);
+ static_cast<NotificationBackendFreedesktop*>(data)->did_receive_capabilities(reply);
+ if (reply)
+ dbus_message_unref(reply);
+ },
+ this, nullptr);
+ dbus_pending_call_unref(pending);
+}
+
+void NotificationBackendFreedesktop::did_receive_capabilities(DBusMessage* reply)
+{
+ FreedesktopCapabilities capabilities;
+ DBusMessageIter iterator;
+ if (reply && dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN && dbus_message_iter_init(reply, &iterator) && dbus_message_iter_get_arg_type(&iterator) == DBUS_TYPE_ARRAY) {
+ DBusMessageIter entries;
+ dbus_message_iter_recurse(&iterator, &entries);
+ while (dbus_message_iter_get_arg_type(&entries) == DBUS_TYPE_STRING) {
+ char const* capability = nullptr;
+ dbus_message_iter_get_basic(&entries, &capability);
+ StringView name { capability, strlen(capability) };
+ if (name == "body"sv)
+ capabilities.body = true;
+ else if (name == "body-markup"sv)
+ capabilities.body_markup = true;
+ else if (name == "persistence"sv)
+ capabilities.persistence = true;
+ else if (name == "actions"sv)
+ capabilities.actions = true;
+ dbus_message_iter_next(&entries);
+ }
+ }
+ m_capabilities = capabilities;
+ for (auto& notification : exchange(m_waiting_for_capabilities, {}))
+ send_notify(notification);
+}
+
+void NotificationBackendFreedesktop::show(PlatformNotification const& notification)
+{
+ if (!m_connection) {
+ if (on_failed)
+ on_failed(notification.id);
+ return;
+ }
+ if (!m_capabilities.has_value()) {
+ m_waiting_for_capabilities.append(notification);
+ return;
+ }
+ send_notify(notification);
+}
+
+static void append_string(DBusMessageIter& iterator, String const& string)
+{
+ auto bytes = string.to_byte_string();
+ char const* characters = bytes.characters();
+ dbus_message_iter_append_basic(&iterator, DBUS_TYPE_STRING, &characters);
+}
+
+static void append_hint(DBusMessageIter& hints, FreedesktopHint const& hint)
+{
+ DBusMessageIter entry;
+ dbus_message_iter_open_container(&hints, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
+ append_string(entry, hint.name);
+
+ DBusMessageIter variant;
+ hint.value.visit(
+ [&](u8 value) {
+ dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, DBUS_TYPE_BYTE_AS_STRING, &variant);
+ dbus_message_iter_append_basic(&variant, DBUS_TYPE_BYTE, &value);
+ },
+ [&](bool value) {
+ dbus_bool_t dbus_value = value;
+ dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, DBUS_TYPE_BOOLEAN_AS_STRING, &variant);
+ dbus_message_iter_append_basic(&variant, DBUS_TYPE_BOOLEAN, &dbus_value);
+ },
+ [&](String const& value) {
+ dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &variant);
+ append_string(variant, value);
+ },
+ [&](FreedesktopImageData const& image) {
+ dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "(iiibiiay)", &variant);
+ DBusMessageIter structure;
+ dbus_message_iter_open_container(&variant, DBUS_TYPE_STRUCT, nullptr, &structure);
+ dbus_message_iter_append_basic(&structure, DBUS_TYPE_INT32, &image.width);
+ dbus_message_iter_append_basic(&structure, DBUS_TYPE_INT32, &image.height);
+ dbus_message_iter_append_basic(&structure, DBUS_TYPE_INT32, &image.rowstride);
+ dbus_bool_t has_alpha = image.has_alpha;
+ dbus_message_iter_append_basic(&structure, DBUS_TYPE_BOOLEAN, &has_alpha);
+ dbus_message_iter_append_basic(&structure, DBUS_TYPE_INT32, &image.bits_per_sample);
+ dbus_message_iter_append_basic(&structure, DBUS_TYPE_INT32, &image.channels);
+ DBusMessageIter bytes;
+ dbus_message_iter_open_container(&structure, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &bytes);
+ auto const* pixels = image.pixels.data();
+ int count = static_cast<int>(image.pixels.size());
+ dbus_message_iter_append_fixed_array(&bytes, DBUS_TYPE_BYTE, &pixels, count);
+ dbus_message_iter_close_container(&structure, &bytes);
+ dbus_message_iter_close_container(&variant, &structure);
+ });
+ dbus_message_iter_close_container(&entry, &variant);
+ dbus_message_iter_close_container(&hints, &entry);
+}
+
+void NotificationBackendFreedesktop::send_notify(PlatformNotification const& notification)
+{
+ u32 replaces_daemon_id = 0;
+ if (notification.replaces.has_value()) {
+ if (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);
+ }
+ }
+ auto arguments = build_notify_arguments(notification, *m_capabilities, replaces_daemon_id);
+
+ auto* message = dbus_message_new_method_call(bus_name, object_path, interface_name, "Notify");
+ DBusMessageIter iterator;
+ dbus_message_iter_init_append(message, &iterator);
+ append_string(iterator, arguments.app_name);
+ dbus_message_iter_append_basic(&iterator, DBUS_TYPE_UINT32, &arguments.replaces_id);
+ append_string(iterator, arguments.app_icon);
+ append_string(iterator, arguments.summary);
+ append_string(iterator, arguments.body);
+
+ DBusMessageIter actions;
+ dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &actions);
+ for (auto const& action : arguments.actions)
+ append_string(actions, action);
+ dbus_message_iter_close_container(&iterator, &actions);
+
+ DBusMessageIter hints;
+ dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, "{sv}", &hints);
+ for (auto const& hint : arguments.hints)
+ append_hint(hints, hint);
+ dbus_message_iter_close_container(&iterator, &hints);
+
+ dbus_message_iter_append_basic(&iterator, DBUS_TYPE_INT32, &arguments.expire_timeout);
+
+ DBusPendingCall* pending = nullptr;
+ if (!dbus_connection_send_with_reply(m_connection, message, &pending, -1) || !pending) {
+ dbus_message_unref(message);
+ if (on_failed)
+ on_failed(notification.id);
+ return;
+ }
+ dbus_message_unref(message);
+
+ struct Context {
+ AK_ALLOC_WITH_KMALLOC;
+ NotificationBackendFreedesktop* backend;
+ u64 id;
+ };
+ dbus_pending_call_set_notify(
+ pending, [](DBusPendingCall* pending, void* data) {
+ auto* context = static_cast<Context*>(data);
+ auto* reply = dbus_pending_call_steal_reply(pending);
+ context->backend->did_receive_notify_reply(context->id, reply);
+ if (reply)
+ dbus_message_unref(reply); },
+ new Context { this, notification.id }, [](void* data) { delete static_cast<Context*>(data); });
+ dbus_pending_call_unref(pending);
+}
+
+void NotificationBackendFreedesktop::did_receive_notify_reply(u64 id, DBusMessage* reply)
+{
+ 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)
+ on_failed(id);
+ return;
+ }
+ m_daemon_id_for.set(id, daemon_id);
+ m_id_for_daemon_id.set(daemon_id, id);
+ if (on_shown)
+ on_shown(id);
+}
+
+void 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);
+ // 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);
+}
+
+void NotificationBackendFreedesktop::did_receive_signal(DBusMessage* message)
+{
+ if (dbus_message_is_signal(message, interface_name, "ActionInvoked")) {
+ u32 daemon_id = 0;
+ char const* action = nullptr;
+ if (!dbus_message_get_args(message, nullptr, DBUS_TYPE_UINT32, &daemon_id, DBUS_TYPE_STRING, &action, DBUS_TYPE_INVALID))
+ return;
+ if (auto id = m_id_for_daemon_id.get(daemon_id); id.has_value() && on_clicked)
+ on_clicked(*id);
+ return;
+ }
+
+ if (dbus_message_is_signal(message, interface_name, "NotificationClosed")) {
+ u32 daemon_id = 0;
+ u32 reason = 0;
+ if (!dbus_message_get_args(message, nullptr, DBUS_TYPE_UINT32, &daemon_id, DBUS_TYPE_UINT32, &reason, DBUS_TYPE_INVALID))
+ return;
+ auto id = m_id_for_daemon_id.take(daemon_id);
+ if (!id.has_value())
+ return;
+ m_daemon_id_for.remove(*id);
+ if (on_closed)
+ on_closed(*id, reason == closed_reason_dismissed_by_user ? ByUser::Yes : ByUser::No);
+ }
+}
+
+}
diff --git a/Libraries/LibWebView/NotificationBackendFreedesktop.h b/Libraries/LibWebView/NotificationBackendFreedesktop.h
new file mode 100644
index 00000000000..7c457d68718
--- /dev/null
+++ b/Libraries/LibWebView/NotificationBackendFreedesktop.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/Optional.h>
+#include <AK/Vector.h>
+#include <LibCore/Forward.h>
+#include <LibWebView/NotificationBackend.h>
+#include <LibWebView/NotificationFreedesktopArguments.h>
+
+struct DBusConnection;
+struct DBusMessage;
+struct DBusPendingCall;
+struct DBusTimeout;
+struct DBusWatch;
+
+namespace WebView {
+
+// Shows notifications through the Desktop Notifications daemon on the session bus (org.freedesktop.Notifications),
+// over libdbus, with its sockets and timers on the browser's event loop. Without a session bus or a daemon, every show
+// fails.
+class WEBVIEW_API NotificationBackendFreedesktop final : public NotificationBackend {
+public:
+ NotificationBackendFreedesktop();
+ virtual ~NotificationBackendFreedesktop() override;
+
+ virtual void show(PlatformNotification const&) override;
+ virtual void close(u64 id) override;
+
+private:
+ bool connect();
+ void request_capabilities();
+ void did_receive_capabilities(DBusMessage* reply);
+ void send_notify(PlatformNotification const&);
+ void did_receive_notify_reply(u64 id, DBusMessage* reply);
+ void did_receive_signal(DBusMessage*);
+ void dispatch();
+
+ static unsigned add_watch(DBusWatch*, void* data);
+ static void remove_watch(DBusWatch*, void* data);
+ static void toggle_watch(DBusWatch*, void* data);
+ static unsigned add_timeout(DBusTimeout*, void* data);
+ static void remove_timeout(DBusTimeout*, void* data);
+ static void toggle_timeout(DBusTimeout*, void* data);
+
+ DBusConnection* m_connection { nullptr };
+
+ // The daemon names notifications by its own ids, handed out in the Notify reply.
+ HashMap<u64, u32> m_daemon_id_for;
+ HashMap<u32, u64> m_id_for_daemon_id;
+
+ Optional<FreedesktopCapabilities> m_capabilities;
+ bool m_capabilities_requested { false };
+ Vector<PlatformNotification> m_waiting_for_capabilities;
+
+ struct Watch {
+ DBusWatch* watch { nullptr };
+ RefPtr<Core::Notifier> notifier;
+ };
+ Vector<Watch> m_watches;
+
+ struct Timeout {
+ DBusTimeout* timeout { nullptr };
+ RefPtr<Core::Timer> timer;
+ };
+ Vector<Timeout> m_timeouts;
+};
+
+}
diff --git a/Libraries/LibWebView/NotificationFreedesktopArguments.cpp b/Libraries/LibWebView/NotificationFreedesktopArguments.cpp
new file mode 100644
index 00000000000..5a2f2694fb2
--- /dev/null
+++ b/Libraries/LibWebView/NotificationFreedesktopArguments.cpp
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/StringBuilder.h>
+#include <LibWebView/NotificationFreedesktopArguments.h>
+
+namespace WebView {
+
+static constexpr i32 expire_timeout_default = -1;
+static constexpr i32 expire_timeout_never = 0;
+static constexpr i32 expire_timeout_without_notification_center_ms = 25000;
+static constexpr u8 urgency_normal = 1;
+static constexpr u8 urgency_critical = 2;
+
+static String escape_markup(String const& text)
+{
+ StringBuilder builder;
+ for (auto code_point : text.code_points()) {
+ switch (code_point) {
+ case '&':
+ builder.append("&amp;"sv);
+ break;
+ case '<':
+ builder.append("&lt;"sv);
+ break;
+ case '>':
+ builder.append("&gt;"sv);
+ break;
+ default:
+ builder.append_code_point(code_point);
+ }
+ }
+ return MUST(builder.to_string());
+}
+
+static FreedesktopImageData image_data_for(Gfx::Bitmap const& bitmap)
+{
+ FreedesktopImageData image;
+ image.width = bitmap.width();
+ image.height = bitmap.height();
+ image.rowstride = bitmap.width() * 4;
+ image.pixels = MUST(ByteBuffer::create_uninitialized(static_cast<size_t>(image.rowstride) * image.height));
+ size_t offset = 0;
+ for (int y = 0; y < bitmap.height(); ++y) {
+ for (int x = 0; x < bitmap.width(); ++x) {
+ // The daemon wants straight RGBA; a premultiplied bitmap's channels are scaled back up by their alpha.
+ auto color = bitmap.get_pixel(x, y);
+ auto alpha = color.alpha();
+ auto straight = [&](u8 channel) -> u8 {
+ if (bitmap.alpha_type() != Gfx::AlphaType::Premultiplied || alpha == 0 || alpha == 255)
+ return channel;
+ return static_cast<u8>(min(255, (channel * 255 + alpha / 2) / alpha));
+ };
+ image.pixels[offset++] = straight(color.red());
+ image.pixels[offset++] = straight(color.green());
+ image.pixels[offset++] = straight(color.blue());
+ image.pixels[offset++] = alpha;
+ }
+ }
+ return image;
+}
+
+FreedesktopNotifyArguments build_notify_arguments(PlatformNotification const& notification, FreedesktopCapabilities const& capabilities, u32 replaces_daemon_id)
+{
+ FreedesktopNotifyArguments arguments;
+ arguments.app_name = "Ladybird"_string;
+ arguments.replaces_id = replaces_daemon_id;
+ arguments.app_icon = "org.ladybird.Ladybird"_string;
+ arguments.summary = notification.data.title.to_utf8();
+
+ if (capabilities.body) {
+ auto attribution = notification.attribution;
+ auto body = notification.data.body.to_utf8();
+ if (capabilities.body_markup) {
+ attribution = escape_markup(attribution);
+ body = escape_markup(body);
+ }
+ arguments.body = MUST(String::formatted("{}\n\n{}", attribution, body));
+ }
+
+ // "default" makes the whole notification clickable where the daemon shows actions as buttons.
+ if (capabilities.actions)
+ arguments.actions = { "default"_string, "Activate"_string };
+
+ bool sticky = notification.data.require_interaction;
+ arguments.hints.append({ "urgency"_string, sticky ? urgency_critical : urgency_normal });
+ if (notification.data.silent == true)
+ arguments.hints.append({ "suppress-sound"_string, true });
+ arguments.hints.append({ "desktop-entry"_string, "org.ladybird.Ladybird"_string });
+ if (notification.icon)
+ arguments.hints.append({ "image-data"_string, image_data_for(*notification.icon) });
+
+ if (sticky)
+ arguments.expire_timeout = expire_timeout_never;
+ else if (capabilities.persistence)
+ arguments.expire_timeout = expire_timeout_default;
+ else
+ arguments.expire_timeout = expire_timeout_without_notification_center_ms;
+
+ return arguments;
+}
+
+}
diff --git a/Libraries/LibWebView/NotificationFreedesktopArguments.h b/Libraries/LibWebView/NotificationFreedesktopArguments.h
new file mode 100644
index 00000000000..bcad9cb7ffa
--- /dev/null
+++ b/Libraries/LibWebView/NotificationFreedesktopArguments.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/ByteBuffer.h>
+#include <AK/String.h>
+#include <AK/Types.h>
+#include <AK/Variant.h>
+#include <AK/Vector.h>
+#include <LibWebView/Export.h>
+#include <LibWebView/NotificationBackend.h>
+
+namespace WebView {
+
+// What a Desktop Notifications daemon (org.freedesktop.Notifications) says it can show, from its GetCapabilities.
+struct FreedesktopCapabilities {
+ bool body { false };
+ bool body_markup { false };
+ bool persistence { false };
+ bool actions { false };
+};
+
+// The "image-data" hint: raw RGBA pixels, (iiibiiay) on the wire.
+struct FreedesktopImageData {
+ i32 width { 0 };
+ i32 height { 0 };
+ i32 rowstride { 0 };
+ bool has_alpha { true };
+ i32 bits_per_sample { 8 };
+ i32 channels { 4 };
+ ByteBuffer pixels;
+};
+
+struct FreedesktopHint {
+ String name;
+ Variant<u8, bool, String, FreedesktopImageData> value;
+};
+
+// The arguments of a Notify call (app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout).
+struct FreedesktopNotifyArguments {
+ String app_name;
+ u32 replaces_id { 0 };
+ String app_icon;
+ String summary;
+ String body;
+ Vector<String> actions;
+ Vector<FreedesktopHint> hints;
+ i32 expire_timeout { 0 };
+};
+
+// Lays a notification out for the daemon: the attribution opens the body, markup is escaped only for a daemon that
+// renders it, a notification that requires interaction never expires and is critical, and one shown by a daemon
+// without a notification center (no "persistence") expires after 25 seconds rather than at the daemon's whim.
+WEBVIEW_API FreedesktopNotifyArguments build_notify_arguments(PlatformNotification const&, FreedesktopCapabilities const&, u32 replaces_daemon_id);
+
+}
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 4ef5a71c0a7..a9b776d6919 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -176,6 +176,7 @@ set(TEST_SOURCES
TestHSTSStore.cpp
TestNavigationLoader.cpp
TestNotificationAttribution.cpp
+ TestNotificationFreedesktopArguments.cpp
TestOmnibox.cpp
TestPermissionManager.cpp
TestPictureInPictureWindow.cpp
diff --git a/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp b/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp
new file mode 100644
index 00000000000..4541fb366c4
--- /dev/null
+++ b/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+#include <LibWebView/NotificationFreedesktopArguments.h>
+
+using namespace WebView;
+
+static PlatformNotification notification(Utf16String title, Utf16String body)
+{
+ PlatformNotification notification;
+ notification.id = 7;
+ notification.data.title = move(title);
+ notification.data.body = move(body);
+ notification.attribution = "example.com"_string;
+ return notification;
+}
+
+static Optional<FreedesktopHint> hint(FreedesktopNotifyArguments const& arguments, StringView name)
+{
+ for (auto const& hint : arguments.hints) {
+ if (hint.name == name)
+ return hint;
+ }
+ return {};
+}
+
+TEST_CASE(summary_is_the_title_and_body_opens_with_the_attribution)
+{
+ auto arguments = build_notify_arguments(notification("New mail"_utf16, "From Alice"_utf16), { .body = true }, 0);
+ EXPECT_EQ(arguments.app_name, "Ladybird"_string);
+ EXPECT_EQ(arguments.summary, "New mail"_string);
+ EXPECT_EQ(arguments.body, "example.com\n\nFrom Alice"_string);
+ EXPECT_EQ(arguments.replaces_id, 0u);
+}
+
+TEST_CASE(replaced_daemon_id_passes_through)
+{
+ auto arguments = build_notify_arguments(notification("t"_utf16, "b"_utf16), { .body = true }, 42);
+ EXPECT_EQ(arguments.replaces_id, 42u);
+}
+
+TEST_CASE(markup_is_escaped_only_when_the_daemon_renders_it)
+{
+ auto plain = build_notify_arguments(notification("<b>&</b>"_utf16, "a < b & c"_utf16), { .body = true }, 0);
+ EXPECT_EQ(plain.summary, "<b>&</b>"_string);
+ EXPECT_EQ(plain.body, "example.com\n\na < b & c"_string);
+
+ auto markup = build_notify_arguments(notification("<b>&</b>"_utf16, "a < b & c"_utf16), { .body = true, .body_markup = true }, 0);
+ EXPECT_EQ(markup.summary, "<b>&</b>"_string);
+ EXPECT_EQ(markup.body, "example.com\n\na &lt; b &amp; c"_string);
+}
+
+TEST_CASE(daemon_without_body_support_gets_no_body)
+{
+ auto arguments = build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0);
+ EXPECT_EQ(arguments.body, ""_string);
+}
+
+TEST_CASE(expire_timeout_follows_persistence_and_require_interaction)
+{
+ EXPECT_EQ(build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0).expire_timeout, 25000);
+ EXPECT_EQ(build_notify_arguments(notification("t"_utf16, "b"_utf16), { .persistence = true }, 0).expire_timeout, -1);
+
+ auto sticky = notification("t"_utf16, "b"_utf16);
+ sticky.data.require_interaction = true;
+ EXPECT_EQ(build_notify_arguments(sticky, { .persistence = true }, 0).expire_timeout, 0);
+}
+
+TEST_CASE(urgency_is_critical_only_when_interaction_is_required)
+{
+ auto normal = hint(build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0), "urgency"sv);
+ EXPECT(normal.has_value());
+ EXPECT_EQ(normal->value.get<u8>(), 1u);
+
+ auto sticky = notification("t"_utf16, "b"_utf16);
+ sticky.data.require_interaction = true;
+ auto critical = hint(build_notify_arguments(sticky, {}, 0), "urgency"sv);
+ EXPECT_EQ(critical->value.get<u8>(), 2u);
+}
+
+TEST_CASE(silent_suppresses_the_sound)
+{
+ auto loud = hint(build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0), "suppress-sound"sv);
+ EXPECT(!loud.has_value());
+
+ auto quiet_notification = notification("t"_utf16, "b"_utf16);
+ quiet_notification.data.silent = true;
+ auto quiet = hint(build_notify_arguments(quiet_notification, {}, 0), "suppress-sound"sv);
+ EXPECT(quiet.has_value());
+ EXPECT_EQ(quiet->value.get<bool>(), true);
+}
+
+TEST_CASE(desktop_entry_names_the_application)
+{
+ auto entry = hint(build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0), "desktop-entry"sv);
+ EXPECT(entry.has_value());
+ EXPECT_EQ(entry->value.get<String>(), "org.ladybird.Ladybird"_string);
+}
+
+TEST_CASE(default_action_only_when_the_daemon_takes_actions)
+{
+ EXPECT(build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0).actions.is_empty());
+ auto with_actions = build_notify_arguments(notification("t"_utf16, "b"_utf16), { .actions = true }, 0);
+ EXPECT_EQ(with_actions.actions.size(), 2u);
+ EXPECT_EQ(with_actions.actions[0], "default"_string);
+}
+
+TEST_CASE(icon_becomes_image_data)
+{
+ auto with_icon = notification("t"_utf16, "b"_utf16);
+ with_icon.icon = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 2, 1 }));
+ auto image = hint(build_notify_arguments(with_icon, {}, 0), "image-data"sv);
+ EXPECT(image.has_value());
+ auto const& data = image->value.get<FreedesktopImageData>();
+ EXPECT_EQ(data.width, 2);
+ EXPECT_EQ(data.height, 1);
+ EXPECT_EQ(data.rowstride, 8);
+ EXPECT_EQ(data.has_alpha, true);
+ EXPECT_EQ(data.bits_per_sample, 8);
+ EXPECT_EQ(data.channels, 4);
+ EXPECT_EQ(data.pixels.size(), 8u);
+}
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 67ec5da004a..f98349e3f12 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -11,6 +11,8 @@
#include <LibWebView/NotificationBackend.h>
#if defined(AK_OS_MACOS)
# include <LibWebView/NotificationBackendMacOS.h>
+#elif defined(AK_OS_LINUX) || defined(AK_OS_FREEBSD)
+# include <LibWebView/NotificationBackendFreedesktop.h>
#endif
#include <LibWebView/SessionStore.h>
#include <LibWebView/URL.h>
@@ -1307,8 +1309,9 @@ NonnullOwnPtr<WebView::NotificationBackend> Application::create_notification_bac
{
#if defined(AK_OS_MACOS)
return make<WebView::NotificationBackendMacOS>();
+#elif defined(AK_OS_LINUX) || defined(AK_OS_FREEBSD)
+ return make<WebView::NotificationBackendFreedesktop>();
#else
- // The Freedesktop backend follows; until then, pages see every show fail.
return WebView::Application::create_notification_backend();
#endif
}