2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported init connect disconnect */
|
2009-10-25 22:53:10 +00:00
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Gio, St } = imports.gi;
|
2011-06-11 04:21:35 +00:00
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2012-01-19 00:55:20 +00:00
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
2019-07-07 21:38:27 +00:00
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2012-09-01 12:42:53 +00:00
|
|
|
const Main = imports.ui.main;
|
2011-09-12 18:16:03 +00:00
|
|
|
|
2019-07-07 21:38:27 +00:00
|
|
|
const { ExtensionState, ExtensionType } = ExtensionUtils;
|
2009-10-25 22:53:10 +00:00
|
|
|
|
2011-08-18 11:56:22 +00:00
|
|
|
const ENABLED_EXTENSIONS_KEY = 'enabled-extensions';
|
2018-01-17 12:43:11 +00:00
|
|
|
const DISABLED_EXTENSIONS_KEY = 'disabled-extensions';
|
2015-12-14 22:52:38 +00:00
|
|
|
const DISABLE_USER_EXTENSIONS_KEY = 'disable-user-extensions';
|
2014-02-18 17:10:22 +00:00
|
|
|
const EXTENSION_DISABLE_VERSION_CHECK_KEY = 'disable-extension-version-validation';
|
2011-08-18 11:56:22 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
var ExtensionManager = class {
|
|
|
|
constructor() {
|
|
|
|
this._initted = false;
|
|
|
|
this._enabled = false;
|
2012-09-01 12:42:53 +00:00
|
|
|
|
2019-07-07 22:01:11 +00:00
|
|
|
this._extensions = new Map();
|
2019-03-07 00:45:45 +00:00
|
|
|
this._enabledExtensions = [];
|
2019-08-01 14:32:57 +00:00
|
|
|
this._requestedExtensions = [];
|
2019-03-07 00:45:45 +00:00
|
|
|
this._extensionOrder = [];
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
|
2019-07-07 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2019-03-07 00:45:45 +00:00
|
|
|
this._sessionUpdated();
|
|
|
|
}
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-07-07 22:01:11 +00:00
|
|
|
lookup(uuid) {
|
|
|
|
return this._extensions.get(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUuids() {
|
|
|
|
return [...this._extensions.keys()];
|
2019-07-07 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 00:53:32 +00:00
|
|
|
_callExtensionDisable(uuid) {
|
2019-07-07 22:01:11 +00:00
|
|
|
let extension = this.lookup(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!extension)
|
|
|
|
return;
|
2011-10-14 22:06:03 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (extension.state != ExtensionState.ENABLED)
|
|
|
|
return;
|
2011-10-14 22:06:03 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
// "Rebase" the extension order by disabling and then enabling extensions
|
|
|
|
// in order to help prevent conflicts.
|
|
|
|
|
|
|
|
// Example:
|
|
|
|
// order = [A, B, C, D, E]
|
|
|
|
// user disables C
|
|
|
|
// this should: disable E, disable D, disable C, enable D, enable E
|
|
|
|
|
|
|
|
let orderIdx = this._extensionOrder.indexOf(uuid);
|
|
|
|
let order = this._extensionOrder.slice(orderIdx + 1);
|
|
|
|
let orderReversed = order.slice().reverse();
|
|
|
|
|
|
|
|
for (let i = 0; i < orderReversed.length; i++) {
|
|
|
|
let uuid = orderReversed[i];
|
|
|
|
try {
|
2019-07-07 22:01:11 +00:00
|
|
|
this.lookup(uuid).stateObj.disable();
|
2019-03-07 00:45:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.logExtensionError(uuid, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extension.stylesheet) {
|
|
|
|
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
|
|
|
theme.unload_stylesheet(extension.stylesheet);
|
|
|
|
delete extension.stylesheet;
|
|
|
|
}
|
2011-10-14 22:06:03 +00:00
|
|
|
|
|
|
|
try {
|
2019-03-07 00:45:45 +00:00
|
|
|
extension.stateObj.disable();
|
2019-01-29 01:26:39 +00:00
|
|
|
} catch (e) {
|
2019-03-07 00:45:45 +00:00
|
|
|
this.logExtensionError(uuid, e);
|
2011-10-14 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
for (let i = 0; i < order.length; i++) {
|
|
|
|
let uuid = order[i];
|
|
|
|
try {
|
2019-07-07 22:01:11 +00:00
|
|
|
this.lookup(uuid).stateObj.enable();
|
2019-03-07 00:45:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.logExtensionError(uuid, e);
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 22:42:42 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
this._extensionOrder.splice(orderIdx, 1);
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (extension.state != ExtensionState.ERROR) {
|
|
|
|
extension.state = ExtensionState.DISABLED;
|
|
|
|
this.emit('extension-state-changed', extension);
|
2011-10-14 22:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 00:53:32 +00:00
|
|
|
_callExtensionEnable(uuid) {
|
2019-07-07 22:01:11 +00:00
|
|
|
let extension = this.lookup(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!extension)
|
|
|
|
return;
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (extension.state == ExtensionState.INITIALIZED)
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionInit(uuid);
|
2011-10-25 18:38:25 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (extension.state != ExtensionState.DISABLED)
|
|
|
|
return;
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
this._extensionOrder.push(uuid);
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
let stylesheetNames = [`${global.session_mode}.css`, 'stylesheet.css'];
|
|
|
|
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
|
|
|
for (let i = 0; i < stylesheetNames.length; i++) {
|
|
|
|
try {
|
|
|
|
let stylesheetFile = extension.dir.get_child(stylesheetNames[i]);
|
|
|
|
theme.load_stylesheet(stylesheetFile);
|
|
|
|
extension.stylesheet = stylesheetFile;
|
|
|
|
break;
|
|
|
|
} catch (e) {
|
|
|
|
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
|
|
|
|
continue; // not an error
|
|
|
|
log(`Failed to load stylesheet for extension ${uuid}: ${e.message}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-10-14 22:06:03 +00:00
|
|
|
|
2018-08-03 15:46:27 +00:00
|
|
|
try {
|
2019-03-07 00:45:45 +00:00
|
|
|
extension.stateObj.enable();
|
|
|
|
extension.state = ExtensionState.ENABLED;
|
|
|
|
this.emit('extension-state-changed', extension);
|
|
|
|
return;
|
2018-08-03 15:46:27 +00:00
|
|
|
} catch (e) {
|
2019-03-07 00:45:45 +00:00
|
|
|
if (extension.stylesheet) {
|
|
|
|
theme.unload_stylesheet(extension.stylesheet);
|
|
|
|
delete extension.stylesheet;
|
|
|
|
}
|
|
|
|
this.logExtensionError(uuid, e);
|
2018-08-03 15:46:27 +00:00
|
|
|
return;
|
2013-02-05 17:45:21 +00:00
|
|
|
}
|
2012-06-04 22:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
enableExtension(uuid) {
|
2019-07-07 22:01:11 +00:00
|
|
|
if (!this._extensions.has(uuid))
|
2018-01-17 12:43:11 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
let enabledExtensions = global.settings.get_strv(ENABLED_EXTENSIONS_KEY);
|
2018-01-17 12:43:11 +00:00
|
|
|
let disabledExtensions = global.settings.get_strv(DISABLED_EXTENSIONS_KEY);
|
|
|
|
|
|
|
|
if (disabledExtensions.includes(uuid)) {
|
|
|
|
disabledExtensions = disabledExtensions.filter(item => item !== uuid);
|
|
|
|
global.settings.set_strv(DISABLED_EXTENSIONS_KEY, disabledExtensions);
|
|
|
|
}
|
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
if (!enabledExtensions.includes(uuid)) {
|
|
|
|
enabledExtensions.push(uuid);
|
|
|
|
global.settings.set_strv(ENABLED_EXTENSIONS_KEY, enabledExtensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableExtension(uuid) {
|
2019-07-07 22:01:11 +00:00
|
|
|
if (!this._extensions.has(uuid))
|
2018-01-17 12:43:11 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
let enabledExtensions = global.settings.get_strv(ENABLED_EXTENSIONS_KEY);
|
2018-01-17 12:43:11 +00:00
|
|
|
let disabledExtensions = global.settings.get_strv(DISABLED_EXTENSIONS_KEY);
|
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
if (enabledExtensions.includes(uuid)) {
|
|
|
|
enabledExtensions = enabledExtensions.filter(item => item !== uuid);
|
|
|
|
global.settings.set_strv(ENABLED_EXTENSIONS_KEY, enabledExtensions);
|
|
|
|
}
|
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
if (!disabledExtensions.includes(uuid)) {
|
|
|
|
disabledExtensions.push(uuid);
|
|
|
|
global.settings.set_strv(DISABLED_EXTENSIONS_KEY, disabledExtensions);
|
|
|
|
}
|
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
logExtensionError(uuid, error) {
|
2019-07-07 22:01:11 +00:00
|
|
|
let extension = this.lookup(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!extension)
|
|
|
|
return;
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
let message = `${error}`;
|
2012-06-29 02:00:45 +00:00
|
|
|
|
2019-07-08 10:21:10 +00:00
|
|
|
extension.error = message;
|
2019-03-07 00:45:45 +00:00
|
|
|
extension.state = ExtensionState.ERROR;
|
|
|
|
if (!extension.errors)
|
|
|
|
extension.errors = [];
|
|
|
|
extension.errors.push(message);
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
log('Extension "%s" had error: %s'.format(uuid, message));
|
2019-07-08 10:21:10 +00:00
|
|
|
this.emit('extension-state-changed', extension);
|
2019-03-07 00:45:45 +00:00
|
|
|
}
|
2011-06-23 01:41:30 +00:00
|
|
|
|
2019-07-07 21:38:27 +00:00
|
|
|
createExtensionObject(uuid, dir, type) {
|
|
|
|
let metadataFile = dir.get_child('metadata.json');
|
|
|
|
if (!metadataFile.query_exists(null)) {
|
|
|
|
throw new Error('Missing metadata.json');
|
|
|
|
}
|
|
|
|
|
2019-01-31 14:08:00 +00:00
|
|
|
let metadataContents, success_;
|
2019-07-07 21:38:27 +00:00
|
|
|
try {
|
2019-01-31 14:08:00 +00:00
|
|
|
[success_, metadataContents] = metadataFile.load_contents(null);
|
2019-07-07 21:38:27 +00:00
|
|
|
if (metadataContents instanceof Uint8Array)
|
|
|
|
metadataContents = imports.byteArray.toString(metadataContents);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Failed to load metadata.json: ${e}`);
|
|
|
|
}
|
|
|
|
let meta;
|
|
|
|
try {
|
|
|
|
meta = JSON.parse(metadataContents);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Failed to parse metadata.json: ${e}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let requiredProperties = ['uuid', 'name', 'description', 'shell-version'];
|
|
|
|
for (let i = 0; i < requiredProperties.length; i++) {
|
|
|
|
let prop = requiredProperties[i];
|
|
|
|
if (!meta[prop]) {
|
|
|
|
throw new Error(`missing "${prop}" property in metadata.json`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uuid != meta.uuid) {
|
|
|
|
throw new Error(`uuid "${meta.uuid}" from metadata.json does not match directory name "${uuid}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let extension = {
|
|
|
|
metadata: meta,
|
|
|
|
uuid: meta.uuid,
|
|
|
|
type,
|
|
|
|
dir,
|
|
|
|
path: dir.get_path(),
|
|
|
|
error: '',
|
|
|
|
hasPrefs: dir.get_child('prefs.js').query_exists(null),
|
|
|
|
canChange: false
|
|
|
|
};
|
2019-07-07 22:01:11 +00:00
|
|
|
this._extensions.set(uuid, extension);
|
2019-07-07 21:38:27 +00:00
|
|
|
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
loadExtension(extension) {
|
|
|
|
// Default to error, we set success as the last step
|
|
|
|
extension.state = ExtensionState.ERROR;
|
2009-10-25 22:53:10 +00:00
|
|
|
|
2019-08-01 14:32:57 +00:00
|
|
|
let requested = this._requestedExtensions.includes(extension.uuid);
|
|
|
|
extension.isRequested = requested;
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
let checkVersion = !global.settings.get_boolean(EXTENSION_DISABLE_VERSION_CHECK_KEY);
|
2014-02-18 17:10:22 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (checkVersion && ExtensionUtils.isOutOfDate(extension)) {
|
|
|
|
extension.state = ExtensionState.OUT_OF_DATE;
|
2012-11-26 19:21:03 +00:00
|
|
|
} else {
|
2019-03-07 00:45:45 +00:00
|
|
|
let enabled = this._enabledExtensions.includes(extension.uuid);
|
|
|
|
if (enabled) {
|
2019-07-08 00:53:32 +00:00
|
|
|
if (!this._callExtensionInit(extension.uuid))
|
2019-03-07 00:45:45 +00:00
|
|
|
return;
|
|
|
|
if (extension.state == ExtensionState.DISABLED)
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionEnable(extension.uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
} else {
|
|
|
|
extension.state = ExtensionState.INITIALIZED;
|
|
|
|
}
|
2012-11-26 19:21:03 +00:00
|
|
|
}
|
2019-03-07 00:45:45 +00:00
|
|
|
|
2018-12-04 08:31:27 +00:00
|
|
|
this._updateCanChange(extension);
|
2019-03-07 00:45:45 +00:00
|
|
|
this.emit('extension-state-changed', extension);
|
2011-10-25 18:38:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
unloadExtension(extension) {
|
|
|
|
// Try to disable it -- if it's ERROR'd, we can't guarantee that,
|
|
|
|
// but it will be removed on next reboot, and hopefully nothing
|
|
|
|
// broke too much.
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionDisable(extension.uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
|
|
|
|
extension.state = ExtensionState.UNINSTALLED;
|
|
|
|
this.emit('extension-state-changed', extension);
|
|
|
|
|
2019-07-07 22:01:11 +00:00
|
|
|
this._extensions.delete(extension.uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
return true;
|
2017-05-17 21:51:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
reloadExtension(oldExtension) {
|
|
|
|
// Grab the things we'll need to pass to createExtensionObject
|
|
|
|
// to reload it.
|
|
|
|
let { uuid: uuid, dir: dir, type: type } = oldExtension;
|
2012-08-24 01:37:50 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
// Then unload the old extension.
|
|
|
|
this.unloadExtension(oldExtension);
|
2012-02-08 17:48:05 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
// Now, recreate the extension and load it.
|
|
|
|
let newExtension;
|
|
|
|
try {
|
2019-07-07 21:38:27 +00:00
|
|
|
newExtension = this.createExtensionObject(uuid, dir, type);
|
2019-03-07 00:45:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.logExtensionError(uuid, e);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-08 17:48:05 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
this.loadExtension(newExtension);
|
2017-05-17 22:03:28 +00:00
|
|
|
}
|
2009-10-25 22:53:10 +00:00
|
|
|
|
2019-07-08 00:53:32 +00:00
|
|
|
_callExtensionInit(uuid) {
|
2019-07-07 22:01:11 +00:00
|
|
|
let extension = this.lookup(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
let dir = extension.dir;
|
2012-06-29 02:00:45 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!extension)
|
|
|
|
throw new Error("Extension was not properly created. Call loadExtension first");
|
|
|
|
|
|
|
|
let extensionJs = dir.get_child('extension.js');
|
|
|
|
if (!extensionJs.query_exists(null)) {
|
|
|
|
this.logExtensionError(uuid, new Error('Missing extension.js'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let extensionModule;
|
|
|
|
let extensionState = null;
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
ExtensionUtils.installImporter(extension);
|
2013-10-25 13:28:11 +00:00
|
|
|
try {
|
2019-03-07 00:45:45 +00:00
|
|
|
extensionModule = extension.imports.extension;
|
2019-01-29 01:26:39 +00:00
|
|
|
} catch (e) {
|
2019-03-07 00:45:45 +00:00
|
|
|
this.logExtensionError(uuid, e);
|
2013-10-25 13:28:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-07 00:45:45 +00:00
|
|
|
|
|
|
|
if (extensionModule.init) {
|
|
|
|
try {
|
|
|
|
extensionState = extensionModule.init(extension);
|
|
|
|
} catch (e) {
|
|
|
|
this.logExtensionError(uuid, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!extensionState)
|
|
|
|
extensionState = extensionModule;
|
|
|
|
extension.stateObj = extensionState;
|
|
|
|
|
|
|
|
extension.state = ExtensionState.DISABLED;
|
|
|
|
this.emit('extension-loaded', uuid);
|
|
|
|
return true;
|
2009-10-25 22:53:10 +00:00
|
|
|
}
|
2011-06-23 01:56:24 +00:00
|
|
|
|
2018-12-04 08:31:27 +00:00
|
|
|
_getModeExtensions() {
|
2019-03-07 00:45:45 +00:00
|
|
|
if (Array.isArray(Main.sessionMode.enabledExtensions))
|
2018-12-04 08:31:27 +00:00
|
|
|
return Main.sessionMode.enabledExtensions;
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateCanChange(extension) {
|
|
|
|
let hasError =
|
|
|
|
extension.state == ExtensionState.ERROR ||
|
|
|
|
extension.state == ExtensionState.OUT_OF_DATE;
|
|
|
|
|
|
|
|
let isMode = this._getModeExtensions().includes(extension.uuid);
|
|
|
|
let modeOnly = global.settings.get_boolean(DISABLE_USER_EXTENSIONS_KEY);
|
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
let changeKey = isMode
|
|
|
|
? DISABLE_USER_EXTENSIONS_KEY
|
|
|
|
: ENABLED_EXTENSIONS_KEY;
|
|
|
|
|
2018-12-04 08:31:27 +00:00
|
|
|
extension.canChange =
|
|
|
|
!hasError &&
|
2018-01-17 12:43:11 +00:00
|
|
|
global.settings.is_writable(changeKey) &&
|
2018-12-04 08:31:27 +00:00
|
|
|
(isMode || !modeOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getEnabledExtensions() {
|
|
|
|
let extensions = this._getModeExtensions();
|
2019-03-07 00:45:45 +00:00
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
if (!global.settings.get_boolean(DISABLE_USER_EXTENSIONS_KEY))
|
|
|
|
extensions = extensions.concat(global.settings.get_strv(ENABLED_EXTENSIONS_KEY));
|
2019-03-07 00:45:45 +00:00
|
|
|
|
2018-01-17 12:43:11 +00:00
|
|
|
// filter out 'disabled-extensions' which takes precedence
|
|
|
|
let disabledExtensions = global.settings.get_strv(DISABLED_EXTENSIONS_KEY);
|
|
|
|
return extensions.filter(item => !disabledExtensions.includes(item));
|
2014-02-18 17:10:22 +00:00
|
|
|
}
|
2019-03-07 00:45:45 +00:00
|
|
|
|
2019-08-01 14:32:57 +00:00
|
|
|
_getRequestedExtensions() {
|
|
|
|
let extensions = global.settings.get_strv(ENABLED_EXTENSIONS_KEY);
|
|
|
|
|
|
|
|
// filter out 'disabled-extensions' which takes precedence
|
|
|
|
let disabledExtensions = global.settings.get_strv(DISABLED_EXTENSIONS_KEY);
|
|
|
|
return extensions.filter(item => !disabledExtensions.includes(item));
|
|
|
|
}
|
|
|
|
|
2018-12-04 08:31:27 +00:00
|
|
|
_onUserExtensionsEnabledChanged() {
|
|
|
|
this._onEnabledExtensionsChanged();
|
|
|
|
this._onSettingsWritableChanged();
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
_onEnabledExtensionsChanged() {
|
|
|
|
let newEnabledExtensions = this._getEnabledExtensions();
|
|
|
|
|
|
|
|
if (!this._enabled)
|
|
|
|
return;
|
|
|
|
|
2019-08-01 14:32:57 +00:00
|
|
|
// Updated requested state and emit change notifications
|
|
|
|
this._requestedExtensions = this._getRequestedExtensions();
|
|
|
|
for (let extension of this._extensions.values()) {
|
|
|
|
let requested = this._requestedExtensions.includes(extension.uuid);
|
|
|
|
if (extension.isRequested == requested)
|
|
|
|
continue;
|
|
|
|
extension.isRequested = requested;
|
|
|
|
this.emit('extension-state-changed', extension);
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
// Find and enable all the newly enabled extensions: UUIDs found in the
|
|
|
|
// new setting, but not in the old one.
|
|
|
|
newEnabledExtensions.filter(
|
|
|
|
uuid => !this._enabledExtensions.includes(uuid)
|
|
|
|
).forEach(uuid => {
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionEnable(uuid);
|
2012-09-01 12:42:53 +00:00
|
|
|
});
|
2019-03-07 00:45:45 +00:00
|
|
|
|
|
|
|
// Find and disable all the newly disabled extensions: UUIDs found in the
|
|
|
|
// old setting, but not in the new one.
|
|
|
|
this._enabledExtensions.filter(
|
|
|
|
item => !newEnabledExtensions.includes(item)
|
|
|
|
).forEach(uuid => {
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionDisable(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this._enabledExtensions = newEnabledExtensions;
|
|
|
|
}
|
|
|
|
|
2018-12-04 08:31:27 +00:00
|
|
|
_onSettingsWritableChanged() {
|
2019-07-07 22:01:11 +00:00
|
|
|
for (let extension of this._extensions.values()) {
|
2018-12-04 08:31:27 +00:00
|
|
|
this._updateCanChange(extension);
|
|
|
|
this.emit('extension-state-changed', extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
_onVersionValidationChanged() {
|
|
|
|
// we want to reload all extensions, but only enable
|
|
|
|
// extensions when allowed by the sessionMode, so
|
|
|
|
// temporarily disable them all
|
|
|
|
this._enabledExtensions = [];
|
2019-07-07 22:01:11 +00:00
|
|
|
|
|
|
|
// The loop modifies the extensions map, so iterate over a copy
|
|
|
|
let extensions = [...this._extensions.values()];
|
|
|
|
for (let extension of extensions)
|
|
|
|
this.reloadExtension(extension);
|
2019-03-07 00:45:45 +00:00
|
|
|
this._enabledExtensions = this._getEnabledExtensions();
|
|
|
|
|
|
|
|
if (Main.sessionMode.allowExtensions) {
|
|
|
|
this._enabledExtensions.forEach(uuid => {
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionEnable(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
});
|
|
|
|
}
|
2012-09-01 12:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
_loadExtensions() {
|
|
|
|
global.settings.connect(`changed::${ENABLED_EXTENSIONS_KEY}`,
|
|
|
|
this._onEnabledExtensionsChanged.bind(this));
|
2018-01-17 12:43:11 +00:00
|
|
|
global.settings.connect(`changed::${DISABLED_EXTENSIONS_KEY}`,
|
|
|
|
this._onEnabledExtensionsChanged.bind(this));
|
2019-03-07 00:45:45 +00:00
|
|
|
global.settings.connect(`changed::${DISABLE_USER_EXTENSIONS_KEY}`,
|
2018-12-04 08:31:27 +00:00
|
|
|
this._onUserExtensionsEnabledChanged.bind(this));
|
2019-03-07 00:45:45 +00:00
|
|
|
global.settings.connect(`changed::${EXTENSION_DISABLE_VERSION_CHECK_KEY}`,
|
|
|
|
this._onVersionValidationChanged.bind(this));
|
2018-12-04 08:31:27 +00:00
|
|
|
global.settings.connect(`writable-changed::${ENABLED_EXTENSIONS_KEY}`,
|
|
|
|
this._onSettingsWritableChanged.bind(this));
|
2018-01-17 12:43:11 +00:00
|
|
|
global.settings.connect(`writable-changed::${DISABLED_EXTENSIONS_KEY}`,
|
|
|
|
this._onSettingsWritableChanged.bind(this));
|
2019-03-07 00:45:45 +00:00
|
|
|
|
|
|
|
this._enabledExtensions = this._getEnabledExtensions();
|
2019-08-01 14:32:57 +00:00
|
|
|
this._requestedExtensions = this._getRequestedExtensions();
|
2012-09-01 12:42:53 +00:00
|
|
|
|
2019-07-07 21:38:27 +00:00
|
|
|
let perUserDir = Gio.File.new_for_path(global.userdatadir);
|
|
|
|
FileUtils.collectFromDatadirs('extensions', true, (dir, info) => {
|
|
|
|
let fileType = info.get_file_type();
|
|
|
|
if (fileType != Gio.FileType.DIRECTORY)
|
|
|
|
return;
|
|
|
|
let uuid = info.get_name();
|
2019-07-07 22:01:11 +00:00
|
|
|
let existing = this.lookup(uuid);
|
2019-07-07 21:38:27 +00:00
|
|
|
if (existing) {
|
|
|
|
log(`Extension ${uuid} already installed in ${existing.path}. ${dir.get_path()} will not be loaded`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let extension;
|
|
|
|
let type = dir.has_prefix(perUserDir)
|
|
|
|
? ExtensionType.PER_USER
|
|
|
|
: ExtensionType.SYSTEM;
|
|
|
|
try {
|
|
|
|
extension = this.createExtensionObject(uuid, dir, type);
|
|
|
|
} catch (e) {
|
|
|
|
logError(e, `Could not load extension ${uuid}`);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-07 00:45:45 +00:00
|
|
|
this.loadExtension(extension);
|
2012-09-01 12:42:53 +00:00
|
|
|
});
|
2019-03-07 00:45:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 00:53:32 +00:00
|
|
|
_enableAllExtensions() {
|
2019-03-07 00:45:45 +00:00
|
|
|
if (this._enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!this._initted) {
|
|
|
|
this._loadExtensions();
|
|
|
|
this._initted = true;
|
|
|
|
} else {
|
|
|
|
this._enabledExtensions.forEach(uuid => {
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionEnable(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this._enabled = true;
|
2012-09-01 12:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 00:53:32 +00:00
|
|
|
_disableAllExtensions() {
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!this._enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this._initted) {
|
|
|
|
this._extensionOrder.slice().reverse().forEach(uuid => {
|
2019-07-08 00:53:32 +00:00
|
|
|
this._callExtensionDisable(uuid);
|
2019-03-07 00:45:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._enabled = false;
|
2012-11-29 15:09:21 +00:00
|
|
|
}
|
2012-09-01 12:42:53 +00:00
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
_sessionUpdated() {
|
|
|
|
// For now sessionMode.allowExtensions controls extensions from both the
|
|
|
|
// 'enabled-extensions' preference and the sessionMode.enabledExtensions
|
|
|
|
// property; it might make sense to make enabledExtensions independent
|
|
|
|
// from allowExtensions in the future
|
|
|
|
if (Main.sessionMode.allowExtensions) {
|
2019-08-01 14:32:57 +00:00
|
|
|
if (this._initted) {
|
2019-03-07 00:45:45 +00:00
|
|
|
this._enabledExtensions = this._getEnabledExtensions();
|
2019-08-01 14:32:57 +00:00
|
|
|
}
|
2019-07-08 00:53:32 +00:00
|
|
|
this._enableAllExtensions();
|
2019-03-07 00:45:45 +00:00
|
|
|
} else {
|
2019-07-08 00:53:32 +00:00
|
|
|
this._disableAllExtensions();
|
2019-03-07 00:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Signals.addSignalMethods(ExtensionManager.prototype);
|