Bug 1416638 - Move a couple of functions into Preferences. r=glandium. draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 14 Nov 2017 19:06:02 +1100
changeset 697568 0c0b4b728fb3201cea8fb80ad461f2d1dca69099
parent 697567 a7b76d4f8bc9b28016db7660e46591f38eb770da
child 697569 f4bb8fc7a970abd48a33865ba7a39b608028b435
push id89043
push usernnethercote@mozilla.com
push dateTue, 14 Nov 2017 08:06:49 +0000
reviewersglandium
bugs1416638
milestone58.0a1
Bug 1416638 - Move a couple of functions into Preferences. r=glandium. This will allow other functions to be moved into Preferences and be marked as `private` in subsequent patches. The patch also renames SetPrefValue() as SetValueFromDom(), because that's a clearer name. MozReview-Commit-ID: CB1xmPSmac6
modules/libpref/Preferences.cpp
modules/libpref/Preferences.h
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -449,38 +449,16 @@ PREF_SetBoolPref(const char* aPrefName, 
 {
   PrefValue pref;
   pref.mBoolVal = aValue;
 
   return pref_SetPref(
     aPrefName, pref, PrefType::Bool, aSetDefault ? kPrefSetDefault : 0);
 }
 
-static nsresult
-SetPrefValue(const char* aPrefName,
-             const dom::PrefValue& aValue,
-             PrefValueKind aKind)
-{
-  bool setDefault = (aKind == PrefValueKind::Default);
-
-  switch (aValue.type()) {
-    case dom::PrefValue::TnsCString:
-      return PREF_SetCStringPref(aPrefName, aValue.get_nsCString(), setDefault);
-
-    case dom::PrefValue::Tint32_t:
-      return PREF_SetIntPref(aPrefName, aValue.get_int32_t(), setDefault);
-
-    case dom::PrefValue::Tbool:
-      return PREF_SetBoolPref(aPrefName, aValue.get_bool(), setDefault);
-
-    default:
-      MOZ_CRASH();
-  }
-}
-
 static PrefSaveData
 pref_savePrefs()
 {
   PrefSaveData savedPrefs(gHashTable->EntryCount());
 
   for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
     auto pref = static_cast<PrefHashEntry*>(iter.Get());
 
@@ -3102,19 +3080,16 @@ Preferences::HandleDirty()
         PREF_DELAY_MS);
     }
   }
 }
 
 static nsresult
 openPrefFile(nsIFile* aFile);
 
-static Result<Ok, const char*>
-pref_InitInitialObjects();
-
 static nsresult
 pref_LoadPrefsInDirList(const char* aListId);
 
 static const char kTelemetryPref[] = "toolkit.telemetry.enabled";
 static const char kOldTelemetryPref[] = "toolkit.telemetry.enabledPreRelease";
 static const char kChannelPref[] = "app.update.channel";
 
 // clang-format off
@@ -3530,17 +3505,17 @@ Preferences::GetInstanceForService()
   }
 
   sPreferences = new Preferences();
 
   MOZ_ASSERT(!gHashTable);
   gHashTable = new PLDHashTable(
     &pref_HashTableOps, sizeof(PrefHashEntry), PREF_HASHTABLE_INITIAL_LENGTH);
 
-  Result<Ok, const char*> res = pref_InitInitialObjects();
+  Result<Ok, const char*> res = InitInitialObjects();
   if (res.isErr()) {
     sPreferences = nullptr;
     gCacheDataDesc = res.unwrapErr();
     return nullptr;
   }
 
   if (!XRE_IsParentProcess()) {
     MOZ_ASSERT(gInitPrefs);
@@ -3745,17 +3720,17 @@ Preferences::Observe(nsISupports* aSubje
     // set a pref. A blocking save here re-saves if necessary and also waits
     // for any pending saves to complete.
     SavePrefFileBlocking();
     MOZ_ASSERT(!mDirty, "Preferences should not be dirty");
     mProfileShutdown = true;
 
   } else if (!nsCRT::strcmp(aTopic, "reload-default-prefs")) {
     // Reload the default prefs from file.
-    Unused << pref_InitInitialObjects();
+    Unused << InitInitialObjects();
 
   } else if (!nsCRT::strcmp(aTopic, "suspend_process_notification")) {
     // Our process is being suspended. The OS may wake our process later,
     // or it may kill the process. In case our process is going to be killed
     // from the suspended state, we save preferences before suspending.
     rv = SavePrefFileBlocking();
   }
 
@@ -3780,17 +3755,17 @@ Preferences::ResetPrefs()
 {
   ENSURE_MAIN_PROCESS("Preferences::ResetPrefs", "all prefs");
 
   NotifyServiceObservers(NS_PREFSERVICE_RESET_TOPIC_ID);
 
   gHashTable->ClearAndPrepareForLength(PREF_HASHTABLE_INITIAL_LENGTH);
   gPrefNameArena.Clear();
 
-  return pref_InitInitialObjects().isOk() ? NS_OK : NS_ERROR_FAILURE;
+  return InitInitialObjects().isOk() ? NS_OK : NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 Preferences::ResetUserPrefs()
 {
   ENSURE_MAIN_PROCESS("Preferences::ResetUserPrefs", "all prefs");
 
   PREF_ClearAllUserPrefs();
@@ -3839,33 +3814,53 @@ Preferences::SavePrefFileAsynchronous()
 
 NS_IMETHODIMP
 Preferences::SavePrefFile(nsIFile* aFile)
 {
   // This is the method accessible from service API. Make it off main thread.
   return SavePrefFileInternal(aFile, SaveMethod::Asynchronous);
 }
 
+/* static */ nsresult
+Preferences::SetValueFromDom(const char* aPrefName,
+                             const dom::PrefValue& aValue,
+                             PrefValueKind aKind)
+{
+  switch (aValue.type()) {
+    case dom::PrefValue::TnsCString:
+      return Preferences::SetCString(aPrefName, aValue.get_nsCString(), aKind);
+
+    case dom::PrefValue::Tint32_t:
+      return Preferences::SetInt(aPrefName, aValue.get_int32_t(), aKind);
+
+    case dom::PrefValue::Tbool:
+      return Preferences::SetBool(aPrefName, aValue.get_bool(), aKind);
+
+    default:
+      MOZ_CRASH();
+  }
+}
+
 void
 Preferences::SetPreference(const PrefSetting& aPref)
 {
   const char* prefName = aPref.name().get();
   const dom::MaybePrefValue& defaultValue = aPref.defaultValue();
   const dom::MaybePrefValue& userValue = aPref.userValue();
 
   if (defaultValue.type() == dom::MaybePrefValue::TPrefValue) {
-    nsresult rv = SetPrefValue(
+    nsresult rv = SetValueFromDom(
       prefName, defaultValue.get_PrefValue(), PrefValueKind::Default);
     if (NS_FAILED(rv)) {
       return;
     }
   }
 
   if (userValue.type() == dom::MaybePrefValue::TPrefValue) {
-    SetPrefValue(prefName, userValue.get_PrefValue(), PrefValueKind::User);
+    SetValueFromDom(prefName, userValue.get_PrefValue(), PrefValueKind::User);
   } else {
     PREF_ClearUserPref(prefName);
   }
 
   // NB: we should never try to clear a default value, that doesn't
   // make sense
 }
 
@@ -4336,18 +4331,18 @@ pref_ReadPrefFromJar(nsZipArchive* aJarR
   PREF_ParseBuf(&ps, manifest.get(), manifest.Length());
   PREF_FinalizeParseState(&ps);
 
   return NS_OK;
 }
 
 // Initialize default preference JavaScript buffers from appropriate TEXT
 // resources.
-static Result<Ok, const char*>
-pref_InitInitialObjects()
+/* static */ Result<Ok, const char*>
+Preferences::InitInitialObjects()
 {
   // In the omni.jar case, we load the following prefs:
   // - jar:$gre/omni.jar!/greprefs.js
   // - jar:$gre/omni.jar!/defaults/pref/*.js
   //
   // In the non-omni.jar case, we load:
   // - $gre/greprefs.js
   //
--- a/modules/libpref/Preferences.h
+++ b/modules/libpref/Preferences.h
@@ -8,16 +8,17 @@
 #define mozilla_Preferences_h
 
 #ifndef MOZILLA_INTERNAL_API
 #error "This header is only usable from within libxul (MOZILLA_INTERNAL_API)."
 #endif
 
 #include "mozilla/Atomics.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/Result.h"
 #include "mozilla/StaticPtr.h"
 #include "nsCOMPtr.h"
 #include "nsIObserver.h"
 #include "nsIPrefBranch.h"
 #include "nsIPrefService.h"
 #include "nsTArray.h"
 #include "nsWeakReference.h"
 
@@ -42,16 +43,17 @@ enum pref_initPhase
   do {                                                                         \
   } while (0)
 #endif
 
 namespace mozilla {
 
 namespace dom {
 class PrefSetting;
+class PrefValue;
 } // namespace dom
 
 enum class PrefValueKind
 {
   Default,
   User
 };
 
@@ -357,16 +359,22 @@ public:
   // Public so the ValueObserver classes can use it.
   enum MatchKind
   {
     PrefixMatch,
     ExactMatch,
   };
 
 private:
+  static mozilla::Result<mozilla::Ok, const char*> InitInitialObjects();
+
+  static nsresult SetValueFromDom(const char* aPrefName,
+                                  const dom::PrefValue& aValue,
+                                  PrefValueKind aKind);
+
   static nsresult RegisterCallback(PrefChangedFunc aCallback,
                                    const char* aPref,
                                    void* aClosure,
                                    MatchKind aMatchKind);
   static nsresult UnregisterCallback(PrefChangedFunc aCallback,
                                      const char* aPref,
                                      void* aClosure,
                                      MatchKind aMatchKind);