summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/AddonManagerTesting.jsm
blob: eb9a921959cf2b109ecf3e819e36be03ebc4aefe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* 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/. */

// This file is a test-only JSM containing utility methods for
// interacting with the add-ons manager.

"use strict";

this.EXPORTED_SYMBOLS = [
  "AddonManagerTesting",
];

const {utils: Cu} = Components;

Cu.import("resource://gre/modules/Promise.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
                                  "resource://gre/modules/AddonManager.jsm");

this.AddonManagerTesting = {
  /**
   * Get the add-on that is specified by its ID.
   *
   * @return {Promise<Object>} A promise that resolves returning the found addon or null
   *         if it is not found.
   */
  getAddonById: function (id) {
    return new Promise(resolve => AddonManager.getAddonByID(id, addon => resolve(addon)));
  },

  /**
   * Uninstall an add-on that is specified by its ID.
   *
   * The returned promise resolves on successful uninstall and rejects
   * if the add-on is not unknown.
   *
   * @return Promise<restartRequired>
   */
  uninstallAddonByID: function (id) {
    let deferred = Promise.defer();

    AddonManager.getAddonByID(id, (addon) => {
      if (!addon) {
        deferred.reject(new Error("Add-on is not known: " + id));
        return;
      }

      let listener = {
        onUninstalling: function (addon, needsRestart) {
          if (addon.id != id) {
            return;
          }

          if (needsRestart) {
            AddonManager.removeAddonListener(listener);
            deferred.resolve(true);
          }
        },

        onUninstalled: function (addon) {
          if (addon.id != id) {
            return;
          }

          AddonManager.removeAddonListener(listener);
          deferred.resolve(false);
        },

        onOperationCancelled: function (addon) {
          if (addon.id != id) {
            return;
          }

          AddonManager.removeAddonListener(listener);
          deferred.reject(new Error("Uninstall cancelled."));
        },
      };

      AddonManager.addAddonListener(listener);
      addon.uninstall();
    });

    return deferred.promise;
  },

  /**
   * Install an XPI add-on from a URL.
   *
   * @return Promise<addon>
   */
  installXPIFromURL: function (url, hash, name, iconURL, version) {
    let deferred = Promise.defer();

    AddonManager.getInstallForURL(url, (install) => {
      let fail = () => { deferred.reject(new Error("Add-on install failed.")) };

      let listener = {
        onDownloadCancelled: fail,
        onDownloadFailed: fail,
        onInstallCancelled: fail,
        onInstallFailed: fail,
        onInstallEnded: function (install, addon) {
          deferred.resolve(addon);
        },
      };

      install.addListener(listener);
      install.install();
    }, "application/x-xpinstall", hash, name, iconURL, version);

    return deferred.promise;
  },
};