Bug 1279034 Add Addon.setEnabled() for a.m.o r=rhelmer,smaug a=sylvestre
authorAndrew Swan <aswan@mozilla.com>
Fri, 24 Jun 2016 12:54:18 -0700
changeset 339767 82eb330f804c59d22fc628c7295d72779bcb571b
parent 339766 f96fe0adc4e6c20c2b44f74b5cd54845fbedb1a1
child 339768 90fd7548ed20179c6e7a192720e260f045cd9b28
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrhelmer, smaug, sylvestre
bugs1279034
milestone49.0a2
Bug 1279034 Add Addon.setEnabled() for a.m.o r=rhelmer,smaug a=sylvestre MozReview-Commit-ID: 6mvHMWinkle
dom/webidl/AddonManager.webidl
toolkit/mozapps/extensions/AddonManager.jsm
toolkit/mozapps/extensions/amWebAPI.js
toolkit/mozapps/extensions/test/browser/browser.ini
toolkit/mozapps/extensions/test/browser/browser_webapi_enable.js
--- a/dom/webidl/AddonManager.webidl
+++ b/dom/webidl/AddonManager.webidl
@@ -21,16 +21,17 @@ interface Addon {
   // If the user has enabled this add-on, note that it still may not be running
   // depending on whether enabling requires a restart or if the add-on is
   // incompatible in some way.
   readonly attribute boolean isEnabled;
   // If the add-on is currently active in the browser.
   readonly attribute boolean isActive;
 
   Promise<boolean> uninstall();
+  Promise<void> setEnabled(boolean value);
 };
 
 [ChromeOnly, JSImplementation="dummy"]
 interface AddonInstall : EventTarget {
   // One of the STATE_* symbols from AddonManager.jsm
   readonly attribute DOMString state;
   // One of the ERROR_* symbols from AddonManager.jsm, or null
   readonly attribute DOMString? error;
--- a/toolkit/mozapps/extensions/AddonManager.jsm
+++ b/toolkit/mozapps/extensions/AddonManager.jsm
@@ -2874,16 +2874,28 @@ var AddonManagerInternal = {
           } catch (err) {
             Cu.reportError(err);
             resolve(false);
           }
         });
       });
     },
 
+    addonSetEnabled(target, id, value) {
+      return new Promise((resolve, reject) => {
+        AddonManager.getAddonByID(id, addon => {
+          if (!addon) {
+            reject({message: `No such addon ${id}`});
+          }
+          addon.userDisabled = !value;
+          resolve();
+        });
+      });
+    },
+
     addonInstallDoInstall(target, id) {
       let state = this.installs.get(id);
       if (!state) {
         return Promise.reject(`invalid id ${id}`);
       }
       return Promise.resolve(state.install.install());
     },
 
--- a/toolkit/mozapps/extensions/amWebAPI.js
+++ b/toolkit/mozapps/extensions/amWebAPI.js
@@ -146,16 +146,20 @@ function WebAPITask(generator) {
     });
   }
 }
 
 Addon.prototype = {
   uninstall: WebAPITask(function*() {
     return yield APIBroker.sendRequest("addonUninstall", this.id);
   }),
+
+  setEnabled: WebAPITask(function* (value) {
+    return yield APIBroker.sendRequest("addonSetEnabled", this.id, value);
+  }),
 };
 
 const INSTALL_EVENTS = [
   "onDownloadStarted",
   "onDownloadProgress",
   "onDownloadEnded",
   "onDownloadCancelled",
   "onDownloadFailed",
--- a/toolkit/mozapps/extensions/test/browser/browser.ini
+++ b/toolkit/mozapps/extensions/test/browser/browser.ini
@@ -63,12 +63,13 @@ skip-if = require_signing
 [browser_newaddon.js]
 [browser_updatessl.js]
 [browser_task_next_test.js]
 [browser_discovery_install.js]
 [browser_update.js]
 [browser_webapi.js]
 [browser_webapi_access.js]
 [browser_webapi_addon_listener.js]
+[browser_webapi_enable.js]
 [browser_webapi_install.js]
 [browser_webapi_uninstall.js]
 
 [include:browser-common.ini]
new file mode 100644
--- /dev/null
+++ b/toolkit/mozapps/extensions/test/browser/browser_webapi_enable.js
@@ -0,0 +1,62 @@
+const TESTPAGE = `${SECURE_TESTROOT}webapi_addon_listener.html`;
+
+Services.prefs.setBoolPref("extensions.webapi.testing", true);
+registerCleanupFunction(() => {
+  Services.prefs.clearUserPref("extensions.webapi.testing");
+});
+
+function* getListenerEvents(browser) {
+  let result = yield ContentTask.spawn(browser, null, function*() {
+    return content.document.getElementById("result").textContent;
+  });
+
+  return result.split('\n').map(JSON.parse);
+}
+
+const ID = "test@tests.mozilla.org";
+
+let provider = new MockProvider(false);
+provider.createAddons([
+  {
+    id: ID,
+    name: "Test add-on",
+    operationsRequiringRestart: AddonManager.OP_NEED_RESTART_NONE,
+  },
+]);
+
+// Test disable and enable from content
+add_task(function* () {
+  yield BrowserTestUtils.withNewTab(TESTPAGE, function*(browser) {
+    let addon = yield promiseAddonByID(ID);
+    isnot(addon, null, "Test addon exists");
+    is(addon.userDisabled, false, "addon is enabled");
+
+    // Disable the addon from content.
+    yield ContentTask.spawn(browser, null, function* () {
+      return content.navigator.mozAddonManager
+        .getAddonByID("test@tests.mozilla.org")
+        .then(addon => { addon.setEnabled(false); });
+    });
+
+    let events = yield getListenerEvents(browser);
+    let expected = [
+      {id: ID, needsRestart: false, event: "onDisabling"},
+      {id: ID, needsRestart: false, event: "onDisabled"},
+    ];
+    Assert.deepEqual(events, expected, "Got expected disable events");
+
+    // Enable the addon from content.
+    yield ContentTask.spawn(browser, null, function* () {
+      return content.navigator.mozAddonManager
+        .getAddonByID("test@tests.mozilla.org")
+        .then(addon => { addon.setEnabled(true); });
+    });
+
+    events = yield getListenerEvents(browser);
+    expected = expected.concat([
+      {id: ID, needsRestart: false, event: "onEnabling"},
+      {id: ID, needsRestart: false, event: "onEnabled"},
+    ]);
+    Assert.deepEqual(events, expected, "Got expected enable events");
+  });
+});