Bug 751922 Asynchronously add favicons to back/forward and history menus.
authorPhilip Chee <philip.chee@gmail.com>
Wed, 16 May 2012 23:54:51 +0800
changeset 10198 62fef30b145376aa20889689ebcd4d687528987a
parent 10197 6dac932587b740f30b1e31012eb69e17151ef8d6
child 10199 71acb69c63abde0ac803f7ac97d928ef6cb73611
push id7728
push userphilip.chee@gmail.com
push dateWed, 16 May 2012 15:58:51 +0000
treeherdercomm-central@62fef30b1453 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs751922
Bug 751922 Asynchronously add favicons to back/forward and history menus.
suite/browser/sessionHistoryUI.js
--- a/suite/browser/sessionHistoryUI.js
+++ b/suite/browser/sessionHistoryUI.js
@@ -58,68 +58,71 @@ function toggleTabsFromOtherComputers()
     // is modified), so make sure we avoid undefined errors.
     let enabled = Weave.Service.isLoggedIn && Weave.Engines.get("tabs") &&
                   Weave.Engines.get("tabs").enabled;
     menuitem.setAttribute("disabled", !enabled);
     menuitem.hidden = false;
   }
 
 function FillHistoryMenu(aParent, aMenu)
-  {
-    // Remove old entries if any
-    deleteHistoryItems(aParent);
+{
+  // Remove old entries if any
+  deleteHistoryItems(aParent);
 
-    var sessionHistory = getWebNavigation().sessionHistory;
+  var sessionHistory = getWebNavigation().sessionHistory;
 
-    var count = sessionHistory.count;
-    var index = sessionHistory.index;
-    var end;
-    var j;
-    var entry;
+  var count = sessionHistory.count;
+  var index = sessionHistory.index;
+  var end;
 
-    switch (aMenu)
+  switch (aMenu)
+  {
+    case "back":
+      end = index > MAX_HISTORY_MENU_ITEMS ? index - MAX_HISTORY_MENU_ITEMS
+                                           : 0;
+      if (index <= end)
+        return false;
+      for (let j = index - 1; j >= end; j--)
       {
-        case "back":
-          end = (index > MAX_HISTORY_MENU_ITEMS) ? index - MAX_HISTORY_MENU_ITEMS : 0;
-          if ((index - 1) < end) return false;
-          for (j = index - 1; j >= end; j--)
-            {
-              entry = sessionHistory.getEntryAtIndex(j, false);
-              if (entry)
-                createMenuItem(aParent, j, entry.title);
-            }
-          break;
-        case "forward":
-          end  = ((count-index) > MAX_HISTORY_MENU_ITEMS) ? index + MAX_HISTORY_MENU_ITEMS : count - 1;
-          if ((index + 1) > end) return false;
-          for (j = index + 1; j <= end; j++)
-            {
-              entry = sessionHistory.getEntryAtIndex(j, false);
-              if (entry)
-                createMenuItem(aParent, j, entry.title);
-            }
-          break;
-        case "go":
-          var startHistory = document.getElementById("startHistorySeparator");
-          var endHistory = document.getElementById("endHistorySeparator");
-          var syncMenuItem = document.getElementById("sync-tabs-menuitem");
-          startHistory.hidden = (count == 0);
-          end = count > MAX_HISTORY_MENU_ITEMS ? count - MAX_HISTORY_MENU_ITEMS : 0;
-          for (j = count - 1; j >= end; j--)
-            {
-              entry = sessionHistory.getEntryAtIndex(j, false);
-              if (entry)
-                createRadioMenuItem(aParent, endHistory, j, entry.title, j == index);
-            }
-          toggleTabsFromOtherComputers();
-          endHistory.hidden = (endHistory == aParent.lastChild || syncMenuItem.hidden);
-          break;
+        let entry = sessionHistory.getEntryAtIndex(j, false);
+        if (entry)
+          createHistoryMenuItem(aParent, j, entry);
+      }
+      break;
+    case "forward":
+      end = count - index > MAX_HISTORY_MENU_ITEMS ? index + MAX_HISTORY_MENU_ITEMS
+                                                   : count - 1;
+      if (index >= end)
+        return false;
+      for (let j = index + 1; j <= end; j++)
+      {
+        let entry = sessionHistory.getEntryAtIndex(j, false);
+        if (entry)
+          createHistoryMenuItem(aParent, j, entry);
       }
-    return true;
+      break;
+    case "go":
+      var startHistory = document.getElementById("startHistorySeparator");
+      var endHistory = document.getElementById("endHistorySeparator");
+      var syncMenuItem = document.getElementById("sync-tabs-menuitem");
+      startHistory.hidden = (count == 0);
+      end = count > MAX_HISTORY_MENU_ITEMS ? count - MAX_HISTORY_MENU_ITEMS
+                                           : 0;
+      for (let j = count - 1; j >= end; j--)
+      {
+        let entry = sessionHistory.getEntryAtIndex(j, false);
+        if (entry)
+          createHistoryMenuItem(aParent, j, entry, endHistory, j == index);
+      }
+      toggleTabsFromOtherComputers();
+      endHistory.hidden = (endHistory == aParent.lastChild || syncMenuItem.hidden);
+      break;
   }
+  return true;
+}
 
 function executeUrlBarHistoryCommand( aTarget )
   {
     var index = aTarget.getAttribute("index");
     var label = aTarget.getAttribute("label");
     if (index != "nothing_available" && label)
       {
         gURLBar.value = label;
@@ -152,43 +155,50 @@ function createUBHistoryMenu( aParent )
       }
     }
     //Create the "Nothing Available" Menu item and disable it.
     var na = aParent.appendChild(document.createElement("menuitem"));
     na.setAttribute("label", gNavigatorBundle.getString("nothingAvailable"));
     na.setAttribute("disabled", "true");
   }
 
-function createMenuItem(aParent, aIndex, aLabel)
+function createHistoryMenuItem(aParent, aIndex, aEntry, aAnchor, aChecked)
+{
+  var menuitem = document.createElement("menuitem");
+  menuitem.setAttribute("label", aEntry.title);
+  menuitem.setAttribute("index", aIndex);
+  if (aChecked)
   {
-    var menuitem = document.createElement( "menuitem" );
-    menuitem.setAttribute( "label", aLabel );
-    menuitem.setAttribute( "index", aIndex );
-    aParent.appendChild(menuitem);
+    menuitem.setAttribute("type", "radio");
+    menuitem.setAttribute("checked", "true");
   }
 
-function createRadioMenuItem(aParent, aAnchor, aIndex, aLabel, aChecked)
+  if (!aChecked || /Mac/.test(navigator.platform))
   {
-    var menuitem = document.createElement("menuitem");
-    menuitem.setAttribute("type", "radio");
-    menuitem.setAttribute("label", aLabel);
-    menuitem.setAttribute("index", aIndex);
-    if (aChecked)
-      menuitem.setAttribute("checked", "true");
-    aParent.insertBefore(menuitem, aAnchor);
+    menuitem.className = "menuitem-iconic bookmark-item menuitem-with-favicon";
+    PlacesUtils.favicons.getFaviconURLForPage(aEntry.URI,
+      function faviconURLCallback(aURI) {
+        if (aURI) {
+          menuitem.setAttribute("image",
+                                PlacesUtils.favicons
+                                           .getFaviconLinkForIcon(aURI).spec);
+        }
+      }
+    );
   }
+  aParent.appendChild(menuitem);
+}
 
 function deleteHistoryItems(aParent)
+{
+  var children = aParent.childNodes;
+  for (let i = children.length - 1; i >= 0; --i)
   {
-    var children = aParent.childNodes;
-    for (var i = children.length - 1; i >= 0; --i )
-      {
-        var index = children[i].getAttribute( "index" );
-        if (index)
-          aParent.removeChild( children[i] );
-      }
+    if (children[i].hasAttribute("index"))
+      aParent.removeChild(children[i]);
   }
+}
 
 function updateGoMenu(event)
   {
     FillHistoryMenu(event.target, "go");
     updateRecentMenuItems();
   }