Bug 1473631: Part 13 - Replace pref observers with callbacks in DataStorage. r?njn draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 05 Jul 2018 15:13:13 -0700
changeset 818485 b2050037df161dbd696f7e88f4d649f7f7d6692b
parent 818484 7eaf4ebeebf6e3df7d14ad59b7b7632abac9d1ae
child 818486 c74f5185dc3d2680ac76e83106680d5f781474f4
push id116269
push usermaglione.k@gmail.com
push dateSat, 14 Jul 2018 02:18:47 +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,