Bug 1419145 - Follow-up: Wait for add-on manager to return before building menu. r=me a=jorgk
authorJorg K <jorgk@jorgk.com>
Mon, 02 Apr 2018 19:56:53 +0200
changeset 30500 0d6ed7a480beb8c00dc7b573f967265d433847d4
parent 30499 3a4f54410a545bb3303d7e73aeecd89972a07bba
child 30501 35726bd64b0afd9751efe41e54b55d47d3df4dd7
push idunknown
push userunknown
push dateunknown
reviewersme, jorgk
bugs1419145
Bug 1419145 - Follow-up: Wait for add-on manager to return before building menu. r=me a=jorgk This returns to Aceman's initial patch
mail/base/content/mailWindowOverlay.js
--- a/mail/base/content/mailWindowOverlay.js
+++ b/mail/base/content/mailWindowOverlay.js
@@ -3729,40 +3729,51 @@ function initAddonPrefsMenu(aMenupopup) 
   // "no add-on prefs", which is the only disabled element. Above this element
   // there may be further items that we want to preserve.
   let noPrefsElem = aMenupopup.querySelector('[disabled="true"]');
   while (aMenupopup.lastChild != noPrefsElem) {
     aMenupopup.lastChild.remove();
   }
 
   // Enumerate all enabled addons with URL to XUL document with prefs.
+  let addonsFound = [];
+  let done = false;
   AddonManager.getAddonsByTypes(["extension"], (addons) => {
-    let addonsFound = [];
     for (let addon of addons) {
       if (!addon.userDisabled && !addon.appDisabled && !addon.softDisabled &&
           addon.optionsURL && (addon.optionsType === null || addon.optionsType == 3)) {
         addonsFound.push(addon);
       }
     }
-
-    // Populate the menu with addon names and icons
-    if (addonsFound.length > 0) {
-      addonsFound.sort((a,b) => a.name.localeCompare(b.name));
-      for (let addon of addonsFound) {
-        let newItem = document.createElement("menuitem");
-        newItem.setAttribute("label", addon.name);
-        newItem.setAttribute("value", addon.optionsURL);
-        if (addon.optionsType)
-          newItem.setAttribute("optionsType", addon.optionsType);
-        let iconURL = addon.iconURL || addon.icon64URL;
-        if (iconURL) {
-          newItem.setAttribute("class", "menuitem-iconic");
-          newItem.setAttribute("image", iconURL);
-        }
-        aMenupopup.appendChild(newItem);
+    done = true;
+  });
+
+  // Wait until the addon manager returns all results.
+  let thread = Components.classes["@mozilla.org/thread-manager;1"]
+                                 .getService().currentThread;
+  while (!done) {
+    thread.processNextEvent(true);
+  }
+
+  // Populate the menu with addon names and icons.
+  // Note: Having the following code in the getAddonsByTypes() async callback
+  // above works on Windows and Linux but doesn't work on Mac, see bug 1419145.
+  if (addonsFound.length > 0) {
+    addonsFound.sort((a,b) => a.name.localeCompare(b.name));
+    for (let addon of addonsFound) {
+      let newItem = document.createElement("menuitem");
+      newItem.setAttribute("label", addon.name);
+      newItem.setAttribute("value", addon.optionsURL);
+      if (addon.optionsType)
+        newItem.setAttribute("optionsType", addon.optionsType);
+      let iconURL = addon.iconURL || addon.icon64URL;
+      if (iconURL) {
+        newItem.setAttribute("class", "menuitem-iconic");
+        newItem.setAttribute("image", iconURL);
       }
-      noPrefsElem.setAttribute("collapsed", "true");
-    } else {
-      // Only show message that there are no addons with prefs.
-      noPrefsElem.setAttribute("collapsed", "false");
+      aMenupopup.appendChild(newItem);
     }
-  });
+    noPrefsElem.setAttribute("collapsed", "true");
+  } else {
+    // Only show message that there are no addons with prefs.
+    noPrefsElem.setAttribute("collapsed", "false");
+  }
 }