Bug 960903 - Listen for subframe loads and clear observers r=yoric
authorTim Taubert <ttaubert@mozilla.com>
Wed, 22 Jan 2014 13:10:58 +0100
changeset 165119 4c85a2007510f24b938b124bb9fd35bbf14b8f4d
parent 165118 c99d792be115612fa33f955fb1ce6634fdf9c1b4
child 165120 c7c63c6c2433a437312066c0f1419a87c9688f5a
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersyoric
bugs960903
milestone29.0a1
Bug 960903 - Listen for subframe loads and clear observers r=yoric
browser/components/sessionstore/content/content-sessionStore.js
--- a/browser/components/sessionstore/content/content-sessionStore.js
+++ b/browser/components/sessionstore/content/content-sessionStore.js
@@ -212,49 +212,54 @@ let SyncHandler = {
  * session history.
  *
  * Example:
  *   {entries: [{url: "about:mozilla", ...}, ...], index: 1}
  */
 let SessionHistoryListener = {
   init: function () {
     gFrameTree.addObserver(this);
+    addEventListener("load", this, true);
     addEventListener("hashchange", this, true);
-    Services.obs.addObserver(this, "browser:purge-session-history", true);
+    Services.obs.addObserver(this, "browser:purge-session-history", false);
+  },
+
+  uninit: function () {
+    Services.obs.removeObserver(this, "browser:purge-session-history");
   },
 
   observe: function () {
     // We need to use setTimeout() here because we listen for
     // "browser:purge-session-history". When that is fired all observers are
     // expected to purge their data. We can't expect to be called *after* the
     // observer in browser.xml that clears session history so we need to wait
     // a tick before actually collecting data.
     setTimeout(() => this.collect(), 0);
   },
 
-  handleEvent: function () {
-    this.collect();
+  handleEvent: function (event) {
+    // We are only interested in "load" events from subframes.
+    if (event.type == "hashchange" || event.target != content.document) {
+      this.collect();
+    }
   },
 
   collect: function () {
     if (docShell) {
       MessageQueue.push("history", () => SessionHistory.collect(docShell));
     }
   },
 
   onFrameTreeCollected: function () {
     this.collect();
   },
 
   onFrameTreeReset: function () {
     this.collect();
-  },
-
-  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
-                                         Ci.nsISupportsWeakReference])
+  }
 };
 
 /**
  * Listens for scroll position changes. Whenever the user scrolls the top-most
  * frame we update the scroll position and will restore it when requested.
  *
  * Causes a SessionStore:update message to be sent that contains the current
  * scroll positions as a tree of strings. If no frame of the whole frame tree
@@ -668,13 +673,14 @@ SessionStorageListener.init();
 ScrollPositionListener.init();
 DocShellCapabilitiesListener.init();
 PrivacyListener.init();
 
 addEventListener("unload", () => {
   // Remove all registered nsIObservers.
   PageStyleListener.uninit();
   SessionStorageListener.uninit();
+  SessionHistoryListener.uninit();
 
   // We don't need to take care of any gFrameTree observers as the gFrameTree
   // will die with the content script. The same goes for the privacy transition
   // observer that will die with the docShell when the tab is closed.
 });