Bug 1946763 - Initialize Nimbus via ExperimentAPI.init() in background tasks r=nalexander,nimbus-reviewers,chumphreys

In bug 1941961, Nimbus initialization moved from Normandy into
ExperimentAPI. This updates BackgroundTaskUtils to call into the same
API as the rest of the browser to initialize Nimbus. To support this,
`ExperimentAPI.init()` now supports options for providing additional
context to the ExperimentManager's targeting context and forcing a sync
in the RemoteSettingsExperimentLoader.

Differential Revision: https://phabricator.services.mozilla.com/D242800
This commit is contained in:
Beth Rennie
2025-04-08 22:58:14 +00:00
parent 5538a4810c
commit 9af0a4ef3d
3 changed files with 25 additions and 19 deletions

View File

@@ -36,6 +36,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
// eslint-disable-next-line mozilla/no-browser-refs-in-toolkit
"resource:///modules/asrouter/ASRouterDefaultConfig.sys.mjs",
ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs",
ExperimentManager: "resource://nimbus/lib/ExperimentManager.sys.mjs",
RemoteSettingsExperimentLoader:
@@ -300,22 +301,10 @@ export var BackgroundTasksUtils = {
* targeting from default browsing profile.
*/
async enableNimbus(commandLine, defaultProfile = {}) {
try {
await lazy.ExperimentManager.onStartup({ defaultProfile });
} catch (err) {
lazy.log.error("Failed to initialize ExperimentManager:", err);
throw err;
}
try {
await lazy.RemoteSettingsExperimentLoader.enable({ forceSync: true });
} catch (err) {
lazy.log.error(
"Failed to initialize RemoteSettingsExperimentLoader:",
err
);
throw err;
}
await lazy.ExperimentAPI.init({
forceSync: true,
extraContext: { defaultProfile },
});
// Allow manual explicit opt-in to experiment branches to facilitate testing.
//

View File

@@ -97,19 +97,27 @@ export const ExperimentAPI = {
* This will initialize the ExperimentManager and the
* RemoteSettingsExperimentLoader. It will also trigger The
* RemoteSettingsExperimentLoader to update recipes.
*
* @param {object} options
* @param {object?} options.extraContext
* Additional context to use in the ExperimentManager's targeting
* context.
* @param {boolean?} options.forceSync
* Force the RemoteSettingsExperimentLoader to trigger a RemoteSettings
* sync before updating recipes for the first time.
*/
async init() {
async init({ extraContext, forceSync = false } = {}) {
if (!initialized) {
initialized = true;
try {
await this._manager.onStartup();
await this._manager.onStartup(extraContext);
} catch (e) {
lazy.log.error("Failed to initialize ExperimentManager:", e);
}
try {
await this._rsLoader.enable();
await this._rsLoader.enable({ forceSync });
} catch (e) {
lazy.log.error("Failed to enable RemoteSettingsExperimentLoader:", e);
}

View File

@@ -31,6 +31,15 @@ export const LABS_MIGRATION_FEATURE_MAP = {
};
async function migrateFirefoxLabsEnrollments() {
const bts = Cc["@mozilla.org/backgroundtasks;1"]?.getService(
Ci.nsIBackgroundTasks
);
if (bts?.isBackgroundTaskMode) {
// This migration does not apply to background task mode.
return;
}
await lazy.ExperimentAPI._rsLoader.finishedUpdating();
await lazy.ExperimentAPI._rsLoader.withUpdateLock(
async () => {