2013-05-30 14:15:09 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2022-07-04 22:30:44 +00:00
|
|
|
/* exported ObjectManager */
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Gio, GLib } = imports.gi;
|
2013-05-30 14:15:09 +00:00
|
|
|
const Params = imports.misc.params;
|
2022-07-04 22:30:44 +00:00
|
|
|
const Signals = imports.misc.signals;
|
2013-05-30 14:15:09 +00:00
|
|
|
|
|
|
|
// Specified in the D-Bus specification here:
|
|
|
|
// http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager
|
2018-08-23 00:55:02 +00:00
|
|
|
const ObjectManagerIface = `
|
|
|
|
<node>
|
|
|
|
<interface name="org.freedesktop.DBus.ObjectManager">
|
|
|
|
<method name="GetManagedObjects">
|
|
|
|
<arg name="objects" type="a{oa{sa{sv}}}" direction="out"/>
|
|
|
|
</method>
|
|
|
|
<signal name="InterfacesAdded">
|
|
|
|
<arg name="objectPath" type="o"/>
|
|
|
|
<arg name="interfaces" type="a{sa{sv}}" />
|
|
|
|
</signal>
|
|
|
|
<signal name="InterfacesRemoved">
|
|
|
|
<arg name="objectPath" type="o"/>
|
|
|
|
<arg name="interfaces" type="as" />
|
|
|
|
</signal>
|
|
|
|
</interface>
|
|
|
|
</node>`;
|
2013-05-30 14:15:09 +00:00
|
|
|
|
|
|
|
const ObjectManagerInfo = Gio.DBusInterfaceInfo.new_for_xml(ObjectManagerIface);
|
|
|
|
|
2022-07-04 22:30:44 +00:00
|
|
|
var ObjectManager = class extends Signals.EventEmitter {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor(params) {
|
2022-07-04 22:30:44 +00:00
|
|
|
super();
|
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
params = Params.parse(params, {
|
|
|
|
connection: null,
|
|
|
|
name: null,
|
|
|
|
objectPath: null,
|
|
|
|
knownInterfaces: null,
|
|
|
|
cancellable: null,
|
|
|
|
onLoaded: null,
|
|
|
|
});
|
2013-05-30 14:15:09 +00:00
|
|
|
|
|
|
|
this._connection = params.connection;
|
|
|
|
this._serviceName = params.name;
|
|
|
|
this._managerPath = params.objectPath;
|
|
|
|
this._cancellable = params.cancellable;
|
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._managerProxy = new Gio.DBusProxy({
|
|
|
|
g_connection: this._connection,
|
|
|
|
g_interface_name: ObjectManagerInfo.name,
|
|
|
|
g_interface_info: ObjectManagerInfo,
|
|
|
|
g_name: this._serviceName,
|
|
|
|
g_object_path: this._managerPath,
|
|
|
|
g_flags: Gio.DBusProxyFlags.DO_NOT_AUTO_START,
|
|
|
|
});
|
2013-05-30 14:15:09 +00:00
|
|
|
|
|
|
|
this._interfaceInfos = {};
|
|
|
|
this._objects = {};
|
|
|
|
this._interfaces = {};
|
|
|
|
this._onLoaded = params.onLoaded;
|
|
|
|
|
|
|
|
if (params.knownInterfaces)
|
|
|
|
this._registerInterfaces(params.knownInterfaces);
|
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
this._initManagerProxy();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2022-07-08 06:52:18 +00:00
|
|
|
_completeLoad() {
|
|
|
|
if (this._onLoaded)
|
|
|
|
this._onLoaded();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2022-07-08 06:52:18 +00:00
|
|
|
async _addInterface(objectPath, interfaceName) {
|
2013-08-22 13:40:12 +00:00
|
|
|
let info = this._interfaceInfos[interfaceName];
|
|
|
|
|
2022-07-08 06:52:18 +00:00
|
|
|
if (!info)
|
2019-01-29 19:36:54 +00:00
|
|
|
return;
|
2013-08-22 13:40:12 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
const proxy = new Gio.DBusProxy({
|
|
|
|
g_connection: this._connection,
|
|
|
|
g_name: this._serviceName,
|
|
|
|
g_object_path: objectPath,
|
|
|
|
g_interface_name: interfaceName,
|
|
|
|
g_interface_info: info,
|
|
|
|
g_flags: Gio.DBusProxyFlags.DO_NOT_AUTO_START,
|
|
|
|
});
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
try {
|
|
|
|
await proxy.init_async(GLib.PRIORITY_DEFAULT, this._cancellable);
|
|
|
|
} catch (e) {
|
|
|
|
logError(e, `could not initialize proxy for interface ${interfaceName}`);
|
|
|
|
return;
|
|
|
|
}
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
let isNewObject;
|
|
|
|
if (!this._objects[objectPath]) {
|
|
|
|
this._objects[objectPath] = {};
|
|
|
|
isNewObject = true;
|
|
|
|
} else {
|
|
|
|
isNewObject = false;
|
|
|
|
}
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
this._objects[objectPath][interfaceName] = proxy;
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
if (!this._interfaces[interfaceName])
|
|
|
|
this._interfaces[interfaceName] = [];
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
this._interfaces[interfaceName].push(proxy);
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
if (isNewObject)
|
|
|
|
this.emit('object-added', objectPath);
|
2019-01-29 19:36:54 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
this.emit('interface-added', interfaceName, proxy);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_removeInterface(objectPath, interfaceName) {
|
2013-05-30 14:15:09 +00:00
|
|
|
if (!this._objects[objectPath])
|
|
|
|
return;
|
|
|
|
|
|
|
|
let proxy = this._objects[objectPath][interfaceName];
|
|
|
|
|
|
|
|
if (this._interfaces[interfaceName]) {
|
|
|
|
let index = this._interfaces[interfaceName].indexOf(proxy);
|
|
|
|
|
|
|
|
if (index >= 0)
|
|
|
|
this._interfaces[interfaceName].splice(index, 1);
|
|
|
|
|
2022-07-08 03:38:39 +00:00
|
|
|
if (this._interfaces[interfaceName].length === 0)
|
2013-05-30 14:15:09 +00:00
|
|
|
delete this._interfaces[interfaceName];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('interface-removed', interfaceName, proxy);
|
|
|
|
|
2023-04-03 14:12:59 +00:00
|
|
|
delete this._objects[objectPath][interfaceName];
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2022-07-08 03:38:39 +00:00
|
|
|
if (Object.keys(this._objects[objectPath]).length === 0) {
|
2013-05-30 14:15:09 +00:00
|
|
|
delete this._objects[objectPath];
|
|
|
|
this.emit('object-removed', objectPath);
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
async _initManagerProxy() {
|
2013-05-30 14:15:09 +00:00
|
|
|
try {
|
2019-12-19 19:50:37 +00:00
|
|
|
await this._managerProxy.init_async(
|
|
|
|
GLib.PRIORITY_DEFAULT, this._cancellable);
|
2019-01-29 01:26:39 +00:00
|
|
|
} catch (e) {
|
2019-01-30 00:18:24 +00:00
|
|
|
logError(e, `could not initialize object manager for object ${this._serviceName}`);
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2022-07-08 06:52:18 +00:00
|
|
|
this._completeLoad();
|
2013-05-30 14:15:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._managerProxy.connectSignal('InterfacesAdded',
|
2022-07-08 03:38:39 +00:00
|
|
|
(objectManager, sender, [objectPath, interfaces]) => {
|
|
|
|
let interfaceNames = Object.keys(interfaces);
|
|
|
|
for (let i = 0; i < interfaceNames.length; i++)
|
|
|
|
this._addInterface(objectPath, interfaceNames[i]);
|
|
|
|
});
|
2013-05-30 14:15:09 +00:00
|
|
|
this._managerProxy.connectSignal('InterfacesRemoved',
|
2022-07-08 03:38:39 +00:00
|
|
|
(objectManager, sender, [objectPath, interfaceNames]) => {
|
|
|
|
for (let i = 0; i < interfaceNames.length; i++)
|
|
|
|
this._removeInterface(objectPath, interfaceNames[i]);
|
|
|
|
});
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2022-07-08 03:38:39 +00:00
|
|
|
if (Object.keys(this._interfaceInfos).length === 0) {
|
2022-07-08 06:52:18 +00:00
|
|
|
this._completeLoad();
|
2013-05-30 14:15:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
this._managerProxy.connect('notify::g-name-owner', () => {
|
2016-10-07 19:40:14 +00:00
|
|
|
if (this._managerProxy.g_name_owner)
|
|
|
|
this._onNameAppeared();
|
|
|
|
else
|
|
|
|
this._onNameVanished();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2016-10-07 19:40:14 +00:00
|
|
|
|
|
|
|
if (this._managerProxy.g_name_owner)
|
|
|
|
this._onNameAppeared();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2016-10-07 19:40:14 +00:00
|
|
|
|
2022-07-08 06:52:18 +00:00
|
|
|
async _onNameAppeared() {
|
|
|
|
try {
|
|
|
|
const [objects] = await this._managerProxy.GetManagedObjectsAsync();
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2017-06-14 20:41:41 +00:00
|
|
|
if (!objects) {
|
2022-07-08 06:52:18 +00:00
|
|
|
this._completeLoad();
|
2017-06-14 20:41:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-08 06:52:18 +00:00
|
|
|
const objectPaths = Object.keys(objects);
|
|
|
|
await Promise.allSettled(objectPaths.flatMap(objectPath => {
|
|
|
|
const object = objects[objectPath];
|
|
|
|
const interfaceNames = Object.getOwnPropertyNames(object);
|
|
|
|
return interfaceNames.map(
|
|
|
|
ifaceName => this._addInterface(objectPath, ifaceName));
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
logError(error, `could not get remote objects for service ${this._serviceName} path ${this._managerPath}`);
|
|
|
|
} finally {
|
|
|
|
this._completeLoad();
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onNameVanished() {
|
2016-10-07 19:40:14 +00:00
|
|
|
let objectPaths = Object.keys(this._objects);
|
|
|
|
for (let i = 0; i < objectPaths.length; i++) {
|
2018-08-31 16:48:00 +00:00
|
|
|
let objectPath = objectPaths[i];
|
|
|
|
let object = this._objects[objectPath];
|
2016-10-07 19:40:14 +00:00
|
|
|
|
|
|
|
let interfaceNames = Object.keys(object);
|
2018-08-31 16:48:00 +00:00
|
|
|
for (let j = 0; j < interfaceNames.length; j++) {
|
|
|
|
let interfaceName = interfaceNames[j];
|
2016-10-07 19:40:14 +00:00
|
|
|
|
|
|
|
if (object[interfaceName])
|
|
|
|
this._removeInterface(objectPath, interfaceName);
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2016-10-07 19:40:14 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_registerInterfaces(interfaces) {
|
2013-05-30 14:15:09 +00:00
|
|
|
for (let i = 0; i < interfaces.length; i++) {
|
|
|
|
let info = Gio.DBusInterfaceInfo.new_for_xml(interfaces[i]);
|
|
|
|
this._interfaceInfos[info.name] = info;
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
getProxy(objectPath, interfaceName) {
|
2013-05-30 14:15:09 +00:00
|
|
|
let object = this._objects[objectPath];
|
|
|
|
|
|
|
|
if (!object)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return object[interfaceName];
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
getProxiesForInterface(interfaceName) {
|
2013-05-30 14:15:09 +00:00
|
|
|
let proxyList = this._interfaces[interfaceName];
|
|
|
|
|
|
|
|
if (!proxyList)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
return proxyList;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:15:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
getAllProxies() {
|
2013-05-30 14:15:09 +00:00
|
|
|
let proxies = [];
|
|
|
|
|
|
|
|
let objectPaths = Object.keys(this._objects);
|
|
|
|
for (let i = 0; i < objectPaths.length; i++) {
|
|
|
|
let object = this._objects[objectPaths];
|
|
|
|
|
|
|
|
let interfaceNames = Object.keys(object);
|
2019-01-31 13:15:58 +00:00
|
|
|
for (let j = 0; j < interfaceNames.length; j++) {
|
|
|
|
let interfaceName = interfaceNames[j];
|
2013-05-30 14:15:09 +00:00
|
|
|
if (object[interfaceName])
|
|
|
|
proxies.push(object(interfaceName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies;
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|