Bug 1267567 - Treat string prefs as Unicode when the script encoding is UTF-8. r=mkaply a=ritu
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Sat, 07 May 2016 00:06:33 +0900
changeset 332800 d2397dcceff47e7a981ec70b4e3337624947f272
parent 332799 9247694843ecada80021515c747239cc3226c621
child 332801 95eba1c7da971bc1068e1b307c11310716851e3e
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkaply, ritu
bugs1267567
milestone48.0a2
Bug 1267567 - Treat string prefs as Unicode when the script encoding is UTF-8. r=mkaply a=ritu
extensions/pref/autoconfig/src/nsJSConfigTriggers.cpp
extensions/pref/autoconfig/src/prefcalls.js
--- a/extensions/pref/autoconfig/src/nsJSConfigTriggers.cpp
+++ b/extensions/pref/autoconfig/src/nsJSConfigTriggers.cpp
@@ -107,25 +107,32 @@ nsresult EvaluateAdminConfigScript(const
     if (!jsapi.Init(autoconfigSb)) {
         return NS_ERROR_UNEXPECTED;
     }
     JSContext* cx = jsapi.cx();
 
     nsAutoCString script(js_buffer, length);
     JS::RootedValue v(cx);
 
-    nsString convertedScript = NS_ConvertUTF8toUTF16(script);
-    if (convertedScript.Length() == 0) {
-      nsContentUtils::ReportToConsoleNonLocalized(
-        NS_LITERAL_STRING("Your AutoConfig file is ASCII. Please convert it to UTF-8."),
-        nsIScriptError::warningFlag,
-        NS_LITERAL_CSTRING("autoconfig"),
-        nullptr);
-      /* If the length is 0, the conversion failed. Fallback to ASCII */
-      convertedScript = NS_ConvertASCIItoUTF16(script);
+    nsString convertedScript;
+    bool isUTF8 = IsUTF8(script);
+    if (isUTF8) {
+        convertedScript = NS_ConvertUTF8toUTF16(script);
+    } else {
+        nsContentUtils::ReportToConsoleNonLocalized(
+            NS_LITERAL_STRING("Your AutoConfig file is ASCII. Please convert it to UTF-8."),
+            nsIScriptError::warningFlag,
+            NS_LITERAL_CSTRING("autoconfig"),
+            nullptr);
+        /* If the length is 0, the conversion failed. Fallback to ASCII */
+        convertedScript = NS_ConvertASCIItoUTF16(script);
+    }
+    JS::Rooted<JS::Value> value(cx, JS::BooleanValue(isUTF8));
+    if (!JS_DefineProperty(cx, autoconfigSb, "gIsUTF8", value, JSPROP_ENUMERATE)) {
+        return NS_ERROR_UNEXPECTED;
     }
     rv = xpc->EvalInSandboxObject(convertedScript, filename, cx,
                                   autoconfigSb, JSVERSION_LATEST, &v);
     NS_ENSURE_SUCCESS(rv, rv);
 
     return NS_OK;
 }
 
--- a/extensions/pref/autoconfig/src/prefcalls.js
+++ b/extensions/pref/autoconfig/src/prefcalls.js
@@ -6,30 +6,39 @@
 const nsILDAPURL = Components.interfaces.nsILDAPURL;
 const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
 const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
 const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
 const nsIPrefService = Components.interfaces.nsIPrefService;
 const PrefServiceContractID = "@mozilla.org/preferences-service;1";
 
 var gVersion;
+var gIsUTF8;
 
 function getPrefBranch() {
     
     var prefService = Components.classes[PrefServiceContractID]
                                 .getService(nsIPrefService);    
     return prefService.getBranch(null);
 }
 
 function pref(prefName, value) {
 
     try { 
         var prefBranch = getPrefBranch();
 
         if (typeof value == "string") {
+            if (gIsUTF8) {
+                const nsISupportsString = Components.interfaces.nsISupportsString;
+                let string = Components.classes["@mozilla.org/supports-string;1"]
+                                       .createInstance(nsISupportsString);
+                string.data = value;
+                prefBranch.setComplexValue(prefName, nsISupportsString, string);
+                return;
+            }
             prefBranch.setCharPref(prefName, value);
         }
         else if (typeof value == "number") {
             prefBranch.setIntPref(prefName, value);
         }
         else if (typeof value == "boolean") {
             prefBranch.setBoolPref(prefName, value);
         }
@@ -41,16 +50,24 @@ function pref(prefName, value) {
 
 function defaultPref(prefName, value) {
     
     try {
         var prefService = Components.classes[PrefServiceContractID]
                                     .getService(nsIPrefService);        
         var prefBranch = prefService.getDefaultBranch(null);
         if (typeof value == "string") {
+            if (gIsUTF8) {
+                const nsISupportsString = Components.interfaces.nsISupportsString;
+                let string = Components.classes["@mozilla.org/supports-string;1"]
+                                       .createInstance(nsISupportsString);
+                string.data = value;
+                prefBranch.setComplexValue(prefName, nsISupportsString, string);
+                return;
+            }
             prefBranch.setCharPref(prefName, value);
         }
         else if (typeof value == "number") {
             prefBranch.setIntPref(prefName, value);
         }
         else if (typeof value == "boolean") {
             prefBranch.setBoolPref(prefName, value);
         }