From 83c08e17cff9173720f4a64219f87b264e1c162d Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Tue, 5 Jul 2022 03:25:02 -0400 Subject: [PATCH] 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: --- .../extensions/extensionsService.js | 2 +- .../notifications/notificationDaemon.js | 2 +- .../org.gnome.ScreenSaver.src.gresource.xml | 2 +- ...g.gnome.Shell.Extensions.src.gresource.xml | 2 +- ...nome.Shell.Notifications.src.gresource.xml | 2 +- ...g.gnome.Shell.Screencast.src.gresource.xml | 2 +- .../screencast/screencastService.js | 2 +- .../screensaver/screenSaverService.js | 2 +- js/js-resources.gresource.xml | 1 + js/misc/dbusUtils.js | 68 +++++++++++++++++++ js/misc/fileUtils.js | 53 +-------------- js/portal-resources.gresource.xml | 2 +- 12 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 js/misc/dbusUtils.js diff --git a/js/dbusServices/extensions/extensionsService.js b/js/dbusServices/extensions/extensionsService.js index 6144dbab4..f863489a7 100644 --- a/js/dbusServices/extensions/extensionsService.js +++ b/js/dbusServices/extensions/extensionsService.js @@ -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; diff --git a/js/dbusServices/notifications/notificationDaemon.js b/js/dbusServices/notifications/notificationDaemon.js index d0c97860c..10f0ddacf 100644 --- a/js/dbusServices/notifications/notificationDaemon.js +++ b/js/dbusServices/notifications/notificationDaemon.js @@ -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'); diff --git a/js/dbusServices/org.gnome.ScreenSaver.src.gresource.xml b/js/dbusServices/org.gnome.ScreenSaver.src.gresource.xml index 834167b95..d77f72a68 100644 --- a/js/dbusServices/org.gnome.ScreenSaver.src.gresource.xml +++ b/js/dbusServices/org.gnome.ScreenSaver.src.gresource.xml @@ -6,6 +6,6 @@ dbusService.js misc/config.js - misc/fileUtils.js + misc/dbusUtils.js diff --git a/js/dbusServices/org.gnome.Shell.Extensions.src.gresource.xml b/js/dbusServices/org.gnome.Shell.Extensions.src.gresource.xml index c3b81cfc2..3ab92a20e 100644 --- a/js/dbusServices/org.gnome.Shell.Extensions.src.gresource.xml +++ b/js/dbusServices/org.gnome.Shell.Extensions.src.gresource.xml @@ -8,7 +8,7 @@ misc/config.js misc/extensionUtils.js - misc/fileUtils.js + misc/dbusUtils.js misc/params.js diff --git a/js/dbusServices/org.gnome.Shell.Notifications.src.gresource.xml b/js/dbusServices/org.gnome.Shell.Notifications.src.gresource.xml index ccf0e98a7..4e039ba69 100644 --- a/js/dbusServices/org.gnome.Shell.Notifications.src.gresource.xml +++ b/js/dbusServices/org.gnome.Shell.Notifications.src.gresource.xml @@ -6,6 +6,6 @@ dbusService.js misc/config.js - misc/fileUtils.js + misc/dbusUtils.js diff --git a/js/dbusServices/org.gnome.Shell.Screencast.src.gresource.xml b/js/dbusServices/org.gnome.Shell.Screencast.src.gresource.xml index e99b1ac18..292f0f160 100644 --- a/js/dbusServices/org.gnome.Shell.Screencast.src.gresource.xml +++ b/js/dbusServices/org.gnome.Shell.Screencast.src.gresource.xml @@ -6,6 +6,6 @@ dbusService.js misc/config.js - misc/fileUtils.js + misc/dbusUtils.js diff --git a/js/dbusServices/screencast/screencastService.js b/js/dbusServices/screencast/screencastService.js index 1fdf5c766..73f6b5595 100644 --- a/js/dbusServices/screencast/screencastService.js +++ b/js/dbusServices/screencast/screencastService.js @@ -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'); diff --git a/js/dbusServices/screensaver/screenSaverService.js b/js/dbusServices/screensaver/screenSaverService.js index bd3687ef6..dbf2a82fb 100644 --- a/js/dbusServices/screensaver/screenSaverService.js +++ b/js/dbusServices/screensaver/screenSaverService.js @@ -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'); diff --git a/js/js-resources.gresource.xml b/js/js-resources.gresource.xml index fc5af0aee..642ad05c3 100644 --- a/js/js-resources.gresource.xml +++ b/js/js-resources.gresource.xml @@ -14,6 +14,7 @@ misc/config.js misc/extensionUtils.js misc/fileUtils.js + misc/dbusUtils.js misc/gnomeSession.js misc/history.js misc/ibusManager.js diff --git a/js/misc/dbusUtils.js b/js/misc/dbusUtils.js new file mode 100644 index 000000000..ac26894eb --- /dev/null +++ b/js/misc/dbusUtils.js @@ -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 = ``; + let ifaceStopTag = ''; + let ifaceStartIndex = xml.indexOf(ifaceStartTag); + let ifaceEndIndex = xml.indexOf(ifaceStopTag, ifaceStartIndex + 1) + ifaceStopTag.length; + + let xmlHeader = '\n' + + '\n'; + let xmlFooter = ''; + + return ( + xmlHeader + + xml.substr(ifaceStartIndex, ifaceEndIndex - ifaceStartIndex) + + xmlFooter); +} diff --git a/js/misc/fileUtils.js b/js/misc/fileUtils.js index 88f043a79..4c86948a9 100644 --- a/js/misc/fileUtils.js +++ b/js/misc/fileUtils.js @@ -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 = ``; - let ifaceStopTag = ''; - let ifaceStartIndex = xml.indexOf(ifaceStartTag); - let ifaceEndIndex = xml.indexOf(ifaceStopTag, ifaceStartIndex + 1) + ifaceStopTag.length; - - let xmlHeader = '\n' + - '\n'; - let xmlFooter = ''; - - return ( - xmlHeader + - xml.substr(ifaceStartIndex, ifaceEndIndex - ifaceStartIndex) + - xmlFooter); -} diff --git a/js/portal-resources.gresource.xml b/js/portal-resources.gresource.xml index 3fd878365..dbcf11b1d 100644 --- a/js/portal-resources.gresource.xml +++ b/js/portal-resources.gresource.xml @@ -4,6 +4,6 @@ portalHelper/main.js misc/config.js - misc/fileUtils.js + misc/dbusUtils.js