gnome-shell/js/misc/extensionUtils.js
Florian Müllner d3f662fbf2 extensionUtils: Split out extension convenience functions
ExtensionUtils was originally used for shared functions between
the extension system and the (old) prefs-tool, but then gained
useful API meant for extensions themselves.

It's a bit weird to mix the two, so split out the extension convenience
API into a separate module.

We will soon split up the module further, and add specific "flavors"
for extensions and preferences, with the current code providing a
shared base for both.

That should explain both the new location and the odd module name. :-)

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2837>
2023-07-15 13:16:42 +02:00

97 lines
2.1 KiB
JavaScript

/* exported ExtensionState, ExtensionType,
* serializeExtension, deserializeExtension */
// Common utils for the extension system, the extensions D-Bus service
// and the Extensions app
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
var ExtensionType = {
SYSTEM: 1,
PER_USER: 2,
};
var ExtensionState = {
ENABLED: 1,
DISABLED: 2,
ERROR: 3,
OUT_OF_DATE: 4,
DOWNLOADING: 5,
INITIALIZED: 6,
DISABLING: 7,
ENABLING: 8,
// Used as an error state for operations on unknown extensions,
// should never be in a real extensionMeta object.
UNINSTALLED: 99,
};
const SERIALIZED_PROPERTIES = [
'type',
'state',
'path',
'error',
'hasPrefs',
'hasUpdate',
'canChange',
];
/**
* Serialize extension into an object that can be used
* in a vardict {GLib.Variant}
*
* @param {object} extension - an extension object
* @returns {object}
*/
function serializeExtension(extension) {
let obj = {...extension.metadata};
SERIALIZED_PROPERTIES.forEach(prop => {
obj[prop] = extension[prop];
});
let res = {};
for (let key in obj) {
let val = obj[key];
let type;
switch (typeof val) {
case 'string':
type = 's';
break;
case 'number':
type = 'd';
break;
case 'boolean':
type = 'b';
break;
default:
continue;
}
res[key] = GLib.Variant.new(type, val);
}
return res;
}
/**
* Deserialize an unpacked variant into an extension object
*
* @param {object} variant - an unpacked {GLib.Variant}
* @returns {object}
*/
function deserializeExtension(variant) {
let res = {metadata: {}};
for (let prop in variant) {
let val = variant[prop].unpack();
if (SERIALIZED_PROPERTIES.includes(prop))
res[prop] = val;
else
res.metadata[prop] = val;
}
// add the 2 additional properties to create a valid extension object, as createExtensionObject()
res.uuid = res.metadata.uuid;
res.dir = Gio.File.new_for_path(res.path);
return res;
}