Bug 1252466 - Switch distribution.js to use Preferences.jsm; r=mixedpuppy
authorMichael Kaply <mozilla@kaply.com>
Tue, 01 Mar 2016 10:58:04 -0600
changeset 286237 47eb779302fa63ada9a1f7e10c0107ea0b08a9bd
parent 286236 39c23264761c7a9828597e2ccabbc1ae3cac1f0b
child 286238 4c924e5c274980190d1e897fd2fd8cc3c87da0f0
push id17917
push usermozilla@kaply.com
push dateTue, 01 Mar 2016 16:55:53 +0000
treeherderfx-team@47eb779302fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1252466
milestone47.0a1
Bug 1252466 - Switch distribution.js to use Preferences.jsm; r=mixedpuppy
browser/components/distribution.js
browser/components/tests/unit/distribution.ini
browser/components/tests/unit/test_distribution.js
--- a/browser/components/distribution.js
+++ b/browser/components/distribution.js
@@ -10,16 +10,17 @@ var Cr = Components.results;
 var Cu = Components.utils;
 
 const DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC =
   "distribution-customization-complete";
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
+Cu.import("resource://gre/modules/Preferences.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
                                   "resource://gre/modules/PlacesUtils.jsm");
 
 this.DistributionCustomizer = function DistributionCustomizer() {
   // For parallel xpcshell testing purposes allow loading the distribution.ini
   // file from the profile folder through an hidden pref.
   let loadFromProfile = false;
   try {
@@ -317,96 +318,76 @@ DistributionCustomizer.prototype = {
 
     // The global section, and several of its fields, is required
     if (!sections["Global"])
       return this._checkCustomizationComplete();
     let globalPrefs = enumToObject(this._ini.getKeys("Global"));
     if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
       return this._checkCustomizationComplete();
 
-    let defaults = this._prefSvc.getDefaultBranch(null);
+    let defaults = new Preferences({defaultBranch: true});
 
     // Global really contains info we set as prefs.  They're only
     // separate because they are "special" (read: required)
 
-    defaults.setCharPref("distribution.id", this._ini.getString("Global", "id"));
-    defaults.setCharPref("distribution.version",
-                         this._ini.getString("Global", "version"));
+    defaults.set("distribution.id", this._ini.getString("Global", "id"));
+    defaults.set("distribution.version", this._ini.getString("Global", "version"));
 
-    let partnerAbout = Cc["@mozilla.org/supports-string;1"].
-      createInstance(Ci.nsISupportsString);
+    let partnerAbout;
     try {
       if (globalPrefs["about." + this._locale]) {
-        partnerAbout.data = this._ini.getString("Global", "about." + this._locale);
+        partnerAbout = this._ini.getString("Global", "about." + this._locale);
       } else if (globalPrefs["about." + this._language]) {
-        partnerAbout.data = this._ini.getString("Global", "about." + this._language);
+        partnerAbout = this._ini.getString("Global", "about." + this._language);
       } else {
-        partnerAbout.data = this._ini.getString("Global", "about");
+        partnerAbout = this._ini.getString("Global", "about");
       }
-      defaults.setComplexValue("distribution.about",
-                               Ci.nsISupportsString, partnerAbout);
+      defaults.set("distribution.about", partnerAbout);
     } catch (e) {
       /* ignore bad prefs due to bug 895473 and move on */
       Cu.reportError(e);
     }
 
     if (sections["Preferences"]) {
       for (let key of enumerate(this._ini.getKeys("Preferences"))) {
         try {
           let value = parseValue(this._ini.getString("Preferences", key));
-          switch (typeof value) {
-          case "boolean":
-            defaults.setBoolPref(key, value);
-            break;
-          case "number":
-            defaults.setIntPref(key, value);
-            break;
-          case "string":
-            defaults.setCharPref(key, value);
-            break;
-          case "undefined":
-            defaults.setCharPref(key, value);
-            break;
-          }
+          Preferences.set(key, value);
         } catch (e) { /* ignore bad prefs and move on */ }
       }
     }
 
-    // We eval() the localizable prefs as well (even though they'll
-    // always get set as a string) to keep the INI format consistent:
-    // string prefs always need to be in quotes
-
     let localizedStr = Cc["@mozilla.org/pref-localizedstring;1"].
       createInstance(Ci.nsIPrefLocalizedString);
 
     var usedLocalizablePreferences = [];
 
     if (sections["LocalizablePreferences-" + this._locale]) {
       for (let key of enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) {
         try {
           let value = parseValue(this._ini.getString("LocalizablePreferences-" + this._locale, key));
           if (value !== undefined) {
             localizedStr.data = "data:text/plain," + key + "=" + value;
-            defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
+            defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
           }
           usedLocalizablePreferences.push(key);
         } catch (e) { /* ignore bad prefs and move on */ }
       }
     }
 
     if (sections["LocalizablePreferences-" + this._language]) {
       for (let key of enumerate(this._ini.getKeys("LocalizablePreferences-" + this._language))) {
         if (usedLocalizablePreferences.indexOf(key) > -1) {
           continue;
         }
         try {
           let value = parseValue(this._ini.getString("LocalizablePreferences-" + this._language, key));
           if (value !== undefined) {
             localizedStr.data = "data:text/plain," + key + "=" + value;
-            defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
+            defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
           }
           usedLocalizablePreferences.push(key);
         } catch (e) { /* ignore bad prefs and move on */ }
       }
     }
 
     if (sections["LocalizablePreferences"]) {
       for (let key of enumerate(this._ini.getKeys("LocalizablePreferences"))) {
@@ -414,17 +395,17 @@ DistributionCustomizer.prototype = {
           continue;
         }
         try {
           let value = parseValue(this._ini.getString("LocalizablePreferences", key));
           if (value !== undefined) {
             value = value.replace(/%LOCALE%/g, this._locale);
             value = value.replace(/%LANGUAGE%/g, this._language);
             localizedStr.data = "data:text/plain," + key + "=" + value;
-            defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
+            defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
           }
         } catch (e) { /* ignore bad prefs and move on */ }
       }
     }
 
     return this._checkCustomizationComplete();
   },
 
--- a/browser/components/tests/unit/distribution.ini
+++ b/browser/components/tests/unit/distribution.ini
@@ -1,15 +1,16 @@
 # Distribution Configuration File
 # Test of distribution preferences
 
 [Global]
 id=disttest
 version=1.0
 about=Test distribution file
+about.en-US=Tèƨƭ δïƨƭřïβúƭïôñ ƒïℓè
 
 [Preferences]
 distribution.test.string="Test String"
 distribution.test.string.noquotes=Test String
 distribution.test.int=777
 distribution.test.bool.true=true
 distribution.test.bool.false=false
 
--- a/browser/components/tests/unit/test_distribution.js
+++ b/browser/components/tests/unit/test_distribution.js
@@ -57,16 +57,20 @@ do_register_cleanup(function () {
   Assert.ok(!iniFile.exists());
 });
 
 add_task(function* () {
   // Force distribution.
   let glue = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver)
   glue.observe(null, TOPIC_BROWSERGLUE_TEST, TOPICDATA_DISTRIBUTION_CUSTOMIZATION);
 
+  Assert.equal(Services.prefs.getCharPref("distribution.id"), "disttest");
+  Assert.equal(Services.prefs.getCharPref("distribution.version"), "1.0");
+  Assert.equal(Services.prefs.getComplexValue("distribution.about", Ci.nsISupportsString).data, "Tèƨƭ δïƨƭřïβúƭïôñ ƒïℓè");
+
   Assert.equal(Services.prefs.getCharPref("distribution.test.string"), "Test String");
   Assert.equal(Services.prefs.getCharPref("distribution.test.string.noquotes"), "Test String");
   Assert.equal(Services.prefs.getIntPref("distribution.test.int"), 777);
   Assert.equal(Services.prefs.getBoolPref("distribution.test.bool.true"), true);
   Assert.equal(Services.prefs.getBoolPref("distribution.test.bool.false"), false);
   Assert.equal(Services.prefs.getComplexValue("distribution.test.locale", Ci.nsIPrefLocalizedString).data, "en-US");
   Assert.equal(Services.prefs.getComplexValue("distribution.test.language.en", Ci.nsIPrefLocalizedString).data, "en");
   Assert.equal(Services.prefs.getComplexValue("distribution.test.locale.en-US", Ci.nsIPrefLocalizedString).data, "en-US");