Bug 1432894 - Remove fallback preferences from Marionette. r?whimboo draft
authorAndreas Tolfsen <ato@sny.no>
Sat, 14 Apr 2018 17:28:42 +0100
changeset 786299 4a2e7361e0b13ea94a9bf1e556e203b53665114e
parent 786298 5296cf655d378fc33176e9c420bbe5b9924558a6
push id107429
push userbmo:ato@sny.no
push dateSun, 22 Apr 2018 16:57:33 +0000
reviewerswhimboo
bugs1432894
milestone61.0a1
Bug 1432894 - Remove fallback preferences from Marionette. r?whimboo Marionette has quite a complicated system in testing/marionette/components/marionette.js for support fallback preferences for earlier Firefoxen. Since Firefox 55 shipped quite some time ago it is time to remove the fallbacks. This will significantly simplify the overall complexity of this file. MozReview-Commit-ID: 5lfstTBVF3N
testing/marionette/components/marionette.js
--- a/testing/marionette/components/marionette.js
+++ b/testing/marionette/components/marionette.js
@@ -6,38 +6,35 @@
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(
     this, "env", "@mozilla.org/process/environment;1", "nsIEnvironment");
 ChromeUtils.defineModuleGetter(this, "Log",
     "resource://gre/modules/Log.jsm");
+const {
+  EnvironmentPrefs,
+  MarionettePrefs,
+} = ChromeUtils.import("chrome://marionette/content/prefs.js", {});
 ChromeUtils.defineModuleGetter(this, "Preferences",
     "resource://gre/modules/Preferences.jsm");
 XPCOMUtils.defineLazyGetter(this, "log", () => {
   let log = Log.repository.getLogger("Marionette");
   log.addAppender(new Log.DumpAppender());
   return log;
 });
 
-const PREF_ENABLED = "marionette.enabled";
-const PREF_LOG_LEVEL_FALLBACK = "marionette.logging";
-const PREF_LOG_LEVEL = "marionette.log.level";
-const PREF_PORT_FALLBACK = "marionette.defaultPrefs.port";
-const PREF_PORT = "marionette.port";
-const PREF_RECOMMENDED = "marionette.prefs.recommended";
-
-const DEFAULT_LOG_LEVEL = "info";
 const NOTIFY_RUNNING = "remote-active";
 
 // Complements -marionette flag for starting the Marionette server.
 // We also set this if Marionette is running in order to start the server
 // again after a Firefox restart.
 const ENV_ENABLED = "MOZ_MARIONETTE";
+const PREF_ENABLED = "marionette.enabled";
 
 // Besides starting based on existing prefs in a profile and a command
 // line flag, we also support inheriting prefs out of an env var, and to
 // start Marionette that way.
 //
 // This allows marionette prefs to persist when we do a restart into
 // a different profile in order to test things like Firefox refresh.
 // The environment variable itself, if present, is interpreted as a
@@ -269,138 +266,47 @@ const RECOMMENDED_PREFS = new Map([
   // Prevent starting into safe mode after application crashes
   ["toolkit.startup.max_resumed_crashes", -1],
 
 ]);
 
 const isRemote = Services.appinfo.processType ==
     Services.appinfo.PROCESS_TYPE_CONTENT;
 
-const LogLevel = {
-  get(level) {
-    let levels = new Map([
-      ["fatal", Log.Level.Fatal],
-      ["error", Log.Level.Error],
-      ["warn", Log.Level.Warn],
-      ["info", Log.Level.Info],
-      ["config", Log.Level.Config],
-      ["debug", Log.Level.Debug],
-      ["trace", Log.Level.Trace],
-    ]);
-
-    let s = String(level).toLowerCase();
-    if (!levels.has(s)) {
-      return DEFAULT_LOG_LEVEL;
-    }
-    return levels.get(s);
-  },
-};
-
-function getPrefVal(pref) {
-  const {PREF_STRING, PREF_BOOL, PREF_INT, PREF_INVALID} = Ci.nsIPrefBranch;
-
-  let type = Services.prefs.getPrefType(pref);
-  switch (type) {
-    case PREF_STRING:
-      return Services.prefs.getStringPref(pref);
-
-    case PREF_BOOL:
-      return Services.prefs.getBoolPref(pref);
-
-    case PREF_INT:
-      return Services.prefs.getIntPref(pref);
-
-    case PREF_INVALID:
-      return undefined;
-
-    default:
-      throw new TypeError(`Unexpected preference type (${type}) for ${pref}`);
-  }
-}
-
-// Get preference value of |preferred|, falling back to |fallback|
-// if |preferred| is not user-modified and |fallback| exists.
-function getPref(preferred, fallback) {
-  if (!Services.prefs.prefHasUserValue(preferred) &&
-      Services.prefs.getPrefType(fallback) != Ci.nsIPrefBranch.PREF_INVALID) {
-    return getPrefVal(fallback, getPrefVal(preferred));
-  }
-  return getPrefVal(preferred);
-}
-
-// Marionette preferences recently changed names.  This is an abstraction
-// that first looks for the new name, but falls back to using the old name
-// if the new does not exist.
-//
-// This shim can be removed when Firefox 55 ships.
-const prefs = {
-  get port() {
-    return getPref(PREF_PORT, PREF_PORT_FALLBACK);
-  },
-
-  get logLevel() {
-    let s = getPref(PREF_LOG_LEVEL, PREF_LOG_LEVEL_FALLBACK);
-    return LogLevel.get(s);
-  },
-
-  readFromEnvironment(key) {
-    const env = Cc["@mozilla.org/process/environment;1"]
-        .getService(Ci.nsIEnvironment);
-
-    if (env.exists(key)) {
-      let prefs;
-      try {
-        prefs = JSON.parse(env.get(key));
-      } catch (e) {
-        Cu.reportError(
-            "Invalid Marionette preferences in environment; " +
-            "preferences will not have been applied");
-        Cu.reportError(e);
-      }
-
-      if (prefs) {
-        for (let prefName of Object.keys(prefs)) {
-          Preferences.set(prefName, prefs[prefName]);
-        }
-      }
-    }
-  },
-};
-
 class MarionetteMainProcess {
   constructor() {
     this.server = null;
 
     // holds reference to ChromeWindow
     // used to run GFX sanity tests on Windows
     this.gfxWindow = null;
 
     // indicates that all pending window checks have been completed
     // and that we are ready to start the Marionette server
     this.finalUIStartup = false;
 
-    log.level = prefs.logLevel;
+    log.level = MarionettePrefs.logLevel;
 
     this.enabled = env.exists(ENV_ENABLED);
     this.alteredPrefs = new Set();
 
     Services.prefs.addObserver(PREF_ENABLED, this);
     Services.ppmm.addMessageListener("Marionette:IsRunning", this);
   }
 
   get running() {
     return this.server && this.server.alive;
   }
 
   set enabled(value) {
-    Services.prefs.setBoolPref(PREF_ENABLED, value);
+    MarionettePrefs.enabled = value;
   }
 
   get enabled() {
-    return Services.prefs.getBoolPref(PREF_ENABLED);
+    return MarionettePrefs.enabled;
   }
 
   receiveMessage({name}) {
     switch (name) {
       case "Marionette:IsRunning":
         return this.running;
 
       default:
@@ -420,17 +326,19 @@ class MarionetteMainProcess {
           this.uninit();
         }
         break;
 
       case "profile-after-change":
         Services.obs.addObserver(this, "command-line-startup");
         Services.obs.addObserver(this, "sessionstore-windows-restored");
 
-        prefs.readFromEnvironment(ENV_PRESERVE_PREFS);
+        for (let [pref, value] of EnvironmentPrefs.from(ENV_PRESERVE_PREFS)) {
+          Preferences.set(pref, value);
+        }
         break;
 
       // In safe mode the command line handlers are getting parsed after the
       // safe mode dialog has been closed. To allow Marionette to start
       // earlier, use the CLI startup observer notification for
       // special-cased handlers, which gets fired before the dialog appears.
       case "command-line-startup":
         Services.obs.removeObserver(this, topic);
@@ -516,29 +424,29 @@ class MarionetteMainProcess {
       // ... and for startup tests
       let startupRecorder = Promise.resolve();
       if ("@mozilla.org/test/startuprecorder;1" in Cc) {
         startupRecorder = Cc["@mozilla.org/test/startuprecorder;1"]
             .getService().wrappedJSObject.done;
       }
       await startupRecorder;
 
-      if (Preferences.get(PREF_RECOMMENDED)) {
+      if (MarionettePrefs.recommendedPrefs) {
         for (let [k, v] of RECOMMENDED_PREFS) {
           if (!Preferences.isSet(k)) {
             log.debug(`Setting recommended pref ${k} to ${v}`);
             Preferences.set(k, v);
             this.alteredPrefs.add(k);
           }
         }
       }
 
       try {
         ChromeUtils.import("chrome://marionette/content/server.js");
-        let listener = new server.TCPListener(prefs.port);
+        let listener = new server.TCPListener(MarionettePrefs.port);
         listener.start();
         this.server = listener;
       } catch (e) {
         log.fatal("Remote protocol server failed to start", e);
         this.uninit();
         Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
       }