2012-05-25 23:07:31 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported init, installExtension, uninstallExtension,
|
|
|
|
checkForUpdates, updateExtension */
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2019-06-29 12:27:03 +00:00
|
|
|
const { Clutter, Gio, GLib, GObject, Soup } = imports.gi;
|
2012-05-25 23:07:31 +00:00
|
|
|
|
|
|
|
const Config = imports.misc.config;
|
2019-06-26 17:51:18 +00:00
|
|
|
const Dialog = imports.ui.dialog;
|
2012-05-25 23:07:31 +00:00
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2019-03-07 00:45:45 +00:00
|
|
|
const Main = imports.ui.main;
|
2012-05-25 23:07:31 +00:00
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var REPOSITORY_URL_BASE = 'https://extensions.gnome.org';
|
2019-01-30 00:18:24 +00:00
|
|
|
var REPOSITORY_URL_DOWNLOAD = `${REPOSITORY_URL_BASE}/download-extension/%s.shell-extension.zip`;
|
|
|
|
var REPOSITORY_URL_INFO = `${REPOSITORY_URL_BASE}/extension-info/`;
|
|
|
|
var REPOSITORY_URL_UPDATE = `${REPOSITORY_URL_BASE}/update-info/`;
|
2012-05-25 23:07:31 +00:00
|
|
|
|
|
|
|
let _httpSession;
|
|
|
|
|
2012-06-27 00:46:37 +00:00
|
|
|
function installExtension(uuid, invocation) {
|
2019-08-19 19:06:04 +00:00
|
|
|
let params = { uuid,
|
2012-05-25 23:07:31 +00:00
|
|
|
shell_version: Config.PACKAGE_VERSION };
|
|
|
|
|
|
|
|
let message = Soup.form_request_new_from_hash('GET', REPOSITORY_URL_INFO, params);
|
|
|
|
|
2019-08-20 00:20:08 +00:00
|
|
|
_httpSession.queue_message(message, () => {
|
2012-06-16 02:38:55 +00:00
|
|
|
if (message.status_code != Soup.KnownStatusCode.OK) {
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.extensionManager.logExtensionError(uuid, `downloading info: ${message.status_code}`);
|
2012-06-16 03:20:36 +00:00
|
|
|
invocation.return_dbus_error('org.gnome.Shell.DownloadInfoError', message.status_code.toString());
|
2012-06-16 02:38:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let info;
|
|
|
|
try {
|
|
|
|
info = JSON.parse(message.response_body.data);
|
|
|
|
} catch (e) {
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.extensionManager.logExtensionError(uuid, `parsing info: ${e}`);
|
2012-06-16 03:20:36 +00:00
|
|
|
invocation.return_dbus_error('org.gnome.Shell.ParseInfoError', e.toString());
|
2012-06-16 02:38:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-16 03:20:36 +00:00
|
|
|
let dialog = new InstallExtensionDialog(uuid, info, invocation);
|
2012-06-16 02:38:55 +00:00
|
|
|
dialog.open(global.get_current_time());
|
|
|
|
});
|
2012-05-25 23:07:31 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 00:46:37 +00:00
|
|
|
function uninstallExtension(uuid) {
|
2019-07-07 22:01:11 +00:00
|
|
|
let extension = Main.extensionManager.lookup(uuid);
|
2012-05-25 23:07:31 +00:00
|
|
|
if (!extension)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't try to uninstall system extensions
|
|
|
|
if (extension.type != ExtensionUtils.ExtensionType.PER_USER)
|
|
|
|
return false;
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!Main.extensionManager.unloadExtension(extension))
|
2012-06-05 17:03:42 +00:00
|
|
|
return false;
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2012-06-27 00:47:44 +00:00
|
|
|
FileUtils.recursivelyDeleteDir(extension.dir, true);
|
2012-05-25 23:07:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-27 00:47:44 +00:00
|
|
|
function gotExtensionZipFile(session, message, uuid, dir, callback, errback) {
|
2012-05-25 23:07:31 +00:00
|
|
|
if (message.status_code != Soup.KnownStatusCode.OK) {
|
2012-06-16 03:20:36 +00:00
|
|
|
errback('DownloadExtensionError', message.status_code);
|
2012-05-25 23:07:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-25 23:12:30 +00:00
|
|
|
try {
|
|
|
|
if (!dir.query_exists(null))
|
|
|
|
dir.make_directory_with_parents(null);
|
|
|
|
} catch (e) {
|
2012-06-16 03:20:36 +00:00
|
|
|
errback('CreateExtensionDirectoryError', e);
|
2012-06-16 02:35:26 +00:00
|
|
|
return;
|
2012-05-25 23:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let [file, stream] = Gio.File.new_tmp('XXXXXX.shell-extension.zip');
|
2012-08-24 01:47:23 +00:00
|
|
|
let contents = message.response_body.flatten().get_as_bytes();
|
2012-05-25 23:07:31 +00:00
|
|
|
stream.output_stream.write_bytes(contents, null);
|
|
|
|
stream.close(null);
|
|
|
|
let [success, pid] = GLib.spawn_async(null,
|
|
|
|
['unzip', '-uod', dir.get_path(), '--', file.get_path()],
|
|
|
|
null,
|
|
|
|
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
|
|
|
|
null);
|
|
|
|
|
|
|
|
if (!success) {
|
2012-06-16 03:20:36 +00:00
|
|
|
errback('ExtractExtensionError');
|
2012-05-25 23:07:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:20:08 +00:00
|
|
|
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (o, status) => {
|
2012-05-25 23:07:31 +00:00
|
|
|
GLib.spawn_close_pid(pid);
|
|
|
|
|
2012-07-06 23:28:00 +00:00
|
|
|
if (status != 0)
|
2012-06-16 03:20:36 +00:00
|
|
|
errback('ExtractExtensionError');
|
2012-07-06 23:28:00 +00:00
|
|
|
else
|
|
|
|
callback();
|
2012-05-25 23:07:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-06-27 00:47:44 +00:00
|
|
|
function updateExtension(uuid) {
|
|
|
|
// This gets a bit tricky. We want the update to be seamless -
|
|
|
|
// if we have any error during downloading or extracting, we
|
|
|
|
// want to not unload the current version.
|
|
|
|
|
|
|
|
let oldExtensionTmpDir = GLib.Dir.make_tmp('XXXXXX-shell-extension');
|
|
|
|
let newExtensionTmpDir = GLib.Dir.make_tmp('XXXXXX-shell-extension');
|
|
|
|
|
|
|
|
let params = { shell_version: Config.PACKAGE_VERSION };
|
|
|
|
|
|
|
|
let url = REPOSITORY_URL_DOWNLOAD.format(uuid);
|
|
|
|
let message = Soup.form_request_new_from_hash('GET', url, params);
|
|
|
|
|
2019-08-20 00:20:08 +00:00
|
|
|
_httpSession.queue_message(message, session => {
|
2017-10-31 00:38:18 +00:00
|
|
|
gotExtensionZipFile(session, message, uuid, newExtensionTmpDir, () => {
|
2019-07-07 22:01:11 +00:00
|
|
|
let oldExtension = Main.extensionManager.lookup(uuid);
|
2012-06-27 00:47:44 +00:00
|
|
|
let extensionDir = oldExtension.dir;
|
|
|
|
|
2019-03-07 00:45:45 +00:00
|
|
|
if (!Main.extensionManager.unloadExtension(oldExtension))
|
2012-06-27 00:47:44 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
FileUtils.recursivelyMoveDir(extensionDir, oldExtensionTmpDir);
|
|
|
|
FileUtils.recursivelyMoveDir(newExtensionTmpDir, extensionDir);
|
|
|
|
|
2017-05-17 21:51:49 +00:00
|
|
|
let extension = null;
|
2012-06-27 00:47:44 +00:00
|
|
|
|
|
|
|
try {
|
2019-07-07 21:38:27 +00:00
|
|
|
extension = Main.extensionManager.createExtensionObject(uuid, extensionDir, ExtensionUtils.ExtensionType.PER_USER);
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.extensionManager.loadExtension(extension);
|
2019-01-29 01:26:39 +00:00
|
|
|
} catch (e) {
|
2017-05-17 21:51:49 +00:00
|
|
|
if (extension)
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.extensionManager.unloadExtension(extension);
|
2012-06-27 00:47:44 +00:00
|
|
|
|
|
|
|
logError(e, 'Error loading extension %s'.format(uuid));
|
|
|
|
|
|
|
|
FileUtils.recursivelyDeleteDir(extensionDir, false);
|
|
|
|
FileUtils.recursivelyMoveDir(oldExtensionTmpDir, extensionDir);
|
|
|
|
|
|
|
|
// Restore what was there before. We can't do much if we
|
|
|
|
// fail here.
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.extensionManager.loadExtension(oldExtension);
|
2012-06-27 00:47:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileUtils.recursivelyDeleteDir(oldExtensionTmpDir, true);
|
2019-08-20 00:20:08 +00:00
|
|
|
}, (code, msg) => {
|
|
|
|
log(`Error while updating extension ${uuid}: ${code} (${msg})`);
|
2012-06-27 00:47:44 +00:00
|
|
|
});
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2012-06-27 00:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkForUpdates() {
|
|
|
|
let metadatas = {};
|
2019-07-07 22:01:11 +00:00
|
|
|
Main.extensionManager.getUuids().forEach(uuid => {
|
2019-11-28 13:06:12 +00:00
|
|
|
metadatas[uuid] = Main.extensionManager.lookup(uuid).metadata;
|
2019-07-07 22:01:11 +00:00
|
|
|
});
|
2012-06-27 00:47:44 +00:00
|
|
|
|
|
|
|
let params = { shell_version: Config.PACKAGE_VERSION,
|
|
|
|
installed: JSON.stringify(metadatas) };
|
|
|
|
|
|
|
|
let url = REPOSITORY_URL_UPDATE;
|
|
|
|
let message = Soup.form_request_new_from_hash('GET', url, params);
|
2019-08-20 00:20:08 +00:00
|
|
|
_httpSession.queue_message(message, () => {
|
2012-06-27 00:47:44 +00:00
|
|
|
if (message.status_code != Soup.KnownStatusCode.OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let operations = JSON.parse(message.response_body.data);
|
|
|
|
for (let uuid in operations) {
|
|
|
|
let operation = operations[uuid];
|
|
|
|
if (operation == 'blacklist')
|
|
|
|
uninstallExtension(uuid);
|
|
|
|
else if (operation == 'upgrade' || operation == 'downgrade')
|
|
|
|
updateExtension(uuid);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:45:44 +00:00
|
|
|
var InstallExtensionDialog = GObject.registerClass(
|
2017-10-31 01:19:44 +00:00
|
|
|
class InstallExtensionDialog extends ModalDialog.ModalDialog {
|
2019-05-23 20:45:44 +00:00
|
|
|
_init(uuid, info, invocation) {
|
|
|
|
super._init({ styleClass: 'extension-dialog' });
|
2012-05-25 23:07:31 +00:00
|
|
|
|
|
|
|
this._uuid = uuid;
|
|
|
|
this._info = info;
|
2012-06-16 03:20:36 +00:00
|
|
|
this._invocation = invocation;
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2019-02-12 14:02:09 +00:00
|
|
|
this.setButtons([{
|
|
|
|
label: _("Cancel"),
|
|
|
|
action: this._onCancelButtonPressed.bind(this),
|
2019-11-05 19:37:28 +00:00
|
|
|
key: Clutter.KEY_Escape,
|
2019-02-12 14:02:09 +00:00
|
|
|
}, {
|
|
|
|
label: _("Install"),
|
|
|
|
action: this._onInstallButtonPressed.bind(this),
|
|
|
|
default: true,
|
|
|
|
}]);
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2019-06-26 17:51:18 +00:00
|
|
|
let content = new Dialog.MessageDialogContent({
|
|
|
|
title: _("Download and install “%s” from extensions.gnome.org?").format(info.name),
|
|
|
|
});
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2019-06-26 17:51:18 +00:00
|
|
|
this.contentLayout.add(content);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2019-02-04 11:30:53 +00:00
|
|
|
_onCancelButtonPressed() {
|
2013-02-20 18:12:42 +00:00
|
|
|
this.close();
|
2012-06-16 03:20:36 +00:00
|
|
|
this._invocation.return_value(GLib.Variant.new('(s)', ['cancelled']));
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2019-02-04 11:30:53 +00:00
|
|
|
_onInstallButtonPressed() {
|
2012-05-25 23:07:31 +00:00
|
|
|
let params = { shell_version: Config.PACKAGE_VERSION };
|
|
|
|
|
|
|
|
let url = REPOSITORY_URL_DOWNLOAD.format(this._uuid);
|
|
|
|
let message = Soup.form_request_new_from_hash('GET', url, params);
|
|
|
|
|
2012-07-06 23:28:00 +00:00
|
|
|
let uuid = this._uuid;
|
2012-06-27 00:47:44 +00:00
|
|
|
let dir = Gio.File.new_for_path(GLib.build_filenamev([global.userdatadir, 'extensions', uuid]));
|
2012-06-16 03:20:36 +00:00
|
|
|
let invocation = this._invocation;
|
2019-08-20 00:20:08 +00:00
|
|
|
function errback(code, msg) {
|
|
|
|
log(`Error while installing ${uuid}: ${code} (${msg})`);
|
|
|
|
invocation.return_dbus_error(`org.gnome.Shell.${code}`, msg || '');
|
2012-06-16 03:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function callback() {
|
2012-07-06 23:28:00 +00:00
|
|
|
try {
|
2019-07-07 21:38:27 +00:00
|
|
|
let extension = Main.extensionManager.createExtensionObject(uuid, dir, ExtensionUtils.ExtensionType.PER_USER);
|
2019-03-07 00:45:45 +00:00
|
|
|
Main.extensionManager.loadExtension(extension);
|
2018-01-17 12:43:11 +00:00
|
|
|
if (!Main.extensionManager.enableExtension(uuid))
|
|
|
|
throw new Error(`Cannot add ${uuid} to enabled extensions gsettings key`);
|
2019-01-29 01:26:39 +00:00
|
|
|
} catch (e) {
|
2012-07-06 23:28:00 +00:00
|
|
|
uninstallExtension(uuid);
|
|
|
|
errback('LoadExtensionError', e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:14:23 +00:00
|
|
|
invocation.return_value(GLib.Variant.new('(s)', ['successful']));
|
2012-06-16 03:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 00:20:08 +00:00
|
|
|
_httpSession.queue_message(message, session => {
|
2012-06-27 00:47:44 +00:00
|
|
|
gotExtensionZipFile(session, message, uuid, dir, callback, errback);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2013-02-20 18:12:42 +00:00
|
|
|
this.close();
|
2012-05-25 23:07:31 +00:00
|
|
|
}
|
2019-05-23 20:45:44 +00:00
|
|
|
});
|
2012-05-25 23:07:31 +00:00
|
|
|
|
|
|
|
function init() {
|
|
|
|
_httpSession = new Soup.SessionAsync({ ssl_use_system_ca_file: true });
|
|
|
|
|
|
|
|
// See: https://bugzilla.gnome.org/show_bug.cgi?id=655189 for context.
|
|
|
|
// _httpSession.add_feature(new Soup.ProxyResolverDefault());
|
|
|
|
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
|
|
|
|
}
|