2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2019-02-08 22:21:36 -05:00
|
|
|
const { Gio, St } = imports.gi;
|
2011-06-11 00:21:35 -04:00
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2012-01-18 19:55:20 -05:00
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
2012-09-01 08:42:53 -04:00
|
|
|
const Main = imports.ui.main;
|
2011-09-12 14:16:03 -04:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var ExtensionState = {
|
2009-10-25 18:53:10 -04:00
|
|
|
ENABLED: 1,
|
|
|
|
DISABLED: 2,
|
|
|
|
ERROR: 3,
|
2011-06-22 21:56:24 -04:00
|
|
|
OUT_OF_DATE: 4,
|
2011-06-27 22:27:56 -04:00
|
|
|
DOWNLOADING: 5,
|
2011-10-25 14:38:25 -04:00
|
|
|
INITIALIZED: 6,
|
2011-06-22 21:56:24 -04:00
|
|
|
|
|
|
|
// Used as an error state for operations on unknown extensions,
|
|
|
|
// should never be in a real extensionMeta object.
|
|
|
|
UNINSTALLED: 99
|
2009-10-25 18:53:10 -04:00
|
|
|
};
|
|
|
|
|
2011-05-25 15:34:33 -04:00
|
|
|
// Arrays of uuids
|
|
|
|
var enabledExtensions;
|
2012-01-30 20:58:29 -05:00
|
|
|
// Contains the order that extensions were enabled in.
|
2017-07-18 13:47:27 -04:00
|
|
|
var extensionOrder = [];
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2011-06-11 00:21:35 -04:00
|
|
|
// We don't really have a class to add signals on. So, create
|
|
|
|
// a simple dummy object, add the signal methods, and export those
|
|
|
|
// publically.
|
|
|
|
var _signals = {};
|
|
|
|
Signals.addSignalMethods(_signals);
|
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
var connect = _signals.connect.bind(_signals);
|
|
|
|
var disconnect = _signals.disconnect.bind(_signals);
|
2011-06-11 00:21:35 -04:00
|
|
|
|
2011-08-18 07:56:22 -04:00
|
|
|
const ENABLED_EXTENSIONS_KEY = 'enabled-extensions';
|
2015-12-14 17:52:38 -05:00
|
|
|
const DISABLE_USER_EXTENSIONS_KEY = 'disable-user-extensions';
|
2014-02-18 12:10:22 -05:00
|
|
|
const EXTENSION_DISABLE_VERSION_CHECK_KEY = 'disable-extension-version-validation';
|
2011-08-18 07:56:22 -04:00
|
|
|
|
2012-09-01 08:42:53 -04:00
|
|
|
var initted = false;
|
|
|
|
var enabled;
|
|
|
|
|
2011-06-22 21:56:24 -04:00
|
|
|
function disableExtension(uuid) {
|
2012-01-30 20:58:29 -05:00
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
2011-06-22 21:56:24 -04:00
|
|
|
return;
|
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
if (extension.state != ExtensionState.ENABLED)
|
2011-06-22 21:56:24 -04:00
|
|
|
return;
|
|
|
|
|
2011-10-14 18:06:03 -04: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 = extensionOrder.indexOf(uuid);
|
|
|
|
let order = extensionOrder.slice(orderIdx + 1);
|
|
|
|
let orderReversed = order.slice().reverse();
|
|
|
|
|
|
|
|
for (let i = 0; i < orderReversed.length; i++) {
|
|
|
|
let uuid = orderReversed[i];
|
|
|
|
try {
|
2012-02-08 12:48:05 -05:00
|
|
|
ExtensionUtils.extensions[uuid].stateObj.disable();
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2012-06-28 22:00:45 -04:00
|
|
|
logExtensionError(uuid, e);
|
2011-10-14 18:06:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:42:42 -04:00
|
|
|
if (extension.stylesheet) {
|
|
|
|
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
2014-09-18 22:22:02 -04:00
|
|
|
theme.unload_stylesheet(extension.stylesheet);
|
2018-08-03 12:19:14 -04:00
|
|
|
delete extension.stylesheet;
|
2012-06-04 18:42:42 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 09:28:11 -04:00
|
|
|
try {
|
|
|
|
extension.stateObj.disable();
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2013-10-25 09:28:11 -04:00
|
|
|
logExtensionError(uuid, e);
|
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2011-10-14 18:06:03 -04:00
|
|
|
for (let i = 0; i < order.length; i++) {
|
|
|
|
let uuid = order[i];
|
|
|
|
try {
|
2012-02-08 12:48:05 -05:00
|
|
|
ExtensionUtils.extensions[uuid].stateObj.enable();
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2012-06-28 22:00:45 -04:00
|
|
|
logExtensionError(uuid, e);
|
2011-10-14 18:06:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-25 14:43:51 -04:00
|
|
|
extensionOrder.splice(orderIdx, 1);
|
|
|
|
|
2013-10-25 09:28:11 -04:00
|
|
|
if ( extension.state != ExtensionState.ERROR ) {
|
|
|
|
extension.state = ExtensionState.DISABLED;
|
|
|
|
_signals.emit('extension-state-changed', extension);
|
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableExtension(uuid) {
|
2012-01-30 20:58:29 -05:00
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
2011-10-25 14:38:25 -04:00
|
|
|
return;
|
|
|
|
|
2012-02-08 12:48:05 -05:00
|
|
|
if (extension.state == ExtensionState.INITIALIZED)
|
|
|
|
initExtension(uuid);
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
if (extension.state != ExtensionState.DISABLED)
|
2011-06-22 21:56:24 -04:00
|
|
|
return;
|
|
|
|
|
2011-10-14 18:06:03 -04:00
|
|
|
extensionOrder.push(uuid);
|
|
|
|
|
2013-02-05 12:45:21 -05:00
|
|
|
let stylesheetNames = [global.session_mode + '.css', 'stylesheet.css'];
|
2018-08-03 11:46:27 -04:00
|
|
|
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
2013-02-05 12:45:21 -05:00
|
|
|
for (let i = 0; i < stylesheetNames.length; i++) {
|
2018-08-03 11:46:27 -04:00
|
|
|
try {
|
|
|
|
let stylesheetFile = extension.dir.get_child(stylesheetNames[i]);
|
2014-09-18 22:22:02 -04:00
|
|
|
theme.load_stylesheet(stylesheetFile);
|
2013-02-05 12:45:21 -05:00
|
|
|
extension.stylesheet = stylesheetFile;
|
|
|
|
break;
|
2018-08-03 11:46:27 -04:00
|
|
|
} 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;
|
2013-02-05 12:45:21 -05:00
|
|
|
}
|
2012-06-04 18:42:42 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 09:28:11 -04:00
|
|
|
try {
|
|
|
|
extension.stateObj.enable();
|
|
|
|
extension.state = ExtensionState.ENABLED;
|
|
|
|
_signals.emit('extension-state-changed', extension);
|
|
|
|
return;
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2018-08-03 11:52:19 -04:00
|
|
|
if (extension.stylesheet) {
|
|
|
|
theme.unload_stylesheet(extension.stylesheet);
|
|
|
|
delete extension.stylesheet;
|
|
|
|
}
|
2013-10-25 09:28:11 -04:00
|
|
|
logExtensionError(uuid, e);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
}
|
|
|
|
|
2012-06-28 22:00:45 -04:00
|
|
|
function logExtensionError(uuid, error) {
|
2012-01-30 20:58:29 -05:00
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
|
|
|
return;
|
|
|
|
|
2019-01-29 18:01:21 -05:00
|
|
|
let message = `${error}`;
|
2012-06-28 22:00:45 -04:00
|
|
|
|
2012-11-26 14:21:03 -05:00
|
|
|
extension.state = ExtensionState.ERROR;
|
2012-01-30 20:58:29 -05:00
|
|
|
if (!extension.errors)
|
|
|
|
extension.errors = [];
|
2012-12-13 13:32:40 -05:00
|
|
|
extension.errors.push(message);
|
2012-01-30 20:58:29 -05:00
|
|
|
|
2012-05-09 21:37:42 -04:00
|
|
|
log('Extension "%s" had error: %s'.format(uuid, message));
|
2011-06-27 22:21:02 -04:00
|
|
|
_signals.emit('extension-state-changed', { uuid: uuid,
|
|
|
|
error: message,
|
2012-06-28 22:00:45 -04:00
|
|
|
state: extension.state });
|
2011-06-22 21:41:30 -04:00
|
|
|
}
|
|
|
|
|
2012-06-04 18:25:51 -04:00
|
|
|
function loadExtension(extension) {
|
2009-10-25 18:53:10 -04:00
|
|
|
// Default to error, we set success as the last step
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.ERROR;
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2014-02-18 12:10:22 -05:00
|
|
|
let checkVersion = !global.settings.get_boolean(EXTENSION_DISABLE_VERSION_CHECK_KEY);
|
|
|
|
|
|
|
|
if (checkVersion && ExtensionUtils.isOutOfDate(extension)) {
|
2012-11-26 14:21:03 -05:00
|
|
|
extension.state = ExtensionState.OUT_OF_DATE;
|
2012-02-08 12:48:05 -05:00
|
|
|
} else {
|
2018-07-14 16:56:22 -04:00
|
|
|
let enabled = enabledExtensions.includes(extension.uuid);
|
2012-11-26 14:21:03 -05:00
|
|
|
if (enabled) {
|
2013-10-25 09:28:11 -04:00
|
|
|
if (!initExtension(extension.uuid))
|
|
|
|
return;
|
2012-11-26 14:21:03 -05:00
|
|
|
if (extension.state == ExtensionState.DISABLED)
|
|
|
|
enableExtension(extension.uuid);
|
|
|
|
} else {
|
|
|
|
extension.state = ExtensionState.INITIALIZED;
|
|
|
|
}
|
2011-10-25 14:38:25 -04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:48:05 -05:00
|
|
|
_signals.emit('extension-state-changed', extension);
|
|
|
|
}
|
|
|
|
|
2012-08-23 21:36:33 -04:00
|
|
|
function unloadExtension(extension) {
|
2012-06-05 13:03:42 -04:00
|
|
|
// 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.
|
2012-08-23 21:36:33 -04:00
|
|
|
disableExtension(extension.uuid);
|
2012-06-05 13:03:42 -04:00
|
|
|
|
|
|
|
extension.state = ExtensionState.UNINSTALLED;
|
|
|
|
_signals.emit('extension-state-changed', extension);
|
|
|
|
|
2012-08-23 21:36:33 -04:00
|
|
|
delete ExtensionUtils.extensions[extension.uuid];
|
2012-06-05 13:03:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 21:37:50 -04:00
|
|
|
function reloadExtension(oldExtension) {
|
|
|
|
// Grab the things we'll need to pass to createExtensionObject
|
|
|
|
// to reload it.
|
|
|
|
let { uuid: uuid, dir: dir, type: type } = oldExtension;
|
|
|
|
|
|
|
|
// Then unload the old extension.
|
|
|
|
unloadExtension(oldExtension);
|
|
|
|
|
|
|
|
// Now, recreate the extension and load it.
|
2017-05-17 17:51:49 -04:00
|
|
|
let newExtension;
|
|
|
|
try {
|
|
|
|
newExtension = ExtensionUtils.createExtensionObject(uuid, dir, type);
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2017-05-17 17:51:49 -04:00
|
|
|
logExtensionError(uuid, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-23 21:37:50 -04:00
|
|
|
loadExtension(newExtension);
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:48:05 -05:00
|
|
|
function initExtension(uuid) {
|
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
let dir = extension.dir;
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error("Extension was not properly created. Call loadExtension first");
|
|
|
|
|
2009-10-25 18:53:10 -04:00
|
|
|
let extensionJs = dir.get_child('extension.js');
|
2017-05-17 18:03:28 -04:00
|
|
|
if (!extensionJs.query_exists(null)) {
|
|
|
|
logExtensionError(uuid, new Error('Missing extension.js'));
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-25 18:53:10 -04:00
|
|
|
|
|
|
|
let extensionModule;
|
2011-06-22 21:56:24 -04:00
|
|
|
let extensionState = null;
|
2012-06-28 22:00:45 -04:00
|
|
|
|
|
|
|
ExtensionUtils.installImporter(extension);
|
2017-05-17 17:57:18 -04:00
|
|
|
try {
|
|
|
|
extensionModule = extension.imports.extension;
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2017-05-17 17:57:18 -04:00
|
|
|
logExtensionError(uuid, e);
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-06-04 18:43:52 -04:00
|
|
|
if (extensionModule.init) {
|
2013-10-25 09:28:11 -04:00
|
|
|
try {
|
|
|
|
extensionState = extensionModule.init(extension);
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2013-10-25 09:28:11 -04:00
|
|
|
logExtensionError(uuid, e);
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-25 18:53:10 -04:00
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
|
|
|
|
if (!extensionState)
|
|
|
|
extensionState = extensionModule;
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.stateObj = extensionState;
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.DISABLED;
|
|
|
|
_signals.emit('extension-loaded', uuid);
|
2013-10-25 09:28:11 -04:00
|
|
|
return true;
|
2009-10-25 18:53:10 -04:00
|
|
|
}
|
|
|
|
|
2012-11-29 10:09:21 -05:00
|
|
|
function getEnabledExtensions() {
|
2015-12-14 17:52:38 -05:00
|
|
|
let extensions;
|
|
|
|
if (Array.isArray(Main.sessionMode.enabledExtensions))
|
|
|
|
extensions = Main.sessionMode.enabledExtensions;
|
|
|
|
else
|
|
|
|
extensions = [];
|
|
|
|
|
|
|
|
if (global.settings.get_boolean(DISABLE_USER_EXTENSIONS_KEY))
|
2012-11-29 10:09:21 -05:00
|
|
|
return extensions;
|
|
|
|
|
2015-12-14 17:52:38 -05:00
|
|
|
return extensions.concat(global.settings.get_strv(ENABLED_EXTENSIONS_KEY));
|
2012-11-29 10:09:21 -05:00
|
|
|
}
|
|
|
|
|
2011-06-22 21:56:24 -04:00
|
|
|
function onEnabledExtensionsChanged() {
|
2012-11-29 10:09:21 -05:00
|
|
|
let newEnabledExtensions = getEnabledExtensions();
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-09-01 08:42:53 -04:00
|
|
|
if (!enabled)
|
|
|
|
return;
|
|
|
|
|
2011-06-22 21:56:24 -04:00
|
|
|
// Find and enable all the newly enabled extensions: UUIDs found in the
|
|
|
|
// new setting, but not in the old one.
|
2017-10-30 20:38:18 -04:00
|
|
|
newEnabledExtensions.filter(
|
|
|
|
uuid => !enabledExtensions.includes(uuid)
|
|
|
|
).forEach(uuid => {
|
2013-11-04 10:07:44 -05:00
|
|
|
enableExtension(uuid);
|
2011-06-22 21:56:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Find and disable all the newly disabled extensions: UUIDs found in the
|
|
|
|
// old setting, but not in the new one.
|
2017-10-30 20:38:18 -04:00
|
|
|
enabledExtensions.filter(
|
|
|
|
item => !newEnabledExtensions.includes(item)
|
|
|
|
).forEach(uuid => {
|
2013-11-04 10:07:44 -05:00
|
|
|
disableExtension(uuid);
|
2011-06-22 21:56:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
enabledExtensions = newEnabledExtensions;
|
|
|
|
}
|
|
|
|
|
2014-02-18 12:10:22 -05:00
|
|
|
function _onVersionValidationChanged() {
|
2014-09-06 10:30:59 -04:00
|
|
|
// we want to reload all extensions, but only enable
|
|
|
|
// extensions when allowed by the sessionMode, so
|
|
|
|
// temporarily disable them all
|
|
|
|
enabledExtensions = [];
|
|
|
|
for (let uuid in ExtensionUtils.extensions)
|
2017-05-17 17:27:37 -04:00
|
|
|
reloadExtension(ExtensionUtils.extensions[uuid]);
|
2014-09-06 10:30:59 -04:00
|
|
|
enabledExtensions = getEnabledExtensions();
|
|
|
|
|
2014-02-18 12:10:22 -05:00
|
|
|
if (Main.sessionMode.allowExtensions) {
|
2017-10-30 20:38:18 -04:00
|
|
|
enabledExtensions.forEach(uuid => {
|
2017-05-17 17:27:37 -04:00
|
|
|
enableExtension(uuid);
|
2014-02-18 12:10:22 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-01 08:42:53 -04:00
|
|
|
function _loadExtensions() {
|
2011-06-22 21:56:24 -04:00
|
|
|
global.settings.connect('changed::' + ENABLED_EXTENSIONS_KEY, onEnabledExtensionsChanged);
|
2015-12-14 17:52:38 -05:00
|
|
|
global.settings.connect('changed::' + DISABLE_USER_EXTENSIONS_KEY, onEnabledExtensionsChanged);
|
2014-02-18 12:10:22 -05:00
|
|
|
global.settings.connect('changed::' + EXTENSION_DISABLE_VERSION_CHECK_KEY, _onVersionValidationChanged);
|
|
|
|
|
2012-11-29 10:09:21 -05:00
|
|
|
enabledExtensions = getEnabledExtensions();
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2012-06-04 17:14:18 -04:00
|
|
|
let finder = new ExtensionUtils.ExtensionFinder();
|
2017-10-30 20:38:18 -04:00
|
|
|
finder.connect('extension-found', (finder, extension) => {
|
2013-10-25 09:28:11 -04:00
|
|
|
loadExtension(extension);
|
2012-01-18 19:55:20 -05:00
|
|
|
});
|
2012-06-04 17:14:18 -04:00
|
|
|
finder.scanExtensions();
|
2009-10-25 18:53:10 -04:00
|
|
|
}
|
2012-09-01 08:42:53 -04:00
|
|
|
|
|
|
|
function enableAllExtensions() {
|
|
|
|
if (enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!initted) {
|
|
|
|
_loadExtensions();
|
|
|
|
initted = true;
|
|
|
|
} else {
|
2017-10-30 20:38:18 -04:00
|
|
|
enabledExtensions.forEach(uuid => {
|
2012-09-01 08:42:53 -04:00
|
|
|
enableExtension(uuid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function disableAllExtensions() {
|
|
|
|
if (!enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (initted) {
|
2017-10-30 20:38:18 -04:00
|
|
|
extensionOrder.slice().reverse().forEach(uuid => {
|
2012-09-01 08:42:53 -04:00
|
|
|
disableExtension(uuid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _sessionUpdated() {
|
2012-11-29 10:09:21 -05:00
|
|
|
// 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) {
|
|
|
|
if (initted)
|
2015-12-14 21:51:50 -05:00
|
|
|
enabledExtensions = getEnabledExtensions();
|
2012-09-01 08:42:53 -04:00
|
|
|
enableAllExtensions();
|
2012-11-29 10:09:21 -05:00
|
|
|
} else {
|
2012-09-01 08:42:53 -04:00
|
|
|
disableAllExtensions();
|
2012-11-29 10:09:21 -05:00
|
|
|
}
|
2012-09-01 08:42:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
Main.sessionMode.connect('updated', _sessionUpdated);
|
|
|
|
_sessionUpdated();
|
|
|
|
}
|