Skip to content

Truly independent web browser

3.1 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteString.h>
#include <AK/HashMap.h>
#include <AK/HashTable.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(u64 daemon_generation, DBusMessage* reply);
    void send_notify(PlatformNotification const&);
    void send_close_notification(u32 daemon_id);
    void remember_daemon_from(DBusMessage* reply);
    bool signal_is_from_the_daemon(DBusMessage*) const;
    void daemon_owner_changed(char const* new_owner);
    void did_receive_notify_reply(u64 id, u64 daemon_generation, 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. They mean something only to the
    // daemon that handed them out, which its replies name; a daemon that replaces it numbers from one again.
    HashMap<u64, u32> m_daemon_id_for;
    HashMap<u32, u64> m_id_for_daemon_id;
    ByteString m_daemon_unique_name;
    // Bumped whenever the name changes hands, so a reply from a daemon that has since gone away is told apart.
    u64 m_daemon_generation { 0 };

    Optional<FreedesktopCapabilities> m_capabilities;
    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;
    };
    Vector<Watch> m_watches;

    struct Timeout {
        DBusTimeout* timeout { nullptr };
        RefPtr<Core::Timer> timer;
    };
    Vector<Timeout> m_timeouts;
};

}