summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/ext-management.js
blob: 59a7959d7829dee2a1613383fa2269b9633d216b (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;

XPCOMUtils.defineLazyGetter(this, "strBundle", function() {
  const stringSvc = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
  return stringSvc.createBundle("chrome://global/locale/extensions.properties");
});
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
                                  "resource://gre/modules/AddonManager.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "promptService",
                                   "@mozilla.org/embedcomp/prompt-service;1",
                                   "nsIPromptService");

function _(key, ...args) {
  if (args.length) {
    return strBundle.formatStringFromName(key, args, args.length);
  }
  return strBundle.GetStringFromName(key);
}

function installType(addon) {
  if (addon.temporarilyInstalled) {
    return "development";
  } else if (addon.foreignInstall) {
    return "sideload";
  } else if (addon.isSystem) {
    return "other";
  }
  return "normal";
}

extensions.registerSchemaAPI("management", "addon_parent", context => {
  let {extension} = context;
  return {
    management: {
      getSelf: function() {
        return new Promise((resolve, reject) => AddonManager.getAddonByID(extension.id, addon => {
          try {
            let m = extension.manifest;
            let extInfo = {
              id: extension.id,
              name: addon.name,
              shortName: m.short_name || "",
              description: addon.description || "",
              version: addon.version,
              mayDisable: !!(addon.permissions & AddonManager.PERM_CAN_DISABLE),
              enabled: addon.isActive,
              optionsUrl: addon.optionsURL || "",
              permissions: Array.from(extension.permissions).filter(perm => {
                return !extension.whiteListedHosts.pat.includes(perm);
              }),
              hostPermissions: extension.whiteListedHosts.pat,
              installType: installType(addon),
            };
            if (addon.homepageURL) {
              extInfo.homepageUrl = addon.homepageURL;
            }
            if (addon.updateURL) {
              extInfo.updateUrl = addon.updateURL;
            }
            if (m.icons) {
              extInfo.icons = Object.keys(m.icons).map(key => {
                return {size: Number(key), url: m.icons[key]};
              });
            }

            resolve(extInfo);
          } catch (err) {
            reject(err);
          }
        }));
      },

      uninstallSelf: function(options) {
        return new Promise((resolve, reject) => {
          if (options && options.showConfirmDialog) {
            let message = _("uninstall.confirmation.message", extension.name);
            if (options.dialogMessage) {
              message = `${options.dialogMessage}\n${message}`;
            }
            let title = _("uninstall.confirmation.title", extension.name);
            let buttonFlags = promptService.BUTTON_POS_0 * promptService.BUTTON_TITLE_IS_STRING +
                              promptService.BUTTON_POS_1 * promptService.BUTTON_TITLE_IS_STRING;
            let button0Title = _("uninstall.confirmation.button-0.label");
            let button1Title = _("uninstall.confirmation.button-1.label");
            let response = promptService.confirmEx(null, title, message, buttonFlags, button0Title, button1Title, null, null, {value: 0});
            if (response == 1) {
              return reject({message: "User cancelled uninstall of extension"});
            }
          }
          AddonManager.getAddonByID(extension.id, addon => {
            let canUninstall = Boolean(addon.permissions & AddonManager.PERM_CAN_UNINSTALL);
            if (!canUninstall) {
              return reject({message: "The add-on cannot be uninstalled"});
            }
            try {
              addon.uninstall();
            } catch (err) {
              return reject(err);
            }
          });
        });
      },
    },
  };
});