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";
|
|
|
|
|
2016-08-26 15:38:03 +03:00
|
|
|
const {interfaces: Ci, utils: Cu} = Components;
|
2016-08-25 16:18:04 +03:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/AddonManager.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
|
|
|
|
|
|
|
Cu.import("chrome://marionette/content/error.js");
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["addon"];
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2016-08-26 15:38:03 +03:00
|
|
|
* @return {Promise: string}
|
|
|
|
* 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
|
|
|
*/
|
2016-12-04 14:42:52 +03:00
|
|
|
addon.install = function (path, temporary = false) {
|
2016-08-25 16:18:04 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-08-26 15:38:03 +03:00
|
|
|
let file = new FileUtils.File(path);
|
|
|
|
|
2016-08-25 16:18:04 +03:00
|
|
|
let listener = {
|
2016-12-04 14:42:52 +03:00
|
|
|
onInstallEnded: function (install, addon) {
|
2016-08-25 16:18:04 +03:00
|
|
|
resolve(addon.id);
|
|
|
|
},
|
|
|
|
|
2016-12-04 14:42:52 +03:00
|
|
|
onInstallFailed: function (install) {
|
2016-08-26 15:38:03 +03:00
|
|
|
reject(lookupError(install.error));
|
2016-08-25 16:18:04 +03:00
|
|
|
},
|
|
|
|
|
2016-12-04 14:42:52 +03:00
|
|
|
onInstalled: function (addon) {
|
2016-08-25 16:18:04 +03:00
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
resolve(addon.id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-26 15:38:03 +03:00
|
|
|
if (!temporary) {
|
2016-12-04 14:42:52 +03:00
|
|
|
AddonManager.getInstallForFile(file, function (aInstall) {
|
2016-08-26 15:38:03 +03:00
|
|
|
if (aInstall.error !== 0) {
|
|
|
|
reject(lookupError(aInstall.error));
|
2016-08-25 16:18:04 +03:00
|
|
|
}
|
|
|
|
aInstall.addListener(listener);
|
|
|
|
aInstall.install();
|
|
|
|
});
|
2016-08-26 15:38:03 +03:00
|
|
|
} else {
|
|
|
|
AddonManager.addAddonListener(listener);
|
|
|
|
AddonManager.installTemporaryAddon(file);
|
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}
|
|
|
|
*/
|
2016-12-04 14:42:52 +03:00
|
|
|
addon.uninstall = function (id) {
|
2016-08-25 16:18:04 +03:00
|
|
|
return new Promise(resolve => {
|
2016-12-04 14:42:52 +03:00
|
|
|
AddonManager.getAddonByID(id, function (addon) {
|
2016-08-25 16:18:04 +03:00
|
|
|
addon.uninstall();
|
2016-08-26 15:38:03 +03:00
|
|
|
resolve();
|
2016-08-25 16:18:04 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|