1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const { PREF_BOOL, PREF_INT, PREF_INVALID, PREF_STRING } = Ci.nsIPrefBranch;
9 * @param {string=} branch
10 * Preference subtree. Uses root tree given `null`.
13 this._branch = Services.prefs.getBranch(branch);
17 * Gets value of `pref` in its known type.
19 * @param {string} pref
21 * @param {*=} fallback
22 * Fallback value to return if `pref` does not exist.
24 * @returns {(string|boolean|number)}
25 * Value of `pref`, or the `fallback` value if `pref` does
29 * If `pref` is not a recognised preference and no `fallback`
30 * value has been provided.
32 get(pref, fallback = null) {
33 switch (this._branch.getPrefType(pref)) {
35 return this._branch.getStringPref(pref);
38 return this._branch.getBoolPref(pref);
41 return this._branch.getIntPref(pref);
45 if (fallback != null) {
48 throw new TypeError(`Unrecognised preference: ${pref}`);
53 * Sets the value of `pref`.
55 * @param {string} pref
57 * @param {(string|boolean|number)} value
61 * If `value` is not the correct type for `pref`.
65 if (typeof value != "undefined" && value != null) {
66 typ = value.constructor.name;
72 return this._branch.setStringPref(pref, value);
75 return this._branch.setBoolPref(pref, value);
78 return this._branch.setIntPref(pref, value);
81 throw new TypeError(`Illegal preference type value: ${typ}`);
87 * Provides shortcuts for lazily getting and setting typed Marionette
90 * Some of Marionette's preferences are stored using primitive values
91 * that internally are represented by complex types.
93 * Because we cannot trust the input of many of these preferences,
94 * this class provides abstraction that lets us safely deal with
95 * potentially malformed input.
97 * A further complication is that we cannot rely on `Preferences.sys.mjs`
98 * in Marionette. See https://bugzilla.mozilla.org/show_bug.cgi?id=1357517
99 * for further details.
101 class MarionetteBranch extends Branch {
102 constructor(branch = "marionette.") {
107 * The `marionette.debugging.clicktostart` preference delays
108 * server startup until a modal dialogue has been clicked to allow
109 * time for user to set breakpoints in the Browser Toolbox.
114 return this.get("debugging.clicktostart", false);
118 * The `marionette.port` preference, detailing which port
119 * the TCP server should listen on.
124 return this.get("port", 2828);
128 this.set("port", newPort);
132 * Gets the `marionette.setpermission.enabled` preference, should
133 * only be used for testdriver's set_permission API.
137 get setPermissionEnabled() {
138 return this.get("setpermission.enabled", false);
142 /** Reads a JSON serialised blob stored in the environment. */
143 export class EnvironmentPrefs {
145 * Reads the environment variable `key` and tries to parse it as
146 * JSON Object, then provides an iterator over its keys and values.
148 * If the environment variable is not set, this function returns empty.
150 * @param {string} key
151 * Environment variable.
153 * @returns {Iterable.<string, (string|boolean|number)>}
156 if (!Services.env.exists(key)) {
162 prefs = JSON.parse(Services.env.get(key));
164 throw new TypeError(`Unable to parse prefs from ${key}`, e);
167 for (let prefName of Object.keys(prefs)) {
168 yield [prefName, prefs[prefName]];
173 // There is a future potential of exposing this as Marionette.prefs.port
174 // if we introduce a Marionette.jsm module.
175 export const MarionettePrefs = new MarionetteBranch();