Bug 692712 - Pre-filter recommended add-ons JSON instead of filtering at startup [r=wjohnston a=asa]
authorMark Finkle <mfinkle@mozilla.com>
Fri, 07 Oct 2011 22:13:45 -0400
changeset 79088 f41ea62c6d4c416e3d1718e5aaafb21e935717b5
parent 79087 b707c898e8674da28cf70a6040750d02ce9e6e42
child 79089 fafffd7c801e721a67f9a017d1fe90a02a4138fb
push id78
push userclegnitto@mozilla.com
push dateFri, 16 Dec 2011 17:32:24 +0000
treeherdermozilla-release@79d24e644fdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswjohnston, asa
bugs692712
milestone9.0a2
Bug 692712 - Pre-filter recommended add-ons JSON instead of filtering at startup [r=wjohnston a=asa]
mobile/chrome/content/aboutHome.xhtml
mobile/chrome/content/extensions.js
mobile/components/AddonUpdateService.js
--- a/mobile/chrome/content/aboutHome.xhtml
+++ b/mobile/chrome/content/aboutHome.xhtml
@@ -123,17 +123,16 @@
       </div>
     </div>
   </div>
 
   <script type="application/javascript;version=1.8"><![CDATA[
     let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils;
     let gChromeWin = null;
 
-    Cu.import("resource://gre/modules/AddonManager.jsm");
     Cu.import("resource://gre/modules/NetUtil.jsm");
 
     function openLink(aElement) {
       try {
         let formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter);
         let url = formatter.formatURLPref(aElement.getAttribute("pref"));
         openTabs([url]);
       } catch (ex) {}
@@ -317,17 +316,17 @@
     var RecommendedAddons = {
       _getFile: function() {
         let dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
         let file = dirService.get("ProfD", Ci.nsILocalFile);
         file.append("recommended-addons.json");
         return file;
       },
 
-      _loadAddons: function(aAddons, aAddonCount, aTotalResults) {
+      _loadAddons: function(aAddons) {
         let list = document.getElementById("newAddons");
         let loading = document.getElementById("loadingAddons");
 
         if (aAddons.length == 0 && loading) {
           loading.innerHTML = "<div class='no-items'>" + document.getElementById("text-noaddons").textContent + "</div>";
           return;
         }
 
@@ -372,33 +371,21 @@
 
       loadFromCacheOrScheduleUpdate: function(aDelay) {
         let self = this;
         let file = this._getFile();
         if (file.exists()) {
           _readFile(file, function(aContent) {
             let json = JSON.parse(aContent);
             if (!json || json.addons.length == 0) {
-              self._loadAddons([], 0, 0);
+              self._loadAddons([]);
               return;
             }
   
-            // Ignore addons already installed
-            let addonsCache = json.addons;
-            AddonManager.getAllAddons(function(aAddons) {
-              let addons = addonsCache.filter(function(addon) {
-                for (let i =0; i < aAddons.length; i++)
-                  if (addon.id == aAddons[i].id)
-                    return false;
-  
-                return true;
-              });
-  
-              self._loadAddons(addons, addons.length, json.totalResults);
-            });
+            self._loadAddons(json.addons);
           });
         } else {
           setTimeout(function() {
             let aus = Cc["@mozilla.org/browser/addon-update-service;1"].getService(Ci.nsITimerCallback);
             aus.notify(null);
           }, aDelay);
         }
       }
--- a/mobile/chrome/content/extensions.js
+++ b/mobile/chrome/content/extensions.js
@@ -908,28 +908,29 @@ var AddonSearchResults = {
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 // XPInstall download helper
 function AddonInstallListener() {
 }
 
 AddonInstallListener.prototype = {
-
   onInstallEnded: function(aInstall, aAddon) {
     let needsRestart = false;
     let mode = "";
     if (aInstall.existingAddon && (aInstall.existingAddon.pendingOperations & AddonManager.PENDING_UPGRADE)) {
       needsRestart = true;
       mode = "update";
     } else if (aAddon.pendingOperations & AddonManager.PENDING_INSTALL) {
       needsRestart = true;
       mode = "normal";
     }
 
+    this._clearRecommendedCache();
+
     // if we already have a mode, then we need to show a restart notification
     // otherwise, we are likely a bootstrapped addon
     if (needsRestart)
       ExtensionsView.showRestart(mode);
 
     if (aAddon.type != "locale")
       this._showInstallCompleteAlert(true, needsRestart);
 
@@ -1060,9 +1061,17 @@ AddonInstallListener.prototype = {
     if (aSucceeded) {
       stringName = "alertAddonsInstalled";
       if (!aNeedsRestart)
         stringName += "NoRestart";
     }
 
     ExtensionsView.showAlert(strings.GetStringFromName(stringName), !aNeedsRestart);
   },
+
+  _clearRecommendedCache: function xpidm_clearRecommendedCache() {
+    let dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
+    let file = dirService.get("ProfD", Ci.nsILocalFile);
+    file.append("recommended-addons.json");
+    if (file.exists())
+      file.remove(false);
+  }
 };
--- a/mobile/components/AddonUpdateService.js
+++ b/mobile/components/AddonUpdateService.js
@@ -181,29 +181,47 @@ var RecommendedSearchResults = {
     let istream = converter.convertToInputStream(aData);
     NetUtil.asyncCopy(istream, ostream, function(rc) {
       if (Components.isSuccessCode(rc))
         Services.obs.notifyObservers(null, "recommended-addons-cache-updated", "");
     });
   },
   
   searchSucceeded: function(aAddons, aAddonCount, aTotalResults) {
-    let json = {
-      addons: aAddons,
-      addonCount: aAddonCount,
-      totalResults: aTotalResults
-    };
+    let self = this;
+
+    // Filter addons already installed
+    AddonManager.getAllAddons(function(aAllAddons) {
+      let addons = aAddons.filter(function(addon) {
+        for (let i = 0; i < aAllAddons.length; i++)
+          if (addon.id == aAllAddons[i].id)
+            return false;
+
+        return true;
+      });
+
+      let json = {
+        addons: []
+      };
 
-    // Avoid any NSS costs. Convert https to http.
-    json.addons.forEach(function(aAddon){
-      aAddon.iconURL = aAddon.iconURL.replace(/^https/, "http");
+      // Avoid any NSS costs. Convert https to http.
+      addons.forEach(function(aAddon){
+        json.addons.push({
+          id: aAddon.id,
+          name: aAddon.name,
+          version: aAddon.version,
+          description: aAddon.description,
+          averageRating: aAddon.averageRating,
+          iconURL: aAddon.iconURL.replace(/^https/, "http")
+        })
+      });
+
+      let file = self._getFile();
+      self._writeFile(file, JSON.stringify(json));
     });
-
-    let file = this._getFile();
-    this._writeFile(file, JSON.stringify(json));
   },
   
   searchFailed: function searchFailed() { },
   
   search: function() {
     const kAddonsMaxDisplay = 2;
 
     if (AddonRepository.isSearching)