Bug 735574 - Extremely frequent leak of 1 BackstagePass, 2 Mutex, 9 ProxyListener coming from fx-team; r=dietrich
authorTim Taubert <tim.taubert@gmx.de>
Thu, 15 Mar 2012 23:28:22 +0100
changeset 92765 13872068ea17531028e0a96f59fd0309f9df2bf4
parent 92764 0bcbe43a02459819a7976966db253ef7cd7fffbd
child 92766 f24062a2aebb1264f882dffa464e9a29e6b52fed
push idunknown
push userunknown
push dateunknown
reviewersdietrich
bugs735574
milestone14.0a1
Bug 735574 - Extremely frequent leak of 1 BackstagePass, 2 Mutex, 9 ProxyListener coming from fx-team; r=dietrich
browser/base/content/test/newtab/browser_newtab_block.js
browser/base/content/test/newtab/head.js
browser/modules/NewTabUtils.jsm
--- a/browser/base/content/test/newtab/browser_newtab_block.js
+++ b/browser/base/content/test/newtab/browser_newtab_block.js
@@ -20,41 +20,41 @@ function runTests() {
 
   yield blockCell(cells[4]);
   checkGrid("0,1,2,3,6,7,8,9,");
 
   yield blockCell(cells[4]);
   checkGrid("0,1,2,3,7,8,9,,");
 
   // we removed a pinned site
-  reset();
+  yield restore();
   setLinks("0,1,2,3,4,5,6,7,8");
   setPinnedLinks(",1");
 
   yield addNewTabPageTab();
   checkGrid("0,1p,2,3,4,5,6,7,8");
 
   yield blockCell(cells[1]);
   checkGrid("0,2,3,4,5,6,7,8,");
 
   // we remove the last site on the grid (which is pinned) and expect the gap
   // to be re-filled and the new site to be unpinned
-  reset();
+  yield restore();
   setLinks("0,1,2,3,4,5,6,7,8,9");
   setPinnedLinks(",,,,,,,,8");
 
   yield addNewTabPageTab();
   checkGrid("0,1,2,3,4,5,6,7,8p");
 
   yield blockCell(cells[8]);
   checkGrid("0,1,2,3,4,5,6,7,9");
 
   // we remove the first site on the grid with the last one pinned. all cells
   // but the last one should shift to the left and a new site fades in
-  reset();
+  yield restore();
   setLinks("0,1,2,3,4,5,6,7,8,9");
   setPinnedLinks(",,,,,,,,8");
 
   yield addNewTabPageTab();
   checkGrid("0,1,2,3,4,5,6,7,8p");
 
   yield blockCell(cells[0]);
   checkGrid("1,2,3,4,5,6,7,9,8p");
--- a/browser/base/content/test/newtab/head.js
+++ b/browser/base/content/test/newtab/head.js
@@ -5,18 +5,16 @@ const PREF_NEWTAB_ENABLED = "browser.new
 
 Services.prefs.setBoolPref(PREF_NEWTAB_ENABLED, true);
 
 let tmp = {};
 Cu.import("resource:///modules/NewTabUtils.jsm", tmp);
 let NewTabUtils = tmp.NewTabUtils;
 
 registerCleanupFunction(function () {
-  reset();
-
   while (gBrowser.tabs.length > 1)
     gBrowser.removeTab(gBrowser.tabs[1]);
 
   Services.prefs.clearUserPref(PREF_NEWTAB_ENABLED);
 });
 
 /**
  * Global variables that are accessed by tests.
@@ -52,18 +50,39 @@ let TestRunner = {
 
   /**
    * Runs the next available test or finishes if there's no test left.
    */
   next: function () {
     try {
       TestRunner._iter.next();
     } catch (e if e instanceof StopIteration) {
-      finish();
+      TestRunner.finish();
     }
+  },
+
+  /**
+   * Finishes all tests and cleans up.
+   */
+  finish: function () {
+    function cleanupAndFinish() {
+      // Restore the old provider.
+      NewTabUtils.links._provider = originalProvider;
+
+      whenPagesUpdated(finish);
+      NewTabUtils.restore();
+    }
+
+    let callbacks = NewTabUtils.links._populateCallbacks;
+    let numCallbacks = callbacks.length;
+
+    if (numCallbacks)
+      callbacks.splice(0, numCallbacks, cleanupAndFinish);
+    else
+      cleanupAndFinish();
   }
 };
 
 /**
  * Allows to provide a list of links that is used to construct the grid.
  * @param aLinksPattern the pattern (see below)
  *
  * Example: setLinks("1,2,3")
@@ -101,23 +120,21 @@ function setPinnedLinks(aLinksPattern) {
     pinnedLinks[index] = link;
   });
 
   // Inject the list of pinned links to work with.
   NewTabUtils.pinnedLinks._links = pinnedLinks;
 }
 
 /**
- * Resets the lists of blocked and pinned links and clears the storage.
+ * Restore the grid state.
  */
-function reset() {
-  NewTabUtils.reset();
-
-  // Restore the old provider to prevent memory leaks.
-  NewTabUtils.links._provider = originalProvider;
+function restore() {
+  whenPagesUpdated();
+  NewTabUtils.restore();
 }
 
 /**
  * Creates a new tab containing 'about:newtab'.
  */
 function addNewTabPageTab() {
   let tab = gBrowser.selectedTab = gBrowser.addTab("about:newtab");
   let browser = tab.linkedBrowser;
@@ -264,21 +281,21 @@ function simulateDrop(aDropTarget, aDrag
 
   if (aDragSource)
     cw.gDrag.end(aDragSource.site);
 }
 
 /**
  * Resumes testing when all pages have been updated.
  */
-function whenPagesUpdated() {
+function whenPagesUpdated(aCallback) {
   let page = {
     update: function () {
       NewTabUtils.allPages.unregister(this);
-      executeSoon(TestRunner.next);
+      executeSoon(aCallback || TestRunner.next);
     }
   };
 
   NewTabUtils.allPages.register(page);
   registerCleanupFunction(function () {
     NewTabUtils.allPages.unregister(page);
   });
 }
--- a/browser/modules/NewTabUtils.jsm
+++ b/browser/modules/NewTabUtils.jsm
@@ -545,17 +545,17 @@ let Links = {
 
     return pinnedLinks;
   },
 
   /**
    * Resets the links cache.
    */
   resetCache: function Links_resetCache() {
-    this._links = [];
+    this._links = null;
   },
 
   /**
    * Implements the nsIObserver interface to get notified about browser history
    * sanitization.
    */
   observe: function Links_observe(aSubject, aTopic, aData) {
     // Make sure to update open about:newtab instances. If there are no opened
@@ -579,22 +579,26 @@ let Links = {
                                          Ci.nsISupportsWeakReference])
 };
 
 /**
  * Singleton that provides the public API of this JSM.
  */
 let NewTabUtils = {
   /**
-   * Resets the NewTabUtils module, its links and its storage.
+   * Restores all sites that have been removed from the grid.
    */
-  reset: function NewTabUtils_reset() {
+  restore: function NewTabUtils_restore() {
     Storage.clear();
     Links.resetCache();
     PinnedLinks.resetCache();
     BlockedLinks.resetCache();
+
+    Links.populateCache(function () {
+      AllPages.update();
+    }, true);
   },
 
   allPages: AllPages,
   links: Links,
   pinnedLinks: PinnedLinks,
   blockedLinks: BlockedLinks
 };