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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
/* 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/. */
"use strict";
const EXPORTED_SYMBOLS = [
"ExperimentAPI",
"NimbusFeatures",
"_ExperimentFeature",
];
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
ExperimentStore: "resource://nimbus/lib/ExperimentStore.jsm",
ExperimentManager: "resource://nimbus/lib/ExperimentManager.jsm",
RemoteSettings: "resource://services-settings/remote-settings.js",
FeatureManifest: "resource://nimbus/FeatureManifest.js",
});
const IS_MAIN_PROCESS =
Services.appinfo.processType === Services.appinfo.PROCESS_TYPE_DEFAULT;
const COLLECTION_ID_PREF = "messaging-system.rsexperimentloader.collection_id";
const COLLECTION_ID_FALLBACK = "nimbus-desktop-experiments";
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"COLLECTION_ID",
COLLECTION_ID_PREF,
COLLECTION_ID_FALLBACK
);
const EXPOSURE_EVENT_CATEGORY = "normandy";
const EXPOSURE_EVENT_METHOD = "expose";
const EXPOSURE_EVENT_OBJECT = "nimbus_experiment";
function parseJSON(value) {
if (value) {
try {
return JSON.parse(value);
} catch (e) {
Cu.reportError(e);
}
}
return null;
}
function featuresCompat(branch) {
if (!branch) {
return [];
}
let { features } = branch;
// In <=v1.5.0 of the Nimbus API, experiments had single feature
if (!features) {
features = [branch.feature];
}
return features;
}
const experimentBranchAccessor = {
get: (target, prop) => {
// Offer an API where we can access `branch.feature.*`.
// This is a useful shorthand that hides the fact that
// even single-feature recipes are still represented
// as an array with 1 item
if (!(prop in target) && target.features) {
return target.features.find(f => f.featureId === prop);
} else if (target.feature?.featureId === prop) {
// Backwards compatibility for version 1.6.2 and older
return target.feature;
}
return target[prop];
},
};
const ExperimentAPI = {
/**
* @returns {Promise} Resolves when the API has synchronized to the main store
*/
ready() {
return this._store.ready();
},
/**
* Returns an experiment, including all its metadata
* Sends exposure event
*
* @param {{slug?: string, featureId?: string}} options slug = An experiment identifier
* or feature = a stable identifier for a type of experiment
* @returns {{slug: string, active: bool}} A matching experiment if one is found.
*/
getExperiment({ slug, featureId } = {}) {
if (!slug && !featureId) {
throw new Error(
"getExperiment(options) must include a slug or a feature."
);
}
let experimentData;
try {
if (slug) {
experimentData = this._store.get(slug);
} else if (featureId) {
experimentData = this._store.getExperimentForFeature(featureId);
}
} catch (e) {
Cu.reportError(e);
}
if (experimentData) {
return {
slug: experimentData.slug,
active: experimentData.active,
branch: new Proxy(experimentData.branch, experimentBranchAccessor),
};
}
return null;
},
/**
* Used by getExperimentMetaData and getRolloutMetaData
*
* @param {{slug: string, featureId: string}} options Enrollment identifier
* @param isRollout Is enrollment an experiment or a rollout
* @returns {object} Enrollment metadata
*/
getEnrollmentMetaData({ slug, featureId }, isRollout) {
if (!slug && !featureId) {
throw new Error(
"getExperiment(options) must include a slug or a feature."
);
}
let experimentData;
try {
if (slug) {
experimentData = this._store.get(slug);
} else if (featureId) {
if (isRollout) {
experimentData = this._store.getRolloutForFeature(featureId);
} else {
experimentData = this._store.getExperimentForFeature(featureId);
}
}
} catch (e) {
Cu.reportError(e);
}
if (experimentData) {
return {
slug: experimentData.slug,
active: experimentData.active,
branch: { slug: experimentData.branch.slug },
};
}
return null;
},
/**
* Return experiment slug its status and the enrolled branch slug
* Does NOT send exposure event because you only have access to the slugs
*/
getExperimentMetaData(options) {
return this.getEnrollmentMetaData(options);
},
/**
* Return rollout slug its status and the enrolled branch slug
* Does NOT send exposure event because you only have access to the slugs
*/
getRolloutMetaData(options) {
return this.getEnrollmentMetaData(options, true);
},
/**
* Return FeatureConfig from first active experiment where it can be found
* @param {{slug: string, featureId: string }}
* @returns {Branch | null}
*/
getActiveBranch({ slug, featureId }) {
let experiment = null;
try {
if (slug) {
experiment = this._store.get(slug);
} else if (featureId) {
experiment = this._store.getExperimentForFeature(featureId);
}
} catch (e) {
Cu.reportError(e);
}
if (!experiment) {
return null;
}
// Default to null for feature-less experiments where we're only
// interested in exposure.
return experiment?.branch || null;
},
/**
* Registers an event listener.
* The following event names are used:
* `update` - an experiment is updated, for example it is no longer active
*
* @param {string} eventName must follow the pattern `event:slug-name`
* @param {{slug?: string, featureId: string?}} options
* @param {function} callback
* @returns {void}
*/
on(eventName, options, callback) {
if (!options) {
throw new Error("Please include an experiment slug or featureId");
}
let fullEventName = `${eventName}:${options.slug || options.featureId}`;
if (this._store._isReady) {
let experiment = this.getExperiment(options);
// Only if we have an experiment that matches what the caller requested
if (experiment) {
// If the store already has the experiment in the store then we should
// notify. This covers the startup scenario or cases where listeners
// are attached later than the `update` events.
callback(fullEventName, experiment);
}
}
this._store.on(fullEventName, callback);
},
/**
* Deregisters an event listener.
* @param {string} eventName
* @param {function} callback
*/
off(eventName, callback) {
this._store.off(eventName, callback);
},
/**
* Returns the recipe for a given experiment slug
*
* This should noly be called from the main process.
*
* Note that the recipe is directly fetched from RemoteSettings, which has
* all the recipe metadata available without relying on the `this._store`.
* Therefore, calling this function does not require to call `this.ready()` first.
*
* @param slug {String} An experiment identifier
* @returns {Recipe|undefined} A matching experiment recipe if one is found
*/
async getRecipe(slug) {
if (!IS_MAIN_PROCESS) {
throw new Error(
"getRecipe() should only be called from the main process"
);
}
let recipe;
try {
[recipe] = await this._remoteSettingsClient.get({
// Do not sync the RS store, let RemoteSettingsExperimentLoader do that
syncIfEmpty: false,
filters: { slug },
});
} catch (e) {
// If an error occurs in .get(), an empty list is returned and the destructuring
// assignment will throw.
Cu.reportError(e);
recipe = undefined;
}
return recipe;
},
/**
* Returns all the branches for a given experiment slug
*
* This should only be called from the main process. Like `getRecipe()`,
* calling this function does not require to call `this.ready()` first.
*
* @param slug {String} An experiment identifier
* @returns {[Branches]|undefined} An array of branches for the given slug
*/
async getAllBranches(slug) {
if (!IS_MAIN_PROCESS) {
throw new Error(
"getAllBranches() should only be called from the main process"
);
}
const recipe = await this.getRecipe(slug);
return recipe?.branches.map(
branch => new Proxy(branch, experimentBranchAccessor)
);
},
recordExposureEvent({ featureId, experimentSlug, branchSlug }) {
Services.telemetry.setEventRecordingEnabled(EXPOSURE_EVENT_CATEGORY, true);
try {
Services.telemetry.recordEvent(
EXPOSURE_EVENT_CATEGORY,
EXPOSURE_EVENT_METHOD,
EXPOSURE_EVENT_OBJECT,
experimentSlug,
{
branchSlug,
featureId,
}
);
} catch (e) {
Cu.reportError(e);
}
Glean.nimbusEvents.exposure.record({
experiment: experimentSlug,
branch: branchSlug,
feature_id: featureId,
});
},
};
/**
* Singleton that holds lazy references to _ExperimentFeature instances
* defined by the FeatureManifest
*/
const NimbusFeatures = {};
for (let feature in lazy.FeatureManifest) {
XPCOMUtils.defineLazyGetter(NimbusFeatures, feature, () => {
return new _ExperimentFeature(feature);
});
}
class _ExperimentFeature {
constructor(featureId, manifest) {
this.featureId = featureId;
this.prefGetters = {};
this.manifest = manifest || lazy.FeatureManifest[featureId];
if (!this.manifest) {
Cu.reportError(
`No manifest entry for ${featureId}. Please add one to toolkit/components/nimbus/FeatureManifest.js`
);
}
this._didSendExposureEvent = false;
const variables = this.manifest?.variables || {};
Object.keys(variables).forEach(key => {
const { type, fallbackPref } = variables[key];
if (fallbackPref) {
XPCOMUtils.defineLazyPreferenceGetter(
this.prefGetters,
key,
fallbackPref,
null,
() => {
ExperimentAPI._store._emitFeatureUpdate(
this.featureId,
"pref-updated"
);
},
type === "json" ? parseJSON : val => val
);
}
});
}
getSetPrefName(variable) {
return this.manifest?.variables?.[variable]?.setPref;
}
getFallbackPrefName(variable) {
return this.manifest?.variables?.[variable]?.fallbackPref;
}
/**
* Wait for ExperimentStore to load giving access to experiment features that
* do not have a pref cache
*/
ready() {
return ExperimentAPI.ready();
}
/**
* Lookup feature variables in experiments, prefs, and remote defaults.
* @param {{defaultValues?: {[variableName: string]: any}}} options
* @returns {{[variableName: string]: any}} The feature value
*/
getAllVariables({ defaultValues = null } = {}) {
const branch = ExperimentAPI.getActiveBranch({ featureId: this.featureId });
const featureValue = featuresCompat(branch).find(
({ featureId }) => featureId === this.featureId
)?.value;
return {
...this.prefGetters,
...defaultValues,
...(featureValue ? featureValue : this.getRollout()?.value),
};
}
getVariable(variable) {
if (!this.manifest?.variables?.[variable]) {
// Only throw in nightly/tests
if (Cu.isInAutomation || AppConstants.NIGHTLY_BUILD) {
throw new Error(
`Nimbus: Warning - variable "${variable}" is not defined in FeatureManifest.js`
);
}
}
// Next, check if an experiment is defined
const branch = ExperimentAPI.getActiveBranch({
featureId: this.featureId,
});
const experimentValue = featuresCompat(branch).find(
({ featureId }) => featureId === this.featureId
)?.value?.[variable];
if (typeof experimentValue !== "undefined") {
return experimentValue;
}
// Next, check remote defaults
const remoteValue = this.getRollout()?.value?.[variable];
if (typeof remoteValue !== "undefined") {
return remoteValue;
}
// Return the default preference value
const prefName = this.getFallbackPrefName(variable);
return prefName ? this.prefGetters[variable] : undefined;
}
getRollout() {
let remoteConfig = ExperimentAPI._store.getRolloutForFeature(
this.featureId
);
if (!remoteConfig) {
return null;
}
if (remoteConfig.branch?.features) {
return remoteConfig.branch?.features.find(
f => f.featureId === this.featureId
);
}
// This path is deprecated and will be removed in the future
if (remoteConfig.branch?.feature) {
return remoteConfig.branch.feature;
}
return null;
}
recordExposureEvent({ once = false } = {}) {
if (once && this._didSendExposureEvent) {
return;
}
let enrollmentData = ExperimentAPI.getExperimentMetaData({
featureId: this.featureId,
});
if (!enrollmentData) {
enrollmentData = ExperimentAPI.getRolloutMetaData({
featureId: this.featureId,
});
}
// Exposure only sent if user is enrolled in an experiment
if (enrollmentData) {
ExperimentAPI.recordExposureEvent({
featureId: this.featureId,
experimentSlug: enrollmentData.slug,
branchSlug: enrollmentData.branch?.slug,
});
this._didSendExposureEvent = true;
}
}
onUpdate(callback) {
ExperimentAPI._store._onFeatureUpdate(this.featureId, callback);
}
off(callback) {
ExperimentAPI._store._offFeatureUpdate(this.featureId, callback);
}
/**
* The applications this feature applies to.
*
*/
get applications() {
return this.manifest.applications ?? ["firefox-desktop"];
}
debug() {
return {
variables: this.getAllVariables(),
experiment: ExperimentAPI.getExperimentMetaData({
featureId: this.featureId,
}),
fallbackPrefs: Object.keys(this.prefGetters).map(prefName => [
prefName,
this.prefGetters[prefName],
]),
rollouts: this.getRollout(),
};
}
}
XPCOMUtils.defineLazyGetter(ExperimentAPI, "_store", function() {
return IS_MAIN_PROCESS
? lazy.ExperimentManager.store
: new lazy.ExperimentStore();
});
XPCOMUtils.defineLazyGetter(ExperimentAPI, "_remoteSettingsClient", function() {
return lazy.RemoteSettings(lazy.COLLECTION_ID);
});
|