Bug 1586798 - Use WalkerFront from the currently selected element in onTagEdit()...
[gecko.git] / testing / marionette / addon.js
blobae5fe672779424f26ace060adbe4fb6c7e74145d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const { AddonManager } = ChromeUtils.import(
8   "resource://gre/modules/AddonManager.jsm"
9 );
10 const { FileUtils } = ChromeUtils.import(
11   "resource://gre/modules/FileUtils.jsm"
14 const { UnknownError } = ChromeUtils.import(
15   "chrome://marionette/content/error.js"
18 this.EXPORTED_SYMBOLS = ["Addon"];
20 // from https://developer.mozilla.org/en-US/Add-ons/Add-on_Manager/AddonManager#AddonInstall_errors
21 const ERRORS = {
22   [-1]: "ERROR_NETWORK_FAILURE: A network error occured.",
23   [-2]: "ERROR_INCORECT_HASH: The downloaded file did not match the expected hash.",
24   [-3]: "ERROR_CORRUPT_FILE: The file appears to be corrupt.",
25   [-4]: "ERROR_FILE_ACCESS: There was an error accessing the filesystem.",
26   [-5]: "ERROR_SIGNEDSTATE_REQUIRED: The addon must be signed and isn't.",
29 async function installAddon(file) {
30   let install = await AddonManager.getInstallForFile(file, null, {
31     source: "internal",
32   });
34   if (install.error) {
35     throw new UnknownError(ERRORS[install.error]);
36   }
38   return install.install().catch(err => {
39     throw new UnknownError(ERRORS[install.error]);
40   });
43 /** Installs addons by path and uninstalls by ID. */
44 class Addon {
45   /**
46    * Install a Firefox addon.
47    *
48    * If the addon is restartless, it can be used right away.  Otherwise a
49    * restart is required.
50    *
51    * Temporary addons will automatically be uninstalled on shutdown and
52    * do not need to be signed, though they must be restartless.
53    *
54    * @param {string} path
55    *     Full path to the extension package archive.
56    * @param {boolean=} temporary
57    *     True to install the addon temporarily, false (default) otherwise.
58    *
59    * @return {Promise.<string>}
60    *     Addon ID.
61    *
62    * @throws {UnknownError}
63    *     If there is a problem installing the addon.
64    */
65   static async install(path, temporary = false) {
66     let addon;
67     let file;
69     try {
70       file = new FileUtils.File(path);
71     } catch (e) {
72       throw new UnknownError(`Expected absolute path: ${e}`, e);
73     }
75     if (!file.exists()) {
76       throw new UnknownError(`No such file or directory: ${path}`);
77     }
79     try {
80       if (temporary) {
81         addon = await AddonManager.installTemporaryAddon(file);
82       } else {
83         addon = await installAddon(file);
84       }
85     } catch (e) {
86       throw new UnknownError(
87         `Could not install add-on: ${path}: ${e.message}`,
88         e
89       );
90     }
92     return addon.id;
93   }
95   /**
96    * Uninstall a Firefox addon.
97    *
98    * If the addon is restartless it will be uninstalled right away.
99    * Otherwise, Firefox must be restarted for the change to take effect.
100    *
101    * @param {string} id
102    *     ID of the addon to uninstall.
103    *
104    * @return {Promise}
105    *
106    * @throws {UnknownError}
107    *     If there is a problem uninstalling the addon.
108    */
109   static async uninstall(id) {
110     let candidate = await AddonManager.getAddonByID(id);
112     return new Promise(resolve => {
113       let listener = {
114         onOperationCancelled: addon => {
115           if (addon.id === candidate.id) {
116             AddonManager.removeAddonListener(listener);
117             throw new UnknownError(
118               `Uninstall of ${candidate.id} has been canceled`
119             );
120           }
121         },
123         onUninstalled: addon => {
124           if (addon.id === candidate.id) {
125             AddonManager.removeAddonListener(listener);
126             resolve();
127           }
128         },
129       };
131       AddonManager.addAddonListener(listener);
132       candidate.uninstall();
133     });
134   }
136 this.Addon = Addon;