2023-06-30 10:57:41 +00:00
|
|
|
// Common utils for the extension system, the extensions D-Bus service
|
|
|
|
// and the Extensions app
|
2012-01-19 00:55:20 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
2018-11-01 12:55:17 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export const ExtensionType = {
|
2012-01-19 00:55:20 +00:00
|
|
|
SYSTEM: 1,
|
2019-08-20 21:43:54 +00:00
|
|
|
PER_USER: 2,
|
2012-01-19 00:55:20 +00:00
|
|
|
};
|
|
|
|
|
2023-07-30 12:56:59 +00:00
|
|
|
/**
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
2023-07-10 09:53:00 +00:00
|
|
|
export const ExtensionState = {
|
2019-07-06 13:31:57 +00:00
|
|
|
ENABLED: 1,
|
|
|
|
DISABLED: 2,
|
|
|
|
ERROR: 3,
|
|
|
|
OUT_OF_DATE: 4,
|
|
|
|
DOWNLOADING: 5,
|
|
|
|
INITIALIZED: 6,
|
2022-07-25 02:25:23 +00:00
|
|
|
DISABLING: 7,
|
|
|
|
ENABLING: 8,
|
2019-07-06 13:31:57 +00:00
|
|
|
|
|
|
|
// Used as an error state for operations on unknown extensions,
|
|
|
|
// should never be in a real extensionMeta object.
|
2019-08-20 21:43:54 +00:00
|
|
|
UNINSTALLED: 99,
|
2019-07-06 13:31:57 +00:00
|
|
|
};
|
|
|
|
|
2020-01-22 13:45:15 +00:00
|
|
|
const SERIALIZED_PROPERTIES = [
|
|
|
|
'type',
|
|
|
|
'state',
|
|
|
|
'path',
|
|
|
|
'error',
|
|
|
|
'hasPrefs',
|
|
|
|
'hasUpdate',
|
|
|
|
'canChange',
|
|
|
|
];
|
2018-11-01 12:55:17 +00:00
|
|
|
|
2023-07-10 02:43:14 +00:00
|
|
|
/**
|
|
|
|
* Serialize extension into an object that can be used
|
|
|
|
* in a vardict {GLib.Variant}
|
|
|
|
*
|
|
|
|
* @param {object} extension - an extension object
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
2023-07-10 09:53:00 +00:00
|
|
|
export function serializeExtension(extension) {
|
2023-07-10 02:43:14 +00:00
|
|
|
let obj = {...extension.metadata};
|
2018-11-01 12:55:17 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-10 02:43:14 +00:00
|
|
|
/**
|
|
|
|
* Deserialize an unpacked variant into an extension object
|
|
|
|
*
|
|
|
|
* @param {object} variant - an unpacked {GLib.Variant}
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
2023-07-10 09:53:00 +00:00
|
|
|
export function deserializeExtension(variant) {
|
2023-07-10 02:43:14 +00:00
|
|
|
let res = {metadata: {}};
|
2018-11-01 12:55:17 +00:00
|
|
|
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;
|
|
|
|
}
|