2012-05-25 19:07:31 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2020-01-22 09:07:45 -05:00
|
|
|
/* exported init, installExtension, uninstallExtension, checkForUpdates */
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2019-06-29 08:27:03 -04:00
|
|
|
const { Clutter, Gio, GLib, GObject, Soup } = imports.gi;
|
2012-05-25 19:07:31 -04:00
|
|
|
|
|
|
|
const Config = imports.misc.config;
|
2019-06-26 13:51:18 -04:00
|
|
|
const Dialog = imports.ui.dialog;
|
2012-05-25 19:07:31 -04:00
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2019-03-06 19:45:45 -05:00
|
|
|
const Main = imports.ui.main;
|
2012-05-25 19:07:31 -04:00
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
|
|
|
|
2020-02-14 10:10:34 -05:00
|
|
|
var REPOSITORY_URL_DOWNLOAD = 'https://extensions.gnome.org/download-extension/%s.shell-extension.zip';
|
|
|
|
var REPOSITORY_URL_INFO = 'https://extensions.gnome.org/extension-info/';
|
|
|
|
var REPOSITORY_URL_UPDATE = 'https://extensions.gnome.org/update-info/';
|
2012-05-25 19:07:31 -04:00
|
|
|
|
|
|
|
let _httpSession;
|
|
|
|
|
2012-06-26 20:46:37 -04:00
|
|
|
function installExtension(uuid, invocation) {
|
2019-08-19 15:06:04 -04:00
|
|
|
let params = { uuid,
|
2012-05-25 19:07:31 -04:00
|
|
|
shell_version: Config.PACKAGE_VERSION };
|
|
|
|
|
|
|
|
let message = Soup.form_request_new_from_hash('GET', REPOSITORY_URL_INFO, params);
|
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
_httpSession.queue_message(message, () => {
|
2012-06-15 22:38:55 -04:00
|
|
|
if (message.status_code != Soup.KnownStatusCode.OK) {
|
2020-02-14 10:10:34 -05:00
|
|
|
Main.extensionManager.logExtensionError(uuid, 'downloading info: %d'.format(message.status_code));
|
2012-06-15 23:20:36 -04:00
|
|
|
invocation.return_dbus_error('org.gnome.Shell.DownloadInfoError', message.status_code.toString());
|
2012-06-15 22:38:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let info;
|
|
|
|
try {
|
|
|
|
info = JSON.parse(message.response_body.data);
|
|
|
|
} catch (e) {
|
2020-02-14 10:10:34 -05:00
|
|
|
Main.extensionManager.logExtensionError(uuid, 'parsing info: %s'.format(e.toString()));
|
2012-06-15 23:20:36 -04:00
|
|
|
invocation.return_dbus_error('org.gnome.Shell.ParseInfoError', e.toString());
|
2012-06-15 22:38:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-15 23:20:36 -04:00
|
|
|
let dialog = new InstallExtensionDialog(uuid, info, invocation);
|
2012-06-15 22:38:55 -04:00
|
|
|
dialog.open(global.get_current_time());
|
|
|
|
});
|
2012-05-25 19:07:31 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 20:46:37 -04:00
|
|
|
function uninstallExtension(uuid) {
|
2019-07-07 18:01:11 -04:00
|
|
|
let extension = Main.extensionManager.lookup(uuid);
|
2012-05-25 19:07:31 -04:00
|
|
|
if (!extension)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't try to uninstall system extensions
|
|
|
|
if (extension.type != ExtensionUtils.ExtensionType.PER_USER)
|
|
|
|
return false;
|
|
|
|
|
2019-03-06 19:45:45 -05:00
|
|
|
if (!Main.extensionManager.unloadExtension(extension))
|
2012-06-05 13:03:42 -04:00
|
|
|
return false;
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2012-06-26 20:47:44 -04:00
|
|
|
FileUtils.recursivelyDeleteDir(extension.dir, true);
|
2020-03-09 11:49:34 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
const updatesDir = Gio.File.new_for_path(GLib.build_filenamev(
|
|
|
|
[global.userdatadir, 'extension-updates', extension.uuid]));
|
|
|
|
FileUtils.recursivelyDeleteDir(updatesDir, true);
|
|
|
|
} catch (e) {
|
|
|
|
// not an error
|
|
|
|
}
|
|
|
|
|
2012-05-25 19:07:31 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-26 20:47:44 -04:00
|
|
|
function gotExtensionZipFile(session, message, uuid, dir, callback, errback) {
|
2012-05-25 19:07:31 -04:00
|
|
|
if (message.status_code != Soup.KnownStatusCode.OK) {
|
2012-06-15 23:20:36 -04:00
|
|
|
errback('DownloadExtensionError', message.status_code);
|
2012-05-25 19:07:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-25 19:12:30 -04:00
|
|
|
try {
|
|
|
|
if (!dir.query_exists(null))
|
|
|
|
dir.make_directory_with_parents(null);
|
|
|
|
} catch (e) {
|
2012-06-15 23:20:36 -04:00
|
|
|
errback('CreateExtensionDirectoryError', e);
|
2012-06-15 22:35:26 -04:00
|
|
|
return;
|
2012-05-25 19:12:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let [file, stream] = Gio.File.new_tmp('XXXXXX.shell-extension.zip');
|
2012-08-23 21:47:23 -04:00
|
|
|
let contents = message.response_body.flatten().get_as_bytes();
|
2012-05-25 19:07:31 -04: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-15 23:20:36 -04:00
|
|
|
errback('ExtractExtensionError');
|
2012-05-25 19:07:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (o, status) => {
|
2012-05-25 19:07:31 -04:00
|
|
|
GLib.spawn_close_pid(pid);
|
|
|
|
|
2012-07-06 19:28:00 -04:00
|
|
|
if (status != 0)
|
2012-06-15 23:20:36 -04:00
|
|
|
errback('ExtractExtensionError');
|
2012-07-06 19:28:00 -04:00
|
|
|
else
|
|
|
|
callback();
|
2012-05-25 19:07:31 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:07:45 -05:00
|
|
|
function downloadExtensionUpdate(uuid) {
|
2020-03-11 15:09:11 -04:00
|
|
|
if (!Main.extensionManager.updatesSupported)
|
|
|
|
return;
|
|
|
|
|
2020-01-22 09:07:45 -05:00
|
|
|
let dir = Gio.File.new_for_path(
|
|
|
|
GLib.build_filenamev([global.userdatadir, 'extension-updates', uuid]));
|
2012-06-26 20:47:44 -04:00
|
|
|
|
|
|
|
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-19 20:20:08 -04:00
|
|
|
_httpSession.queue_message(message, session => {
|
2020-01-22 09:07:45 -05:00
|
|
|
gotExtensionZipFile(session, message, uuid, dir, () => {
|
|
|
|
Main.extensionManager.notifyExtensionUpdate(uuid);
|
2019-08-19 20:20:08 -04:00
|
|
|
}, (code, msg) => {
|
2020-02-14 10:10:34 -05:00
|
|
|
log('Error while downloading update for extension %s: %s (%s)'.format(uuid, code, msg));
|
2012-06-26 20:47:44 -04:00
|
|
|
});
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2012-06-26 20:47:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkForUpdates() {
|
2020-03-11 15:09:11 -04:00
|
|
|
if (!Main.extensionManager.updatesSupported)
|
|
|
|
return;
|
|
|
|
|
2012-06-26 20:47:44 -04:00
|
|
|
let metadatas = {};
|
2019-07-07 18:01:11 -04:00
|
|
|
Main.extensionManager.getUuids().forEach(uuid => {
|
2020-01-22 09:42:06 -05:00
|
|
|
let extension = Main.extensionManager.lookup(uuid);
|
|
|
|
if (extension.type !== ExtensionUtils.ExtensionType.PER_USER)
|
|
|
|
return;
|
2020-01-26 19:13:49 -05:00
|
|
|
if (extension.hasUpdate)
|
|
|
|
return;
|
2020-07-14 00:38:25 -04:00
|
|
|
metadatas[uuid] = {
|
|
|
|
version: extension.metadata.version,
|
|
|
|
};
|
2019-07-07 18:01:11 -04:00
|
|
|
});
|
2012-06-26 20:47:44 -04:00
|
|
|
|
2020-03-15 18:41:27 -04:00
|
|
|
if (Object.keys(metadatas).length === 0)
|
|
|
|
return; // nothing to update
|
|
|
|
|
2020-01-22 10:53:32 -05:00
|
|
|
let versionCheck = global.settings.get_boolean(
|
|
|
|
'disable-extension-version-validation');
|
|
|
|
let params = {
|
|
|
|
shell_version: Config.PACKAGE_VERSION,
|
2020-02-14 10:10:34 -05:00
|
|
|
disable_version_validation: versionCheck.toString(),
|
2020-01-22 10:53:32 -05:00
|
|
|
};
|
2012-06-26 20:47:44 -04:00
|
|
|
|
2021-03-29 14:42:42 -04:00
|
|
|
const uri = Soup.URI.new(REPOSITORY_URL_UPDATE);
|
|
|
|
uri.set_query_from_form(params);
|
|
|
|
|
|
|
|
const message = Soup.Message.new_from_uri('POST', uri);
|
|
|
|
message.set_request(
|
|
|
|
'application/json',
|
|
|
|
Soup.MemoryUse.COPY,
|
|
|
|
JSON.stringify(metadatas)
|
|
|
|
);
|
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
_httpSession.queue_message(message, () => {
|
2012-06-26 20:47:44 -04: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];
|
2020-03-25 19:39:29 -04:00
|
|
|
if (operation === 'upgrade' || operation === 'downgrade')
|
2020-01-22 09:07:45 -05:00
|
|
|
downloadExtensionUpdate(uuid);
|
2012-06-26 20:47:44 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:45:44 -04:00
|
|
|
var InstallExtensionDialog = GObject.registerClass(
|
2017-10-30 21:19:44 -04:00
|
|
|
class InstallExtensionDialog extends ModalDialog.ModalDialog {
|
2019-05-23 16:45:44 -04:00
|
|
|
_init(uuid, info, invocation) {
|
|
|
|
super._init({ styleClass: 'extension-dialog' });
|
2012-05-25 19:07:31 -04:00
|
|
|
|
|
|
|
this._uuid = uuid;
|
|
|
|
this._info = info;
|
2012-06-15 23:20:36 -04:00
|
|
|
this._invocation = invocation;
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2019-02-12 09:02:09 -05:00
|
|
|
this.setButtons([{
|
|
|
|
label: _("Cancel"),
|
|
|
|
action: this._onCancelButtonPressed.bind(this),
|
2019-11-05 14:37:28 -05:00
|
|
|
key: Clutter.KEY_Escape,
|
2019-02-12 09:02:09 -05:00
|
|
|
}, {
|
|
|
|
label: _("Install"),
|
|
|
|
action: this._onInstallButtonPressed.bind(this),
|
|
|
|
default: true,
|
|
|
|
}]);
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2019-06-26 13:51:18 -04:00
|
|
|
let content = new Dialog.MessageDialogContent({
|
2020-01-27 15:51:00 -05:00
|
|
|
title: _('Install Extension'),
|
|
|
|
description: _('Download and install “%s” from extensions.gnome.org?').format(info.name),
|
2019-06-26 13:51:18 -04:00
|
|
|
});
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2019-06-26 13:51:18 -04:00
|
|
|
this.contentLayout.add(content);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_onCancelButtonPressed() {
|
2013-02-20 13:12:42 -05:00
|
|
|
this.close();
|
2012-06-15 23:20:36 -04:00
|
|
|
this._invocation.return_value(GLib.Variant.new('(s)', ['cancelled']));
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_onInstallButtonPressed() {
|
2012-05-25 19:07:31 -04: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 19:28:00 -04:00
|
|
|
let uuid = this._uuid;
|
2012-06-26 20:47:44 -04:00
|
|
|
let dir = Gio.File.new_for_path(GLib.build_filenamev([global.userdatadir, 'extensions', uuid]));
|
2012-06-15 23:20:36 -04:00
|
|
|
let invocation = this._invocation;
|
2019-08-19 20:20:08 -04:00
|
|
|
function errback(code, msg) {
|
2020-02-14 10:10:34 -05:00
|
|
|
log('Error while installing %s: %s (%s)'.format(uuid, code, msg));
|
|
|
|
invocation.return_dbus_error('org.gnome.Shell.%s'.format(code), msg || '');
|
2012-06-15 23:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function callback() {
|
2012-07-06 19:28:00 -04:00
|
|
|
try {
|
2019-07-07 17:38:27 -04:00
|
|
|
let extension = Main.extensionManager.createExtensionObject(uuid, dir, ExtensionUtils.ExtensionType.PER_USER);
|
2019-03-06 19:45:45 -05:00
|
|
|
Main.extensionManager.loadExtension(extension);
|
2018-01-17 07:43:11 -05:00
|
|
|
if (!Main.extensionManager.enableExtension(uuid))
|
2020-02-14 10:10:34 -05:00
|
|
|
throw new Error('Cannot add %s to enabled extensions gsettings key'.format(uuid));
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2012-07-06 19:28:00 -04:00
|
|
|
uninstallExtension(uuid);
|
|
|
|
errback('LoadExtensionError', e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:14:23 -05:00
|
|
|
invocation.return_value(GLib.Variant.new('(s)', ['successful']));
|
2012-06-15 23:20:36 -04:00
|
|
|
}
|
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
_httpSession.queue_message(message, session => {
|
2012-06-26 20:47:44 -04:00
|
|
|
gotExtensionZipFile(session, message, uuid, dir, callback, errback);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2012-05-25 19:07:31 -04:00
|
|
|
|
2013-02-20 13:12:42 -05:00
|
|
|
this.close();
|
2012-05-25 19:07:31 -04:00
|
|
|
}
|
2019-05-23 16:45:44 -04:00
|
|
|
});
|
2012-05-25 19:07:31 -04:00
|
|
|
|
|
|
|
function init() {
|
2020-05-14 14:40:57 -04:00
|
|
|
_httpSession = new Soup.Session({ ssl_use_system_ca_file: true });
|
2012-05-25 19:07:31 -04:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|