extensionSystem: Turn into a class
The extension system started out as a set of simple functions, but gained more state later, and even some hacks to emit signals without having an object to emit them on. There is no good reason for that weirdness, so rather than imitating an object, wrap the existing system into a real ExtensionManager object. https://bugzilla.gnome.org/show_bug.cgi?id=789852
This commit is contained in:
parent
d82810240f
commit
ea17740719
@ -7,6 +7,7 @@ const Dialog = imports.ui.dialog;
|
||||
const ExtensionUtils = imports.misc.extensionUtils;
|
||||
const ExtensionSystem = imports.ui.extensionSystem;
|
||||
const FileUtils = imports.misc.fileUtils;
|
||||
const Main = imports.ui.main;
|
||||
const ModalDialog = imports.ui.modalDialog;
|
||||
|
||||
var REPOSITORY_URL_BASE = 'https://extensions.gnome.org';
|
||||
@ -24,7 +25,7 @@ function installExtension(uuid, invocation) {
|
||||
|
||||
_httpSession.queue_message(message, (session, message) => {
|
||||
if (message.status_code != Soup.KnownStatusCode.OK) {
|
||||
ExtensionSystem.logExtensionError(uuid, `downloading info: ${message.status_code}`);
|
||||
Main.extensionManager.logExtensionError(uuid, `downloading info: ${message.status_code}`);
|
||||
invocation.return_dbus_error('org.gnome.Shell.DownloadInfoError', message.status_code.toString());
|
||||
return;
|
||||
}
|
||||
@ -33,7 +34,7 @@ function installExtension(uuid, invocation) {
|
||||
try {
|
||||
info = JSON.parse(message.response_body.data);
|
||||
} catch (e) {
|
||||
ExtensionSystem.logExtensionError(uuid, `parsing info: ${e}`);
|
||||
Main.extensionManager.logExtensionError(uuid, `parsing info: ${e}`);
|
||||
invocation.return_dbus_error('org.gnome.Shell.ParseInfoError', e.toString());
|
||||
return;
|
||||
}
|
||||
@ -52,7 +53,7 @@ function uninstallExtension(uuid) {
|
||||
if (extension.type != ExtensionUtils.ExtensionType.PER_USER)
|
||||
return false;
|
||||
|
||||
if (!ExtensionSystem.unloadExtension(extension))
|
||||
if (!Main.extensionManager.unloadExtension(extension))
|
||||
return false;
|
||||
|
||||
FileUtils.recursivelyDeleteDir(extension.dir, true);
|
||||
@ -116,7 +117,7 @@ function updateExtension(uuid) {
|
||||
let oldExtension = ExtensionUtils.extensions[uuid];
|
||||
let extensionDir = oldExtension.dir;
|
||||
|
||||
if (!ExtensionSystem.unloadExtension(oldExtension))
|
||||
if (!Main.extensionManager.unloadExtension(oldExtension))
|
||||
return;
|
||||
|
||||
FileUtils.recursivelyMoveDir(extensionDir, oldExtensionTmpDir);
|
||||
@ -126,10 +127,10 @@ function updateExtension(uuid) {
|
||||
|
||||
try {
|
||||
extension = ExtensionUtils.createExtensionObject(uuid, extensionDir, ExtensionUtils.ExtensionType.PER_USER);
|
||||
ExtensionSystem.loadExtension(extension);
|
||||
Main.extensionManager.loadExtension(extension);
|
||||
} catch (e) {
|
||||
if (extension)
|
||||
ExtensionSystem.unloadExtension(extension);
|
||||
Main.extensionManager.unloadExtension(extension);
|
||||
|
||||
logError(e, 'Error loading extension %s'.format(uuid));
|
||||
|
||||
@ -138,7 +139,7 @@ function updateExtension(uuid) {
|
||||
|
||||
// Restore what was there before. We can't do much if we
|
||||
// fail here.
|
||||
ExtensionSystem.loadExtension(oldExtension);
|
||||
Main.extensionManager.loadExtension(oldExtension);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -233,7 +234,7 @@ class InstallExtensionDialog extends ModalDialog.ModalDialog {
|
||||
|
||||
try {
|
||||
let extension = ExtensionUtils.createExtensionObject(uuid, dir, ExtensionUtils.ExtensionType.PER_USER);
|
||||
ExtensionSystem.loadExtension(extension);
|
||||
Main.extensionManager.loadExtension(extension);
|
||||
} catch (e) {
|
||||
uninstallExtension(uuid);
|
||||
errback('LoadExtensionError', e);
|
||||
|
@ -8,28 +8,23 @@ const Main = imports.ui.main;
|
||||
|
||||
const { ExtensionState } = ExtensionUtils;
|
||||
|
||||
// Arrays of uuids
|
||||
var enabledExtensions;
|
||||
// Contains the order that extensions were enabled in.
|
||||
var extensionOrder = [];
|
||||
|
||||
// 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);
|
||||
|
||||
var connect = _signals.connect.bind(_signals);
|
||||
var disconnect = _signals.disconnect.bind(_signals);
|
||||
|
||||
const ENABLED_EXTENSIONS_KEY = 'enabled-extensions';
|
||||
const DISABLE_USER_EXTENSIONS_KEY = 'disable-user-extensions';
|
||||
const EXTENSION_DISABLE_VERSION_CHECK_KEY = 'disable-extension-version-validation';
|
||||
|
||||
var initted = false;
|
||||
var enabled;
|
||||
var ExtensionManager = class {
|
||||
constructor() {
|
||||
this._initted = false;
|
||||
this._enabled = false;
|
||||
|
||||
function disableExtension(uuid) {
|
||||
this._enabledExtensions = [];
|
||||
this._extensionOrder = [];
|
||||
|
||||
Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
|
||||
this._sessionUpdated();
|
||||
}
|
||||
|
||||
disableExtension(uuid) {
|
||||
let extension = ExtensionUtils.extensions[uuid];
|
||||
if (!extension)
|
||||
return;
|
||||
@ -45,8 +40,8 @@ function disableExtension(uuid) {
|
||||
// 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 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++) {
|
||||
@ -54,7 +49,7 @@ function disableExtension(uuid) {
|
||||
try {
|
||||
ExtensionUtils.extensions[uuid].stateObj.disable();
|
||||
} catch (e) {
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +62,7 @@ function disableExtension(uuid) {
|
||||
try {
|
||||
extension.stateObj.disable();
|
||||
} catch (e) {
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
}
|
||||
|
||||
for (let i = 0; i < order.length; i++) {
|
||||
@ -75,30 +70,30 @@ function disableExtension(uuid) {
|
||||
try {
|
||||
ExtensionUtils.extensions[uuid].stateObj.enable();
|
||||
} catch (e) {
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
}
|
||||
}
|
||||
|
||||
extensionOrder.splice(orderIdx, 1);
|
||||
this._extensionOrder.splice(orderIdx, 1);
|
||||
|
||||
if ( extension.state != ExtensionState.ERROR ) {
|
||||
if (extension.state != ExtensionState.ERROR) {
|
||||
extension.state = ExtensionState.DISABLED;
|
||||
_signals.emit('extension-state-changed', extension);
|
||||
this.emit('extension-state-changed', extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function enableExtension(uuid) {
|
||||
enableExtension(uuid) {
|
||||
let extension = ExtensionUtils.extensions[uuid];
|
||||
if (!extension)
|
||||
return;
|
||||
|
||||
if (extension.state == ExtensionState.INITIALIZED)
|
||||
initExtension(uuid);
|
||||
this.initExtension(uuid);
|
||||
|
||||
if (extension.state != ExtensionState.DISABLED)
|
||||
return;
|
||||
|
||||
extensionOrder.push(uuid);
|
||||
this._extensionOrder.push(uuid);
|
||||
|
||||
let stylesheetNames = [`${global.session_mode}.css`, 'stylesheet.css'];
|
||||
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
||||
@ -119,19 +114,19 @@ function enableExtension(uuid) {
|
||||
try {
|
||||
extension.stateObj.enable();
|
||||
extension.state = ExtensionState.ENABLED;
|
||||
_signals.emit('extension-state-changed', extension);
|
||||
this.emit('extension-state-changed', extension);
|
||||
return;
|
||||
} catch (e) {
|
||||
if (extension.stylesheet) {
|
||||
theme.unload_stylesheet(extension.stylesheet);
|
||||
delete extension.stylesheet;
|
||||
}
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function logExtensionError(uuid, error) {
|
||||
logExtensionError(uuid, error) {
|
||||
let extension = ExtensionUtils.extensions[uuid];
|
||||
if (!extension)
|
||||
return;
|
||||
@ -144,12 +139,12 @@ function logExtensionError(uuid, error) {
|
||||
extension.errors.push(message);
|
||||
|
||||
log('Extension "%s" had error: %s'.format(uuid, message));
|
||||
_signals.emit('extension-state-changed', { uuid: uuid,
|
||||
this.emit('extension-state-changed', { uuid: uuid,
|
||||
error: message,
|
||||
state: extension.state });
|
||||
}
|
||||
}
|
||||
|
||||
function loadExtension(extension) {
|
||||
loadExtension(extension) {
|
||||
// Default to error, we set success as the last step
|
||||
extension.state = ExtensionState.ERROR;
|
||||
|
||||
@ -158,54 +153,54 @@ function loadExtension(extension) {
|
||||
if (checkVersion && ExtensionUtils.isOutOfDate(extension)) {
|
||||
extension.state = ExtensionState.OUT_OF_DATE;
|
||||
} else {
|
||||
let enabled = enabledExtensions.includes(extension.uuid);
|
||||
let enabled = this._enabledExtensions.includes(extension.uuid);
|
||||
if (enabled) {
|
||||
if (!initExtension(extension.uuid))
|
||||
if (!this.initExtension(extension.uuid))
|
||||
return;
|
||||
if (extension.state == ExtensionState.DISABLED)
|
||||
enableExtension(extension.uuid);
|
||||
this.enableExtension(extension.uuid);
|
||||
} else {
|
||||
extension.state = ExtensionState.INITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
_signals.emit('extension-state-changed', extension);
|
||||
}
|
||||
this.emit('extension-state-changed', extension);
|
||||
}
|
||||
|
||||
function unloadExtension(extension) {
|
||||
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.
|
||||
disableExtension(extension.uuid);
|
||||
this.disableExtension(extension.uuid);
|
||||
|
||||
extension.state = ExtensionState.UNINSTALLED;
|
||||
_signals.emit('extension-state-changed', extension);
|
||||
this.emit('extension-state-changed', extension);
|
||||
|
||||
delete ExtensionUtils.extensions[extension.uuid];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function reloadExtension(oldExtension) {
|
||||
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);
|
||||
this.unloadExtension(oldExtension);
|
||||
|
||||
// Now, recreate the extension and load it.
|
||||
let newExtension;
|
||||
try {
|
||||
newExtension = ExtensionUtils.createExtensionObject(uuid, dir, type);
|
||||
} catch (e) {
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
return;
|
||||
}
|
||||
|
||||
loadExtension(newExtension);
|
||||
}
|
||||
this.loadExtension(newExtension);
|
||||
}
|
||||
|
||||
function initExtension(uuid) {
|
||||
initExtension(uuid) {
|
||||
let extension = ExtensionUtils.extensions[uuid];
|
||||
let dir = extension.dir;
|
||||
|
||||
@ -214,7 +209,7 @@ function initExtension(uuid) {
|
||||
|
||||
let extensionJs = dir.get_child('extension.js');
|
||||
if (!extensionJs.query_exists(null)) {
|
||||
logExtensionError(uuid, new Error('Missing extension.js'));
|
||||
this.logExtensionError(uuid, new Error('Missing extension.js'));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -225,7 +220,7 @@ function initExtension(uuid) {
|
||||
try {
|
||||
extensionModule = extension.imports.extension;
|
||||
} catch (e) {
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -233,7 +228,7 @@ function initExtension(uuid) {
|
||||
try {
|
||||
extensionState = extensionModule.init(extension);
|
||||
} catch (e) {
|
||||
logExtensionError(uuid, e);
|
||||
this.logExtensionError(uuid, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -243,11 +238,11 @@ function initExtension(uuid) {
|
||||
extension.stateObj = extensionState;
|
||||
|
||||
extension.state = ExtensionState.DISABLED;
|
||||
_signals.emit('extension-loaded', uuid);
|
||||
this.emit('extension-loaded', uuid);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function getEnabledExtensions() {
|
||||
_getEnabledExtensions() {
|
||||
let extensions;
|
||||
if (Array.isArray(Main.sessionMode.enabledExtensions))
|
||||
extensions = Main.sessionMode.enabledExtensions;
|
||||
@ -258,106 +253,106 @@ function getEnabledExtensions() {
|
||||
return extensions;
|
||||
|
||||
return extensions.concat(global.settings.get_strv(ENABLED_EXTENSIONS_KEY));
|
||||
}
|
||||
}
|
||||
|
||||
function onEnabledExtensionsChanged() {
|
||||
let newEnabledExtensions = getEnabledExtensions();
|
||||
_onEnabledExtensionsChanged() {
|
||||
let newEnabledExtensions = this._getEnabledExtensions();
|
||||
|
||||
if (!enabled)
|
||||
if (!this._enabled)
|
||||
return;
|
||||
|
||||
// Find and enable all the newly enabled extensions: UUIDs found in the
|
||||
// new setting, but not in the old one.
|
||||
newEnabledExtensions.filter(
|
||||
uuid => !enabledExtensions.includes(uuid)
|
||||
uuid => !this._enabledExtensions.includes(uuid)
|
||||
).forEach(uuid => {
|
||||
enableExtension(uuid);
|
||||
this.enableExtension(uuid);
|
||||
});
|
||||
|
||||
// Find and disable all the newly disabled extensions: UUIDs found in the
|
||||
// old setting, but not in the new one.
|
||||
enabledExtensions.filter(
|
||||
this._enabledExtensions.filter(
|
||||
item => !newEnabledExtensions.includes(item)
|
||||
).forEach(uuid => {
|
||||
disableExtension(uuid);
|
||||
this.disableExtension(uuid);
|
||||
});
|
||||
|
||||
enabledExtensions = newEnabledExtensions;
|
||||
}
|
||||
this._enabledExtensions = newEnabledExtensions;
|
||||
}
|
||||
|
||||
function _onVersionValidationChanged() {
|
||||
_onVersionValidationChanged() {
|
||||
// we want to reload all extensions, but only enable
|
||||
// extensions when allowed by the sessionMode, so
|
||||
// temporarily disable them all
|
||||
enabledExtensions = [];
|
||||
this._enabledExtensions = [];
|
||||
for (let uuid in ExtensionUtils.extensions)
|
||||
reloadExtension(ExtensionUtils.extensions[uuid]);
|
||||
enabledExtensions = getEnabledExtensions();
|
||||
this.reloadExtension(ExtensionUtils.extensions[uuid]);
|
||||
this._enabledExtensions = this._getEnabledExtensions();
|
||||
|
||||
if (Main.sessionMode.allowExtensions) {
|
||||
enabledExtensions.forEach(uuid => {
|
||||
enableExtension(uuid);
|
||||
this._enabledExtensions.forEach(uuid => {
|
||||
this.enableExtension(uuid);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _loadExtensions() {
|
||||
global.settings.connect(`changed::${ENABLED_EXTENSIONS_KEY}`, onEnabledExtensionsChanged);
|
||||
global.settings.connect(`changed::${DISABLE_USER_EXTENSIONS_KEY}`, onEnabledExtensionsChanged);
|
||||
global.settings.connect(`changed::${EXTENSION_DISABLE_VERSION_CHECK_KEY}`, _onVersionValidationChanged);
|
||||
_loadExtensions() {
|
||||
global.settings.connect(`changed::${ENABLED_EXTENSIONS_KEY}`,
|
||||
this._onEnabledExtensionsChanged.bind(this));
|
||||
global.settings.connect(`changed::${DISABLE_USER_EXTENSIONS_KEY}`,
|
||||
this._onEnabledExtensionsChanged.bind(this));
|
||||
global.settings.connect(`changed::${EXTENSION_DISABLE_VERSION_CHECK_KEY}`,
|
||||
this._onVersionValidationChanged.bind(this));
|
||||
|
||||
enabledExtensions = getEnabledExtensions();
|
||||
this._enabledExtensions = this._getEnabledExtensions();
|
||||
|
||||
let finder = new ExtensionUtils.ExtensionFinder();
|
||||
finder.connect('extension-found', (finder, extension) => {
|
||||
loadExtension(extension);
|
||||
this.loadExtension(extension);
|
||||
});
|
||||
finder.scanExtensions();
|
||||
}
|
||||
}
|
||||
|
||||
function enableAllExtensions() {
|
||||
if (enabled)
|
||||
enableAllExtensions() {
|
||||
if (this._enabled)
|
||||
return;
|
||||
|
||||
if (!initted) {
|
||||
_loadExtensions();
|
||||
initted = true;
|
||||
if (!this._initted) {
|
||||
this._loadExtensions();
|
||||
this._initted = true;
|
||||
} else {
|
||||
enabledExtensions.forEach(uuid => {
|
||||
enableExtension(uuid);
|
||||
this._enabledExtensions.forEach(uuid => {
|
||||
this.enableExtension(uuid);
|
||||
});
|
||||
}
|
||||
enabled = true;
|
||||
}
|
||||
this._enabled = true;
|
||||
}
|
||||
|
||||
function disableAllExtensions() {
|
||||
if (!enabled)
|
||||
disableAllExtensions() {
|
||||
if (!this._enabled)
|
||||
return;
|
||||
|
||||
if (initted) {
|
||||
extensionOrder.slice().reverse().forEach(uuid => {
|
||||
disableExtension(uuid);
|
||||
if (this._initted) {
|
||||
this._extensionOrder.slice().reverse().forEach(uuid => {
|
||||
this.disableExtension(uuid);
|
||||
});
|
||||
}
|
||||
|
||||
enabled = false;
|
||||
}
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
function _sessionUpdated() {
|
||||
_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) {
|
||||
if (initted)
|
||||
enabledExtensions = getEnabledExtensions();
|
||||
enableAllExtensions();
|
||||
if (this._initted)
|
||||
this._enabledExtensions = this._getEnabledExtensions();
|
||||
this.enableAllExtensions();
|
||||
} else {
|
||||
disableAllExtensions();
|
||||
this.disableAllExtensions();
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
Main.sessionMode.connect('updated', _sessionUpdated);
|
||||
_sessionUpdated();
|
||||
}
|
||||
}
|
||||
};
|
||||
Signals.addSignalMethods(ExtensionManager.prototype);
|
||||
|
@ -7,7 +7,6 @@ const Signals = imports.signals;
|
||||
const System = imports.system;
|
||||
|
||||
const History = imports.misc.history;
|
||||
const ExtensionSystem = imports.ui.extensionSystem;
|
||||
const ExtensionUtils = imports.misc.extensionUtils;
|
||||
const ShellEntry = imports.ui.shellEntry;
|
||||
const Tweener = imports.ui.tweener;
|
||||
@ -628,7 +627,7 @@ var Extensions = class Extensions {
|
||||
for (let uuid in ExtensionUtils.extensions)
|
||||
this._loadExtension(null, uuid);
|
||||
|
||||
ExtensionSystem.connect('extension-loaded',
|
||||
Main.extensionManager.connect('extension-loaded',
|
||||
this._loadExtension.bind(this));
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,7 @@ const LOG_DOMAIN = 'GNOME Shell';
|
||||
const GNOMESHELL_STARTED_MESSAGE_ID = 'f3ea493c22934e26811cd62abe8e203a';
|
||||
|
||||
var componentManager = null;
|
||||
var extensionManager = null;
|
||||
var panel = null;
|
||||
var overview = null;
|
||||
var runDialog = null;
|
||||
@ -226,7 +227,7 @@ function _initializeUI() {
|
||||
_startDate = new Date();
|
||||
|
||||
ExtensionDownloader.init();
|
||||
ExtensionSystem.init();
|
||||
extensionManager = new ExtensionSystem.ExtensionManager();
|
||||
|
||||
if (sessionMode.isGreeter && screenShield) {
|
||||
layoutManager.connect('startup-prepared', () => {
|
||||
|
@ -4,7 +4,6 @@ const { Gio, GLib, Meta, Shell } = imports.gi;
|
||||
const Lang = imports.lang;
|
||||
|
||||
const Config = imports.misc.config;
|
||||
const ExtensionSystem = imports.ui.extensionSystem;
|
||||
const ExtensionDownloader = imports.ui.extensionDownloader;
|
||||
const ExtensionUtils = imports.misc.extensionUtils;
|
||||
const Main = imports.ui.main;
|
||||
@ -251,7 +250,7 @@ var GnomeShellExtensions = class {
|
||||
constructor() {
|
||||
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(GnomeShellExtensionsIface, this);
|
||||
this._dbusImpl.export(Gio.DBus.session, '/org/gnome/Shell');
|
||||
ExtensionSystem.connect('extension-state-changed',
|
||||
Main.extensionManager.connect('extension-state-changed',
|
||||
this._extensionStateChanged.bind(this));
|
||||
}
|
||||
|
||||
@ -335,7 +334,7 @@ var GnomeShellExtensions = class {
|
||||
if (!extension)
|
||||
return;
|
||||
|
||||
ExtensionSystem.reloadExtension(extension);
|
||||
Main.extensionManager.reloadExtension(extension);
|
||||
}
|
||||
|
||||
CheckForUpdates() {
|
||||
|
Loading…
Reference in New Issue
Block a user