Bug 1473631: Part 11 - Replace pref observers with callbacks in nsSHistory. r?njn draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 05 Jul 2018 14:58:04 -0700
changeset 818483 5d1d3a69c397bda43b55a428e84b8226bffcb1fc
parent 818482 4b186852a9ec326bc86a1f658ee7c2abca8c10ba
child 818484 7eaf4ebeebf6e3df7d14ad59b7b7632abac9d1ae
push id116269
push usermaglione.k@gmail.com
push dateSat, 14 Jul 2018 02:18:47 +0000
reviewersnjn
bugs1473631
milestone63.0a1
Bug 1473631: Part 11 - Replace pref observers with callbacks in nsSHistory. r?njn MozReview-Commit-ID: EvwiYRg4SnC
docshell/shistory/nsSHistory.cpp
--- a/docshell/shistory/nsSHistory.cpp
+++ b/docshell/shistory/nsSHistory.cpp
@@ -150,33 +150,40 @@ enum HistCmd
 class nsSHistoryObserver final : public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
   nsSHistoryObserver() {}
 
+  void PrefChanged(const char* aPref);
+
 protected:
   ~nsSHistoryObserver() {}
 };
 
 StaticRefPtr<nsSHistoryObserver> gObserver;
 
 NS_IMPL_ISUPPORTS(nsSHistoryObserver, nsIObserver)
 
+void
+nsSHistoryObserver::PrefChanged(const char* aPref)
+{
+  nsSHistory::UpdatePrefs();
+  nsSHistory::GloballyEvictContentViewers();
+
+}
+
 NS_IMETHODIMP
 nsSHistoryObserver::Observe(nsISupports* aSubject, const char* aTopic,
                             const char16_t* aData)
 {
-  if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
-    nsSHistory::UpdatePrefs();
-    nsSHistory::GloballyEvictContentViewers();
-  } else if (!strcmp(aTopic, "cacheservice:empty-cache") ||
-             !strcmp(aTopic, "memory-pressure")) {
+  if (!strcmp(aTopic, "cacheservice:empty-cache") ||
+      !strcmp(aTopic, "memory-pressure")) {
     nsSHistory::GloballyEvictAllContentViewers();
   }
 
   return NS_OK;
 }
 
 namespace {
 
@@ -346,17 +353,19 @@ 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
   if (!gObserver) {
     gObserver = new nsSHistoryObserver();
-    Preferences::AddStrongObservers(gObserver, kObservedPrefs);
+    Preferences::RegisterCallbacks(
+        PREF_CHANGE_METHOD(nsSHistoryObserver::PrefChanged),
+        kObservedPrefs, gObserver.get());
 
     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(gObserver, "cacheservice:empty-cache", false);
 
@@ -368,17 +377,20 @@ nsSHistory::Startup()
   return NS_OK;
 }
 
 // static
 void
 nsSHistory::Shutdown()
 {
   if (gObserver) {
-    Preferences::RemoveObservers(gObserver, kObservedPrefs);
+    Preferences::UnregisterCallbacks(
+        PREF_CHANGE_METHOD(nsSHistoryObserver::PrefChanged),
+        kObservedPrefs, gObserver.get());
+
     nsCOMPtr<nsIObserverService> obsSvc =
       mozilla::services::GetObserverService();
     if (obsSvc) {
       obsSvc->RemoveObserver(gObserver, "cacheservice:empty-cache");
       obsSvc->RemoveObserver(gObserver, "memory-pressure");
     }
     gObserver = nullptr;
   }