Commit 881a5070 authored by Florian Quèze's avatar Florian Quèze
Browse files

Bug 1345294 - nsIPrefBranch should have methods to get/set unicode strings, r=bsmedberg.

parent 2ce3d7d3
Loading
Loading
Loading
Loading
+31 −2
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ interface nsIPrefBranch : nsISupports
  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.
@@ -114,7 +114,7 @@ interface nsIPrefBranch : nsISupports
  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.
@@ -126,6 +126,33 @@ interface nsIPrefBranch : nsISupports
   */
  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.
   *
@@ -164,6 +191,7 @@ interface nsIPrefBranch : nsISupports
   *                  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.
@@ -185,6 +213,7 @@ interface nsIPrefBranch : nsISupports
   *                  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.
+34 −0
Original line number Diff line number Diff line
@@ -242,6 +242,36 @@ nsresult nsPrefBranch::SetCharPrefInternal(const char *aPrefName, const char *aV
  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)
@@ -425,6 +455,10 @@ nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const ns
  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;
+1 −0
Original line number Diff line number Diff line
@@ -212,6 +212,7 @@ protected:
  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);
+16 −0
Original line number Diff line number Diff line
@@ -28,6 +28,22 @@ function run_test() {
  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);