Bug 1416638 - Inline and remove PREF_Set*(). r=glandium. draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 14 Nov 2017 19:06:02 +1100
changeset 697570 a5cef1224363c993d4c12c6a97f0a5304316e405
parent 697569 f4bb8fc7a970abd48a33865ba7a39b608028b435
child 697571 19ac1f9c8605cd8b673e0e5c83cbe4bcf710d35d
push id89043
push usernnethercote@mozilla.com
push dateTue, 14 Nov 2017 08:06:49 +0000
reviewersglandium
bugs1416638
milestone58.0a1
Bug 1416638 - Inline and remove PREF_Set*(). r=glandium. This patch also adds some Set*InAnyProcess() methods, and makes nsPrefBranch a friend of Preferences so it can call those methods. And it moves the thin Set*() wrapper functions to Preferences.h, alongside SetUint(). MozReview-Commit-ID: 88HhmcTFZNc
modules/libpref/Preferences.cpp
modules/libpref/Preferences.h
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -392,73 +392,16 @@ StrEscape(const char* aOriginal, nsCStri
         aResult.Append(*p);
         break;
     }
   }
 
   aResult.Append('"');
 }
 
-//
-// External calls
-//
-
-// Set a char* pref. This function takes a dotted notation of the preference
-// name (e.g. "browser.startup.homepage"). Note that this will cause the
-// preference to be saved to the file if it is different from the default. In
-// other words, this is used to set the _user_ preferences.
-//
-// If aSetDefault is set to true however, it sets the default value. This will
-// only affect the program behavior if the user does not have a value saved
-// over it for the particular preference. In addition, these will never be
-// saved out to disk.
-//
-// Each set returns PREF_VALUECHANGED if the user value changed (triggering a
-// callback), or PREF_NOERROR if the value was unchanged.
-static nsresult
-PREF_SetCStringPref(const char* aPrefName,
-                    const nsACString& aValue,
-                    bool aSetDefault)
-{
-  if (aValue.Length() > MAX_PREF_LENGTH) {
-    return NS_ERROR_ILLEGAL_VALUE;
-  }
-
-  // It's ok to stash a pointer to the temporary PromiseFlatCString's chars in
-  // pref because pref_SetPref() duplicates those chars.
-  PrefValue pref;
-  const nsCString& flat = PromiseFlatCString(aValue);
-  pref.mStringVal = flat.get();
-
-  return pref_SetPref(
-    aPrefName, pref, PrefType::String, aSetDefault ? kPrefSetDefault : 0);
-}
-
-// Like PREF_SetCStringPref(), but for integers.
-static nsresult
-PREF_SetIntPref(const char* aPrefName, int32_t aValue, bool aSetDefault)
-{
-  PrefValue pref;
-  pref.mIntVal = aValue;
-
-  return pref_SetPref(
-    aPrefName, pref, PrefType::Int, aSetDefault ? kPrefSetDefault : 0);
-}
-
-// Like PREF_SetCStringPref(), but for booleans.
-static nsresult
-PREF_SetBoolPref(const char* aPrefName, bool aValue, bool aSetDefault)
-{
-  PrefValue pref;
-  pref.mBoolVal = aValue;
-
-  return pref_SetPref(
-    aPrefName, pref, PrefType::Bool, aSetDefault ? kPrefSetDefault : 0);
-}
-
 static PrefSaveData
 pref_savePrefs()
 {
   PrefSaveData savedPrefs(gHashTable->EntryCount());
 
   for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
     auto pref = static_cast<PrefHashEntry*>(iter.Get());
 
@@ -1803,18 +1746,16 @@ PREF_ParseBuf(PrefParseState* aPS, const
 //===========================================================================
 // nsPrefBranch et al.
 //===========================================================================
 
 namespace mozilla {
 class PreferenceServiceReporter;
 } // namespace mozilla
 
-class nsPrefBranch;
-
 class PrefCallback : public PLDHashEntryHdr
 {
   friend class mozilla::PreferenceServiceReporter;
 
 public:
   typedef PrefCallback* KeyType;
   typedef const PrefCallback* KeyTypePointer;
 
@@ -2183,17 +2124,17 @@ nsPrefBranch::GetBoolPref(const char* aP
 
 NS_IMETHODIMP
 nsPrefBranch::SetBoolPref(const char* aPrefName, bool aValue)
 {
   ENSURE_MAIN_PROCESS("SetBoolPref", aPrefName);
   NS_ENSURE_ARG(aPrefName);
 
   const PrefName& pref = GetPrefName(aPrefName);
-  return PREF_SetBoolPref(pref.get(), aValue, mKind == PrefValueKind::Default);
+  return Preferences::SetBoolInAnyProcess(pref.get(), aValue, mKind);
 }
 
 NS_IMETHODIMP
 nsPrefBranch::GetFloatPrefWithDefault(const char* aPrefName,
                                       float aDefaultValue,
                                       uint8_t aArgc,
                                       float* aRetVal)
 {
@@ -2259,18 +2200,17 @@ nsPrefBranch::SetCharPref(const char* aP
 nsresult
 nsPrefBranch::SetCharPrefNoLengthCheck(const char* aPrefName,
                                        const nsACString& aValue)
 {
   ENSURE_MAIN_PROCESS("SetCharPref", aPrefName);
   NS_ENSURE_ARG(aPrefName);
 
   const PrefName& pref = GetPrefName(aPrefName);
-  return PREF_SetCStringPref(
-    pref.get(), aValue, mKind == PrefValueKind::Default);
+  return Preferences::SetCStringInAnyProcess(pref.get(), aValue, mKind);
 }
 
 NS_IMETHODIMP
 nsPrefBranch::GetStringPref(const char* aPrefName,
                             const nsACString& aDefaultValue,
                             uint8_t aArgc,
                             nsACString& aRetVal)
 {
@@ -2324,18 +2264,19 @@ nsPrefBranch::GetIntPref(const char* aPr
   return PREF_GetIntPref(pref.get(), aRetVal, mKind == PrefValueKind::Default);
 }
 
 NS_IMETHODIMP
 nsPrefBranch::SetIntPref(const char* aPrefName, int32_t aValue)
 {
   ENSURE_MAIN_PROCESS("SetIntPref", aPrefName);
   NS_ENSURE_ARG(aPrefName);
+
   const PrefName& pref = GetPrefName(aPrefName);
-  return PREF_SetIntPref(pref.get(), aValue, mKind == PrefValueKind::Default);
+  return Preferences::SetIntInAnyProcess(pref.get(), aValue, mKind);
 }
 
 NS_IMETHODIMP
 nsPrefBranch::GetComplexValue(const char* aPrefName,
                               const nsIID& aType,
                               void** aRetVal)
 {
   NS_ENSURE_ARG(aPrefName);
@@ -3686,17 +3627,17 @@ Preferences::InitializeUserPrefs()
 
   sPreferences->mDirty = false;
 
   // Don't set mCurrentFile until we're done so that dirty flags work properly.
   sPreferences->mCurrentFile = prefsFile.forget();
 
   // Migrate the old prerelease telemetry pref.
   if (!Preferences::GetBool(kOldTelemetryPref, true)) {
-    Preferences::SetBool(kTelemetryPref, false);
+    Preferences::SetBoolInAnyProcess(kTelemetryPref, false);
     Preferences::ClearUser(kOldTelemetryPref);
   }
 
   sPreferences->NotifyServiceObservers(NS_PREFSERVICE_READ_TOPIC_ID);
 }
 
 NS_IMETHODIMP
 Preferences::Observe(nsISupports* aSubject,
@@ -3823,23 +3764,26 @@ Preferences::SavePrefFile(nsIFile* aFile
 
 /* 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);
+      return Preferences::SetCStringInAnyProcess(
+        aPrefName, aValue.get_nsCString(), aKind);
 
     case dom::PrefValue::Tint32_t:
-      return Preferences::SetInt(aPrefName, aValue.get_int32_t(), aKind);
+      return Preferences::SetIntInAnyProcess(
+        aPrefName, aValue.get_int32_t(), aKind);
 
     case dom::PrefValue::Tbool:
-      return Preferences::SetBool(aPrefName, aValue.get_bool(), aKind);
+      return Preferences::SetBoolInAnyProcess(
+        aPrefName, aValue.get_bool(), aKind);
 
     default:
       MOZ_CRASH();
   }
 }
 
 void
 Preferences::SetPreference(const PrefSetting& aPref)
@@ -4491,35 +4435,38 @@ Preferences::InitInitialObjects()
     prerelease = true;
 #else
     nsAutoCString prefValue;
     Preferences::GetCString(kChannelPref, prefValue, PrefValueKind::Default);
     if (prefValue.EqualsLiteral("beta")) {
       prerelease = true;
     }
 #endif
-    PREF_SetBoolPref(kTelemetryPref, prerelease, true);
+    Preferences::SetBoolInAnyProcess(
+      kTelemetryPref, prerelease, PrefValueKind::Default);
   }
 #else
   // For platforms with Unified Telemetry (here meaning not-Android),
   // toolkit.telemetry.enabled determines whether we send "extended" data.
   // We only want extended data from pre-release channels due to size. We
   // also want it to be recorded for local developer builds (non-official builds
   // on the "default" channel).
   bool developerBuild = false;
 #ifndef MOZILLA_OFFICIAL
   developerBuild = !strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "default");
 #endif
 
   if (!strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "nightly") ||
       !strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "aurora") ||
       !strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "beta") || developerBuild) {
-    PREF_SetBoolPref(kTelemetryPref, true, true);
+    Preferences::SetBoolInAnyProcess(
+      kTelemetryPref, true, PrefValueKind::Default);
   } else {
-    PREF_SetBoolPref(kTelemetryPref, false, true);
+    Preferences::SetBoolInAnyProcess(
+      kTelemetryPref, false, PrefValueKind::Default);
   }
   PREF_LockPref(kTelemetryPref, true);
 #endif // MOZ_WIDGET_ANDROID
 
   NS_CreateServicesFromCategory(NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID,
                                 nullptr,
                                 NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID);
 
@@ -4638,79 +4585,88 @@ Preferences::GetComplex(const char* aPre
   NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
   nsCOMPtr<nsIPrefBranch> branch = (aKind == PrefValueKind::Default)
                                      ? sPreferences->mDefaultRootBranch
                                      : sPreferences->mRootBranch;
   return branch->GetComplexValue(aPrefName, aType, aResult);
 }
 
 /* static */ nsresult
-Preferences::SetCString(const char* aPrefName,
-                        const char* aValue,
-                        PrefValueKind aKind)
+Preferences::SetCStringInAnyProcess(const char* aPrefName,
+                                    const nsACString& aValue,
+                                    PrefValueKind aKind)
 {
-  ENSURE_MAIN_PROCESS("SetCString", aPrefName);
   NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
-  return PREF_SetCStringPref(
-    aPrefName, nsDependentCString(aValue), aKind == PrefValueKind::Default);
+
+  if (aValue.Length() > MAX_PREF_LENGTH) {
+    return NS_ERROR_ILLEGAL_VALUE;
+  }
+
+  // It's ok to stash a pointer to the temporary PromiseFlatCString's chars in
+  // pref because pref_SetPref() duplicates those chars.
+  PrefValue prefValue;
+  const nsCString& flat = PromiseFlatCString(aValue);
+  prefValue.mStringVal = flat.get();
+  return pref_SetPref(aPrefName,
+                      prefValue,
+                      PrefType::String,
+                      aKind == PrefValueKind::Default ? kPrefSetDefault : 0);
 }
 
 /* static */ nsresult
 Preferences::SetCString(const char* aPrefName,
                         const nsACString& aValue,
                         PrefValueKind aKind)
 {
   ENSURE_MAIN_PROCESS("SetCString", aPrefName);
-  NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
-  return PREF_SetCStringPref(
-    aPrefName, aValue, aKind == PrefValueKind::Default);
+  return SetCStringInAnyProcess(aPrefName, aValue, aKind);
 }
 
 /* static */ nsresult
-Preferences::SetString(const char* aPrefName,
-                       const char16ptr_t aValue,
-                       PrefValueKind aKind)
+Preferences::SetBoolInAnyProcess(const char* aPrefName,
+                                 bool aValue,
+                                 PrefValueKind aKind)
 {
-  ENSURE_MAIN_PROCESS("SetString", aPrefName);
   NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
-  return PREF_SetCStringPref(
-    aPrefName, NS_ConvertUTF16toUTF8(aValue), aKind == PrefValueKind::Default);
-}
-
-/* static */ nsresult
-Preferences::SetString(const char* aPrefName,
-                       const nsAString& aValue,
-                       PrefValueKind aKind)
-{
-  ENSURE_MAIN_PROCESS("SetString", aPrefName);
-  NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
-  return PREF_SetCStringPref(
-    aPrefName, NS_ConvertUTF16toUTF8(aValue), aKind == PrefValueKind::Default);
+
+  PrefValue prefValue;
+  prefValue.mBoolVal = aValue;
+  return pref_SetPref(aPrefName,
+                      prefValue,
+                      PrefType::Bool,
+                      aKind == PrefValueKind::Default ? kPrefSetDefault : 0);
 }
 
 /* static */ nsresult
 Preferences::SetBool(const char* aPrefName, bool aValue, PrefValueKind aKind)
 {
   ENSURE_MAIN_PROCESS("SetBool", aPrefName);
+  return SetBoolInAnyProcess(aPrefName, aValue, aKind);
+}
+
+/* static */ nsresult
+Preferences::SetIntInAnyProcess(const char* aPrefName,
+                                int32_t aValue,
+                                PrefValueKind aKind)
+{
   NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
-  return PREF_SetBoolPref(aPrefName, aValue, aKind == PrefValueKind::Default);
+
+  PrefValue prefValue;
+  prefValue.mIntVal = aValue;
+  return pref_SetPref(aPrefName,
+                      prefValue,
+                      PrefType::Int,
+                      aKind == PrefValueKind::Default ? kPrefSetDefault : 0);
 }
 
 /* static */ nsresult
 Preferences::SetInt(const char* aPrefName, int32_t aValue, PrefValueKind aKind)
 {
   ENSURE_MAIN_PROCESS("SetInt", aPrefName);
-  NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE);
-  return PREF_SetIntPref(aPrefName, aValue, aKind == PrefValueKind::Default);
-}
-
-/* static */ nsresult
-Preferences::SetFloat(const char* aPrefName, float aValue, PrefValueKind aKind)
-{
-  return SetCString(aPrefName, nsPrintfCString("%f", aValue).get(), aKind);
+  return SetIntInAnyProcess(aPrefName, aValue, aKind);
 }
 
 /* static */ nsresult
 Preferences::SetComplex(const char* aPrefName,
                         const nsIID& aType,
                         nsISupports* aValue,
                         PrefValueKind aKind)
 {
--- a/modules/libpref/Preferences.h
+++ b/modules/libpref/Preferences.h
@@ -14,16 +14,18 @@
 #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 "nsPrintfCString.h"
+#include "nsString.h"
 #include "nsTArray.h"
 #include "nsWeakReference.h"
 
 class nsIFile;
 
 // The callback function will get passed the pref name which triggered the call
 // and the void* data which was passed to the registered callback function.
 typedef void (*PrefChangedFunc)(const char* aPref, void* aData);
@@ -39,16 +41,18 @@ enum pref_initPhase
 };
 #define SET_PREF_PHASE(p) Preferences::SetInitPhase(p)
 #else
 #define SET_PREF_PHASE(p)                                                      \
   do {                                                                         \
   } while (0)
 #endif
 
+class nsPrefBranch;
+
 namespace mozilla {
 
 namespace dom {
 class PrefSetting;
 class PrefValue;
 } // namespace dom
 
 enum class PrefValueKind
@@ -58,16 +62,18 @@ enum class PrefValueKind
 };
 
 class Preferences final
   : public nsIPrefService
   , public nsIObserver
   , public nsIPrefBranch
   , public nsSupportsWeakReference
 {
+  friend class ::nsPrefBranch;
+
 public:
   typedef mozilla::dom::PrefSetting PrefSetting;
 
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIPREFSERVICE
   NS_FORWARD_NSIPREFBRANCH(mRootBranch->)
   NS_DECL_NSIOBSERVER
 
@@ -170,43 +176,65 @@ public:
                         PrefValueKind aKind = PrefValueKind::User)
   {
     float result = aFallback;
     GetFloat(aPrefName, &result);
     return result;
   }
 
   // Value setters.
+
   static nsresult SetBool(const char* aPrefName,
                           bool aValue,
                           PrefValueKind aKind = PrefValueKind::User);
   static nsresult SetInt(const char* aPrefName,
                          int32_t aValue,
                          PrefValueKind aKind = PrefValueKind::User);
+  static nsresult SetCString(const char* aPrefName,
+                             const nsACString& aValue,
+                             PrefValueKind aKind = PrefValueKind::User);
+
   static nsresult SetUint(const char* aPrefName,
                           uint32_t aValue,
                           PrefValueKind aKind = PrefValueKind::User)
   {
     return SetInt(aPrefName, static_cast<int32_t>(aValue), aKind);
   }
+
   static nsresult SetFloat(const char* aPrefName,
                            float aValue,
-                           PrefValueKind aKind = PrefValueKind::User);
+                           PrefValueKind aKind = PrefValueKind::User)
+  {
+    return SetCString(aPrefName, nsPrintfCString("%f", aValue).get(), aKind);
+  }
+
   static nsresult SetCString(const char* aPrefName,
                              const char* aValue,
-                             PrefValueKind aKind = PrefValueKind::User);
-  static nsresult SetCString(const char* aPrefName,
-                             const nsACString& aValue,
-                             PrefValueKind aKind = PrefValueKind::User);
+                             PrefValueKind aKind = PrefValueKind::User)
+  {
+    return Preferences::SetCString(
+      aPrefName, nsDependentCString(aValue), aKind);
+  }
+
   static nsresult SetString(const char* aPrefName,
                             const char16ptr_t aValue,
-                            PrefValueKind aKind = PrefValueKind::User);
+                            PrefValueKind aKind = PrefValueKind::User)
+  {
+    return Preferences::SetCString(
+      aPrefName, NS_ConvertUTF16toUTF8(aValue), aKind);
+  }
+
   static nsresult SetString(const char* aPrefName,
                             const nsAString& aValue,
-                            PrefValueKind aKind = PrefValueKind::User);
+                            PrefValueKind aKind = PrefValueKind::User)
+  {
+    return Preferences::SetCString(
+      aPrefName, NS_ConvertUTF16toUTF8(aValue), aKind);
+  }
+
   static nsresult SetComplex(const char* aPrefName,
                              const nsIID& aType,
                              nsISupports* aValue,
                              PrefValueKind aKind = PrefValueKind::User);
 
   // Clears user set pref.
   static nsresult ClearUser(const char* aPref);
 
@@ -365,16 +393,30 @@ public:
 
 private:
   static mozilla::Result<mozilla::Ok, const char*> InitInitialObjects();
 
   static nsresult SetValueFromDom(const char* aPrefName,
                                   const dom::PrefValue& aValue,
                                   PrefValueKind aKind);
 
+  static nsresult SetBoolInAnyProcess(
+    const char* aPrefName,
+    bool aValue,
+    PrefValueKind aKind = PrefValueKind::User);
+
+  static nsresult SetIntInAnyProcess(const char* aPrefName,
+                                     int32_t aValue,
+                                     PrefValueKind aKind = PrefValueKind::User);
+
+  static nsresult SetCStringInAnyProcess(
+    const char* aPrefName,
+    const nsACString& aValue,
+    PrefValueKind aKind = PrefValueKind::User);
+
   static nsresult RegisterCallback(PrefChangedFunc aCallback,
                                    const char* aPref,
                                    void* aClosure,
                                    MatchKind aMatchKind);
   static nsresult UnregisterCallback(PrefChangedFunc aCallback,
                                      const char* aPref,
                                      void* aClosure,
                                      MatchKind aMatchKind);