summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/ext-management.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 11:10:00 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 11:10:00 -0500
commitf164d9124708b50789dbb6959e1de96cc5697c48 (patch)
tree6dffd12e08c5383130df0252fb69cd6d6330794f /toolkit/components/webextensions/ext-management.js
parent30de4018913f0cdaea19d1dd12ecd8209e2ed08e (diff)
downloadUXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar
UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.gz
UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.lz
UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.xz
UXP-f164d9124708b50789dbb6959e1de96cc5697c48.zip
Rename Toolkit's webextensions component directory to better reflect what it is.
Diffstat (limited to 'toolkit/components/webextensions/ext-management.js')
-rw-r--r--toolkit/components/webextensions/ext-management.js109
1 files changed, 109 insertions, 0 deletions
diff --git a/toolkit/components/webextensions/ext-management.js b/toolkit/components/webextensions/ext-management.js
new file mode 100644
index 000000000..59a7959d7
--- /dev/null
+++ b/toolkit/components/webextensions/ext-management.js
@@ -0,0 +1,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);
+ }
+ });
+ });
+ },
+ },
+ };
+});