Bug 1456051 - Make addon an ES module. r=maja_zf

This does not really functionally change anything, but it prevents
us from polluting the global space in testing/marionette/driver.js.

MozReview-Commit-ID: Guwt3g0lS8q

--HG--
extra : rebase_source : ad2c38e51d54b7c4ead6b8d6a49d05a0dfef7c6c
This commit is contained in:
Andreas Tolfsen 2018-04-23 08:12:30 +01:00
Родитель 921d8f6a6e
Коммит 916ef8de56
3 изменённых файлов: 91 добавлений и 87 удалений

Просмотреть файл

@ -9,10 +9,7 @@ ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
const {UnknownError} = ChromeUtils.import("chrome://marionette/content/error.js", {});
this.EXPORTED_SYMBOLS = ["addon"];
/** @namespace */
this.addon = {};
this.EXPORTED_SYMBOLS = ["Addon"];
// from https://developer.mozilla.org/en-US/Add-ons/Add-on_Manager/AddonManager#AddonInstall_errors
const ERRORS = {
@ -60,89 +57,93 @@ async function installAddon(file) {
});
}
/**
* Install a Firefox addon.
*
* If the addon is restartless, it can be used right away. Otherwise a
* restart is required.
*
* Temporary addons will automatically be uninstalled on shutdown and
* do not need to be signed, though they must be restartless.
*
* @param {string} path
* Full path to the extension package archive.
* @param {boolean=} temporary
* True to install the addon temporarily, false (default) otherwise.
*
* @return {Promise.<string>}
* Addon ID.
*
* @throws {UnknownError}
* If there is a problem installing the addon.
*/
addon.install = async function(path, temporary = false) {
let addon;
let file;
/** Installs addons by path and uninstalls by ID. */
class Addon {
/**
* Install a Firefox addon.
*
* If the addon is restartless, it can be used right away. Otherwise a
* restart is required.
*
* Temporary addons will automatically be uninstalled on shutdown and
* do not need to be signed, though they must be restartless.
*
* @param {string} path
* Full path to the extension package archive.
* @param {boolean=} temporary
* True to install the addon temporarily, false (default) otherwise.
*
* @return {Promise.<string>}
* Addon ID.
*
* @throws {UnknownError}
* If there is a problem installing the addon.
*/
static async install(path, temporary = false) {
let addon;
let file;
try {
file = new FileUtils.File(path);
} catch (e) {
throw new UnknownError(`Expected absolute path: ${e}`, e);
}
if (!file.exists()) {
throw new UnknownError(`No such file or directory: ${path}`);
}
try {
if (temporary) {
addon = await AddonManager.installTemporaryAddon(file);
} else {
addon = await installAddon(file);
try {
file = new FileUtils.File(path);
} catch (e) {
throw new UnknownError(`Expected absolute path: ${e}`, e);
}
} catch (e) {
throw new UnknownError(
`Could not install add-on: ${path}: ${e.message}`, e);
if (!file.exists()) {
throw new UnknownError(`No such file or directory: ${path}`);
}
try {
if (temporary) {
addon = await AddonManager.installTemporaryAddon(file);
} else {
addon = await installAddon(file);
}
} catch (e) {
throw new UnknownError(
`Could not install add-on: ${path}: ${e.message}`, e);
}
return addon.id;
}
return addon.id;
};
/**
* Uninstall a Firefox addon.
*
* If the addon is restartless it will be uninstalled right away.
* Otherwise, Firefox must be restarted for the change to take effect.
*
* @param {string} id
* ID of the addon to uninstall.
*
* @return {Promise}
*
* @throws {UnknownError}
* If there is a problem uninstalling the addon.
*/
static async uninstall(id) {
let candidate = await AddonManager.getAddonByID(id);
/**
* Uninstall a Firefox addon.
*
* If the addon is restartless it will be uninstalled right away.
* Otherwise, Firefox must be restarted for the change to take effect.
*
* @param {string} id
* ID of the addon to uninstall.
*
* @return {Promise}
*
* @throws {UnknownError}
* If there is a problem uninstalling the addon.
*/
addon.uninstall = async function(id) {
let candidate = await AddonManager.getAddonByID(id);
return new Promise(resolve => {
let listener = {
onOperationCancelled: addon => {
if (addon.id === candidate.id) {
AddonManager.removeAddonListener(listener);
throw new UnknownError(`Uninstall of ${candidate.id} has been canceled`);
}
},
return new Promise(resolve => {
let listener = {
onOperationCancelled: addon => {
if (addon.id === candidate.id) {
AddonManager.removeAddonListener(listener);
throw new UnknownError(`Uninstall of ${candidate.id} has been canceled`);
}
},
onUninstalled: addon => {
if (addon.id === candidate.id) {
AddonManager.removeAddonListener(listener);
resolve();
}
},
};
onUninstalled: addon => {
if (addon.id === candidate.id) {
AddonManager.removeAddonListener(listener);
resolve();
}
},
};
AddonManager.addAddonListener(listener);
addon.uninstall();
});
};
AddonManager.addAddonListener(listener);
candidate.uninstall();
});
}
}
this.Addon = Addon;

Просмотреть файл

@ -1,4 +1,7 @@
addon module
============
.. js:autoclass:: addon
Addon
-----
.. js:autoclass:: Addon
:members:

Просмотреть файл

@ -11,7 +11,7 @@ ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("chrome://marionette/content/accessibility.js");
ChromeUtils.import("chrome://marionette/content/addon.js");
const {Addon} = ChromeUtils.import("chrome://marionette/content/addon.js", {});
ChromeUtils.import("chrome://marionette/content/assert.js");
ChromeUtils.import("chrome://marionette/content/atom.js");
const {
@ -3296,7 +3296,7 @@ GeckoDriver.prototype.installAddon = function(cmd) {
throw new InvalidArgumentError();
}
return addon.install(path, temp);
return Addon.install(path, temp);
};
GeckoDriver.prototype.uninstallAddon = function(cmd) {
@ -3307,7 +3307,7 @@ GeckoDriver.prototype.uninstallAddon = function(cmd) {
throw new InvalidArgumentError();
}
return addon.uninstall(id);
return Addon.uninstall(id);
};
/** Receives all messages from content messageManager. */