tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit e4bfb45d8f897ae1fe9371b3a67affb0bdb9258d
parent 5916cab7888df34a1b6f97c30f06015cf1fb2cfc
Author: Beth Rennie <beth@brennie.ca>
Date:   Tue,  7 Oct 2025 13:13:19 +0000

Bug 1985079 - Move NimbusEnrollments ownership to SharedDataMap r=nimbus-reviewers,relud

Differential Revision: https://phabricator.services.mozilla.com/D264293

Diffstat:
Mtoolkit/components/nimbus/lib/Enrollments.sys.mjs | 28++++++++++++++++++++++++++--
Mtoolkit/components/nimbus/lib/ExperimentStore.sys.mjs | 16----------------
Mtoolkit/components/nimbus/lib/SharedDataMap.sys.mjs | 12+++++++++++-
3 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/toolkit/components/nimbus/lib/Enrollments.sys.mjs b/toolkit/components/nimbus/lib/Enrollments.sys.mjs @@ -55,6 +55,13 @@ let SYNC_ENROLLMENTS_ENABLED = Services.prefs.getBoolPref( */ export class NimbusEnrollments { /** + * Whether the NimbusEvents instance is initialized or not. + * + * @type {boolean} + */ + #initialized; + + /** * The ExperimentStore. * * @type {ExperimentStore} @@ -96,6 +103,7 @@ export class NimbusEnrollments { #pending; constructor(store) { + this.#initialized = false; this.#store = store; this.#flushTask = new lazy.DeferredTask( @@ -113,6 +121,19 @@ export class NimbusEnrollments { this.#pending = new Map(); } + async init() { + if (this.#initialized) { + throw new Error("Already initialized"); + } + + this.#initialized = true; + + const conn = await lazy.ProfilesDatastoreService.getConnection(); + return conn.executeTransaction(async txn => { + return NimbusEnrollments.loadEnrollments(txn); + }); + } + /** * The number of pending writes. */ @@ -449,11 +470,14 @@ export class NimbusEnrollments { /** * Load the enrollments from the NimbusEnrollments table. + + * @param {OpenedConnection} txn An optional connection, used when this is + * called within a transaction. * * @returns {Promise<Record<string, object>>} The enrollments from the * NimbusEnrollments table. */ - static async loadEnrollments() { + static async loadEnrollments(txn) { function copyProperties(target, src, properties) { for (const property of properties) { target[property] = src[property]; @@ -511,7 +535,7 @@ export class NimbusEnrollments { return [enrollment.slug, enrollment]; } - const conn = await lazy.ProfilesDatastoreService.getConnection(); + const conn = txn ?? (await lazy.ProfilesDatastoreService.getConnection()); const rows = await conn.execute( ` SELECT diff --git a/toolkit/components/nimbus/lib/ExperimentStore.sys.mjs b/toolkit/components/nimbus/lib/ExperimentStore.sys.mjs @@ -214,28 +214,12 @@ ChromeUtils.defineLazyGetter(lazy, "syncDataStore", () => { const DEFAULT_STORE_ID = "ExperimentStoreData"; -const IS_MAIN_PROCESS = - Services.appinfo.processType === Services.appinfo.PROCESS_TYPE_DEFAULT; - export class ExperimentStore extends SharedDataMap { static SYNC_DATA_PREF_BRANCH = SYNC_DATA_PREF_BRANCH; static SYNC_DEFAULTS_PREF_BRANCH = SYNC_DEFAULTS_PREF_BRANCH; constructor(sharedDataKey, options) { super(sharedDataKey ?? DEFAULT_STORE_ID, options); - - this._db = null; - - if (IS_MAIN_PROCESS) { - if (lazy.NimbusEnrollments.databaseEnabled) { - // We may be in an xpcshell test that has not initialized the - // ProfilesDatastoreService. - // - // TODO(bug 1967779): require the ProfilesDatastoreService to be initialized - // and remove this check. - this._db = new lazy.NimbusEnrollments(this); - } - } } /** diff --git a/toolkit/components/nimbus/lib/SharedDataMap.sys.mjs b/toolkit/components/nimbus/lib/SharedDataMap.sys.mjs @@ -22,6 +22,7 @@ export class SharedDataMap extends EventEmitter { this._isReady = false; this._readyDeferred = Promise.withResolvers(); this._data = null; + this._db = null; if (IS_MAIN_PROCESS) { this._shutdownBlocker = () => { @@ -48,6 +49,15 @@ export class SharedDataMap extends EventEmitter { } return null; }); + + if (lazy.NimbusEnrollments.databaseEnabled) { + // We may be in an xpcshell test that has not initialized the + // ProfilesDatastoreService. + // + // TODO(bug 1967779): require the ProfilesDatastoreService to be initialized + // and remove this check. + this._db = new lazy.NimbusEnrollments(this); + } } else { this._syncFromParent(); Services.cpmm.sharedData.addEventListener("change", this); @@ -63,7 +73,7 @@ export class SharedDataMap extends EventEmitter { await this._jsonFile.load(); if (lazy.NimbusEnrollments.readFromDatabaseEnabled) { - this._data = await lazy.NimbusEnrollments.loadEnrollments(); + this._data = await this._db.init(); } else { this._data = this._jsonFile.data; }