2012-05-25 23:07:31 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2020-01-22 14:07:45 +00:00
|
|
|
/* exported init, installExtension, uninstallExtension, checkForUpdates */
|
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;
|
|
|
|
|
2022-02-10 23:09:54 +00:00
|
|
|
Gio._promisify(Soup.Session.prototype, 'send_and_read_async');
|
|
|
|
Gio._promisify(Gio.OutputStream.prototype, 'write_bytes_async');
|
|
|
|
Gio._promisify(Gio.IOStream.prototype, 'close_async');
|
|
|
|
Gio._promisify(Gio.Subprocess.prototype, 'wait_check_async');
|
2021-08-06 22:07:30 +00:00
|
|
|
|
2020-02-14 15:10:34 +00: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 23:07:31 +00:00
|
|
|
|
|
|
|
let _httpSession;
|
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
/**
|
|
|
|
* @param {string} uuid - extension uuid
|
|
|
|
* @param {Gio.DBusMethodInvocation} invocation - the caller
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async function installExtension(uuid, invocation) {
|
2021-08-06 22:47:24 +00:00
|
|
|
const params = {
|
|
|
|
uuid,
|
|
|
|
shell_version: Config.PACKAGE_VERSION,
|
|
|
|
};
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
const message = Soup.Message.new_from_encoded_form('GET',
|
|
|
|
REPOSITORY_URL_INFO,
|
|
|
|
Soup.form_encode_hash(params));
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
let info;
|
|
|
|
try {
|
|
|
|
const bytes = await _httpSession.send_and_read_async(
|
|
|
|
message,
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
|
|
|
null);
|
|
|
|
checkResponse(message);
|
2021-08-12 14:38:57 +00:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
info = JSON.parse(decoder.decode(bytes.get_data()));
|
2021-08-03 23:35:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
Main.extensionManager.logExtensionError(uuid, e);
|
|
|
|
invocation.return_dbus_error(
|
|
|
|
'org.gnome.Shell.ExtensionError', e.message);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-16 02:38:55 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
const dialog = new InstallExtensionDialog(uuid, info, invocation);
|
|
|
|
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
|
2021-08-06 22:47:24 +00:00
|
|
|
if (extension.type !== ExtensionUtils.ExtensionType.PER_USER)
|
2012-05-25 23:07:31 +00:00
|
|
|
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);
|
2020-03-09 15:49:34 +00: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 23:07:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-06 22:07:30 +00:00
|
|
|
/**
|
|
|
|
* Check return status of reponse
|
|
|
|
*
|
|
|
|
* @param {Soup.Message} message - an http response
|
|
|
|
* @returns {void}
|
|
|
|
* @throws
|
|
|
|
*/
|
|
|
|
function checkResponse(message) {
|
|
|
|
const { statusCode } = message;
|
|
|
|
const phrase = Soup.Status.get_phrase(statusCode);
|
2021-08-03 23:35:08 +00:00
|
|
|
if (statusCode !== Soup.Status.OK)
|
2022-02-07 14:14:06 +00:00
|
|
|
throw new Error(`Unexpected response: ${phrase}`);
|
2021-08-06 22:07:30 +00:00
|
|
|
}
|
2012-05-25 23:12:30 +00:00
|
|
|
|
2021-08-06 22:07:30 +00:00
|
|
|
/**
|
|
|
|
* @param {GLib.Bytes} bytes - archive data
|
|
|
|
* @param {Gio.File} dir - target directory
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async function extractExtensionArchive(bytes, dir) {
|
|
|
|
if (!dir.query_exists(null))
|
|
|
|
dir.make_directory_with_parents(null);
|
|
|
|
|
|
|
|
const [file, stream] = Gio.File.new_tmp('XXXXXX.shell-extension.zip');
|
|
|
|
await stream.output_stream.write_bytes_async(bytes,
|
|
|
|
GLib.PRIORITY_DEFAULT, null);
|
|
|
|
stream.close_async(GLib.PRIORITY_DEFAULT, null);
|
|
|
|
|
|
|
|
const unzip = Gio.Subprocess.new(
|
2021-08-06 22:47:24 +00:00
|
|
|
['unzip', '-uod', dir.get_path(), '--', file.get_path()],
|
2021-08-06 22:07:30 +00:00
|
|
|
Gio.SubprocessFlags.NONE);
|
|
|
|
await unzip.wait_check_async(null);
|
2012-05-25 23:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
/**
|
|
|
|
* @param {string} uuid - extension uuid
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async function downloadExtensionUpdate(uuid) {
|
2020-03-11 19:09:11 +00:00
|
|
|
if (!Main.extensionManager.updatesSupported)
|
|
|
|
return;
|
|
|
|
|
2021-08-06 22:47:24 +00:00
|
|
|
const dir = Gio.File.new_for_path(
|
2020-01-22 14:07:45 +00:00
|
|
|
GLib.build_filenamev([global.userdatadir, 'extension-updates', uuid]));
|
2012-06-27 00:47:44 +00:00
|
|
|
|
2021-08-06 22:47:24 +00:00
|
|
|
const params = { shell_version: Config.PACKAGE_VERSION };
|
2021-08-03 23:35:08 +00:00
|
|
|
const message = Soup.Message.new_from_encoded_form('GET',
|
|
|
|
REPOSITORY_URL_DOWNLOAD.format(uuid),
|
|
|
|
Soup.form_encode_hash(params));
|
2012-06-27 00:47:44 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
try {
|
|
|
|
const bytes = await _httpSession.send_and_read_async(
|
|
|
|
message,
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
|
|
|
null);
|
|
|
|
checkResponse(message);
|
|
|
|
|
|
|
|
await extractExtensionArchive(bytes, dir);
|
|
|
|
Main.extensionManager.notifyExtensionUpdate(uuid);
|
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Error while downloading update for extension ${uuid}: (${e.message})`);
|
2021-08-03 23:35:08 +00:00
|
|
|
}
|
2012-06-27 00:47:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
/**
|
|
|
|
* Check extensions.gnome.org for updates
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async function checkForUpdates() {
|
2020-03-11 19:09:11 +00:00
|
|
|
if (!Main.extensionManager.updatesSupported)
|
|
|
|
return;
|
|
|
|
|
2012-06-27 00:47:44 +00:00
|
|
|
let metadatas = {};
|
2019-07-07 22:01:11 +00:00
|
|
|
Main.extensionManager.getUuids().forEach(uuid => {
|
2020-01-22 14:42:06 +00:00
|
|
|
let extension = Main.extensionManager.lookup(uuid);
|
|
|
|
if (extension.type !== ExtensionUtils.ExtensionType.PER_USER)
|
|
|
|
return;
|
2020-01-27 00:13:49 +00:00
|
|
|
if (extension.hasUpdate)
|
|
|
|
return;
|
2020-07-14 04:38:25 +00:00
|
|
|
metadatas[uuid] = {
|
|
|
|
version: extension.metadata.version,
|
|
|
|
};
|
2019-07-07 22:01:11 +00:00
|
|
|
});
|
2012-06-27 00:47:44 +00:00
|
|
|
|
2020-03-15 22:41:27 +00:00
|
|
|
if (Object.keys(metadatas).length === 0)
|
|
|
|
return; // nothing to update
|
|
|
|
|
2021-08-06 22:47:24 +00:00
|
|
|
const versionCheck = global.settings.get_boolean(
|
2020-01-22 15:53:32 +00:00
|
|
|
'disable-extension-version-validation');
|
2021-08-06 22:47:24 +00:00
|
|
|
const params = {
|
2020-01-22 15:53:32 +00:00
|
|
|
shell_version: Config.PACKAGE_VERSION,
|
2022-02-07 14:14:06 +00:00
|
|
|
disable_version_validation: `${versionCheck}`,
|
2020-01-22 15:53:32 +00:00
|
|
|
};
|
2021-08-12 14:38:57 +00:00
|
|
|
const requestBody = new GLib.Bytes(JSON.stringify(metadatas));
|
2012-06-27 00:47:44 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
const message = Soup.Message.new('POST',
|
2022-02-07 14:14:06 +00:00
|
|
|
`${REPOSITORY_URL_UPDATE}?${Soup.form_encode_hash(params)}`);
|
2021-08-03 23:35:08 +00:00
|
|
|
message.set_request_body_from_bytes('application/json', requestBody);
|
2021-03-29 18:42:42 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
let json;
|
|
|
|
try {
|
|
|
|
const bytes = await _httpSession.send_and_read_async(
|
|
|
|
message,
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
|
|
|
null);
|
|
|
|
checkResponse(message);
|
2021-08-12 14:38:57 +00:00
|
|
|
json = new TextDecoder().decode(bytes.get_data());
|
2021-08-03 23:35:08 +00:00
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Update check failed: ${e.message}`);
|
2021-08-03 23:35:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-29 18:42:42 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
const operations = JSON.parse(json);
|
|
|
|
const updates = [];
|
|
|
|
for (const uuid in operations) {
|
|
|
|
const operation = operations[uuid];
|
|
|
|
if (operation === 'upgrade' || operation === 'downgrade')
|
|
|
|
updates.push(uuid);
|
|
|
|
}
|
2012-06-27 00:47:44 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
try {
|
|
|
|
await Promise.allSettled(
|
|
|
|
updates.map(uuid => downloadExtensionUpdate(uuid)));
|
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Some extension updates failed to download: ${e.message}`);
|
2021-08-03 23:35:08 +00:00
|
|
|
}
|
2012-06-27 00:47:44 +00:00
|
|
|
}
|
|
|
|
|
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([{
|
2021-08-06 22:47:24 +00:00
|
|
|
label: _('Cancel'),
|
2019-02-12 14:02:09 +00:00
|
|
|
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
|
|
|
}, {
|
2021-08-06 22:47:24 +00:00
|
|
|
label: _('Install'),
|
2019-02-12 14:02:09 +00:00
|
|
|
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({
|
2020-01-27 20:51:00 +00:00
|
|
|
title: _('Install Extension'),
|
|
|
|
description: _('Download and install “%s” from extensions.gnome.org?').format(info.name),
|
2019-06-26 17:51:18 +00:00
|
|
|
});
|
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
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
async _onInstallButtonPressed() {
|
|
|
|
this.close();
|
|
|
|
|
2021-08-06 22:47:24 +00:00
|
|
|
const params = { shell_version: Config.PACKAGE_VERSION };
|
2021-08-03 23:35:08 +00:00
|
|
|
const message = Soup.Message.new_from_encoded_form('GET',
|
|
|
|
REPOSITORY_URL_DOWNLOAD.format(this._uuid),
|
|
|
|
Soup.form_encode_hash(params));
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
const dir = Gio.File.new_for_path(
|
|
|
|
GLib.build_filenamev([global.userdatadir, 'extensions', this._uuid]));
|
2012-05-25 23:07:31 +00:00
|
|
|
|
2021-08-03 23:35:08 +00:00
|
|
|
try {
|
|
|
|
const bytes = await _httpSession.send_and_read_async(
|
|
|
|
message,
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
|
|
|
null);
|
|
|
|
checkResponse(message);
|
|
|
|
|
|
|
|
await extractExtensionArchive(bytes, dir);
|
|
|
|
|
|
|
|
const extension = Main.extensionManager.createExtensionObject(
|
|
|
|
this._uuid, dir, ExtensionUtils.ExtensionType.PER_USER);
|
|
|
|
Main.extensionManager.loadExtension(extension);
|
|
|
|
if (!Main.extensionManager.enableExtension(this._uuid))
|
2022-02-07 14:14:06 +00:00
|
|
|
throw new Error(`Cannot enable ${this._uuid}`);
|
2021-08-03 23:35:08 +00:00
|
|
|
|
|
|
|
this._invocation.return_value(new GLib.Variant('(s)', ['successful']));
|
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Error while installing ${this._uuid}: ${e.message}`);
|
2021-08-03 23:35:08 +00:00
|
|
|
this._invocation.return_dbus_error(
|
|
|
|
'org.gnome.Shell.ExtensionError', e.message);
|
|
|
|
}
|
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() {
|
2021-08-03 22:28:25 +00:00
|
|
|
_httpSession = new Soup.Session();
|
2012-05-25 23:07:31 +00:00
|
|
|
}
|