extensionUtils: Use signals rather than callbacks for finding extensions

This allows us to move to a file-monitor based approach in the future.

Since we need signals, we convert the current set of functions to an
object we attach signals too, leading to the new ExtensionFinder object.

https://bugzilla.gnome.org/show_bug.cgi?id=677586
This commit is contained in:
Jasper St. Pierre
2012-06-04 17:14:18 -04:00
parent 5265884af9
commit 498b023989
3 changed files with 61 additions and 46 deletions

View File

@@ -202,24 +202,29 @@ const Application = new Lang.Class({
}, },
_scanExtensions: function() { _scanExtensions: function() {
ExtensionUtils.scanExtensions(Lang.bind(this, function(uuid, dir, type) { let finder = new ExtensionUtils.ExtensionFinder();
if (ExtensionUtils.extensions[uuid] !== undefined) finder.connect('extension-found', Lang.bind(this, this._extensionFound));
return; finder.scanExtensions();
let extension;
try {
extension = ExtensionUtils.createExtensionObject(uuid, dir, type);
} catch(e) {
logError(e, 'Could not create extensions object');
return;
}
let iter = this._model.append();
this._model.set(iter, [0, 1], [uuid, extension.metadata.name]);
this._extensionIters[uuid] = iter;
}));
}, },
_extensionFound: function(signals, uuid, dir, type) {
if (ExtensionUtils.extensions[uuid] !== undefined)
return;
let extension;
try {
extension = ExtensionUtils.createExtensionObject(uuid, dir, type);
} catch(e) {
logError(e, 'Could not create extensions object');
return;
}
let iter = this._model.append();
this._model.set(iter, [0, 1], [uuid, extension.metadata.name]);
this._extensionIters[uuid] = iter;
},
_onActivate: function() { _onActivate: function() {
this._window.present(); this._window.present();
}, },

View File

@@ -3,6 +3,9 @@
// Common utils for the extension system and the extension // Common utils for the extension system and the extension
// preferences tool // preferences tool
const Lang = imports.lang;
const Signals = imports.signals;
const GLib = imports.gi.GLib; const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio; const Gio = imports.gi.Gio;
const ShellJS = imports.gi.ShellJS; const ShellJS = imports.gi.ShellJS;
@@ -149,36 +152,41 @@ function installImporter(extension) {
_extension = null; _extension = null;
} }
function scanExtensionsInDirectory(callback, dir, type) { const ExtensionFinder = new Lang.Class({
let fileEnum; Name: 'ExtensionFinder',
let file, info;
try {
fileEnum = dir.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NONE, null);
} catch(e) {
logError(e, 'Could not enumerate extensions directory');
return;
}
while ((info = fileEnum.next_file(null)) != null) { _scanExtensionsInDirectory: function(dir, type) {
let fileType = info.get_file_type(); let fileEnum;
if (fileType != Gio.FileType.DIRECTORY) let file, info;
continue; try {
let uuid = info.get_name(); fileEnum = dir.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NONE, null);
let extensionDir = dir.get_child(uuid); } catch(e) {
callback(uuid, extensionDir, type); logError(e, 'Could not enumerate extensions directory');
} return;
fileEnum.close(null); }
}
function scanExtensions(callback) { while ((info = fileEnum.next_file(null)) != null) {
let userExtensionsDir = Gio.File.new_for_path(GLib.build_filenamev([global.userdatadir, 'extensions'])); let fileType = info.get_file_type();
scanExtensionsInDirectory(callback, userExtensionsDir, ExtensionType.PER_USER); if (fileType != Gio.FileType.DIRECTORY)
continue;
let uuid = info.get_name();
let extensionDir = dir.get_child(uuid);
this.emit('extension-found', uuid, extensionDir, type);
}
fileEnum.close(null);
},
let systemDataDirs = GLib.get_system_data_dirs(); scanExtensions: function() {
for (let i = 0; i < systemDataDirs.length; i++) { let userExtensionsDir = Gio.File.new_for_path(GLib.build_filenamev([global.userdatadir, 'extensions']));
let dirPath = GLib.build_filenamev([systemDataDirs[i], 'gnome-shell', 'extensions']); this._scanExtensionsInDirectory(userExtensionsDir, ExtensionType.PER_USER);
let dir = Gio.file_new_for_path(dirPath);
if (dir.query_exists(null)) let systemDataDirs = GLib.get_system_data_dirs();
scanExtensionsInDirectory(callback, dir, ExtensionType.SYSTEM); for (let i = 0; i < systemDataDirs.length; i++) {
let dirPath = GLib.build_filenamev([systemDataDirs[i], 'gnome-shell', 'extensions']);
let dir = Gio.file_new_for_path(dirPath);
if (dir.query_exists(null))
this._scanExtensionsInDirectory(dir, ExtensionType.SYSTEM);
}
} }
} });
Signals.addSignalMethods(ExtensionFinder.prototype);

View File

@@ -262,8 +262,10 @@ function init() {
} }
function loadExtensions() { function loadExtensions() {
ExtensionUtils.scanExtensions(function(uuid, dir, type) { let finder = new ExtensionUtils.ExtensionFinder();
finder.connect('extension-found', function(signals, uuid, dir, type) {
let enabled = enabledExtensions.indexOf(uuid) != -1; let enabled = enabledExtensions.indexOf(uuid) != -1;
loadExtension(dir, type, enabled); loadExtension(dir, type, enabled);
}); });
finder.scanExtensions();
} }