Bug 1338306 - nsIPrefBranch.get*Pref should support providing a default value, r=bsmedberg.
authorFlorian Quèze <florian@queze.net>
Wed, 01 Mar 2017 13:15:35 +0100
changeset 374326 f6479c35a73ca37abded7fe570dccc67d3e38fac
parent 374325 467c4bdf5110eb6ace092a4163a5eb4a68e0812d
child 374327 8a5b7f743d6655ebda44bb1d2523aa6f155d0605
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1338306
milestone54.0a1
Bug 1338306 - nsIPrefBranch.get*Pref should support providing a default value, r=bsmedberg.
modules/libpref/nsIPrefBranch.idl
modules/libpref/nsPrefBranch.cpp
modules/libpref/test/unit/test_defaultValues.js
modules/libpref/test/unit/xpcshell.ini
--- a/modules/libpref/nsIPrefBranch.idl
+++ b/modules/libpref/nsIPrefBranch.idl
@@ -52,22 +52,26 @@ interface nsIPrefBranch : nsISupports
    *                  value will be PREF_STRING, PREF_INT, or PREF_BOOL.
    */
   long getPrefType(in string aPrefName);
 
   /**
    * Called to get the state of an individual boolean preference.
    *
    * @param aPrefName The boolean preference to get the state of.
+   * @param aDefaultValue The value to return if the preference is not set.
    *
    * @return boolean  The value of the requested boolean preference.
    *
    * @see setBoolPref
    */
-  boolean getBoolPref(in string aPrefName);
+  [optional_argc,binaryname(GetBoolPrefWithDefault)]
+  boolean getBoolPref(in string aPrefName, [optional] in boolean aDefaultValue);
+  [noscript,binaryname(GetBoolPref)]
+  boolean getBoolPrefXPCOM(in string aPrefName);
 
   /**
    * Called to set the state of an individual boolean preference.
    *
    * @param aPrefName The boolean preference to set the state of.
    * @param aValue    The boolean value to set the preference to.
    *
    * @throws Error if setting failed or the preference has a default
@@ -78,33 +82,41 @@ interface nsIPrefBranch : nsISupports
   void setBoolPref(in string aPrefName, in boolean aValue);
 
   /**
    * Called to get the state of an individual floating-point preference.
    * "Floating point" preferences are really string preferences that
    * are converted to floating point numbers.
    *
    * @param aPrefName The floating point preference to get the state of.
+   * @param aDefaultValue The value to return if the preference is not set.
    *
    * @return float  The value of the requested floating point preference.
    *
    * @see setCharPref
    */
-  float getFloatPref(in string aPrefName);
+  [optional_argc,binaryname(GetFloatPrefWithDefault)]
+  float getFloatPref(in string aPrefName, [optional] in float aDefaultValue);
+  [noscript,binaryname(GetFloatPref)]
+  float getFloatPrefXPCOM(in string aPrefName);
 
   /**
    * Called to get the state of an individual string preference.
    *
    * @param aPrefName The string preference to retrieve.
+   * @param aDefaultValue The string to return if the preference is not set.
    *
    * @return string   The value of the requested string preference.
    *
    * @see setCharPref
    */
-  string getCharPref(in string aPrefName);
+  [optional_argc,binaryname(GetCharPrefWithDefault)]
+  string getCharPref(in string aPrefName, [optional] in string aDefaultValue);
+  [noscript,binaryname(GetCharPref)]
+  string getCharPrefXPCOM(in string aPrefName);
 
   /**
    * Called to set the state of an individual string preference.
    *
    * @param aPrefName The string preference to set.
    * @param aValue    The string value to set the preference to.
    *
    * @throws Error if setting failed or the preference has a default
@@ -113,22 +125,26 @@ interface nsIPrefBranch : nsISupports
    * @see getCharPref
    */
   void setCharPref(in string aPrefName, in string aValue);
 
   /**
    * Called to get the state of an individual integer preference.
    *
    * @param aPrefName The integer preference to get the value of.
+   * @param aDefaultValue The value to return if the preference is not set.
    *
    * @return long     The value of the requested integer preference.
    *
    * @see setIntPref
    */
-  long getIntPref(in string aPrefName);
+  [optional_argc,binaryname(GetIntPrefWithDefault)]
+  long getIntPref(in string aPrefName, [optional] in long aDefaultValue);
+  [noscript,binaryname(GetIntPref)]
+  long getIntPrefXPCOM(in string aPrefName);
 
   /**
    * Called to set the state of an individual integer preference.
    *
    * @param aPrefName The integer preference to set the value of.
    * @param aValue    The integer value to set the preference to.
    *
    * @throws Error if setting failed or the preference has a default
--- a/modules/libpref/nsPrefBranch.cpp
+++ b/modules/libpref/nsPrefBranch.cpp
@@ -140,44 +140,87 @@ NS_IMETHODIMP nsPrefBranch::GetPrefType(
     case PrefType::Invalid:
     default:
       *_retval = PREF_INVALID;
       break;
   }
   return NS_OK;
 }
 
+NS_IMETHODIMP nsPrefBranch::GetBoolPrefWithDefault(const char *aPrefName,
+                                                   bool aDefaultValue,
+                                                   uint8_t _argc, bool *_retval)
+{
+  nsresult rv = GetBoolPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    *_retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, bool *_retval)
 {
   NS_ENSURE_ARG(aPrefName);
   const char *pref = getPrefName(aPrefName);
   return PREF_GetBoolPref(pref, _retval, mIsDefault);
 }
 
 NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, bool aValue)
 {
   ENSURE_MAIN_PROCESS("Cannot SetBoolPref from content process:", aPrefName);
   NS_ENSURE_ARG(aPrefName);
   const char *pref = getPrefName(aPrefName);
   return PREF_SetBoolPref(pref, aValue, mIsDefault);
 }
 
+NS_IMETHODIMP nsPrefBranch::GetFloatPrefWithDefault(const char *aPrefName,
+                                                    float aDefaultValue,
+                                                    uint8_t _argc, float *_retval)
+{
+  nsresult rv = GetFloatPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    *_retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetFloatPref(const char *aPrefName, float *_retval)
 {
   NS_ENSURE_ARG(aPrefName);
   const char *pref = getPrefName(aPrefName);
   nsAutoCString stringVal;
   nsresult rv = GetCharPref(pref, getter_Copies(stringVal));
   if (NS_SUCCEEDED(rv)) {
     *_retval = stringVal.ToFloat(&rv);
   }
 
   return rv;
 }
 
+NS_IMETHODIMP nsPrefBranch::GetCharPrefWithDefault(const char *aPrefName,
+                                                   const char *aDefaultValue,
+                                                   uint8_t _argc, char **_retval)
+{
+  nsresult rv = GetCharPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    NS_ENSURE_ARG(aDefaultValue);
+    *_retval = NS_strdup(aDefaultValue);
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
 {
   NS_ENSURE_ARG(aPrefName);
   const char *pref = getPrefName(aPrefName);
   return PREF_CopyCharPref(pref, _retval, mIsDefault);
 }
 
 NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValue)
@@ -194,16 +237,30 @@ nsresult nsPrefBranch::SetCharPrefIntern
 {
   ENSURE_MAIN_PROCESS("Cannot SetCharPref from content process:", aPrefName);
   NS_ENSURE_ARG(aPrefName);
   NS_ENSURE_ARG(aValue);
   const char *pref = getPrefName(aPrefName);
   return PREF_SetCharPref(pref, aValue, mIsDefault);
 }
 
+NS_IMETHODIMP nsPrefBranch::GetIntPrefWithDefault(const char *aPrefName,
+                                                  int32_t aDefaultValue,
+                                                  uint8_t _argc, int32_t *_retval)
+{
+  nsresult rv = GetIntPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    *_retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, int32_t *_retval)
 {
   NS_ENSURE_ARG(aPrefName);
   const char *pref = getPrefName(aPrefName);
   return PREF_GetIntPref(pref, _retval, mIsDefault);
 }
 
 NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, int32_t aValue)
new file mode 100644
--- /dev/null
+++ b/modules/libpref/test/unit/test_defaultValues.js
@@ -0,0 +1,48 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* Tests for providing a default value to get{Bool,Char,Float,Int}Pref */
+
+function run_test() {
+  var ps = Cc["@mozilla.org/preferences-service;1"]
+             .getService(Ci.nsIPrefService)
+             .QueryInterface(Ci.nsIPrefBranch);
+
+  let prefName = "test.default.values.bool";
+  do_check_throws(function() { ps.getBoolPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getBoolPref(prefName, false), false);
+  strictEqual(ps.getBoolPref(prefName, true), true);
+  ps.setBoolPref(prefName, true);
+  strictEqual(ps.getBoolPref(prefName), true);
+  strictEqual(ps.getBoolPref(prefName, false), true);
+  strictEqual(ps.getBoolPref(prefName, true), true);
+
+  prefName = "test.default.values.char";
+  do_check_throws(function() { ps.getCharPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getCharPref(prefName, ""), "");
+  strictEqual(ps.getCharPref(prefName, "string"), "string");
+  ps.setCharPref(prefName, "foo");
+  strictEqual(ps.getCharPref(prefName), "foo");
+  strictEqual(ps.getCharPref(prefName, "string"), "foo");
+
+  prefName = "test.default.values.float";
+  do_check_throws(function() { ps.getFloatPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getFloatPref(prefName, 3.5), 3.5);
+  strictEqual(ps.getFloatPref(prefName, 0), 0);
+  ps.setCharPref(prefName, 1.75);
+  strictEqual(ps.getFloatPref(prefName), 1.75);
+  strictEqual(ps.getFloatPref(prefName, 3.5), 1.75);
+
+  prefName = "test.default.values.int";
+  do_check_throws(function() { ps.getIntPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getIntPref(prefName, 3), 3);
+  strictEqual(ps.getIntPref(prefName, 0), 0);
+  ps.setIntPref(prefName, 42);
+  strictEqual(ps.getIntPref(prefName), 42);
+  strictEqual(ps.getIntPref(prefName, 3), 42);
+}
--- a/modules/libpref/test/unit/xpcshell.ini
+++ b/modules/libpref/test/unit/xpcshell.ini
@@ -7,11 +7,12 @@ support-files =
 [test_warnings.js]
 [test_bug345529.js]
 [test_bug506224.js]
 [test_bug577950.js]
 [test_bug790374.js]
 [test_stickyprefs.js]
 support-files = data/testPrefSticky.js data/testPrefStickyUser.js
 [test_changeType.js]
+[test_defaultValues.js]
 [test_dirtyPrefs.js]
 [test_extprefs.js]
 [test_libPrefs.js]