2023-05-25 14:31:22 -04:00
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
2020-03-03 21:06:57 -05:00
|
|
|
|
2023-05-25 14:31:22 -04:00
|
|
|
import {programArgs} from 'system';
|
2020-03-03 21:06:57 -05:00
|
|
|
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
|
|
|
const IDLE_SHUTDOWN_TIME = 2; // s
|
|
|
|
|
2023-05-25 14:31:22 -04:00
|
|
|
export class ServiceImplementation {
|
2020-03-03 21:06:57 -05:00
|
|
|
constructor(info, objectPath) {
|
|
|
|
this._objectPath = objectPath;
|
|
|
|
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(info, this);
|
|
|
|
|
|
|
|
this._injectTracking('return_dbus_error');
|
|
|
|
this._injectTracking('return_error_literal');
|
|
|
|
this._injectTracking('return_gerror');
|
|
|
|
this._injectTracking('return_value');
|
|
|
|
this._injectTracking('return_value_with_unix_fd_list');
|
|
|
|
|
|
|
|
this._senders = new Map();
|
2020-03-20 17:32:08 -04:00
|
|
|
this._holdCount = 0;
|
2020-03-03 21:06:57 -05:00
|
|
|
|
|
|
|
this._hasSignals = this._dbusImpl.get_info().signals.length > 0;
|
|
|
|
this._shutdownTimeoutId = 0;
|
|
|
|
|
|
|
|
// subclasses may override this to disable automatic shutdown
|
|
|
|
this._autoShutdown = true;
|
2020-07-22 12:22:38 -04:00
|
|
|
|
|
|
|
this._queueShutdownCheck();
|
2020-03-03 21:06:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// subclasses may override this to own additional names
|
|
|
|
register() {
|
|
|
|
}
|
|
|
|
|
|
|
|
export() {
|
|
|
|
this._dbusImpl.export(Gio.DBus.session, this._objectPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
unexport() {
|
|
|
|
this._dbusImpl.unexport();
|
|
|
|
}
|
|
|
|
|
2020-03-20 17:32:08 -04:00
|
|
|
hold() {
|
|
|
|
this._holdCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
release() {
|
|
|
|
if (this._holdCount === 0) {
|
|
|
|
logError(new Error('Unmatched call to release()'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._holdCount--;
|
|
|
|
|
|
|
|
if (this._holdCount === 0)
|
|
|
|
this._queueShutdownCheck();
|
|
|
|
}
|
|
|
|
|
2020-03-03 21:06:57 -05:00
|
|
|
/**
|
|
|
|
* Complete @invocation with an appropriate error if @error is set;
|
|
|
|
* useful for implementing early returns from method implementations.
|
|
|
|
*
|
2023-08-06 21:15:51 -04:00
|
|
|
* @param {Gio.DBusMethodInvocation}
|
|
|
|
* @param {Error}
|
|
|
|
*
|
2020-03-03 21:06:57 -05:00
|
|
|
* @returns {bool} - true if @invocation was completed
|
|
|
|
*/
|
|
|
|
|
|
|
|
_handleError(invocation, error) {
|
|
|
|
if (error === null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (error instanceof GLib.Error) {
|
|
|
|
invocation.return_gerror(error);
|
|
|
|
} else {
|
|
|
|
let name = error.name;
|
|
|
|
if (!name.includes('.')) // likely a normal JS error
|
|
|
|
name = `org.gnome.gjs.JSError.${name}`;
|
|
|
|
invocation.return_dbus_error(name, error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_maybeShutdown() {
|
|
|
|
if (!this._autoShutdown)
|
|
|
|
return;
|
|
|
|
|
2022-02-09 13:46:37 -05:00
|
|
|
if (GLib.getenv('SHELL_DBUS_PERSIST'))
|
|
|
|
return;
|
|
|
|
|
2020-03-20 17:32:08 -04:00
|
|
|
if (this._holdCount > 0)
|
2020-03-03 21:06:57 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
this.emit('shutdown');
|
|
|
|
}
|
|
|
|
|
|
|
|
_queueShutdownCheck() {
|
|
|
|
if (this._shutdownTimeoutId)
|
|
|
|
GLib.source_remove(this._shutdownTimeoutId);
|
|
|
|
|
|
|
|
this._shutdownTimeoutId = GLib.timeout_add_seconds(
|
|
|
|
GLib.PRIORITY_DEFAULT, IDLE_SHUTDOWN_TIME,
|
|
|
|
() => {
|
|
|
|
this._shutdownTimeoutId = 0;
|
|
|
|
this._maybeShutdown();
|
|
|
|
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_trackSender(sender) {
|
|
|
|
if (this._senders.has(sender))
|
|
|
|
return;
|
|
|
|
|
2020-03-20 17:32:08 -04:00
|
|
|
this.hold();
|
2020-03-03 21:06:57 -05:00
|
|
|
this._senders.set(sender,
|
|
|
|
this._dbusImpl.get_connection().watch_name(
|
|
|
|
sender,
|
|
|
|
Gio.BusNameWatcherFlags.NONE,
|
|
|
|
null,
|
|
|
|
() => this._untrackSender(sender)));
|
|
|
|
}
|
|
|
|
|
|
|
|
_untrackSender(sender) {
|
|
|
|
const id = this._senders.get(sender);
|
|
|
|
|
|
|
|
if (id)
|
|
|
|
this._dbusImpl.get_connection().unwatch_name(id);
|
|
|
|
|
|
|
|
if (this._senders.delete(sender))
|
2020-03-20 17:32:08 -04:00
|
|
|
this.release();
|
2020-03-03 21:06:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_injectTracking(methodName) {
|
2023-08-06 18:40:20 -04:00
|
|
|
const {prototype} = Gio.DBusMethodInvocation;
|
2020-03-03 21:06:57 -05:00
|
|
|
const origMethod = prototype[methodName];
|
|
|
|
const that = this;
|
|
|
|
|
|
|
|
prototype[methodName] = function (...args) {
|
|
|
|
origMethod.apply(this, args);
|
|
|
|
|
|
|
|
if (that._hasSignals)
|
|
|
|
that._trackSender(this.get_sender());
|
|
|
|
|
|
|
|
that._queueShutdownCheck();
|
|
|
|
};
|
|
|
|
}
|
2023-05-25 14:31:22 -04:00
|
|
|
}
|
2020-03-03 21:06:57 -05:00
|
|
|
Signals.addSignalMethods(ServiceImplementation.prototype);
|
|
|
|
|
2023-05-25 14:31:22 -04:00
|
|
|
export class DBusService {
|
2020-03-03 21:06:57 -05:00
|
|
|
constructor(name, service) {
|
|
|
|
this._name = name;
|
|
|
|
this._service = service;
|
|
|
|
this._loop = new GLib.MainLoop(null, false);
|
|
|
|
|
|
|
|
this._service.connect('shutdown', () => this._loop.quit());
|
|
|
|
}
|
|
|
|
|
2023-05-25 14:31:22 -04:00
|
|
|
async runAsync() {
|
2020-03-03 21:06:57 -05:00
|
|
|
// Bail out when not running under gnome-shell
|
|
|
|
Gio.DBus.watch_name(Gio.BusType.SESSION,
|
|
|
|
'org.gnome.Shell',
|
|
|
|
Gio.BusNameWatcherFlags.NONE,
|
|
|
|
null,
|
|
|
|
() => this._loop.quit());
|
|
|
|
|
|
|
|
this._service.register();
|
|
|
|
|
2022-02-02 19:57:46 -05:00
|
|
|
let flags = Gio.BusNameOwnerFlags.ALLOW_REPLACEMENT;
|
|
|
|
if (programArgs.includes('--replace'))
|
|
|
|
flags |= Gio.BusNameOwnerFlags.REPLACE;
|
|
|
|
|
2020-03-03 21:06:57 -05:00
|
|
|
Gio.DBus.own_name(Gio.BusType.SESSION,
|
|
|
|
this._name,
|
2022-02-02 19:57:46 -05:00
|
|
|
flags,
|
2020-03-03 21:06:57 -05:00
|
|
|
() => this._service.export(),
|
|
|
|
null,
|
|
|
|
() => this._loop.quit());
|
|
|
|
|
2023-05-25 14:31:22 -04:00
|
|
|
await this._loop.runAsync();
|
2020-03-03 21:06:57 -05:00
|
|
|
}
|
2023-05-25 14:31:22 -04:00
|
|
|
}
|