dbusServices: Refactor service utilities from fileUtils into dbusUtils

To enable porting services to ECMAScript modules independently of
the shell, split DBus service utility functions into a new file,
dbusUtils.js

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2365>
This commit is contained in:
Evan Welsh 2022-07-05 03:25:02 -04:00
parent a88e59c1a8
commit 83c08e17cf
12 changed files with 80 additions and 60 deletions

View File

@ -5,7 +5,7 @@ const { Gio, GLib, Shew } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const { loadInterfaceXML } = imports.misc.fileUtils;
const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ExtensionPrefsDialog } = imports.extensionPrefsDialog;
const { ServiceImplementation } = imports.dbusService;

View File

@ -3,7 +3,7 @@
const { Gio, GLib } = imports.gi;
const { loadInterfaceXML } = imports.misc.fileUtils;
const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ServiceImplementation } = imports.dbusService;
const NotificationsIface = loadInterfaceXML('org.freedesktop.Notifications');

View File

@ -6,6 +6,6 @@
<file>dbusService.js</file>
<file>misc/config.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dbusUtils.js</file>
</gresource>
</gresources>

View File

@ -8,7 +8,7 @@
<file>misc/config.js</file>
<file>misc/extensionUtils.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dbusUtils.js</file>
<file>misc/params.js</file>
</gresource>

View File

@ -6,6 +6,6 @@
<file>dbusService.js</file>
<file>misc/config.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dbusUtils.js</file>
</gresource>
</gresources>

View File

@ -6,6 +6,6 @@
<file>dbusService.js</file>
<file>misc/config.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dbusUtils.js</file>
</gresource>
</gresources>

View File

@ -6,7 +6,7 @@ imports.gi.versions.Gtk = '4.0';
const { Gio, GLib, Gst, Gtk } = imports.gi;
const { loadInterfaceXML, loadSubInterfaceXML } = imports.misc.fileUtils;
const { loadInterfaceXML, loadSubInterfaceXML } = imports.misc.dbusUtils;
const { ServiceImplementation } = imports.dbusService;
const ScreencastIface = loadInterfaceXML('org.gnome.Shell.Screencast');

View File

@ -3,7 +3,7 @@
const { Gio, GLib } = imports.gi;
const { loadInterfaceXML } = imports.misc.fileUtils;
const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ServiceImplementation } = imports.dbusService;
const ScreenSaverIface = loadInterfaceXML('org.gnome.ScreenSaver');

View File

@ -14,6 +14,7 @@
<file>misc/config.js</file>
<file>misc/extensionUtils.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dbusUtils.js</file>
<file>misc/gnomeSession.js</file>
<file>misc/history.js</file>
<file>misc/ibusManager.js</file>

68
js/misc/dbusUtils.js Normal file
View File

@ -0,0 +1,68 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported loadInterfaceXML, loadSubInterfaceXML */
const Config = imports.misc.config;
const { Gio, GLib } = imports.gi;
let _ifaceResource = null;
/**
* @private
*/
function _ensureIfaceResource() {
if (_ifaceResource)
return;
// don't use global.datadir so the method is usable from tests/tools
let dir = GLib.getenv('GNOME_SHELL_DATADIR') || Config.PKGDATADIR;
let path = `${dir}/gnome-shell-dbus-interfaces.gresource`;
_ifaceResource = Gio.Resource.load(path);
_ifaceResource._register();
}
/**
* @param {string} iface the interface name
* @returns {string | null} the XML string or null if it is not found
*/
function loadInterfaceXML(iface) {
_ensureIfaceResource();
let uri = `resource:///org/gnome/shell/dbus-interfaces/${iface}.xml`;
let f = Gio.File.new_for_uri(uri);
try {
let [ok_, bytes] = f.load_contents(null);
return new TextDecoder().decode(bytes);
} catch (e) {
log(`Failed to load D-Bus interface ${iface}`);
}
return null;
}
/**
* @param {string} iface the interface name
* @param {string} ifaceFile the interface filename
* @returns {string | null} the XML string or null if it is not found
*/
function loadSubInterfaceXML(iface, ifaceFile) {
let xml = loadInterfaceXML(ifaceFile);
if (!xml)
return null;
let ifaceStartTag = `<interface name="${iface}">`;
let ifaceStopTag = '</interface>';
let ifaceStartIndex = xml.indexOf(ifaceStartTag);
let ifaceEndIndex = xml.indexOf(ifaceStopTag, ifaceStartIndex + 1) + ifaceStopTag.length;
let xmlHeader = '<!DOCTYPE node PUBLIC\n' +
'\'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\'\n' +
'\'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\'>\n' +
'<node>\n';
let xmlFooter = '</node>';
return (
xmlHeader +
xml.substr(ifaceStartIndex, ifaceEndIndex - ifaceStartIndex) +
xmlFooter);
}

View File

@ -3,7 +3,8 @@
recursivelyMoveDir, loadInterfaceXML, loadSubInterfaceXML */
const { Gio, GLib } = imports.gi;
const Config = imports.misc.config;
var { loadInterfaceXML } = imports.misc.dbusUtils;
function collectFromDatadirs(subdir, includeUserDir, processFile) {
let dataDirs = GLib.get_system_data_dirs();
@ -65,53 +66,3 @@ function recursivelyMoveDir(srcDir, destDir) {
recursivelyMoveDir(srcChild, destChild);
}
}
let _ifaceResource = null;
function ensureIfaceResource() {
if (_ifaceResource)
return;
// don't use global.datadir so the method is usable from tests/tools
let dir = GLib.getenv('GNOME_SHELL_DATADIR') || Config.PKGDATADIR;
let path = `${dir}/gnome-shell-dbus-interfaces.gresource`;
_ifaceResource = Gio.Resource.load(path);
_ifaceResource._register();
}
function loadInterfaceXML(iface) {
ensureIfaceResource();
let uri = `resource:///org/gnome/shell/dbus-interfaces/${iface}.xml`;
let f = Gio.File.new_for_uri(uri);
try {
let [ok_, bytes] = f.load_contents(null);
return new TextDecoder().decode(bytes);
} catch (e) {
log(`Failed to load D-Bus interface ${iface}`);
}
return null;
}
function loadSubInterfaceXML(iface, ifaceFile) {
let xml = loadInterfaceXML(ifaceFile);
if (!xml)
return null;
let ifaceStartTag = `<interface name="${iface}">`;
let ifaceStopTag = '</interface>';
let ifaceStartIndex = xml.indexOf(ifaceStartTag);
let ifaceEndIndex = xml.indexOf(ifaceStopTag, ifaceStartIndex + 1) + ifaceStopTag.length;
let xmlHeader = '<!DOCTYPE node PUBLIC\n' +
'\'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\'\n' +
'\'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\'>\n' +
'<node>\n';
let xmlFooter = '</node>';
return (
xmlHeader +
xml.substr(ifaceStartIndex, ifaceEndIndex - ifaceStartIndex) +
xmlFooter);
}

View File

@ -4,6 +4,6 @@
<file>portalHelper/main.js</file>
<file>misc/config.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dbusUtils.js</file>
</gresource>
</gresources>