Bug 542032 - Don't look up prefs on every tab & window close [r=zpao]
authorMarco Castelluccio <mar.castelluccio@studenti.unina.it>
Fri, 02 Dec 2011 10:19:45 -0800
changeset 83104 a5879751101ef6e5e0429df4e7bb2cc1c38e0cac
parent 83103 392c2c86c5bc19b7549dd17029ce4c2b48594f35
child 83105 87d0900637fec7fb6d12378a688e9330266c9e46
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerszpao
bugs542032
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 542032 - Don't look up prefs on every tab & window close [r=zpao]
browser/components/sessionstore/src/nsSessionStore.js
--- a/browser/components/sessionstore/src/nsSessionStore.js
+++ b/browser/components/sessionstore/src/nsSessionStore.js
@@ -173,16 +173,26 @@ function SessionStoreService() {
   });
 
   // when crash recovery is disabled, session data is not written to disk
   XPCOMUtils.defineLazyGetter(this, "_resume_from_crash", function () {
     // get crash recovery state from prefs and allow for proper reaction to state changes
     this._prefBranch.addObserver("sessionstore.resume_from_crash", this, true);
     return this._prefBranch.getBoolPref("sessionstore.resume_from_crash");
   });
+
+  XPCOMUtils.defineLazyGetter(this, "_max_tabs_undo", function () {
+    this._prefBranch.addObserver("sessionstore.max_tabs_undo", this, true);
+    return this._prefBranch.getIntPref("sessionstore.max_tabs_undo");
+  });
+
+  XPCOMUtils.defineLazyGetter(this, "_max_windows_undo", function () {
+    this._prefBranch.addObserver("sessionstore.max_windows_undo", this, true);
+    return this._prefBranch.getIntPref("sessionstore.max_windows_undo");
+  });
 }
 
 SessionStoreService.prototype = {
   classID: Components.ID("{5280606b-2510-4fe0-97ef-9b5a22eafe6b}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore,
                                          Ci.nsIDOMEventListener,
                                          Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
@@ -286,20 +296,16 @@ SessionStoreService.prototype = {
     }, this);
 
     var pbs = Cc["@mozilla.org/privatebrowsing;1"].
               getService(Ci.nsIPrivateBrowsingService);
     this._inPrivateBrowsing = pbs.privateBrowsingEnabled;
 
     // Do pref migration before we store any values and start observing changes
     this._migratePrefs();
-
-    // observe prefs changes so we can modify stored data to match
-    this._prefBranch.addObserver("sessionstore.max_tabs_undo", this, true);
-    this._prefBranch.addObserver("sessionstore.max_windows_undo", this, true);
     
     // this pref is only read at startup, so no need to observe it
     this._sessionhistory_max_entries =
       this._prefBranch.getIntPref("sessionhistory.max_entries");
 
     this._restoreOnDemand =
       this._prefBranch.getBoolPref("sessionstore.restore_on_demand");
     this._prefBranch.addObserver("sessionstore.restore_on_demand", this, true);
@@ -637,21 +643,23 @@ SessionStoreService.prototype = {
 
       this._clearRestoringWindows();
       break;
     case "nsPref:changed": // catch pref changes
       switch (aData) {
       // if the user decreases the max number of closed tabs they want
       // preserved update our internal states to match that max
       case "sessionstore.max_tabs_undo":
+        this._max_tabs_undo = this._prefBranch.getIntPref("sessionstore.max_tabs_undo");
         for (let ix in this._windows) {
-          this._windows[ix]._closedTabs.splice(this._prefBranch.getIntPref("sessionstore.max_tabs_undo"), this._windows[ix]._closedTabs.length);
+          this._windows[ix]._closedTabs.splice(this._max_tabs_undo, this._windows[ix]._closedTabs.length);
         }
         break;
       case "sessionstore.max_windows_undo":
+        this._max_windows_undo = this._prefBranch.getIntPref("sessionstore.max_windows_undo");
         this._capClosedWindows();
         break;
       case "sessionstore.interval":
         this._interval = this._prefBranch.getIntPref("sessionstore.interval");
         // reset timer and save
         if (this._saveTimer) {
           this._saveTimer.cancel();
           this._saveTimer = null;
@@ -1089,20 +1097,19 @@ SessionStoreService.prototype = {
    *        Tab reference
    */
   onTabClose: function sss_onTabClose(aWindow, aTab) {
     // notify the tabbrowser that the tab state will be retrieved for the last time
     // (so that extension authors can easily set data on soon-to-be-closed tabs)
     var event = aWindow.document.createEvent("Events");
     event.initEvent("SSTabClosing", true, false);
     aTab.dispatchEvent(event);
-    
-    var maxTabsUndo = this._prefBranch.getIntPref("sessionstore.max_tabs_undo");
+
     // don't update our internal state if we don't have to
-    if (maxTabsUndo == 0) {
+    if (this._max_tabs_undo == 0) {
       return;
     }
     
     // make sure that the tab related data is up-to-date
     var tabState = this._collectTabData(aTab);
     this._updateTextAndScrollDataForTab(aWindow, aTab.linkedBrowser, tabState);
 
     // store closed-tab data for undo
@@ -1113,18 +1120,18 @@ SessionStoreService.prototype = {
       
       this._windows[aWindow.__SSi]._closedTabs.unshift({
         state: tabState,
         title: tabTitle,
         image: aTab.getAttribute("image"),
         pos: aTab._tPos
       });
       var length = this._windows[aWindow.__SSi]._closedTabs.length;
-      if (length > maxTabsUndo)
-        this._windows[aWindow.__SSi]._closedTabs.splice(maxTabsUndo, length - maxTabsUndo);
+      if (length > this._max_tabs_undo)
+        this._windows[aWindow.__SSi]._closedTabs.splice(this._max_tabs_undo, length - this._max_tabs_undo);
     }
   },
 
   /**
    * When a tab loads, save state.
    * @param aWindow
    *        Window reference
    * @param aBrowser
@@ -4259,27 +4266,26 @@ SessionStoreService.prototype = {
   },
 
   /**
    * Resize this._closedWindows to the value of the pref, except in the case
    * where we don't have any non-popup windows on Windows and Linux. Then we must
    * resize such that we have at least one non-popup window.
    */
   _capClosedWindows : function sss_capClosedWindows() {
-    let maxWindowsUndo = this._prefBranch.getIntPref("sessionstore.max_windows_undo");
-    if (this._closedWindows.length <= maxWindowsUndo)
+    if (this._closedWindows.length <= this._max_windows_undo)
       return;
-    let spliceTo = maxWindowsUndo;
+    let spliceTo = this._max_windows_undo;
 #ifndef XP_MACOSX
     let normalWindowIndex = 0;
     // try to find a non-popup window in this._closedWindows
     while (normalWindowIndex < this._closedWindows.length &&
            !!this._closedWindows[normalWindowIndex].isPopup)
       normalWindowIndex++;
-    if (normalWindowIndex >= maxWindowsUndo)
+    if (normalWindowIndex >= this._max_windows_undo)
       spliceTo = normalWindowIndex + 1;
 #endif
     this._closedWindows.splice(spliceTo, this._closedWindows.length);
   },
 
   _clearRestoringWindows: function sss__clearRestoringWindows() {
     for (let i = 0; i < this._closedWindows.length; i++) {
       delete this._closedWindows[i]._shouldRestore;