Bug 1345294 - nsIPrefBranch should have methods to get/set unicode strings, r=bsmedberg.
authorFlorian Quèze <florian@queze.net>
Thu, 16 Mar 2017 19:26:01 +0100
changeset 348172 6e8fab91c7b5330502facd1317d1ddcb824c96b6
parent 348171 b5df0025c79dc5cce54216194ab9ba8ddb38d222
child 348173 5a8192a650e92565aa2e85721569dad58cc1922c
push id39092
push userkwierso@gmail.com
push dateFri, 17 Mar 2017 18:14:05 +0000
treeherderautoland@88576fd417e7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1345294
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1345294 - nsIPrefBranch should have methods to get/set unicode strings, r=bsmedberg.
modules/libpref/nsIPrefBranch.idl
modules/libpref/nsPrefBranch.cpp
modules/libpref/nsPrefBranch.h
modules/libpref/test/unit/test_defaultValues.js
--- a/modules/libpref/nsIPrefBranch.idl
+++ b/modules/libpref/nsIPrefBranch.idl
@@ -94,44 +94,71 @@ interface nsIPrefBranch : nsISupports
    * @see setCharPref
    */
   [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.
+   * Called to get the state of an individual ascii 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
    */
   [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.
+   * Called to set the state of an individual ascii 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
              value of a type other than string.
    *
    * @see getCharPref
    */
   void setCharPref(in string aPrefName, in string aValue);
 
   /**
+   * Called to get the state of an individual unicode 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 setStringPref
+   */
+  [optional_argc]
+  AUTF8String getStringPref(in string aPrefName,
+                            [optional] in AUTF8String aDefaultValue);
+
+  /**
+   * Called to set the state of an individual unicode 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
+             value of a type other than string.
+   *
+   * @see getStringPref
+   */
+  void setStringPref(in string aPrefName, in AUTF8String 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
@@ -159,16 +186,17 @@ interface nsIPrefBranch : nsISupports
    * preference is a preference which represents an XPCOM object that can not
    * be easily represented using a standard boolean, integer or string value.
    *
    * @param aPrefName The complex preference to get the value of.
    * @param aType     The XPCOM interface that this complex preference
    *                  represents. Interfaces currently supported are:
    *                    - nsIFile
    *                    - nsISupportsString (UniChar)
+   *                      (deprecated; see getStringPref)
    *                    - nsIPrefLocalizedString (Localized UniChar)
    * @param aValue    The XPCOM object into which to the complex preference 
    *                  value should be retrieved.
    *
    * @throws Error The value does not exist or is the wrong type.
    *
    * @see setComplexValue
    */
@@ -180,16 +208,17 @@ interface nsIPrefBranch : nsISupports
    * preference is a preference which represents an XPCOM object that can not
    * be easily represented using a standard boolean, integer or string value.
    *
    * @param aPrefName The complex preference to set the value of.
    * @param aType     The XPCOM interface that this complex preference
    *                  represents. Interfaces currently supported are:
    *                    - nsIFile
    *                    - nsISupportsString (UniChar)
+   *                      (deprecated; see setStringPref)
    *                    - nsIPrefLocalizedString (Localized UniChar)
    * @param aValue    The XPCOM object from which to set the complex preference 
    *                  value.
    *
    * @throws Error if setting failed or the value is the wrong type.
    *
    * @see getComplexValue
    */
--- a/modules/libpref/nsPrefBranch.cpp
+++ b/modules/libpref/nsPrefBranch.cpp
@@ -237,16 +237,46 @@ 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::GetStringPref(const char *aPrefName,
+                                          const nsACString& aDefaultValue,
+                                          uint8_t _argc,
+                                          nsACString& _retval)
+{
+  nsXPIDLCString utf8String;
+  nsresult rv = GetCharPref(aPrefName, getter_Copies(utf8String));
+  if (NS_SUCCEEDED(rv)) {
+    _retval = utf8String;
+    return rv;
+  }
+
+  if (_argc == 1) {
+    _retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
+NS_IMETHODIMP nsPrefBranch::SetStringPref(const char *aPrefName, const nsACString& aValue)
+{
+  nsresult rv = CheckSanityOfStringLength(aPrefName, aValue);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  return SetCharPrefInternal(aPrefName, PromiseFlatCString(aValue).get());
+}
+
 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;
@@ -420,16 +450,20 @@ nsresult nsPrefBranch::CheckSanityOfStri
   }
   return CheckSanityOfStringLength(aPrefName, strlen(aValue));
 }
 
 nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const nsAString& aValue) {
   return CheckSanityOfStringLength(aPrefName, aValue.Length());
 }
 
+nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const nsACString& aValue) {
+  return CheckSanityOfStringLength(aPrefName, aValue.Length());
+}
+
 nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const uint32_t aLength) {
   if (aLength > MAX_PREF_LENGTH) {
     return NS_ERROR_ILLEGAL_VALUE;
   }
   if (aLength <= MAX_ADVISABLE_PREF_LENGTH) {
     return NS_OK;
   }
   nsresult rv;
--- a/modules/libpref/nsPrefBranch.h
+++ b/modules/libpref/nsPrefBranch.h
@@ -207,16 +207,17 @@ protected:
     , mFreeingObserverList(false)
   {}
 
   nsresult   GetDefaultFromPropertiesFile(const char *aPrefName, char16_t **return_buf);
   // As SetCharPref, but without any check on the length of |aValue|
   nsresult   SetCharPrefInternal(const char *aPrefName, const char *aValue);
   // Reject strings that are more than 1Mb, warn if strings are more than 16kb
   nsresult   CheckSanityOfStringLength(const char* aPrefName, const nsAString& aValue);
+  nsresult   CheckSanityOfStringLength(const char* aPrefName, const nsACString& aValue);
   nsresult   CheckSanityOfStringLength(const char* aPrefName, const char* aValue);
   nsresult   CheckSanityOfStringLength(const char* aPrefName, const uint32_t aLength);
   void RemoveExpiredCallback(PrefCallback *aCallback);
   const char *getPrefName(const char *aPrefName);
   void       freeObserverList(void);
 
 private:
   int32_t               mPrefRootLength;
--- a/modules/libpref/test/unit/test_defaultValues.js
+++ b/modules/libpref/test/unit/test_defaultValues.js
@@ -23,16 +23,32 @@ function run_test() {
   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.string";
+  do_check_throws(function() { ps.getCharPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getStringPref(prefName, ""), "");
+  strictEqual(ps.getStringPref(prefName, "éèçàê€"), "éèçàê€");
+  ps.setStringPref(prefName, "éèçàê€");
+  strictEqual(ps.getStringPref(prefName), "éèçàê€");
+  strictEqual(ps.getStringPref(prefName, "string"), "éèçàê€");
+  strictEqual(ps.getStringPref(prefName),
+              ps.getComplexValue(prefName, Ci.nsISupportsString).data);
+  let str = Cc["@mozilla.org/supports-string;1"].
+              createInstance(Ci.nsISupportsString);
+  str.data = "ù€ÚîœïŒëøÇ“";
+  ps.setComplexValue(prefName, Ci.nsISupportsString, str);
+  strictEqual(ps.getStringPref(prefName), "ù€ÚîœïŒëøÇ“");
+
   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);