summaryrefslogtreecommitdiffstats
path: root/toolkit/components/featuregates/FeatureGate.sys.mjs
blob: e62c6c67a5e88f3f59f1ccdf179043c6c5289592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  FeatureGateImplementation:
    "resource://featuregates/FeatureGateImplementation.sys.mjs",
});

ChromeUtils.defineLazyGetter(lazy, "gFeatureDefinitionsPromise", async () => {
  const url = "resource://featuregates/feature_definitions.json";
  return fetchFeatureDefinitions(url);
});

async function fetchFeatureDefinitions(url) {
  const res = await fetch(url);
  let definitionsJson = await res.json();
  return new Map(Object.entries(definitionsJson));
}

function buildFeatureGateImplementation(definition) {
  const targetValueKeys = ["defaultValue", "isPublic"];
  for (const key of targetValueKeys) {
    definition[`${key}OriginalValue`] = definition[key];
    definition[key] = FeatureGate.evaluateTargetedValue(definition[key]);
  }
  return new lazy.FeatureGateImplementation(definition);
}

let featureGatePrefObserver = {
  onChange() {
    FeatureGate.annotateCrashReporter();
  },
  // Ignore onEnable and onDisable since onChange is called in both cases.
  onEnable() {},
  onDisable() {},
};

const kFeatureGateCache = new Map();

/** A high level control for turning features on and off. */
export class FeatureGate {
  /*
   * This is structured as a class with static methods to that sphinx-js can
   * easily document it. This constructor is required for sphinx-js to detect
   * this class for documentation.
   */

  constructor() {}

  /**
   * Constructs a feature gate object that is defined in ``Features.toml``.
   * This is the primary way to create a ``FeatureGate``.
   *
   * @param {string} id The ID of the feature's definition in `Features.toml`.
   * @param {string} testDefinitionsUrl A URL from which definitions can be fetched. Only use this in tests.
   * @throws If the ``id`` passed is not defined in ``Features.toml``.
   */
  static async fromId(id, testDefinitionsUrl = undefined) {
    let featureDefinitions;
    if (testDefinitionsUrl) {
      featureDefinitions = await fetchFeatureDefinitions(testDefinitionsUrl);
    } else {
      featureDefinitions = await lazy.gFeatureDefinitionsPromise;
    }

    if (!featureDefinitions.has(id)) {
      throw new Error(
        `Unknown feature id ${id}. Features must be defined in toolkit/components/featuregates/Features.toml`
      );
    }

    // Make a copy of the definition, since we are about to modify it
    return buildFeatureGateImplementation({ ...featureDefinitions.get(id) });
  }

  /**
   * Constructs feature gate objects for each of the definitions in ``Features.toml``.
   * @param {string} testDefinitionsUrl A URL from which definitions can be fetched. Only use this in tests.
   */
  static async all(testDefinitionsUrl = undefined) {
    let featureDefinitions;
    if (testDefinitionsUrl) {
      featureDefinitions = await fetchFeatureDefinitions(testDefinitionsUrl);
    } else {
      featureDefinitions = await lazy.gFeatureDefinitionsPromise;
    }

    let definitions = [];
    for (let definition of featureDefinitions.values()) {
      // Make a copy of the definition, since we are about to modify it
      definitions[definitions.length] = buildFeatureGateImplementation(
        Object.assign({}, definition)
      );
    }
    return definitions;
  }

  static async observePrefChangesForCrashReportAnnotation(
    testDefinitionsUrl = undefined
  ) {
    let featureDefinitions = await FeatureGate.all(testDefinitionsUrl);

    for (let definition of featureDefinitions.values()) {
      FeatureGate.addObserver(
        definition.id,
        featureGatePrefObserver,
        testDefinitionsUrl
      );
    }
  }

  static async annotateCrashReporter() {
    if (!Services.appinfo.crashReporterEnabled) {
      return;
    }
    let features = await FeatureGate.all();
    let enabledFeatures = [];
    for (let feature of features) {
      if (await feature.getValue()) {
        enabledFeatures.push(feature.preference);
      }
    }
    Services.appinfo.annotateCrashReport(
      "ExperimentalFeatures",
      enabledFeatures.join(",")
    );
  }

  /**
   * Add an observer for a feature gate by ID. If the feature is of type
   * boolean and currently enabled, `onEnable` will be called.
   *
   * The underlying feature gate instance will be shared with all other callers
   * of this function, and share an observer.
   *
   * @param {string} id The ID of the feature's definition in `Features.toml`.
   * @param {object} observer Functions to be called when the feature changes.
   *        All observer functions are optional.
   * @param {Function()} [observer.onEnable] Called when the feature becomes enabled.
   * @param {Function()} [observer.onDisable] Called when the feature becomes disabled.
   * @param {Function(newValue)} [observer.onChange] Called when the
   *        feature's state changes to any value. The new value will be passed to the
   *        function.
   * @param {string} testDefinitionsUrl A URL from which definitions can be fetched. Only use this in tests.
   * @returns {Promise<boolean>} The current value of the feature.
   */
  static async addObserver(id, observer, testDefinitionsUrl = undefined) {
    if (!kFeatureGateCache.has(id)) {
      kFeatureGateCache.set(
        id,
        await FeatureGate.fromId(id, testDefinitionsUrl)
      );
    }
    const feature = kFeatureGateCache.get(id);
    return feature.addObserver(observer);
  }

  /**
   * Remove an observer of changes from this feature
   * @param {string} id The ID of the feature's definition in `Features.toml`.
   * @param observer Then observer that was passed to addObserver to remove.
   */
  static async removeObserver(id, observer) {
    let feature = kFeatureGateCache.get(id);
    if (!feature) {
      return;
    }
    feature.removeObserver(observer);
    if (feature._observers.size === 0) {
      kFeatureGateCache.delete(id);
    }
  }

  /**
   * Get the current value of this feature gate. Implementors should avoid
   * storing the result to avoid missing changes to the feature's value.
   * Consider using :func:`addObserver` if it is necessary to store the value
   * of the feature.
   *
   * @async
   * @param {string} id The ID of the feature's definition in `Features.toml`.
   * @returns {Promise<boolean>} A promise for the value associated with this feature.
   */
  static async getValue(id, testDefinitionsUrl = undefined) {
    let feature = kFeatureGateCache.get(id);
    if (!feature) {
      feature = await FeatureGate.fromId(id, testDefinitionsUrl);
    }
    return feature.getValue();
  }

  /**
   * An alias of `getValue` for boolean typed feature gates.
   *
   * @async
   * @param {string} id The ID of the feature's definition in `Features.toml`.
   * @returns {Promise<boolean>} A promise for the value associated with this feature.
   * @throws {Error} If the feature is not a boolean.
   */
  static async isEnabled(id, testDefinitionsUrl = undefined) {
    let feature = kFeatureGateCache.get(id);
    if (!feature) {
      feature = await FeatureGate.fromId(id, testDefinitionsUrl);
    }
    return feature.isEnabled();
  }

  static targetingFacts = new Map([
    [
      "release",
      AppConstants.MOZ_UPDATE_CHANNEL === "release" || AppConstants.IS_ESR,
    ],
    ["beta", AppConstants.MOZ_UPDATE_CHANNEL === "beta"],
    ["early_beta_or_earlier", AppConstants.EARLY_BETA_OR_EARLIER],
    ["dev-edition", AppConstants.MOZ_DEV_EDITION],
    ["nightly", AppConstants.NIGHTLY_BUILD],
    ["win", AppConstants.platform === "win"],
    ["mac", AppConstants.platform === "macosx"],
    ["linux", AppConstants.platform === "linux"],
    ["android", AppConstants.platform === "android"],
    ["thunderbird", AppConstants.MOZ_APP_NAME === "thunderbird"],
  ]);

  /**
   * Take a map of conditions to values, and return the value who's conditions
   * match this browser, or the default value in the map.
   *
   * @example
   *   Calling the function as
   *
   *       evaluateTargetedValue({"default": false, "nightly,linux": true})
   *
   *   would return true on Nightly on Linux, and false otherwise.
   *
   * @param {Object} targetedValue
   *   An object mapping string conditions to values. The conditions are comma
   *   separated values specified in `targetingFacts`. A condition "default" is
   *   required, as the fallback valued. All conditions must be true.
   *
   *   If multiple values have conditions that match, then an arbitrary one will
   *   be returned. Specifically, the one returned first in an `Object.entries`
   *   iteration of the targetedValue.
   *
   * @param {Map} [targetingFacts]
   *   A map of target facts to use. Defaults to facts about the current browser.
   *
   * @param {boolean} [options.mergeFactsWithDefault]
   *   If set to true, the value passed for `targetingFacts` will be merged with
   *   the default facts.
   *
   * @returns A value from `targetedValue`.
   */
  static evaluateTargetedValue(
    targetedValue,
    targetingFacts = FeatureGate.targetingFacts,
    { mergeFactsWithDefault = false } = {}
  ) {
    if (!Object.hasOwnProperty.call(targetedValue, "default")) {
      throw new Error(
        `Targeted value ${JSON.stringify(targetedValue)} has no default key`
      );
    }

    if (mergeFactsWithDefault) {
      const combinedFacts = new Map(FeatureGate.targetingFacts);
      for (const [key, value] of targetingFacts.entries()) {
        combinedFacts.set(key, value);
      }
      targetingFacts = combinedFacts;
    }

    for (const [key, value] of Object.entries(targetedValue)) {
      if (key === "default") {
        continue;
      }
      if (key.split(",").every(part => targetingFacts.get(part))) {
        return value;
      }
    }

    return targetedValue.default;
  }
}