Bug 865250 - Part 3: Use & merge switch statements instead of many if-else statements in BrowserApp.setPreferences(). r=mleibovic
authorTetsuharu OHZEKI <saneyuki.s.snyk@gmail.com>
Tue, 30 Apr 2013 22:10:01 -0400
changeset 141387 308c708ad4a9407261b34441520f9d75cb902e5f
parent 141386 eeddc4f18310b8131868fc96fff085bf0cba5311
child 141388 716dcacd68f4b913a846669b73ed623cbc1079a6
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmleibovic
bugs865250
milestone23.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 865250 - Part 3: Use & merge switch statements instead of many if-else statements in BrowserApp.setPreferences(). r=mleibovic
mobile/android/chrome/content/browser.js
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -1070,76 +1070,83 @@ var BrowserApp = {
       }
     }
     this._prefObservers = newPrefObservers;
   },
 
   setPreferences: function setPreferences(aPref) {
     let json = JSON.parse(aPref);
 
-    if (json.name == "plugin.enable") {
+    switch (json.name) {
       // The plugin pref is actually two separate prefs, so
       // we need to handle it differently
-      PluginHelper.setPluginPreference(json.value);
-      return;
-    } else if (json.name == "privacy.masterpassword.enabled") {
-      // MasterPassword pref is not real, we just need take action and leave
+      case "plugin.enable":
+        PluginHelper.setPluginPreference(json.value);
+        return;
+
       // MasterPassword pref is not real, we just need take action and leave
-      if (MasterPassword.enabled)
-        MasterPassword.removePassword(json.value);
-      else
-        MasterPassword.setPassword(json.value);
-      return;
-    } else if (json.name === "privacy.donottrackheader") {
+      case "privacy.masterpassword.enabled":
+        if (MasterPassword.enabled)
+          MasterPassword.removePassword(json.value);
+        else
+          MasterPassword.setPassword(json.value);
+        return;
+
       // "privacy.donottrackheader" is not "real" pref name, it's used in the setting menu.
-      switch (json.value) {
-        case kDoNotTrackPrefState.NO_PREF:
+      case "privacy.donottrackheader":
+        switch (json.value) {
           // Don't tell anything about tracking me
-          Services.prefs.setBoolPref("privacy.donottrackheader.enabled", false);
-          Services.prefs.clearUserPref("privacy.donottrackheader.value");
-          break;
-        case kDoNotTrackPrefState.ALLOW_TRACKING:
+          case kDoNotTrackPrefState.NO_PREF:
+            Services.prefs.setBoolPref("privacy.donottrackheader.enabled", false);
+            Services.prefs.clearUserPref("privacy.donottrackheader.value");
+            break;
           // Accept tracking me
-          Services.prefs.setBoolPref("privacy.donottrackheader.enabled", true);
-          Services.prefs.setIntPref("privacy.donottrackheader.value", 0);
-          break;
-        case kDoNotTrackPrefState.DISALLOW_TRACKING:
+          case kDoNotTrackPrefState.ALLOW_TRACKING:
+            Services.prefs.setBoolPref("privacy.donottrackheader.enabled", true);
+            Services.prefs.setIntPref("privacy.donottrackheader.value", 0);
+            break;
           // Not accept tracking me
-          Services.prefs.setBoolPref("privacy.donottrackheader.enabled", true);
-          Services.prefs.setIntPref("privacy.donottrackheader.value", 1);
-          break;
-      }
-      return;
-    } else if (json.name == SearchEngines.PREF_SUGGEST_ENABLED) {
+          case kDoNotTrackPrefState.DISALLOW_TRACKING:
+            Services.prefs.setBoolPref("privacy.donottrackheader.enabled", true);
+            Services.prefs.setIntPref("privacy.donottrackheader.value", 1);
+            break;
+        }
+        return;
+
       // Enabling or disabling suggestions will prevent future prompts
-      Services.prefs.setBoolPref(SearchEngines.PREF_SUGGEST_PROMPTED, true);
-    }
-
-    // when sending to java, we normalized special preferences that use
-    // integers and strings to represent booleans.  here, we convert them back
-    // to their actual types so we can store them.
-    switch (json.name) {
+      case SearchEngines.PREF_SUGGEST_ENABLED:
+        Services.prefs.setBoolPref(SearchEngines.PREF_SUGGEST_PROMPTED, true);
+        break;
+
+      // When sending to Java, we normalized special preferences that use
+      // integers and strings to represent booleans. Here, we convert them back
+      // to their actual types so we can store them.
       case "network.cookie.cookieBehavior":
         json.type = "int";
         json.value = parseInt(json.value);
         break;
       case "font.size.inflation.minTwips":
         json.type = "int";
         json.value = parseInt(json.value);
         break;
     }
 
-    if (json.type == "bool") {
-      Services.prefs.setBoolPref(json.name, json.value);
-    } else if (json.type == "int") {
-      Services.prefs.setIntPref(json.name, json.value);
-    } else {
-      let pref = Cc["@mozilla.org/pref-localizedstring;1"].createInstance(Ci.nsIPrefLocalizedString);
-      pref.data = json.value;
-      Services.prefs.setComplexValue(json.name, Ci.nsISupportsString, pref);
+    switch (json.type) {
+      case "bool":
+        Services.prefs.setBoolPref(json.name, json.value);
+        break;
+      case "int":
+        Services.prefs.setIntPref(json.name, json.value);
+        break;
+      default: {
+        let pref = Cc["@mozilla.org/pref-localizedstring;1"].createInstance(Ci.nsIPrefLocalizedString);
+        pref.data = json.value;
+        Services.prefs.setComplexValue(json.name, Ci.nsISupportsString, pref);
+        break;
+      }
     }
   },
 
   sanitize: function (aItems) {
     let json = JSON.parse(aItems);
     let success = true;
 
     for (let key in json) {