Bug 735874 - (1/2) Refactor add-on manager enable/disable functions [r=mfinkle]
authorMatt Brubeck <mbrubeck@mozilla.com>
Wed, 14 Mar 2012 15:31:21 -0700
changeset 89411 4b9a25a297c99a13a5711a011bfaa6078be1f943
parent 89410 0b24813652b150f6201e950d857c0a603ccfaa10
child 89412 60c732729b692b24b3ac339c95296a8d64cd0ca1
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmfinkle
bugs735874
milestone14.0a1
Bug 735874 - (1/2) Refactor add-on manager enable/disable functions [r=mfinkle]
mobile/android/chrome/content/aboutAddons.js
--- a/mobile/android/chrome/content/aboutAddons.js
+++ b/mobile/android/chrome/content/aboutAddons.js
@@ -289,103 +289,81 @@ var Addons = {
     }
 
     let list = document.querySelector("#addons-list");
     list.style.display = "none";
     let details = document.querySelector("#addons-details");
     details.style.display = "block";
   },
 
-  enable: function enable() {
+  setEnabled: function setEnabled(aValue, aAddon) {
     let detailItem = document.querySelector("#addons-details > .addon-item");
-    if (!detailItem.addon)
+    let addon = aAddon || detailItem.addon;
+    if (!addon)
       return;
 
     let opType;
-    let isDisabled;
-    if (detailItem.addon.type == "search") {
-      isDisabled = false;
-      detailItem.addon.engine.hidden = false;
-      opType = "needs-enable";
-    } else if (detailItem.addon.type == "theme") {
-      // We can have only one theme enabled, so disable the current one if any
-      let theme = null;
-      let list = document.getElementById("addons-list");
-      let item = list.firstElementChild;
-      while (item) {
-        if (item.addon && (item.addon.type == "theme") && (item.addon.isActive)) {
-          theme = item;
-          break;
+    if (addon.type == "search") {
+      addon.engine.hidden = !aValue;
+      opType = aValue ? "needs-enable" : "needs-disable";
+    } else if (addon.type == "theme") {
+      if (aValue) {
+        // We can have only one theme enabled, so disable the current one if any
+        let list = document.getElementById("addons-list");
+        let item = list.firstElementChild;
+        while (item) {
+          if (item.addon && (item.addon.type == "theme") && (item.addon.isActive)) {
+            this.setEnabled(false, item);
+            break;
+          }
+          item = item.nextSibling;
         }
-        item = item.nextSibling;
       }
-      if (theme)
-        this.disable(theme);
-
-      detailItem.addon.userDisabled = false;
-      isDisabled = false;
+      addon.userDisabled = !aValue;
+    } else if (addon.type == "locale") {
+      addon.userDisabled = !aValue;
     } else {
-      detailItem.addon.userDisabled = false;
-      isDisabled = false;
-      opType = this._getOpTypeForOperations(detailItem.addon.pendingOperations);
+      addon.userDisabled = !aValue;
+      opType = this._getOpTypeForOperations(addon.pendingOperations);
 
-      if (detailItem.addon.pendingOperations & AddonManager.PENDING_ENABLE) {
+      if ((addon.pendingOperations & AddonManager.PENDING_ENABLE) ||
+          (addon.pendingOperations & AddonManager.PENDING_DISABLE)) {
         this.showRestart();
-      } else {
-        if (detailItem.getAttribute("opType") == "needs-disable")
-          this.hideRestart();
+      } else if (addon == detailItem.addon &&
+            detailItem.getAttribute("opType") == "needs-disable" ||
+            detailItem.getAttribute("opType") == "needs-enable") {
+        this.hideRestart();
       }
     }
 
-    detailItem.setAttribute("opType", opType);
-    detailItem.setAttribute("isDisabled", isDisabled);
+    if (addon == detailItem.addon) {
+      detailItem.setAttribute("isDisabled", !aValue);
+      if (opType)
+        detailItem.setAttribute("opType", opType);
+      else
+        detailItem.removeAttribute("opType");
+    }
 
     // Sync to the list item
-    let listItem = this._getElementForAddon(detailItem.addon.id);
-    listItem.setAttribute("isDisabled", detailItem.getAttribute("isDisabled"));
-    listItem.setAttribute("opType", detailItem.getAttribute("opType"));
+    let listItem = this._getElementForAddon(addon.id);
+    if (listItem) {
+      listItem.setAttribute("isDisabled", !aValue);
+      if (opType)
+        listItem.setAttribute("opType", opType);
+      else
+        listItem.removeAttribute("opType");
+    }
+  },
+
+  enable: function enable() {
+    this.setEnabled(true);
   },
 
   disable: function disable() {
-    let detailItem = document.querySelector("#addons-details > .addon-item");
-    if (!detailItem.addon)
-      return;
-
-    let opType;
-    let isDisabled;
-    if (detailItem.addon.type == "search") {
-      isDisabled = true;
-      detailItem.addon.engine.hidden = true;
-      opType = "needs-disable";
-    } else if (detailItem.addon.type == "theme") {
-      detailItem.addon.userDisabled = true;
-      isDisabled = true;
-    } else if (detailItem.addon.type == "locale") {
-      detailItem.addon.userDisabled = true;
-      isDisabled = true;
-    } else {
-      detailItem.addon.userDisabled = true;
-      opType = this._getOpTypeForOperations(detailItem.addon.pendingOperations);
-      isDisabled = !detailItem.addon.isActive;
-
-      if (detailItem.addon.pendingOperations & AddonManager.PENDING_DISABLE) {
-        this.showRestart();
-      } else {
-        if (detailItem.getAttribute("opType") == "needs-enable")
-          this.hideRestart();
-      }
-    }
-
-    detailItem.setAttribute("opType", opType);
-    detailItem.setAttribute("isDisabled", isDisabled);
-
-    // Sync to the list item
-    let listItem = this._getElementForAddon(detailItem.addon.id);
-    listItem.setAttribute("isDisabled", detailItem.getAttribute("isDisabled"));
-    listItem.setAttribute("opType", detailItem.getAttribute("opType"));
+    this.setEnabled(false);
   },
 
   uninstall: function uninstall() {
     let list = document.getElementById("addons-list");
     let detailItem = document.querySelector("#addons-details > .addon-item");
     if (!detailItem.addon)
       return;