2016-08-25 16:18:04 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
|
2018-01-30 03:32:43 +03:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
const {UnknownError} = ChromeUtils.import("chrome://marionette/content/error.js", {});
|
2016-08-25 16:18:04 +03:00
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["addon"];
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/** @namespace */
|
2016-08-25 16:18:04 +03:00
|
|
|
this.addon = {};
|
|
|
|
|
2016-08-26 15:38:03 +03:00
|
|
|
// from https://developer.mozilla.org/en-US/Add-ons/Add-on_Manager/AddonManager#AddonInstall_errors
|
|
|
|
addon.Errors = {
|
|
|
|
[-1]: "ERROR_NETWORK_FAILURE: A network error occured.",
|
|
|
|
[-2]: "ERROR_INCORECT_HASH: The downloaded file did not match the expected hash.",
|
|
|
|
[-3]: "ERROR_CORRUPT_FILE: The file appears to be corrupt.",
|
|
|
|
[-4]: "ERROR_FILE_ACCESS: There was an error accessing the filesystem.",
|
|
|
|
[-5]: "ERROR_SIGNEDSTATE_REQUIRED: The addon must be signed and isn't.",
|
|
|
|
};
|
|
|
|
|
|
|
|
function lookupError(code) {
|
|
|
|
let msg = addon.Errors[code];
|
|
|
|
return new UnknownError(msg);
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:53:45 +03:00
|
|
|
async function installAddon(file) {
|
|
|
|
let install = await AddonManager.getInstallForFile(file);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (install.error != 0) {
|
|
|
|
reject(new UnknownError(lookupError(install.error)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let addonId = install.addon.id;
|
|
|
|
|
|
|
|
let success = install => {
|
|
|
|
if (install.addon.id === addonId) {
|
|
|
|
install.removeListener(listener);
|
|
|
|
resolve(install.addon);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let fail = install => {
|
|
|
|
if (install.addon.id === addonId) {
|
|
|
|
install.removeListener(listener);
|
|
|
|
reject(new UnknownError(lookupError(install.error)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let listener = {
|
|
|
|
onDownloadCancelled: fail,
|
|
|
|
onDownloadFailed: fail,
|
|
|
|
onInstallCancelled: fail,
|
|
|
|
onInstallFailed: fail,
|
|
|
|
onInstallEnded: success,
|
|
|
|
};
|
|
|
|
|
|
|
|
install.addListener(listener);
|
|
|
|
install.install();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-25 16:18:04 +03:00
|
|
|
/**
|
2016-08-26 15:38:03 +03:00
|
|
|
* Install a Firefox addon.
|
2016-08-25 16:18:04 +03:00
|
|
|
*
|
2016-08-26 15:38:03 +03:00
|
|
|
* If the addon is restartless, it can be used right away. Otherwise a
|
|
|
|
* restart is required.
|
2016-08-25 16:18:04 +03:00
|
|
|
*
|
2016-08-26 15:38:03 +03:00
|
|
|
* Temporary addons will automatically be uninstalled on shutdown and
|
2016-08-25 16:18:04 +03:00
|
|
|
* do not need to be signed, though they must be restartless.
|
|
|
|
*
|
|
|
|
* @param {string} path
|
2016-08-26 15:38:03 +03:00
|
|
|
* Full path to the extension package archive.
|
2016-08-25 16:18:04 +03:00
|
|
|
* @param {boolean=} temporary
|
2016-08-26 15:38:03 +03:00
|
|
|
* True to install the addon temporarily, false (default) otherwise.
|
2016-08-25 16:18:04 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @return {Promise.<string>}
|
2016-08-26 15:38:03 +03:00
|
|
|
* Addon ID.
|
2016-08-25 16:18:04 +03:00
|
|
|
*
|
2016-08-26 15:38:03 +03:00
|
|
|
* @throws {UnknownError}
|
|
|
|
* If there is a problem installing the addon.
|
2016-08-25 16:18:04 +03:00
|
|
|
*/
|
2017-08-23 16:53:45 +03:00
|
|
|
addon.install = async function(path, temporary = false) {
|
|
|
|
let addon;
|
2017-10-20 09:31:52 +03:00
|
|
|
let file;
|
|
|
|
|
|
|
|
try {
|
|
|
|
file = new FileUtils.File(path);
|
|
|
|
} catch (e) {
|
|
|
|
throw new UnknownError(`${path} is not an absolute path.`);
|
|
|
|
}
|
2016-08-25 16:18:04 +03:00
|
|
|
|
2017-09-24 12:08:44 +03:00
|
|
|
if (!file.exists()) {
|
|
|
|
throw new UnknownError(`Could not find add-on at '${path}'`);
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:53:45 +03:00
|
|
|
try {
|
|
|
|
if (temporary) {
|
|
|
|
addon = await AddonManager.installTemporaryAddon(file);
|
2016-08-26 15:38:03 +03:00
|
|
|
} else {
|
2017-08-23 16:53:45 +03:00
|
|
|
addon = await installAddon(file);
|
2016-08-25 16:18:04 +03:00
|
|
|
}
|
2017-08-23 16:53:45 +03:00
|
|
|
} catch (e) {
|
|
|
|
throw new UnknownError(
|
|
|
|
`Could not install add-on at '${path}': ${e.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return addon.id;
|
2016-08-25 16:18:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstall a Firefox addon.
|
|
|
|
*
|
2016-08-26 15:38:03 +03:00
|
|
|
* If the addon is restartless it will be uninstalled right away.
|
|
|
|
* Otherwise, Firefox must be restarted for the change to take effect.
|
2016-08-25 16:18:04 +03:00
|
|
|
*
|
|
|
|
* @param {string} id
|
2016-08-26 15:38:03 +03:00
|
|
|
* ID of the addon to uninstall.
|
2016-08-25 16:18:04 +03:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
2017-08-23 16:53:45 +03:00
|
|
|
*
|
|
|
|
* @throws {UnknownError}
|
|
|
|
* If there is a problem uninstalling the addon.
|
2016-08-25 16:18:04 +03:00
|
|
|
*/
|
2017-08-23 16:53:45 +03:00
|
|
|
addon.uninstall = async function(id) {
|
|
|
|
return AddonManager.getAddonByID(id).then(addon => {
|
|
|
|
let listener = {
|
|
|
|
onOperationCancelled: addon => {
|
|
|
|
if (addon.id === id) {
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
throw new UnknownError(`Uninstall of ${id} has been canceled`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onUninstalled: addon => {
|
|
|
|
if (addon.id === id) {
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
Promise.resolve();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
AddonManager.addAddonListener(listener);
|
|
|
|
addon.uninstall();
|
2016-08-25 16:18:04 +03:00
|
|
|
});
|
|
|
|
};
|