2013-05-30 10:15:09 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Params = imports.misc.params;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
|
|
|
// Specified in the D-Bus specification here:
|
|
|
|
// http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager
|
2013-10-24 17:51:58 -04: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 10:15:09 -04:00
|
|
|
|
|
|
|
const ObjectManagerInfo = Gio.DBusInterfaceInfo.new_for_xml(ObjectManagerIface);
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var ObjectManager = new Lang.Class({
|
2013-05-30 10:15:09 -04:00
|
|
|
Name: 'ObjectManager',
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(params) {
|
2013-05-30 10:15:09 -04:00
|
|
|
params = Params.parse(params, { connection: null,
|
|
|
|
name: null,
|
|
|
|
objectPath: null,
|
|
|
|
knownInterfaces: null,
|
|
|
|
cancellable: null,
|
|
|
|
onLoaded: null });
|
|
|
|
|
|
|
|
this._connection = params.connection;
|
|
|
|
this._serviceName = params.name;
|
|
|
|
this._managerPath = params.objectPath;
|
|
|
|
this._cancellable = params.cancellable;
|
|
|
|
|
|
|
|
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,
|
2016-10-07 15:40:14 -04:00
|
|
|
g_flags: Gio.DBusProxyFlags.DO_NOT_AUTO_START });
|
2013-05-30 10:15:09 -04:00
|
|
|
|
|
|
|
this._interfaceInfos = {};
|
|
|
|
this._objects = {};
|
|
|
|
this._interfaces = {};
|
|
|
|
this._onLoaded = params.onLoaded;
|
|
|
|
|
|
|
|
if (params.knownInterfaces)
|
|
|
|
this._registerInterfaces(params.knownInterfaces);
|
|
|
|
|
|
|
|
// Start out inhibiting load until at least the proxy
|
|
|
|
// manager is loaded and the remote objects are fetched
|
|
|
|
this._numLoadInhibitors = 1;
|
|
|
|
this._managerProxy.init_async(GLib.PRIORITY_DEFAULT,
|
|
|
|
this._cancellable,
|
2017-12-01 19:27:35 -05:00
|
|
|
this._onManagerProxyLoaded.bind(this));
|
2013-05-30 10:15:09 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_tryToCompleteLoad() {
|
2016-10-07 15:40:14 -04:00
|
|
|
if (this._numLoadInhibitors == 0)
|
|
|
|
return;
|
|
|
|
|
2013-05-30 10:15:09 -04:00
|
|
|
this._numLoadInhibitors--;
|
|
|
|
if (this._numLoadInhibitors == 0) {
|
|
|
|
if (this._onLoaded)
|
|
|
|
this._onLoaded();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_addInterface(objectPath, interfaceName, onFinished) {
|
2013-08-22 09:40:12 -04:00
|
|
|
let info = this._interfaceInfos[interfaceName];
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
if (onFinished)
|
|
|
|
onFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let proxy = new Gio.DBusProxy({ g_connection: this._connection,
|
|
|
|
g_name: this._serviceName,
|
|
|
|
g_object_path: objectPath,
|
|
|
|
g_interface_name: interfaceName,
|
|
|
|
g_interface_info: info,
|
2016-10-07 15:40:14 -04:00
|
|
|
g_flags: Gio.DBusProxyFlags.DO_NOT_AUTO_START });
|
2013-08-22 09:40:12 -04:00
|
|
|
|
|
|
|
proxy.init_async(GLib.PRIORITY_DEFAULT,
|
|
|
|
this._cancellable,
|
2017-10-30 20:38:18 -04:00
|
|
|
(initable, result) => {
|
2013-05-30 10:15:09 -04:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
initable.init_finish(result);
|
|
|
|
} catch(e) {
|
|
|
|
logError(e, 'could not initialize proxy for interface ' + interfaceName);
|
|
|
|
|
|
|
|
if (onFinished)
|
|
|
|
onFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let isNewObject;
|
|
|
|
if (!this._objects[objectPath]) {
|
|
|
|
this._objects[objectPath] = {};
|
|
|
|
isNewObject = true;
|
|
|
|
} else {
|
|
|
|
isNewObject = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._objects[objectPath][interfaceName] = proxy;
|
|
|
|
|
|
|
|
if (!this._interfaces[interfaceName])
|
|
|
|
this._interfaces[interfaceName] = [];
|
|
|
|
|
|
|
|
this._interfaces[interfaceName].push(proxy);
|
|
|
|
|
|
|
|
if (isNewObject)
|
|
|
|
this.emit('object-added', objectPath);
|
|
|
|
|
|
|
|
this.emit('interface-added', interfaceName, proxy);
|
|
|
|
|
|
|
|
if (onFinished)
|
|
|
|
onFinished();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-05-30 10:15:09 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_removeInterface(objectPath, interfaceName) {
|
2013-05-30 10:15:09 -04: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);
|
|
|
|
|
|
|
|
if (this._interfaces[interfaceName].length == 0)
|
|
|
|
delete this._interfaces[interfaceName];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('interface-removed', interfaceName, proxy);
|
|
|
|
|
|
|
|
this._objects[objectPath][interfaceName] = null;
|
|
|
|
|
|
|
|
if (Object.keys(this._objects[objectPath]).length == 0) {
|
|
|
|
delete this._objects[objectPath];
|
|
|
|
this.emit('object-removed', objectPath);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onManagerProxyLoaded(initable, result) {
|
2013-05-30 10:15:09 -04:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
initable.init_finish(result);
|
|
|
|
} catch(e) {
|
|
|
|
logError(e, 'could not initialize object manager for object ' + params.name);
|
|
|
|
|
|
|
|
this._tryToCompleteLoad();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._managerProxy.connectSignal('InterfacesAdded',
|
2017-10-30 20:38:18 -04:00
|
|
|
(objectManager, sender, [objectPath, interfaces]) => {
|
2013-05-30 10:15:09 -04:00
|
|
|
let interfaceNames = Object.keys(interfaces);
|
|
|
|
for (let i = 0; i < interfaceNames.length; i++)
|
|
|
|
this._addInterface(objectPath, interfaceNames[i]);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-05-30 10:15:09 -04:00
|
|
|
this._managerProxy.connectSignal('InterfacesRemoved',
|
2017-10-30 20:38:18 -04:00
|
|
|
(objectManager, sender, [objectPath, interfaceNames]) => {
|
2013-05-30 10:15:09 -04:00
|
|
|
for (let i = 0; i < interfaceNames.length; i++)
|
|
|
|
this._removeInterface(objectPath, interfaceNames[i]);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-05-30 10:15:09 -04:00
|
|
|
|
|
|
|
if (Object.keys(this._interfaceInfos).length == 0) {
|
|
|
|
this._tryToCompleteLoad();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._managerProxy.connect('notify::g-name-owner', () => {
|
2016-10-07 15:40:14 -04:00
|
|
|
if (this._managerProxy.g_name_owner)
|
|
|
|
this._onNameAppeared();
|
|
|
|
else
|
|
|
|
this._onNameVanished();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2016-10-07 15:40:14 -04:00
|
|
|
|
|
|
|
if (this._managerProxy.g_name_owner)
|
|
|
|
this._onNameAppeared();
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onNameAppeared() {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._managerProxy.GetManagedObjectsRemote((result, error) => {
|
2013-05-30 10:15:09 -04:00
|
|
|
if (!result) {
|
|
|
|
if (error) {
|
|
|
|
logError(error, 'could not get remote objects for service ' + this._serviceName + ' path ' + this._managerPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tryToCompleteLoad();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [objects] = result;
|
|
|
|
|
2017-06-14 16:41:41 -04:00
|
|
|
if (!objects) {
|
|
|
|
this._tryToCompleteLoad();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-30 10:15:09 -04:00
|
|
|
let objectPaths = Object.keys(objects);
|
|
|
|
for (let i = 0; i < objectPaths.length; i++) {
|
|
|
|
let objectPath = objectPaths[i];
|
|
|
|
let object = objects[objectPath];
|
|
|
|
|
|
|
|
let interfaceNames = Object.getOwnPropertyNames(object);
|
|
|
|
for (let j = 0; j < interfaceNames.length; j++) {
|
|
|
|
let interfaceName = interfaceNames[j];
|
|
|
|
|
|
|
|
// Prevent load from completing until the interface is loaded
|
|
|
|
this._numLoadInhibitors++;
|
|
|
|
this._addInterface(objectPath,
|
|
|
|
interfaceName,
|
2017-12-01 19:27:35 -05:00
|
|
|
this._tryToCompleteLoad.bind(this));
|
2013-05-30 10:15:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._tryToCompleteLoad();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-05-30 10:15:09 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onNameVanished() {
|
2016-10-07 15:40:14 -04:00
|
|
|
let objectPaths = Object.keys(this._objects);
|
|
|
|
for (let i = 0; i < objectPaths.length; i++) {
|
2018-08-31 12:48:00 -04:00
|
|
|
let objectPath = objectPaths[i];
|
|
|
|
let object = this._objects[objectPath];
|
2016-10-07 15:40:14 -04:00
|
|
|
|
|
|
|
let interfaceNames = Object.keys(object);
|
|
|
|
for (let j = 0; i < interfaceNames.length; i++) {
|
|
|
|
let interfaceName = interfaceNames[i];
|
|
|
|
|
|
|
|
if (object[interfaceName])
|
|
|
|
this._removeInterface(objectPath, interfaceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_registerInterfaces(interfaces) {
|
2013-05-30 10:15:09 -04: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-30 20:03:21 -04:00
|
|
|
getProxy(objectPath, interfaceName) {
|
2013-05-30 10:15:09 -04:00
|
|
|
let object = this._objects[objectPath];
|
|
|
|
|
|
|
|
if (!object)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return object[interfaceName];
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
getProxiesForInterface(interfaceName) {
|
2013-05-30 10:15:09 -04:00
|
|
|
let proxyList = this._interfaces[interfaceName];
|
|
|
|
|
|
|
|
if (!proxyList)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
return proxyList;
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
getAllProxies() {
|
2013-05-30 10:15:09 -04: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);
|
|
|
|
for (let j = 0; i < interfaceNames.length; i++) {
|
|
|
|
let interfaceName = interfaceNames[i];
|
|
|
|
if (object[interfaceName])
|
|
|
|
proxies.push(object(interfaceName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Signals.addSignalMethods(ObjectManager.prototype);
|