From cd99fbae50b7d08a1ce93440d7a93f57cb098155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Mon, 10 Jul 2023 06:19:53 +0200 Subject: [PATCH] extensionBase: Add static lookupByURL() method With convenience API like getSettings() now being provided by the ExtensionObject subclass, extensions will need to access their entry point more often. Having to pass a pointer through the hierarchy can be annoying, so add a static method that allows them to look it up: ```js const ext = Extension.lookupByURL(import.meta.url); this._settings = ext.getSettings(); ``` Part-of: --- js/extensions/sharedInternals.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/js/extensions/sharedInternals.js b/js/extensions/sharedInternals.js index 89906d9da..8ec152332 100644 --- a/js/extensions/sharedInternals.js +++ b/js/extensions/sharedInternals.js @@ -20,6 +20,32 @@ export function setExtensionManager(extensionManager) { export class ExtensionBase { #gettextDomain; + /** + * Look up an extension by URL (usually 'import.meta.url') + * + * @param {string} url - a file:// URL + */ + static lookupByURL(url) { + if (!url.startsWith('file://')) + return null; + + // Keep the last '/' from 'file://' to force an absolute path + let path = url.slice(6); + + // Walk up the directory tree, looking for an extension with + // the same UUID as a directory name. + do { + path = GLib.path_get_dirname(path); + + const dirName = GLib.path_get_basename(path); + const extension = _extensionManager.lookup(dirName); + if (extension !== undefined) + return extension.stateObj; + } while (path !== '/'); + + return null; + } + /** * @param {object} metadata - metadata passed in when loading the extension */