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
|
/* 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 { Log } from "resource://gre/modules/Log.sys.mjs";
import { PromiseUtils } from "resource://gre/modules/PromiseUtils.sys.mjs";
import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
AddonRollouts: "resource://normandy/lib/AddonRollouts.sys.mjs",
AddonStudies: "resource://normandy/lib/AddonStudies.sys.mjs",
CleanupManager: "resource://normandy/lib/CleanupManager.sys.mjs",
ExperimentManager: "resource://nimbus/lib/ExperimentManager.sys.mjs",
LogManager: "resource://normandy/lib/LogManager.sys.mjs",
NormandyMigrations: "resource://normandy/NormandyMigrations.sys.mjs",
PreferenceExperiments:
"resource://normandy/lib/PreferenceExperiments.sys.mjs",
PreferenceRollouts: "resource://normandy/lib/PreferenceRollouts.sys.mjs",
RecipeRunner: "resource://normandy/lib/RecipeRunner.sys.mjs",
RemoteSettingsExperimentLoader:
"resource://nimbus/lib/RemoteSettingsExperimentLoader.sys.mjs",
ShieldPreferences: "resource://normandy/lib/ShieldPreferences.sys.mjs",
TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs",
TelemetryUtils: "resource://gre/modules/TelemetryUtils.sys.mjs",
});
const UI_AVAILABLE_NOTIFICATION = "sessionstore-windows-restored";
const BOOTSTRAP_LOGGER_NAME = "app.normandy.bootstrap";
const SHIELD_INIT_NOTIFICATION = "shield-init-complete";
const STARTUP_EXPERIMENT_PREFS_BRANCH = "app.normandy.startupExperimentPrefs.";
const STARTUP_ROLLOUT_PREFS_BRANCH = "app.normandy.startupRolloutPrefs.";
const PREF_LOGGING_LEVEL = "app.normandy.logging.level";
// Logging
const log = Log.repository.getLogger(BOOTSTRAP_LOGGER_NAME);
log.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
log.level = Services.prefs.getIntPref(PREF_LOGGING_LEVEL, Log.Level.Warn);
export var Normandy = {
studyPrefsChanged: {},
rolloutPrefsChanged: {},
defaultPrefsHaveBeenApplied: PromiseUtils.defer(),
uiAvailableNotificationObserved: PromiseUtils.defer(),
/** Initialization that needs to happen before the first paint on startup. */
async init({ runAsync = true } = {}) {
// It is important to register the listener for the UI before the first
// await, to avoid missing it.
Services.obs.addObserver(this, UI_AVAILABLE_NOTIFICATION);
// Listen for when Telemetry is disabled or re-enabled.
Services.obs.addObserver(
this,
lazy.TelemetryUtils.TELEMETRY_UPLOAD_DISABLED_TOPIC
);
// It is important this happens before the first `await`. Note that this
// also happens before migrations are applied.
this.rolloutPrefsChanged = this.applyStartupPrefs(
STARTUP_ROLLOUT_PREFS_BRANCH
);
this.studyPrefsChanged = this.applyStartupPrefs(
STARTUP_EXPERIMENT_PREFS_BRANCH
);
this.defaultPrefsHaveBeenApplied.resolve();
await lazy.NormandyMigrations.applyAll();
// Wait for the UI to be ready, or time out after 5 minutes.
if (runAsync) {
await Promise.race([
this.uiAvailableNotificationObserved.promise,
new Promise(resolve => setTimeout(resolve, 5 * 60 * 1000)),
]);
}
// Remove observer for UI notifications. It will error if the notification
// was already removed, which is fine.
try {
Services.obs.removeObserver(this, UI_AVAILABLE_NOTIFICATION);
} catch (e) {}
await this.finishInit();
},
async observe(subject, topic, data) {
if (topic === UI_AVAILABLE_NOTIFICATION) {
Services.obs.removeObserver(this, UI_AVAILABLE_NOTIFICATION);
this.uiAvailableNotificationObserved.resolve();
} else if (topic === lazy.TelemetryUtils.TELEMETRY_UPLOAD_DISABLED_TOPIC) {
await Promise.all(
[
lazy.PreferenceExperiments,
lazy.PreferenceRollouts,
lazy.AddonStudies,
lazy.AddonRollouts,
].map(service => service.onTelemetryDisabled())
);
}
},
async finishInit() {
try {
lazy.TelemetryEvents.init();
} catch (err) {
log.error("Failed to initialize telemetry events:", err);
}
await lazy.PreferenceRollouts.recordOriginalValues(
this.rolloutPrefsChanged
);
await lazy.PreferenceExperiments.recordOriginalValues(
this.studyPrefsChanged
);
// Setup logging and listen for changes to logging prefs
lazy.LogManager.configure(
Services.prefs.getIntPref(PREF_LOGGING_LEVEL, Log.Level.Warn)
);
Services.prefs.addObserver(PREF_LOGGING_LEVEL, lazy.LogManager.configure);
lazy.CleanupManager.addCleanupHandler(() =>
Services.prefs.removeObserver(
PREF_LOGGING_LEVEL,
lazy.LogManager.configure
)
);
try {
await lazy.ExperimentManager.onStartup();
} catch (err) {
log.error("Failed to initialize ExperimentManager:", err);
}
try {
await lazy.RemoteSettingsExperimentLoader.init();
} catch (err) {
log.error("Failed to initialize RemoteSettingsExperimentLoader:", err);
}
try {
await lazy.AddonStudies.init();
} catch (err) {
log.error("Failed to initialize addon studies:", err);
}
try {
await lazy.PreferenceRollouts.init();
} catch (err) {
log.error("Failed to initialize preference rollouts:", err);
}
try {
await lazy.AddonRollouts.init();
} catch (err) {
log.error("Failed to initialize addon rollouts:", err);
}
try {
await lazy.PreferenceExperiments.init();
} catch (err) {
log.error("Failed to initialize preference experiments:", err);
}
try {
lazy.ShieldPreferences.init();
} catch (err) {
log.error("Failed to initialize preferences UI:", err);
}
await lazy.RecipeRunner.init();
Services.obs.notifyObservers(null, SHIELD_INIT_NOTIFICATION);
},
async uninit() {
await lazy.CleanupManager.cleanup();
// Note that Service.pref.removeObserver and Service.obs.removeObserver have
// oppositely ordered parameters.
Services.prefs.removeObserver(
PREF_LOGGING_LEVEL,
lazy.LogManager.configure
);
for (const topic of [
lazy.TelemetryUtils.TELEMETRY_UPLOAD_DISABLED_TOPIC,
UI_AVAILABLE_NOTIFICATION,
]) {
try {
Services.obs.removeObserver(this, topic);
} catch (e) {
// topic must have already been removed or never added
}
}
},
/**
* Copy a preference subtree from one branch to another, being careful about
* types, and return the values the target branch originally had. Prefs will
* be read from the user branch and applied to the default branch.
*
* @param sourcePrefix
* The pref prefix to read prefs from.
* @returns
* The original values that each pref had on the default branch.
*/
applyStartupPrefs(sourcePrefix) {
// Note that this is called before Normandy's migrations are applied. This
// currently has no effect, but future changes should be careful to be
// backwards compatible.
const originalValues = {};
const sourceBranch = Services.prefs.getBranch(sourcePrefix);
const targetBranch = Services.prefs.getDefaultBranch("");
for (const prefName of sourceBranch.getChildList("")) {
const sourcePrefType = sourceBranch.getPrefType(prefName);
const targetPrefType = targetBranch.getPrefType(prefName);
if (
targetPrefType !== Services.prefs.PREF_INVALID &&
targetPrefType !== sourcePrefType
) {
console.error(
new Error(
`Error setting startup pref ${prefName}; pref type does not match.`
)
);
continue;
}
// record the value of the default branch before setting it
try {
switch (targetPrefType) {
case Services.prefs.PREF_STRING: {
originalValues[prefName] = targetBranch.getCharPref(prefName);
break;
}
case Services.prefs.PREF_INT: {
originalValues[prefName] = targetBranch.getIntPref(prefName);
break;
}
case Services.prefs.PREF_BOOL: {
originalValues[prefName] = targetBranch.getBoolPref(prefName);
break;
}
case Services.prefs.PREF_INVALID: {
originalValues[prefName] = null;
break;
}
default: {
// This should never happen
log.error(
`Error getting startup pref ${prefName}; unknown value type ${sourcePrefType}.`
);
}
}
} catch (e) {
if (e.result === Cr.NS_ERROR_UNEXPECTED) {
// There is a value for the pref on the user branch but not on the default branch. This is ok.
originalValues[prefName] = null;
} else {
// Unexpected error, report it and move on
console.error(e);
continue;
}
}
// now set the new default value
switch (sourcePrefType) {
case Services.prefs.PREF_STRING: {
targetBranch.setCharPref(
prefName,
sourceBranch.getCharPref(prefName)
);
break;
}
case Services.prefs.PREF_INT: {
targetBranch.setIntPref(prefName, sourceBranch.getIntPref(prefName));
break;
}
case Services.prefs.PREF_BOOL: {
targetBranch.setBoolPref(
prefName,
sourceBranch.getBoolPref(prefName)
);
break;
}
default: {
// This should never happen.
console.error(
new Error(
`Error getting startup pref ${prefName}; unexpected value type ${sourcePrefType}.`
)
);
}
}
}
return originalValues;
},
};
|