1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported NotificationDaemon */
const { Gio, GLib } = imports.gi;
const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ServiceImplementation } = imports.dbusService;
const NotificationsIface = loadInterfaceXML('org.freedesktop.Notifications');
const NotificationsProxy = Gio.DBusProxy.makeProxyWrapper(NotificationsIface);
Gio._promisify(Gio.DBusConnection.prototype, 'call');
var NotificationDaemon = class extends ServiceImplementation {
constructor() {
super(NotificationsIface, '/org/freedesktop/Notifications');
this._autoShutdown = false;
this._activeNotifications = new Map();
this._proxy = new NotificationsProxy(Gio.DBus.session,
'org.gnome.Shell',
'/org/freedesktop/Notifications',
(proxy, error) => {
if (error)
log(error.message);
});
this._proxy.connectSignal('ActionInvoked',
(proxy, sender, params) => {
const [id] = params;
this._emitSignal(
this._activeNotifications.get(id),
'ActionInvoked',
new GLib.Variant('(us)', params));
});
this._proxy.connectSignal('NotificationClosed',
(proxy, sender, params) => {
const [id] = params;
this._emitSignal(
this._activeNotifications.get(id),
'NotificationClosed',
new GLib.Variant('(uu)', params));
this._activeNotifications.delete(id);
});
}
_emitSignal(sender, signalName, params) {
if (!sender)
return;
this._dbusImpl.get_connection()?.emit_signal(
sender,
this._dbusImpl.get_object_path(),
'org.freedesktop.Notifications',
signalName,
params);
}
_untrackSender(sender) {
super._untrackSender(sender);
this._activeNotifications.forEach((value, key) => {
if (value === sender)
this._activeNotifications.delete(key);
});
}
_checkNotificationId(invocation, id) {
if (id === 0)
return true;
if (!this._activeNotifications.has(id))
return true;
if (this._activeNotifications.get(id) === invocation.get_sender())
return true;
const error = new GLib.Error(Gio.DBusError,
Gio.DBusError.INVALID_ARGS, 'Invalid notification ID');
this._handleError(invocation, error);
return false;
}
register() {
Gio.DBus.session.own_name(
'org.freedesktop.Notifications',
Gio.BusNameOwnerFlags.REPLACE,
null, null);
}
async NotifyAsync(params, invocation) {
const sender = invocation.get_sender();
const pid = await this._getSenderPid(sender);
const replaceId = params[1];
const hints = params[6];
if (!this._checkNotificationId(invocation, replaceId))
return;
params[6] = {
...hints,
'sender-pid': new GLib.Variant('u', pid),
};
try {
const [id] = await this._proxy.NotifyAsync(...params);
this._activeNotifications.set(id, sender);
invocation.return_value(new GLib.Variant('(u)', [id]));
} catch (error) {
this._handleError(invocation, error);
}
}
async CloseNotificationAsync(params, invocation) {
const [id] = params;
if (!this._checkNotificationId(invocation, id))
return;
try {
await this._proxy.CloseNotificationAsync(...params);
invocation.return_value(null);
} catch (error) {
this._handleError(invocation, error);
}
}
async GetCapabilitiesAsync(params, invocation) {
try {
const res = await this._proxy.GetCapabilitiesAsync(...params);
invocation.return_value(new GLib.Variant('(as)', res));
} catch (error) {
this._handleError(invocation, error);
}
}
async GetServerInformationAsync(params, invocation) {
try {
const res = await this._proxy.GetServerInformationAsync(...params);
invocation.return_value(new GLib.Variant('(ssss)', res));
} catch (error) {
this._handleError(invocation, error);
}
}
async _getSenderPid(sender) {
const res = await Gio.DBus.session.call(
'org.freedesktop.DBus',
'/',
'org.freedesktop.DBus',
'GetConnectionUnixProcessID',
new GLib.Variant('(s)', [sender]),
new GLib.VariantType('(u)'),
Gio.DBusCallFlags.NONE,
-1,
null);
const [pid] = res.deepUnpack();
return pid;
}
};
|