Bug 406160 - user configuration for browser.sessionhistory.max_entries ignored, r=biesi
authortimeless@mozdev.org
Sat, 01 May 2010 21:59:08 -0700
changeset 41722 301603e7b9d8482044d0937792ff75bd69e6a7e4
parent 41721 fe361241397d60138f2cf20f218f37d9518f5e73
child 41723 67b93e5e8f5dbcd727189d1c8aebfc778e7b1a9c
push idunknown
push userunknown
push dateunknown
reviewersbiesi
bugs406160
milestone1.9.3a5pre
Bug 406160 - user configuration for browser.sessionhistory.max_entries ignored, r=biesi
docshell/shistory/src/nsSHistory.cpp
docshell/shistory/src/nsSHistory.h
--- a/docshell/shistory/src/nsSHistory.cpp
+++ b/docshell/shistory/src/nsSHistory.cpp
@@ -51,17 +51,16 @@
 #include "nsISHContainer.h"
 #include "nsIDocShellTreeItem.h"
 #include "nsIDocShellTreeNode.h"
 #include "nsIDocShellLoadInfo.h"
 #include "nsIServiceManager.h"
 #include "nsIPrefService.h"
 #include "nsIURI.h"
 #include "nsIContentViewer.h"
-#include "nsIPrefBranch2.h"
 #include "nsICacheService.h"
 #include "nsIObserverService.h"
 #include "prclist.h"
 #include "mozilla/Services.h"
 
 // For calculating max history entries and max cachable contentviewers
 #include "nspr.h"
 #include <math.h>  // for log()
@@ -105,23 +104,20 @@ protected:
 NS_IMPL_ISUPPORTS1(nsSHistoryObserver, nsIObserver)
 
 NS_IMETHODIMP
 nsSHistoryObserver::Observe(nsISupports *aSubject, const char *aTopic,
                             const PRUnichar *aData)
 {
   if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
     nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
-    prefs->GetIntPref(PREF_SHISTORY_MAX_TOTAL_VIEWERS,
-                      &nsSHistory::sHistoryMaxTotalViewers);
-    if (nsSHistory::sHistoryMaxTotalViewers < 0) {
-      nsSHistory::sHistoryMaxTotalViewers = nsSHistory::CalcMaxTotalViewers();
+    if (prefs) {
+      nsSHistory::UpdatePrefs(prefs);
+      nsSHistory::EvictGlobalContentViewer();
     }
-
-    nsSHistory::EvictGlobalContentViewer();
   } else if (!strcmp(aTopic, NS_CACHESERVICE_EMPTYCACHE_TOPIC_ID) ||
              !strcmp(aTopic, "memory-pressure")) {
     nsSHistory::EvictAllContentViewersGlobally();
   }
 
   return NS_OK;
 }
 
@@ -211,25 +207,39 @@ nsSHistory::CalcMaxTotalViewers()
   // Cap it off at 8 max
   if (viewers > 8) {
     viewers = 8;
   }
   return viewers;
 }
 
 // static
+void
+nsSHistory::UpdatePrefs(nsIPrefBranch *aPrefBranch)
+{
+  aPrefBranch->GetIntPref(PREF_SHISTORY_SIZE, &gHistoryMaxSize);
+  aPrefBranch->GetIntPref(PREF_SHISTORY_MAX_TOTAL_VIEWERS,
+                          &sHistoryMaxTotalViewers);
+  // If the pref is negative, that means we calculate how many viewers
+  // we think we should cache, based on total memory
+  if (sHistoryMaxTotalViewers < 0) {
+    sHistoryMaxTotalViewers = CalcMaxTotalViewers();
+  }
+}
+
+// static
 nsresult
 nsSHistory::Startup()
 {
   nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
   if (prefs) {
     nsCOMPtr<nsIPrefBranch> sesHBranch;
     prefs->GetBranch(nsnull, getter_AddRefs(sesHBranch));
     if (sesHBranch) {
-      sesHBranch->GetIntPref(PREF_SHISTORY_SIZE, &gHistoryMaxSize);
+      UpdatePrefs(sesHBranch);
     }
 
     // The goal of this is to unbreak users who have inadvertently set their
     // session history size to less than the default value.
     PRInt32  defaultHistoryMaxSize = 50;
     nsCOMPtr<nsIPrefBranch> defaultBranch;
     prefs->GetDefaultBranch(nsnull, getter_AddRefs(defaultBranch));
     if (defaultBranch) {
@@ -237,45 +247,39 @@ nsSHistory::Startup()
     }
 
     if (gHistoryMaxSize < defaultHistoryMaxSize) {
       gHistoryMaxSize = defaultHistoryMaxSize;
     }
     
     // Allow the user to override the max total number of cached viewers,
     // but keep the per SHistory cached viewer limit constant
-    nsCOMPtr<nsIPrefBranch2> branch = do_QueryInterface(prefs);
+    nsCOMPtr<nsIPrefBranch2> branch = do_QueryInterface(sesHBranch);
     if (branch) {
-      branch->GetIntPref(PREF_SHISTORY_MAX_TOTAL_VIEWERS,
-                         &sHistoryMaxTotalViewers);
       nsSHistoryObserver* obs = new nsSHistoryObserver();
       if (!obs) {
         return NS_ERROR_OUT_OF_MEMORY;
       }
+      branch->AddObserver(PREF_SHISTORY_SIZE, obs, PR_FALSE);
       branch->AddObserver(PREF_SHISTORY_MAX_TOTAL_VIEWERS,
                           obs, PR_FALSE);
 
       nsCOMPtr<nsIObserverService> obsSvc =
         mozilla::services::GetObserverService();
       if (obsSvc) {
         // Observe empty-cache notifications so tahat clearing the disk/memory
         // cache will also evict all content viewers.
         obsSvc->AddObserver(obs,
                             NS_CACHESERVICE_EMPTYCACHE_TOPIC_ID, PR_FALSE);
 
         // Same for memory-pressure notifications
         obsSvc->AddObserver(obs, "memory-pressure", PR_FALSE);
       }
     }
   }
-  // If the pref is negative, that means we calculate how many viewers
-  // we think we should cache, based on total memory
-  if (sHistoryMaxTotalViewers < 0) {
-    sHistoryMaxTotalViewers = CalcMaxTotalViewers();
-  }
 
   // Initialize the global list of all SHistory objects
   PR_INIT_CLIST(&gSHistoryList);
   return NS_OK;
 }
 
 /* Add an entry to the History list at mIndex and 
  * increment the index to point to the new entry
--- a/docshell/shistory/src/nsSHistory.h
+++ b/docshell/shistory/src/nsSHistory.h
@@ -48,16 +48,17 @@
 #include "nsISHistoryInternal.h"
 #include "nsISHTransaction.h"
 #include "nsIWebNavigation.h"
 #include "nsIWeakReference.h"
 #include "nsISimpleEnumerator.h"
 #include "nsISHistoryListener.h"
 #include "nsIHistoryEntry.h"
 #include "nsIObserver.h"
+#include "nsIPrefBranch2.h"
 
 // Needed to maintain global list of all SHistory objects
 #include "prclist.h"
 
 class nsIDocShell;
 class nsSHEnumerator;
 class nsSHistoryObserver;
 class nsSHistory: public PRCList,
@@ -70,16 +71,17 @@ public:
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSISHISTORY
   NS_DECL_NSISHISTORYINTERNAL
   NS_DECL_NSIWEBNAVIGATION
 
   // One time initialization method called upon docshell module construction
   static nsresult Startup();
+  static void UpdatePrefs(nsIPrefBranch *aPrefBranch);
 
   // Max number of total cached content viewers.  If the pref
   // browser.sessionhistory.max_total_viewers is negative, then
   // this value is calculated based on the total amount of memory.
   // Otherwise, it comes straight from the pref.
   static PRUint32 GetMaxTotalViewers() { return sHistoryMaxTotalViewers; }
 
 protected: