Bug 561723 - Port Bug 551505 [Fix context of RestoreDocument_proxy] to SeaMonkey. r=Neil, sr=Neil
authorMisak Khachatryan <misak.bugzilla@gmail.com>
Mon, 26 Apr 2010 16:50:37 +0500
changeset 5541 c0df81ab60c414712e94863fe3034291c0ef2a04
parent 5540 b2b86be4f4f2b583b51cb875f016584396ff3580
child 5542 6ba6d8da7a2b3e310faa2dd7b32feb4e90fe180c
push idunknown
push userunknown
push dateunknown
reviewersNeil, Neil
bugs561723, 551505
Bug 561723 - Port Bug 551505 [Fix context of RestoreDocument_proxy] to SeaMonkey. r=Neil, sr=Neil
suite/common/src/nsSessionStore.js
--- a/suite/common/src/nsSessionStore.js
+++ b/suite/common/src/nsSessionStore.js
@@ -405,16 +405,22 @@ SessionStoreService.prototype = {
 
   /**
    * Implement nsIDOMEventListener for handling various window and tab events
    */
   handleEvent: function sss_handleEvent(aEvent) {
     var win = aEvent.currentTarget.ownerDocument.defaultView;
     switch (aEvent.type) {
       case "load":
+        // If __SS_restore_data is set, then we need to restore the document
+        // (form data, scrolling, etc.). This will only happen when a tab is
+        // first restored.
+        if (aEvent.currentTarget.__SS_restore_data)
+          this.restoreDocument(win, aEvent.currentTarget, aEvent);
+        // We still need to call onTabLoad, so fall through to "pageshow" case.
       case "pageshow":
         this.onTabLoad(win, aEvent.currentTarget, aEvent);
         break;
       case "change":
       case "input":
       case "DOMAutoComplete":
         this.onTabInput(win, aEvent.currentTarget);
         break;
@@ -1972,18 +1978,16 @@ SessionStoreService.prototype = {
 
     if (tabData.entries.length > 0) {
       // restore those aspects of the currently active documents
       // which are not preserved in the plain history entries
       // (mainly scroll state and text data)
       browser.__SS_restore_data = tabData.entries[activeIndex] || {};
       browser.__SS_restore_pageStyle = tabData.pageStyle || "";
       browser.__SS_restore_tab = tab;
-      browser.__SS_restore = this.restoreDocument_proxy;
-      browser.addEventListener("load", browser.__SS_restore, true);
     }
 
     aWindow.setTimeout(function() {
       _this.restoreHistory(aWindow, aTabs, aTabData, aIdMap, aDocIdentMap);
     }, 0);
   },
 
   /**
@@ -2112,17 +2116,17 @@ SessionStoreService.prototype = {
         catch (ex) { Components.utils.reportError(ex); } // throws e.g. for URIs that can't have sessionStorage
       }
     }
   },
 
   /**
    * Restore properties to a loaded document
    */
-  restoreDocument_proxy: function sss_restoreDocument_proxy(aEvent) {
+  restoreDocument: function sss_restoreDocument(aWindow, aBrowser, aEvent) {
     // wait for the top frame to be loaded completely
     if (!aEvent || !aEvent.originalTarget || !aEvent.originalTarget.defaultView || aEvent.originalTarget.defaultView != aEvent.originalTarget.defaultView.top) {
       return;
     }
 
     // always call this before injecting content into a document!
     function hasExpectedURL(aDocument, aURL)
       !aURL || aURL.replace(/#.*/, "") == aDocument.location.href.replace(/#.*/, "");
@@ -2160,23 +2164,22 @@ SessionStoreService.prototype = {
           Array.forEach(node.options, function(aOpt, aIx) {
             aOpt.selected = value.indexOf(aIx) > -1;
           });
         }
         // NB: dispatching "change" events might have unintended side-effects
       }
     }
 
-    let selectedPageStyle = this.__SS_restore_pageStyle;
-    let window = this.ownerDocument.defaultView;
+    let selectedPageStyle = aBrowser.__SS_restore_pageStyle;
     function restoreTextDataAndScrolling(aContent, aData, aPrefix) {
       if (aData.formdata)
         restoreFormData(aContent.document, aData.formdata, aData.url);
       if (aData.innerHTML) {
-        window.setTimeout(function() {
+        aWindow.setTimeout(function() {
           if (aContent.document.designMode == "on" &&
               hasExpectedURL(aContent.document, aData.url)) {
             aContent.document.body.innerHTML = aData.innerHTML;
           }
         }, 0);
       }
       if (aData.scroll && /(\d+),(\d+)/.test(aData.scroll)) {
         aContent.scrollTo(RegExp.$1, RegExp.$2);
@@ -2189,37 +2192,35 @@ SessionStoreService.prototype = {
           hasExpectedURL(aContent.document, aData.url)) {
           restoreTextDataAndScrolling(aContent.frames[i], aData.children[i], aPrefix + i + "|");
         }
       }
     }
 
     // don't restore text data and scrolling state if the user has navigated
     // away before the loading completed (except for in-page navigation)
-    if (hasExpectedURL(aEvent.originalTarget, this.__SS_restore_data.url)) {
+    if (hasExpectedURL(aEvent.originalTarget, aBrowser.__SS_restore_data.url)) {
       var content = aEvent.originalTarget.defaultView;
-      if (this.currentURI.spec == "about:config") {
+      if (aBrowser.currentURI.spec == "about:config") {
         // unwrap the document for about:config because otherwise the properties
         // of the XBL bindings - as the textbox - aren't accessible (see bug 350718)
         content = content.wrappedJSObject;
       }
-      restoreTextDataAndScrolling(content, this.__SS_restore_data, "");
-      this.markupDocumentViewer.authorStyleDisabled = selectedPageStyle == "_nostyle";
+      restoreTextDataAndScrolling(content, aBrowser.__SS_restore_data, "");
+      aBrowser.markupDocumentViewer.authorStyleDisabled = selectedPageStyle == "_nostyle";
 
       // notify the tabbrowser that this document has been completely restored
-      var event = this.ownerDocument.createEvent("Events");
+      var event = aBrowser.ownerDocument.createEvent("Events");
       event.initEvent("SSTabRestored", true, false);
-      this.__SS_restore_tab.dispatchEvent(event);
+      aBrowser.__SS_restore_tab.dispatchEvent(event);
     }
 
-    this.removeEventListener("load", this.__SS_restore, true);
-    delete this.__SS_restore_data;
-    delete this.__SS_restore_pageStyle;
-    delete this.__SS_restore_tab;
-    delete this.__SS_restore;
+    delete aBrowser.__SS_restore_data;
+    delete aBrowser.__SS_restore_pageStyle;
+    delete aBrowser.__SS_restore_tab;
   },
 
   /**
    * Restore visibility and dimension features to a window
    * @param aWindow
    *        Window reference
    * @param aWinData
    *        Object containing session data for the window