2012-01-19 00:55:20 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
// Common utils for the extension system and the extension
|
|
|
|
// preferences tool
|
|
|
|
|
2012-06-04 21:14:18 +00:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2012-01-19 00:55:20 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
|
|
|
|
const Config = imports.misc.config;
|
2012-11-30 17:06:11 +00:00
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2012-01-19 00:55:20 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var ExtensionType = {
|
2012-01-19 00:55:20 +00:00
|
|
|
SYSTEM: 1,
|
|
|
|
PER_USER: 2
|
|
|
|
};
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
// Maps uuid -> metadata object
|
2017-07-18 17:47:27 +00:00
|
|
|
var extensions = {};
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2016-10-03 23:08:18 +00:00
|
|
|
/**
|
|
|
|
* getCurrentExtension:
|
|
|
|
*
|
|
|
|
* Returns the current extension, or null if not called from an extension.
|
|
|
|
*/
|
2012-01-31 01:58:29 +00:00
|
|
|
function getCurrentExtension() {
|
2016-10-03 23:08:18 +00:00
|
|
|
let stack = (new Error()).stack.split('\n');
|
|
|
|
let extensionStackLine;
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2016-10-03 23:08:18 +00:00
|
|
|
// Search for an occurrence of an extension stack frame
|
|
|
|
// Start at 1 because 0 is the stack frame of this function
|
|
|
|
for (let i = 1; i < stack.length; i++) {
|
|
|
|
if (stack[i].indexOf('/gnome-shell/extensions/') > -1) {
|
|
|
|
extensionStackLine = stack[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 01:58:29 +00:00
|
|
|
if (!extensionStackLine)
|
2016-10-03 23:08:18 +00:00
|
|
|
return null;
|
2012-01-31 01:58:29 +00:00
|
|
|
|
|
|
|
// The stack line is like:
|
|
|
|
// init([object Object])@/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
|
|
|
|
//
|
|
|
|
// In the case that we're importing from
|
|
|
|
// module scope, the first field is blank:
|
|
|
|
// @/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
|
|
|
|
let match = new RegExp('@(.+):\\d+').exec(extensionStackLine);
|
|
|
|
if (!match)
|
2016-10-03 23:08:18 +00:00
|
|
|
return null;
|
2012-01-31 01:58:29 +00:00
|
|
|
|
|
|
|
let path = match[1];
|
2012-05-28 21:21:02 +00:00
|
|
|
let file = Gio.File.new_for_path(path);
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2014-11-29 18:05:11 +00:00
|
|
|
// Walk up the directory tree, looking for an extension with
|
2012-05-28 21:21:02 +00:00
|
|
|
// the same UUID as a directory name.
|
|
|
|
while (file != null) {
|
|
|
|
let extension = extensions[file.get_basename()];
|
|
|
|
if (extension !== undefined)
|
|
|
|
return extension;
|
|
|
|
file = file.get_parent();
|
|
|
|
}
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2016-10-03 23:08:18 +00:00
|
|
|
return null;
|
2012-01-31 01:58:29 +00:00
|
|
|
}
|
|
|
|
|
2012-01-19 00:55:20 +00:00
|
|
|
/**
|
|
|
|
* versionCheck:
|
|
|
|
* @required: an array of versions we're compatible with
|
|
|
|
* @current: the version we have
|
|
|
|
*
|
|
|
|
* Check if a component is compatible for an extension.
|
|
|
|
* @required is an array, and at least one version must match.
|
|
|
|
* @current must be in the format <major>.<minor>.<point>.<micro>
|
|
|
|
* <micro> is always ignored
|
|
|
|
* <point> is ignored if <minor> is even (so you can target the
|
|
|
|
* whole stable release)
|
|
|
|
* <minor> and <major> must match
|
|
|
|
* Each target version must be at least <major> and <minor>
|
|
|
|
*/
|
|
|
|
function versionCheck(required, current) {
|
|
|
|
let currentArray = current.split('.');
|
|
|
|
let major = currentArray[0];
|
|
|
|
let minor = currentArray[1];
|
|
|
|
let point = currentArray[2];
|
|
|
|
for (let i = 0; i < required.length; i++) {
|
|
|
|
let requiredArray = required[i].split('.');
|
|
|
|
if (requiredArray[0] == major &&
|
|
|
|
requiredArray[1] == minor &&
|
|
|
|
(requiredArray[2] == point ||
|
|
|
|
(requiredArray[2] == undefined && parseInt(minor) % 2 == 0)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
function isOutOfDate(extension) {
|
|
|
|
if (!versionCheck(extension.metadata['shell-version'], Config.PACKAGE_VERSION))
|
2012-01-19 00:55:20 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
function createExtensionObject(uuid, dir, type) {
|
2012-01-19 00:55:20 +00:00
|
|
|
let info;
|
|
|
|
|
|
|
|
let metadataFile = dir.get_child('metadata.json');
|
|
|
|
if (!metadataFile.query_exists(null)) {
|
|
|
|
throw new Error('Missing metadata.json');
|
|
|
|
}
|
|
|
|
|
|
|
|
let metadataContents, success, tag;
|
|
|
|
try {
|
|
|
|
[success, metadataContents, tag] = metadataFile.load_contents(null);
|
|
|
|
} 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 + '"');
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
let extension = {};
|
|
|
|
|
|
|
|
extension.metadata = meta;
|
|
|
|
extension.uuid = meta.uuid;
|
|
|
|
extension.type = type;
|
|
|
|
extension.dir = dir;
|
|
|
|
extension.path = dir.get_path();
|
2012-12-13 19:16:21 +00:00
|
|
|
extension.error = '';
|
2012-01-31 01:58:29 +00:00
|
|
|
extension.hasPrefs = dir.get_child('prefs.js').query_exists(null);
|
|
|
|
|
|
|
|
extensions[uuid] = extension;
|
2012-01-19 00:55:20 +00:00
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
return extension;
|
2012-01-19 00:55:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
function installImporter(extension) {
|
2016-10-05 05:27:18 +00:00
|
|
|
let oldSearchPath = imports.searchPath.slice(); // make a copy
|
2016-10-17 21:05:05 +00:00
|
|
|
imports.searchPath = [extension.dir.get_parent().get_path()];
|
2016-10-05 05:27:18 +00:00
|
|
|
// importing a "subdir" creates a new importer object that doesn't affect
|
|
|
|
// the global one
|
2016-10-17 21:05:05 +00:00
|
|
|
extension.imports = imports[extension.uuid];
|
2016-10-05 05:27:18 +00:00
|
|
|
imports.searchPath = oldSearchPath;
|
2012-01-19 00:55:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var ExtensionFinder = new Lang.Class({
|
2012-06-04 21:14:18 +00:00
|
|
|
Name: 'ExtensionFinder',
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_loadExtension(extensionDir, info, perUserDir) {
|
2012-11-30 17:06:11 +00:00
|
|
|
let fileType = info.get_file_type();
|
|
|
|
if (fileType != Gio.FileType.DIRECTORY)
|
|
|
|
return;
|
|
|
|
let uuid = info.get_name();
|
|
|
|
let existing = extensions[uuid];
|
|
|
|
if (existing) {
|
|
|
|
log('Extension %s already installed in %s. %s will not be loaded'.format(uuid, existing.path, extensionDir.get_path()));
|
2012-06-04 21:14:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-04 03:13:15 +00:00
|
|
|
|
2012-11-30 17:06:11 +00:00
|
|
|
let extension;
|
|
|
|
let type = extensionDir.has_prefix(perUserDir) ? ExtensionType.PER_USER
|
|
|
|
: ExtensionType.SYSTEM;
|
|
|
|
try {
|
|
|
|
extension = createExtensionObject(uuid, extensionDir, type);
|
|
|
|
} catch(e) {
|
|
|
|
logError(e, 'Could not load extension %s'.format(uuid));
|
|
|
|
return;
|
2012-06-04 21:14:18 +00:00
|
|
|
}
|
2012-11-30 17:06:11 +00:00
|
|
|
this.emit('extension-found', extension);
|
2012-06-04 21:14:18 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
scanExtensions() {
|
2012-11-30 17:06:11 +00:00
|
|
|
let perUserDir = Gio.File.new_for_path(global.userdatadir);
|
2017-12-02 00:27:35 +00:00
|
|
|
FileUtils.collectFromDatadirs('extensions', true, (dir, info) => {
|
|
|
|
this._loadExtension(dir, info, perUserDir);
|
|
|
|
});
|
2012-01-19 00:55:20 +00:00
|
|
|
}
|
2012-06-04 21:14:18 +00:00
|
|
|
});
|
|
|
|
Signals.addSignalMethods(ExtensionFinder.prototype);
|