Bug 1473631: Part 13 - Replace pref observers with callbacks in DataStorage. r=njn
authorKris Maglione <maglione.k@gmail.com>
Thu, 05 Jul 2018 15:13:13 -0700
changeset 819133 1aa09d94e4fb19297cd3e80fcc6d14856c898ee8
parent 819132 59534a1c5946aefbc845437efa8925a53fa289e5
child 819134 25ab77df83364c1cf96b5ae047cf747e88090b29
push id116438
push usermaglione.k@gmail.com
push dateTue, 17 Jul 2018 05:49:06 +0000
reviewersnjn
bugs1473631
milestone63.0a1
Bug 1473631: Part 13 - Replace pref observers with callbacks in DataStorage. r=njn MozReview-Commit-ID: 4UFghn8JDTu
security/manager/ssl/DataStorage.cpp
security/manager/ssl/DataStorage.h
--- a/security/manager/ssl/DataStorage.cpp
+++ b/security/manager/ssl/DataStorage.cpp
@@ -185,16 +185,19 @@ DataStorage::DataStorage(const nsString&
   , mReadyMonitor("DataStorage::mReadyMonitor")
   , mReady(false)
   , mFilename(aFilename)
 {
 }
 
 DataStorage::~DataStorage()
 {
+  Preferences::UnregisterCallback(PREF_CHANGE_METHOD(DataStorage::PrefChanged),
+                                  "test.datastorage.write_timer_ms",
+                                  this);
 }
 
 // static
 already_AddRefed<DataStorage>
 DataStorage::Get(DataStorageClass aFilename)
 {
   switch (aFilename) {
 #define DATA_STORAGE(_)         \
@@ -425,20 +428,19 @@ DataStorage::Init(bool& aDataWillPersist
   // In the Parent process, this is a backstop for xpcshell and other cases
   // where profile-before-change might not get sent.
   os->AddObserver(this, "xpcom-shutdown", false);
   os->AddObserver(this, "xpcom-shutdown-threads", false);
 
   // For test purposes, we can set the write timer to be very fast.
   mTimerDelay = Preferences::GetInt("test.datastorage.write_timer_ms",
                                     sDataStorageDefaultTimerDelay);
-  rv = Preferences::AddStrongObserver(this, "test.datastorage.write_timer_ms");
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
+  Preferences::RegisterCallback(PREF_CHANGE_METHOD(DataStorage::PrefChanged),
+                                "test.datastorage.write_timer_ms",
+                                this);
 
   return NS_OK;
 }
 
 class DataStorage::Reader : public Runnable
 {
 public:
   explicit Reader(DataStorage* aDataStorage)
@@ -1151,20 +1153,16 @@ DataStorage::Observe(nsISupports* /*aSub
   if (!NS_IsMainThread()) {
     MOZ_ASSERT_UNREACHABLE("DataStorage::Observe called off main thread");
     return NS_ERROR_NOT_SAME_THREAD;
   }
 
   if (strcmp(aTopic, "last-pb-context-exited") == 0) {
     MutexAutoLock lock(mMutex);
     mPrivateDataTable.Clear();
-  } else if (strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0) {
-    MutexAutoLock lock(mMutex);
-    mTimerDelay = Preferences::GetInt("test.datastorage.write_timer_ms",
-                                      sDataStorageDefaultTimerDelay);
   }
 
   if (!XRE_IsParentProcess()) {
     if (strcmp(aTopic, "xpcom-shutdown") == 0) {
       sDataStorages->Clear();
     }
     return NS_OK;
   }
@@ -1188,16 +1186,24 @@ DataStorage::Observe(nsISupports* /*aSub
   } else if (strcmp(aTopic, "profile-before-change") == 0 ||
              strcmp(aTopic, "xpcom-shutdown-threads") == 0) {
     DataStorageSharedThread::Shutdown();
   }
 
   return NS_OK;
 }
 
+void
+DataStorage::PrefChanged(const char* aPref)
+{
+  MutexAutoLock lock(mMutex);
+  mTimerDelay = Preferences::GetInt("test.datastorage.write_timer_ms",
+                                    sDataStorageDefaultTimerDelay);
+}
+
 DataStorage::Entry::Entry()
   : mScore(0)
   , mLastAccessed((int32_t)(PR_Now() / sOneDayInMicroseconds))
 {
 }
 
 // Updates this entry's score. Returns true if the score has actually changed.
 // If it's been less than a day since this entry has been accessed, the score
--- a/security/manager/ssl/DataStorage.h
+++ b/security/manager/ssl/DataStorage.h
@@ -192,16 +192,18 @@ private:
 
   static nsresult ValidateKeyAndValue(const nsCString& aKey,
                                       const nsCString& aValue);
   static void TimerCallback(nsITimer* aTimer, void* aClosure);
   void SetTimer();
   void ShutdownTimer();
   void NotifyObservers(const char* aTopic);
 
+  void PrefChanged(const char* aPref);
+
   bool GetInternal(const nsCString& aKey, Entry* aEntry, DataStorageType aType,
                    const MutexAutoLock& aProofOfLock);
   nsresult PutInternal(const nsCString& aKey, Entry& aEntry,
                        DataStorageType aType,
                        const MutexAutoLock& aProofOfLock);
   void MaybeEvictOneEntry(DataStorageType aType,
                           const MutexAutoLock& aProofOfLock);
   DataStorageTable& GetTableForType(DataStorageType aType,