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: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
This commit is contained in:
Florian Müllner 2023-07-10 06:19:53 +02:00
parent 348a8e49fe
commit cd99fbae50

View File

@ -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
*/