diff options
Diffstat (limited to 'toolkit/components/normandy')
122 files changed, 24062 insertions, 0 deletions
diff --git a/toolkit/components/normandy/Normandy.sys.mjs b/toolkit/components/normandy/Normandy.sys.mjs new file mode 100644 index 0000000000..653461df48 --- /dev/null +++ b/toolkit/components/normandy/Normandy.sys.mjs @@ -0,0 +1,280 @@ +/* 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 { 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", +}); + +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: Promise.withResolvers(), + uiAvailableNotificationObserved: Promise.withResolvers(), + + /** 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); + + // 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(); + } + }, + + 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 + ); + + try { + Services.obs.removeObserver(this, UI_AVAILABLE_NOTIFICATION); + } 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; + }, +}; diff --git a/toolkit/components/normandy/NormandyMigrations.sys.mjs b/toolkit/components/normandy/NormandyMigrations.sys.mjs new file mode 100644 index 0000000000..389102e144 --- /dev/null +++ b/toolkit/components/normandy/NormandyMigrations.sys.mjs @@ -0,0 +1,139 @@ +/* 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 { AddonStudies } from "resource://normandy/lib/AddonStudies.sys.mjs"; +import { PreferenceExperiments } from "resource://normandy/lib/PreferenceExperiments.sys.mjs"; +import { RecipeRunner } from "resource://normandy/lib/RecipeRunner.sys.mjs"; + +const BOOTSTRAP_LOGGER_NAME = "app.normandy.bootstrap"; + +const PREF_PREFIX = "app.normandy"; +const LEGACY_PREF_PREFIX = "extensions.shield-recipe-client"; +const PREF_LOGGING_LEVEL = "app.normandy.logging.level"; +const PREF_MIGRATIONS_APPLIED = "app.normandy.migrationsApplied"; +const PREF_OPTOUTSTUDIES_ENABLED = "app.shield.optoutstudies.enabled"; + +// 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 const NormandyMigrations = { + async applyAll() { + let migrationsApplied = Services.prefs.getIntPref( + PREF_MIGRATIONS_APPLIED, + 0 + ); + + for (let i = migrationsApplied; i < this.migrations.length; i++) { + await this.applyOne(i); + migrationsApplied++; + Services.prefs.setIntPref(PREF_MIGRATIONS_APPLIED, migrationsApplied); + } + }, + + async applyOne(id) { + const migration = this.migrations[id]; + log.debug(`Running Normandy migration ${migration.name}`); + await migration(); + }, + + migrations: [ + migrateShieldPrefs, + migrateStudiesEnabledWithoutHealthReporting, + AddonStudies.migrations + .migration01AddonStudyFieldsToSlugAndUserFacingFields, + PreferenceExperiments.migrations.migration01MoveExperiments, + PreferenceExperiments.migrations.migration02MultiPreference, + PreferenceExperiments.migrations.migration03AddActionName, + PreferenceExperiments.migrations.migration04RenameNameToSlug, + RecipeRunner.migrations.migration01RemoveOldRecipesCollection, + AddonStudies.migrations.migration02RemoveOldAddonStudyAction, + migrateRemoveLastBuildIdPref, + PreferenceExperiments.migrations.migration05RemoveOldAction, + PreferenceExperiments.migrations.migration06TrackOverriddenPrefs, + ], +}; + +function migrateShieldPrefs() { + const legacyBranch = Services.prefs.getBranch(LEGACY_PREF_PREFIX + "."); + const newBranch = Services.prefs.getBranch(PREF_PREFIX + "."); + + for (const prefName of legacyBranch.getChildList("")) { + const legacyPrefType = legacyBranch.getPrefType(prefName); + const newPrefType = newBranch.getPrefType(prefName); + + // If new preference exists and is not the same as the legacy pref, skip it + if ( + newPrefType !== Services.prefs.PREF_INVALID && + newPrefType !== legacyPrefType + ) { + log.error( + `Error migrating normandy pref ${prefName}; pref type does not match.` + ); + continue; + } + + // Now move the value over. If it matches the default, this will be a no-op + switch (legacyPrefType) { + case Services.prefs.PREF_STRING: + newBranch.setCharPref(prefName, legacyBranch.getCharPref(prefName)); + break; + + case Services.prefs.PREF_INT: + newBranch.setIntPref(prefName, legacyBranch.getIntPref(prefName)); + break; + + case Services.prefs.PREF_BOOL: + newBranch.setBoolPref(prefName, legacyBranch.getBoolPref(prefName)); + break; + + case Services.prefs.PREF_INVALID: + // This should never happen. + log.error( + `Error migrating pref ${prefName}; pref type is invalid (${legacyPrefType}).` + ); + break; + + default: + // This should never happen either. + log.error( + `Error getting startup pref ${prefName}; unknown value type ${legacyPrefType}.` + ); + } + + legacyBranch.clearUserPref(prefName); + } +} + +/** + * Migration to handle moving the studies opt-out pref from under the health + * report upload pref to an independent pref. + * + * If the pref was set to true and the health report upload pref was set + * to true then the pref should stay true. Otherwise set it to false. + */ +function migrateStudiesEnabledWithoutHealthReporting() { + const optOutStudiesEnabled = Services.prefs.getBoolPref( + PREF_OPTOUTSTUDIES_ENABLED, + false + ); + const healthReportUploadEnabled = Services.prefs.getBoolPref( + "datareporting.healthreport.uploadEnabled", + false + ); + Services.prefs.setBoolPref( + PREF_OPTOUTSTUDIES_ENABLED, + optOutStudiesEnabled && healthReportUploadEnabled + ); +} + +/** + * Tracking last build ID is now done by comparing Services.appinfo.appBuildID + * and Services.appinfo.lastAppBuildID. Remove the manual tracking. + */ +function migrateRemoveLastBuildIdPref() { + Services.prefs.clearUserPref("app.normandy.last_seen_buildid"); +} diff --git a/toolkit/components/normandy/ShieldContentProcess.sys.mjs b/toolkit/components/normandy/ShieldContentProcess.sys.mjs new file mode 100644 index 0000000000..5406da63ab --- /dev/null +++ b/toolkit/components/normandy/ShieldContentProcess.sys.mjs @@ -0,0 +1,17 @@ +/* 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/. */ + +/** + * Registers about: pages provided by Shield, and listens for a shutdown event + * from the add-on before un-registering them. + * + * This file is loaded as a process script. It is executed once for each + * process, including the parent one. + */ + +import { AboutPages } from "resource://normandy-content/AboutPages.sys.mjs"; + +export function AboutStudies() { + return AboutPages.aboutStudies; +} diff --git a/toolkit/components/normandy/actions/AddonRollbackAction.sys.mjs b/toolkit/components/normandy/actions/AddonRollbackAction.sys.mjs new file mode 100644 index 0000000000..1176bff6d9 --- /dev/null +++ b/toolkit/components/normandy/actions/AddonRollbackAction.sys.mjs @@ -0,0 +1,82 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + AddonManager: "resource://gre/modules/AddonManager.sys.mjs", + AddonRollouts: "resource://normandy/lib/AddonRollouts.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +export class AddonRollbackAction extends BaseAction { + get schema() { + return lazy.ActionSchemas["addon-rollback"]; + } + + async _run(recipe) { + const { rolloutSlug } = recipe.arguments; + const rollout = await lazy.AddonRollouts.get(rolloutSlug); + + if (!rollout) { + this.log.debug(`Rollback ${rolloutSlug} not applicable, skipping`); + return; + } + + switch (rollout.state) { + case lazy.AddonRollouts.STATE_ACTIVE: { + await lazy.AddonRollouts.update({ + ...rollout, + state: lazy.AddonRollouts.STATE_ROLLED_BACK, + }); + + const addon = await lazy.AddonManager.getAddonByID(rollout.addonId); + if (addon) { + try { + await addon.uninstall(); + } catch (err) { + lazy.TelemetryEvents.sendEvent( + "unenrollFailed", + "addon_rollback", + rolloutSlug, + { + reason: "uninstall-failed", + } + ); + throw err; + } + } else { + this.log.warn( + `Could not uninstall addon ${rollout.addonId} for rollback ${rolloutSlug}: it is not installed.` + ); + } + + lazy.TelemetryEvents.sendEvent( + "unenroll", + "addon_rollback", + rolloutSlug, + { + reason: "rollback", + } + ); + lazy.TelemetryEnvironment.setExperimentInactive(rolloutSlug); + break; + } + + case lazy.AddonRollouts.STATE_ROLLED_BACK: { + return; // Do nothing + } + + default: { + throw new Error( + `Unexpected state when rolling back ${rolloutSlug}: ${rollout.state}` + ); + } + } + } +} diff --git a/toolkit/components/normandy/actions/AddonRolloutAction.sys.mjs b/toolkit/components/normandy/actions/AddonRolloutAction.sys.mjs new file mode 100644 index 0000000000..003bd31fff --- /dev/null +++ b/toolkit/components/normandy/actions/AddonRolloutAction.sys.mjs @@ -0,0 +1,219 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + AddonRollouts: "resource://normandy/lib/AddonRollouts.sys.mjs", + NormandyAddonManager: "resource://normandy/lib/NormandyAddonManager.sys.mjs", + NormandyApi: "resource://normandy/lib/NormandyApi.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +class AddonRolloutError extends Error { + /** + * @param {string} slug + * @param {object} extra Extra details to include when reporting the error to telemetry. + * @param {string} extra.reason The specific reason for the failure. + */ + constructor(slug, extra) { + let message; + let { reason } = extra; + switch (reason) { + case "conflict": { + message = "an existing rollout already exists for this add-on"; + break; + } + case "addon-id-changed": { + message = "upgrade add-on ID does not match installed add-on ID"; + break; + } + case "upgrade-required": { + message = "a newer version of the add-on is already installed"; + break; + } + case "download-failure": { + message = "the add-on failed to download"; + break; + } + case "metadata-mismatch": { + message = "the server metadata does not match the downloaded add-on"; + break; + } + case "install-failure": { + message = "the add-on failed to install"; + break; + } + default: { + throw new Error(`Unexpected AddonRolloutError reason: ${reason}`); + } + } + super(`Cannot install add-on for rollout (${slug}): ${message}.`); + this.slug = slug; + this.extra = extra; + } +} + +export class AddonRolloutAction extends BaseAction { + get schema() { + return lazy.ActionSchemas["addon-rollout"]; + } + + async _run(recipe) { + const { extensionApiId, slug } = recipe.arguments; + + const existingRollout = await lazy.AddonRollouts.get(slug); + const eventName = existingRollout ? "update" : "enroll"; + const extensionDetails = await lazy.NormandyApi.fetchExtensionDetails( + extensionApiId + ); + + // Check if the existing rollout matches the current rollout + if ( + existingRollout && + existingRollout.addonId === extensionDetails.extension_id + ) { + const versionCompare = Services.vc.compare( + existingRollout.addonVersion, + extensionDetails.version + ); + + if (versionCompare === 0) { + return; // Do nothing + } + } + + const createError = (reason, extra = {}) => { + return new AddonRolloutError(slug, { + ...extra, + reason, + }); + }; + + // Check for a conflict (addon already installed by another rollout) + const activeRollouts = await lazy.AddonRollouts.getAllActive(); + const conflictingRollout = activeRollouts.find( + rollout => + rollout.slug !== slug && + rollout.addonId === extensionDetails.extension_id + ); + if (conflictingRollout) { + const conflictError = createError("conflict", { + addonId: conflictingRollout.addonId, + conflictingSlug: conflictingRollout.slug, + }); + this.reportError(conflictError, "enrollFailed"); + throw conflictError; + } + + const onInstallStarted = (install, installDeferred) => { + const existingAddon = install.existingAddon; + + if (existingRollout && existingRollout.addonId !== install.addon.id) { + installDeferred.reject(createError("addon-id-changed")); + return false; // cancel the upgrade, the add-on ID has changed + } + + if ( + existingAddon && + Services.vc.compare(existingAddon.version, install.addon.version) > 0 + ) { + installDeferred.reject(createError("upgrade-required")); + return false; // cancel the installation, must be an upgrade + } + + return true; + }; + + const applyNormandyChanges = async install => { + const details = { + addonId: install.addon.id, + addonVersion: install.addon.version, + extensionApiId, + xpiUrl: extensionDetails.xpi, + xpiHash: extensionDetails.hash, + xpiHashAlgorithm: extensionDetails.hash_algorithm, + }; + + if (existingRollout) { + await lazy.AddonRollouts.update({ + ...existingRollout, + ...details, + }); + } else { + await lazy.AddonRollouts.add({ + recipeId: recipe.id, + state: lazy.AddonRollouts.STATE_ACTIVE, + slug, + ...details, + }); + } + }; + + const undoNormandyChanges = async () => { + if (existingRollout) { + await lazy.AddonRollouts.update(existingRollout); + } else { + await lazy.AddonRollouts.delete(recipe.id); + } + }; + + const [installedId, installedVersion] = + await lazy.NormandyAddonManager.downloadAndInstall({ + createError, + extensionDetails, + applyNormandyChanges, + undoNormandyChanges, + onInstallStarted, + reportError: error => this.reportError(error, `${eventName}Failed`), + }); + + if (existingRollout) { + this.log.debug(`Updated addon rollout ${slug}`); + } else { + this.log.debug(`Enrolled in addon rollout ${slug}`); + lazy.TelemetryEnvironment.setExperimentActive( + slug, + lazy.AddonRollouts.STATE_ACTIVE, + { + type: "normandy-addonrollout", + } + ); + } + + // All done, report success to Telemetry + lazy.TelemetryEvents.sendEvent(eventName, "addon_rollout", slug, { + addonId: installedId, + addonVersion: installedVersion, + }); + } + + reportError(error, eventName) { + if (error instanceof AddonRolloutError) { + // One of our known errors. Report it nicely to telemetry + lazy.TelemetryEvents.sendEvent( + eventName, + "addon_rollout", + error.slug, + error.extra + ); + } else { + /* + * Some unknown error. Add some helpful details, and report it to + * telemetry. The actual stack trace and error message could possibly + * contain PII, so we don't include them here. Instead include some + * information that should still be helpful, and is less likely to be + * unsafe. + */ + const safeErrorMessage = `${error.fileName}:${error.lineNumber}:${error.columnNumber} ${error.name}`; + lazy.TelemetryEvents.sendEvent(eventName, "addon_rollout", error.slug, { + reason: safeErrorMessage.slice(0, 80), // max length is 80 chars + }); + } + } +} diff --git a/toolkit/components/normandy/actions/BaseAction.sys.mjs b/toolkit/components/normandy/actions/BaseAction.sys.mjs new file mode 100644 index 0000000000..cb9198a55e --- /dev/null +++ b/toolkit/components/normandy/actions/BaseAction.sys.mjs @@ -0,0 +1,338 @@ +/* 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 { Uptake } from "resource://normandy/lib/Uptake.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + JsonSchemaValidator: + "resource://gre/modules/components-utils/JsonSchemaValidator.sys.mjs", + LogManager: "resource://normandy/lib/LogManager.sys.mjs", +}); + +/** + * Base class for local actions. + * + * This should be subclassed. Subclasses must implement _run() for + * per-recipe behavior, and may implement _preExecution and _finalize + * for actions to be taken once before and after recipes are run. + * + * Other methods should be overridden with care, to maintain the life + * cycle events and error reporting implemented by this class. + */ +export class BaseAction { + constructor() { + this.state = BaseAction.STATE_PREPARING; + this.log = lazy.LogManager.getLogger(`action.${this.name}`); + this.lastError = null; + } + + /** + * Be sure to run the _preExecution() hook once during its + * lifecycle. + * + * This is not intended for overriding by subclasses. + */ + _ensurePreExecution() { + if (this.state !== BaseAction.STATE_PREPARING) { + return; + } + + try { + this._preExecution(); + // if _preExecution changed the state, don't overwrite it + if (this.state === BaseAction.STATE_PREPARING) { + this.state = BaseAction.STATE_READY; + } + } catch (err) { + // Sometimes err.message is editable. If it is, add helpful details. + // Otherwise log the helpful details and move on. + try { + err.message = `Could not initialize action ${this.name}: ${err.message}`; + } catch (_e) { + this.log.error( + `Could not initialize action ${this.name}, error follows.` + ); + } + this.fail(err); + } + } + + get schema() { + return { + type: "object", + properties: {}, + }; + } + + /** + * Disable the action for a non-error reason, such as the user opting out of + * this type of action. + */ + disable() { + this.state = BaseAction.STATE_DISABLED; + } + + fail(err) { + switch (this.state) { + case BaseAction.STATE_PREPARING: { + Uptake.reportAction(this.name, Uptake.ACTION_PRE_EXECUTION_ERROR); + break; + } + default: { + console.error(new Error("BaseAction.fail() called at unexpected time")); + } + } + this.state = BaseAction.STATE_FAILED; + this.lastError = err; + console.error(err); + } + + // Gets the name of the action. Does not necessarily match the + // server slug for the action. + get name() { + return this.constructor.name; + } + + /** + * Action specific pre-execution behavior should be implemented + * here. It will be called once per execution session. + */ + _preExecution() { + // Does nothing, may be overridden + } + + validateArguments(args, schema = this.schema) { + let { valid, parsedValue: validated } = lazy.JsonSchemaValidator.validate( + args, + schema, + { + allowAdditionalProperties: true, + } + ); + if (!valid) { + throw new Error( + `Arguments do not match schema. arguments:\n${JSON.stringify(args)}\n` + + `schema:\n${JSON.stringify(schema)}` + ); + } + return validated; + } + + /** + * Execute the per-recipe behavior of this action for a given + * recipe. Reports Uptake telemetry for the execution of the recipe. + * + * @param {Recipe} recipe + * @param {BaseAction.suitability} suitability + * @throws If this action has already been finalized. + */ + async processRecipe(recipe, suitability) { + if (!BaseAction.suitabilitySet.has(suitability)) { + throw new Error(`Unknown recipe status ${suitability}`); + } + + this._ensurePreExecution(); + + if (this.state === BaseAction.STATE_FINALIZED) { + throw new Error("Action has already been finalized"); + } + + if (this.state !== BaseAction.STATE_READY) { + Uptake.reportRecipe(recipe, Uptake.RECIPE_ACTION_DISABLED); + this.log.warn( + `Skipping recipe ${recipe.name} because ${this.name} was disabled during preExecution.` + ); + return; + } + + let uptakeResult = BaseAction.suitabilityToUptakeStatus[suitability]; + if (!uptakeResult) { + throw new Error( + `Coding error, no uptake status for suitability ${suitability}` + ); + } + + // If capabilties don't match, we can't even be sure that the arguments + // should be valid. In that case don't try to validate them. + if (suitability !== BaseAction.suitability.CAPABILITIES_MISMATCH) { + try { + recipe.arguments = this.validateArguments(recipe.arguments); + } catch (error) { + console.error(error); + uptakeResult = Uptake.RECIPE_EXECUTION_ERROR; + suitability = BaseAction.suitability.ARGUMENTS_INVALID; + } + } + + try { + await this._processRecipe(recipe, suitability); + } catch (err) { + console.error(err); + uptakeResult = Uptake.RECIPE_EXECUTION_ERROR; + } + Uptake.reportRecipe(recipe, uptakeResult); + } + + /** + * Action specific recipe behavior may be implemented here. It will be + * executed once for each recipe that applies to this client. + * The recipe will be passed as a parameter. + * + * @param {Recipe} recipe + */ + async _run(recipe) { + throw new Error("Not implemented"); + } + + /** + * Action specific recipe behavior should be implemented here. It will be + * executed once for every recipe currently published. The suitability of the + * recipe will be passed, it will be one of the constants from + * `BaseAction.suitability`. + * + * By default, this calls `_run()` for recipes with `status == FILTER_MATCH`, + * and does nothing for all other recipes. It is invalid for an action to + * override both `_run` and `_processRecipe`. + * + * @param {Recipe} recipe + * @param {RecipeSuitability} suitability + */ + async _processRecipe(recipe, suitability) { + if (!suitability) { + throw new Error("Suitability is undefined:", suitability); + } + if (suitability == BaseAction.suitability.FILTER_MATCH) { + await this._run(recipe); + } + } + + /** + * Finish an execution session. After this method is called, no + * other methods may be called on this method, and all relevant + * recipes will be assumed to have been seen. + */ + async finalize(options) { + // It's possible that no recipes used this action, so processRecipe() + // was never called. In that case, we should ensure that we call + // _preExecute() here. + this._ensurePreExecution(); + + let status; + switch (this.state) { + case BaseAction.STATE_FINALIZED: { + throw new Error("Action has already been finalized"); + } + case BaseAction.STATE_READY: { + try { + await this._finalize(options); + status = Uptake.ACTION_SUCCESS; + } catch (err) { + status = Uptake.ACTION_POST_EXECUTION_ERROR; + // Sometimes Error.message can be updated in place. This gives better messages when debugging errors. + try { + err.message = `Could not run postExecution hook for ${this.name}: ${err.message}`; + } catch (err) { + // Sometimes Error.message cannot be updated. Log a warning, and move on. + this.log.debug(`Could not run postExecution hook for ${this.name}`); + } + + this.lastError = err; + console.error(err); + } + break; + } + case BaseAction.STATE_DISABLED: { + this.log.debug( + `Skipping post-execution hook for ${this.name} because it is disabled.` + ); + status = Uptake.ACTION_SUCCESS; + break; + } + case BaseAction.STATE_FAILED: { + this.log.debug( + `Skipping post-execution hook for ${this.name} because it failed during pre-execution.` + ); + // Don't report a status. A status should have already been reported by this.fail(). + break; + } + default: { + throw new Error(`Unexpected state during finalize: ${this.state}`); + } + } + + this.state = BaseAction.STATE_FINALIZED; + if (status) { + Uptake.reportAction(this.name, status); + } + } + + /** + * Action specific post-execution behavior should be implemented + * here. It will be executed once after all recipes have been + * processed. + */ + async _finalize(_options = {}) { + // Does nothing, may be overridden + } +} + +BaseAction.STATE_PREPARING = "ACTION_PREPARING"; +BaseAction.STATE_READY = "ACTION_READY"; +BaseAction.STATE_DISABLED = "ACTION_DISABLED"; +BaseAction.STATE_FAILED = "ACTION_FAILED"; +BaseAction.STATE_FINALIZED = "ACTION_FINALIZED"; + +// Make sure to update the docs in ../docs/suitabilities.rst when changing this. +BaseAction.suitability = { + /** + * The recipe's signature is not valid. If any action is taken this recipe + * should be treated with extreme suspicion. + */ + SIGNATURE_ERROR: "RECIPE_SUITABILITY_SIGNATURE_ERROR", + + /** + * The recipe requires capabilities that this recipe runner does not have. + * Use caution when interacting with this recipe, as it may not match the + * expected schema. + */ + CAPABILITIES_MISMATCH: "RECIPE_SUITABILITY_CAPABILITIES_MISMATCH", + + /** + * The recipe is suitable to execute in this client. + */ + FILTER_MATCH: "RECIPE_SUITABILITY_FILTER_MATCH", + + /** + * This client does not match the recipe's filter, but it is otherwise a + * suitable recipe. + */ + FILTER_MISMATCH: "RECIPE_SUITABILITY_FILTER_MISMATCH", + + /** + * There was an error while evaluating the filter. It is unknown if this + * client matches this filter. This may be temporary, due to network errors, + * or permanent due to syntax errors. + */ + FILTER_ERROR: "RECIPE_SUITABILITY_FILTER_ERROR", + + /** + * The arguments of the recipe do not match the expected schema for the named + * action. + */ + ARGUMENTS_INVALID: "RECIPE_SUITABILITY_ARGUMENTS_INVALID", +}; + +BaseAction.suitabilitySet = new Set(Object.values(BaseAction.suitability)); + +BaseAction.suitabilityToUptakeStatus = { + [BaseAction.suitability.SIGNATURE_ERROR]: Uptake.RECIPE_INVALID_SIGNATURE, + [BaseAction.suitability.CAPABILITIES_MISMATCH]: + Uptake.RECIPE_INCOMPATIBLE_CAPABILITIES, + [BaseAction.suitability.FILTER_MATCH]: Uptake.RECIPE_SUCCESS, + [BaseAction.suitability.FILTER_MISMATCH]: Uptake.RECIPE_DIDNT_MATCH_FILTER, + [BaseAction.suitability.FILTER_ERROR]: Uptake.RECIPE_FILTER_BROKEN, + [BaseAction.suitability.ARGUMENTS_INVALID]: Uptake.RECIPE_ARGUMENTS_INVALID, +}; diff --git a/toolkit/components/normandy/actions/BaseStudyAction.sys.mjs b/toolkit/components/normandy/actions/BaseStudyAction.sys.mjs new file mode 100644 index 0000000000..cca66e65fa --- /dev/null +++ b/toolkit/components/normandy/actions/BaseStudyAction.sys.mjs @@ -0,0 +1,37 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const OPT_OUT_STUDIES_ENABLED_PREF = "app.shield.optoutstudies.enabled"; + +/** + * Base class for local study actions. + * + * This should be subclassed. Subclasses must implement _run() for + * per-recipe behavior, and may implement _finalize for actions to be + * taken once after recipes are run. + * + * For actions that need to be taken once before recipes are run + * _preExecution may be overriden but the overridden method must + * call the parent method to ensure the appropriate checks occur. + * + * Other methods should be overridden with care, to maintain the life + * cycle events and error reporting implemented by this class. + */ +export class BaseStudyAction extends BaseAction { + _preExecution() { + if (!Services.policies.isAllowed("Shield")) { + this.log.debug("Disabling Shield because it's blocked by policy."); + this.disable(); + } + + if (!Services.prefs.getBoolPref(OPT_OUT_STUDIES_ENABLED_PREF, true)) { + this.log.debug( + "User has opted-out of opt-out experiments, disabling action." + ); + this.disable(); + } + } +} diff --git a/toolkit/components/normandy/actions/BranchedAddonStudyAction.sys.mjs b/toolkit/components/normandy/actions/BranchedAddonStudyAction.sys.mjs new file mode 100644 index 0000000000..d775429ced --- /dev/null +++ b/toolkit/components/normandy/actions/BranchedAddonStudyAction.sys.mjs @@ -0,0 +1,762 @@ +/* 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/. */ + +/* + * This action handles the life cycle of add-on based studies. Currently that + * means installing the add-on the first time the recipe applies to this + * client, updating the add-on to new versions if the recipe changes, and + * uninstalling them when the recipe no longer applies. + */ + +import { BaseStudyAction } from "resource://normandy/actions/BaseStudyAction.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + AddonManager: "resource://gre/modules/AddonManager.sys.mjs", + AddonStudies: "resource://normandy/lib/AddonStudies.sys.mjs", + BaseAction: "resource://normandy/actions/BaseAction.sys.mjs", + ClientEnvironment: "resource://normandy/lib/ClientEnvironment.sys.mjs", + NormandyApi: "resource://normandy/lib/NormandyApi.sys.mjs", + Sampling: "resource://gre/modules/components-utils/Sampling.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +class AddonStudyEnrollError extends Error { + /** + * @param {string} studyName + * @param {object} extra Extra details to include when reporting the error to telemetry. + * @param {string} extra.reason The specific reason for the failure. + */ + constructor(studyName, extra) { + let message; + let { reason } = extra; + switch (reason) { + case "conflicting-addon-id": { + message = "an add-on with this ID is already installed"; + break; + } + case "download-failure": { + message = "the add-on failed to download"; + break; + } + case "metadata-mismatch": { + message = "the server metadata does not match the downloaded add-on"; + break; + } + case "install-failure": { + message = "the add-on failed to install"; + break; + } + default: { + throw new Error(`Unexpected AddonStudyEnrollError reason: ${reason}`); + } + } + super(`Cannot install study add-on for ${studyName}: ${message}.`); + this.studyName = studyName; + this.extra = extra; + } +} + +class AddonStudyUpdateError extends Error { + /** + * @param {string} studyName + * @param {object} extra Extra details to include when reporting the error to telemetry. + * @param {string} extra.reason The specific reason for the failure. + */ + constructor(studyName, extra) { + let message; + let { reason } = extra; + switch (reason) { + case "addon-id-mismatch": { + message = "new add-on ID does not match old add-on ID"; + break; + } + case "addon-does-not-exist": { + message = "an add-on with this ID does not exist"; + break; + } + case "no-downgrade": { + message = "the add-on was an older version than is installed"; + break; + } + case "metadata-mismatch": { + message = "the server metadata does not match the downloaded add-on"; + break; + } + case "download-failure": { + message = "the add-on failed to download"; + break; + } + case "install-failure": { + message = "the add-on failed to install"; + break; + } + default: { + throw new Error(`Unexpected AddonStudyUpdateError reason: ${reason}`); + } + } + super(`Cannot update study add-on for ${studyName}: ${message}.`); + this.studyName = studyName; + this.extra = extra; + } +} + +export class BranchedAddonStudyAction extends BaseStudyAction { + get schema() { + return lazy.ActionSchemas["branched-addon-study"]; + } + + constructor() { + super(); + this.seenRecipeIds = new Set(); + } + + async _run(recipe) { + throw new Error("_run should not be called anymore"); + } + + /** + * This hook is executed once for every recipe currently enabled on the + * server. It is responsible for: + * + * - Enrolling studies the first time they have a FILTER_MATCH suitability. + * - Updating studies that have changed and still have a FILTER_MATCH suitability. + * - Marking studies as having been seen in this session. + * - Unenrolling studies when they have permanent errors. + * - Unenrolling studies when temporary errors persist for too long. + * + * If the action fails to perform any of these tasks, it should throw to + * properly report its status. + */ + async _processRecipe(recipe, suitability) { + this.seenRecipeIds.add(recipe.id); + const study = await lazy.AddonStudies.get(recipe.id); + + switch (suitability) { + case lazy.BaseAction.suitability.FILTER_MATCH: { + if (!study) { + await this.enroll(recipe); + } else if (study.active) { + await this.update(recipe, study); + } + break; + } + + case lazy.BaseAction.suitability.SIGNATURE_ERROR: { + await this._considerTemporaryError({ + study, + reason: "signature-error", + }); + break; + } + + case lazy.BaseAction.suitability.FILTER_ERROR: { + await this._considerTemporaryError({ + study, + reason: "filter-error", + }); + break; + } + + case lazy.BaseAction.suitability.CAPABILITIES_MISMATCH: { + if (study?.active) { + await this.unenroll(recipe.id, "capability-mismatch"); + } + break; + } + + case lazy.BaseAction.suitability.FILTER_MISMATCH: { + if (study?.active) { + await this.unenroll(recipe.id, "filter-mismatch"); + } + break; + } + + case lazy.BaseAction.suitability.ARGUMENTS_INVALID: { + if (study?.active) { + await this.unenroll(recipe.id, "arguments-invalid"); + } + break; + } + + default: { + throw new Error(`Unknown recipe suitability "${suitability}".`); + } + } + } + + /** + * This hook is executed once after all recipes that apply to this client + * have been processed. It is responsible for unenrolling the client from any + * studies that no longer apply, based on this.seenRecipeIds. + */ + async _finalize({ noRecipes } = {}) { + const activeStudies = await lazy.AddonStudies.getAllActive({ + branched: lazy.AddonStudies.FILTER_BRANCHED_ONLY, + }); + + if (noRecipes) { + if (this.seenRecipeIds.size) { + throw new BranchedAddonStudyAction.BadNoRecipesArg(); + } + for (const study of activeStudies) { + await this._considerTemporaryError({ study, reason: "no-recipes" }); + } + } else { + for (const study of activeStudies) { + if (!this.seenRecipeIds.has(study.recipeId)) { + this.log.debug( + `Stopping branched add-on study for recipe ${study.recipeId}` + ); + try { + await this.unenroll(study.recipeId, "recipe-not-seen"); + } catch (err) { + console.error(err); + } + } + } + } + } + + /** + * Download and install the addon for a given recipe + * + * @param recipe Object describing the study to enroll in. + * @param extensionDetails Object describing the addon to be installed. + * @param onInstallStarted A function that returns a callback for the install listener. + * @param onComplete A callback function that is run on completion of the download. + * @param onFailedInstall A callback function that is run if the installation fails. + * @param errorClass The class of error to be thrown when exceptions occur. + * @param reportError A function that reports errors to Telemetry. + * @param [errorExtra] Optional, an object that will be merged into the + * `extra` field of the error generated, if any. + */ + async downloadAndInstall({ + recipe, + extensionDetails, + branchSlug, + onInstallStarted, + onComplete, + onFailedInstall, + errorClass, + reportError, + errorExtra = {}, + }) { + const { slug } = recipe.arguments; + const { hash, hash_algorithm } = extensionDetails; + + const downloadDeferred = Promise.withResolvers(); + const installDeferred = Promise.withResolvers(); + + const install = await lazy.AddonManager.getInstallForURL( + extensionDetails.xpi, + { + hash: `${hash_algorithm}:${hash}`, + telemetryInfo: { source: "internal" }, + } + ); + + const listener = { + onDownloadFailed() { + downloadDeferred.reject( + new errorClass(slug, { + reason: "download-failure", + branch: branchSlug, + detail: lazy.AddonManager.errorToString(install.error), + ...errorExtra, + }) + ); + }, + + onDownloadEnded() { + downloadDeferred.resolve(); + return false; // temporarily pause installation for Normandy bookkeeping + }, + + onInstallFailed() { + installDeferred.reject( + new errorClass(slug, { + reason: "install-failure", + branch: branchSlug, + detail: lazy.AddonManager.errorToString(install.error), + }) + ); + }, + + onInstallEnded() { + installDeferred.resolve(); + }, + }; + + listener.onInstallStarted = onInstallStarted(installDeferred); + + install.addListener(listener); + + // Download the add-on + try { + install.install(); + await downloadDeferred.promise; + } catch (err) { + reportError(err); + install.removeListener(listener); + throw err; + } + + await onComplete(install, listener); + + // Finish paused installation + try { + install.install(); + await installDeferred.promise; + } catch (err) { + reportError(err); + install.removeListener(listener); + await onFailedInstall(); + throw err; + } + + install.removeListener(listener); + + return [install.addon.id, install.addon.version]; + } + + async chooseBranch({ slug, branches }) { + const ratios = branches.map(branch => branch.ratio); + const userId = lazy.ClientEnvironment.userId; + + // It's important that the input be: + // - Unique per-user (no one is bucketed alike) + // - Unique per-experiment (bucketing differs across multiple experiments) + // - Differs from the input used for sampling the recipe (otherwise only + // branches that contain the same buckets as the recipe sampling will + // receive users) + const input = `${userId}-${slug}-addon-branch`; + + const index = await lazy.Sampling.ratioSample(input, ratios); + return branches[index]; + } + + /** + * Enroll in the study represented by the given recipe. + * @param recipe Object describing the study to enroll in. + * @param extensionDetails Object describing the addon to be installed. + */ + async enroll(recipe) { + // This function first downloads the add-on to get its metadata. Then it + // uses that metadata to record a study in `AddonStudies`. Then, it finishes + // installing the add-on, and finally sends telemetry. If any of these steps + // fails, the previous ones are undone, as needed. + // + // This ordering is important because the only intermediate states we can be + // in are: + // 1. The add-on is only downloaded, in which case AddonManager will clean it up. + // 2. The study has been recorded, in which case we will unenroll on next + // start up. The start up code will assume that the add-on was uninstalled + // while the browser was shutdown. + // 3. After installation is complete, but before telemetry, in which case we + // lose an enroll event. This is acceptable. + // + // This way a shutdown, crash or unexpected error can't leave Normandy in a + // long term inconsistent state. The main thing avoided is having a study + // add-on installed but no record of it, which would leave it permanently + // installed. + + if (recipe.arguments.isEnrollmentPaused) { + // Recipe does not need anything done + return; + } + + const { slug, userFacingName, userFacingDescription } = recipe.arguments; + const branch = await this.chooseBranch({ + slug: recipe.arguments.slug, + branches: recipe.arguments.branches, + }); + this.log.debug(`Enrolling in branch ${branch.slug}`); + + if (branch.extensionApiId === null) { + const study = { + recipeId: recipe.id, + slug, + userFacingName, + userFacingDescription, + branch: branch.slug, + addonId: null, + addonVersion: null, + addonUrl: null, + extensionApiId: null, + extensionHash: null, + extensionHashAlgorithm: null, + active: true, + studyStartDate: new Date(), + studyEndDate: null, + temporaryErrorDeadline: null, + }; + + try { + await lazy.AddonStudies.add(study); + } catch (err) { + this.reportEnrollError(err); + throw err; + } + + // All done, report success to Telemetry + lazy.TelemetryEvents.sendEvent("enroll", "addon_study", slug, { + addonId: lazy.AddonStudies.NO_ADDON_MARKER, + addonVersion: lazy.AddonStudies.NO_ADDON_MARKER, + branch: branch.slug, + }); + } else { + const extensionDetails = await lazy.NormandyApi.fetchExtensionDetails( + branch.extensionApiId + ); + + const onInstallStarted = installDeferred => cbInstall => { + const versionMatches = + cbInstall.addon.version === extensionDetails.version; + const idMatches = cbInstall.addon.id === extensionDetails.extension_id; + + if (cbInstall.existingAddon) { + installDeferred.reject( + new AddonStudyEnrollError(slug, { + reason: "conflicting-addon-id", + branch: branch.slug, + }) + ); + return false; // cancel the installation, no upgrades allowed + } else if (!versionMatches || !idMatches) { + installDeferred.reject( + new AddonStudyEnrollError(slug, { + branch: branch.slug, + reason: "metadata-mismatch", + }) + ); + return false; // cancel the installation, server metadata does not match downloaded add-on + } + return true; + }; + + let study; + const onComplete = async (install, listener) => { + study = { + recipeId: recipe.id, + slug, + userFacingName, + userFacingDescription, + branch: branch.slug, + addonId: install.addon.id, + addonVersion: install.addon.version, + addonUrl: extensionDetails.xpi, + extensionApiId: branch.extensionApiId, + extensionHash: extensionDetails.hash, + extensionHashAlgorithm: extensionDetails.hash_algorithm, + active: true, + studyStartDate: new Date(), + studyEndDate: null, + temporaryErrorDeadline: null, + }; + + try { + await lazy.AddonStudies.add(study); + } catch (err) { + this.reportEnrollError(err); + install.removeListener(listener); + install.cancel(); + throw err; + } + }; + + const onFailedInstall = async () => { + await lazy.AddonStudies.delete(recipe.id); + }; + + const [installedId, installedVersion] = await this.downloadAndInstall({ + recipe, + branchSlug: branch.slug, + extensionDetails, + onInstallStarted, + onComplete, + onFailedInstall, + errorClass: AddonStudyEnrollError, + reportError: this.reportEnrollError, + }); + + // All done, report success to Telemetry + lazy.TelemetryEvents.sendEvent("enroll", "addon_study", slug, { + addonId: installedId, + addonVersion: installedVersion, + branch: branch.slug, + }); + } + + lazy.TelemetryEnvironment.setExperimentActive(slug, branch.slug, { + type: "normandy-addonstudy", + }); + } + + /** + * Update the study represented by the given recipe. + * @param recipe Object describing the study to be updated. + * @param extensionDetails Object describing the addon to be installed. + */ + async update(recipe, study) { + const { slug } = recipe.arguments; + + // Stay in the same branch, don't re-sample every time. + const branch = recipe.arguments.branches.find( + branch => branch.slug === study.branch + ); + + if (!branch) { + // Our branch has been removed. Unenroll. + await this.unenroll(recipe.id, "branch-removed"); + return; + } + + // Since we saw a non-error suitability, clear the temporary error deadline. + study.temporaryErrorDeadline = null; + await lazy.AddonStudies.update(study); + + const extensionDetails = await lazy.NormandyApi.fetchExtensionDetails( + branch.extensionApiId + ); + + let error; + + if (study.addonId && study.addonId !== extensionDetails.extension_id) { + error = new AddonStudyUpdateError(slug, { + branch: branch.slug, + reason: "addon-id-mismatch", + }); + } + + const versionCompare = Services.vc.compare( + study.addonVersion, + extensionDetails.version + ); + if (versionCompare > 0) { + error = new AddonStudyUpdateError(slug, { + branch: branch.slug, + reason: "no-downgrade", + }); + } else if (versionCompare === 0) { + return; // Unchanged, do nothing + } + + if (error) { + this.reportUpdateError(error); + throw error; + } + + const onInstallStarted = installDeferred => cbInstall => { + const versionMatches = + cbInstall.addon.version === extensionDetails.version; + const idMatches = cbInstall.addon.id === extensionDetails.extension_id; + + if (!cbInstall.existingAddon) { + installDeferred.reject( + new AddonStudyUpdateError(slug, { + branch: branch.slug, + reason: "addon-does-not-exist", + }) + ); + return false; // cancel the installation, must upgrade an existing add-on + } else if (!versionMatches || !idMatches) { + installDeferred.reject( + new AddonStudyUpdateError(slug, { + branch: branch.slug, + reason: "metadata-mismatch", + }) + ); + return false; // cancel the installation, server metadata do not match downloaded add-on + } + + return true; + }; + + const onComplete = async (install, listener) => { + try { + await lazy.AddonStudies.update({ + ...study, + addonVersion: install.addon.version, + addonUrl: extensionDetails.xpi, + extensionHash: extensionDetails.hash, + extensionHashAlgorithm: extensionDetails.hash_algorithm, + extensionApiId: branch.extensionApiId, + }); + } catch (err) { + this.reportUpdateError(err); + install.removeListener(listener); + install.cancel(); + throw err; + } + }; + + const onFailedInstall = () => { + lazy.AddonStudies.update(study); + }; + + const [installedId, installedVersion] = await this.downloadAndInstall({ + recipe, + extensionDetails, + branchSlug: branch.slug, + onInstallStarted, + onComplete, + onFailedInstall, + errorClass: AddonStudyUpdateError, + reportError: this.reportUpdateError, + errorExtra: {}, + }); + + // All done, report success to Telemetry + lazy.TelemetryEvents.sendEvent("update", "addon_study", slug, { + addonId: installedId, + addonVersion: installedVersion, + branch: branch.slug, + }); + } + + reportEnrollError(error) { + if (error instanceof AddonStudyEnrollError) { + // One of our known errors. Report it nicely to telemetry + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "addon_study", + error.studyName, + error.extra + ); + } else { + /* + * Some unknown error. Add some helpful details, and report it to + * telemetry. The actual stack trace and error message could possibly + * contain PII, so we don't include them here. Instead include some + * information that should still be helpful, and is less likely to be + * unsafe. + */ + const safeErrorMessage = `${error.fileName}:${error.lineNumber}:${error.columnNumber} ${error.name}`; + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "addon_study", + error.studyName, + { + reason: safeErrorMessage.slice(0, 80), // max length is 80 chars + } + ); + } + } + + reportUpdateError(error) { + if (error instanceof AddonStudyUpdateError) { + // One of our known errors. Report it nicely to telemetry + lazy.TelemetryEvents.sendEvent( + "updateFailed", + "addon_study", + error.studyName, + error.extra + ); + } else { + /* + * Some unknown error. Add some helpful details, and report it to + * telemetry. The actual stack trace and error message could possibly + * contain PII, so we don't include them here. Instead include some + * information that should still be helpful, and is less likely to be + * unsafe. + */ + const safeErrorMessage = `${error.fileName}:${error.lineNumber}:${error.columnNumber} ${error.name}`; + lazy.TelemetryEvents.sendEvent( + "updateFailed", + "addon_study", + error.studyName, + { + reason: safeErrorMessage.slice(0, 80), // max length is 80 chars + } + ); + } + } + + /** + * Unenrolls the client from the study with a given recipe ID. + * @param recipeId The recipe ID of an enrolled study + * @param reason The reason for this unenrollment, to be used in Telemetry + * @throws If the specified study does not exist, or if it is already inactive. + */ + async unenroll(recipeId, reason = "unknown") { + const study = await lazy.AddonStudies.get(recipeId); + if (!study) { + throw new Error(`No study found for recipe ${recipeId}.`); + } + if (!study.active) { + throw new Error( + `Cannot stop study for recipe ${recipeId}; it is already inactive.` + ); + } + + await lazy.AddonStudies.markAsEnded(study, reason); + + // Study branches may indicate that no add-on should be installed, as a + // form of control branch. In that case, `study.addonId` will be null (as + // will the other add-on related fields). Only try to uninstall the add-on + // if we expect one should be installed. + if (study.addonId) { + const addon = await lazy.AddonManager.getAddonByID(study.addonId); + if (addon) { + await addon.uninstall(); + } else { + this.log.warn( + `Could not uninstall addon ${study.addonId} for recipe ${study.recipeId}: it is not installed.` + ); + } + } + } + + /** + * Given that a temporary error has occured for a study, check if it + * should be temporarily ignored, or if the deadline has passed. If the + * deadline is passed, the study will be ended. If this is the first + * temporary error, a deadline will be generated. Otherwise, nothing will + * happen. + * + * If a temporary deadline exists but cannot be parsed, a new one will be + * made. + * + * The deadline is 7 days from the first time that recipe failed, as + * reckoned by the client's clock. + * + * @param {Object} args + * @param {Study} args.study The enrolled study to potentially unenroll. + * @param {String} args.reason If the study should end, the reason it is ending. + */ + async _considerTemporaryError({ study, reason }) { + if (!study?.active) { + return; + } + + let now = Date.now(); // milliseconds-since-epoch + let day = 24 * 60 * 60 * 1000; + let newDeadline = new Date(now + 7 * day); + + if (study.temporaryErrorDeadline) { + // if deadline is an invalid date, set it to one week from now. + if (isNaN(study.temporaryErrorDeadline)) { + study.temporaryErrorDeadline = newDeadline; + await lazy.AddonStudies.update(study); + return; + } + + if (now > study.temporaryErrorDeadline) { + await this.unenroll(study.recipeId, reason); + } + } else { + // there is no deadline, so set one + study.temporaryErrorDeadline = newDeadline; + await lazy.AddonStudies.update(study); + } + } +} + +BranchedAddonStudyAction.BadNoRecipesArg = class extends Error { + message = "noRecipes is true, but some recipes observed"; +}; diff --git a/toolkit/components/normandy/actions/ConsoleLogAction.sys.mjs b/toolkit/components/normandy/actions/ConsoleLogAction.sys.mjs new file mode 100644 index 0000000000..a0b9f859fa --- /dev/null +++ b/toolkit/components/normandy/actions/ConsoleLogAction.sys.mjs @@ -0,0 +1,20 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", +}); + +export class ConsoleLogAction extends BaseAction { + get schema() { + return lazy.ActionSchemas["console-log"]; + } + + async _run(recipe) { + this.log.info(recipe.arguments.message); + } +} diff --git a/toolkit/components/normandy/actions/MessagingExperimentAction.sys.mjs b/toolkit/components/normandy/actions/MessagingExperimentAction.sys.mjs new file mode 100644 index 0000000000..393d2cc3f2 --- /dev/null +++ b/toolkit/components/normandy/actions/MessagingExperimentAction.sys.mjs @@ -0,0 +1,34 @@ +/* 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 { BaseStudyAction } from "resource://normandy/actions/BaseStudyAction.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + ExperimentManager: "resource://nimbus/lib/ExperimentManager.sys.mjs", +}); + +const RECIPE_SOURCE = "normandy"; + +export class MessagingExperimentAction extends BaseStudyAction { + constructor() { + super(); + this.manager = lazy.ExperimentManager; + } + get schema() { + return lazy.ActionSchemas["messaging-experiment"]; + } + + async _run(recipe) { + if (recipe.arguments) { + await this.manager.onRecipe(recipe.arguments, RECIPE_SOURCE); + } + } + + async _finalize() { + this.manager.onFinalize(RECIPE_SOURCE); + } +} diff --git a/toolkit/components/normandy/actions/PreferenceExperimentAction.sys.mjs b/toolkit/components/normandy/actions/PreferenceExperimentAction.sys.mjs new file mode 100644 index 0000000000..310d1b08fd --- /dev/null +++ b/toolkit/components/normandy/actions/PreferenceExperimentAction.sys.mjs @@ -0,0 +1,278 @@ +/* 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 { BaseStudyAction } from "resource://normandy/actions/BaseStudyAction.sys.mjs"; + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + BaseAction: "resource://normandy/actions/BaseAction.sys.mjs", + ClientEnvironment: "resource://normandy/lib/ClientEnvironment.sys.mjs", + PreferenceExperiments: + "resource://normandy/lib/PreferenceExperiments.sys.mjs", + Sampling: "resource://gre/modules/components-utils/Sampling.sys.mjs", +}); + +/** + * Enrolls a user in a preference experiment, in which we assign the + * user to an experiment branch and modify a preference temporarily to + * measure how it affects Firefox via Telemetry. + */ +export class PreferenceExperimentAction extends BaseStudyAction { + get schema() { + return lazy.ActionSchemas["multi-preference-experiment"]; + } + + constructor() { + super(); + this.seenExperimentSlugs = new Set(); + } + + async _processRecipe(recipe, suitability) { + const { + branches, + isHighPopulation, + isEnrollmentPaused, + slug, + userFacingName, + userFacingDescription, + } = recipe.arguments || {}; + + let experiment; + // Slug might not exist, because if suitability is ARGUMENTS_INVALID, the + // arguments is not guaranteed to match the schema. + if (slug) { + this.seenExperimentSlugs.add(slug); + + try { + experiment = await lazy.PreferenceExperiments.get(slug); + } catch (err) { + // This is probably that the experiment doesn't exist. If that's not the + // case, re-throw the error. + if (!(err instanceof lazy.PreferenceExperiments.NotFoundError)) { + throw err; + } + } + } + + switch (suitability) { + case lazy.BaseAction.suitability.SIGNATURE_ERROR: { + this._considerTemporaryError({ experiment, reason: "signature-error" }); + break; + } + + case lazy.BaseAction.suitability.CAPABILITIES_MISMATCH: { + if (experiment && !experiment.expired) { + await lazy.PreferenceExperiments.stop(slug, { + resetValue: true, + reason: "capability-mismatch", + caller: + "PreferenceExperimentAction._processRecipe::capabilities_mismatch", + }); + } + break; + } + + case lazy.BaseAction.suitability.FILTER_MATCH: { + // If we're not in the experiment, try to enroll + if (!experiment) { + // Check all preferences that could be used by this experiment. + // If there's already an active experiment that has set that preference, abort. + const activeExperiments = + await lazy.PreferenceExperiments.getAllActive(); + for (const branch of branches) { + const conflictingPrefs = Object.keys(branch.preferences).filter( + preferenceName => { + return activeExperiments.some(exp => + exp.preferences.hasOwnProperty(preferenceName) + ); + } + ); + if (conflictingPrefs.length) { + throw new Error( + `Experiment ${slug} ignored; another active experiment is already using the + ${conflictingPrefs[0]} preference.` + ); + } + } + + // Determine if enrollment is currently paused for this experiment. + if (isEnrollmentPaused) { + this.log.debug(`Enrollment is paused for experiment "${slug}"`); + return; + } + + // Otherwise, enroll! + const branch = await this.chooseBranch(slug, branches); + const experimentType = isHighPopulation ? "exp-highpop" : "exp"; + await lazy.PreferenceExperiments.start({ + slug, + actionName: this.name, + branch: branch.slug, + preferences: branch.preferences, + experimentType, + userFacingName, + userFacingDescription, + }); + } else if (experiment.expired) { + this.log.debug(`Experiment ${slug} has expired, aborting.`); + } else { + experiment.temporaryErrorDeadline = null; + await lazy.PreferenceExperiments.update(experiment); + await lazy.PreferenceExperiments.markLastSeen(slug); + } + break; + } + + case lazy.BaseAction.suitability.FILTER_MISMATCH: { + if (experiment && !experiment.expired) { + await lazy.PreferenceExperiments.stop(slug, { + resetValue: true, + reason: "filter-mismatch", + caller: + "PreferenceExperimentAction._processRecipe::filter_mismatch", + }); + } + break; + } + + case lazy.BaseAction.suitability.FILTER_ERROR: { + this._considerTemporaryError({ experiment, reason: "filter-error" }); + break; + } + + case lazy.BaseAction.suitability.ARGUMENTS_INVALID: { + if (experiment && !experiment.expired) { + await lazy.PreferenceExperiments.stop(slug, { + resetValue: true, + reason: "arguments-invalid", + caller: + "PreferenceExperimentAction._processRecipe::arguments_invalid", + }); + } + break; + } + + default: { + throw new Error(`Unknown recipe suitability "${suitability}".`); + } + } + } + + async _run(recipe) { + throw new Error("_run shouldn't be called anymore"); + } + + async chooseBranch(slug, branches) { + const ratios = branches.map(branch => branch.ratio); + const userId = lazy.ClientEnvironment.userId; + + // It's important that the input be: + // - Unique per-user (no one is bucketed alike) + // - Unique per-experiment (bucketing differs across multiple experiments) + // - Differs from the input used for sampling the recipe (otherwise only + // branches that contain the same buckets as the recipe sampling will + // receive users) + const input = `${userId}-${slug}-branch`; + + const index = await lazy.Sampling.ratioSample(input, ratios); + return branches[index]; + } + + /** + * End any experiments which we didn't see during this session. + * This is the "normal" way experiments end, as they are disabled on + * the server and so we stop seeing them. This can also happen if + * the user doesn't match the filter any more. + */ + async _finalize({ noRecipes } = {}) { + const activeExperiments = await lazy.PreferenceExperiments.getAllActive(); + + if (noRecipes && this.seenExperimentSlugs.size) { + throw new PreferenceExperimentAction.BadNoRecipesArg(); + } + + return Promise.all( + activeExperiments.map(experiment => { + if (this.name != experiment.actionName) { + // Another action is responsible for cleaning this one + // up. Leave it alone. + return null; + } + + if (noRecipes) { + return this._considerTemporaryError({ + experiment, + reason: "no-recipes", + }); + } + + if (this.seenExperimentSlugs.has(experiment.slug)) { + return null; + } + + return lazy.PreferenceExperiments.stop(experiment.slug, { + resetValue: true, + reason: "recipe-not-seen", + caller: "PreferenceExperimentAction._finalize", + }).catch(e => { + this.log.warn(`Stopping experiment ${experiment.slug} failed: ${e}`); + }); + }) + ); + } + + /** + * Given that a temporary error has occurred for an experiment, check if it + * should be temporarily ignored, or if the deadline has passed. If the + * deadline is passed, the experiment will be ended. If this is the first + * temporary error, a deadline will be generated. Otherwise, nothing will + * happen. + * + * If a temporary deadline exists but cannot be parsed, a new one will be + * made. + * + * The deadline is 7 days from the first time that recipe failed, as + * reckoned by the client's clock. + * + * @param {Object} args + * @param {Experiment} args.experiment The enrolled experiment to potentially unenroll. + * @param {String} args.reason If the recipe should end, the reason it is ending. + */ + async _considerTemporaryError({ experiment, reason }) { + if (!experiment || experiment.expired) { + return; + } + + let now = Date.now(); // milliseconds-since-epoch + let day = 24 * 60 * 60 * 1000; + let newDeadline = new Date(now + 7 * day).toJSON(); + + if (experiment.temporaryErrorDeadline) { + let deadline = new Date(experiment.temporaryErrorDeadline); + // if deadline is an invalid date, set it to one week from now. + if (isNaN(deadline)) { + experiment.temporaryErrorDeadline = newDeadline; + await lazy.PreferenceExperiments.update(experiment); + return; + } + + if (now > deadline) { + await lazy.PreferenceExperiments.stop(experiment.slug, { + resetValue: true, + reason, + caller: "PreferenceExperimentAction._considerTemporaryFailure", + }); + } + } else { + // there is no deadline, so set one + experiment.temporaryErrorDeadline = newDeadline; + await lazy.PreferenceExperiments.update(experiment); + } + } +} + +PreferenceExperimentAction.BadNoRecipesArg = class extends Error { + message = "noRecipes is true, but some recipes observed"; +}; diff --git a/toolkit/components/normandy/actions/PreferenceRollbackAction.sys.mjs b/toolkit/components/normandy/actions/PreferenceRollbackAction.sys.mjs new file mode 100644 index 0000000000..71f7f80886 --- /dev/null +++ b/toolkit/components/normandy/actions/PreferenceRollbackAction.sys.mjs @@ -0,0 +1,95 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + PrefUtils: "resource://normandy/lib/PrefUtils.sys.mjs", + PreferenceRollouts: "resource://normandy/lib/PreferenceRollouts.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +export class PreferenceRollbackAction extends BaseAction { + get schema() { + return lazy.ActionSchemas["preference-rollback"]; + } + + async _run(recipe) { + const { rolloutSlug } = recipe.arguments; + const rollout = await lazy.PreferenceRollouts.get(rolloutSlug); + + if (lazy.PreferenceRollouts.GRADUATION_SET.has(rolloutSlug)) { + // graduated rollouts can't be rolled back + lazy.TelemetryEvents.sendEvent( + "unenrollFailed", + "preference_rollback", + rolloutSlug, + { + reason: "in-graduation-set", + } + ); + throw new Error( + `Cannot rollback rollout in graduation set "${rolloutSlug}".` + ); + } + + if (!rollout) { + this.log.debug(`Rollback ${rolloutSlug} not applicable, skipping`); + return; + } + + switch (rollout.state) { + case lazy.PreferenceRollouts.STATE_ACTIVE: { + this.log.info(`Rolling back ${rolloutSlug}`); + rollout.state = lazy.PreferenceRollouts.STATE_ROLLED_BACK; + for (const { preferenceName, previousValue } of rollout.preferences) { + lazy.PrefUtils.setPref(preferenceName, previousValue, { + branch: "default", + }); + } + await lazy.PreferenceRollouts.update(rollout); + lazy.TelemetryEvents.sendEvent( + "unenroll", + "preference_rollback", + rolloutSlug, + { + reason: "rollback", + } + ); + lazy.TelemetryEnvironment.setExperimentInactive(rolloutSlug); + break; + } + case lazy.PreferenceRollouts.STATE_ROLLED_BACK: { + // The rollout has already been rolled back, so nothing to do here. + break; + } + case lazy.PreferenceRollouts.STATE_GRADUATED: { + // graduated rollouts can't be rolled back + lazy.TelemetryEvents.sendEvent( + "unenrollFailed", + "preference_rollback", + rolloutSlug, + { + reason: "graduated", + } + ); + throw new Error( + `Cannot rollback already graduated rollout ${rolloutSlug}` + ); + } + default: { + throw new Error( + `Unexpected state when rolling back ${rolloutSlug}: ${rollout.state}` + ); + } + } + } + + async _finalize() { + await lazy.PreferenceRollouts.saveStartupPrefs(); + } +} diff --git a/toolkit/components/normandy/actions/PreferenceRolloutAction.sys.mjs b/toolkit/components/normandy/actions/PreferenceRolloutAction.sys.mjs new file mode 100644 index 0000000000..f8d984fc69 --- /dev/null +++ b/toolkit/components/normandy/actions/PreferenceRolloutAction.sys.mjs @@ -0,0 +1,253 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + PrefUtils: "resource://normandy/lib/PrefUtils.sys.mjs", + PreferenceRollouts: "resource://normandy/lib/PreferenceRollouts.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +const PREFERENCE_TYPE_MAP = { + boolean: Services.prefs.PREF_BOOL, + string: Services.prefs.PREF_STRING, + number: Services.prefs.PREF_INT, +}; + +export class PreferenceRolloutAction extends BaseAction { + get schema() { + return lazy.ActionSchemas["preference-rollout"]; + } + + async _run(recipe) { + const args = recipe.arguments; + + // Check if the rollout is on the list of rollouts to stop applying. + if (lazy.PreferenceRollouts.GRADUATION_SET.has(args.slug)) { + this.log.debug( + `Skipping rollout "${args.slug}" because it is in the graduation set.` + ); + return; + } + + // Determine which preferences are already being managed, to avoid + // conflicts between recipes. This will throw if there is a problem. + await this._verifyRolloutPrefs(args); + + const newRollout = { + slug: args.slug, + state: "active", + preferences: args.preferences.map(({ preferenceName, value }) => ({ + preferenceName, + value, + previousValue: lazy.PrefUtils.getPref(preferenceName, { + branch: "default", + }), + })), + }; + + const existingRollout = await lazy.PreferenceRollouts.get(args.slug); + if (existingRollout) { + const anyChanged = await this._updatePrefsForExistingRollout( + existingRollout, + newRollout + ); + + // If anything was different about the new rollout, write it to the db and send an event about it + if (anyChanged) { + await lazy.PreferenceRollouts.update(newRollout); + lazy.TelemetryEvents.sendEvent( + "update", + "preference_rollout", + args.slug, + { + previousState: existingRollout.state, + } + ); + + switch (existingRollout.state) { + case lazy.PreferenceRollouts.STATE_ACTIVE: { + this.log.debug(`Updated preference rollout ${args.slug}`); + break; + } + case lazy.PreferenceRollouts.STATE_GRADUATED: { + this.log.debug(`Ungraduated preference rollout ${args.slug}`); + lazy.TelemetryEnvironment.setExperimentActive( + args.slug, + newRollout.state, + { type: "normandy-prefrollout" } + ); + break; + } + default: { + console.error( + new Error( + `Updated pref rollout in unexpected state: ${existingRollout.state}` + ) + ); + } + } + } else { + this.log.debug(`No updates to preference rollout ${args.slug}`); + } + } else { + // new enrollment + // Check if this rollout would be a no-op, which is not allowed. + if ( + newRollout.preferences.every( + ({ value, previousValue }) => value === previousValue + ) + ) { + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "preference_rollout", + args.slug, + { reason: "would-be-no-op" } + ); + // Throw so that this recipe execution is marked as a failure + throw new Error( + `New rollout ${args.slug} does not change any preferences.` + ); + } + + await lazy.PreferenceRollouts.add(newRollout); + + for (const { preferenceName, value } of args.preferences) { + lazy.PrefUtils.setPref(preferenceName, value, { branch: "default" }); + } + + this.log.debug(`Enrolled in preference rollout ${args.slug}`); + lazy.TelemetryEnvironment.setExperimentActive( + args.slug, + newRollout.state, + { + type: "normandy-prefrollout", + } + ); + lazy.TelemetryEvents.sendEvent( + "enroll", + "preference_rollout", + args.slug, + {} + ); + } + } + + /** + * Check that all the preferences in a rollout are ok to set. This means 1) no + * other rollout is managing them, and 2) they match the types of the builtin + * values. + * @param {PreferenceRollout} rollout The arguments from a rollout recipe. + * @throws If the preferences are not valid, with details in the error message. + */ + async _verifyRolloutPrefs({ slug, preferences }) { + const existingManagedPrefs = new Set(); + for (const rollout of await lazy.PreferenceRollouts.getAllActive()) { + if (rollout.slug === slug) { + continue; + } + for (const prefSpec of rollout.preferences) { + existingManagedPrefs.add(prefSpec.preferenceName); + } + } + + for (const prefSpec of preferences) { + if (existingManagedPrefs.has(prefSpec.preferenceName)) { + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "preference_rollout", + slug, + { + reason: "conflict", + preference: prefSpec.preferenceName, + } + ); + // Throw so that this recipe execution is marked as a failure + throw new Error( + `Cannot start rollout ${slug}. Preference ${prefSpec.preferenceName} is already managed.` + ); + } + const existingPrefType = Services.prefs.getPrefType( + prefSpec.preferenceName + ); + const rolloutPrefType = PREFERENCE_TYPE_MAP[typeof prefSpec.value]; + + if ( + existingPrefType !== Services.prefs.PREF_INVALID && + existingPrefType !== rolloutPrefType + ) { + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "preference_rollout", + slug, + { + reason: "invalid type", + preference: prefSpec.preferenceName, + } + ); + // Throw so that this recipe execution is marked as a failure + throw new Error( + `Cannot start rollout "${slug}" on "${prefSpec.preferenceName}". ` + + `Existing preference is of type ${existingPrefType}, but rollout ` + + `specifies type ${rolloutPrefType}` + ); + } + } + } + + async _updatePrefsForExistingRollout(existingRollout, newRollout) { + let anyChanged = false; + const oldPrefSpecs = new Map( + existingRollout.preferences.map(p => [p.preferenceName, p]) + ); + const newPrefSpecs = new Map( + newRollout.preferences.map(p => [p.preferenceName, p]) + ); + + // Check for any preferences that no longer exist, and un-set them. + for (const { preferenceName, previousValue } of oldPrefSpecs.values()) { + if (!newPrefSpecs.has(preferenceName)) { + this.log.debug( + `updating ${existingRollout.slug}: ${preferenceName} no longer exists` + ); + anyChanged = true; + lazy.PrefUtils.setPref(preferenceName, previousValue, { + branch: "default", + }); + } + } + + // Check for any preferences that are new and need added, or changed and need updated. + for (const prefSpec of Object.values(newRollout.preferences)) { + let oldValue = null; + if (oldPrefSpecs.has(prefSpec.preferenceName)) { + let oldPrefSpec = oldPrefSpecs.get(prefSpec.preferenceName); + oldValue = oldPrefSpec.value; + + // Trust the old rollout for the values of `previousValue`, but don't + // consider this a change, since it already matches the DB, and doesn't + // have any other stateful effect. + prefSpec.previousValue = oldPrefSpec.previousValue; + } + if (oldValue !== newPrefSpecs.get(prefSpec.preferenceName).value) { + anyChanged = true; + this.log.debug( + `updating ${existingRollout.slug}: ${prefSpec.preferenceName} value changed from ${oldValue} to ${prefSpec.value}` + ); + lazy.PrefUtils.setPref(prefSpec.preferenceName, prefSpec.value, { + branch: "default", + }); + } + } + return anyChanged; + } + + async _finalize() { + await lazy.PreferenceRollouts.saveStartupPrefs(); + } +} diff --git a/toolkit/components/normandy/actions/ShowHeartbeatAction.sys.mjs b/toolkit/components/normandy/actions/ShowHeartbeatAction.sys.mjs new file mode 100644 index 0000000000..db9a4ec5ff --- /dev/null +++ b/toolkit/components/normandy/actions/ShowHeartbeatAction.sys.mjs @@ -0,0 +1,222 @@ +/* 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 { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs", + BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs", + ClientEnvironment: "resource://normandy/lib/ClientEnvironment.sys.mjs", + Heartbeat: "resource://normandy/lib/Heartbeat.sys.mjs", + NormandyUtils: "resource://normandy/lib/NormandyUtils.sys.mjs", + ShellService: "resource:///modules/ShellService.sys.mjs", + Storage: "resource://normandy/lib/Storage.sys.mjs", + UpdateUtils: "resource://gre/modules/UpdateUtils.sys.mjs", +}); + +ChromeUtils.defineLazyGetter(lazy, "gAllRecipeStorage", function () { + return new lazy.Storage("normandy-heartbeat"); +}); + +const DAY_IN_MS = 24 * 60 * 60 * 1000; +const HEARTBEAT_THROTTLE = 1 * DAY_IN_MS; + +export class ShowHeartbeatAction extends BaseAction { + static Heartbeat = lazy.Heartbeat; + + static overrideHeartbeatForTests(newHeartbeat) { + if (newHeartbeat) { + this.Heartbeat = newHeartbeat; + } else { + this.Heartbeat = lazy.Heartbeat; + } + } + + get schema() { + return lazy.ActionSchemas["show-heartbeat"]; + } + + async _run(recipe) { + const { + message, + engagementButtonLabel, + thanksMessage, + learnMoreMessage, + learnMoreUrl, + } = recipe.arguments; + + const recipeStorage = new lazy.Storage(recipe.id); + + if (!(await this.shouldShow(recipeStorage, recipe))) { + return; + } + + this.log.debug( + `Heartbeat for recipe ${recipe.id} showing prompt "${message}"` + ); + const targetWindow = lazy.BrowserWindowTracker.getTopWindow(); + + if (!targetWindow) { + throw new Error("No window to show heartbeat in"); + } + + const heartbeat = new ShowHeartbeatAction.Heartbeat(targetWindow, { + surveyId: this.generateSurveyId(recipe), + message, + engagementButtonLabel, + thanksMessage, + learnMoreMessage, + learnMoreUrl, + postAnswerUrl: await this.generatePostAnswerURL(recipe), + flowId: lazy.NormandyUtils.generateUuid(), + // Recipes coming from Nimbus won't have a revision_id. + ...(Object.hasOwn(recipe, "revision_id") + ? { surveyVersion: recipe.revision_id } + : {}), + }); + + heartbeat.eventEmitter.once( + "Voted", + this.updateLastInteraction.bind(this, recipeStorage) + ); + heartbeat.eventEmitter.once( + "Engaged", + this.updateLastInteraction.bind(this, recipeStorage) + ); + + let now = Date.now(); + await Promise.all([ + lazy.gAllRecipeStorage.setItem("lastShown", now), + recipeStorage.setItem("lastShown", now), + ]); + } + + async shouldShow(recipeStorage, recipe) { + const { repeatOption, repeatEvery } = recipe.arguments; + // Don't show any heartbeats to a user more than once per throttle period + let lastShown = await lazy.gAllRecipeStorage.getItem("lastShown"); + if (lastShown) { + const duration = new Date() - lastShown; + if (duration < HEARTBEAT_THROTTLE) { + // show the number of hours since the last heartbeat, with at most 1 decimal point. + const hoursAgo = Math.floor(duration / 1000 / 60 / 6) / 10; + this.log.debug( + `A heartbeat was shown too recently (${hoursAgo} hours), skipping recipe ${recipe.id}.` + ); + return false; + } + } + + switch (repeatOption) { + case "once": { + // Don't show if we've ever shown before + if (await recipeStorage.getItem("lastShown")) { + this.log.debug( + `Heartbeat for "once" recipe ${recipe.id} has been shown before, skipping.` + ); + return false; + } + break; + } + + case "nag": { + // Show a heartbeat again only if the user has not interacted with it before + if (await recipeStorage.getItem("lastInteraction")) { + this.log.debug( + `Heartbeat for "nag" recipe ${recipe.id} has already been interacted with, skipping.` + ); + return false; + } + break; + } + + case "xdays": { + // Show this heartbeat again if it has been at least `repeatEvery` days since the last time it was shown. + let lastShown = await lazy.gAllRecipeStorage.getItem("lastShown"); + if (lastShown) { + lastShown = new Date(lastShown); + const duration = new Date() - lastShown; + if (duration < repeatEvery * DAY_IN_MS) { + // show the number of hours since the last time this hearbeat was shown, with at most 1 decimal point. + const hoursAgo = Math.floor(duration / 1000 / 60 / 6) / 10; + this.log.debug( + `Heartbeat for "xdays" recipe ${recipe.id} ran in the last ${repeatEvery} days, skipping. (${hoursAgo} hours ago)` + ); + return false; + } + } + } + } + + return true; + } + + /** + * Returns a surveyId value. If recipe calls to include the Normandy client + * ID, then the client ID is attached to the surveyId in the format + * `${surveyId}::${userId}`. + * + * @return {String} Survey ID, possibly with user UUID + */ + generateSurveyId(recipe) { + const { includeTelemetryUUID, surveyId } = recipe.arguments; + if (includeTelemetryUUID) { + return `${surveyId}::${lazy.ClientEnvironment.userId}`; + } + return surveyId; + } + + /** + * Generate the appropriate post-answer URL for a recipe. + * @param recipe + * @return {String} URL with post-answer query params + */ + async generatePostAnswerURL(recipe) { + const { postAnswerUrl, message, includeTelemetryUUID } = recipe.arguments; + + // Don`t bother with empty URLs. + if (!postAnswerUrl) { + return postAnswerUrl; + } + + const userId = lazy.ClientEnvironment.userId; + const searchEngine = (await Services.search.getDefault()).identifier; + const args = { + fxVersion: Services.appinfo.version, + isDefaultBrowser: lazy.ShellService.isDefaultBrowser() ? 1 : 0, + searchEngine, + source: "heartbeat", + // `surveyversion` used to be the version of the heartbeat action when it + // was hosted on a server. Keeping it around for compatibility. + surveyversion: Services.appinfo.version, + syncSetup: Services.prefs.prefHasUserValue("services.sync.username") + ? 1 + : 0, + updateChannel: lazy.UpdateUtils.getUpdateChannel(false), + utm_campaign: encodeURIComponent(message.replace(/\s+/g, "")), + utm_medium: recipe.action, + utm_source: "firefox", + }; + if (includeTelemetryUUID) { + args.userId = userId; + } + + let url = new URL(postAnswerUrl); + // create a URL object to append arguments to + for (const [key, val] of Object.entries(args)) { + if (!url.searchParams.has(key)) { + url.searchParams.set(key, val); + } + } + + // return the address with encoded queries + return url.toString(); + } + + updateLastInteraction(recipeStorage) { + recipeStorage.setItem("lastInteraction", Date.now()); + } +} diff --git a/toolkit/components/normandy/actions/schemas/README.md b/toolkit/components/normandy/actions/schemas/README.md new file mode 100644 index 0000000000..06a7dd4b15 --- /dev/null +++ b/toolkit/components/normandy/actions/schemas/README.md @@ -0,0 +1,13 @@ +# Normandy Action Argument Schemas + +This is a collection of schemas describing the arguments expected by Normandy +actions. Its primary purpose is to be used in the Normandy server and Delivery +Console to validate data and provide better user interactions. + +# Contributing + +Modifications to this package are made as part of mozilla-central following +standard procedures: push a patch to Phabricator as part of a bug. + +To release, bump the package version and land that, as above -- it can be part +of another patch -- and then `cd` to this directory and `npm publish`. diff --git a/toolkit/components/normandy/actions/schemas/export_json.js b/toolkit/components/normandy/actions/schemas/export_json.js new file mode 100644 index 0000000000..940db247aa --- /dev/null +++ b/toolkit/components/normandy/actions/schemas/export_json.js @@ -0,0 +1,20 @@ +#!/usr/bin/env node +/* 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/. */ + +/* eslint-env node */ + +/** + * This script exports the schemas from this package in JSON format, for use by + * other tools. It is run as a part of the publishing process to NPM. + */ + +const fs = require("fs"); +const schemas = require("./index.js"); + +fs.writeFile("./schemas.json", JSON.stringify(schemas), err => { + if (err) { + console.error("error", err); + } +}); diff --git a/toolkit/components/normandy/actions/schemas/index.sys.mjs b/toolkit/components/normandy/actions/schemas/index.sys.mjs new file mode 100644 index 0000000000..7ef006e905 --- /dev/null +++ b/toolkit/components/normandy/actions/schemas/index.sys.mjs @@ -0,0 +1,528 @@ +/* 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/. */ + +export const ActionSchemas = { + "console-log": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Log a message to the console", + type: "object", + required: ["message"], + properties: { + message: { + description: "Message to log to the console", + type: "string", + default: "", + }, + }, + }, + + "messaging-experiment": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Messaging Experiment", + type: "object", + required: ["slug", "branches", "isEnrollmentPaused"], + properties: { + slug: { + description: "Unique identifier for this experiment", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + isEnrollmentPaused: { + description: "If true, new users will not be enrolled in the study.", + type: "boolean", + default: true, + }, + branches: { + description: "List of experimental branches", + type: "array", + minItems: 1, + items: { + type: "object", + required: ["slug", "value", "ratio", "groups"], + properties: { + slug: { + description: + "Unique identifier for this branch of the experiment.", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + value: { + description: "Message content.", + type: "object", + properties: {}, + }, + ratio: { + description: + "Ratio of users who should be grouped into this branch.", + type: "integer", + minimum: 1, + }, + groups: { + description: + "A list of experiment groups that can be used to exclude or select related experiments. May be empty.", + type: "array", + items: { + type: "string", + description: "Identifier of the group", + }, + }, + }, + }, + }, + }, + }, + + "preference-rollout": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Change preferences permanently", + type: "object", + required: ["slug", "preferences"], + properties: { + slug: { + description: + "Unique identifer for the rollout, used in telemetry and rollbacks", + type: "string", + pattern: "^[a-z0-9\\-_]+$", + }, + preferences: { + description: "The preferences to change, and their values", + type: "array", + minItems: 1, + items: { + type: "object", + required: ["preferenceName", "value"], + properties: { + preferenceName: { + description: "Full dotted-path of the preference being changed", + type: "string", + }, + value: { + description: "Value to set the preference to", + type: ["string", "integer", "boolean"], + }, + }, + }, + }, + }, + }, + + "preference-rollback": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Undo a preference rollout", + type: "object", + required: ["rolloutSlug"], + properties: { + rolloutSlug: { + description: "Unique identifer for the rollout to undo", + type: "string", + pattern: "^[a-z0-9\\-_]+$", + }, + }, + }, + + "addon-study": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Enroll a user in an opt-out SHIELD study", + type: "object", + required: [ + "name", + "description", + "addonUrl", + "extensionApiId", + "isEnrollmentPaused", + ], + properties: { + name: { + description: "User-facing name of the study", + type: "string", + minLength: 1, + }, + description: { + description: "User-facing description of the study", + type: "string", + minLength: 1, + }, + addonUrl: { + description: "URL of the add-on XPI file", + type: "string", + format: "uri", + minLength: 1, + }, + extensionApiId: { + description: + "The record ID of the extension used for Normandy API calls.", + type: "integer", + }, + isEnrollmentPaused: { + description: "If true, new users will not be enrolled in the study.", + type: "boolean", + default: true, + }, + }, + }, + + "addon-rollout": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Install add-on permanently", + type: "object", + required: ["extensionApiId", "slug"], + properties: { + extensionApiId: { + description: + "The record ID of the extension used for Normandy API calls.", + type: "integer", + }, + slug: { + description: + "Unique identifer for the rollout, used in telemetry and rollbacks.", + type: "string", + pattern: "^[a-z0-9\\-_]+$", + }, + }, + }, + + "addon-rollback": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Undo an add-on rollout", + type: "object", + required: ["rolloutSlug"], + properties: { + rolloutSlug: { + description: "Unique identifer for the rollout to undo.", + type: "string", + pattern: "^[a-z0-9\\-_]+$", + }, + }, + }, + + "branched-addon-study": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Enroll a user in an add-on experiment, with managed branches", + type: "object", + required: [ + "slug", + "userFacingName", + "userFacingDescription", + "branches", + "isEnrollmentPaused", + ], + properties: { + slug: { + description: "Machine-readable identifier", + type: "string", + minLength: 1, + }, + userFacingName: { + description: "User-facing name of the study", + type: "string", + minLength: 1, + }, + userFacingDescription: { + description: "User-facing description of the study", + type: "string", + minLength: 1, + }, + isEnrollmentPaused: { + description: "If true, new users will not be enrolled in the study.", + type: "boolean", + default: true, + }, + branches: { + description: "List of experimental branches", + type: "array", + minItems: 1, + items: { + type: "object", + required: ["slug", "ratio", "extensionApiId"], + properties: { + slug: { + description: + "Unique identifier for this branch of the experiment.", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + ratio: { + description: + "Ratio of users who should be grouped into this branch.", + type: "integer", + minimum: 1, + }, + extensionApiId: { + description: + "The record ID of the add-on uploaded to the Normandy server. May be null, in which case no add-on will be installed.", + type: ["number", "null"], + default: null, + }, + }, + }, + }, + }, + }, + + "show-heartbeat": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Show a Heartbeat survey.", + description: "This action shows a single survey.", + + type: "object", + required: [ + "surveyId", + "message", + "thanksMessage", + "postAnswerUrl", + "learnMoreMessage", + "learnMoreUrl", + ], + properties: { + repeatOption: { + type: "string", + enum: ["once", "xdays", "nag"], + description: "Determines how often a prompt is shown executes.", + default: "once", + }, + repeatEvery: { + description: + "For repeatOption=xdays, how often (in days) the prompt is displayed.", + default: null, + type: ["number", "null"], + }, + includeTelemetryUUID: { + type: "boolean", + description: "Include unique user ID in post-answer-url and Telemetry", + default: false, + }, + surveyId: { + type: "string", + description: "Slug uniquely identifying this survey in telemetry", + }, + message: { + description: "Message to show to the user", + type: "string", + }, + engagementButtonLabel: { + description: + "Text for the engagement button. If specified, this button will be shown instead of rating stars.", + default: null, + type: ["string", "null"], + }, + thanksMessage: { + description: + "Thanks message to show to the user after they've rated Firefox", + type: "string", + }, + postAnswerUrl: { + description: + "URL to redirect the user to after rating Firefox or clicking the engagement button", + default: null, + type: ["string", "null"], + }, + learnMoreMessage: { + description: "Message to show to the user to learn more", + default: null, + type: ["string", "null"], + }, + learnMoreUrl: { + description: "URL to show to the user when they click Learn More", + default: null, + type: ["string", "null"], + }, + }, + }, + + "multi-preference-experiment": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Run a feature experiment activated by a set of preferences.", + type: "object", + required: [ + "slug", + "userFacingName", + "userFacingDescription", + "branches", + "isEnrollmentPaused", + ], + properties: { + slug: { + description: "Unique identifier for this experiment", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + userFacingName: { + description: "User-facing name of the experiment", + type: "string", + minLength: 1, + }, + userFacingDescription: { + description: "User-facing description of the experiment", + type: "string", + minLength: 1, + }, + experimentDocumentUrl: { + description: "URL of a document describing the experiment", + type: "string", + format: "uri", + default: "", + }, + isHighPopulation: { + description: + "Marks the preference experiment as a high population experiment, that should be excluded from certain types of telemetry", + type: "boolean", + default: "false", + }, + isEnrollmentPaused: { + description: "If true, new users will not be enrolled in the study.", + type: "boolean", + default: true, + }, + branches: { + description: "List of experimental branches", + type: "array", + minItems: 1, + items: { + type: "object", + required: ["slug", "ratio", "preferences"], + properties: { + slug: { + description: + "Unique identifier for this branch of the experiment", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + ratio: { + description: + "Ratio of users who should be grouped into this branch", + type: "integer", + minimum: 1, + }, + preferences: { + description: + "The set of preferences to be set if this branch is chosen", + type: "object", + patternProperties: { + ".*": { + type: "object", + properties: { + preferenceType: { + description: + "Data type of the preference that controls this experiment", + type: "string", + enum: ["string", "integer", "boolean"], + }, + preferenceBranchType: { + description: + "Controls whether the default or user value of the preference is modified", + type: "string", + enum: ["user", "default"], + default: "default", + }, + preferenceValue: { + description: + "Value for this preference when this branch is chosen", + type: ["string", "number", "boolean"], + }, + }, + required: [ + "preferenceType", + "preferenceBranchType", + "preferenceValue", + ], + }, + }, + }, + }, + }, + }, + }, + }, + + "single-preference-experiment": { + $schema: "http://json-schema.org/draft-04/schema#", + title: "Run a feature experiment activated by a preference.", + type: "object", + required: [ + "slug", + "preferenceName", + "preferenceType", + "branches", + "isEnrollmentPaused", + ], + properties: { + slug: { + description: "Unique identifier for this experiment", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + experimentDocumentUrl: { + description: "URL of a document describing the experiment", + type: "string", + format: "uri", + default: "", + }, + preferenceName: { + description: + "Full dotted-path of the preference that controls this experiment", + type: "string", + }, + preferenceType: { + description: + "Data type of the preference that controls this experiment", + type: "string", + enum: ["string", "integer", "boolean"], + }, + preferenceBranchType: { + description: + "Controls whether the default or user value of the preference is modified", + type: "string", + enum: ["user", "default"], + default: "default", + }, + isHighPopulation: { + description: + "Marks the preference experiment as a high population experiment, that should be excluded from certain types of telemetry", + type: "boolean", + default: "false", + }, + isEnrollmentPaused: { + description: "If true, new users will not be enrolled in the study.", + type: "boolean", + default: true, + }, + branches: { + description: "List of experimental branches", + type: "array", + minItems: 1, + items: { + type: "object", + required: ["slug", "value", "ratio"], + properties: { + slug: { + description: + "Unique identifier for this branch of the experiment", + type: "string", + pattern: "^[A-Za-z0-9\\-_]+$", + }, + value: { + description: "Value to set the preference to for this branch", + type: ["string", "number", "boolean"], + }, + ratio: { + description: + "Ratio of users who should be grouped into this branch", + type: "integer", + minimum: 1, + }, + }, + }, + }, + }, + }, +}; + +// Legacy name used on Normandy server +ActionSchemas["opt-out-study"] = ActionSchemas["addon-study"]; + +// If running in Node.js, export the schemas. +if (typeof module !== "undefined") { + /* globals module */ + module.exports = ActionSchemas; +} diff --git a/toolkit/components/normandy/actions/schemas/package.json b/toolkit/components/normandy/actions/schemas/package.json new file mode 100644 index 0000000000..f09031f88d --- /dev/null +++ b/toolkit/components/normandy/actions/schemas/package.json @@ -0,0 +1,11 @@ +{ + "name": "@mozilla/normandy-action-argument-schemas", + "version": "0.11.0", + "description": "Schemas for Normandy action arguments", + "main": "index.js", + "author": "Michael Cooper <mcooper@mozilla.com>", + "license": "MPL-2.0", + "scripts": { + "prepack": "node export_json.js" + } +} diff --git a/toolkit/components/normandy/components.conf b/toolkit/components/normandy/components.conf new file mode 100644 index 0000000000..0c61d6d2f5 --- /dev/null +++ b/toolkit/components/normandy/components.conf @@ -0,0 +1,14 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +Classes = [ + { + 'cid': '{6ab96943-a163-482c-9622-4faedc0e827f}', + 'contract_ids': ['@mozilla.org/network/protocol/about;1?what=studies'], + 'esModule': 'resource://gre/modules/ShieldContentProcess.sys.mjs', + 'constructor': 'AboutStudies', + }, +] diff --git a/toolkit/components/normandy/content/AboutPages.sys.mjs b/toolkit/components/normandy/content/AboutPages.sys.mjs new file mode 100644 index 0000000000..5fc13c2724 --- /dev/null +++ b/toolkit/components/normandy/content/AboutPages.sys.mjs @@ -0,0 +1,232 @@ +/* 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AddonStudies: "resource://normandy/lib/AddonStudies.sys.mjs", + BranchedAddonStudyAction: + "resource://normandy/actions/BranchedAddonStudyAction.sys.mjs", + ExperimentManager: "resource://nimbus/lib/ExperimentManager.sys.mjs", + PreferenceExperiments: + "resource://normandy/lib/PreferenceExperiments.sys.mjs", + RecipeRunner: "resource://normandy/lib/RecipeRunner.sys.mjs", + RemoteSettingsExperimentLoader: + "resource://nimbus/lib/RemoteSettingsExperimentLoader.sys.mjs", +}); + +const SHIELD_LEARN_MORE_URL_PREF = "app.normandy.shieldLearnMoreUrl"; +XPCOMUtils.defineLazyPreferenceGetter( + lazy, + "gOptOutStudiesEnabled", + "app.shield.optoutstudies.enabled" +); + +/** + * Class for managing an about: page that Normandy provides. Adapted from + * browser/components/pocket/content/AboutPocket.sys.mjs. + * + * @implements nsIFactory + * @implements nsIAboutModule + */ +class AboutPage { + constructor({ chromeUrl, aboutHost, classID, description, uriFlags }) { + this.chromeUrl = chromeUrl; + this.aboutHost = aboutHost; + this.classID = Components.ID(classID); + this.description = description; + this.uriFlags = uriFlags; + } + + getURIFlags() { + return this.uriFlags; + } + + newChannel(uri, loadInfo) { + const newURI = Services.io.newURI(this.chromeUrl); + const channel = Services.io.newChannelFromURIWithLoadInfo(newURI, loadInfo); + channel.originalURI = uri; + + if (this.uriFlags & Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT) { + const principal = Services.scriptSecurityManager.createContentPrincipal( + uri, + {} + ); + channel.owner = principal; + } + return channel; + } +} +AboutPage.prototype.QueryInterface = ChromeUtils.generateQI(["nsIAboutModule"]); + +/** + * The module exported by this file. + */ +export let AboutPages = {}; + +/** + * The weak set that keeps track of which browsing contexts + * have an about:studies page. + */ +let BrowsingContexts = new WeakSet(); +/** + * about:studies page for displaying in-progress and past Shield studies. + * @type {AboutPage} + * @implements {nsIMessageListener} + */ +ChromeUtils.defineLazyGetter(AboutPages, "aboutStudies", () => { + const aboutStudies = new AboutPage({ + chromeUrl: "resource://normandy-content/about-studies/about-studies.html", + aboutHost: "studies", + classID: "{6ab96943-a163-482c-9622-4faedc0e827f}", + description: "Shield Study Listing", + uriFlags: + Ci.nsIAboutModule.ALLOW_SCRIPT | + Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT | + Ci.nsIAboutModule.URI_MUST_LOAD_IN_CHILD | + Ci.nsIAboutModule.IS_SECURE_CHROME_UI, + }); + + // Extra methods for about:study-specific behavior. + Object.assign(aboutStudies, { + getAddonStudyList() { + return lazy.AddonStudies.getAll(); + }, + + getPreferenceStudyList() { + return lazy.PreferenceExperiments.getAll(); + }, + + getMessagingSystemList() { + return lazy.ExperimentManager.store.getAll(); + }, + + async optInToExperiment(data) { + try { + await lazy.RemoteSettingsExperimentLoader.optInToExperiment(data); + return { + error: false, + message: "Opt-in was successful.", + }; + } catch (error) { + return { + error: true, + message: error.message, + }; + } + }, + + /** Add a browsing context to the weak set; + * this weak set keeps track of all contexts + * that are housing an about:studies page. + */ + addToWeakSet(browsingContext) { + BrowsingContexts.add(browsingContext); + }, + /** Remove a browsing context to the weak set; + * this weak set keeps track of all contexts + * that are housing an about:studies page. + */ + removeFromWeakSet(browsingContext) { + BrowsingContexts.delete(browsingContext); + }, + + /** + * Sends a message to every about:studies page, + * by iterating over the BrowsingContexts weakset. + * @param {string} message The message string to send to. + * @param {object} data The data object to send. + */ + _sendToAll(message, data) { + ChromeUtils.nondeterministicGetWeakSetKeys(BrowsingContexts).forEach( + browser => + browser.currentWindowGlobal + .getActor("ShieldFrame") + .sendAsyncMessage(message, data) + ); + }, + + /** + * Get if studies are enabled. This has to be in the parent process, + * since RecipeRunner is stateful, and can't be interacted with from + * content processes safely. + */ + async getStudiesEnabled() { + await lazy.RecipeRunner.initializedPromise.promise; + return lazy.RecipeRunner.enabled && lazy.gOptOutStudiesEnabled; + }, + + /** + * Disable an active add-on study and remove its add-on. + * @param {String} recipeId the id of the addon to remove + * @param {String} reason the reason for removal + */ + async removeAddonStudy(recipeId, reason) { + try { + const action = new lazy.BranchedAddonStudyAction(); + await action.unenroll(recipeId, reason); + } catch (err) { + // If the exception was that the study was already removed, that's ok. + // If not, rethrow the error. + if (!err.toString().includes("already inactive")) { + throw err; + } + } finally { + // Update any open tabs with the new study list now that it has changed, + // even if the above failed. + this.getAddonStudyList().then(list => + this._sendToAll("Shield:UpdateAddonStudyList", list) + ); + } + }, + + /** + * Disable an active preference study. + * @param {String} experimentName the name of the experiment to remove + * @param {String} reason the reason for removal + */ + async removePreferenceStudy(experimentName, reason) { + try { + await lazy.PreferenceExperiments.stop(experimentName, { + reason, + caller: "AboutPages.removePreferenceStudy", + }); + } catch (err) { + // If the exception was that the study was already removed, that's ok. + // If not, rethrow the error. + if (!err.toString().includes("already expired")) { + throw err; + } + } finally { + // Update any open tabs with the new study list now that it has changed, + // even if the above failed. + this.getPreferenceStudyList().then(list => + this._sendToAll("Shield:UpdatePreferenceStudyList", list) + ); + } + }, + + async removeMessagingSystemExperiment(slug, reason) { + lazy.ExperimentManager.unenroll(slug, reason); + this._sendToAll( + "Shield:UpdateMessagingSystemExperimentList", + lazy.ExperimentManager.store.getAll() + ); + }, + + openDataPreferences() { + const browserWindow = + Services.wm.getMostRecentWindow("navigator:browser"); + browserWindow.openPreferences("privacy-reports"); + }, + + getShieldLearnMoreHref() { + return Services.urlFormatter.formatURLPref(SHIELD_LEARN_MORE_URL_PREF); + }, + }); + + return aboutStudies; +}); diff --git a/toolkit/components/normandy/content/ShieldFrameChild.sys.mjs b/toolkit/components/normandy/content/ShieldFrameChild.sys.mjs new file mode 100644 index 0000000000..96c57e1848 --- /dev/null +++ b/toolkit/components/normandy/content/ShieldFrameChild.sys.mjs @@ -0,0 +1,162 @@ +/* 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/. */ + +/** + * Listen for DOM events bubbling up from the about:studies page, and perform + * privileged actions in response to them. If we need to do anything that the + * content process can't handle (such as reading IndexedDB), we send a message + * to the parent process and handle it there. + */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AboutPages: "resource://normandy-content/AboutPages.sys.mjs", +}); + +ChromeUtils.defineLazyGetter(lazy, "gBrandBundle", function () { + return Services.strings.createBundle( + "chrome://branding/locale/brand.properties" + ); +}); + +ChromeUtils.defineLazyGetter(lazy, "gStringBundle", function () { + return Services.strings.createBundle( + "chrome://global/locale/aboutStudies.properties" + ); +}); + +/** + * Listen for DOM events bubbling up from the about:studies page, and perform + * privileged actions in response to them. If we need to do anything that the + * content process can't handle (such as reading IndexedDB), we send a message + * to the parent process and handle it there. + */ +export class ShieldFrameChild extends JSWindowActorChild { + async handleEvent(event) { + // On page show or page hide, + // add this child to the WeakSet in AboutStudies. + switch (event.type) { + case "pageshow": + this.sendAsyncMessage("Shield:AddToWeakSet"); + return; + + case "pagehide": + this.sendAsyncMessage("Shield:RemoveFromWeakSet"); + return; + } + switch (event.detail.action) { + // Actions that require the parent process + case "GetRemoteValue:AddonStudyList": + let addonStudies = await this.sendQuery("Shield:GetAddonStudyList"); + this.triggerPageCallback( + "ReceiveRemoteValue:AddonStudyList", + addonStudies + ); + break; + case "GetRemoteValue:PreferenceStudyList": + let prefStudies = await this.sendQuery("Shield:GetPreferenceStudyList"); + this.triggerPageCallback( + "ReceiveRemoteValue:PreferenceStudyList", + prefStudies + ); + break; + case "GetRemoteValue:MessagingSystemList": + let experiments = await this.sendQuery("Shield:GetMessagingSystemList"); + this.triggerPageCallback( + "ReceiveRemoteValue:MessagingSystemList", + experiments + ); + break; + case "RemoveAddonStudy": + this.sendAsyncMessage("Shield:RemoveAddonStudy", event.detail.data); + break; + case "RemovePreferenceStudy": + this.sendAsyncMessage( + "Shield:RemovePreferenceStudy", + event.detail.data + ); + break; + case "RemoveMessagingSystemExperiment": + this.sendAsyncMessage( + "Shield:RemoveMessagingSystemExperiment", + event.detail.data + ); + break; + case "GetRemoteValue:StudiesEnabled": + let studiesEnabled = await this.sendQuery("Shield:GetStudiesEnabled"); + this.triggerPageCallback( + "ReceiveRemoteValue:StudiesEnabled", + studiesEnabled + ); + break; + case "NavigateToDataPreferences": + this.sendAsyncMessage("Shield:OpenDataPreferences"); + break; + // Actions that can be performed in the content process + case "GetRemoteValue:ShieldLearnMoreHref": + this.triggerPageCallback( + "ReceiveRemoteValue:ShieldLearnMoreHref", + lazy.AboutPages.aboutStudies.getShieldLearnMoreHref() + ); + break; + case "GetRemoteValue:ShieldTranslations": + const strings = {}; + for (let str of lazy.gStringBundle.getSimpleEnumeration()) { + strings[str.key] = str.value; + } + const brandName = lazy.gBrandBundle.GetStringFromName("brandShortName"); + strings.enabledList = lazy.gStringBundle.formatStringFromName( + "enabledList", + [brandName] + ); + + this.triggerPageCallback( + "ReceiveRemoteValue:ShieldTranslations", + strings + ); + break; + case "ExperimentOptIn": + const message = await this.sendQuery( + "Shield:ExperimentOptIn", + event.detail.data + ); + this.triggerPageCallback("ReceiveRemoteValue:OptInMessage", message); + break; + } + } + + receiveMessage(msg) { + switch (msg.name) { + case "Shield:UpdateAddonStudyList": + this.triggerPageCallback("ReceiveRemoteValue:AddonStudyList", msg.data); + break; + case "Shield:UpdatePreferenceStudyList": + this.triggerPageCallback( + "ReceiveRemoteValue:PreferenceStudyList", + msg.data + ); + break; + case "Shield:UpdateMessagingSystemExperimentList": + this.triggerPageCallback( + "ReceiveRemoteValue:MessagingSystemList", + msg.data + ); + break; + } + } + /** + * Trigger an event to communicate with the unprivileged about:studies page. + * @param {String} type The type of event to trigger. + * @param {Object} detail The data to pass along to the event. + */ + triggerPageCallback(type, detail) { + // Clone details and use the event class from the unprivileged context. + const event = new this.document.defaultView.CustomEvent(type, { + bubbles: true, + detail: Cu.cloneInto(detail, this.document.defaultView), + }); + this.document.dispatchEvent(event); + } +} diff --git a/toolkit/components/normandy/content/ShieldFrameParent.sys.mjs b/toolkit/components/normandy/content/ShieldFrameParent.sys.mjs new file mode 100644 index 0000000000..73aa620474 --- /dev/null +++ b/toolkit/components/normandy/content/ShieldFrameParent.sys.mjs @@ -0,0 +1,53 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AboutPages: "resource://normandy-content/AboutPages.sys.mjs", +}); + +export class ShieldFrameParent extends JSWindowActorParent { + async receiveMessage(msg) { + let { aboutStudies } = lazy.AboutPages; + switch (msg.name) { + case "Shield:AddToWeakSet": + aboutStudies.addToWeakSet(this.browsingContext); + break; + case "Shield:RemoveFromWeakSet": + aboutStudies.removeFromWeakSet(this.browsingContext); + break; + case "Shield:GetAddonStudyList": + return aboutStudies.getAddonStudyList(); + case "Shield:GetPreferenceStudyList": + return aboutStudies.getPreferenceStudyList(); + case "Shield:GetMessagingSystemList": + return aboutStudies.getMessagingSystemList(); + case "Shield:RemoveAddonStudy": + aboutStudies.removeAddonStudy(msg.data.recipeId, msg.data.reason); + break; + case "Shield:RemovePreferenceStudy": + aboutStudies.removePreferenceStudy( + msg.data.experimentName, + msg.data.reason + ); + break; + case "Shield:RemoveMessagingSystemExperiment": + aboutStudies.removeMessagingSystemExperiment( + msg.data.slug, + msg.data.reason + ); + break; + case "Shield:OpenDataPreferences": + aboutStudies.openDataPreferences(); + break; + case "Shield:GetStudiesEnabled": + return aboutStudies.getStudiesEnabled(); + case "Shield:ExperimentOptIn": + return aboutStudies.optInToExperiment(msg.data); + } + + return null; + } +} diff --git a/toolkit/components/normandy/content/about-studies/about-studies.css b/toolkit/components/normandy/content/about-studies/about-studies.css new file mode 100644 index 0000000000..76819ba423 --- /dev/null +++ b/toolkit/components/normandy/content/about-studies/about-studies.css @@ -0,0 +1,176 @@ +/* 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/. */ + +:root { + --icon-background-color-1: #0A84FF; + --icon-background-color-2: #008EA4; + --icon-background-color-3: #ED00B5; + --icon-background-color-4: #058B00; + --icon-background-color-5: #A47F00; + --icon-background-color-6: #FF0039; + --icon-background-disabled-color: #737373; + --body-text-disabled-color: #737373; + --study-status-active-color: #058B00; + --study-status-disabled-color: #737373; +} + +html, +body, +#app { + height: 100%; + width: 100%; +} + +button > .button-box { + padding-inline: 10px; +} + +.about-studies-container { + max-width: 960px; + margin: 0 auto; +} + +.info-box { + margin-bottom: 10px; + text-align: center; +} + +.info-box-content { + align-items: center; + background: var(--in-content-box-info-background); + border: 1px solid var(--in-content-border-color); + display: inline-flex; + padding: 10px 15px; +} + +.info-box-content > * { + margin-right: 10px; +} + +.info-box-content > *:last-child { + margin-right: 0; +} + +.study-list { + list-style-type: none; + margin: 0; + padding: 0; +} + +.study { + align-items: center; + border-bottom: 1px solid var(--in-content-border-color); + display: flex; + flex-direction: row; + padding: 10px; +} + +.study.disabled { + color: var(--body-text-disabled-color); +} + +.study .study-status { + color: var(--study-status-active-color); + font-weight: bold; +} + +.study.disabled .study-status { + color: var(--study-status-disabled-color); +} + +.study:last-child { + border-bottom: none; +} + +.study > * { + margin-right: 15px; +} + +.study > *:last-child { + margin-right: 0; +} + +.study-icon { + color: #FFF; + flex: 0 0 40px; + font-size: 26px; + height: 40px; + line-height: 40px; + text-align: center; + text-transform: capitalize; +} + +.study:nth-child(6n+0) .study-icon { + background: var(--icon-background-color-1); +} + +.study:nth-child(6n+1) .study-icon { + background: var(--icon-background-color-2); +} + +.study:nth-child(6n+2) .study-icon { + background: var(--icon-background-color-3); +} + +.study:nth-child(6n+3) .study-icon { + background: var(--icon-background-color-4); +} + +.study:nth-child(6n+4) .study-icon { + background: var(--icon-background-color-5); +} + +.study:nth-child(6n+5) .study-icon { + background: var(--icon-background-color-6); +} + +.study.disabled .study-icon { + background: var(--icon-background-disabled-color); +} + +.study-details { + flex: 1; + overflow: hidden; +} + +.study-name { + font-weight: bold; +} + +.study-header { + margin-bottom: .3em; +} + +.study-header > * { + margin-right: 5px; +} + +.study-header > *:last-child { + margin-right: 0; +} + +.study-description code { + background-color: rgb(128, 128, 128, 0.1); + border-radius: 3px; + box-sizing: border-box; + color: var(--in-content-text-color); + font-size: 85%; + font-family: 'Fira Mono', 'mono', monospace; + padding: .05em .4em; +} + +.study-actions { + flex: 0 0; +} + +.opt-in-box { + border-radius: 3px; + padding: 10px; + color: var(--study-status-active-color); + border: 1px solid; +} + +.opt-in-box.opt-in-error { + color: var(--text-color-error); +} diff --git a/toolkit/components/normandy/content/about-studies/about-studies.html b/toolkit/components/normandy/content/about-studies/about-studies.html new file mode 100644 index 0000000000..e6f1347227 --- /dev/null +++ b/toolkit/components/normandy/content/about-studies/about-studies.html @@ -0,0 +1,29 @@ +<!-- 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/. --> +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8" /> + <meta name="color-scheme" content="light dark" /> + <meta + http-equiv="Content-Security-Policy" + content="default-src chrome:; script-src resource:; style-src resource: chrome:; object-src 'none'" + /> + <title>about:studies</title> + <link rel="stylesheet" href="chrome://global/skin/global.css" /> + <link rel="stylesheet" href="chrome://global/skin/in-content/common.css" /> + <link + rel="stylesheet" + href="resource://normandy-content/about-studies/about-studies.css" + /> + </head> + <body> + <div id="app"></div> + <script src="resource://normandy-vendor/React.js"></script> + <script src="resource://normandy-vendor/ReactDOM.js"></script> + <script src="resource://normandy-vendor/PropTypes.js"></script> + <script src="resource://normandy-vendor/classnames.js"></script> + <script src="resource://normandy-content/about-studies/about-studies.js"></script> + </body> +</html> diff --git a/toolkit/components/normandy/content/about-studies/about-studies.js b/toolkit/components/normandy/content/about-studies/about-studies.js new file mode 100644 index 0000000000..b43c1b32d8 --- /dev/null +++ b/toolkit/components/normandy/content/about-studies/about-studies.js @@ -0,0 +1,559 @@ +/* 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"; +/* global classnames PropTypes React ReactDOM */ + +/** + * Shorthand for creating elements (to avoid using a JSX preprocessor) + */ +const r = React.createElement; + +/** + * Dispatches a page event to the privileged frame script for this tab. + * @param {String} action + * @param {Object} data + */ +function sendPageEvent(action, data) { + const event = new CustomEvent("ShieldPageEvent", { + bubbles: true, + detail: { action, data }, + }); + document.dispatchEvent(event); +} + +function readOptinParams() { + let searchParams = new URLSearchParams(new URL(location).search); + return { + slug: searchParams.get("optin_slug"), + branch: searchParams.get("optin_branch"), + collection: searchParams.get("optin_collection"), + applyTargeting: !!searchParams.get("apply_targeting"), + }; +} + +/** + * Handle basic layout and routing within about:studies. + */ +class AboutStudies extends React.Component { + constructor(props) { + super(props); + + this.remoteValueNameMap = { + AddonStudyList: "addonStudies", + PreferenceStudyList: "prefStudies", + MessagingSystemList: "experiments", + ShieldLearnMoreHref: "learnMoreHref", + StudiesEnabled: "studiesEnabled", + ShieldTranslations: "translations", + }; + + this.state = {}; + for (const stateName of Object.values(this.remoteValueNameMap)) { + this.state[stateName] = null; + } + this.state.optInMessage = false; + } + + initializeData() { + for (const remoteName of Object.keys(this.remoteValueNameMap)) { + document.addEventListener(`ReceiveRemoteValue:${remoteName}`, this); + sendPageEvent(`GetRemoteValue:${remoteName}`); + } + } + + componentWillMount() { + let optinParams = readOptinParams(); + if (optinParams.branch && optinParams.slug) { + const onOptIn = ({ detail: value }) => { + this.setState({ optInMessage: value }); + this.initializeData(); + document.removeEventListener( + `ReceiveRemoteValue:OptInMessage`, + onOptIn + ); + }; + document.addEventListener(`ReceiveRemoteValue:OptInMessage`, onOptIn); + sendPageEvent(`ExperimentOptIn`, optinParams); + } else { + this.initializeData(); + } + } + + componentWillUnmount() { + for (const remoteName of Object.keys(this.remoteValueNameMap)) { + document.removeEventListener(`ReceiveRemoteValue:${remoteName}`, this); + } + } + + /** Event handle to receive remote values from documentAddEventListener */ + handleEvent({ type, detail: value }) { + const prefix = "ReceiveRemoteValue:"; + if (type.startsWith(prefix)) { + const name = type.substring(prefix.length); + this.setState({ [this.remoteValueNameMap[name]]: value }); + } + } + + render() { + const { + translations, + learnMoreHref, + studiesEnabled, + addonStudies, + prefStudies, + experiments, + optInMessage, + } = this.state; + // Wait for all values to be loaded before rendering. Some of the values may + // be falsey, so an explicit null check is needed. + if (Object.values(this.state).some(v => v === null)) { + return null; + } + + return r( + "div", + { className: "about-studies-container main-content" }, + r(WhatsThisBox, { translations, learnMoreHref, studiesEnabled }), + optInMessage && r(OptInBox, optInMessage), + r(StudyList, { + translations, + addonStudies, + prefStudies, + experiments, + }) + ); + } +} + +/** + * Explains the contents of the page, and offers a way to learn more and update preferences. + */ +class WhatsThisBox extends React.Component { + handleUpdateClick() { + sendPageEvent("NavigateToDataPreferences"); + } + + render() { + const { learnMoreHref, studiesEnabled, translations } = this.props; + + return r( + "div", + { className: "info-box" }, + r( + "div", + { className: "info-box-content" }, + r( + "span", + {}, + studiesEnabled ? translations.enabledList : translations.disabledList + ), + r( + "a", + { id: "shield-studies-learn-more", href: learnMoreHref }, + translations.learnMore + ), + + r( + "button", + { + id: "shield-studies-update-preferences", + onClick: this.handleUpdateClick, + }, + r( + "div", + { className: "button-box" }, + navigator.platform.includes("Win") + ? translations.updateButtonWin + : translations.updateButtonUnix + ) + ) + ) + ); + } +} +/**OptInMessage + * Explains the contents of the page, and offers a way to learn more and update preferences. + */ +function OptInBox({ error, message }) { + return r( + "div", + { className: "opt-in-box" + (error ? " opt-in-error" : "") }, + message + ); +} + +/** + * Shows a list of studies, with an option to end in-progress ones. + */ +class StudyList extends React.Component { + render() { + const { addonStudies, prefStudies, translations, experiments } = this.props; + + if (!addonStudies.length && !prefStudies.length && !experiments.length) { + return r("p", { className: "study-list-info" }, translations.noStudies); + } + + const activeStudies = []; + const inactiveStudies = []; + + // Since we are modifying the study objects, it is polite to make copies + for (const study of addonStudies) { + const clonedStudy = Object.assign({}, study, { + type: "addon", + sortDate: study.studyStartDate, + }); + if (study.active) { + activeStudies.push(clonedStudy); + } else { + inactiveStudies.push(clonedStudy); + } + } + + for (const study of prefStudies) { + const clonedStudy = Object.assign({}, study, { + type: "pref", + sortDate: new Date(study.lastSeen), + }); + if (study.expired) { + inactiveStudies.push(clonedStudy); + } else { + activeStudies.push(clonedStudy); + } + } + + for (const study of experiments) { + const clonedStudy = Object.assign({}, study, { + type: study.experimentType, + sortDate: new Date(study.lastSeen), + }); + if (!study.active && !study.isRollout) { + inactiveStudies.push(clonedStudy); + } else { + activeStudies.push(clonedStudy); + } + } + + activeStudies.sort((a, b) => b.sortDate - a.sortDate); + inactiveStudies.sort((a, b) => b.sortDate - a.sortDate); + return r( + "div", + {}, + r("h2", {}, translations.activeStudiesList), + r( + "ul", + { className: "study-list active-study-list" }, + activeStudies.map(study => { + if (study.type === "addon") { + return r(AddonStudyListItem, { + key: study.slug, + study, + translations, + }); + } + if (study.type === "nimbus" || study.type === "rollout") { + return r(MessagingSystemListItem, { + key: study.slug, + study, + translations, + }); + } + if (study.type === "pref") { + return r(PreferenceStudyListItem, { + key: study.slug, + study, + translations, + }); + } + return null; + }) + ), + r("h2", {}, translations.completedStudiesList), + r( + "ul", + { className: "study-list inactive-study-list" }, + inactiveStudies.map(study => { + if (study.type === "addon") { + return r(AddonStudyListItem, { + key: study.slug, + study, + translations, + }); + } + if ( + study.type === "nimbus" || + study.type === "messaging_experiment" + ) { + return r(MessagingSystemListItem, { + key: study.slug, + study, + translations, + }); + } + if (study.type === "pref") { + return r(PreferenceStudyListItem, { + key: study.slug, + study, + translations, + }); + } + return null; + }) + ) + ); + } +} +StudyList.propTypes = { + addonStudies: PropTypes.array.isRequired, + translations: PropTypes.object.isRequired, +}; + +class MessagingSystemListItem extends React.Component { + constructor(props) { + super(props); + this.handleClickRemove = this.handleClickRemove.bind(this); + } + + handleClickRemove() { + sendPageEvent("RemoveMessagingSystemExperiment", { + slug: this.props.study.slug, + reason: "individual-opt-out", + }); + } + + render() { + const { study, translations } = this.props; + const userFacingName = study.userFacingName || study.slug; + const userFacingDescription = + study.userFacingDescription || "Nimbus experiment."; + return r( + "li", + { + className: classnames("study nimbus", { + disabled: !study.active, + }), + "data-study-slug": study.slug, // used to identify this row in tests + }, + r("div", { className: "study-icon" }, userFacingName.slice(0, 1)), + r( + "div", + { className: "study-details" }, + r( + "div", + { className: "study-header" }, + r("span", { className: "study-name" }, userFacingName), + r("span", {}, "\u2022"), // • + !study.isRollout && [ + r("span", { className: "study-branch-slug" }, study.branch.slug), + r("span", {}, "\u2022"), // • + ], + r( + "span", + { className: "study-status" }, + study.active + ? translations.activeStatus + : translations.completeStatus + ) + ), + r("div", { className: "study-description" }, userFacingDescription) + ), + r( + "div", + { className: "study-actions" }, + study.active && + r( + "button", + { className: "remove-button", onClick: this.handleClickRemove }, + r("div", { className: "button-box" }, translations.removeButton) + ) + ) + ); + } +} + +/** + * Details about an individual add-on study, with an option to end it if it is active. + */ +class AddonStudyListItem extends React.Component { + constructor(props) { + super(props); + this.handleClickRemove = this.handleClickRemove.bind(this); + } + + handleClickRemove() { + sendPageEvent("RemoveAddonStudy", { + recipeId: this.props.study.recipeId, + reason: "individual-opt-out", + }); + } + + render() { + const { study, translations } = this.props; + return r( + "li", + { + className: classnames("study addon-study", { disabled: !study.active }), + "data-study-slug": study.slug, // used to identify this row in tests + }, + r( + "div", + { className: "study-icon" }, + study.userFacingName + .replace(/-?add-?on-?/i, "") + .replace(/-?study-?/i, "") + .slice(0, 1) + ), + r( + "div", + { className: "study-details" }, + r( + "div", + { className: "study-header" }, + r("span", { className: "study-name" }, study.userFacingName), + r("span", {}, "\u2022"), // • + r( + "span", + { className: "study-status" }, + study.active + ? translations.activeStatus + : translations.completeStatus + ) + ), + r( + "div", + { className: "study-description" }, + study.userFacingDescription + ) + ), + r( + "div", + { className: "study-actions" }, + study.active && + r( + "button", + { className: "remove-button", onClick: this.handleClickRemove }, + r("div", { className: "button-box" }, translations.removeButton) + ) + ) + ); + } +} +AddonStudyListItem.propTypes = { + study: PropTypes.shape({ + recipeId: PropTypes.number.isRequired, + slug: PropTypes.string.isRequired, + userFacingName: PropTypes.string.isRequired, + active: PropTypes.bool.isRequired, + userFacingDescription: PropTypes.string.isRequired, + }).isRequired, + translations: PropTypes.object.isRequired, +}; + +/** + * Details about an individual preference study, with an option to end it if it is active. + */ +class PreferenceStudyListItem extends React.Component { + constructor(props) { + super(props); + this.handleClickRemove = this.handleClickRemove.bind(this); + } + + handleClickRemove() { + sendPageEvent("RemovePreferenceStudy", { + experimentName: this.props.study.slug, + reason: "individual-opt-out", + }); + } + + render() { + const { study, translations } = this.props; + + let iconLetter = (study.userFacingName || study.slug) + .replace(/-?pref-?(flip|study)-?/, "") + .replace(/-?study-?/, "") + .slice(0, 1) + .toUpperCase(); + + let description = study.userFacingDescription; + if (!description) { + // Assume there is exactly one preference (old-style preference experiment). + const [preferenceName, { preferenceValue }] = Object.entries( + study.preferences + )[0]; + // Sanitize the values by setting them as the text content of an element, + // and then getting the HTML representation of that text. This will have the + // browser safely sanitize them. Use outerHTML to also include the <code> + // element in the string. + const sanitizer = document.createElement("code"); + sanitizer.textContent = preferenceName; + const sanitizedPreferenceName = sanitizer.outerHTML; + sanitizer.textContent = preferenceValue; + const sanitizedPreferenceValue = sanitizer.outerHTML; + description = translations.preferenceStudyDescription + .replace(/%(?:1\$)?S/, sanitizedPreferenceName) + .replace(/%(?:2\$)?S/, sanitizedPreferenceValue); + } + + return r( + "li", + { + className: classnames("study pref-study", { disabled: study.expired }), + "data-study-slug": study.slug, // used to identify this row in tests + }, + r("div", { className: "study-icon" }, iconLetter), + r( + "div", + { className: "study-details" }, + r( + "div", + { className: "study-header" }, + r( + "span", + { className: "study-name" }, + study.userFacingName || study.slug + ), + r("span", {}, "\u2022"), // • + r( + "span", + { className: "study-status" }, + study.expired + ? translations.completeStatus + : translations.activeStatus + ) + ), + r("div", { + className: "study-description", + dangerouslySetInnerHTML: { __html: description }, + }) + ), + r( + "div", + { className: "study-actions" }, + !study.expired && + r( + "button", + { className: "remove-button", onClick: this.handleClickRemove }, + r("div", { className: "button-box" }, translations.removeButton) + ) + ) + ); + } +} +PreferenceStudyListItem.propTypes = { + study: PropTypes.shape({ + slug: PropTypes.string.isRequired, + userFacingName: PropTypes.string, + userFacingDescription: PropTypes.string, + expired: PropTypes.bool.isRequired, + preferenceName: PropTypes.string.isRequired, + preferenceValue: PropTypes.oneOf( + PropTypes.string, + PropTypes.bool, + PropTypes.number + ).isRequired, + }).isRequired, + translations: PropTypes.object.isRequired, +}; + +ReactDOM.render(r(AboutStudies), document.getElementById("app")); diff --git a/toolkit/components/normandy/docs/data-collection.rst b/toolkit/components/normandy/docs/data-collection.rst new file mode 100644 index 0000000000..b966a91b0a --- /dev/null +++ b/toolkit/components/normandy/docs/data-collection.rst @@ -0,0 +1,401 @@ +Data Collection +=============== +This document describes the types of data that Normandy collects. + +Uptake +------ +Normandy monitors the execution of recipes and reports to +:ref:`telemetry` the amount of successful and failed runs. This data +is reported using :ref:`telemetry/collection/uptake` under the +``normandy`` namespace. + +Runner Status +^^^^^^^^^^^^^ +Once per-fetch and execution of recipes, one of the following statuses is +reported under the key ``normandy/runner``: + +.. data:: RUNNER_SUCCESS + + :Telemetry value: success + + The operation completed successfully. Individual failures with actions and + recipes may have been reported separately. + +.. data:: RUNNER_SERVER_ERROR + + :Telemetry value: server_error + + The data returned by the server when fetching the recipe is invalid in some + way. + +Action Status +^^^^^^^^^^^^^ +For each action available from the Normandy service, one of the +following statuses is reported under the key +``normandy/action/<action name>``: + +.. data:: ACTION_NETWORK_ERROR + + :Telemetry value: network_error + + There was a network-related error while fetching actions + +.. data:: ACTION_PRE_EXECUTION_ERROR + + :Telemetry value: custom_1_error + + There was an error while running the pre-execution hook for the action. + +.. data:: ACTION_POST_EXECUTION_ERROR + + :Telemetry value: custom_2_error + + There was an error while running the post-execution hook for the action. + +.. data:: ACTION_SERVER_ERROR + + :Telemetry value: server_error + + The data returned by the server when fetching the action is invalid in some + way. + +.. data:: ACTION_SUCCESS + + :Telemetry value: success + + The operation completed successfully. Individual failures with recipes may + be reported separately. + +Recipe Status +^^^^^^^^^^^^^ +For each recipe that is fetched and executed, one of the following statuses is +reported under the key ``normandy/recipe/<recipe id>``: + +.. data:: RECIPE_ACTION_DISABLED + + :Telemetry value: custom_1_error + + The action for this recipe failed in some way and was disabled, so the recipe + could not be executed. + +.. data:: RECIPE_DIDNT_MATCH_FILTER + + :Telemetry value: backoff + + The recipe included a Jexl filter that the client did not match, so + the recipe was not executed. + +.. data:: RECIPE_EXECUTION_ERROR + + :Telemetry value: apply_error + + An error occurred while executing the recipe. + +.. data:: RECIPE_FILTER_BROKEN + + :Telemetry value: content_error + + An error occurred while evaluating the Jexl filter for this + recipe. Sometimes this represents a bug in the Jexl filter + parser/evaluator, such as in `bug 1477156 + <https://bugzilla.mozilla.org/show_bug.cgi?id=1477156>`_, or it can + represent an error fetching some data that a Jexl recipe needs such + as `bug 1447804 + <https://bugzilla.mozilla.org/show_bug.cgi?id=1447804>`_. + +.. data:: RECIPE_INVALID_ACTION + + :Telemetry value: download_error + + The action specified by the recipe was invalid and it could not be executed. + +.. data:: RECIPE_SUCCESS + + :Telemetry value: success + + The recipe was executed successfully. + +.. data:: RECIPE_SIGNATURE_INVALID + + :Telemetry value: signature_error + + Normandy failed to verify the signature of the recipe. + + +Additionally, Normandy reports a :ref:`keyed scalar <Scalars>` to measure recipe +freshness. This scalar is called ``normandy.recipe_freshness``, and it +corresponds to the ``last_modified`` date of each recipe (using its ID +as the key), reported as seconds since 1970 in UTC. + + +Enrollment +----------- +Normandy records enrollment and unenrollment of users into studies, and +records that data using :ref:`Telemetry Events <eventtelemetry>`. All data is stored in the +``normandy`` category. + + +Preference Studies +^^^^^^^^^^^^^^^^^^ +Enrollment + method + The string ``"enroll"`` + object + The string ``"preference_study"`` + value + The name of the study (``recipe.arguments.slug``). + extra + branch + The name of the branch the user was assigned to (example: + ``"control"`` or ``"experiment"``). + experimentType + The type of preference experiment. Currently this can take + values "exp" and "exp-highpop", the latter being for + experiments targeting large numbers of users. + +Enrollment Failed + method + The string ``"enrollFailed"`` + object + The string ``"preference_study"`` + value + The slug of the study (``recipe.arguments.slug``) + extra + reason + The reason for unenrollment. Possible values are: + + * ``"invalid-branch"``: The recipe specifies an invalid preference + branch (not to be confused with the experiment branch). Valid values + are "default" and "user". + preferenceBranch + If the reason was ``"invalid-branch"``, the branch that was + specified, truncated to 80 characters. + +Unenrollment + method + The string ``"unenroll"``. + object + The string ``"preference_study"``. + value + The name of the study (``recipe.arguments.slug``). + extra + didResetValue + The string ``"true"`` if the preference was set back to its + original value, ``"false"`` if it was left as its current + value. This can happen when, for example, the user changes a + preference that was involved in a user-branch study. + reason + The reason for unenrollment. Possible values are: + + * ``"recipe-not-seen"``: The recipe was no longer + applicable to this client This can be because the recipe + was disabled, or the user no longer matches the recipe's + filter. + * ``"unknown"``: A reason was not specified. This should be + considered a bug. + +Unenroll Failed + method + The string ``"unenrollFailed"``. + object + The string ``"preference_study"``. + value + The name of the study (``recipe.arguments.slug``) + extra + reason + A code describing the reason the unenroll failed. Possible values are: + + * ``"does-not-exist"``: The system attempted to unenroll a study that + does not exist. This is a bug. + * ``"already-unenrolled"``: The system attempted to unenroll a study + that has already been unenrolled. This is likely a bug. + caller + On Nightly builds only, a string identifying the source of the requested stop. + originalReason + The code that would had been used for the unenrollment, had it not failed. + +Experimental Preference Changed + method + The string ``"expPrefChanged"`` + object + The string ``"preference_study"``. + value + The name of the study (``recipe.arguments.slug``) + extra + preferenceName + The name of the preference that changed. Note that the value of the + preference (old or new) is not given. + reason + A code describing the reason that Normandy detected the preference + change. Possible values are: + + * ``"atEnroll"``: The preferences already had user value when the + experiment started. + * ``"sideload"``: A preference was changed while Normandy's observers + weren't active, likely while the browser was shut down. + * ``"observer"``: The preference was observed to change by Normandy at + runtime. + +Preference Rollouts +^^^^^^^^^^^^^^^^^^^ +Enrollment + Sent when a user first enrolls in a rollout. + + method + The string ``"enroll"`` + object + The string ``"preference_rollout"`` + value + The slug of the rollout (``recipe.arguments.slug``) + +Enroll Failed + Sent when a user attempts to enroll in a rollout, but the enrollment process fails. + + method + The string ``"enrollFailed"`` + object + The string ``"preference_rollout"`` + value + The slug of the rollout (``recipe.arguments.slug``) + extra + reason + A code describing the reason the unenroll failed. Possible values are: + + * ``"invalid type"``: The preferences specified in the rollout do not + match the preferences built in to the browser. The represents a + misconfiguration of the preferences in the recipe on the server. + * ``"would-be-no-op"``: All of the preference specified in the rollout + already have the given values. This represents an error in targeting + on the server. + * ``"conflict"``: At least one of the preferences specified in the + rollout is already managed by another active rollout. + preference + For ``reason="invalid type"``, the first preference that was invalid. + For ``reason="conflict"``, the first preference that is conflicting. + +Update + Sent when the preferences specified in the recipe have changed, and the + client updates the preferences of the browser to match. + + method + The string ``"update"`` + object + The string ``"preference_rollout"`` + value + The slug of the rollout (``recipe.arguments.slug``) + extra + previousState + The state the rollout was in before this update (such as ``"active"`` or ``"graduated"``). + +Graduation + Sent when Normandy determines that further intervention is no longer + needed for this rollout. After this point, Normandy will stop making + changes to the browser for this rollout, unless the rollout recipe changes + to specify preferences different than the built-in. + + method + The string ``"graduate"`` + object + The string ``"preference_rollout"`` + value + The slug of the rollout (``recipe.arguments.slug``) + extra + reason + A code describing the reason for the graduation. Possible values are: + + * ``"all-prefs-match"``: All preferences specified in the rollout now + have built-in values that match the rollouts values. + ``"in-graduation-set"``: The browser has changed versions (usually + updated) to one that specifies this rollout no longer applies and + should be graduated regardless of the built-in preference values. + This behavior is controlled by the constant + ``PreferenceRollouts.GRADUATION_SET``. + +Add-on Studies +^^^^^^^^^^^^^^ +Enrollment + method + The string ``"enroll"`` + object + The string ``"addon_study"`` + value + The name of the study (``recipe.arguments.name``). + extra + addonId + The add-on's ID (example: ``"feature-study@shield.mozilla.com"``). + addonVersion + The add-on's version (example: ``"1.2.3"``). + +Enroll Failure + method + The string ``"enrollFailed"`` + object + The string ``"addon_study"`` + value + The name of the study (``recipe.arguments.name``). + reason + A string containing the filename and line number of the code + that failed, and the name of the error thrown. This information + is purposely limited to avoid leaking personally identifiable + information. This should be considered a bug. + +Update + method + The string ``"update"``, + object + The string ``"addon_study"``, + value + The name of the study (``recipe.arguments.name``). + extra + addonId + The add-on's ID (example: ``"feature-study@shield.mozilla.com"``). + addonVersion + The add-on's version (example: ``"1.2.3"``). + +Update Failure + method + The string ``"updateFailed"`` + object + The string ``"addon_study"`` + value + The name of the study (``recipe.arguments.name``). + extra + reason + A string containing the filename and line number of the code + that failed, and the name of the error thrown. This information + is purposely limited to avoid leaking personally identifiable + information. This should be considered a bug. + +Unenrollment + method + The string ``"unenroll"``. + object + The string ``"addon_study"``. + value + The name of the study (``recipe.arguments.name``). + extra + addonId + The add-on's ID (example: ``"feature-study@shield.mozilla.com"``). + addonVersion + The add-on's version (example: ``"1.2.3"``). + reason + The reason for unenrollment. Possible values are: + + * ``"install-failure"``: The add-on failed to install. + * ``"individual-opt-out"``: The user opted-out of this + particular study. + * ``"general-opt-out"``: The user opted-out of studies in + general. + * ``"recipe-not-seen"``: The recipe was no longer applicable + to this client. This can be because the recipe was + disabled, or the user no longer matches the recipe's + filter. + * ``"uninstalled"``: The study's add-on as uninstalled by some + mechanism. For example, this could be a user action or the + add-on self-uninstalling. + * ``"uninstalled-sideload"``: The study's add-on was + uninstalled while Normandy was inactive. This could be that + the add-on is no longer compatible, or was manually removed + from a profile. + * ``"unknown"``: A reason was not specified. This should be + considered a bug. diff --git a/toolkit/components/normandy/docs/execution-model.rst b/toolkit/components/normandy/docs/execution-model.rst new file mode 100644 index 0000000000..6d5b16870c --- /dev/null +++ b/toolkit/components/normandy/docs/execution-model.rst @@ -0,0 +1,95 @@ +Execution Model +=============== +This document describes the execution model of the Normandy Client. + +The basic unit of instruction from the server is a *recipe*, which contains +instructions for filtering, and arguments for a given action. See below for +details. + +One iteration through all of these steps is called a *Normandy session*. This +happens at least once every 6 hours, and possibly more often if Remote +Settings syncs new changes. + +1. Fetching +----------- +A list of all active recipes is retrieved from Remote Settings, which has +likely been syncing them in the background. + +2. Suitability +-------------- + +Once recipes have been retrieved, they go through several checks to determine +their suitability for this client. Recipes contain information about which +clients should execute the recipe. All recipes are processed by all clients, +and all filtering happens in the client. + +For more information, see `the suitabilities docs <./suitabilities.html>`_. + +Signature +~~~~~~~~~ + +First, recipes are validated using a signature generated by Autograph_ that +is included with the recipe. This signature validates both the contents of +the recipe as well as its source. + +This signature is separate and distinct from the signing that happens on the +Remote Settings collection. This provides additional assurance that this +recipe is legitimate and intended to run on this client. + +.. _Autograph: https://github.com/mozilla-services/autograph + +Capabilities +~~~~~~~~~~~~ +Next a recipe is checked for compatibility using *capabilities*. +Capabilities are simple strings, such as ``"action:show-heartbeat"``. A +recipe contains a list of required capabilities, and the Normandy Client has +a list of capabilities that it supports. If any of the capabilities required +by the recipe are not compatible with the client, then the recipe does not +execute. + +Capabilities are used to avoid running recipes on a client that are so +incompatible as to be harmful. For example, some changes to filter expression +handling cannot be detected by filter expressions, and so older clients that +receive filters using these new features would break. + +.. note:: + + Capabilities were first introduced in Firefox 70. Clients prior to this + do not check capabilities, and run all recipes provided. To accommodate + this, the server splits recipes into two Remote Settings collections, + ``normandy-recipes``, and ``normandy-recipes-capabilities``. Clients + prior to Firefox 70 use the former, whereas Firefox 70 and above use the + latter. Recipes that only require "baseline" capabilities are published + to both, and those that require advanced capabilities are only published + to the capabilities aware collection. + +Filter Expressions +~~~~~~~~~~~~~~~~~~ +Finally the recipe's filter expression is checked. Filter expressions are +written in an expression language named JEXL_ that is similar to JavaScript, +but far simpler. It is intended to be as safe to evaluate as possible. + +.. _JEXL: https://github.com/mozilla/mozjexl + +Filters are evaluated in a context that contains details about the client +including browser versions, installed add-ons, and Telemetry data. Filters +have access to "transforms" which are simple functions that can do things like +check preference values or parse strings into ``Date`` objects. Filters don't +have access to change any state in the browser, and are generally +idempotent. However, filters are *not* considered to be "pure functions", +because they have access to state that may change, such as time and location. + +3. Execution +------------ +After a recipe's suitability is determined, that recipe is executed. The +recipe specifies an *action* by name, as well as arguments to pass to that +action. The arguments are validated against an expected schema. + +All action have a pre- and post-step that runs once each Normandy session. +The pre-step is run before any recipes are executed, and once the post-step +is executed, no more recipes will be executed on that action in this session. + +Each recipe is passed to the action, along with its suitability. Individual +actions have their own semantics about what to do with recipes. Many actions +maintain their own life cycle of events for new recipes, existing recipes, +and recipes that stop applying to this client. diff --git a/toolkit/components/normandy/docs/index.rst b/toolkit/components/normandy/docs/index.rst new file mode 100644 index 0000000000..eb77000ee3 --- /dev/null +++ b/toolkit/components/normandy/docs/index.rst @@ -0,0 +1,30 @@ +.. _components/normandy: + +==================== +Shield Recipe Client +==================== + +Normandy (aka the Shield Recipe Client) is a targeted change control +system, allowing small changes to be made within a release of Firefox, +such as studies. + +It downloads recipes and actions from :ref:`Remote Settings <services/remotesettings>` +and then executes them. + +.. note:: + + Previously, the recipes were fetched from the `recipe server`_, but in `Bug 1513854`_ + the source was changed to *Remote Settings*. The cryptographic signatures are verified + at the *Remote Settings* level (integrity) and at the *Normandy* level + (authenticity of publisher). + +.. _recipe server: https://github.com/mozilla/normandy/ +.. _Bug 1513854: https://bugzilla.mozilla.org/show_bug.cgi?id=1513854 + +.. toctree:: + :maxdepth: 1 + + data-collection + execution-model + suitabilities + services diff --git a/toolkit/components/normandy/docs/services.rst b/toolkit/components/normandy/docs/services.rst new file mode 100644 index 0000000000..6bec61a751 --- /dev/null +++ b/toolkit/components/normandy/docs/services.rst @@ -0,0 +1,22 @@ +Normandy Services +================= +The Normandy Client relies on several external services for correct operation. + +Normandy Server +--------------- +This is the place where recipes are created, edited and approved. Normandy +Client interacts with it to get an index of services, and to fetch extensions +and their metadata for add-on studies and rollout. + +Remote Settings +--------------- +This is the primary way that recipes are loaded from the internet by +Normandy. It manages keeping the local list of recipes on the client up to +date and notifying Normandy Client of changes. + +Classify Client +--------------- +This is a service that helps Normandy with filtering. It determines the +region a user is in, based on IP. It also includes an up-to-date time and +date. This allows Normandy to perform location and time based filtering +without having to rely on the local clock. diff --git a/toolkit/components/normandy/docs/suitabilities.rst b/toolkit/components/normandy/docs/suitabilities.rst new file mode 100644 index 0000000000..b199fc2938 --- /dev/null +++ b/toolkit/components/normandy/docs/suitabilities.rst @@ -0,0 +1,73 @@ +Suitabilities +============= + +When Normandy's core passes a recipe to an action, it also passes a +*suitability*, which is the result of evaluating a recipe's filters, +compatibility, signatures, and other checks. This gives actions more +information to make decisions with, which is especially important for +experiments. + +Temporary errors +---------------- +Some of the suitabilities below represent *temporary errors*. These could be +caused by infrastructure problems that prevent the recipe from working +correctly, or are otherwise not the fault of the recipe itself. These +suitabilities should not immediately cause a change in state. If the problem +persists, then eventually it should be considered permanent and state should +be changed. + +In the case of a permanent failure, action such as unenrollment should happen +immediately. For temporary failures, that action should be delayed until the +failure persists longer than some threshold. It is up to individual actions +to track and manage this transition. + +List of Suitabilities +--------------------- + +``FILTER_MATCH`` +~~~~~~~~~~~~~~~~ +All checks have passed, and the recipe is suitable to execute in this client. +Experiments and Rollouts should enroll or update. Heartbeats should be shown +to the user, etc. + +``SIGNATURE_ERROR`` +~~~~~~~~~~~~~~~~~~~ +The recipe's signature is not valid. If any action is taken this recipe +should be treated with extreme suspicion. + +This should be considered a temporary error, because it may be related to +server errors, local clocks, or other temporary problems. + +``CAPABILITIES_MISMATCH`` +~~~~~~~~~~~~~~~~~~~~~~~~~ +The recipe requires capabilities that this recipe runner does not have. Use +caution when interacting with this recipe, as it may not match the expected +schema. + +This should be considered a permanent error, because it is the result of a +choice made on the server. + +``FILTER_MISMATCH`` +~~~~~~~~~~~~~~~~~~~ +This client does not match the recipe's filter, but it is otherwise a +suitable recipe. + +This should be considered a permanent error, since the filter explicitly does +not match the client. + +``FILTER_ERROR`` +~~~~~~~~~~~~~~~~ +There was an error while evaluating the filter. It is unknown if this client +matches this filter. This may be temporary, due to network errors, or +permanent due to syntax errors. + +This should be considered a temporary error, because it may be the result of +infrastructure, such as `Classify Client <./services.html#classify-client>`_, +temporarily failing. + +``ARGUMENTS_INVALID`` +~~~~~~~~~~~~~~~~~~~~~ +The arguments of the recipe do not match the expected schema for the named +action. + +This should be considered a permanent error, since the arguments are generally validated by the server. This likely represents an unrecogonized compatibility error. diff --git a/toolkit/components/normandy/jar.mn b/toolkit/components/normandy/jar.mn new file mode 100644 index 0000000000..ff3dbfb319 --- /dev/null +++ b/toolkit/components/normandy/jar.mn @@ -0,0 +1,19 @@ +# 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/. + +toolkit.jar: +% resource normandy %res/normandy/ + res/normandy/Normandy.sys.mjs (./Normandy.sys.mjs) + res/normandy/NormandyMigrations.sys.mjs (./NormandyMigrations.sys.mjs) + res/normandy/lib/ (./lib/*) + res/normandy/skin/ (./skin/*) + res/normandy/actions/ (./actions/*.sys.mjs) + res/normandy/actions/schemas/index.sys.mjs (./actions/schemas/index.sys.mjs) + res/normandy/schemas/ (./schemas/*.schema.json) + +% resource normandy-content %res/normandy/content/ contentaccessible=yes + res/normandy/content/ (./content/*) + +% resource normandy-vendor %res/normandy/vendor/ contentaccessible=yes + res/normandy/vendor/ (./vendor/*) diff --git a/toolkit/components/normandy/lib/ActionsManager.sys.mjs b/toolkit/components/normandy/lib/ActionsManager.sys.mjs new file mode 100644 index 0000000000..6f811613af --- /dev/null +++ b/toolkit/components/normandy/lib/ActionsManager.sys.mjs @@ -0,0 +1,100 @@ +/* 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 { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AddonRollbackAction: + "resource://normandy/actions/AddonRollbackAction.sys.mjs", + AddonRolloutAction: "resource://normandy/actions/AddonRolloutAction.sys.mjs", + BaseAction: "resource://normandy/actions/BaseAction.sys.mjs", + BranchedAddonStudyAction: + "resource://normandy/actions/BranchedAddonStudyAction.sys.mjs", + ConsoleLogAction: "resource://normandy/actions/ConsoleLogAction.sys.mjs", + MessagingExperimentAction: + "resource://normandy/actions/MessagingExperimentAction.sys.mjs", + PreferenceExperimentAction: + "resource://normandy/actions/PreferenceExperimentAction.sys.mjs", + PreferenceRollbackAction: + "resource://normandy/actions/PreferenceRollbackAction.sys.mjs", + PreferenceRolloutAction: + "resource://normandy/actions/PreferenceRolloutAction.sys.mjs", + ShowHeartbeatAction: + "resource://normandy/actions/ShowHeartbeatAction.sys.mjs", + Uptake: "resource://normandy/lib/Uptake.sys.mjs", +}); + +const log = LogManager.getLogger("recipe-runner"); + +/** + * A class to manage the actions that recipes can use in Normandy. + */ +export class ActionsManager { + constructor() { + this.finalized = false; + + this.localActions = {}; + for (const [name, Constructor] of Object.entries( + ActionsManager.actionConstructors + )) { + this.localActions[name] = new Constructor(); + } + } + + static actionConstructors = { + "addon-rollback": lazy.AddonRollbackAction, + "addon-rollout": lazy.AddonRolloutAction, + "branched-addon-study": lazy.BranchedAddonStudyAction, + "console-log": lazy.ConsoleLogAction, + "messaging-experiment": lazy.MessagingExperimentAction, + "multi-preference-experiment": lazy.PreferenceExperimentAction, + "preference-rollback": lazy.PreferenceRollbackAction, + "preference-rollout": lazy.PreferenceRolloutAction, + "show-heartbeat": lazy.ShowHeartbeatAction, + }; + + static getCapabilities() { + // Prefix each action name with "action." to turn it into a capability name. + let capabilities = new Set(); + for (const actionName of Object.keys(ActionsManager.actionConstructors)) { + capabilities.add(`action.${actionName}`); + } + return capabilities; + } + + async processRecipe(recipe, suitability) { + let actionName = recipe.action; + + if (actionName in this.localActions) { + log.info(`Executing recipe "${recipe.name}" (action=${recipe.action})`); + const action = this.localActions[actionName]; + await action.processRecipe(recipe, suitability); + + // If the recipe doesn't have matching capabilities, then a missing action + // is expected. In this case, don't send an error + } else if ( + suitability !== lazy.BaseAction.suitability.CAPABILITIES_MISMATCH + ) { + log.error( + `Could not execute recipe ${recipe.name}:`, + `Action ${recipe.action} is either missing or invalid.` + ); + await lazy.Uptake.reportRecipe(recipe, lazy.Uptake.RECIPE_INVALID_ACTION); + } + } + + async finalize(options) { + if (this.finalized) { + throw new Error("ActionsManager has already been finalized"); + } + this.finalized = true; + + // Finalize local actions + for (const action of Object.values(this.localActions)) { + action.finalize(options); + } + } +} diff --git a/toolkit/components/normandy/lib/AddonRollouts.sys.mjs b/toolkit/components/normandy/lib/AddonRollouts.sys.mjs new file mode 100644 index 0000000000..ab2994a5f9 --- /dev/null +++ b/toolkit/components/normandy/lib/AddonRollouts.sys.mjs @@ -0,0 +1,210 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + IndexedDB: "resource://gre/modules/IndexedDB.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", +}); + +/** + * AddonRollouts store info about an active or expired addon rollouts. + * @typedef {object} AddonRollout + * @property {int} recipeId + * The ID of the recipe. + * @property {string} slug + * Unique slug of the rollout. + * @property {string} state + * The current state of the rollout: "active", or "rolled-back". + * Active means that Normandy is actively managing therollout. Rolled-back + * means that the rollout was previously active, but has been rolled back for + * this user. + * @property {int} extensionApiId + * The ID used to look up the extension in Normandy's API. + * @property {string} addonId + * The add-on ID for this particular rollout. + * @property {string} addonVersion + * The rollout add-on version number + * @property {string} xpiUrl + * URL that the add-on was installed from. + * @property {string} xpiHash + * The hash of the XPI file. + * @property {string} xpiHashAlgorithm + * The algorithm used to hash the XPI file. + */ + +const DB_NAME = "normandy-addon-rollout"; +const STORE_NAME = "addon-rollouts"; +const DB_OPTIONS = { version: 1 }; + +/** + * Create a new connection to the database. + */ +function openDatabase() { + return lazy.IndexedDB.open(DB_NAME, DB_OPTIONS, db => { + db.createObjectStore(STORE_NAME, { + keyPath: "slug", + }); + }); +} + +/** + * Cache the database connection so that it is shared among multiple operations. + */ +let databasePromise; +function getDatabase() { + if (!databasePromise) { + databasePromise = openDatabase(); + } + return databasePromise; +} + +/** + * Get a transaction for interacting with the rollout store. + * + * @param {IDBDatabase} db + * @param {String} mode Either "readonly" or "readwrite" + * + * NOTE: Methods on the store returned by this function MUST be called + * synchronously, otherwise the transaction with the store will expire. + * This is why the helper takes a database as an argument; if we fetched the + * database in the helper directly, the helper would be async and the + * transaction would expire before methods on the store were called. + */ +function getStore(db, mode) { + if (!mode) { + throw new Error("mode is required"); + } + return db.objectStore(STORE_NAME, mode); +} + +export const AddonRollouts = { + STATE_ACTIVE: "active", + STATE_ROLLED_BACK: "rolled-back", + + async init() { + for (const rollout of await this.getAllActive()) { + lazy.TelemetryEnvironment.setExperimentActive( + rollout.slug, + rollout.state, + { + type: "normandy-addonrollout", + } + ); + } + }, + + /** + * Add a new rollout + * @param {AddonRollout} rollout + */ + async add(rollout) { + const db = await getDatabase(); + return getStore(db, "readwrite").add(rollout); + }, + + /** + * Update an existing rollout + * @param {AddonRollout} rollout + * @throws If a matching rollout does not exist. + */ + async update(rollout) { + if (!(await this.has(rollout.slug))) { + throw new Error( + `Tried to update ${rollout.slug}, but it doesn't already exist.` + ); + } + const db = await getDatabase(); + return getStore(db, "readwrite").put(rollout); + }, + + /** + * Update many existing rollouts. More efficient than calling `update` many + * times in a row. + * @param {Array<PreferenceRollout>} rollouts + * @throws If any of the passed rollouts have a slug that doesn't exist in the database already. + */ + async updateMany(rollouts) { + // Don't touch the database if there is nothing to do + if (!rollouts.length) { + return; + } + + // Both of the below operations use .map() instead of a normal loop becaues + // once we get the object store, we can't let it expire by spinning the + // event loop. This approach queues up all the interactions with the store + // immediately, preventing it from expiring too soon. + + const db = await getDatabase(); + let store = await getStore(db, "readonly"); + await Promise.all( + rollouts.map(async ({ slug }) => { + let existingRollout = await store.get(slug); + if (!existingRollout) { + throw new Error(`Tried to update ${slug}, but it doesn't exist.`); + } + }) + ); + + // awaiting spun the event loop, so the store is now invalid. Get a new + // store. This is also a chance to get it in readwrite mode. + store = await getStore(db, "readwrite"); + await Promise.all(rollouts.map(rollout => store.put(rollout))); + }, + + /** + * Test whether there is a rollout in storage with the given slug. + * @param {string} slug + * @returns {Promise<boolean>} + */ + async has(slug) { + const db = await getDatabase(); + const rollout = await getStore(db, "readonly").get(slug); + return !!rollout; + }, + + /** + * Get a rollout by slug + * @param {string} slug + */ + async get(slug) { + const db = await getDatabase(); + return getStore(db, "readonly").get(slug); + }, + + /** Get all rollouts in the database. */ + async getAll() { + const db = await getDatabase(); + return getStore(db, "readonly").getAll(); + }, + + /** Get all rollouts in the "active" state. */ + async getAllActive() { + const rollouts = await this.getAll(); + return rollouts.filter(rollout => rollout.state === this.STATE_ACTIVE); + }, + + /** + * Test wrapper that temporarily replaces the stored rollout data with fake + * data for testing. + */ + withTestMock() { + return function (testFunction) { + return async function inner(...args) { + let db = await getDatabase(); + const oldData = await getStore(db, "readonly").getAll(); + await getStore(db, "readwrite").clear(); + try { + await testFunction(...args); + } finally { + db = await getDatabase(); + await getStore(db, "readwrite").clear(); + const store = getStore(db, "readwrite"); + await Promise.all(oldData.map(d => store.add(d))); + } + }; + }; + }, +}; diff --git a/toolkit/components/normandy/lib/AddonStudies.sys.mjs b/toolkit/components/normandy/lib/AddonStudies.sys.mjs new file mode 100644 index 0000000000..202bf8ccb3 --- /dev/null +++ b/toolkit/components/normandy/lib/AddonStudies.sys.mjs @@ -0,0 +1,468 @@ +/* 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/. */ + +/** + * @typedef {Object} Study + * @property {Number} recipeId + * ID of the recipe that created the study. Used as the primary key of the + * study. + * @property {Number} slug + * String code used to identify the study for use in Telemetry and logging. + * @property {string} userFacingName + * Name of the study to show to the user + * @property {string} userFacingDescription + * Description of the study and its intent. + * @property {string} branch + * The branch the user is enrolled in + * @property {boolean} active + * Is the study still running? + * @property {string} addonId + * Add-on ID for this particular study. + * @property {string} addonUrl + * URL that the study add-on was installed from. + * @property {string} addonVersion + * Study add-on version number + * @property {int} extensionApiId + * The ID used to look up the extension in Normandy's API. + * @property {string} extensionHash + * The hash of the XPI file. + * @property {string} extensionHashAlgorithm + * The algorithm used to hash the XPI file. + * @property {Date} studyStartDate + * Date when the study was started. + * @property {Date|null} studyEndDate + * Date when the study was ended. + * @property {Date|null} temporaryErrorDeadline + * Date of when temporary errors with this experiment should no longer be + * considered temporary. After this point, further errors will result in + * unenrollment. + */ + +import { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AddonManager: "resource://gre/modules/AddonManager.sys.mjs", + BranchedAddonStudyAction: + "resource://normandy/actions/BranchedAddonStudyAction.sys.mjs", + CleanupManager: "resource://normandy/lib/CleanupManager.sys.mjs", + IndexedDB: "resource://gre/modules/IndexedDB.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +const DB_NAME = "shield"; +const STORE_NAME = "addon-studies"; +const VERSION_STORE_NAME = "addon-studies-version"; +const DB_VERSION = 2; +const STUDY_ENDED_TOPIC = "shield-study-ended"; +const log = LogManager.getLogger("addon-studies"); + +/** + * Create a new connection to the database. + */ +function openDatabase() { + return lazy.IndexedDB.open(DB_NAME, DB_VERSION, async (db, event) => { + if (event.oldVersion < 1) { + db.createObjectStore(STORE_NAME, { + keyPath: "recipeId", + }); + } + + if (event.oldVersion < 2) { + db.createObjectStore(VERSION_STORE_NAME); + } + }); +} + +/** + * Cache the database connection so that it is shared among multiple operations. + */ +let databasePromise; +async function getDatabase() { + if (!databasePromise) { + databasePromise = openDatabase(); + } + return databasePromise; +} + +/** + * Get a transaction for interacting with the study store. + * + * @param {IDBDatabase} db + * @param {String} mode Either "readonly" or "readwrite" + * + * NOTE: Methods on the store returned by this function MUST be called + * synchronously, otherwise the transaction with the store will expire. + * This is why the helper takes a database as an argument; if we fetched the + * database in the helper directly, the helper would be async and the + * transaction would expire before methods on the store were called. + */ +function getStore(db, mode) { + if (!mode) { + throw new Error("mode is required"); + } + return db.objectStore(STORE_NAME, mode); +} + +export var AddonStudies = { + /** + * Test wrapper that temporarily replaces the stored studies with the given + * ones. The original stored studies are restored upon completion. + * + * This is defined here instead of in test code since it needs to access the + * getDatabase, which we don't expose to avoid outside modules relying on the + * type of storage used for studies. + * + * @param {Array} [addonStudies=[]] + */ + withStudies(addonStudies = []) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const oldStudies = await AddonStudies.getAll(); + let db = await getDatabase(); + await AddonStudies.clear(); + const store = getStore(db, "readwrite"); + await Promise.all(addonStudies.map(study => store.add(study))); + + try { + await testFunction({ ...args, addonStudies }); + } finally { + db = await getDatabase(); + await AddonStudies.clear(); + const store = getStore(db, "readwrite"); + await Promise.all(oldStudies.map(study => store.add(study))); + } + }; + }; + }, + + async init() { + for (const study of await this.getAllActive()) { + // If an active study's add-on has been removed since we last ran, stop it. + const addon = await lazy.AddonManager.getAddonByID(study.addonId); + if (!addon) { + await this.markAsEnded(study, "uninstalled-sideload"); + continue; + } + + // Otherwise mark that study as active in Telemetry + lazy.TelemetryEnvironment.setExperimentActive(study.slug, study.branch, { + type: "normandy-addonstudy", + }); + } + + // Listen for add-on uninstalls so we can stop the corresponding studies. + lazy.AddonManager.addAddonListener(this); + lazy.CleanupManager.addCleanupHandler(() => { + lazy.AddonManager.removeAddonListener(this); + }); + }, + + /** + * These migrations should only be called from `NormandyMigrations.jsm` and + * tests. + */ + migrations: { + /** + * Change from "name" and "description" to "slug", "userFacingName", + * and "userFacingDescription". + */ + async migration01AddonStudyFieldsToSlugAndUserFacingFields() { + const db = await getDatabase(); + const studies = await db.objectStore(STORE_NAME, "readonly").getAll(); + + // If there are no studies, stop here to avoid opening the DB again. + if (studies.length === 0) { + return; + } + + // Object stores expire after `await`, so this method accumulates a bunch of + // promises, and then awaits them at the end. + const writePromises = []; + const objectStore = db.objectStore(STORE_NAME, "readwrite"); + + for (const study of studies) { + // use existing name as slug + if (!study.slug) { + study.slug = study.name; + } + + // Rename `name` and `description` as `userFacingName` and `userFacingDescription` + if (study.name && !study.userFacingName) { + study.userFacingName = study.name; + } + delete study.name; + if (study.description && !study.userFacingDescription) { + study.userFacingDescription = study.description; + } + delete study.description; + + // Specify that existing recipes don't have branches + if (!study.branch) { + study.branch = AddonStudies.NO_BRANCHES_MARKER; + } + + writePromises.push(objectStore.put(study)); + } + + await Promise.all(writePromises); + }, + + async migration02RemoveOldAddonStudyAction() { + const studies = await AddonStudies.getAllActive({ + branched: AddonStudies.FILTER_NOT_BRANCHED, + }); + if (!studies.length) { + return; + } + const action = new lazy.BranchedAddonStudyAction(); + for (const study of studies) { + try { + await action.unenroll( + study.recipeId, + "migration-removing-unbranched-action" + ); + } catch (e) { + log.error( + `Stopping add-on study ${study.slug} during migration failed: ${e}` + ); + } + } + }, + }, + + /** + * If a study add-on is uninstalled, mark the study as having ended. + * @param {Addon} addon + */ + async onUninstalled(addon) { + const activeStudies = (await this.getAll()).filter(study => study.active); + const matchingStudy = activeStudies.find( + study => study.addonId === addon.id + ); + if (matchingStudy) { + await this.markAsEnded(matchingStudy, "uninstalled"); + } + }, + + /** + * Remove all stored studies. + */ + async clear() { + const db = await getDatabase(); + await getStore(db, "readwrite").clear(); + }, + + /** + * Test whether there is a study in storage for the given recipe ID. + * @param {Number} recipeId + * @returns {Boolean} + */ + async has(recipeId) { + const db = await getDatabase(); + const study = await getStore(db, "readonly").get(recipeId); + return !!study; + }, + + /** + * Fetch a study from storage. + * @param {Number} recipeId + * @return {Study} The requested study, or null if none with that ID exist. + */ + async get(recipeId) { + const db = await getDatabase(); + return getStore(db, "readonly").get(recipeId); + }, + + FILTER_BRANCHED_ONLY: Symbol("FILTER_BRANCHED_ONLY"), + FILTER_NOT_BRANCHED: Symbol("FILTER_NOT_BRANCHED"), + FILTER_ALL: Symbol("FILTER_ALL"), + + /** + * Fetch all studies in storage. + * @return {Array<Study>} + */ + async getAll({ branched = AddonStudies.FILTER_ALL } = {}) { + const db = await getDatabase(); + let results = await getStore(db, "readonly").getAll(); + + if (branched == AddonStudies.FILTER_BRANCHED_ONLY) { + results = results.filter( + study => study.branch != AddonStudies.NO_BRANCHES_MARKER + ); + } else if (branched == AddonStudies.FILTER_NOT_BRANCHED) { + results = results.filter( + study => study.branch == AddonStudies.NO_BRANCHES_MARKER + ); + } + return results; + }, + + /** + * Fetch all studies in storage. + * @return {Array<Study>} + */ + async getAllActive(options) { + return (await this.getAll(options)).filter(study => study.active); + }, + + /** + * Add a study to storage. + * @return {Promise<void, Error>} Resolves when the study is stored, or rejects with an error. + */ + async add(study) { + const db = await getDatabase(); + return getStore(db, "readwrite").add(study); + }, + + /** + * Update a study in storage. + * @return {Promise<void, Error>} Resolves when the study is updated, or rejects with an error. + */ + async update(study) { + const db = await getDatabase(); + return getStore(db, "readwrite").put(study); + }, + + /** + * Update many existing studies. More efficient than calling `update` many + * times in a row. + * @param {Array<AddonStudy>} studies + * @throws If any of the passed studies have a slug that doesn't exist in the database already. + */ + async updateMany(studies) { + // Don't touch the database if there is nothing to do + if (!studies.length) { + return; + } + + // Both of the below operations use .map() instead of a normal loop becaues + // once we get the object store, we can't let it expire by spinning the + // event loop. This approach queues up all the interactions with the store + // immediately, preventing it from expiring too soon. + + const db = await getDatabase(); + let store = await getStore(db, "readonly"); + await Promise.all( + studies.map(async ({ recipeId }) => { + let existingStudy = await store.get(recipeId); + if (!existingStudy) { + throw new Error( + `Tried to update addon study ${recipeId}, but it doesn't exist.` + ); + } + }) + ); + + // awaiting spun the event loop, so the store is now invalid. Get a new + // store. This is also a chance to get it in readwrite mode. + store = await getStore(db, "readwrite"); + await Promise.all(studies.map(study => store.put(study))); + }, + + /** + * Remove a study from storage + * @param recipeId The recipeId of the study to delete + * @return {Promise<void, Error>} Resolves when the study is deleted, or rejects with an error. + */ + async delete(recipeId) { + const db = await getDatabase(); + return getStore(db, "readwrite").delete(recipeId); + }, + + /** + * Mark a study object as having ended. Modifies the study in-place. + * @param {IDBDatabase} db + * @param {Study} study + * @param {String} reason Why the study is ending. + */ + async markAsEnded(study, reason = "unknown") { + if (reason === "unknown") { + log.warn(`Study ${study.slug} ending for unknown reason.`); + } + + study.active = false; + study.temporaryErrorDeadline = null; + study.studyEndDate = new Date(); + const db = await getDatabase(); + await getStore(db, "readwrite").put(study); + + Services.obs.notifyObservers(study, STUDY_ENDED_TOPIC, `${study.recipeId}`); + lazy.TelemetryEvents.sendEvent("unenroll", "addon_study", study.slug, { + addonId: study.addonId || AddonStudies.NO_ADDON_MARKER, + addonVersion: study.addonVersion || AddonStudies.NO_ADDON_MARKER, + reason, + branch: study.branch, + }); + lazy.TelemetryEnvironment.setExperimentInactive(study.slug); + + await this.callUnenrollListeners(study.addonId, reason); + }, + + // Maps extension id -> Set(callbacks) + _unenrollListeners: new Map(), + + /** + * Register a callback to be invoked when a given study ends. + * + * @param {string} id The extension id + * @param {function} listener The callback + */ + addUnenrollListener(id, listener) { + let listeners = this._unenrollListeners.get(id); + if (!listeners) { + listeners = new Set(); + this._unenrollListeners.set(id, listeners); + } + listeners.add(listener); + }, + + /** + * Unregister a callback to be invoked when a given study ends. + * + * @param {string} id The extension id + * @param {function} listener The callback + */ + removeUnenrollListener(id, listener) { + let listeners = this._unenrollListeners.get(id); + if (listeners) { + listeners.delete(listener); + } + }, + + /** + * Invoke the unenroll callback (if any) for the given extension + * + * @param {string} id The extension id + * @param {string} reason Why the study is ending + * + * @returns {Promise} A Promise resolved after the unenroll listener + * (if any) has finished its unenroll tasks. + */ + async callUnenrollListeners(id, reason) { + let callbacks = this._unenrollListeners.get(id) || []; + + async function callCallback(cb, reason) { + try { + await cb(reason); + } catch (err) { + console.error(err); + } + } + + let promises = []; + for (let callback of callbacks) { + promises.push(callCallback(callback, reason)); + } + + // Wait for all the promises to be settled. This won't throw even if some of + // the listeners fail. + await Promise.all(promises); + }, +}; + +AddonStudies.NO_BRANCHES_MARKER = "__NO_BRANCHES__"; +AddonStudies.NO_ADDON_MARKER = "__NO_ADDON__"; diff --git a/toolkit/components/normandy/lib/CleanupManager.sys.mjs b/toolkit/components/normandy/lib/CleanupManager.sys.mjs new file mode 100644 index 0000000000..9101f0f63c --- /dev/null +++ b/toolkit/components/normandy/lib/CleanupManager.sys.mjs @@ -0,0 +1,49 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AsyncShutdown: "resource://gre/modules/AsyncShutdown.sys.mjs", +}); + +class CleanupManagerClass { + constructor() { + this.handlers = new Set(); + this.cleanupPromise = null; + } + + addCleanupHandler(handler) { + this.handlers.add(handler); + } + + removeCleanupHandler(handler) { + this.handlers.delete(handler); + } + + async cleanup() { + if (this.cleanupPromise === null) { + this.cleanupPromise = (async () => { + for (const handler of this.handlers) { + try { + await handler(); + } catch (ex) { + console.error(ex); + } + } + })(); + + // Block shutdown to ensure any cleanup tasks that write data are + // finished. + lazy.AsyncShutdown.profileBeforeChange.addBlocker( + "ShieldRecipeClient: Cleaning up", + this.cleanupPromise + ); + } + + return this.cleanupPromise; + } +} + +export var CleanupManager = new CleanupManagerClass(); diff --git a/toolkit/components/normandy/lib/ClientEnvironment.sys.mjs b/toolkit/components/normandy/lib/ClientEnvironment.sys.mjs new file mode 100644 index 0000000000..16645dd3b2 --- /dev/null +++ b/toolkit/components/normandy/lib/ClientEnvironment.sys.mjs @@ -0,0 +1,123 @@ +/* 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 { ClientEnvironmentBase } from "resource://gre/modules/components-utils/ClientEnvironment.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AddonRollouts: "resource://normandy/lib/AddonRollouts.sys.mjs", + AddonStudies: "resource://normandy/lib/AddonStudies.sys.mjs", + NormandyApi: "resource://normandy/lib/NormandyApi.sys.mjs", + PreferenceExperiments: + "resource://normandy/lib/PreferenceExperiments.sys.mjs", + PreferenceRollouts: "resource://normandy/lib/PreferenceRollouts.sys.mjs", +}); + +// Cached API request for client attributes that are determined by the Normandy +// service. +let _classifyRequest = null; + +export class ClientEnvironment extends ClientEnvironmentBase { + /** + * Fetches information about the client that is calculated on the server, + * like geolocation and the current time. + * + * The server request is made lazily and is cached for the entire browser + * session. + */ + static async getClientClassification() { + if (!_classifyRequest) { + _classifyRequest = lazy.NormandyApi.classifyClient(); + } + return _classifyRequest; + } + + static clearClassifyCache() { + _classifyRequest = null; + } + + /** + * Test wrapper that mocks the server request for classifying the client. + * @param {Object} data Fake server data to use + * @param {Function} testFunction Test function to execute while mock data is in effect. + */ + static withMockClassify(data, testFunction) { + return async function inner() { + const oldRequest = _classifyRequest; + _classifyRequest = Promise.resolve(data); + await testFunction(); + _classifyRequest = oldRequest; + }; + } + + static get userId() { + return ClientEnvironment.randomizationId; + } + + static get country() { + return (async () => { + const { country } = await ClientEnvironment.getClientClassification(); + return country; + })(); + } + + static get request_time() { + return (async () => { + const { request_time } = + await ClientEnvironment.getClientClassification(); + return request_time; + })(); + } + + static get experiments() { + return (async () => { + const names = { all: [], active: [], expired: [] }; + + for (const { + slug, + expired, + } of await lazy.PreferenceExperiments.getAll()) { + names.all.push(slug); + if (expired) { + names.expired.push(slug); + } else { + names.active.push(slug); + } + } + + return names; + })(); + } + + static get studies() { + return (async () => { + const rv = { pref: {}, addon: {} }; + for (const prefStudy of await lazy.PreferenceExperiments.getAll()) { + rv.pref[prefStudy.slug] = prefStudy; + } + for (const addonStudy of await lazy.AddonStudies.getAll()) { + rv.addon[addonStudy.slug] = addonStudy; + } + return rv; + })(); + } + + static get rollouts() { + return (async () => { + const rv = { pref: {}, addon: {} }; + for (const prefRollout of await lazy.PreferenceRollouts.getAll()) { + rv.pref[prefRollout.slug] = prefRollout; + } + for (const addonRollout of await lazy.AddonRollouts.getAll()) { + rv.addon[addonRollout.slug] = addonRollout; + } + return rv; + })(); + } + + static get isFirstRun() { + return Services.prefs.getBoolPref("app.normandy.first_run", true); + } +} diff --git a/toolkit/components/normandy/lib/EventEmitter.sys.mjs b/toolkit/components/normandy/lib/EventEmitter.sys.mjs new file mode 100644 index 0000000000..551fc4d6b2 --- /dev/null +++ b/toolkit/components/normandy/lib/EventEmitter.sys.mjs @@ -0,0 +1,59 @@ +/* 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 { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const log = LogManager.getLogger("event-emitter"); + +export var EventEmitter = function () { + const listeners = {}; + + return { + emit(eventName, event) { + // Fire events async + Promise.resolve().then(() => { + if (!(eventName in listeners)) { + log.debug( + `EventEmitter: Event fired with no listeners: ${eventName}` + ); + return; + } + // Clone callbacks array to avoid problems with mutation while iterating + const callbacks = Array.from(listeners[eventName]); + for (const cb of callbacks) { + // Clone event so it can't by modified by the handler + let eventToPass = event; + if (typeof event === "object") { + eventToPass = Object.assign({}, event); + } + cb(eventToPass); + } + }); + }, + + on(eventName, callback) { + if (!(eventName in listeners)) { + listeners[eventName] = []; + } + listeners[eventName].push(callback); + }, + + off(eventName, callback) { + if (eventName in listeners) { + const index = listeners[eventName].indexOf(callback); + if (index !== -1) { + listeners[eventName].splice(index, 1); + } + } + }, + + once(eventName, callback) { + const inner = event => { + callback(event); + this.off(eventName, inner); + }; + this.on(eventName, inner); + }, + }; +}; diff --git a/toolkit/components/normandy/lib/Heartbeat.sys.mjs b/toolkit/components/normandy/lib/Heartbeat.sys.mjs new file mode 100644 index 0000000000..7e052e90c9 --- /dev/null +++ b/toolkit/components/normandy/lib/Heartbeat.sys.mjs @@ -0,0 +1,395 @@ +/* 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 { TelemetryController } from "resource://gre/modules/TelemetryController.sys.mjs"; +import { clearTimeout, setTimeout } from "resource://gre/modules/Timer.sys.mjs"; +import { CleanupManager } from "resource://normandy/lib/CleanupManager.sys.mjs"; +import { EventEmitter } from "resource://normandy/lib/EventEmitter.sys.mjs"; +import { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const PREF_SURVEY_DURATION = "browser.uitour.surveyDuration"; +const NOTIFICATION_TIME = 3000; +const HEARTBEAT_CSS_URI = Services.io.newURI( + "resource://normandy/skin/shared/Heartbeat.css" +); +const log = LogManager.getLogger("heartbeat"); +const windowsWithInjectedCss = new WeakSet(); +let anyWindowsWithInjectedCss = false; + +// Add cleanup handler for CSS injected into windows by Heartbeat +CleanupManager.addCleanupHandler(() => { + if (anyWindowsWithInjectedCss) { + for (let window of Services.wm.getEnumerator("navigator:browser")) { + if (windowsWithInjectedCss.has(window)) { + const utils = window.windowUtils; + utils.removeSheet(HEARTBEAT_CSS_URI, window.AGENT_SHEET); + windowsWithInjectedCss.delete(window); + } + } + } +}); + +/** + * Show the Heartbeat UI to request user feedback. + * + * @param chromeWindow + * The chrome window that the heartbeat notification is displayed in. + * @param {Object} options Options object. + * @param {String} options.message + * The message, or question, to display on the notification. + * @param {String} options.thanksMessage + * The thank you message to display after user votes. + * @param {String} options.flowId + * An identifier for this rating flow. Please note that this is only used to + * identify the notification box. + * @param {String} [options.engagementButtonLabel=null] + * The text of the engagement button to use instead of stars. If this is null + * or invalid, rating stars are used. + * @param {String} [options.learnMoreMessage=null] + * The label of the learn more link. No link will be shown if this is null. + * @param {String} [options.learnMoreUrl=null] + * The learn more URL to open when clicking on the learn more link. No learn more + * will be shown if this is an invalid URL. + * @param {String} [options.surveyId] + * An ID for the survey, reflected in the Telemetry ping. + * @param {Number} [options.surveyVersion] + * Survey's version number, reflected in the Telemetry ping. + * @param {boolean} [options.testing] + * Whether this is a test survey, reflected in the Telemetry ping. + * @param {String} [options.postAnswerURL=null] + * The url to visit after the user answers the question. + */ +export var Heartbeat = class { + constructor(chromeWindow, options) { + if (typeof options.flowId !== "string") { + throw new Error( + `flowId must be a string, but got ${JSON.stringify( + options.flowId + )}, a ${typeof options.flowId}` + ); + } + + if (!options.flowId) { + throw new Error("flowId must not be an empty string"); + } + + if (typeof options.message !== "string") { + throw new Error( + `message must be a string, but got ${JSON.stringify( + options.message + )}, a ${typeof options.message}` + ); + } + + if (!options.message) { + throw new Error("message must not be an empty string"); + } + + if (options.postAnswerUrl) { + options.postAnswerUrl = new URL(options.postAnswerUrl); + } else { + options.postAnswerUrl = null; + } + + if (options.learnMoreUrl) { + try { + options.learnMoreUrl = new URL(options.learnMoreUrl); + } catch (e) { + options.learnMoreUrl = null; + } + } + + this.chromeWindow = chromeWindow; + this.eventEmitter = new EventEmitter(); + this.options = options; + this.surveyResults = {}; + this.buttons = []; + + if (!windowsWithInjectedCss.has(chromeWindow)) { + windowsWithInjectedCss.add(chromeWindow); + const utils = chromeWindow.windowUtils; + utils.loadSheet(HEARTBEAT_CSS_URI, chromeWindow.AGENT_SHEET); + anyWindowsWithInjectedCss = true; + } + + // so event handlers are consistent + this.handleWindowClosed = this.handleWindowClosed.bind(this); + this.close = this.close.bind(this); + + // Add Learn More Link + if (this.options.learnMoreMessage && this.options.learnMoreUrl) { + this.buttons.push({ + link: this.options.learnMoreUrl.toString(), + label: this.options.learnMoreMessage, + callback: () => { + this.maybeNotifyHeartbeat("LearnMore"); + return true; + }, + }); + } + + if (this.options.engagementButtonLabel) { + this.buttons.push({ + label: this.options.engagementButtonLabel, + callback: () => { + // Let the consumer know user engaged. + this.maybeNotifyHeartbeat("Engaged"); + + this.userEngaged({ + type: "button", + flowId: this.options.flowId, + }); + + // Return true so that the notification bar doesn't close itself since + // we have a thank you message to show. + return true; + }, + }); + } + + // Build the heartbeat stars + if (!this.options.engagementButtonLabel) { + const numStars = this.options.engagementButtonLabel ? 0 : 5; + this.ratingContainer = this.chromeWindow.document.createElement("span"); + this.ratingContainer.id = "star-rating-container"; + + for (let i = 0; i < numStars; i++) { + // create a star rating element + const ratingElement = + this.chromeWindow.document.createXULElement("toolbarbutton"); + + // style it + const starIndex = numStars - i; + ratingElement.className = "plain star-x"; + ratingElement.id = "star" + starIndex; + ratingElement.setAttribute("data-score", starIndex); + + // Add the click handler + ratingElement.addEventListener("click", ev => { + const rating = parseInt(ev.target.getAttribute("data-score")); + this.maybeNotifyHeartbeat("Voted", { score: rating }); + this.userEngaged({ + type: "stars", + score: rating, + flowId: this.options.flowId, + }); + }); + + this.ratingContainer.appendChild(ratingElement); + } + } + + this.notificationBox = this.chromeWindow.gNotificationBox; + this.noticePromise = new Promise(resolve => { + this.notificationBox + .appendNotification( + "heartbeat-" + this.options.flowId, + { + label: this.options.message, + image: "resource://normandy/skin/shared/heartbeat-icon.svg", + priority: this.notificationBox.PRIORITY_SYSTEM, + eventCallback: eventType => { + if (eventType !== "removed") { + return; + } + this.maybeNotifyHeartbeat("NotificationClosed"); + }, + }, + this.buttons + ) + .then(noticeEl => { + noticeEl.classList.add("heartbeat"); + this.chromeWindow.requestAnimationFrame(() => { + noticeEl.messageText.classList.add("heartbeat"); + }); + if (this.ratingContainer) { + noticeEl.buttonContainer.append(this.ratingContainer); + } + resolve(noticeEl); + }, resolve); + }); + + // Let the consumer know the notification was shown. + this.maybeNotifyHeartbeat("NotificationOffered"); + this.chromeWindow.addEventListener( + "SSWindowClosing", + this.handleWindowClosed + ); + + const surveyDuration = + Services.prefs.getIntPref(PREF_SURVEY_DURATION, 300) * 1000; + this.surveyEndTimer = setTimeout(() => { + this.maybeNotifyHeartbeat("SurveyExpired"); + this.close(); + }, surveyDuration); + + CleanupManager.addCleanupHandler(this.close); + } + + maybeNotifyHeartbeat(name, data = {}) { + if (this.pingSent) { + log.warn( + "Heartbeat event received after Telemetry ping sent. name:", + name, + "data:", + data + ); + return; + } + + const timestamp = Date.now(); + let sendPing = false; + let cleanup = false; + + const phases = { + NotificationOffered: () => { + this.surveyResults.flowId = this.options.flowId; + this.surveyResults.offeredTS = timestamp; + }, + LearnMore: () => { + if (!this.surveyResults.learnMoreTS) { + this.surveyResults.learnMoreTS = timestamp; + } + }, + Engaged: () => { + this.surveyResults.engagedTS = timestamp; + }, + Voted: () => { + this.surveyResults.votedTS = timestamp; + this.surveyResults.score = data.score; + }, + SurveyExpired: () => { + this.surveyResults.expiredTS = timestamp; + }, + NotificationClosed: () => { + this.surveyResults.closedTS = timestamp; + cleanup = true; + sendPing = true; + }, + WindowClosed: () => { + this.surveyResults.windowClosedTS = timestamp; + cleanup = true; + sendPing = true; + }, + default: () => { + log.error("Unrecognized Heartbeat event:", name); + }, + }; + + (phases[name] || phases.default)(); + + data.timestamp = timestamp; + data.flowId = this.options.flowId; + this.eventEmitter.emit(name, data); + + if (sendPing) { + // Send the ping to Telemetry + const payload = Object.assign({ version: 1 }, this.surveyResults); + for (const meta of ["surveyId", "surveyVersion", "testing"]) { + if (this.options.hasOwnProperty(meta)) { + payload[meta] = this.options[meta]; + } + } + + log.debug("Sending telemetry"); + TelemetryController.submitExternalPing("heartbeat", payload, { + addClientId: true, + addEnvironment: true, + }); + + // only for testing + this.eventEmitter.emit("TelemetrySent", payload); + + // Survey is complete, clear out the expiry timer & survey configuration + this.endTimerIfPresent("surveyEndTimer"); + + this.pingSent = true; + this.surveyResults = null; + } + + if (cleanup) { + this.cleanup(); + } + } + + userEngaged(engagementParams) { + this.noticePromise.then(noticeEl => { + // Make the heartbeat icon pulse twice + noticeEl.label = this.options.thanksMessage; + noticeEl.messageImage?.classList.remove("pulse-onshow"); + noticeEl.messageImage?.classList.add("pulse-twice"); + // Remove the custom contents of the buttons + for (let button of noticeEl.buttonContainer.querySelectorAll("button")) { + button.remove(); + } + }); + + // Remove the custom contents of the notice + if (this.ratingContainer) { + this.ratingContainer.remove(); + } + + // Open the engagement tab if we have a valid engagement URL. + if (this.options.postAnswerUrl) { + for (const key in engagementParams) { + this.options.postAnswerUrl.searchParams.append( + key, + engagementParams[key] + ); + } + // Open the engagement URL in a new tab. + let { gBrowser } = this.chromeWindow; + gBrowser.selectedTab = gBrowser.addWebTab( + this.options.postAnswerUrl.toString(), + { + triggeringPrincipal: + Services.scriptSecurityManager.createNullPrincipal({}), + } + ); + } + + this.endTimerIfPresent("surveyEndTimer"); + + this.engagementCloseTimer = setTimeout( + () => this.close(), + NOTIFICATION_TIME + ); + } + + endTimerIfPresent(timerName) { + if (this[timerName]) { + clearTimeout(this[timerName]); + this[timerName] = null; + } + } + + handleWindowClosed() { + this.maybeNotifyHeartbeat("WindowClosed"); + } + + close() { + this.noticePromise.then(noticeEl => + this.notificationBox.removeNotification(noticeEl) + ); + } + + cleanup() { + // Kill the timers which might call things after we've cleaned up: + this.endTimerIfPresent("surveyEndTimer"); + this.endTimerIfPresent("engagementCloseTimer"); + // remove listeners + this.chromeWindow.removeEventListener( + "SSWindowClosing", + this.handleWindowClosed + ); + // remove references for garbage collection + this.chromeWindow = null; + this.notificationBox = null; + this.noticePromise = null; + this.ratingContainer = null; + this.eventEmitter = null; + // Ensure we don't re-enter and release the CleanupManager's reference to us: + CleanupManager.removeCleanupHandler(this.close); + } +}; diff --git a/toolkit/components/normandy/lib/LegacyHeartbeat.sys.mjs b/toolkit/components/normandy/lib/LegacyHeartbeat.sys.mjs new file mode 100644 index 0000000000..501c9f70af --- /dev/null +++ b/toolkit/components/normandy/lib/LegacyHeartbeat.sys.mjs @@ -0,0 +1,48 @@ +/* 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/. */ + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs", + NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs", +}); + +const FEATURE_ID = "legacyHeartbeat"; + +/** + * A bridge between Nimbus and Normandy's Heartbeat implementation. + */ +export const LegacyHeartbeat = { + getHeartbeatRecipe() { + const survey = lazy.NimbusFeatures.legacyHeartbeat.getVariable("survey"); + + if (typeof survey == "undefined") { + return null; + } + + let isRollout = false; + let enrollmentData = lazy.ExperimentAPI.getExperimentMetaData({ + featureId: FEATURE_ID, + }); + + if (!enrollmentData) { + enrollmentData = lazy.ExperimentAPI.getRolloutMetaData({ + featureId: FEATURE_ID, + }); + isRollout = true; + } + + return { + id: `nimbus:${enrollmentData.slug}`, + name: `Nimbus legacyHeartbeat ${isRollout ? "rollout" : "experiment"} ${ + enrollmentData.slug + }`, + action: "show-heartbeat", + arguments: survey, + capabilities: ["action.show-heartbeat"], + filter_expression: "true", + use_only_baseline_capabilities: true, + }; + }, +}; diff --git a/toolkit/components/normandy/lib/LogManager.sys.mjs b/toolkit/components/normandy/lib/LogManager.sys.mjs new file mode 100644 index 0000000000..9c1fd0f8fa --- /dev/null +++ b/toolkit/components/normandy/lib/LogManager.sys.mjs @@ -0,0 +1,34 @@ +/* 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"; + +const ROOT_LOGGER_NAME = "app.normandy"; +let rootLogger = null; + +export var LogManager = { + /** + * Configure the root logger for the Recipe Client. Must be called at + * least once before using any loggers created via getLogger. + * @param {Number} loggingLevel + * Logging level to use as defined in Log.sys.mjs + */ + configure(loggingLevel) { + if (!rootLogger) { + rootLogger = Log.repository.getLogger(ROOT_LOGGER_NAME); + rootLogger.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter())); + } + rootLogger.level = loggingLevel; + }, + + /** + * Obtain a named logger with the recipe client logger as its parent. + * @param {String} name + * Name of the logger to obtain. + * @return {Logger} + */ + getLogger(name) { + return Log.repository.getLogger(`${ROOT_LOGGER_NAME}.${name}`); + }, +}; diff --git a/toolkit/components/normandy/lib/NormandyAddonManager.sys.mjs b/toolkit/components/normandy/lib/NormandyAddonManager.sys.mjs new file mode 100644 index 0000000000..514e1b5198 --- /dev/null +++ b/toolkit/components/normandy/lib/NormandyAddonManager.sys.mjs @@ -0,0 +1,111 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + AddonManager: "resource://gre/modules/AddonManager.sys.mjs", +}); + +export const NormandyAddonManager = { + async downloadAndInstall({ + createError, + extensionDetails, + applyNormandyChanges, + undoNormandyChanges, + onInstallStarted, + reportError, + }) { + const { extension_id, hash, hash_algorithm, version, xpi } = + extensionDetails; + + const downloadDeferred = Promise.withResolvers(); + const installDeferred = Promise.withResolvers(); + + const install = await lazy.AddonManager.getInstallForURL(xpi, { + hash: `${hash_algorithm}:${hash}`, + telemetryInfo: { source: "internal" }, + }); + + const listener = { + onInstallStarted(cbInstall) { + const versionMatches = cbInstall.addon.version === version; + const idMatches = cbInstall.addon.id === extension_id; + + if (!versionMatches || !idMatches) { + installDeferred.reject(createError("metadata-mismatch")); + return false; // cancel the installation, server metadata does not match downloaded add-on + } + + if (onInstallStarted) { + return onInstallStarted(cbInstall, installDeferred); + } + + return true; + }, + + onDownloadFailed() { + downloadDeferred.reject( + createError("download-failure", { + detail: lazy.AddonManager.errorToString(install.error), + }) + ); + }, + + onDownloadEnded() { + downloadDeferred.resolve(); + return false; // temporarily pause installation for Normandy bookkeeping + }, + + onInstallFailed() { + installDeferred.reject( + createError("install-failure", { + detail: lazy.AddonManager.errorToString(install.error), + }) + ); + }, + + onInstallEnded() { + installDeferred.resolve(); + }, + }; + + install.addListener(listener); + + // Download the add-on + try { + install.install(); + await downloadDeferred.promise; + } catch (err) { + reportError(err); + install.removeListener(listener); + throw err; + } + + // Complete any book-keeping + try { + await applyNormandyChanges(install); + } catch (err) { + reportError(err); + install.removeListener(listener); + install.cancel(); + throw err; + } + + // Finish paused installation + try { + install.install(); + await installDeferred.promise; + } catch (err) { + reportError(err); + install.removeListener(listener); + await undoNormandyChanges(); + throw err; + } + + install.removeListener(listener); + + return [install.addon.id, install.addon.version]; + }, +}; diff --git a/toolkit/components/normandy/lib/NormandyApi.sys.mjs b/toolkit/components/normandy/lib/NormandyApi.sys.mjs new file mode 100644 index 0000000000..4b3387130f --- /dev/null +++ b/toolkit/components/normandy/lib/NormandyApi.sys.mjs @@ -0,0 +1,155 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + CanonicalJSON: "resource://gre/modules/CanonicalJSON.sys.mjs", +}); + +const prefs = Services.prefs.getBranch("app.normandy."); + +let indexPromise = null; + +function getChainRootIdentifier() { + const normandy_url = Services.prefs.getCharPref("app.normandy.api_url"); + if (normandy_url == "https://normandy.cdn.mozilla.net/api/v1") { + return Ci.nsIContentSignatureVerifier.ContentSignatureProdRoot; + } + if (normandy_url.includes("stage.")) { + return Ci.nsIContentSignatureVerifier.ContentSignatureStageRoot; + } + if (normandy_url.includes("dev.")) { + return Ci.nsIContentSignatureVerifier.ContentSignatureDevRoot; + } + if (Services.env.exists("XPCSHELL_TEST_PROFILE_DIR")) { + return Ci.nsIX509CertDB.AppXPCShellRoot; + } + return Ci.nsIContentSignatureVerifier.ContentSignatureLocalRoot; +} + +export var NormandyApi = { + InvalidSignatureError: class InvalidSignatureError extends Error {}, + + clearIndexCache() { + indexPromise = null; + }, + + get(endpoint, data) { + const url = new URL(endpoint); + if (data) { + for (const key of Object.keys(data)) { + url.searchParams.set(key, data[key]); + } + } + return fetch(url.href, { + method: "get", + headers: { Accept: "application/json" }, + credentials: "omit", + }); + }, + + absolutify(url) { + if (url.startsWith("http")) { + return url; + } + const apiBase = prefs.getCharPref("api_url"); + const server = new URL(apiBase).origin; + if (url.startsWith("/")) { + return server + url; + } + throw new Error("Can't use relative urls"); + }, + + async getApiUrl(name) { + if (!indexPromise) { + const apiBase = new URL(prefs.getCharPref("api_url")); + if (!apiBase.pathname.endsWith("/")) { + apiBase.pathname += "/"; + } + indexPromise = this.get(apiBase.toString()).then(res => res.json()); + } + const index = await indexPromise; + if (!(name in index)) { + throw new Error(`API endpoint with name "${name}" not found.`); + } + const url = index[name]; + return this.absolutify(url); + }, + + /** + * Verify content signature, by serializing the specified `object` as + * canonical JSON, and using the Normandy signer verifier to check that + * it matches the signature specified in `signaturePayload`. + * + * If the the signature is not valid, an error is thrown. Otherwise this + * function returns undefined. + * + * @param {object|String} data The object (or string) to be checked + * @param {object} signaturePayload The signature information + * @param {String} signaturePayload.x5u The certificate chain URL + * @param {String} signaturePayload.signature base64 signature bytes + * @param {String} type The object type (eg. `"recipe"`, `"action"`) + * @returns {Promise<undefined>} If the signature is valid, this function returns without error + * @throws {NormandyApi.InvalidSignatureError} if signature is invalid. + */ + async verifyObjectSignature(data, signaturePayload, type) { + const { signature, x5u } = signaturePayload; + const certChainResponse = await this.get(this.absolutify(x5u)); + const certChain = await certChainResponse.text(); + const builtSignature = `p384ecdsa=${signature}`; + + const serialized = + typeof data == "string" ? data : lazy.CanonicalJSON.stringify(data); + + const verifier = Cc[ + "@mozilla.org/security/contentsignatureverifier;1" + ].createInstance(Ci.nsIContentSignatureVerifier); + + let valid; + try { + valid = await verifier.asyncVerifyContentSignature( + serialized, + builtSignature, + certChain, + "normandy.content-signature.mozilla.org", + getChainRootIdentifier() + ); + } catch (err) { + throw new NormandyApi.InvalidSignatureError( + `${type} signature validation failed: ${err}` + ); + } + + if (!valid) { + throw new NormandyApi.InvalidSignatureError( + `${type} signature is not valid` + ); + } + }, + + /** + * Fetch metadata about this client determined by the server. + * @return {object} Metadata specified by the server + */ + async classifyClient() { + const classifyClientUrl = await this.getApiUrl("classify-client"); + const response = await this.get(classifyClientUrl); + const clientData = await response.json(); + clientData.request_time = new Date(clientData.request_time); + return clientData; + }, + + /** + * Fetch details for an extension from the server. + * @param extensionId {integer} The ID of the extension to look up + * @resolves {Object} + */ + async fetchExtensionDetails(extensionId) { + const baseUrl = await this.getApiUrl("extension-list"); + const extensionDetailsUrl = `${baseUrl}${extensionId}/`; + const response = await this.get(extensionDetailsUrl); + return response.json(); + }, +}; diff --git a/toolkit/components/normandy/lib/NormandyUtils.sys.mjs b/toolkit/components/normandy/lib/NormandyUtils.sys.mjs new file mode 100644 index 0000000000..7db12c59b7 --- /dev/null +++ b/toolkit/components/normandy/lib/NormandyUtils.sys.mjs @@ -0,0 +1,10 @@ +/* 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/. */ + +export var NormandyUtils = { + generateUuid() { + // Generate a random UUID, convert it to a string, and slice the braces off the ends. + return Services.uuid.generateUUID().toString().slice(1, -1); + }, +}; diff --git a/toolkit/components/normandy/lib/PrefUtils.sys.mjs b/toolkit/components/normandy/lib/PrefUtils.sys.mjs new file mode 100644 index 0000000000..76cfa9cfbd --- /dev/null +++ b/toolkit/components/normandy/lib/PrefUtils.sys.mjs @@ -0,0 +1,130 @@ +/* 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/. */ + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + LogManager: "resource://normandy/lib/LogManager.sys.mjs", +}); + +ChromeUtils.defineLazyGetter(lazy, "log", () => { + return lazy.LogManager.getLogger("preference-experiments"); +}); + +const kPrefBranches = { + user: Services.prefs, + default: Services.prefs.getDefaultBranch(""), +}; + +export var PrefUtils = { + /** + * Get a preference of any type from the named branch. + * @param {string} pref + * @param {object} [options] + * @param {"default"|"user"} [options.branchName="user"] One of "default" or "user" + * @param {string|boolean|integer|null} [options.defaultValue] + * The value to return if the preference does not exist. Defaults to null. + */ + getPref(pref, { branch = "user", defaultValue = null } = {}) { + const branchObj = kPrefBranches[branch]; + if (!branchObj) { + throw new this.UnexpectedPreferenceBranch( + `"${branch}" is not a valid preference branch` + ); + } + const type = branchObj.getPrefType(pref); + + try { + switch (type) { + case Services.prefs.PREF_BOOL: { + return branchObj.getBoolPref(pref); + } + case Services.prefs.PREF_STRING: { + return branchObj.getStringPref(pref); + } + case Services.prefs.PREF_INT: { + return branchObj.getIntPref(pref); + } + case Services.prefs.PREF_INVALID: { + return defaultValue; + } + } + } catch (e) { + if (branch === "default" && 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. + return defaultValue; + } + // Unexpected error, re-throw it + throw e; + } + + // If `type` isn't any of the above, throw an error. Don't do this in a + // default branch of switch so that error handling is easier. + throw new TypeError(`Unknown preference type (${type}) for ${pref}.`); + }, + + /** + * Set a preference on the named branch + * @param {string} pref + * @param {string|boolean|integer|null} value The value to set. + * @param {object} options + * @param {"user"|"default"} options.branchName The branch to make the change on. + */ + setPref(pref, value, { branch = "user" } = {}) { + if (value === null) { + this.clearPref(pref, { branch }); + return; + } + const branchObj = kPrefBranches[branch]; + if (!branchObj) { + throw new this.UnexpectedPreferenceBranch( + `"${branch}" is not a valid preference branch` + ); + } + switch (typeof value) { + case "boolean": { + branchObj.setBoolPref(pref, value); + break; + } + case "string": { + branchObj.setStringPref(pref, value); + break; + } + case "number": { + branchObj.setIntPref(pref, value); + break; + } + default: { + throw new TypeError( + `Unexpected value type (${typeof value}) for ${pref}.` + ); + } + } + }, + + /** + * Remove a preference from a branch. Note that default branch preferences + * cannot effectively be cleared. If "default" is passed for a branch, an + * error will be logged and nothing else will happen. + * + * @param {string} pref + * @param {object} options + * @param {"user"|"default"} options.branchName The branch to clear + */ + clearPref(pref, { branch = "user" } = {}) { + if (branch === "user") { + kPrefBranches.user.clearUserPref(pref); + } else if (branch === "default") { + lazy.log.warn( + `Cannot reset pref ${pref} on the default branch. Pref will be cleared at next restart.` + ); + } else { + throw new this.UnexpectedPreferenceBranch( + `"${branch}" is not a valid preference branch` + ); + } + }, + + UnexpectedPreferenceType: class extends Error {}, + UnexpectedPreferenceBranch: class extends Error {}, +}; diff --git a/toolkit/components/normandy/lib/PreferenceExperiments.sys.mjs b/toolkit/components/normandy/lib/PreferenceExperiments.sys.mjs new file mode 100644 index 0000000000..c89beff978 --- /dev/null +++ b/toolkit/components/normandy/lib/PreferenceExperiments.sys.mjs @@ -0,0 +1,1036 @@ +/* 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/. */ + +/** + * Preference Experiments temporarily change a preference to one of several test + * values for the duration of the experiment. Telemetry packets are annotated to + * show what experiments are active, and we use this data to measure the + * effectiveness of the preference change. + * + * Info on active and past experiments is stored in a JSON file in the profile + * folder. + * + * Active preference experiments are stopped if they aren't active on the recipe + * server. They also expire if Firefox isn't able to contact the recipe server + * after a period of time, as well as if the user modifies the preference during + * an active experiment. + */ + +/** + * Experiments store info about an active or expired preference experiment. + * @typedef {Object} Experiment + * @property {string} slug + * A string uniquely identifying the experiment. Used for telemetry, and other + * machine-oriented use cases. Used as a display name if `userFacingName` is + * null. + * @property {string|null} userFacingName + * A user-friendly name for the experiment. Null on old-style single-preference + * experiments, which do not have a userFacingName. + * @property {string|null} userFacingDescription + * A user-friendly description of the experiment. Null on old-style + * single-preference experiments, which do not have a userFacingDescription. + * @property {string} branch + * Experiment branch that the user was matched to + * @property {boolean} expired + * If false, the experiment is active. + * ISO-formatted date string of when the experiment was last seen from the + * recipe server. + * @property {string|null} temporaryErrorDeadline + * ISO-formatted date string of when temporary errors with this experiment + * should not longer be considered temporary. After this point, further errors + * will result in unenrollment. + * @property {Object} preferences + * An object consisting of all the preferences that are set by this experiment. + * Keys are the name of each preference affected by this experiment. Values are + * Preference Objects, about which see below. + * @property {string} experimentType + * The type to report to Telemetry's experiment marker API. + * @property {string} actionName + * The action who knows about this experiment and is responsible for cleaning + * it up. This should correspond to the `name` of some BaseAction subclass. + */ + +/** + * Each Preference stores information about a preference that an + * experiment sets. + * @property {string|integer|boolean} preferenceValue + * Value to change the preference to during the experiment. + * @property {string} preferenceType + * Type of the preference value being set. + * @property {string|integer|boolean|undefined} previousPreferenceValue + * Value of the preference prior to the experiment, or undefined if it was + * unset. + * @property {PreferenceBranchType} preferenceBranchType + * Controls how we modify the preference to affect the client. + * + * If "default", when the experiment is active, the default value for the + * preference is modified on startup of the add-on. If "user", the user value + * for the preference is modified when the experiment starts, and is reset to + * its original value when the experiment ends. + * @property {boolean} overridden + * Tracks if this preference has been changed away from the experimental value. + */ + +import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs"; +import { CleanupManager } from "resource://normandy/lib/CleanupManager.sys.mjs"; +import { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + JSONFile: "resource://gre/modules/JSONFile.sys.mjs", + PrefUtils: "resource://normandy/lib/PrefUtils.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +const EXPERIMENT_FILE = "shield-preference-experiments.json"; +const STARTUP_EXPERIMENT_PREFS_BRANCH = "app.normandy.startupExperimentPrefs."; + +const MAX_EXPERIMENT_TYPE_LENGTH = 20; // enforced by TelemetryEnvironment +const EXPERIMENT_TYPE_PREFIX = "normandy-"; +const MAX_EXPERIMENT_SUBTYPE_LENGTH = + MAX_EXPERIMENT_TYPE_LENGTH - EXPERIMENT_TYPE_PREFIX.length; + +const PREFERENCE_TYPE_MAP = { + boolean: Services.prefs.PREF_BOOL, + string: Services.prefs.PREF_STRING, + integer: Services.prefs.PREF_INT, +}; + +const UserPreferences = Services.prefs; +const DefaultPreferences = Services.prefs.getDefaultBranch(""); + +/** + * Enum storing Preference modules for each type of preference branch. + * @enum {Object} + */ +const PreferenceBranchType = { + user: UserPreferences, + default: DefaultPreferences, +}; + +/** + * Asynchronously load the JSON file that stores experiment status in the profile. + */ +let gStorePromise; +function ensureStorage() { + if (gStorePromise === undefined) { + const path = PathUtils.join( + Services.dirsvc.get("ProfD", Ci.nsIFile).path, + EXPERIMENT_FILE + ); + const storage = new lazy.JSONFile({ path }); + // `storage.load()` is defined as being infallible: It won't ever throw an + // error. However, if there are are I/O errors, such as a corrupt, missing, + // or unreadable file the data loaded will be an empty object. This can + // happen ever after our migrations have run. If that happens, edit the + // storage to match our expected schema before returning it to the rest of + // the module. + gStorePromise = storage.load().then(() => { + if (!storage.data.experiments) { + storage.data = { ...storage.data, experiments: {} }; + } + return storage; + }); + } + return gStorePromise; +} + +const log = LogManager.getLogger("preference-experiments"); + +// List of active preference observers. Cleaned up on shutdown. +let experimentObservers = new Map(); +CleanupManager.addCleanupHandler(() => + PreferenceExperiments.stopAllObservers() +); + +export var PreferenceExperiments = { + /** + * Update the the experiment storage with changes that happened during early startup. + * @param {object} studyPrefsChanged Map from pref name to previous pref value + */ + async recordOriginalValues(studyPrefsChanged) { + const store = await ensureStorage(); + + for (const experiment of Object.values(store.data.experiments)) { + for (const [prefName, prefInfo] of Object.entries( + experiment.preferences + )) { + if (studyPrefsChanged.hasOwnProperty(prefName)) { + if (experiment.expired) { + log.warn( + "Expired preference experiment changed value during startup" + ); + } + if (prefInfo.preferenceBranch !== "default") { + log.warn( + "Non-default branch preference experiment changed value during startup" + ); + } + prefInfo.previousPreferenceValue = studyPrefsChanged[prefName]; + } + } + } + + // not calling store.saveSoon() because if the data doesn't get + // written, it will get updated with fresher data next time the + // browser starts. + }, + + /** + * Set the default preference value for active experiments that use the + * default preference branch. + */ + async init() { + CleanupManager.addCleanupHandler(() => this.saveStartupPrefs()); + + for (const experiment of await this.getAllActive()) { + // Check that the current value of the preference is still what we set it to + for (const [preferenceName, spec] of Object.entries( + experiment.preferences + )) { + if ( + !spec.overridden && + lazy.PrefUtils.getPref(preferenceName) !== spec.preferenceValue + ) { + // if not, record the difference + await this.recordPrefChange({ + experiment, + preferenceName, + reason: "sideload", + }); + } + } + + // Notify Telemetry of experiments we're running, since they don't persist between restarts + lazy.TelemetryEnvironment.setExperimentActive( + experiment.slug, + experiment.branch, + { + type: EXPERIMENT_TYPE_PREFIX + experiment.experimentType, + } + ); + + // Watch for changes to the experiment's preference + this.startObserver(experiment.slug, experiment.preferences); + } + }, + + /** + * Save in-progress, default-branch preference experiments in a sub-branch of + * the normandy preferences. On startup, we read these to set the + * experimental values. + * + * This is needed because the default branch does not persist between Firefox + * restarts. To compensate for that, Normandy sets the default branch to the + * experiment values again every startup. The values to set the preferences + * to are stored in user-branch preferences because preferences have minimal + * impact on the performance of startup. + */ + async saveStartupPrefs() { + const prefBranch = Services.prefs.getBranch( + STARTUP_EXPERIMENT_PREFS_BRANCH + ); + for (const pref of prefBranch.getChildList("")) { + prefBranch.clearUserPref(pref); + } + + // Only store prefs to set on the default branch. + // Be careful not to store user branch prefs here, because this + // would cause the default branch to match the user branch, + // causing the user branch pref to get cleared. + const allExperiments = await this.getAllActive(); + const defaultBranchPrefs = allExperiments + .flatMap(exp => Object.entries(exp.preferences)) + .filter( + ([preferenceName, preferenceInfo]) => + preferenceInfo.preferenceBranchType === "default" + ); + for (const [preferenceName, { preferenceValue }] of defaultBranchPrefs) { + switch (typeof preferenceValue) { + case "string": + prefBranch.setCharPref(preferenceName, preferenceValue); + break; + + case "number": + prefBranch.setIntPref(preferenceName, preferenceValue); + break; + + case "boolean": + prefBranch.setBoolPref(preferenceName, preferenceValue); + break; + + default: + throw new Error(`Invalid preference type ${typeof preferenceValue}`); + } + } + }, + + /** + * Test wrapper that temporarily replaces the stored experiment data with fake + * data for testing. + */ + withMockExperiments(prefExperiments = []) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const experiments = {}; + + for (const exp of prefExperiments) { + if (exp.name) { + throw new Error( + "Preference experiments 'name' field has been replaced by 'slug' and 'userFacingName', please update." + ); + } + + experiments[exp.slug] = exp; + } + const data = { experiments }; + + const oldPromise = gStorePromise; + gStorePromise = Promise.resolve({ + data, + saveSoon() {}, + }); + const oldObservers = experimentObservers; + experimentObservers = new Map(); + try { + await testFunction({ ...args, prefExperiments }); + } finally { + gStorePromise = oldPromise; + PreferenceExperiments.stopAllObservers(); + experimentObservers = oldObservers; + } + }; + }; + }, + + /** + * Clear all stored data about active and past experiments. + */ + async clearAllExperimentStorage() { + const store = await ensureStorage(); + store.data = { + experiments: {}, + }; + store.saveSoon(); + }, + + /** + * Start a new preference experiment. + * @param {Object} experiment + * @param {string} experiment.slug + * @param {string} experiment.actionName The action who knows about this + * experiment and is responsible for cleaning it up. This should + * correspond to the name of some BaseAction subclass. + * @param {string} experiment.branch + * @param {string} experiment.preferenceName + * @param {string|integer|boolean} experiment.preferenceValue + * @param {PreferenceBranchType} experiment.preferenceBranchType + * @returns {Experiment} The experiment object stored in the data store + * @rejects {Error} + * - If an experiment with the given name already exists + * - if an experiment for the given preference is active + * - If the given preferenceType does not match the existing stored preference + */ + async start({ + name = null, // To check if old code is still using `name` instead of `slug`, and provide a nice error message + slug, + actionName, + branch, + preferences, + experimentType = "exp", + userFacingName = null, + userFacingDescription = null, + }) { + if (name) { + throw new Error( + "Preference experiments 'name' field has been replaced by 'slug' and 'userFacingName', please update." + ); + } + + log.debug(`PreferenceExperiments.start(${slug}, ${branch})`); + + const store = await ensureStorage(); + if (slug in store.data.experiments) { + lazy.TelemetryEvents.sendEvent("enrollFailed", "preference_study", slug, { + reason: "name-conflict", + }); + throw new Error( + `A preference experiment with the slug "${slug}" already exists.` + ); + } + + const activeExperiments = Object.values(store.data.experiments).filter( + e => !e.expired + ); + const preferencesWithConflicts = Object.keys(preferences).filter( + preferenceName => { + return activeExperiments.some(e => + e.preferences.hasOwnProperty(preferenceName) + ); + } + ); + + if (preferencesWithConflicts.length) { + lazy.TelemetryEvents.sendEvent("enrollFailed", "preference_study", slug, { + reason: "pref-conflict", + }); + throw new Error( + `Another preference experiment for the pref "${preferencesWithConflicts[0]}" is currently active.` + ); + } + + if (experimentType.length > MAX_EXPERIMENT_SUBTYPE_LENGTH) { + lazy.TelemetryEvents.sendEvent("enrollFailed", "preference_study", slug, { + reason: "experiment-type-too-long", + }); + throw new Error( + `experimentType must be less than ${MAX_EXPERIMENT_SUBTYPE_LENGTH} characters. ` + + `"${experimentType}" is ${experimentType.length} long.` + ); + } + + // Sanity check each preference + for (const [preferenceName, preferenceInfo] of Object.entries( + preferences + )) { + // Ensure preferenceBranchType is set, using the default from + // the schema. This also modifies the preferenceInfo for use in + // the rest of the function. + preferenceInfo.preferenceBranchType = + preferenceInfo.preferenceBranchType || "default"; + const { preferenceBranchType, preferenceType } = preferenceInfo; + if ( + !(preferenceBranchType === "user" || preferenceBranchType === "default") + ) { + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "preference_study", + slug, + { + reason: "invalid-branch", + prefBranch: preferenceBranchType.slice(0, 80), + } + ); + throw new Error( + `Invalid value for preferenceBranchType: ${preferenceBranchType}` + ); + } + + const prevPrefType = Services.prefs.getPrefType(preferenceName); + const givenPrefType = PREFERENCE_TYPE_MAP[preferenceType]; + + if (!preferenceType || !givenPrefType) { + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "preference_study", + slug, + { + reason: "invalid-type", + } + ); + throw new Error( + `Invalid preferenceType provided (given "${preferenceType}")` + ); + } + + if ( + prevPrefType !== Services.prefs.PREF_INVALID && + prevPrefType !== givenPrefType + ) { + lazy.TelemetryEvents.sendEvent( + "enrollFailed", + "preference_study", + slug, + { + reason: "invalid-type", + } + ); + throw new Error( + `Previous preference value is of type "${prevPrefType}", but was given ` + + `"${givenPrefType}" (${preferenceType})` + ); + } + + preferenceInfo.previousPreferenceValue = lazy.PrefUtils.getPref( + preferenceName, + { branch: preferenceBranchType } + ); + } + + const alreadyOverriddenPrefs = new Set(); + for (const [preferenceName, preferenceInfo] of Object.entries( + preferences + )) { + const { preferenceValue, preferenceBranchType } = preferenceInfo; + + if (preferenceBranchType === "default") { + // Only set the pref if there is no user-branch value, because + // changing the default-branch value to the same value as the + // user-branch will effectively delete the user value. + if (Services.prefs.prefHasUserValue(preferenceName)) { + alreadyOverriddenPrefs.add(preferenceName); + } else { + lazy.PrefUtils.setPref(preferenceName, preferenceValue, { + branch: preferenceBranchType, + }); + } + } else if (preferenceBranchType === "user") { + // The original value was already backed up above. + lazy.PrefUtils.setPref(preferenceName, preferenceValue, { + branch: preferenceBranchType, + }); + } else { + log.error(`Unexpected preference branch type ${preferenceBranchType}`); + } + } + PreferenceExperiments.startObserver(slug, preferences); + + /** @type {Experiment} */ + const experiment = { + slug, + actionName, + branch, + expired: false, + lastSeen: new Date().toJSON(), + preferences, + experimentType, + userFacingName, + userFacingDescription, + }; + + store.data.experiments[slug] = experiment; + store.saveSoon(); + + // Record telemetry that the experiment started + lazy.TelemetryEnvironment.setExperimentActive(slug, branch, { + type: EXPERIMENT_TYPE_PREFIX + experimentType, + }); + lazy.TelemetryEvents.sendEvent("enroll", "preference_study", slug, { + experimentType, + branch, + }); + + // Send events for any default branch preferences set that already had user + // values overriding them. + for (const preferenceName of alreadyOverriddenPrefs) { + await this.recordPrefChange({ + experiment, + preferenceName, + reason: "onEnroll", + }); + } + await this.saveStartupPrefs(); + + return experiment; + }, + + /** + * Register a preference observer that stops an experiment when the user + * modifies the preference. + * @param {string} experimentSlug + * @param {string} preferenceName + * @param {string|integer|boolean} preferenceValue + * @throws {Error} + * If an observer for the experiment is already active. + */ + startObserver(experimentSlug, preferences) { + log.debug(`PreferenceExperiments.startObserver(${experimentSlug})`); + + if (experimentObservers.has(experimentSlug)) { + throw new Error( + `An observer for the preference experiment ${experimentSlug} is already active.` + ); + } + + const observerInfo = { + preferences, + observe(aSubject, aTopic, preferenceName) { + const prefInfo = preferences[preferenceName]; + // if `preferenceName` is one of the experiment prefs but with more on + // the end (ie, foo.bar vs foo.bar.baz) then this can be triggered for + // changes we don't care about. Check for that. + if (!prefInfo) { + return; + } + const originalValue = prefInfo.preferenceValue; + const newValue = lazy.PrefUtils.getPref(preferenceName); + if (newValue !== originalValue) { + PreferenceExperiments.recordPrefChange({ + experimentSlug, + preferenceName, + reason: "observer", + }); + Services.prefs.removeObserver(preferenceName, observerInfo); + } + }, + }; + experimentObservers.set(experimentSlug, observerInfo); + for (const [preferenceName, spec] of Object.entries(preferences)) { + if (!spec.overridden) { + Services.prefs.addObserver(preferenceName, observerInfo); + } + } + }, + + /** + * Check if a preference observer is active for an experiment. + * @param {string} experimentSlug + * @return {Boolean} + */ + hasObserver(experimentSlug) { + log.debug(`PreferenceExperiments.hasObserver(${experimentSlug})`); + return experimentObservers.has(experimentSlug); + }, + + /** + * Disable a preference observer for an experiment. + * @param {string} experimentSlug + * @throws {Error} + * If there is no active observer for the experiment. + */ + stopObserver(experimentSlug) { + log.debug(`PreferenceExperiments.stopObserver(${experimentSlug})`); + + if (!experimentObservers.has(experimentSlug)) { + throw new Error( + `No observer for the preference experiment ${experimentSlug} found.` + ); + } + + const observer = experimentObservers.get(experimentSlug); + for (const preferenceName of Object.keys(observer.preferences)) { + Services.prefs.removeObserver(preferenceName, observer); + } + experimentObservers.delete(experimentSlug); + }, + + /** + * Disable all currently-active preference observers for experiments. + */ + stopAllObservers() { + log.debug("PreferenceExperiments.stopAllObservers()"); + for (const observer of experimentObservers.values()) { + for (const preferenceName of Object.keys(observer.preferences)) { + Services.prefs.removeObserver(preferenceName, observer); + } + } + experimentObservers.clear(); + }, + + /** + * Update the timestamp storing when Normandy last sent a recipe for the + * experiment. + * @param {string} experimentSlug + * @rejects {Error} + * If there is no stored experiment with the given slug. + */ + async markLastSeen(experimentSlug) { + log.debug(`PreferenceExperiments.markLastSeen(${experimentSlug})`); + + const store = await ensureStorage(); + if (!(experimentSlug in store.data.experiments)) { + throw new Error( + `Could not find a preference experiment with the slug "${experimentSlug}"` + ); + } + + store.data.experiments[experimentSlug].lastSeen = new Date().toJSON(); + store.saveSoon(); + }, + + /** + * Called when an experimental pref has changed away from its experimental + * value for the first time. + * + * One of `experiment` or `slug` must be passed. + * + * @param {object} options + * @param {Experiment} [options.experiment] + * The experiment that had a pref change. If this is passed, slug is ignored. + * @param {string} [options.slug] + * The slug of the experiment that had a pref change. This will be used to + * fetch an experiment if none was passed. + * @param {string} options.preferenceName The preference changed. + * @param {string} options.reason The reason the preference change was detected. + */ + async recordPrefChange({ + experiment = null, + experimentSlug = null, + preferenceName, + reason, + }) { + if (!experiment) { + experiment = await PreferenceExperiments.get(experimentSlug); + } + let preferenceSpecification = experiment.preferences[preferenceName]; + if (!preferenceSpecification) { + throw new PreferenceExperiments.InvalidPreferenceName( + `Preference "${preferenceName}" is not a part of experiment "${experimentSlug}"` + ); + } + + preferenceSpecification.overridden = true; + await this.update(experiment); + + lazy.TelemetryEvents.sendEvent( + "expPrefChanged", + "preference_study", + experiment.slug, + { + preferenceName, + reason, + } + ); + }, + + /** + * Stop an active experiment, deactivate preference watchers, and optionally + * reset the associated preference to its previous value. + * @param {string} experimentSlug + * @param {Object} options + * @param {boolean} [options.resetValue = true] + * If true, reset the preference to its original value prior to + * the experiment. Optional, defaults to true. + * @param {String} [options.reason = "unknown"] + * Reason that the experiment is ending. Optional, defaults to + * "unknown". + * @rejects {Error} + * If there is no stored experiment with the given slug, or if the + * experiment has already expired. + */ + async stop( + experimentSlug, + { resetValue = true, reason = "unknown", changedPref, caller } = {} + ) { + log.debug( + `PreferenceExperiments.stop(${experimentSlug}, {resetValue: ${resetValue}, reason: ${reason}, changedPref: ${changedPref}, caller: ${caller}})` + ); + if (reason === "unknown") { + log.warn(`experiment ${experimentSlug} ending for unknown reason`); + } + + const store = await ensureStorage(); + if (!(experimentSlug in store.data.experiments)) { + lazy.TelemetryEvents.sendEvent( + "unenrollFailed", + "preference_study", + experimentSlug, + { + reason: "does-not-exist", + originalReason: reason, + ...(changedPref ? { changedPref } : {}), + } + ); + throw new Error( + `Could not find a preference experiment with the slug "${experimentSlug}"` + ); + } + + const experiment = store.data.experiments[experimentSlug]; + if (experiment.expired) { + const extra = { + reason: "already-unenrolled", + originalReason: reason, + }; + if (changedPref) { + extra.changedPref = changedPref; + } + if (caller && AppConstants.NIGHTLY_BUILD) { + extra.caller = caller; + } + lazy.TelemetryEvents.sendEvent( + "unenrollFailed", + "preference_study", + experimentSlug, + extra + ); + throw new Error( + `Cannot stop preference experiment "${experimentSlug}" because it is already expired` + ); + } + + if (PreferenceExperiments.hasObserver(experimentSlug)) { + PreferenceExperiments.stopObserver(experimentSlug); + } + + if (resetValue) { + for (const [ + preferenceName, + { previousPreferenceValue, preferenceBranchType, overridden }, + ] of Object.entries(experiment.preferences)) { + // Overridden user prefs should keep their new value, even if that value + // is the same as the experimental value, since it is the value the user + // chose. + if (overridden && preferenceBranchType === "user") { + continue; + } + + const preferences = PreferenceBranchType[preferenceBranchType]; + + if (previousPreferenceValue !== null) { + lazy.PrefUtils.setPref(preferenceName, previousPreferenceValue, { + branch: preferenceBranchType, + }); + } else if (preferenceBranchType === "user") { + // Remove the "user set" value (which Shield set), but leave the default intact. + preferences.clearUserPref(preferenceName); + } else { + log.warn( + `Can't revert pref ${preferenceName} for experiment ${experimentSlug} ` + + `because it had no default value. ` + + `Preference will be reset at the next restart.` + ); + // It would seem that Services.prefs.deleteBranch() could be used for + // this, but in Normandy's case it does not work. See bug 1502410. + } + } + } + + experiment.expired = true; + if (experiment.temporaryErrorDeadline) { + experiment.temporaryErrorDeadline = null; + } + await store.saveSoon(); + + lazy.TelemetryEnvironment.setExperimentInactive(experimentSlug); + lazy.TelemetryEvents.sendEvent( + "unenroll", + "preference_study", + experimentSlug, + { + didResetValue: resetValue ? "true" : "false", + branch: experiment.branch, + reason, + ...(changedPref ? { changedPref } : {}), + } + ); + await this.saveStartupPrefs(); + Services.obs.notifyObservers( + null, + "normandy:preference-experiment:stopped", + experimentSlug + ); + }, + + /** + * Clone an experiment using knowledge of its structure to avoid + * having to serialize/deserialize it. + * + * We do this in places where return experiments so clients can't + * accidentally mutate our data underneath us. + */ + _cloneExperiment(experiment) { + return { + ...experiment, + preferences: { + ...experiment.preferences, + }, + }; + }, + + /** + * Get the experiment object for the experiment. + * @param {string} experimentSlug + * @resolves {Experiment} + * @rejects {Error} + * If no preference experiment exists with the given slug. + */ + async get(experimentSlug) { + log.debug(`PreferenceExperiments.get(${experimentSlug})`); + const store = await ensureStorage(); + if (!(experimentSlug in store.data.experiments)) { + throw new PreferenceExperiments.NotFoundError( + `Could not find a preference experiment with the slug "${experimentSlug}"` + ); + } + + return this._cloneExperiment(store.data.experiments[experimentSlug]); + }, + + /** + * Get a list of all stored experiment objects. + * @resolves {Experiment[]} + */ + async getAll() { + const store = await ensureStorage(); + return Object.values(store.data.experiments).map(experiment => + this._cloneExperiment(experiment) + ); + }, + + /** + * Get a list of experiment objects for all active experiments. + * @resolves {Experiment[]} + */ + async getAllActive() { + const store = await ensureStorage(); + return Object.values(store.data.experiments) + .filter(e => !e.expired) + .map(e => this._cloneExperiment(e)); + }, + + /** + * Check if an experiment exists with the given slug. + * @param {string} experimentSlug + * @resolves {boolean} True if the experiment exists, false if it doesn't. + */ + async has(experimentSlug) { + log.debug(`PreferenceExperiments.has(${experimentSlug})`); + const store = await ensureStorage(); + return experimentSlug in store.data.experiments; + }, + + /** + * Update an experiment in the data store. If an experiment with the given + * slug is not already in the store, an error will be thrown. + * + * @param experiment {Experiment} The experiment to update + * @param experiment.slug {String} The experiment must have a slug + */ + async update(experiment) { + const store = await ensureStorage(); + + if (!(experiment.slug in store.data.experiments)) { + throw new Error( + `Could not update a preference experiment with the slug "${experiment.slug}"` + ); + } + + store.data.experiments[experiment.slug] = experiment; + store.saveSoon(); + }, + + NotFoundError: class extends Error {}, + InvalidPreferenceName: class extends Error {}, + + /** + * These migrations should only be called from `NormandyMigrations.jsm` and tests. + */ + migrations: { + /** Move experiments into a specific key. */ + async migration01MoveExperiments(storage = null) { + if (storage === null) { + storage = await ensureStorage(); + } + if (Object.hasOwnProperty.call(storage.data, "experiments")) { + return; + } + storage.data = { + experiments: storage.data, + }; + delete storage.data.experiments.__version; + storage.saveSoon(); + }, + + /** Migrate storage.data to multi-preference format */ + async migration02MultiPreference(storage = null) { + if (storage === null) { + storage = await ensureStorage(); + } + + const oldExperiments = storage.data.experiments; + const v2Experiments = {}; + + for (let [expName, oldExperiment] of Object.entries(oldExperiments)) { + if (expName == "__version") { + // A stray "__version" entry snuck in, likely from old migrations. + // Ignore it and continue. It won't be propagated to future + // migrations, since `v2Experiments` won't have it. + continue; + } + if (oldExperiment.preferences) { + // experiment is already migrated + v2Experiments[expName] = oldExperiment; + continue; + } + v2Experiments[expName] = { + name: oldExperiment.name, + branch: oldExperiment.branch, + expired: oldExperiment.expired, + lastSeen: oldExperiment.lastSeen, + preferences: { + [oldExperiment.preferenceName]: { + preferenceBranchType: oldExperiment.preferenceBranchType, + preferenceType: oldExperiment.preferenceType, + preferenceValue: oldExperiment.preferenceValue, + previousPreferenceValue: oldExperiment.previousPreferenceValue, + }, + }, + experimentType: oldExperiment.experimentType, + }; + } + storage.data.experiments = v2Experiments; + storage.saveSoon(); + }, + + /** Add "actionName" field for experiments that don't have it. */ + async migration03AddActionName(storage = null) { + if (storage === null) { + storage = await ensureStorage(); + } + + for (const experiment of Object.values(storage.data.experiments)) { + if (!experiment.actionName) { + // Assume SinglePreferenceExperimentAction because as of this + // writing, no multi-pref experiment recipe has launched. + experiment.actionName = "SinglePreferenceExperimentAction"; + } + } + storage.saveSoon(); + }, + + async migration04RenameNameToSlug(storage = null) { + if (!storage) { + storage = await ensureStorage(); + } + // Rename "name" to "slug" to match the intended purpose of the field. + for (const experiment of Object.values(storage.data.experiments)) { + if (experiment.name && !experiment.slug) { + experiment.slug = experiment.name; + delete experiment.name; + } + } + storage.saveSoon(); + }, + + async migration05RemoveOldAction() { + const experiments = await PreferenceExperiments.getAllActive(); + for (const experiment of experiments) { + if (experiment.actionName == "SinglePreferenceExperimentAction") { + try { + await PreferenceExperiments.stop(experiment.slug, { + resetValue: true, + reason: "migration-removing-single-pref-action", + caller: "migration05RemoveOldAction", + }); + } catch (e) { + log.error( + `Stopping preference experiment ${experiment.slug} during migration failed: ${e}` + ); + } + } + } + }, + + async migration06TrackOverriddenPrefs(storage = null) { + if (!storage) { + storage = await ensureStorage(); + } + for (const experiment of Object.values(storage.data.experiments)) { + for (const [preferenceName, specification] of Object.entries( + experiment.preferences + )) { + if (specification.overridden !== undefined) { + continue; + } + specification.overridden = + lazy.PrefUtils.getPref(preferenceName) !== + specification.preferenceValue; + } + } + storage.saveSoon(); + }, + }, +}; diff --git a/toolkit/components/normandy/lib/PreferenceRollouts.sys.mjs b/toolkit/components/normandy/lib/PreferenceRollouts.sys.mjs new file mode 100644 index 0000000000..433c856603 --- /dev/null +++ b/toolkit/components/normandy/lib/PreferenceRollouts.sys.mjs @@ -0,0 +1,329 @@ +/* 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 { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { + CleanupManager: "resource://normandy/lib/CleanupManager.sys.mjs", + IndexedDB: "resource://gre/modules/IndexedDB.sys.mjs", + PrefUtils: "resource://normandy/lib/PrefUtils.sys.mjs", + TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs", + TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs", +}); + +const log = LogManager.getLogger("recipe-runner"); + +/** + * PreferenceRollouts store info about an active or expired preference rollout. + * @typedef {object} PreferenceRollout + * @property {string} slug + * Unique slug of the experiment + * @property {string} state + * The current state of the rollout: "active", "rolled-back", "graduated". + * Active means that Normandy is actively managing therollout. Rolled-back + * means that the rollout was previously active, but has been rolled back for + * this user. Graduated means that the built-in default now matches the + * rollout value, and so Normandy is no longer managing the preference. + * @property {Array<PreferenceSpec>} preferences + * An array of preferences specifications involved in the rollout. + */ + +/** + * PreferenceSpec describe how a preference should change during a rollout. + * @typedef {object} PreferenceSpec + * @property {string} preferenceName + * The preference to modify. + * @property {string} preferenceType + * Type of the preference being set. + * @property {string|integer|boolean} value + * The value to change the preference to. + * @property {string|integer|boolean} previousValue + * The value the preference would have on the default branch if this rollout + * were not active. + */ + +const STARTUP_PREFS_BRANCH = "app.normandy.startupRolloutPrefs."; +const DB_NAME = "normandy-preference-rollout"; +const STORE_NAME = "preference-rollouts"; +const DB_VERSION = 1; + +/** + * Create a new connection to the database. + */ +function openDatabase() { + return lazy.IndexedDB.open(DB_NAME, DB_VERSION, db => { + db.createObjectStore(STORE_NAME, { + keyPath: "slug", + }); + }); +} + +/** + * Cache the database connection so that it is shared among multiple operations. + */ +let databasePromise; +function getDatabase() { + if (!databasePromise) { + databasePromise = openDatabase(); + } + return databasePromise; +} + +/** + * Get a transaction for interacting with the rollout store. + * + * @param {IDBDatabase} db + * @param {String} mode Either "readonly" or "readwrite" + * + * NOTE: Methods on the store returned by this function MUST be called + * synchronously, otherwise the transaction with the store will expire. + * This is why the helper takes a database as an argument; if we fetched the + * database in the helper directly, the helper would be async and the + * transaction would expire before methods on the store were called. + */ +function getStore(db, mode) { + if (!mode) { + throw new Error("mode is required"); + } + return db.objectStore(STORE_NAME, mode); +} + +export var PreferenceRollouts = { + STATE_ACTIVE: "active", + STATE_ROLLED_BACK: "rolled-back", + STATE_GRADUATED: "graduated", + + // A set of rollout slugs that are obsolete based on the code in this build of + // Firefox. This may include things like the preference no longer being + // applicable, or the feature changing in such a way that Normandy's automatic + // graduation system cannot detect that the rollout should hand off to the + // built-in code. + GRADUATION_SET: new Set([ + "pref-webrender-intel-rollout-70-release", + "bug-1703186-rollout-http3-support-release-88-89", + "rollout-doh-nightly-rollout-to-all-us-desktop-users-nightly-74-80-bug-1613481", + "rollout-doh-beta-rollout-to-all-us-desktop-users-v2-beta-74-80-bug-1613489", + "rollout-doh-us-staged-rollout-to-all-us-desktop-users-release-73-77-bug-1586331", + "bug-1648229-rollout-comcast-steering-rollout-release-78-80", + "bug-1732206-rollout-fission-release-rollout-release-94-95", + "bug-1745237-rollout-fission-beta-96-97-rollout-beta-96-97", + "bug-1750601-rollout-doh-steering-in-canada-staggered-starting-for-release-97-98", + "bug-1758988-rollout-doh-enablment-to-new-countries-staggered-st-release-98-100", + "bug-1758818-rollout-enabling-doh-in-new-countries-staggered-sta-release-98-100", + ]), + + /** + * Update the rollout database with changes that happened during early startup. + * @param {object} rolloutPrefsChanged Map from pref name to previous pref value + */ + async recordOriginalValues(originalPreferences) { + for (const rollout of await this.getAllActive()) { + let shouldSaveRollout = false; + + // Count the number of preferences in this rollout that are now redundant. + let prefMatchingDefaultCount = 0; + + for (const prefSpec of rollout.preferences) { + const builtInDefault = originalPreferences[prefSpec.preferenceName]; + if (prefSpec.value === builtInDefault) { + prefMatchingDefaultCount++; + } + // Store the current built-in default. That way, if the preference is + // rolled back during the current session (ie, until the browser is + // shut down), the correct value will be used. + if (prefSpec.previousValue !== builtInDefault) { + prefSpec.previousValue = builtInDefault; + shouldSaveRollout = true; + } + } + + if (prefMatchingDefaultCount === rollout.preferences.length) { + // Firefox's builtin defaults have caught up to the rollout, making all + // of the rollout's changes redundant, so graduate the rollout. + await this.graduate(rollout, "all-prefs-match"); + // `this.graduate` writes the rollout to the db, so we don't need to do it anymore. + shouldSaveRollout = false; + } + + if (shouldSaveRollout) { + const db = await getDatabase(); + await getStore(db, "readwrite").put(rollout); + } + } + }, + + async init() { + lazy.CleanupManager.addCleanupHandler(() => this.saveStartupPrefs()); + + for (const rollout of await this.getAllActive()) { + if (this.GRADUATION_SET.has(rollout.slug)) { + await this.graduate(rollout, "in-graduation-set"); + continue; + } + lazy.TelemetryEnvironment.setExperimentActive( + rollout.slug, + rollout.state, + { + type: "normandy-prefrollout", + } + ); + } + }, + + /** + * Test wrapper that temporarily replaces the stored rollout data with fake + * data for testing. + */ + withTestMock({ + graduationSet = new Set(), + rollouts: prefRollouts = [], + } = {}) { + return testFunction => { + return async args => { + let db = await getDatabase(); + const oldData = await getStore(db, "readonly").getAll(); + await getStore(db, "readwrite").clear(); + await Promise.all(prefRollouts.map(r => this.add(r))); + const oldGraduationSet = this.GRADUATION_SET; + this.GRADUATION_SET = graduationSet; + + try { + await testFunction({ ...args, prefRollouts }); + } finally { + this.GRADUATION_SET = oldGraduationSet; + db = await getDatabase(); + await getStore(db, "readwrite").clear(); + const store = getStore(db, "readwrite"); + await Promise.all(oldData.map(d => store.add(d))); + } + }; + }; + }, + + /** + * Add a new rollout + * @param {PreferenceRollout} rollout + */ + async add(rollout) { + const db = await getDatabase(); + return getStore(db, "readwrite").add(rollout); + }, + + /** + * Update an existing rollout + * @param {PreferenceRollout} rollout + * @throws If a matching rollout does not exist. + */ + async update(rollout) { + if (!(await this.has(rollout.slug))) { + throw new Error( + `Tried to update ${rollout.slug}, but it doesn't already exist.` + ); + } + const db = await getDatabase(); + return getStore(db, "readwrite").put(rollout); + }, + + /** + * Update many existing rollouts. More efficient than calling `update` many + * times in a row. + * @param {Array<PreferenceRollout>} rollouts + * @throws If any of the passed rollouts have a slug that doesn't exist in the database already. + */ + async updateMany(rollouts) { + // Don't touch the database if there is nothing to do + if (!rollouts.length) { + return; + } + + // Both of the below operations use .map() instead of a normal loop becaues + // once we get the object store, we can't let it expire by spinning the + // event loop. This approach queues up all the interactions with the store + // immediately, preventing it from expiring too soon. + + const db = await getDatabase(); + let store = await getStore(db, "readonly"); + await Promise.all( + rollouts.map(async ({ slug }) => { + let existingRollout = await store.get(slug); + if (!existingRollout) { + throw new Error(`Tried to update ${slug}, but it doesn't exist.`); + } + }) + ); + + // awaiting spun the event loop, so the store is now invalid. Get a new + // store. This is also a chance to get it in readwrite mode. + store = await getStore(db, "readwrite"); + await Promise.all(rollouts.map(rollout => store.put(rollout))); + }, + + /** + * Test whether there is a rollout in storage with the given slug. + * @param {string} slug + * @returns {boolean} + */ + async has(slug) { + const db = await getDatabase(); + const rollout = await getStore(db, "readonly").get(slug); + return !!rollout; + }, + + /** + * Get a rollout by slug + * @param {string} slug + */ + async get(slug) { + const db = await getDatabase(); + return getStore(db, "readonly").get(slug); + }, + + /** Get all rollouts in the database. */ + async getAll() { + const db = await getDatabase(); + return getStore(db, "readonly").getAll(); + }, + + /** Get all rollouts in the "active" state. */ + async getAllActive() { + const rollouts = await this.getAll(); + return rollouts.filter(rollout => rollout.state === this.STATE_ACTIVE); + }, + + /** + * Save in-progress preference rollouts in a sub-branch of the normandy prefs. + * On startup, we read these to set the rollout values. + */ + async saveStartupPrefs() { + const prefBranch = Services.prefs.getBranch(STARTUP_PREFS_BRANCH); + for (const pref of prefBranch.getChildList("")) { + prefBranch.clearUserPref(pref); + } + + for (const rollout of await this.getAllActive()) { + for (const prefSpec of rollout.preferences) { + lazy.PrefUtils.setPref( + STARTUP_PREFS_BRANCH + prefSpec.preferenceName, + prefSpec.value + ); + } + } + }, + + async graduate(rollout, reason) { + log.debug(`Graduating rollout: ${rollout.slug}`); + rollout.state = this.STATE_GRADUATED; + const db = await getDatabase(); + await getStore(db, "readwrite").put(rollout); + lazy.TelemetryEvents.sendEvent( + "graduate", + "preference_rollout", + rollout.slug, + { + reason, + } + ); + }, +}; diff --git a/toolkit/components/normandy/lib/RecipeRunner.sys.mjs b/toolkit/components/normandy/lib/RecipeRunner.sys.mjs new file mode 100644 index 0000000000..7e02a3150a --- /dev/null +++ b/toolkit/components/normandy/lib/RecipeRunner.sys.mjs @@ -0,0 +1,644 @@ +/* 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; +import { LogManager } from "resource://normandy/lib/LogManager.sys.mjs"; + +const lazy = {}; + +XPCOMUtils.defineLazyServiceGetter( + lazy, + "timerManager", + "@mozilla.org/updates/timer-manager;1", + "nsIUpdateTimerManager" +); + +ChromeUtils.defineESModuleGetters(lazy, { + ActionsManager: "resource://normandy/lib/ActionsManager.sys.mjs", + BaseAction: "resource://normandy/actions/BaseAction.sys.mjs", + CleanupManager: "resource://normandy/lib/CleanupManager.sys.mjs", + ClientEnvironment: "resource://normandy/lib/ClientEnvironment.sys.mjs", + FilterExpressions: + "resource://gre/modules/components-utils/FilterExpressions.sys.mjs", + LegacyHeartbeat: "resource://normandy/lib/LegacyHeartbeat.sys.mjs", + Normandy: "resource://normandy/Normandy.sys.mjs", + NormandyApi: "resource://normandy/lib/NormandyApi.sys.mjs", + RemoteSettings: "resource://services-settings/remote-settings.sys.mjs", + RemoteSettingsClient: + "resource://services-settings/RemoteSettingsClient.sys.mjs", + Storage: "resource://normandy/lib/Storage.sys.mjs", + TargetingContext: "resource://messaging-system/targeting/Targeting.sys.mjs", + Uptake: "resource://normandy/lib/Uptake.sys.mjs", + clearTimeout: "resource://gre/modules/Timer.sys.mjs", + setTimeout: "resource://gre/modules/Timer.sys.mjs", +}); + +const log = LogManager.getLogger("recipe-runner"); +const TIMER_NAME = "recipe-client-addon-run"; +const REMOTE_SETTINGS_COLLECTION = "normandy-recipes-capabilities"; +const PREF_CHANGED_TOPIC = "nsPref:changed"; + +const RUN_INTERVAL_PREF = "app.normandy.run_interval_seconds"; +const FIRST_RUN_PREF = "app.normandy.first_run"; +const SHIELD_ENABLED_PREF = "app.normandy.enabled"; +const DEV_MODE_PREF = "app.normandy.dev_mode"; +const API_URL_PREF = "app.normandy.api_url"; +const LAZY_CLASSIFY_PREF = "app.normandy.experiments.lazy_classify"; +const ONSYNC_SKEW_SEC_PREF = "app.normandy.onsync_skew_sec"; + +// Timer last update preference. +// see https://searchfox.org/mozilla-central/rev/11cfa0462/toolkit/components/timermanager/UpdateTimerManager.jsm#8 +const TIMER_LAST_UPDATE_PREF = `app.update.lastUpdateTime.${TIMER_NAME}`; + +const PREFS_TO_WATCH = [RUN_INTERVAL_PREF, SHIELD_ENABLED_PREF, API_URL_PREF]; + +ChromeUtils.defineLazyGetter(lazy, "gRemoteSettingsClient", () => { + return lazy.RemoteSettings(REMOTE_SETTINGS_COLLECTION); +}); + +/** + * cacheProxy returns an object Proxy that will memoize properties of the target. + */ +function cacheProxy(target) { + const cache = new Map(); + return new Proxy(target, { + get(target, prop, receiver) { + if (!cache.has(prop)) { + cache.set(prop, target[prop]); + } + return cache.get(prop); + }, + set(target, prop, value, receiver) { + cache.set(prop, value); + return true; + }, + has(target, prop) { + return cache.has(prop) || prop in target; + }, + }); +} + +export var RecipeRunner = { + initializedPromise: Promise.withResolvers(), + + async init() { + this.running = false; + this.enabled = null; + this.loadFromRemoteSettings = false; + this._syncSkewTimeout = null; + + this.checkPrefs(); // sets this.enabled + this.watchPrefs(); + this.setUpRemoteSettings(); + + // Here "first run" means the first run this profile has ever done. This + // preference is set to true at the end of this function, and never reset to + // false. + const firstRun = Services.prefs.getBoolPref(FIRST_RUN_PREF, true); + + // If we've seen a build ID from a previous run that doesn't match the + // current build ID, run immediately. This is probably an upgrade or + // downgrade, which may cause recipe eligibility to change. + let hasNewBuildID = + Services.appinfo.lastAppBuildID != null && + Services.appinfo.lastAppBuildID != Services.appinfo.appBuildID; + + // Dev mode is a mode used for development and QA that bypasses the normal + // timer function of Normandy, to make testing more convenient. + const devMode = Services.prefs.getBoolPref(DEV_MODE_PREF, false); + + if (this.enabled && (devMode || firstRun || hasNewBuildID)) { + // In dev mode, if remote settings is enabled, force an immediate sync + // before running. This ensures that the latest data is used for testing. + // This is not needed for the first run case, because remote settings + // already handles empty collections well. + if (devMode) { + await lazy.gRemoteSettingsClient.sync(); + } + let trigger; + if (devMode) { + trigger = "devMode"; + } else if (firstRun) { + trigger = "firstRun"; + } else if (hasNewBuildID) { + trigger = "newBuildID"; + } + + await this.run({ trigger }); + } + + // Update the firstRun pref, to indicate that Normandy has run at least once + // on this profile. + if (firstRun) { + Services.prefs.setBoolPref(FIRST_RUN_PREF, false); + } + + this.initializedPromise.resolve(); + }, + + enable() { + if (this.enabled) { + return; + } + this.registerTimer(); + this.enabled = true; + }, + + disable() { + if (this.enabled) { + this.unregisterTimer(); + } + // this.enabled may be null, so always set it to false + this.enabled = false; + }, + + /** Watch for prefs to change, and call this.observer when they do */ + watchPrefs() { + for (const pref of PREFS_TO_WATCH) { + Services.prefs.addObserver(pref, this); + } + + lazy.CleanupManager.addCleanupHandler(this.unwatchPrefs.bind(this)); + }, + + unwatchPrefs() { + for (const pref of PREFS_TO_WATCH) { + Services.prefs.removeObserver(pref, this); + } + }, + + /** When prefs change, this is fired */ + observe(subject, topic, data) { + switch (topic) { + case PREF_CHANGED_TOPIC: { + const prefName = data; + + switch (prefName) { + case RUN_INTERVAL_PREF: + this.updateRunInterval(); + break; + + // explicit fall-through + case SHIELD_ENABLED_PREF: + case API_URL_PREF: + this.checkPrefs(); + break; + + default: + log.debug( + `Observer fired with unexpected pref change: ${prefName}` + ); + } + + break; + } + } + }, + + checkPrefs() { + if (!Services.prefs.getBoolPref(SHIELD_ENABLED_PREF)) { + log.debug( + `Disabling Shield because ${SHIELD_ENABLED_PREF} is set to false` + ); + this.disable(); + return; + } + + const apiUrl = Services.prefs.getCharPref(API_URL_PREF); + if (!apiUrl) { + log.warn(`Disabling Shield because ${API_URL_PREF} is not set.`); + this.disable(); + return; + } + if (!apiUrl.startsWith("https://")) { + log.warn( + `Disabling Shield because ${API_URL_PREF} is not an HTTPS url: ${apiUrl}.` + ); + this.disable(); + return; + } + + log.debug(`Enabling Shield`); + this.enable(); + }, + + registerTimer() { + this.updateRunInterval(); + lazy.CleanupManager.addCleanupHandler(() => + lazy.timerManager.unregisterTimer(TIMER_NAME) + ); + }, + + unregisterTimer() { + lazy.timerManager.unregisterTimer(TIMER_NAME); + }, + + setUpRemoteSettings() { + if (this._alreadySetUpRemoteSettings) { + return; + } + this._alreadySetUpRemoteSettings = true; + + if (!this._onSync) { + this._onSync = this.onSync.bind(this); + } + lazy.gRemoteSettingsClient.on("sync", this._onSync); + + lazy.CleanupManager.addCleanupHandler(() => { + lazy.gRemoteSettingsClient.off("sync", this._onSync); + this._alreadySetUpRemoteSettings = false; + }); + }, + + /** Called when our Remote Settings collection is updated */ + async onSync() { + if (!this.enabled) { + return; + } + + // Delay the Normandy run by a random amount, determined by preference. + // This helps alleviate server load, since we don't have a thundering + // herd of users trying to update all at once. + if (this._syncSkewTimeout) { + lazy.clearTimeout(this._syncSkewTimeout); + } + let minSkewSec = 1; // this is primarily is to avoid race conditions in tests + let maxSkewSec = Services.prefs.getIntPref(ONSYNC_SKEW_SEC_PREF, 0); + if (maxSkewSec >= minSkewSec) { + let skewMillis = + (minSkewSec + Math.random() * (maxSkewSec - minSkewSec)) * 1000; + log.debug( + `Delaying on-sync Normandy run for ${Math.floor( + skewMillis / 1000 + )} seconds` + ); + this._syncSkewTimeout = lazy.setTimeout( + () => this.run({ trigger: "sync" }), + skewMillis + ); + } else { + log.debug(`Not skewing on-sync Normandy run`); + await this.run({ trigger: "sync" }); + } + }, + + updateRunInterval() { + // Run once every `runInterval` wall-clock seconds. This is managed by setting a "last ran" + // timestamp, and running if it is more than `runInterval` seconds ago. Even with very short + // intervals, the timer will only fire at most once every few minutes. + const runInterval = Services.prefs.getIntPref(RUN_INTERVAL_PREF); + lazy.timerManager.registerTimer(TIMER_NAME, () => this.run(), runInterval); + }, + + async run({ trigger = "timer" } = {}) { + if (this.running) { + // Do nothing if already running. + return; + } + this.running = true; + + await lazy.Normandy.defaultPrefsHaveBeenApplied.promise; + + try { + this.running = true; + Services.obs.notifyObservers(null, "recipe-runner:start"); + + if (this._syncSkewTimeout) { + lazy.clearTimeout(this._syncSkewTimeout); + this._syncSkewTimeout = null; + } + + this.clearCaches(); + // Unless lazy classification is enabled, prep the classify cache. + if (!Services.prefs.getBoolPref(LAZY_CLASSIFY_PREF, false)) { + try { + await lazy.ClientEnvironment.getClientClassification(); + } catch (err) { + // Try to go on without this data; the filter expressions will + // gracefully fail without this info if they need it. + } + } + + // Fetch recipes before execution in case we fail and exit early. + let recipesAndSignatures; + try { + recipesAndSignatures = await lazy.gRemoteSettingsClient.get({ + // Do not return an empty list if an error occurs. + emptyListFallback: false, + }); + } catch (e) { + await lazy.Uptake.reportRunner(lazy.Uptake.RUNNER_SERVER_ERROR); + return; + } + + const actionsManager = new lazy.ActionsManager(); + + const legacyHeartbeat = lazy.LegacyHeartbeat.getHeartbeatRecipe(); + const noRecipes = + !recipesAndSignatures.length && legacyHeartbeat === null; + + // Execute recipes, if we have any. + if (noRecipes) { + log.debug("No recipes to execute"); + } else { + for (const { recipe, signature } of recipesAndSignatures) { + let suitability = await this.getRecipeSuitability(recipe, signature); + await actionsManager.processRecipe(recipe, suitability); + } + + if (legacyHeartbeat !== null) { + await actionsManager.processRecipe( + legacyHeartbeat, + lazy.BaseAction.suitability.FILTER_MATCH + ); + } + } + + await actionsManager.finalize({ noRecipes }); + + await lazy.Uptake.reportRunner(lazy.Uptake.RUNNER_SUCCESS); + Services.obs.notifyObservers(null, "recipe-runner:end"); + } finally { + this.running = false; + if (trigger != "timer") { + // `run()` was executed outside the scheduled timer. + // Update the last time it ran to make sure it is rescheduled later. + const lastUpdateTime = Math.round(Date.now() / 1000); + Services.prefs.setIntPref(TIMER_LAST_UPDATE_PREF, lastUpdateTime); + } + } + }, + + getFilterContext(recipe) { + const environment = cacheProxy(lazy.ClientEnvironment); + environment.recipe = { + id: recipe.id, + arguments: recipe.arguments, + }; + return { + env: environment, + // Backwards compatibility -- see bug 1477255. + normandy: environment, + }; + }, + + /** + * Return the set of capabilities this runner has. + * + * This is used to pre-filter recipes that aren't compatible with this client. + * + * @returns {Set<String>} The capabilities supported by this client. + */ + getCapabilities() { + let capabilities = new Set([ + "capabilities-v1", // The initial version of the capabilities system. + ]); + + // Get capabilities from ActionsManager. + for (const actionCapability of lazy.ActionsManager.getCapabilities()) { + capabilities.add(actionCapability); + } + + // Add a capability for each transform available to JEXL. + for (const transform of lazy.FilterExpressions.getAvailableTransforms()) { + capabilities.add(`jexl.transform.${transform}`); + } + + // Add two capabilities for each top level key available in the context: one + // for the `normandy.` namespace, and another for the `env.` namespace. + capabilities.add("jexl.context.env"); + capabilities.add("jexl.context.normandy"); + let env = lazy.ClientEnvironment; + while (env && env.name) { + // Walk up the class chain for ClientEnvironment, collecting applicable + // properties as we go. Stop when we get to an unnamed object, which is + // usually just a plain function is the super class of a class that doesn't + // extend anything. Also stop if we get to an undefined object, just in + // case. + for (const [name, descriptor] of Object.entries( + Object.getOwnPropertyDescriptors(env) + )) { + // All of the properties we are looking for are are static getters (so + // will have a truthy `get` property) and are defined on the class, so + // will be configurable + if (descriptor.configurable && descriptor.get) { + capabilities.add(`jexl.context.env.${name}`); + capabilities.add(`jexl.context.normandy.${name}`); + } + } + // Check for the next parent + env = Object.getPrototypeOf(env); + } + + return capabilities; + }, + + /** + * Decide if a recipe is suitable to run, and returns a value from + * `BaseAction.suitability`. + * + * This checks several things in order: + * - recipe signature + * - capabilities + * - filter expression + * + * If the provided signature does not match the provided recipe, then + * `SIGNATURE_ERROR` is returned. Recipes with this suitability should not be + * trusted. These recipes are included so that temporary signature errors on + * the server can be handled intelligently by actions. + * + * Capabilities are a simple set of strings in the recipe. If the Normandy + * client has all of the capabilities listed, then execution continues. If + * not, then `CAPABILITY_MISMATCH` is returned. Recipes with this suitability + * should be considered incompatible and treated with caution. + * + * If the capabilities check passes, then the filter expression is evaluated + * against the current environment. The result of the expression is cast to a + * boolean. If it is true, then `FILTER_MATCH` is returned. If not, then + * `FILTER_MISMATCH` is returned. + * + * If there is an error while evaluating the recipe's filter, `FILTER_ERROR` + * is returned instead. + * + * @param {object} recipe + * @param {object} signature + * @param {string} recipe.filter_expression The expression to evaluate against the environment. + * @param {Set<String>} runnerCapabilities The capabilities provided by this runner. + * @return {Promise<BaseAction.suitability>} The recipe's suitability + */ + async getRecipeSuitability(recipe, signature) { + let generator = this.getAllSuitabilities(recipe, signature); + // For our purposes, only the first suitability matters, so pull the first + // value out of the async generator. This additionally guarantees if we fail + // a security or compatibility check, we won't continue to run other checks, + // which is good for the general case of running recipes. + let { value: suitability } = await generator.next(); + switch (suitability) { + case lazy.BaseAction.suitability.SIGNATURE_ERROR: { + await lazy.Uptake.reportRecipe( + recipe, + lazy.Uptake.RECIPE_INVALID_SIGNATURE + ); + break; + } + + case lazy.BaseAction.suitability.CAPABILITIES_MISMATCH: { + await lazy.Uptake.reportRecipe( + recipe, + lazy.Uptake.RECIPE_INCOMPATIBLE_CAPABILITIES + ); + break; + } + + case lazy.BaseAction.suitability.FILTER_MATCH: { + // No telemetry needs to be sent for this right now. + break; + } + + case lazy.BaseAction.suitability.FILTER_MISMATCH: { + // This represents a terminal state for the given recipe, so + // report its outcome. Others are reported when executed in + // ActionsManager. + await lazy.Uptake.reportRecipe( + recipe, + lazy.Uptake.RECIPE_DIDNT_MATCH_FILTER + ); + break; + } + + case lazy.BaseAction.suitability.FILTER_ERROR: { + await lazy.Uptake.reportRecipe( + recipe, + lazy.Uptake.RECIPE_FILTER_BROKEN + ); + break; + } + + case lazy.BaseAction.suitability.ARGUMENTS_INVALID: { + // This shouldn't ever occur, since the arguments schema is checked by + // BaseAction itself. + throw new Error(`Shouldn't get ${suitability} in RecipeRunner`); + } + + default: { + throw new Error(`Unexpected recipe suitability ${suitability}`); + } + } + + return suitability; + }, + + /** + * Some uses cases, such as Normandy Devtools, want the status of all + * suitabilities, not only the most important one. This checks the cases of + * suitabilities in order from most blocking to least blocking. The first + * yielded is the "primary" suitability to pass on to actions. + * + * If this function yields only [FILTER_MATCH], then the recipe fully matches + * and should be executed. If any other statuses are yielded, then the recipe + * should not be executed as normal. + * + * This is a generator so that the execution can be halted as needed. For + * example, after receiving a signature error, a caller can stop advancing + * the iterator to avoid exposing the browser to unneeded risk. + */ + async *getAllSuitabilities(recipe, signature) { + try { + await lazy.NormandyApi.verifyObjectSignature(recipe, signature, "recipe"); + } catch (e) { + yield lazy.BaseAction.suitability.SIGNATURE_ERROR; + } + + const runnerCapabilities = this.getCapabilities(); + if (Array.isArray(recipe.capabilities)) { + for (const recipeCapability of recipe.capabilities) { + if (!runnerCapabilities.has(recipeCapability)) { + log.debug( + `Recipe "${recipe.name}" requires unknown capabilities. ` + + `Recipe capabilities: ${JSON.stringify(recipe.capabilities)}. ` + + `Local runner capabilities: ${JSON.stringify( + Array.from(runnerCapabilities) + )}` + ); + yield lazy.BaseAction.suitability.CAPABILITIES_MISMATCH; + } + } + } + + const context = this.getFilterContext(recipe); + const targetingContext = new lazy.TargetingContext(); + try { + if (await targetingContext.eval(recipe.filter_expression, context)) { + yield lazy.BaseAction.suitability.FILTER_MATCH; + } else { + yield lazy.BaseAction.suitability.FILTER_MISMATCH; + } + } catch (err) { + log.error( + `Error checking filter for "${recipe.name}". Filter: [${recipe.filter_expression}]. Error: "${err}"` + ); + yield lazy.BaseAction.suitability.FILTER_ERROR; + } + }, + + /** + * Clear all caches of systems used by RecipeRunner, in preparation + * for a clean run. + */ + clearCaches() { + lazy.ClientEnvironment.clearClassifyCache(); + lazy.NormandyApi.clearIndexCache(); + }, + + /** + * Clear out cached state and fetch/execute recipes from the given + * API url. This is used mainly by the mock-recipe-server JS that is + * executed in the browser console. + */ + async testRun(baseApiUrl) { + const oldApiUrl = Services.prefs.getCharPref(API_URL_PREF); + Services.prefs.setCharPref(API_URL_PREF, baseApiUrl); + + try { + lazy.Storage.clearAllStorage(); + this.clearCaches(); + await this.run(); + } finally { + Services.prefs.setCharPref(API_URL_PREF, oldApiUrl); + this.clearCaches(); + } + }, + + /** + * Offer a mechanism to get access to the lazily-instantiated + * gRemoteSettingsClient, because if someone instantiates it + * themselves, it won't have the options we provided in this module, + * and it will prevent instantiation by this module later. + * + * This is only meant to be used in testing, where it is a + * convenient hook to store data in the underlying remote-settings + * collection. + */ + get _remoteSettingsClientForTesting() { + return lazy.gRemoteSettingsClient; + }, + + migrations: { + /** + * Delete the now-unused collection of recipes, since we are using the + * "normandy-recipes-capabilities" collection now. + */ + async migration01RemoveOldRecipesCollection() { + // Don't bother to open IDB and clear on clean profiles. + const lastCheckPref = + "services.settings.main.normandy-recipes.last_check"; + if (Services.prefs.prefHasUserValue(lastCheckPref)) { + // We instantiate a client, but it won't take part of sync. + const client = new lazy.RemoteSettingsClient("normandy-recipes"); + await client.db.clear(); + Services.prefs.clearUserPref(lastCheckPref); + } + }, + }, +}; diff --git a/toolkit/components/normandy/lib/ShieldPreferences.sys.mjs b/toolkit/components/normandy/lib/ShieldPreferences.sys.mjs new file mode 100644 index 0000000000..730febe975 --- /dev/null +++ b/toolkit/components/normandy/lib/ShieldPreferences.sys.mjs @@ -0,0 +1,78 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + BranchedAddonStudyAction: + "resource://normandy/actions/BranchedAddonStudyAction.sys.mjs", + AddonStudies: "resource://normandy/lib/AddonStudies.sys.mjs", + CleanupManager: "resource://normandy/lib/CleanupManager.sys.mjs", + PreferenceExperiments: + "resource://normandy/lib/PreferenceExperiments.sys.mjs", +}); + +const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed"; // from modules/libpref/nsIPrefBranch.idl +const PREF_OPT_OUT_STUDIES_ENABLED = "app.shield.optoutstudies.enabled"; + +/** + * Handles Shield-specific preferences, including their UI. + */ +export var ShieldPreferences = { + init() { + // Watch for changes to the Opt-out pref + Services.prefs.addObserver(PREF_OPT_OUT_STUDIES_ENABLED, this); + + lazy.CleanupManager.addCleanupHandler(() => { + Services.prefs.removeObserver(PREF_OPT_OUT_STUDIES_ENABLED, this); + }); + }, + + observe(subject, topic, data) { + switch (topic) { + case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID: + this.observePrefChange(data); + break; + } + }, + + async observePrefChange(prefName) { + let prefValue; + switch (prefName) { + // If the opt-out pref changes to be false, disable all current studies. + case PREF_OPT_OUT_STUDIES_ENABLED: { + prefValue = Services.prefs.getBoolPref(PREF_OPT_OUT_STUDIES_ENABLED); + if (!prefValue) { + const action = new lazy.BranchedAddonStudyAction(); + const studyPromises = (await lazy.AddonStudies.getAll()).map( + study => { + if (!study.active) { + return null; + } + return action.unenroll(study.recipeId, "general-opt-out"); + } + ); + + const experimentPromises = ( + await lazy.PreferenceExperiments.getAll() + ).map(experiment => { + if (experiment.expired) { + return null; + } + return lazy.PreferenceExperiments.stop(experiment.slug, { + reason: "general-opt-out", + caller: "observePrefChange::general-opt-out", + }); + }); + + const allPromises = studyPromises + .concat(experimentPromises) + .map(p => p && p.catch(err => console.error(err))); + await Promise.all(allPromises); + } + break; + } + } + }, +}; diff --git a/toolkit/components/normandy/lib/Storage.sys.mjs b/toolkit/components/normandy/lib/Storage.sys.mjs new file mode 100644 index 0000000000..d03f3a2b98 --- /dev/null +++ b/toolkit/components/normandy/lib/Storage.sys.mjs @@ -0,0 +1,88 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + JSONFile: "resource://gre/modules/JSONFile.sys.mjs", +}); + +// Lazy-load JSON file that backs Storage instances. +ChromeUtils.defineLazyGetter(lazy, "lazyStore", async function () { + const path = PathUtils.join( + PathUtils.profileDir, + "shield-recipe-client.json" + ); + const store = new lazy.JSONFile({ path }); + await store.load(); + return store; +}); + +export var Storage = class { + constructor(prefix) { + this.prefix = prefix; + } + + /** + * Clear ALL storage data and save to the disk. + */ + static async clearAllStorage() { + const store = await lazy.lazyStore; + store.data = {}; + store.saveSoon(); + } + + /** + * Sets an item in the prefixed storage. + * @returns {Promise} + * @resolves With the stored value, or null. + * @rejects Javascript exception. + */ + async getItem(name) { + const store = await lazy.lazyStore; + const namespace = store.data[this.prefix] || {}; + return namespace[name] || null; + } + + /** + * Sets an item in the prefixed storage. + * @returns {Promise} + * @resolves When the operation is completed successfully + * @rejects Javascript exception. + */ + async setItem(name, value) { + const store = await lazy.lazyStore; + if (!(this.prefix in store.data)) { + store.data[this.prefix] = {}; + } + store.data[this.prefix][name] = value; + store.saveSoon(); + } + + /** + * Removes a single item from the prefixed storage. + * @returns {Promise} + * @resolves When the operation is completed successfully + * @rejects Javascript exception. + */ + async removeItem(name) { + const store = await lazy.lazyStore; + if (this.prefix in store.data) { + delete store.data[this.prefix][name]; + store.saveSoon(); + } + } + + /** + * Clears all storage for the prefix. + * @returns {Promise} + * @resolves When the operation is completed successfully + * @rejects Javascript exception. + */ + async clear() { + const store = await lazy.lazyStore; + store.data[this.prefix] = {}; + store.saveSoon(); + } +}; diff --git a/toolkit/components/normandy/lib/TelemetryEvents.sys.mjs b/toolkit/components/normandy/lib/TelemetryEvents.sys.mjs new file mode 100644 index 0000000000..771072dd9a --- /dev/null +++ b/toolkit/components/normandy/lib/TelemetryEvents.sys.mjs @@ -0,0 +1,28 @@ +/* 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/. */ + +const TELEMETRY_CATEGORY = "normandy"; + +export const TelemetryEvents = { + init() { + Services.telemetry.setEventRecordingEnabled(TELEMETRY_CATEGORY, true); + }, + + sendEvent(method, object, value, extra) { + for (const val of Object.values(extra)) { + if (val == null) { + throw new Error( + "Extra parameters in telemetry events must not be null" + ); + } + } + Services.telemetry.recordEvent( + TELEMETRY_CATEGORY, + method, + object, + value, + extra + ); + }, +}; diff --git a/toolkit/components/normandy/lib/Uptake.sys.mjs b/toolkit/components/normandy/lib/Uptake.sys.mjs new file mode 100644 index 0000000000..73f3eb2068 --- /dev/null +++ b/toolkit/components/normandy/lib/Uptake.sys.mjs @@ -0,0 +1,67 @@ +/* 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 { UptakeTelemetry } from "resource://services-common/uptake-telemetry.sys.mjs"; + +const COMPONENT = "normandy"; + +export var Uptake = { + // Action uptake + ACTION_NETWORK_ERROR: UptakeTelemetry.STATUS.NETWORK_ERROR, + ACTION_PRE_EXECUTION_ERROR: UptakeTelemetry.STATUS.CUSTOM_1_ERROR, + ACTION_POST_EXECUTION_ERROR: UptakeTelemetry.STATUS.CUSTOM_2_ERROR, + ACTION_SERVER_ERROR: UptakeTelemetry.STATUS.SERVER_ERROR, + ACTION_SUCCESS: UptakeTelemetry.STATUS.SUCCESS, + + // Per-recipe uptake + RECIPE_ACTION_DISABLED: UptakeTelemetry.STATUS.CUSTOM_1_ERROR, + RECIPE_DIDNT_MATCH_FILTER: UptakeTelemetry.STATUS.BACKOFF, + RECIPE_INCOMPATIBLE_CAPABILITIES: UptakeTelemetry.STATUS.BACKOFF, + RECIPE_EXECUTION_ERROR: UptakeTelemetry.STATUS.APPLY_ERROR, + RECIPE_FILTER_BROKEN: UptakeTelemetry.STATUS.CONTENT_ERROR, + RECIPE_ARGUMENTS_INVALID: UptakeTelemetry.STATUS.CONTENT_ERROR, + RECIPE_INVALID_ACTION: UptakeTelemetry.STATUS.DOWNLOAD_ERROR, + RECIPE_SUCCESS: UptakeTelemetry.STATUS.SUCCESS, + RECIPE_INVALID_SIGNATURE: UptakeTelemetry.STATUS.SIGNATURE_ERROR, + + // Uptake for the runner as a whole + RUNNER_NETWORK_ERROR: UptakeTelemetry.STATUS.NETWORK_ERROR, + RUNNER_SERVER_ERROR: UptakeTelemetry.STATUS.SERVER_ERROR, + RUNNER_SUCCESS: UptakeTelemetry.STATUS.SUCCESS, + + async _report(status, source) { + // Telemetry doesn't help us much with error detection, so do some here. + if (!status) { + throw new Error( + `Uptake status is required (got "${JSON.stringify(status)}"` + ); + } + if (!source) { + throw new Error( + `Uptake source is required (got "${JSON.stringify(status)}` + ); + } + await UptakeTelemetry.report(COMPONENT, status, { + source: `${COMPONENT}/${source}`, + }); + }, + + async reportRunner(status) { + await Uptake._report(status, "runner"); + }, + + async reportRecipe(recipe, status) { + await Uptake._report(status, `recipe/${recipe.id}`); + const revisionId = parseInt(recipe.revision_id, 10); + Services.telemetry.keyedScalarSet( + "normandy.recipe_freshness", + recipe.id, + revisionId + ); + }, + + async reportAction(actionName, status) { + await Uptake._report(status, `action/${actionName}`); + }, +}; diff --git a/toolkit/components/normandy/moz.build b/toolkit/components/normandy/moz.build new file mode 100644 index 0000000000..e060f6ac6f --- /dev/null +++ b/toolkit/components/normandy/moz.build @@ -0,0 +1,28 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +with Files("**"): + BUG_COMPONENT = ("Firefox", "Normandy Client") + +JAR_MANIFESTS += ["jar.mn"] + +EXTRA_JS_MODULES += [ + "ShieldContentProcess.sys.mjs", +] + +TESTING_JS_MODULES += [ + "test/NormandyTestUtils.sys.mjs", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +SPHINX_TREES["normandy"] = "docs" + +TEST_DIRS += ["test/browser"] + +XPCSHELL_TESTS_MANIFESTS += ["test/unit/xpcshell.toml"] diff --git a/toolkit/components/normandy/schemas/LegacyHeartbeat.schema.json b/toolkit/components/normandy/schemas/LegacyHeartbeat.schema.json new file mode 100644 index 0000000000..6915fc9b71 --- /dev/null +++ b/toolkit/components/normandy/schemas/LegacyHeartbeat.schema.json @@ -0,0 +1,73 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Legacy (Normandy) Heartbeat, via Nimbus", + "description": "The schema for the Legacy Heartbeat Nimbus feature.", + "type": "object", + "properties": { + "survey": { + "$comment": "Hearbeat arguments are nested under survey to prevent simultaneous rollouts and experiments from overriding eachothers optional variables", + "type": "object", + "properties": { + "repeatOption": { + "type": "string", + "enum": ["once", "xdays", "nag"], + "description": "Determines how often a prompt is shown executes.", + "default": "once" + }, + "repeatEvery": { + "description": "For repeatOption=xdays, how often (in days) the prompt is displayed.", + "default": null, + "type": ["number", "null"] + }, + "includeTelemetryUUID": { + "type": "boolean", + "description": "Include unique user ID in post-answer-url and Telemetry", + "default": false + }, + "surveyId": { + "description": "Slug uniquely identifying this survey in telemetry", + "type": "string" + }, + "message": { + "description": "Message to show to the user", + "type": "string" + }, + "engagementButtonLabel": { + "description": "Text for the engagement button. If specified, this button will be shown instead of rating stars.", + "default": null, + "type": ["string", "null"] + }, + "thanksMessage": { + "description": "Thanks message to show to the user after they've rated Firefox", + "type": "string" + }, + "postAnswerUrl": { + "description": "URL to redirect the user to after rating Firefox or clicking the engagement button", + "default": null, + "type": ["string", "null"] + }, + "learnMoreMessage": { + "description": "Message to show to the user to learn more", + "default": null, + "type": ["string", "null"] + }, + "learnMoreUrl": { + "description": "URL to show to the user when they click Learn More", + "default": null, + "type": ["string", "null"] + } + }, + "required": [ + "surveyId", + "message", + "thanksMessage", + "postAnswerUrl", + "learnMoreMessage", + "learnMoreUrl" + ], + "additionalProperties": false + } + }, + "required": ["survey"], + "additionalProperties": false +} diff --git a/toolkit/components/normandy/skin/shared/Heartbeat.css b/toolkit/components/normandy/skin/shared/Heartbeat.css new file mode 100644 index 0000000000..009bf56019 --- /dev/null +++ b/toolkit/components/normandy/skin/shared/Heartbeat.css @@ -0,0 +1,106 @@ +/* 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/. */ + +/* Notification overrides for Heartbeat UI */ + +@keyframes pulse-onshow { + 0% { + opacity: 0; + transform: scale(1); + } + + 25% { + opacity: 1; + transform: scale(1.1); + } + + 50% { + transform: scale(1); + } + + 75% { + transform: scale(1.1); + } + + 100% { + transform: scale(1); + } +} + +@keyframes pulse-twice { + 0% { + transform: scale(1.1); + } + + 50% { + transform: scale(0.8); + } + + 100% { + transform: scale(1); + } +} + +.messageImage.heartbeat { + /* Needed for the animation to not get clipped when pulsing. */ + margin-inline: 8px; +} + +.messageImage.heartbeat.pulse-onshow { + animation-duration: 1.5s; + animation-iteration-count: 1; + animation-name: pulse-onshow; + animation-timing-function: cubic-bezier(0.7, 1.8, 0.9, 1.1); +} + +.messageImage.heartbeat.pulse-twice { + animation-duration: 1s; + animation-iteration-count: 2; + animation-name: pulse-twice; + animation-timing-function: linear; +} + +/* Learn More link styles */ +.heartbeat > hbox > .text-link { + margin-inline-start: 0 !important; +} + +.heartbeat > hbox > .text-link:hover { + text-decoration: none !important; +} + +/* Heartbeat UI Rating Star Classes */ +#star-rating-container { + display: flex; + margin-block: 4px; +} + +#star-rating-container > #star5 { + order: 5; +} + +#star-rating-container > #star4 { + order: 4; +} + +#star-rating-container > #star3 { + order: 3; +} + +#star-rating-container > #star2 { + order: 2; +} + +#star-rating-container > .star-x { + background: url("resource://normandy/skin/shared/heartbeat-star-off.svg"); + cursor: pointer; + height: 16px; + margin-inline-end: 4px !important; /* Overrides the margin-inline-end for all platforms defined in the .plain class */ + width: 16px; +} + +#star-rating-container > .star-x:hover, +#star-rating-container > .star-x:hover ~ .star-x { + background: url("resource://normandy/skin/shared/heartbeat-star-lit.svg"); +} diff --git a/toolkit/components/normandy/skin/shared/heartbeat-icon.svg b/toolkit/components/normandy/skin/shared/heartbeat-icon.svg new file mode 100644 index 0000000000..db2cb5f034 --- /dev/null +++ b/toolkit/components/normandy/skin/shared/heartbeat-icon.svg @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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/. --> +<svg width="288px" height="248px" viewBox="0 0 288 248" xmlns="http://www.w3.org/2000/svg"> + <path fill="#d74345" d="M144,248.571429 C141.214272,248.571429 138.857152,247.607152 136.928571,245.678571 L36.6428571,148.928571 C35.5714232,148.071424 34.0982237,146.678581 32.2232143,144.75 C30.3482049,142.821419 27.3750204,139.312525 23.3035714,134.223214 C19.2321225,129.133903 15.5893018,123.910741 12.375,118.553571 C9.16069821,113.196402 6.29465545,106.714324 3.77678571,99.1071429 C1.25891598,91.499962 0,84.1071788 0,76.9285714 C0,53.357025 6.80350339,34.9286379 20.4107143,21.6428571 C34.0179252,8.35707643 52.8213086,1.71428571 76.8214286,1.71428571 C83.4643189,1.71428571 90.2410369,2.86605991 97.1517857,5.16964286 C104.062535,7.4732258 110.491042,10.5803376 116.4375,14.4910714 C122.383958,18.4018053 127.499979,22.0714114 131.785714,25.5 C136.07145,28.9285886 140.142838,32.5714093 144,36.4285714 C147.857162,32.5714093 151.92855,28.9285886 156.214286,25.5 C160.500021,22.0714114 165.616042,18.4018053 171.5625,14.4910714 C177.508958,10.5803376 183.937465,7.4732258 190.848214,5.16964286 C197.758963,2.86605991 204.535681,1.71428571 211.178571,1.71428571 C235.178691,1.71428571 253.982075,8.35707643 267.589286,21.6428571 C281.196497,34.9286379 288,53.357025 288,76.9285714 C288,100.607261 275.732266,124.714163 251.196429,149.25 L151.071429,245.678571 C149.142847,247.607152 146.785728,248.571429 144,248.571429 L144,248.571429 Z" transform="translate(0,-1)"/> + <g transform="translate(0,-0.29)"> + <mask id="mask" fill="#fff"> + <path d="M144,246.857143 C141.214272,246.857143 138.857152,245.892867 136.928571,243.964286 L36.6428571,147.214286 C35.5714232,146.357139 34.0982237,144.964295 32.2232143,143.035714 C30.3482049,141.107133 27.3750204,137.59824 23.3035714,132.508929 C19.2321225,127.419617 15.5893018,122.196455 12.375,116.839286 C9.16069821,111.482116 6.29465545,105.000038 3.77678571,97.3928571 C1.25891598,89.7856763 0,82.392893 0,75.2142857 C0,51.6427393 6.80350339,33.2143521 20.4107143,19.9285714 C34.0179252,6.64279071 52.8213086,0 76.8214286,0 C83.4643189,0 90.2410369,1.1517742 97.1517857,3.45535714 C104.062535,5.75894009 110.491042,8.86605187 116.4375,12.7767857 C122.383958,16.6875196 127.499979,20.3571257 131.785714,23.7857143 C136.07145,27.2143029 140.142838,30.8571236 144,34.7142857 C147.857162,30.8571236 151.92855,27.2143029 156.214286,23.7857143 C160.500021,20.3571257 165.616042,16.6875196 171.5625,12.7767857 C177.508958,8.86605187 183.937465,5.75894009 190.848214,3.45535714 C197.758963,1.1517742 204.535681,0 211.178571,0 C235.178691,0 253.982075,6.64279071 267.589286,19.9285714 C281.196497,33.2143521 288,51.6427393 288,75.2142857 C288,98.8929755 275.732266,122.999877 251.196429,147.535714 L151.071429,243.964286 C149.142847,245.892867 146.785728,246.857143 144,246.857143 L144,246.857143 Z"/> + </mask> + <path fill="none" stroke="#fff" stroke-width="6" stroke-linecap="round" stroke-linejoin="round" mask="url(#mask)" d="M-166,115.135254 C-166,115.135254 0.595052083,115.135254 2.9765625,115.135254 L91.9101562,115.135254 L97.9638977,100.101562 L105.430695,115.135254 L114.893585,115.135254 L131.129913,189.53125 L148.161163,57 L165.348663,131.027344 L172.272491,115.135254 L250.84967,115.135254 L428.259813,115.135254"/> + </g> +</svg> diff --git a/toolkit/components/normandy/skin/shared/heartbeat-star-lit.svg b/toolkit/components/normandy/skin/shared/heartbeat-star-lit.svg new file mode 100644 index 0000000000..3f59099b98 --- /dev/null +++ b/toolkit/components/normandy/skin/shared/heartbeat-star-lit.svg @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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/. --> +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="100%" height="100%"> + <path fill="#0095dd" d="M8,0C7.7,0,7.4,0.2,7.2,0.7l-2,4.1L0.9,5.5c-1,0.2-1.2,0.9-0.5,1.6l3.1,3.3l-0.7,4.6C2.7,15.6,3,16,3.4,16c0.2,0,0.4-0.1,0.6-0.2L8,13.7l3.9,2.1c0.2,0.1,0.5,0.2,0.6,0.2c0.5,0,0.8-0.4,0.7-1.1l-0.7-4.6l3.1-3.3c0.7-0.7,0.4-1.4-0.5-1.6l-4.3-0.7l-2-4.1C8.6,0.2,8.3,0,8,0L8,0z"/> +</svg> diff --git a/toolkit/components/normandy/skin/shared/heartbeat-star-off.svg b/toolkit/components/normandy/skin/shared/heartbeat-star-off.svg new file mode 100644 index 0000000000..143fe48e22 --- /dev/null +++ b/toolkit/components/normandy/skin/shared/heartbeat-star-off.svg @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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/. --> +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="100%" height="100%"> + <path fill="#c0c0c0" d="M8,0C7.7,0,7.4,0.2,7.2,0.7l-2,4.1L0.9,5.5c-1,0.2-1.2,0.9-0.5,1.6l3.1,3.3l-0.7,4.6C2.7,15.6,3,16,3.4,16c0.2,0,0.4-0.1,0.6-0.2L8,13.7l3.9,2.1c0.2,0.1,0.5,0.2,0.6,0.2c0.5,0,0.8-0.4,0.7-1.1l-0.7-4.6l3.1-3.3c0.7-0.7,0.4-1.4-0.5-1.6l-4.3-0.7l-2-4.1C8.6,0.2,8.3,0,8,0L8,0z"/> +</svg> diff --git a/toolkit/components/normandy/test/.eslintrc.js b/toolkit/components/normandy/test/.eslintrc.js new file mode 100644 index 0000000000..0ee96759d4 --- /dev/null +++ b/toolkit/components/normandy/test/.eslintrc.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + rules: { + "require-yield": 0, + }, +}; diff --git a/toolkit/components/normandy/test/NormandyTestUtils.sys.mjs b/toolkit/components/normandy/test/NormandyTestUtils.sys.mjs new file mode 100644 index 0000000000..751514d4c1 --- /dev/null +++ b/toolkit/components/normandy/test/NormandyTestUtils.sys.mjs @@ -0,0 +1,345 @@ +/* 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 { Preferences } from "resource://gre/modules/Preferences.sys.mjs"; +import { AddonStudies } from "resource://normandy/lib/AddonStudies.sys.mjs"; +import { RecipeRunner } from "resource://normandy/lib/RecipeRunner.sys.mjs"; +import { sinon } from "resource://testing-common/Sinon.sys.mjs"; + +const FIXTURE_ADDON_ID = "normandydriver-a@example.com"; +const UUID_REGEX = + /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i; + +// Factory IDs +let _addonStudyFactoryId = 0; +let _preferenceStudyFactoryId = 0; +let _preferenceRolloutFactoryId = 0; + +let testGlobals = {}; + +const preferenceBranches = { + user: Preferences, + default: new Preferences({ defaultBranch: true }), +}; + +export const NormandyTestUtils = { + init({ add_task, Assert } = {}) { + testGlobals.add_task = add_task; + testGlobals.Assert = Assert; + }, + + factories: { + addonStudyFactory(attrs = {}) { + for (const key of ["name", "description"]) { + if (attrs && attrs[key]) { + throw new Error( + `${key} is no longer a valid key for addon studies, please update to v2 study schema` + ); + } + } + + // Generate a slug from userFacingName + let recipeId = _addonStudyFactoryId++; + let { userFacingName = `Test study ${recipeId}`, slug } = attrs; + delete attrs.slug; + if (userFacingName && !slug) { + slug = userFacingName.replace(" ", "-").toLowerCase(); + } + + return Object.assign( + { + recipeId, + slug, + userFacingName: "Test study", + userFacingDescription: "test description", + branch: AddonStudies.NO_BRANCHES_MARKER, + active: true, + addonId: FIXTURE_ADDON_ID, + addonUrl: "http://test/addon.xpi", + addonVersion: "1.0.0", + studyStartDate: new Date(), + studyEndDate: null, + extensionApiId: 1, + extensionHash: + "ade1c14196ec4fe0aa0a6ba40ac433d7c8d1ec985581a8a94d43dc58991b5171", + extensionHashAlgorithm: "sha256", + temporaryErrorDeadline: null, + }, + attrs + ); + }, + + branchedAddonStudyFactory(attrs = {}) { + return NormandyTestUtils.factories.addonStudyFactory( + Object.assign( + { + branch: "a", + }, + attrs + ) + ); + }, + + preferenceStudyFactory(attrs = {}) { + const defaultPref = { + "test.study": {}, + }; + const defaultPrefInfo = { + preferenceValue: false, + preferenceType: "boolean", + previousPreferenceValue: null, + preferenceBranchType: "default", + overridden: false, + }; + const preferences = {}; + for (const [prefName, prefInfo] of Object.entries( + attrs.preferences || defaultPref + )) { + preferences[prefName] = { ...defaultPrefInfo, ...prefInfo }; + } + + // Generate a slug from userFacingName + let { + userFacingName = `Test study ${_preferenceStudyFactoryId++}`, + slug, + } = attrs; + delete attrs.slug; + if (userFacingName && !slug) { + slug = userFacingName.replace(" ", "-").toLowerCase(); + } + + return Object.assign( + { + userFacingName, + userFacingDescription: `${userFacingName} description`, + slug, + branch: "control", + expired: false, + lastSeen: new Date().toJSON(), + experimentType: "exp", + actionName: "PreferenceExperimentAction", + }, + attrs, + { + preferences, + } + ); + }, + + preferenceRolloutFactory(attrs = {}) { + const defaultPrefInfo = { + preferenceName: "test.rollout.{}", + value: true, + previousValue: false, + }; + const preferences = (attrs.preferences ?? [{}]).map((override, idx) => ({ + ...defaultPrefInfo, + preferenceName: defaultPrefInfo.preferenceName.replace( + "{}", + (idx + 1).toString() + ), + ...override, + })); + + return Object.assign( + { + slug: `test-rollout-${_preferenceRolloutFactoryId++}`, + state: "active", + }, + attrs, + { + preferences, + } + ); + }, + }, + + /** + * Combine a list of functions right to left. The rightmost function is passed + * to the preceding function as the argument; the result of this is passed to + * the next function until all are exhausted. For example, this: + * + * decorate(func1, func2, func3); + * + * is equivalent to this: + * + * func1(func2(func3)); + */ + decorate(...args) { + const funcs = Array.from(args); + let decorated = funcs.pop(); + const origName = decorated.name; + funcs.reverse(); + for (const func of funcs) { + decorated = func(decorated); + } + Object.defineProperty(decorated, "name", { value: origName }); + return decorated; + }, + + /** + * Wrapper around add_task for declaring tests that use several with-style + * wrappers. The last argument should be your test function; all other arguments + * should be functions that accept a single test function argument. + * + * The arguments are combined using decorate and passed to add_task as a single + * test function. + * + * @param {[Function]} args + * @example + * decorate_task( + * withMockPreferences(), + * withMockNormandyApi(), + * async function myTest(mockPreferences, mockApi) { + * // Do a test + * } + * ); + */ + decorate_task(...args) { + return testGlobals.add_task(NormandyTestUtils.decorate(...args)); + }, + + isUuid(s) { + return UUID_REGEX.test(s); + }, + + withMockRecipeCollection(recipes = []) { + return function wrapper(testFunc) { + return async function inner(args) { + let recipeIds = new Set(); + for (const recipe of recipes) { + if (!recipe.id || recipeIds.has(recipe.id)) { + throw new Error( + "To use withMockRecipeCollection each recipe must have a unique ID" + ); + } + recipeIds.add(recipe.id); + } + + let db = await RecipeRunner._remoteSettingsClientForTesting.db; + await db.clear(); + const fakeSig = { signature: "abc" }; + + for (const recipe of recipes) { + await db.create({ + id: `recipe-${recipe.id}`, + recipe, + signature: fakeSig, + }); + } + + // last modified needs to be some positive integer + let lastModified = await db.getLastModified(); + await db.importChanges({}, lastModified + 1); + + const mockRecipeCollection = { + async addRecipes(newRecipes) { + for (const recipe of newRecipes) { + if (!recipe.id || recipeIds.has(recipe)) { + throw new Error( + "To use withMockRecipeCollection each recipe must have a unique ID" + ); + } + } + db = await RecipeRunner._remoteSettingsClientForTesting.db; + for (const recipe of newRecipes) { + recipeIds.add(recipe.id); + await db.create({ + id: `recipe-${recipe.id}`, + recipe, + signature: fakeSig, + }); + } + lastModified = (await db.getLastModified()) || 0; + await db.importChanges({}, lastModified + 1); + }, + }; + + try { + await testFunc({ ...args, mockRecipeCollection }); + } finally { + db = await RecipeRunner._remoteSettingsClientForTesting.db; + await db.clear(); + lastModified = await db.getLastModified(); + await db.importChanges({}, lastModified + 1); + } + }; + }; + }, + + MockPreferences: class { + constructor() { + this.oldValues = { user: {}, default: {} }; + } + + set(name, value, branch = "user") { + this.preserve(name, branch); + preferenceBranches[branch].set(name, value); + } + + preserve(name, branch) { + if (!(name in this.oldValues[branch])) { + this.oldValues[branch][name] = preferenceBranches[branch].get( + name, + undefined + ); + } + } + + cleanup() { + for (const [branchName, values] of Object.entries(this.oldValues)) { + const preferenceBranch = preferenceBranches[branchName]; + for (const [name, value] of Object.entries(values)) { + if (value !== undefined) { + preferenceBranch.set(name, value); + } else { + preferenceBranch.reset(name); + } + } + } + } + }, + + withMockPreferences() { + return function (testFunction) { + return async function inner(args) { + const mockPreferences = new NormandyTestUtils.MockPreferences(); + try { + await testFunction({ ...args, mockPreferences }); + } finally { + mockPreferences.cleanup(); + } + }; + }; + }, + + withStub(object, method, { returnValue, as = `${method}Stub` } = {}) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const stub = sinon.stub(object, method); + if (returnValue) { + stub.returns(returnValue); + } + try { + await testFunction({ ...args, [as]: stub }); + } finally { + stub.restore(); + } + }; + }; + }, + + withSpy(object, method, { as = `${method}Spy` } = {}) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const spy = sinon.spy(object, method); + try { + await testFunction({ ...args, [as]: spy }); + } finally { + spy.restore(); + } + }; + }; + }, +}; diff --git a/toolkit/components/normandy/test/browser/action_server.sjs b/toolkit/components/normandy/test/browser/action_server.sjs new file mode 100644 index 0000000000..5d6a3e6bb0 --- /dev/null +++ b/toolkit/components/normandy/test/browser/action_server.sjs @@ -0,0 +1,10 @@ +// Returns JS for an action, regardless of the URL. +function handleRequest(request, response) { + // Allow cross-origin, so you can XHR to it! + response.setHeader("Access-Control-Allow-Origin", "*", false); + // Avoid confusing cache behaviors + response.setHeader("Cache-Control", "no-cache", false); + + // Write response body + response.write('registerAsyncCallback("action", async () => {});'); +} diff --git a/toolkit/components/normandy/test/browser/addons/normandydriver-a-1.0/manifest.json b/toolkit/components/normandy/test/browser/addons/normandydriver-a-1.0/manifest.json new file mode 100644 index 0000000000..fca9426a3f --- /dev/null +++ b/toolkit/components/normandy/test/browser/addons/normandydriver-a-1.0/manifest.json @@ -0,0 +1,11 @@ +{ + "manifest_version": 2, + "name": "normandy_fixture_a", + "version": "1.0", + "description": "Dummy test fixture that's a webextension, branch A", + "browser_specific_settings": { + "gecko": { + "id": "normandydriver-a@example.com" + } + } +} diff --git a/toolkit/components/normandy/test/browser/addons/normandydriver-a-2.0/manifest.json b/toolkit/components/normandy/test/browser/addons/normandydriver-a-2.0/manifest.json new file mode 100644 index 0000000000..40f7351425 --- /dev/null +++ b/toolkit/components/normandy/test/browser/addons/normandydriver-a-2.0/manifest.json @@ -0,0 +1,11 @@ +{ + "manifest_version": 2, + "name": "normandy_fixture_a", + "version": "2.0", + "description": "Dummy test fixture that's a webextension, branch A", + "browser_specific_settings": { + "gecko": { + "id": "normandydriver-a@example.com" + } + } +} diff --git a/toolkit/components/normandy/test/browser/addons/normandydriver-b-1.0/manifest.json b/toolkit/components/normandy/test/browser/addons/normandydriver-b-1.0/manifest.json new file mode 100644 index 0000000000..044ae7ebc3 --- /dev/null +++ b/toolkit/components/normandy/test/browser/addons/normandydriver-b-1.0/manifest.json @@ -0,0 +1,11 @@ +{ + "manifest_version": 2, + "name": "normandy_fixture_b", + "version": "1.0", + "description": "Dummy test fixture that's a webextension, branch B", + "browser_specific_settings": { + "gecko": { + "id": "normandydriver-b@example.com" + } + } +} diff --git a/toolkit/components/normandy/test/browser/browser.toml b/toolkit/components/normandy/test/browser/browser.toml new file mode 100644 index 0000000000..1114750faf --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser.toml @@ -0,0 +1,77 @@ +[DEFAULT] +tags = "addons" +support-files = [ + "action_server.sjs", + "addons/normandydriver-a-1.0.xpi", + "addons/normandydriver-b-1.0.xpi", + "addons/normandydriver-a-2.0.xpi", +] +generated-files = [ + "addons/normandydriver-a-1.0.xpi", + "addons/normandydriver-b-1.0.xpi", + "addons/normandydriver-a-2.0.xpi", +] +head = "head.js" + +["browser_ActionsManager.js"] + +["browser_AddonRollouts.js"] + +["browser_AddonStudies.js"] +skip-if = ["verify && os == 'linux'"] + +["browser_BaseAction.js"] + +["browser_CleanupManager.js"] + +["browser_ClientEnvironment.js"] + +["browser_EventEmitter.js"] + +["browser_Heartbeat.js"] + +["browser_LegacyHeartbeat.js"] + +["browser_LogManager.js"] + +["browser_Normandy.js"] + +["browser_NormandyAddonManager.js"] + +["browser_NormandyMigrations.js"] + +["browser_PreferenceExperiments.js"] + +["browser_PreferenceRollouts.js"] + +["browser_RecipeRunner.js"] +tags = "remote-settings" + +["browser_ShieldPreferences.js"] + +["browser_Storage.js"] + +["browser_Uptake.js"] + +["browser_about_preferences.js"] + +["browser_about_studies.js"] +https_first_disabled = true + +["browser_actions_AddonRollbackAction.js"] + +["browser_actions_AddonRolloutAction.js"] + +["browser_actions_BranchedAddonStudyAction.js"] + +["browser_actions_ConsoleLogAction.js"] + +["browser_actions_MessagingExperimentAction.js"] + +["browser_actions_PreferenceExperimentAction.js"] + +["browser_actions_PreferenceRollbackAction.js"] + +["browser_actions_PreferenceRolloutAction.js"] + +["browser_actions_ShowHeartbeatAction.js"] diff --git a/toolkit/components/normandy/test/browser/browser_ActionsManager.js b/toolkit/components/normandy/test/browser/browser_ActionsManager.js new file mode 100644 index 0000000000..8b5772fa26 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_ActionsManager.js @@ -0,0 +1,68 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { ActionsManager } = ChromeUtils.importESModule( + "resource://normandy/lib/ActionsManager.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); +const { ActionSchemas } = ChromeUtils.importESModule( + "resource://normandy/actions/schemas/index.sys.mjs" +); + +// Test life cycle methods for actions +decorate_task(async function (reportActionStub, Stub) { + let manager = new ActionsManager(); + const recipe = { id: 1, action: "test-local-action-used" }; + + let actionUsed = { + processRecipe: sinon.stub(), + finalize: sinon.stub(), + }; + let actionUnused = { + processRecipe: sinon.stub(), + finalize: sinon.stub(), + }; + manager.localActions = { + "test-local-action-used": actionUsed, + "test-local-action-unused": actionUnused, + }; + + await manager.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await manager.finalize(); + + Assert.deepEqual( + actionUsed.processRecipe.args, + [[recipe, BaseAction.suitability.FILTER_MATCH]], + "used action should be called with the recipe" + ); + ok( + actionUsed.finalize.calledOnce, + "finalize should be called on used action" + ); + Assert.deepEqual( + actionUnused.processRecipe.args, + [], + "unused action should not be called with the recipe" + ); + ok( + actionUnused.finalize.calledOnce, + "finalize should be called on the unused action" + ); +}); + +decorate_task(async function () { + for (const [name, Constructor] of Object.entries( + ActionsManager.actionConstructors + )) { + const action = new Constructor(); + Assert.deepEqual( + ActionSchemas[name], + action.schema, + "action name should map to a schema" + ); + } +}); diff --git a/toolkit/components/normandy/test/browser/browser_AddonRollouts.js b/toolkit/components/normandy/test/browser/browser_AddonRollouts.js new file mode 100644 index 0000000000..0826907b68 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_AddonRollouts.js @@ -0,0 +1,141 @@ +"use strict"; + +const { IndexedDB } = ChromeUtils.importESModule( + "resource://gre/modules/IndexedDB.sys.mjs" +); + +const { AddonRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonRollouts.sys.mjs" +); + +decorate_task(AddonRollouts.withTestMock(), async function testGetMissing() { + ok( + !(await AddonRollouts.get("does-not-exist")), + "get should return null when the requested rollout does not exist" + ); +}); + +decorate_task( + AddonRollouts.withTestMock(), + async function testAddUpdateAndGet() { + const rollout = { + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extension: {}, + }; + await AddonRollouts.add(rollout); + let storedRollout = await AddonRollouts.get(rollout.slug); + Assert.deepEqual( + rollout, + storedRollout, + "get should retrieve a rollout from storage." + ); + + rollout.state = AddonRollouts.STATE_ROLLED_BACK; + await AddonRollouts.update(rollout); + storedRollout = await AddonRollouts.get(rollout.slug); + Assert.deepEqual( + rollout, + storedRollout, + "get should retrieve a rollout from storage." + ); + } +); + +decorate_task( + AddonRollouts.withTestMock(), + async function testCantUpdateNonexistent() { + const rollout = { + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensions: {}, + }; + await Assert.rejects( + AddonRollouts.update(rollout), + /doesn't already exist/, + "Update should fail if the rollout doesn't exist" + ); + ok( + !(await AddonRollouts.has("test-rollout")), + "rollout should not have been added" + ); + } +); + +decorate_task(AddonRollouts.withTestMock(), async function testGetAll() { + const rollout1 = { slug: "test-rollout-1", extension: {} }; + const rollout2 = { slug: "test-rollout-2", extension: {} }; + await AddonRollouts.add(rollout1); + await AddonRollouts.add(rollout2); + + const storedRollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + storedRollouts.sort((a, b) => a.id - b.id), + [rollout1, rollout2], + "getAll should return every stored rollout." + ); +}); + +decorate_task(AddonRollouts.withTestMock(), async function testGetAllActive() { + const rollout1 = { + slug: "test-rollout-1", + state: AddonRollouts.STATE_ACTIVE, + }; + const rollout3 = { + slug: "test-rollout-2", + state: AddonRollouts.STATE_ROLLED_BACK, + }; + await AddonRollouts.add(rollout1); + await AddonRollouts.add(rollout3); + + const activeRollouts = await AddonRollouts.getAllActive(); + Assert.deepEqual( + activeRollouts, + [rollout1], + "getAllActive should return only active rollouts" + ); +}); + +decorate_task(AddonRollouts.withTestMock(), async function testHas() { + const rollout = { slug: "test-rollout", extensions: {} }; + await AddonRollouts.add(rollout); + ok( + await AddonRollouts.has(rollout.slug), + "has should return true for an existing rollout" + ); + ok( + !(await AddonRollouts.has("does not exist")), + "has should return false for a missing rollout" + ); +}); + +// init should mark active rollouts in telemetry +decorate_task( + AddonRollouts.withTestMock(), + withStub(TelemetryEnvironment, "setExperimentActive"), + async function testInitTelemetry({ setExperimentActiveStub }) { + await AddonRollouts.add({ + slug: "test-rollout-active-1", + state: AddonRollouts.STATE_ACTIVE, + }); + await AddonRollouts.add({ + slug: "test-rollout-active-2", + state: AddonRollouts.STATE_ACTIVE, + }); + await AddonRollouts.add({ + slug: "test-rollout-rolled-back", + state: AddonRollouts.STATE_ROLLED_BACK, + }); + + await AddonRollouts.init(); + + Assert.deepEqual( + setExperimentActiveStub.args, + [ + ["test-rollout-active-1", "active", { type: "normandy-addonrollout" }], + ["test-rollout-active-2", "active", { type: "normandy-addonrollout" }], + ], + "init should set activate a telemetry experiment for active addons" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_AddonStudies.js b/toolkit/components/normandy/test/browser/browser_AddonStudies.js new file mode 100644 index 0000000000..090ed92d59 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_AddonStudies.js @@ -0,0 +1,293 @@ +"use strict"; + +const { IndexedDB } = ChromeUtils.importESModule( + "resource://gre/modules/IndexedDB.sys.mjs" +); + +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); +const { addonStudyFactory, branchedAddonStudyFactory } = + NormandyTestUtils.factories; + +// Initialize test utils +AddonTestUtils.initMochitest(this); + +decorate_task(AddonStudies.withStudies(), async function testGetMissing() { + ok( + !(await AddonStudies.get("does-not-exist")), + "get returns null when the requested study does not exist" + ); +}); + +decorate_task( + AddonStudies.withStudies([addonStudyFactory({ slug: "test-study" })]), + async function testGet({ addonStudies: [study] }) { + const storedStudy = await AddonStudies.get(study.recipeId); + Assert.deepEqual(study, storedStudy, "get retrieved a study from storage."); + } +); + +decorate_task( + AddonStudies.withStudies([addonStudyFactory(), addonStudyFactory()]), + async function testGetAll({ addonStudies }) { + const storedStudies = await AddonStudies.getAll(); + Assert.deepEqual( + new Set(storedStudies), + new Set(addonStudies), + "getAll returns every stored study." + ); + } +); + +decorate_task( + AddonStudies.withStudies([addonStudyFactory({ slug: "test-study" })]), + async function testHas({ addonStudies: [study] }) { + let hasStudy = await AddonStudies.has(study.recipeId); + ok(hasStudy, "has returns true for a study that exists in storage."); + + hasStudy = await AddonStudies.has("does-not-exist"); + ok( + !hasStudy, + "has returns false for a study that doesn't exist in storage." + ); + } +); + +decorate_task( + AddonStudies.withStudies([ + addonStudyFactory({ slug: "test-study1" }), + addonStudyFactory({ slug: "test-study2" }), + ]), + async function testClear({ addonStudies: [study1, study2] }) { + const hasAll = + (await AddonStudies.has(study1.recipeId)) && + (await AddonStudies.has(study2.recipeId)); + ok(hasAll, "Before calling clear, both studies are in storage."); + + await AddonStudies.clear(); + const hasAny = + (await AddonStudies.has(study1.recipeId)) || + (await AddonStudies.has(study2.recipeId)); + ok(!hasAny, "After calling clear, all studies are removed from storage."); + } +); + +decorate_task( + AddonStudies.withStudies([addonStudyFactory({ slug: "foo" })]), + async function testUpdate({ addonStudies: [study] }) { + Assert.deepEqual(await AddonStudies.get(study.recipeId), study); + + const updatedStudy = { + ...study, + slug: "bar", + }; + await AddonStudies.update(updatedStudy); + + Assert.deepEqual(await AddonStudies.get(study.recipeId), updatedStudy); + } +); + +decorate_task( + AddonStudies.withStudies([ + addonStudyFactory({ + active: true, + addonId: "does.not.exist@example.com", + studyEndDate: null, + }), + addonStudyFactory({ active: true, addonId: "installed@example.com" }), + addonStudyFactory({ + active: false, + addonId: "already.gone@example.com", + studyEndDate: new Date(2012, 1), + }), + ]), + withSendEventSpy(), + withInstalledWebExtension( + { id: "installed@example.com" }, + { expectUninstall: true } + ), + async function testInit({ + addonStudies: [activeUninstalledStudy, activeInstalledStudy, inactiveStudy], + sendEventSpy, + installedWebExtension: { addonId }, + }) { + await AddonStudies.init(); + + const newActiveStudy = await AddonStudies.get( + activeUninstalledStudy.recipeId + ); + ok( + !newActiveStudy.active, + "init marks studies as inactive if their add-on is not installed." + ); + ok( + newActiveStudy.studyEndDate, + "init sets the study end date if a study's add-on is not installed." + ); + let events = Services.telemetry.snapshotEvents( + Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS, + false + ); + events = (events.parent || []).filter(e => e[1] == "normandy"); + Assert.deepEqual( + events[0].slice(2), // strip timestamp and "normandy" + [ + "unenroll", + "addon_study", + activeUninstalledStudy.slug, + { + addonId: activeUninstalledStudy.addonId, + addonVersion: activeUninstalledStudy.addonVersion, + reason: "uninstalled-sideload", + branch: AddonStudies.NO_BRANCHES_MARKER, + }, + ], + "AddonStudies.init() should send the correct telemetry event" + ); + + const newInactiveStudy = await AddonStudies.get(inactiveStudy.recipeId); + is( + newInactiveStudy.studyEndDate.getFullYear(), + 2012, + "init does not modify inactive studies." + ); + + const newActiveInstalledStudy = await AddonStudies.get( + activeInstalledStudy.recipeId + ); + Assert.deepEqual( + activeInstalledStudy, + newActiveInstalledStudy, + "init does not modify studies whose add-on is still installed." + ); + + // Only activeUninstalledStudy should have generated any events + ok(sendEventSpy.calledOnce, "no extra events should be generated"); + + // Clean up + const addon = await AddonManager.getAddonByID(addonId); + await addon.uninstall(); + await TestUtils.topicObserved("shield-study-ended", (subject, message) => { + return message === `${activeInstalledStudy.recipeId}`; + }); + } +); + +// init should register telemetry experiments +decorate_task( + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + active: true, + addonId: "installed1@example.com", + }), + branchedAddonStudyFactory({ + active: true, + addonId: "installed2@example.com", + }), + ]), + withInstalledWebExtensionSafe({ id: "installed1@example.com" }), + withInstalledWebExtension({ id: "installed2@example.com" }), + withStub(TelemetryEnvironment, "setExperimentActive"), + async function testInit({ addonStudies, setExperimentActiveStub }) { + await AddonStudies.init(); + Assert.deepEqual( + setExperimentActiveStub.args, + [ + [ + addonStudies[0].slug, + addonStudies[0].branch, + { + type: "normandy-addonstudy", + }, + ], + [ + addonStudies[1].slug, + addonStudies[1].branch, + { + type: "normandy-addonstudy", + }, + ], + ], + "Add-on studies are registered in Telemetry by AddonStudies.init" + ); + } +); + +// Test that AddonStudies.init() ends studies that have been uninstalled +decorate_task( + AddonStudies.withStudies([ + addonStudyFactory({ + active: true, + addonId: "installed@example.com", + studyEndDate: null, + }), + ]), + withInstalledWebExtension( + { id: "installed@example.com" }, + { expectUninstall: true } + ), + async function testInit({ + addonStudies: [study], + installedWebExtension: { addonId }, + }) { + const addon = await AddonManager.getAddonByID(addonId); + await addon.uninstall(); + await TestUtils.topicObserved("shield-study-ended", (subject, message) => { + return message === `${study.recipeId}`; + }); + + const newStudy = await AddonStudies.get(study.recipeId); + ok( + !newStudy.active, + "Studies are marked as inactive when their add-on is uninstalled." + ); + ok( + newStudy.studyEndDate, + "The study end date is set when the add-on for the study is uninstalled." + ); + } +); + +decorate_task( + AddonStudies.withStudies([ + NormandyTestUtils.factories.addonStudyFactory({ active: true }), + NormandyTestUtils.factories.branchedAddonStudyFactory(), + ]), + async function testRemoveOldAddonStudies({ + addonStudies: [noBranchStudy, branchedStudy], + }) { + // pre check, both studies are active + const preActiveIds = (await AddonStudies.getAllActive()).map( + addon => addon.recipeId + ); + Assert.deepEqual( + preActiveIds, + [noBranchStudy.recipeId, branchedStudy.recipeId], + "Both studies should be active" + ); + + // run the migration + await AddonStudies.migrations.migration02RemoveOldAddonStudyAction(); + + // The unbrached study should end + const postActiveIds = (await AddonStudies.getAllActive()).map( + addon => addon.recipeId + ); + Assert.deepEqual( + postActiveIds, + [branchedStudy.recipeId], + "The unbranched study should end" + ); + + // But both studies should still be present + const postAllIds = (await AddonStudies.getAll()).map( + addon => addon.recipeId + ); + Assert.deepEqual( + postAllIds, + [noBranchStudy.recipeId, branchedStudy.recipeId], + "Both studies should still be present" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_BaseAction.js b/toolkit/components/normandy/test/browser/browser_BaseAction.js new file mode 100644 index 0000000000..240a235346 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_BaseAction.js @@ -0,0 +1,350 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); + +class NoopAction extends BaseAction { + constructor() { + super(); + this._testPreExecutionFlag = false; + this._testRunFlag = false; + this._testFinalizeFlag = false; + } + + _preExecution() { + this._testPreExecutionFlag = true; + } + + _run(recipe) { + this._testRunFlag = true; + } + + _finalize() { + this._testFinalizeFlag = true; + } +} + +NoopAction._errorToThrow = new Error("test error"); + +class FailPreExecutionAction extends NoopAction { + _preExecution() { + throw NoopAction._errorToThrow; + } +} + +class FailRunAction extends NoopAction { + _run(recipe) { + throw NoopAction._errorToThrow; + } +} + +class FailFinalizeAction extends NoopAction { + _finalize() { + throw NoopAction._errorToThrow; + } +} + +// Test that constructor and override methods are run +decorate_task( + withStub(Uptake, "reportRecipe"), + withStub(Uptake, "reportAction"), + async () => { + let action = new NoopAction(); + is( + action._testPreExecutionFlag, + false, + "_preExecution should not have been called on a new action" + ); + is( + action._testRunFlag, + false, + "_run has should not have been called on a new action" + ); + is( + action._testFinalizeFlag, + false, + "_finalize should not be called on a new action" + ); + + const recipe = recipeFactory(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is( + action._testPreExecutionFlag, + true, + "_preExecution should be called when a recipe is executed" + ); + is( + action._testRunFlag, + true, + "_run should be called when a recipe is executed" + ); + is( + action._testFinalizeFlag, + false, + "_finalize should not have been called when a recipe is executed" + ); + + await action.finalize(); + is( + action._testFinalizeFlag, + true, + "_finalizeExecution should be called when finalize was called" + ); + + action = new NoopAction(); + await action.finalize(); + is( + action._testPreExecutionFlag, + true, + "_preExecution should be called when finalized even if no recipes" + ); + is( + action._testRunFlag, + false, + "_run should be called if no recipes were run" + ); + is( + action._testFinalizeFlag, + true, + "_finalize should be called when finalized" + ); + } +); + +// Test that per-recipe uptake telemetry is recorded +decorate_task( + withStub(Uptake, "reportRecipe"), + async function ({ reportRecipeStub }) { + const action = new NoopAction(); + const recipe = recipeFactory(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + Assert.deepEqual( + reportRecipeStub.args, + [[recipe, Uptake.RECIPE_SUCCESS]], + "per-recipe uptake telemetry should be reported" + ); + } +); + +// Finalize causes action telemetry to be recorded +decorate_task( + withStub(Uptake, "reportAction"), + async function ({ reportActionStub }) { + const action = new NoopAction(); + await action.finalize(); + Assert.equal( + action.state, + NoopAction.STATE_FINALIZED, + "Action should be marked as finalized" + ); + Assert.deepEqual( + reportActionStub.args, + [[action.name, Uptake.ACTION_SUCCESS]], + "action uptake telemetry should be reported" + ); + } +); + +// Recipes can't be run after finalize is called +decorate_task( + withStub(Uptake, "reportRecipe"), + async function ({ reportRecipeStub }) { + const action = new NoopAction(); + const recipe1 = recipeFactory(); + const recipe2 = recipeFactory(); + + await action.processRecipe(recipe1, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + await Assert.rejects( + action.processRecipe(recipe2, BaseAction.suitability.FILTER_MATCH), + /^Error: Action has already been finalized$/, + "running recipes after finalization is an error" + ); + + Assert.deepEqual( + reportRecipeStub.args, + [[recipe1, Uptake.RECIPE_SUCCESS]], + "Only recipes executed prior to finalizer should report uptake telemetry" + ); + } +); + +// Test an action with a failing pre-execution step +decorate_task( + withStub(Uptake, "reportRecipe"), + withStub(Uptake, "reportAction"), + async function ({ reportRecipeStub, reportActionStub }) { + const recipe = recipeFactory(); + const action = new FailPreExecutionAction(); + is( + action.state, + FailPreExecutionAction.STATE_PREPARING, + "Pre-execution should not happen immediately" + ); + + // Should fail, putting the action into a "failed" state, but the entry + // point `processRecipe` should not itself throw an exception. + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is( + action.state, + FailPreExecutionAction.STATE_FAILED, + "Action fails if pre-execution fails" + ); + is( + action.lastError, + NoopAction._errorToThrow, + "The thrown error should be stored in lastError" + ); + + // Should not throw, even though the action is in a disabled state. + await action.finalize(); + is( + action.state, + FailPreExecutionAction.STATE_FINALIZED, + "Action should be finalized" + ); + is( + action.lastError, + NoopAction._errorToThrow, + "lastError should not have changed" + ); + + is(action._testRunFlag, false, "_run should not have been called"); + is( + action._testFinalizeFlag, + false, + "_finalize should not have been called" + ); + + Assert.deepEqual( + reportRecipeStub.args, + [[recipe, Uptake.RECIPE_ACTION_DISABLED]], + "Recipe should report recipe status as action disabled" + ); + + Assert.deepEqual( + reportActionStub.args, + [[action.name, Uptake.ACTION_PRE_EXECUTION_ERROR]], + "Action should report pre execution error" + ); + } +); + +// Test an action with a failing recipe step +decorate_task( + withStub(Uptake, "reportRecipe"), + withStub(Uptake, "reportAction"), + async function ({ reportRecipeStub, reportActionStub }) { + const recipe = recipeFactory(); + const action = new FailRunAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is( + action.state, + FailRunAction.STATE_READY, + "Action should not be marked as failed due to a recipe failure" + ); + await action.finalize(); + is( + action.state, + FailRunAction.STATE_FINALIZED, + "Action should be marked as finalized after finalize is called" + ); + + ok(action._testFinalizeFlag, "_finalize should have been called"); + + Assert.deepEqual( + reportRecipeStub.args, + [[recipe, Uptake.RECIPE_EXECUTION_ERROR]], + "Recipe should report recipe execution error" + ); + + Assert.deepEqual( + reportActionStub.args, + [[action.name, Uptake.ACTION_SUCCESS]], + "Action should report success" + ); + } +); + +// Test an action with a failing finalize step +decorate_task( + withStub(Uptake, "reportRecipe"), + withStub(Uptake, "reportAction"), + async function ({ reportRecipeStub, reportActionStub }) { + const recipe = recipeFactory(); + const action = new FailFinalizeAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual( + reportRecipeStub.args, + [[recipe, Uptake.RECIPE_SUCCESS]], + "Recipe should report success" + ); + + Assert.deepEqual( + reportActionStub.args, + [[action.name, Uptake.ACTION_POST_EXECUTION_ERROR]], + "Action should report post execution error" + ); + } +); + +// Disable disables an action +decorate_task( + withStub(Uptake, "reportRecipe"), + withStub(Uptake, "reportAction"), + async function ({ reportRecipeStub, reportActionStub }) { + const recipe = recipeFactory(); + const action = new NoopAction(); + + action.disable(); + is( + action.state, + NoopAction.STATE_DISABLED, + "Action should be marked as disabled" + ); + + // Should not throw, even though the action is disabled + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + + // Should not throw, even though the action is disabled + await action.finalize(); + + is(action._testRunFlag, false, "_run should not have been called"); + is( + action._testFinalizeFlag, + false, + "_finalize should not have been called" + ); + + Assert.deepEqual( + reportActionStub.args, + [[action.name, Uptake.ACTION_SUCCESS]], + "Action should not report pre execution error" + ); + + Assert.deepEqual( + reportRecipeStub.args, + [[recipe, Uptake.RECIPE_ACTION_DISABLED]], + "Recipe should report recipe status as action disabled" + ); + } +); + +// If the capabilities don't match, processRecipe shouldn't validate the arguments +decorate_task(async function () { + const recipe = recipeFactory(); + const action = new NoopAction(); + const verifySpy = sinon.spy(action, "validateArguments"); + await action.processRecipe( + recipe, + BaseAction.suitability.CAPABILITIES_MISMATCH + ); + ok(!verifySpy.called, "validateArguments should not be called"); +}); diff --git a/toolkit/components/normandy/test/browser/browser_CleanupManager.js b/toolkit/components/normandy/test/browser/browser_CleanupManager.js new file mode 100644 index 0000000000..f1b4930394 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_CleanupManager.js @@ -0,0 +1,26 @@ +"use strict"; + +const { CleanupManager } = ChromeUtils.importESModule( + "resource://normandy/lib/CleanupManager.sys.mjs" +); /* global CleanupManagerClass */ + +add_task(async function testCleanupManager() { + const spy1 = sinon.spy(); + const spy2 = sinon.spy(); + const spy3 = sinon.spy(); + + const manager = new CleanupManager.constructor(); + manager.addCleanupHandler(spy1); + manager.addCleanupHandler(spy2); + manager.addCleanupHandler(spy3); + manager.removeCleanupHandler(spy2); // Test removal + + await manager.cleanup(); + ok(spy1.called, "cleanup called the spy1 handler"); + ok(!spy2.called, "cleanup did not call the spy2 handler"); + ok(spy3.called, "cleanup called the spy3 handler"); + + await manager.cleanup(); + ok(spy1.calledOnce, "cleanup only called the spy1 handler once"); + ok(spy3.calledOnce, "cleanup only called the spy3 handler once"); +}); diff --git a/toolkit/components/normandy/test/browser/browser_ClientEnvironment.js b/toolkit/components/normandy/test/browser/browser_ClientEnvironment.js new file mode 100644 index 0000000000..f1322cffae --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_ClientEnvironment.js @@ -0,0 +1,272 @@ +"use strict"; + +const { TelemetryController } = ChromeUtils.importESModule( + "resource://gre/modules/TelemetryController.sys.mjs" +); + +const { AddonRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonRollouts.sys.mjs" +); +const { ClientEnvironment } = ChromeUtils.importESModule( + "resource://normandy/lib/ClientEnvironment.sys.mjs" +); +const { PreferenceExperiments } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceExperiments.sys.mjs" +); +const { PreferenceRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceRollouts.sys.mjs" +); +const { RecipeRunner } = ChromeUtils.importESModule( + "resource://normandy/lib/RecipeRunner.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +add_task(async function testTelemetry() { + // setup + await SpecialPowers.pushPrefEnv({ + set: [["privacy.reduceTimerPrecision", true]], + }); + + await TelemetryController.submitExternalPing("testfoo", { foo: 1 }); + await TelemetryController.submitExternalPing("testbar", { bar: 2 }); + await TelemetryController.submitExternalPing("testfoo", { foo: 3 }); + + // Test it can access telemetry + const telemetry = await ClientEnvironment.telemetry; + is(typeof telemetry, "object", "Telemetry is accesible"); + + // Test it reads different types of telemetry + is( + telemetry.testfoo.payload.foo, + 3, + "telemetry filters pull the latest ping from a type" + ); + is( + telemetry.testbar.payload.bar, + 2, + "telemetry filters pull from submitted telemetry pings" + ); +}); + +add_task(async function testUserId() { + // Test that userId is available + ok(NormandyTestUtils.isUuid(ClientEnvironment.userId), "userId available"); + + // test that it pulls from the right preference + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.user_id", "fake id"]], + }); + is(ClientEnvironment.userId, "fake id", "userId is pulled from preferences"); +}); + +add_task(async function testDistribution() { + // distribution id defaults to "default" for most builds, and + // "mozilla-MSIX" for MSIX builds. + is( + ClientEnvironment.distribution, + AppConstants.platform === "win" && + Services.sysinfo.getProperty("hasWinPackageId") + ? "mozilla-MSIX" + : "default", + "distribution has a default value" + ); + + // distribution id is read from a preference + Services.prefs + .getDefaultBranch(null) + .setStringPref("distribution.id", "funnelcake"); + is( + ClientEnvironment.distribution, + "funnelcake", + "distribution is read from preferences" + ); + Services.prefs + .getDefaultBranch(null) + .setStringPref("distribution.id", "default"); +}); + +const mockClassify = { country: "FR", request_time: new Date(2017, 1, 1) }; +add_task( + ClientEnvironment.withMockClassify( + mockClassify, + async function testCountryRequestTime() { + // Test that country and request_time pull their data from the server. + is( + await ClientEnvironment.country, + mockClassify.country, + "country is read from the server API" + ); + is( + await ClientEnvironment.request_time, + mockClassify.request_time, + "request_time is read from the server API" + ); + } + ) +); + +add_task(async function testSync() { + is( + ClientEnvironment.syncMobileDevices, + 0, + "syncMobileDevices defaults to zero" + ); + is( + ClientEnvironment.syncDesktopDevices, + 0, + "syncDesktopDevices defaults to zero" + ); + is( + ClientEnvironment.syncTotalDevices, + 0, + "syncTotalDevices defaults to zero" + ); + await SpecialPowers.pushPrefEnv({ + set: [ + ["services.sync.clients.devices.mobile", 5], + ["services.sync.clients.devices.desktop", 4], + ], + }); + is( + ClientEnvironment.syncMobileDevices, + 5, + "syncMobileDevices is read when set" + ); + is( + ClientEnvironment.syncDesktopDevices, + 4, + "syncDesktopDevices is read when set" + ); + is( + ClientEnvironment.syncTotalDevices, + 9, + "syncTotalDevices is read when set" + ); +}); + +add_task(async function testDoNotTrack() { + // doNotTrack defaults to false + ok(!ClientEnvironment.doNotTrack, "doNotTrack has a default value"); + + // doNotTrack is read from a preference + await SpecialPowers.pushPrefEnv({ + set: [["privacy.donottrackheader.enabled", true]], + }); + ok(ClientEnvironment.doNotTrack, "doNotTrack is read from preferences"); +}); + +add_task(async function testExperiments() { + const active = { slug: "active", expired: false }; + const expired = { slug: "expired", expired: true }; + const getAll = sinon + .stub(PreferenceExperiments, "getAll") + .callsFake(async () => [active, expired]); + + const experiments = await ClientEnvironment.experiments; + Assert.deepEqual( + experiments.all, + ["active", "expired"], + "experiments.all returns all stored experiment names" + ); + Assert.deepEqual( + experiments.active, + ["active"], + "experiments.active returns all active experiment names" + ); + Assert.deepEqual( + experiments.expired, + ["expired"], + "experiments.expired returns all expired experiment names" + ); + + getAll.restore(); +}); + +add_task(async function isFirstRun() { + await SpecialPowers.pushPrefEnv({ set: [["app.normandy.first_run", true]] }); + ok(ClientEnvironment.isFirstRun, "isFirstRun is read from preferences"); +}); + +decorate_task( + PreferenceExperiments.withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ + branch: "a-test-branch", + }), + ]), + AddonStudies.withStudies([ + NormandyTestUtils.factories.branchedAddonStudyFactory({ + branch: "b-test-branch", + }), + ]), + async function testStudies({ + prefExperiments: [prefExperiment], + addonStudies: [addonStudy], + }) { + Assert.deepEqual( + await ClientEnvironment.studies, + { + pref: { + [prefExperiment.slug]: prefExperiment, + }, + addon: { + [addonStudy.slug]: addonStudy, + }, + }, + "addon and preference studies shold be accessible" + ); + is( + (await ClientEnvironment.studies).pref[prefExperiment.slug].branch, + "a-test-branch", + "A specific preference experiment field should be accessible in the context" + ); + is( + (await ClientEnvironment.studies).addon[addonStudy.slug].branch, + "b-test-branch", + "A specific addon study field should be accessible in the context" + ); + + ok(RecipeRunner.getCapabilities().has("jexl.context.normandy.studies")); + ok(RecipeRunner.getCapabilities().has("jexl.context.env.studies")); + } +); + +decorate_task(PreferenceRollouts.withTestMock(), async function testRollouts() { + const prefRollout = { + slug: "test-rollout", + preference: [], + }; + await PreferenceRollouts.add(prefRollout); + const addonRollout = { + slug: "test-rollout-1", + extension: {}, + }; + await AddonRollouts.add(addonRollout); + + Assert.deepEqual( + await ClientEnvironment.rollouts, + { + pref: { + [prefRollout.slug]: prefRollout, + }, + addon: { + [addonRollout.slug]: addonRollout, + }, + }, + "addon and preference rollouts should be accessible" + ); + is( + (await ClientEnvironment.rollouts).pref[prefRollout.slug].slug, + prefRollout.slug, + "A specific preference rollout field should be accessible in the context" + ); + is( + (await ClientEnvironment.rollouts).addon[addonRollout.slug].slug, + addonRollout.slug, + "A specific addon rollout field should be accessible in the context" + ); + + ok(RecipeRunner.getCapabilities().has("jexl.context.normandy.rollouts")); + ok(RecipeRunner.getCapabilities().has("jexl.context.env.rollouts")); +}); diff --git a/toolkit/components/normandy/test/browser/browser_EventEmitter.js b/toolkit/components/normandy/test/browser/browser_EventEmitter.js new file mode 100644 index 0000000000..a64c52896f --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_EventEmitter.js @@ -0,0 +1,110 @@ +"use strict"; + +const { EventEmitter } = ChromeUtils.importESModule( + "resource://normandy/lib/EventEmitter.sys.mjs" +); + +const evidence = { + a: 0, + b: 0, + c: 0, + log: "", +}; + +function listenerA(x) { + evidence.a += x; + evidence.log += "a"; +} + +function listenerB(x) { + evidence.b += x; + evidence.log += "b"; +} + +function listenerC(x) { + evidence.c += x; + evidence.log += "c"; +} + +decorate_task(async function () { + const eventEmitter = new EventEmitter(); + + // Fire an unrelated event, to make sure nothing goes wrong + eventEmitter.on("nothing"); + + // bind listeners + eventEmitter.on("event", listenerA); + eventEmitter.on("event", listenerB); + eventEmitter.once("event", listenerC); + + // one event for all listeners + eventEmitter.emit("event", 1); + // another event for a and b, since c should have turned off already + eventEmitter.emit("event", 10); + + // make sure events haven't actually fired yet, just queued + Assert.deepEqual( + evidence, + { + a: 0, + b: 0, + c: 0, + log: "", + }, + "events are fired async" + ); + + // Spin the event loop to run events, so we can safely "off" + await Promise.resolve(); + + // Check intermediate event results + Assert.deepEqual( + evidence, + { + a: 11, + b: 11, + c: 1, + log: "abcab", + }, + "intermediate events are fired" + ); + + // one more event for a + eventEmitter.off("event", listenerB); + eventEmitter.emit("event", 100); + + // And another unrelated event + eventEmitter.on("nothing"); + + // Spin the event loop to run events + await Promise.resolve(); + + Assert.deepEqual( + evidence, + { + a: 111, + b: 11, + c: 1, + log: "abcaba", // events are in order + }, + "events fired as expected" + ); + + // Test that mutating the data passed to the event doesn't actually + // mutate it for other events. + let handlerRunCount = 0; + const mutationHandler = data => { + handlerRunCount++; + data.count++; + is(data.count, 1, "Event data is not mutated between handlers."); + }; + eventEmitter.on("mutationTest", mutationHandler); + eventEmitter.on("mutationTest", mutationHandler); + + const data = { count: 0 }; + eventEmitter.emit("mutationTest", data); + await Promise.resolve(); + + is(handlerRunCount, 2, "Mutation handler was executed twice."); + is(data.count, 0, "Event data cannot be mutated by handlers."); +}); diff --git a/toolkit/components/normandy/test/browser/browser_Heartbeat.js b/toolkit/components/normandy/test/browser/browser_Heartbeat.js new file mode 100644 index 0000000000..bcc73811f2 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_Heartbeat.js @@ -0,0 +1,268 @@ +"use strict"; + +const { Heartbeat } = ChromeUtils.importESModule( + "resource://normandy/lib/Heartbeat.sys.mjs" +); + +/** + * Assert an array is in non-descending order, and that every element is a number + */ +function assertOrdered(arr) { + for (let i = 0; i < arr.length; i++) { + Assert.equal(typeof arr[i], "number", `element ${i} is type "number"`); + } + for (let i = 0; i < arr.length - 1; i++) { + Assert.lessOrEqual( + arr[i], + arr[i + 1], + `element ${i} is less than or equal to element ${i + 1}` + ); + } +} + +/* Close every notification in a target window and notification box */ +function closeAllNotifications(targetWindow, notificationBox) { + if (notificationBox.allNotifications.length === 0) { + return Promise.resolve(); + } + + return new Promise(resolve => { + const notificationSet = new Set(notificationBox.allNotifications); + + const observer = new targetWindow.MutationObserver(mutations => { + for (const mutation of mutations) { + for (let i = 0; i < mutation.removedNodes.length; i++) { + const node = mutation.removedNodes.item(i); + if (notificationSet.has(node)) { + notificationSet.delete(node); + } + } + } + if (notificationSet.size === 0) { + Assert.equal( + notificationBox.allNotifications.length, + 0, + "No notifications left" + ); + observer.disconnect(); + resolve(); + } + }); + + observer.observe(notificationBox.stack, { childList: true }); + + for (const notification of notificationBox.allNotifications) { + notification.close(); + } + }); +} + +/* Check that the correct telemetry was sent */ +function assertTelemetrySent(hb, eventNames) { + return new Promise(resolve => { + hb.eventEmitter.once("TelemetrySent", payload => { + const events = [0]; + for (const name of eventNames) { + Assert.equal( + typeof payload[name], + "number", + `payload field ${name} is a number` + ); + events.push(payload[name]); + } + events.push(Date.now()); + + assertOrdered(events); + resolve(); + }); + }); +} + +function getStars(notice) { + return notice.buttonContainer.querySelectorAll(".star-x"); +} + +async function getUpdatedNotice(heartbeat) { + let notice = await heartbeat.noticePromise; + // ensure notice is updated and the DOM is ready to be queried + await notice.updateComplete; + return notice; +} + +add_setup(async function () { + let win = await BrowserTestUtils.openNewBrowserWindow(); + // Open a new tab to keep the window open. + await BrowserTestUtils.openNewForegroundTab( + win.gBrowser, + "https://example.com" + ); +}); + +// Several of the behaviors of heartbeat prompt are mutually exclusive, so checks are broken up +// into three batches. + +/* Batch #1 - General UI, Stars, and telemetry data */ +add_task(async function () { + const targetWindow = Services.wm.getMostRecentWindow("navigator:browser"); + const notificationBox = targetWindow.gNotificationBox; + + const preCount = notificationBox.allNotifications.length; + const hb = new Heartbeat(targetWindow, { + testing: true, + flowId: "test", + message: "test", + engagementButtonLabel: undefined, + learnMoreMessage: "Learn More", + learnMoreUrl: "https://example.org/learnmore", + }); + let notice = await getUpdatedNotice(hb); + + // Check UI + const learnMoreEl = notice.supportLinkEls[0]; + Assert.equal( + notificationBox.allNotifications.length, + preCount + 1, + "Correct number of notifications open" + ); + Assert.equal(getStars(notice).length, 5, "Correct number of stars"); + Assert.equal( + notice.buttonContainer.querySelectorAll(".notification-button").length, + 0, + "Engagement button not shown" + ); + Assert.equal( + learnMoreEl.href, + "https://example.org/learnmore", + "Learn more url correct" + ); + Assert.equal(learnMoreEl.value, "Learn More", "Learn more label correct"); + // There's a space included before the learn more link in proton. + Assert.equal( + notice.messageText.textContent.trim(), + "test", + "Message is correct" + ); + + // Check that when clicking the learn more link, a tab opens with the right URL + let loadedPromise; + const tabOpenPromise = new Promise(resolve => { + targetWindow.gBrowser.tabContainer.addEventListener( + "TabOpen", + event => { + let tab = event.target; + loadedPromise = BrowserTestUtils.browserLoaded( + tab.linkedBrowser, + true, + url => url && url !== "about:blank" + ); + resolve(tab); + }, + { once: true } + ); + }); + learnMoreEl.click(); + const tab = await tabOpenPromise; + const tabUrl = await loadedPromise; + + Assert.equal( + tabUrl, + "https://example.org/learnmore", + "Learn more link opened the right url" + ); + + const telemetrySentPromise = assertTelemetrySent(hb, [ + "offeredTS", + "learnMoreTS", + "closedTS", + ]); + // Close notification to trigger telemetry to be sent + await closeAllNotifications(targetWindow, notificationBox); + await telemetrySentPromise; + BrowserTestUtils.removeTab(tab); +}); + +// Batch #2 - Engagement buttons +add_task(async function () { + const targetWindow = Services.wm.getMostRecentWindow("navigator:browser"); + const notificationBox = targetWindow.gNotificationBox; + const hb = new Heartbeat(targetWindow, { + testing: true, + flowId: "test", + message: "test", + engagementButtonLabel: "Click me!", + postAnswerUrl: "https://example.org/postAnswer", + learnMoreMessage: "Learn More", + learnMoreUrl: "https://example.org/learnMore", + }); + let notice = await getUpdatedNotice(hb); + const engagementButton = notice.buttonContainer.querySelector( + ".notification-button" + ); + + Assert.equal(getStars(notice).length, 0, "Stars not shown"); + Assert.ok(engagementButton, "Engagement button added"); + Assert.equal( + engagementButton.label, + "Click me!", + "Engagement button has correct label" + ); + + let loadedPromise; + const tabOpenPromise = new Promise(resolve => { + targetWindow.gBrowser.tabContainer.addEventListener( + "TabOpen", + event => { + let tab = event.target; + loadedPromise = BrowserTestUtils.browserLoaded( + tab.linkedBrowser, + true, + url => url && url !== "about:blank" + ); + resolve(tab); + }, + { once: true } + ); + }); + engagementButton.click(); + const tab = await tabOpenPromise; + const tabUrl = await loadedPromise; + // the postAnswer url gets query parameters appended onto the end, so use Assert.startsWith instead of Assert.equal + Assert.ok( + tabUrl.startsWith("https://example.org/postAnswer"), + "Engagement button opened the right url" + ); + + const telemetrySentPromise = assertTelemetrySent(hb, [ + "offeredTS", + "engagedTS", + "closedTS", + ]); + // Close notification to trigger telemetry to be sent + await closeAllNotifications(targetWindow, notificationBox); + await telemetrySentPromise; + BrowserTestUtils.removeTab(tab); +}); + +// Batch 3 - Closing the window while heartbeat is open +add_task(async function () { + const targetWindow = await BrowserTestUtils.openNewBrowserWindow(); + + const hb = new Heartbeat(targetWindow, { + testing: true, + flowId: "test", + message: "test", + }); + + const telemetrySentPromise = assertTelemetrySent(hb, [ + "offeredTS", + "windowClosedTS", + ]); + // triggers sending ping to normandy + await BrowserTestUtils.closeWindow(targetWindow); + await telemetrySentPromise; +}); + +add_task(async function cleanup() { + const win = Services.wm.getMostRecentWindow("navigator:browser"); + await BrowserTestUtils.closeWindow(win); +}); diff --git a/toolkit/components/normandy/test/browser/browser_LegacyHeartbeat.js b/toolkit/components/normandy/test/browser/browser_LegacyHeartbeat.js new file mode 100644 index 0000000000..465e5c1040 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_LegacyHeartbeat.js @@ -0,0 +1,88 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { ClientEnvironment } = ChromeUtils.importESModule( + "resource://normandy/lib/ClientEnvironment.sys.mjs" +); +const { Heartbeat } = ChromeUtils.importESModule( + "resource://normandy/lib/Heartbeat.sys.mjs" +); +const { Normandy } = ChromeUtils.importESModule( + "resource://normandy/Normandy.sys.mjs" +); +const { ExperimentAPI } = ChromeUtils.importESModule( + "resource://nimbus/ExperimentAPI.sys.mjs" +); +const { ExperimentFakes } = ChromeUtils.importESModule( + "resource://testing-common/NimbusTestUtils.sys.mjs" +); +const { RecipeRunner } = ChromeUtils.importESModule( + "resource://normandy/lib/RecipeRunner.sys.mjs" +); +const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" +); + +const SURVEY = { + surveyId: "a survey", + message: "test message", + engagementButtonLabel: "", + thanksMessage: "thanks!", + postAnswerUrl: "https://example.com", + learnMoreMessage: "Learn More", + learnMoreUrl: "https://example.com", + repeatOption: "once", +}; + +function assertSurvey(actual, expected) { + for (const key of Object.keys(actual)) { + if (["postAnswerUrl", "flowId"].includes(key)) { + continue; + } + + Assert.equal( + actual[key], + expected[key], + `Heartbeat should receive correct ${key} parameter` + ); + } + + Assert.ok(actual.postAnswerUrl.startsWith(expected.postAnswerUrl)); +} + +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testLegacyHeartbeatTrigger({ heartbeatClassStub }) { + const sandbox = sinon.createSandbox(); + + const cleanupEnrollment = await ExperimentFakes.enrollWithFeatureConfig({ + featureId: "legacyHeartbeat", + value: { + survey: SURVEY, + }, + }); + + const client = RemoteSettings("normandy-recipes-capabilities"); + sandbox.stub(client, "get").resolves([]); + + try { + await RecipeRunner.run(); + Assert.equal( + heartbeatClassStub.args.length, + 1, + "Heartbeat should be instantiated once" + ); + assertSurvey(heartbeatClassStub.args[0][1], SURVEY); + + await cleanupEnrollment(); + } finally { + sandbox.restore(); + } + } +); diff --git a/toolkit/components/normandy/test/browser/browser_LogManager.js b/toolkit/components/normandy/test/browser/browser_LogManager.js new file mode 100644 index 0000000000..65f64c994b --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_LogManager.js @@ -0,0 +1,30 @@ +"use strict"; + +const { LogManager } = ChromeUtils.importESModule( + "resource://normandy/lib/LogManager.sys.mjs" +); +const { Log } = ChromeUtils.importESModule( + "resource://gre/modules/Log.sys.mjs" +); + +add_task(async function () { + // Ensure that configuring the logger affects all generated loggers. + const firstLogger = LogManager.getLogger("first"); + LogManager.configure(5); + const secondLogger = LogManager.getLogger("second"); + is(firstLogger.level, 5, "First logger level inherited from root logger."); + is(secondLogger.level, 5, "Second logger level inherited from root logger."); + + // Ensure that our loggers have at least one appender. + LogManager.configure(Log.Level.Warn); + const logger = LogManager.getLogger("test"); + ok(!!logger.appenders.length, "Loggers have at least one appender."); + + // Ensure our loggers log to the console. + await new Promise(resolve => { + SimpleTest.waitForExplicitFinish(); + SimpleTest.monitorConsole(resolve, [{ message: /legend has it/ }]); + logger.warn("legend has it"); + SimpleTest.endMonitorConsole(); + }); +}); diff --git a/toolkit/components/normandy/test/browser/browser_Normandy.js b/toolkit/components/normandy/test/browser/browser_Normandy.js new file mode 100644 index 0000000000..7deba94ee8 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_Normandy.js @@ -0,0 +1,306 @@ +"use strict"; + +const { TelemetryUtils } = ChromeUtils.importESModule( + "resource://gre/modules/TelemetryUtils.sys.mjs" +); +const { Normandy } = ChromeUtils.importESModule( + "resource://normandy/Normandy.sys.mjs" +); +const { AddonRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonRollouts.sys.mjs" +); +const { PreferenceExperiments } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceExperiments.sys.mjs" +); +const { PreferenceRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceRollouts.sys.mjs" +); +const { RecipeRunner } = ChromeUtils.importESModule( + "resource://normandy/lib/RecipeRunner.sys.mjs" +); +const { + NormandyTestUtils: { factories }, +} = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +const experimentPref1 = "test.initExperimentPrefs1"; +const experimentPref2 = "test.initExperimentPrefs2"; +const experimentPref3 = "test.initExperimentPrefs3"; +const experimentPref4 = "test.initExperimentPrefs4"; + +function withStubInits() { + return function (testFunction) { + return decorate( + withStub(AddonRollouts, "init"), + withStub(AddonStudies, "init"), + withStub(PreferenceRollouts, "init"), + withStub(PreferenceExperiments, "init"), + withStub(RecipeRunner, "init"), + withStub(TelemetryEvents, "init"), + testFunction + ); + }; +} + +decorate_task( + withPrefEnv({ + set: [ + [`app.normandy.startupExperimentPrefs.${experimentPref1}`, true], + [`app.normandy.startupExperimentPrefs.${experimentPref2}`, 2], + [`app.normandy.startupExperimentPrefs.${experimentPref3}`, "string"], + ], + }), + async function testApplyStartupPrefs() { + const defaultBranch = Services.prefs.getDefaultBranch(""); + for (const pref of [experimentPref1, experimentPref2, experimentPref3]) { + is( + defaultBranch.getPrefType(pref), + defaultBranch.PREF_INVALID, + `Pref ${pref} don't exist before being initialized.` + ); + } + + let oldValues = Normandy.applyStartupPrefs( + "app.normandy.startupExperimentPrefs." + ); + + Assert.deepEqual( + oldValues, + { + [experimentPref1]: null, + [experimentPref2]: null, + [experimentPref3]: null, + }, + "the correct set of old values should be reported" + ); + + ok( + defaultBranch.getBoolPref(experimentPref1), + `Pref ${experimentPref1} has a default value after being initialized.` + ); + is( + defaultBranch.getIntPref(experimentPref2), + 2, + `Pref ${experimentPref2} has a default value after being initialized.` + ); + is( + defaultBranch.getCharPref(experimentPref3), + "string", + `Pref ${experimentPref3} has a default value after being initialized.` + ); + + for (const pref of [experimentPref1, experimentPref2, experimentPref3]) { + ok( + !defaultBranch.prefHasUserValue(pref), + `Pref ${pref} doesn't have a user value after being initialized.` + ); + Services.prefs.clearUserPref(pref); + defaultBranch.deleteBranch(pref); + } + } +); + +decorate_task( + withPrefEnv({ + set: [ + ["app.normandy.startupExperimentPrefs.test.existingPref", "experiment"], + ], + }), + async function testApplyStartupPrefsExisting() { + const defaultBranch = Services.prefs.getDefaultBranch(""); + defaultBranch.setCharPref("test.existingPref", "default"); + Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs."); + is( + defaultBranch.getCharPref("test.existingPref"), + "experiment", + "applyStartupPrefs overwrites the default values of existing preferences." + ); + } +); + +decorate_task( + withPrefEnv({ + set: [ + ["app.normandy.startupExperimentPrefs.test.mismatchPref", "experiment"], + ], + }), + async function testApplyStartupPrefsMismatch() { + const defaultBranch = Services.prefs.getDefaultBranch(""); + defaultBranch.setIntPref("test.mismatchPref", 2); + Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs."); + is( + defaultBranch.getPrefType("test.mismatchPref"), + Services.prefs.PREF_INT, + "applyStartupPrefs skips prefs that don't match the existing default value's type." + ); + } +); + +decorate_task( + withStub(Normandy, "finishInit"), + async function testStartupDelayed({ finishInitStub }) { + let originalDeferred = Normandy.uiAvailableNotificationObserved; + let mockUiAvailableDeferred = Promise.withResolvers(); + Normandy.uiAvailableNotificationObserved = mockUiAvailableDeferred; + + let initPromise = Normandy.init(); + await null; + + ok( + !finishInitStub.called, + "When initialized, do not call finishInit immediately." + ); + + Normandy.observe(null, "sessionstore-windows-restored"); + await initPromise; + ok( + finishInitStub.called, + "Once the sessionstore-windows-restored event is observed, finishInit should be called." + ); + + Normandy.uiAvailableNotificationObserved = originalDeferred; + } +); + +// During startup, preferences that are changed for experiments should +// be record by calling PreferenceExperiments.recordOriginalValues. +decorate_task( + withStub(PreferenceExperiments, "recordOriginalValues", { + as: "experimentsRecordOriginalValuesStub", + }), + withStub(PreferenceRollouts, "recordOriginalValues", { + as: "rolloutsRecordOriginalValueStub", + }), + async function testApplyStartupPrefs({ + experimentsRecordOriginalValuesStub, + rolloutsRecordOriginalValueStub, + }) { + const defaultBranch = Services.prefs.getDefaultBranch(""); + + defaultBranch.setBoolPref(experimentPref1, false); + defaultBranch.setIntPref(experimentPref2, 1); + defaultBranch.setCharPref(experimentPref3, "original string"); + // experimentPref4 is left unset + + Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs."); + Normandy.studyPrefsChanged = { "test.study-pref": 1 }; + Normandy.rolloutPrefsChanged = { "test.rollout-pref": 1 }; + await Normandy.finishInit(); + + Assert.deepEqual( + experimentsRecordOriginalValuesStub.args, + [[{ "test.study-pref": 1 }]], + "finishInit should record original values of the study prefs" + ); + Assert.deepEqual( + rolloutsRecordOriginalValueStub.args, + [[{ "test.rollout-pref": 1 }]], + "finishInit should record original values of the study prefs" + ); + + // cleanup + defaultBranch.deleteBranch(experimentPref1); + defaultBranch.deleteBranch(experimentPref2); + defaultBranch.deleteBranch(experimentPref3); + } +); + +// Test that startup prefs are handled correctly when there is a value on the user branch but not the default branch. +decorate_task( + withPrefEnv({ + set: [ + ["app.normandy.startupExperimentPrefs.testing.does-not-exist", "foo"], + ["testing.does-not-exist", "foo"], + ], + }), + withStub(PreferenceExperiments, "recordOriginalValues"), + async function testApplyStartupPrefsNoDefaultValue() { + Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs"); + ok( + true, + "initExperimentPrefs should not throw for prefs that doesn't exist on the default branch" + ); + } +); + +decorate_task(withStubInits(), async function testStartup() { + const initObserved = TestUtils.topicObserved("shield-init-complete"); + await Normandy.finishInit(); + ok(AddonStudies.init.called, "startup calls AddonStudies.init"); + ok( + PreferenceExperiments.init.called, + "startup calls PreferenceExperiments.init" + ); + ok(RecipeRunner.init.called, "startup calls RecipeRunner.init"); + await initObserved; +}); + +decorate_task(withStubInits(), async function testStartupPrefInitFail() { + PreferenceExperiments.init.rejects(); + + await Normandy.finishInit(); + ok(AddonStudies.init.called, "startup calls AddonStudies.init"); + ok(AddonRollouts.init.called, "startup calls AddonRollouts.init"); + ok( + PreferenceExperiments.init.called, + "startup calls PreferenceExperiments.init" + ); + ok(RecipeRunner.init.called, "startup calls RecipeRunner.init"); + ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init"); + ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init"); +}); + +decorate_task( + withStubInits(), + async function testStartupAddonStudiesInitFail() { + AddonStudies.init.rejects(); + + await Normandy.finishInit(); + ok(AddonStudies.init.called, "startup calls AddonStudies.init"); + ok(AddonRollouts.init.called, "startup calls AddonRollouts.init"); + ok( + PreferenceExperiments.init.called, + "startup calls PreferenceExperiments.init" + ); + ok(RecipeRunner.init.called, "startup calls RecipeRunner.init"); + ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init"); + ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init"); + } +); + +decorate_task( + withStubInits(), + async function testStartupTelemetryEventsInitFail() { + TelemetryEvents.init.throws(); + + await Normandy.finishInit(); + ok(AddonStudies.init.called, "startup calls AddonStudies.init"); + ok(AddonRollouts.init.called, "startup calls AddonRollouts.init"); + ok( + PreferenceExperiments.init.called, + "startup calls PreferenceExperiments.init" + ); + ok(RecipeRunner.init.called, "startup calls RecipeRunner.init"); + ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init"); + ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init"); + } +); + +decorate_task( + withStubInits(), + async function testStartupPreferenceRolloutsInitFail() { + PreferenceRollouts.init.throws(); + + await Normandy.finishInit(); + ok(AddonStudies.init.called, "startup calls AddonStudies.init"); + ok(AddonRollouts.init.called, "startup calls AddonRollouts.init"); + ok( + PreferenceExperiments.init.called, + "startup calls PreferenceExperiments.init" + ); + ok(RecipeRunner.init.called, "startup calls RecipeRunner.init"); + ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init"); + ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init"); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_NormandyAddonManager.js b/toolkit/components/normandy/test/browser/browser_NormandyAddonManager.js new file mode 100644 index 0000000000..d43f310659 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_NormandyAddonManager.js @@ -0,0 +1,189 @@ +"use strict"; + +const { NormandyAddonManager } = ChromeUtils.importESModule( + "resource://normandy/lib/NormandyAddonManager.sys.mjs" +); + +decorate_task(ensureAddonCleanup(), async function download_and_install() { + const applyDeferred = Promise.withResolvers(); + + const [addonId, addonVersion] = await NormandyAddonManager.downloadAndInstall( + { + extensionDetails: { + extension_id: FIXTURE_ADDON_ID, + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + hash_algorithm: "sha256", + version: "1.0", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + }, + applyNormandyChanges: () => { + applyDeferred.resolve(); + }, + createError: () => {}, + reportError: () => {}, + undoNormandyChanges: () => {}, + } + ); + + // Ensure applyNormandyChanges was called + await applyDeferred; + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, addonId, "add-on is installed"); + is(addon.version, addonVersion, "add-on version is correct"); + + // Cleanup + await addon.uninstall(); +}); + +decorate_task(ensureAddonCleanup(), async function id_mismatch() { + const applyDeferred = Promise.withResolvers(); + const undoDeferred = Promise.withResolvers(); + + let error; + + try { + await NormandyAddonManager.downloadAndInstall({ + extensionDetails: { + extension_id: FIXTURE_ADDON_ID, + hash: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].hash, + hash_algorithm: "sha256", + version: "1.0", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].url, + }, + applyNormandyChanges: () => { + applyDeferred.resolve(); + }, + createError: (reason, extra) => { + return [reason, extra]; + }, + reportError: err => { + return err; + }, + undoNormandyChanges: () => { + undoDeferred.resolve(); + }, + }); + } catch ([reason, extra]) { + error = true; + is(reason, "metadata-mismatch", "the expected reason is provided"); + Assert.deepEqual( + extra, + undefined, + "the expected extra details are provided" + ); + } + + ok(error, "an error occured"); + + // Ensure applyNormandyChanges was called + await applyDeferred; + + // Ensure undoNormandyChanges was called + await undoDeferred; + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + ok(!addon, "add-on is not installed"); +}); + +decorate_task(ensureAddonCleanup(), async function version_mismatch() { + const applyDeferred = Promise.withResolvers(); + const undoDeferred = Promise.withResolvers(); + + let error; + + try { + await NormandyAddonManager.downloadAndInstall({ + extensionDetails: { + extension_id: FIXTURE_ADDON_ID, + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + hash_algorithm: "sha256", + version: "2.0", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + }, + applyNormandyChanges: () => { + applyDeferred.resolve(); + }, + createError: (reason, extra) => { + return [reason, extra]; + }, + reportError: err => { + return err; + }, + undoNormandyChanges: () => { + undoDeferred.resolve(); + }, + }); + } catch ([reason, extra]) { + error = true; + is(reason, "metadata-mismatch", "the expected reason is provided"); + Assert.deepEqual( + extra, + undefined, + "the expected extra details are provided" + ); + } + + ok(error, "should throw an error"); + + // Ensure applyNormandyChanges was called + await applyDeferred; + + // Ensure undoNormandyChanges was called + await undoDeferred; + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + ok(!addon, "add-on is not installed"); +}); + +decorate_task(ensureAddonCleanup(), async function download_failure() { + const applyDeferred = Promise.withResolvers(); + const undoDeferred = Promise.withResolvers(); + + let error; + + try { + await NormandyAddonManager.downloadAndInstall({ + extensionDetails: { + extension_id: FIXTURE_ADDON_ID, + hash: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].hash, + hash_algorithm: "sha256", + version: "1.0", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + }, + applyNormandyChanges: () => { + applyDeferred.resolve(); + }, + createError: (reason, extra) => { + return [reason, extra]; + }, + reportError: err => { + return err; + }, + undoNormandyChanges: () => { + undoDeferred.resolve(); + }, + }); + } catch ([reason, extra]) { + error = true; + is(reason, "download-failure", "the expected reason is provided"); + Assert.deepEqual( + extra, + { + detail: "ERROR_INCORRECT_HASH", + }, + "the expected extra details are provided" + ); + } + + ok(error, "an error occured"); + + // Ensure applyNormandyChanges was called + await applyDeferred; + + // Ensure undoNormandyChanges was called + await undoDeferred; + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + ok(!addon, "add-on is not installed"); +}); diff --git a/toolkit/components/normandy/test/browser/browser_NormandyMigrations.js b/toolkit/components/normandy/test/browser/browser_NormandyMigrations.js new file mode 100644 index 0000000000..9e60219c8b --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_NormandyMigrations.js @@ -0,0 +1,106 @@ +const { NormandyMigrations } = ChromeUtils.importESModule( + "resource://normandy/NormandyMigrations.sys.mjs" +); + +decorate_task( + withMockPreferences(), + async function testApplyMigrations({ mockPreferences }) { + const migrationsAppliedPref = "app.normandy.migrationsApplied"; + mockPreferences.set(migrationsAppliedPref, 0); + + await NormandyMigrations.applyAll(); + + is( + Services.prefs.getIntPref(migrationsAppliedPref), + NormandyMigrations.migrations.length, + "All migrations should have been applied" + ); + } +); + +decorate_task( + withMockPreferences(), + async function testPrefMigration({ mockPreferences }) { + const legacyPref = "extensions.shield-recipe-client.test"; + const migratedPref = "app.normandy.test"; + mockPreferences.set(legacyPref, 1); + + ok( + Services.prefs.prefHasUserValue(legacyPref), + "Legacy pref should have a user value before running migration" + ); + ok( + !Services.prefs.prefHasUserValue(migratedPref), + "Migrated pref should not have a user value before running migration" + ); + + await NormandyMigrations.applyOne(0); + + ok( + !Services.prefs.prefHasUserValue(legacyPref), + "Legacy pref should not have a user value after running migration" + ); + ok( + Services.prefs.prefHasUserValue(migratedPref), + "Migrated pref should have a user value after running migration" + ); + is( + Services.prefs.getIntPref(migratedPref), + 1, + "Value should have been migrated" + ); + + Services.prefs.clearUserPref(migratedPref); + } +); + +decorate_task( + withMockPreferences(), + async function testMigration0({ mockPreferences }) { + const studiesEnabledPref = "app.shield.optoutstudies.enabled"; + const healthReportUploadEnabledPref = + "datareporting.healthreport.uploadEnabled"; + + // Both enabled + mockPreferences.set(studiesEnabledPref, true); + mockPreferences.set(healthReportUploadEnabledPref, true); + await NormandyMigrations.applyOne(1); + ok( + Services.prefs.getBoolPref(studiesEnabledPref), + "Studies should be enabled." + ); + + mockPreferences.cleanup(); + + // Telemetry disabled, studies enabled + mockPreferences.set(studiesEnabledPref, true); + mockPreferences.set(healthReportUploadEnabledPref, false); + await NormandyMigrations.applyOne(1); + ok( + !Services.prefs.getBoolPref(studiesEnabledPref), + "Studies should be disabled." + ); + + mockPreferences.cleanup(); + + // Telemetry enabled, studies disabled + mockPreferences.set(studiesEnabledPref, false); + mockPreferences.set(healthReportUploadEnabledPref, true); + await NormandyMigrations.applyOne(1); + ok( + !Services.prefs.getBoolPref(studiesEnabledPref), + "Studies should be disabled." + ); + + mockPreferences.cleanup(); + + // Both disabled + mockPreferences.set(studiesEnabledPref, false); + mockPreferences.set(healthReportUploadEnabledPref, false); + await NormandyMigrations.applyOne(1); + ok( + !Services.prefs.getBoolPref(studiesEnabledPref), + "Studies should be disabled." + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_PreferenceExperiments.js b/toolkit/components/normandy/test/browser/browser_PreferenceExperiments.js new file mode 100644 index 0000000000..4269020975 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_PreferenceExperiments.js @@ -0,0 +1,2190 @@ +"use strict"; + +const { PreferenceExperiments } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceExperiments.sys.mjs" +); +const { CleanupManager } = ChromeUtils.importESModule( + "resource://normandy/lib/CleanupManager.sys.mjs" +); +const { NormandyUtils } = ChromeUtils.importESModule( + "resource://normandy/lib/NormandyUtils.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +// Save ourselves some typing +const { withMockExperiments } = PreferenceExperiments; +const DefaultPreferences = new Preferences({ defaultBranch: true }); +const startupPrefs = "app.normandy.startupExperimentPrefs"; +const { preferenceStudyFactory } = NormandyTestUtils.factories; + +const NOW = new Date(); + +const mockV1Data = { + hypothetical_experiment: { + name: "hypothetical_experiment", + branch: "hypo_1", + expired: false, + lastSeen: NOW.toJSON(), + preferenceName: "some.pref", + preferenceValue: 2, + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceBranchType: "user", + experimentType: "exp", + }, + another_experiment: { + name: "another_experiment", + branch: "another_4", + expired: true, + lastSeen: NOW.toJSON(), + preferenceName: "another.pref", + preferenceValue: true, + preferenceType: "boolean", + previousPreferenceValue: false, + preferenceBranchType: "default", + experimentType: "exp", + }, +}; + +const mockV2Data = { + experiments: { + hypothetical_experiment: { + name: "hypothetical_experiment", + branch: "hypo_1", + expired: false, + lastSeen: NOW.toJSON(), + preferenceName: "some.pref", + preferenceValue: 2, + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceBranchType: "user", + experimentType: "exp", + }, + another_experiment: { + name: "another_experiment", + branch: "another_4", + expired: true, + lastSeen: NOW.toJSON(), + preferenceName: "another.pref", + preferenceValue: true, + preferenceType: "boolean", + previousPreferenceValue: false, + preferenceBranchType: "default", + experimentType: "exp", + }, + }, +}; + +const mockV3Data = { + experiments: { + hypothetical_experiment: { + name: "hypothetical_experiment", + branch: "hypo_1", + expired: false, + lastSeen: NOW.toJSON(), + preferences: { + "some.pref": { + preferenceValue: 2, + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceBranchType: "user", + }, + }, + experimentType: "exp", + }, + another_experiment: { + name: "another_experiment", + branch: "another_4", + expired: true, + lastSeen: NOW.toJSON(), + preferences: { + "another.pref": { + preferenceValue: true, + preferenceType: "boolean", + previousPreferenceValue: false, + preferenceBranchType: "default", + }, + }, + experimentType: "exp", + }, + }, +}; + +const mockV4Data = { + experiments: { + hypothetical_experiment: { + name: "hypothetical_experiment", + branch: "hypo_1", + actionName: "SinglePreferenceExperimentAction", + expired: false, + lastSeen: NOW.toJSON(), + preferences: { + "some.pref": { + preferenceValue: 2, + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceBranchType: "user", + }, + }, + experimentType: "exp", + }, + another_experiment: { + name: "another_experiment", + branch: "another_4", + actionName: "SinglePreferenceExperimentAction", + expired: true, + lastSeen: NOW.toJSON(), + preferences: { + "another.pref": { + preferenceValue: true, + preferenceType: "boolean", + previousPreferenceValue: false, + preferenceBranchType: "default", + }, + }, + experimentType: "exp", + }, + }, +}; + +const mockV5Data = { + experiments: { + hypothetical_experiment: { + slug: "hypothetical_experiment", + branch: "hypo_1", + actionName: "SinglePreferenceExperimentAction", + expired: false, + lastSeen: NOW.toJSON(), + preferences: { + "some.pref": { + preferenceValue: 2, + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceBranchType: "user", + }, + }, + experimentType: "exp", + }, + another_experiment: { + slug: "another_experiment", + branch: "another_4", + actionName: "SinglePreferenceExperimentAction", + expired: true, + lastSeen: NOW.toJSON(), + preferences: { + "another.pref": { + preferenceValue: true, + preferenceType: "boolean", + previousPreferenceValue: false, + preferenceBranchType: "default", + }, + }, + experimentType: "exp", + }, + }, +}; + +const migrationsInfo = [ + { + migration: PreferenceExperiments.migrations.migration01MoveExperiments, + dataBefore: mockV1Data, + dataAfter: mockV2Data, + }, + { + migration: PreferenceExperiments.migrations.migration02MultiPreference, + dataBefore: mockV2Data, + dataAfter: mockV3Data, + }, + { + migration: PreferenceExperiments.migrations.migration03AddActionName, + dataBefore: mockV3Data, + dataAfter: mockV4Data, + }, + { + migration: PreferenceExperiments.migrations.migration04RenameNameToSlug, + dataBefore: mockV4Data, + dataAfter: mockV5Data, + }, + // Migration 5 is not a simple data migration. This style of tests does not apply to it. +]; + +/** + * Make a mock `JsonFile` object with a no-op `saveSoon` method and a deep copy + * of the data passed. + * @param {Object} data the data in the store + */ +function makeMockJsonFile(data = {}) { + return { + // Deep clone the data in case migrations mutate it. + data: JSON.parse(JSON.stringify(data)), + saveSoon: () => {}, + }; +} + +/** Test that each migration results in the expected data */ +add_task(async function test_migrations() { + for (const { migration, dataAfter, dataBefore } of migrationsInfo) { + let mockJsonFile = makeMockJsonFile(dataBefore); + await migration(mockJsonFile); + Assert.deepEqual( + mockJsonFile.data, + dataAfter, + `Migration ${migration.name} should result in the expected data` + ); + } +}); + +add_task(async function migrations_are_idempotent() { + for (const { migration, dataBefore } of migrationsInfo) { + const mockJsonFileOnce = makeMockJsonFile(dataBefore); + const mockJsonFileTwice = makeMockJsonFile(dataBefore); + await migration(mockJsonFileOnce); + await migration(mockJsonFileTwice); + await migration(mockJsonFileTwice); + Assert.deepEqual( + mockJsonFileOnce.data, + mockJsonFileTwice.data, + "migrating data twice should be idempotent for " + migration.name + ); + } +}); + +add_task(async function migration03KeepsActionName() { + let mockData = JSON.parse(JSON.stringify(mockV3Data)); + mockData.experiments.another_experiment.actionName = "SomeOldAction"; + const mockJsonFile = makeMockJsonFile(mockData); + // Output should be the same as mockV4Data, but preserving the action. + const migratedData = JSON.parse(JSON.stringify(mockV4Data)); + migratedData.experiments.another_experiment.actionName = "SomeOldAction"; + + await PreferenceExperiments.migrations.migration03AddActionName(mockJsonFile); + Assert.deepEqual(mockJsonFile.data, migratedData); +}); + +// Test that migration 5 works as expected +decorate_task( + withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ + actionName: "PreferenceExperimentAction", + expired: false, + }), + NormandyTestUtils.factories.preferenceStudyFactory({ + actionName: "SinglePreferenceExperimentAction", + expired: false, + }), + ]), + async function migration05Works({ prefExperiments: [expKeep, expExpire] }) { + // pre check + const activeSlugsBefore = (await PreferenceExperiments.getAllActive()).map( + e => e.slug + ); + Assert.deepEqual( + activeSlugsBefore, + [expKeep.slug, expExpire.slug], + "Both experiments should be present and active before the migration" + ); + + // run the migration + await PreferenceExperiments.migrations.migration05RemoveOldAction(); + + // verify behavior + const activeSlugsAfter = (await PreferenceExperiments.getAllActive()).map( + e => e.slug + ); + Assert.deepEqual( + activeSlugsAfter, + [expKeep.slug], + "The single pref experiment should be ended by the migration" + ); + const allSlugsAfter = (await PreferenceExperiments.getAll()).map( + e => e.slug + ); + Assert.deepEqual( + allSlugsAfter, + [expKeep.slug, expExpire.slug], + "Both experiments should still exist after the migration" + ); + } +); + +// clearAllExperimentStorage +decorate_task( + withMockExperiments([preferenceStudyFactory({ slug: "test" })]), + async function ({ prefExperiments }) { + ok(await PreferenceExperiments.has("test"), "Mock experiment is detected."); + await PreferenceExperiments.clearAllExperimentStorage(); + ok( + !(await PreferenceExperiments.has("test")), + "clearAllExperimentStorage removed all stored experiments" + ); + } +); + +// start should throw if an experiment with the given name already exists +decorate_task( + withMockExperiments([preferenceStudyFactory({ slug: "test" })]), + withSendEventSpy(), + async function ({ sendEventSpy }) { + await Assert.rejects( + PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "value", + preferenceType: "string", + preferenceBranchType: "default", + }, + }, + }), + /test.*already exists/, + "start threw an error due to a conflicting experiment name" + ); + + sendEventSpy.assertEvents([ + ["enrollFailed", "preference_study", "test", { reason: "name-conflict" }], + ]); + } +); + +// start should throw if an experiment for any of the given +// preferences are active +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + preferences: { "fake.preferenceinteger": {} }, + }), + ]), + withSendEventSpy(), + async function ({ sendEventSpy }) { + await Assert.rejects( + PreferenceExperiments.start({ + slug: "different", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "value", + preferenceType: "string", + preferenceBranchType: "default", + }, + "fake.preferenceinteger": { + preferenceValue: 2, + preferenceType: "integer", + preferenceBranchType: "default", + }, + }, + }), + /another.*is currently active/i, + "start threw an error due to an active experiment for the given preference" + ); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "preference_study", + "different", + { reason: "pref-conflict" }, + ], + ]); + } +); + +// start should throw if an invalid preferenceBranchType is given +decorate_task( + withMockExperiments(), + withSendEventSpy(), + async function ({ sendEventSpy }) { + await Assert.rejects( + PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "value", + preferenceType: "string", + preferenceBranchType: "invalid", + }, + }, + }), + /invalid value for preferenceBranchType: invalid/i, + "start threw an error due to an invalid preference branch type" + ); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "preference_study", + "test", + { reason: "invalid-branch" }, + ], + ]); + } +); + +// start should save experiment data, modify preferences, and register a +// watcher. +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "startObserver"), + withSendEventSpy(), + async function testStart({ + prefExperiments, + mockPreferences, + startObserverStub, + sendEventSpy, + }) { + mockPreferences.set("fake.preference", "oldvalue", "default"); + mockPreferences.set("fake.preference", "uservalue", "user"); + mockPreferences.set("fake.preferenceinteger", 1, "default"); + mockPreferences.set("fake.preferenceinteger", 101, "user"); + + const experiment = { + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "newvalue", + preferenceBranchType: "default", + preferenceType: "string", + }, + "fake.preferenceinteger": { + preferenceValue: 2, + preferenceBranchType: "default", + preferenceType: "integer", + }, + }, + }; + await PreferenceExperiments.start(experiment); + ok(await PreferenceExperiments.get("test"), "start saved the experiment"); + ok( + startObserverStub.calledWith("test", experiment.preferences), + "start registered an observer" + ); + + const expectedExperiment = { + slug: "test", + branch: "branch", + expired: false, + preferences: { + "fake.preference": { + preferenceValue: "newvalue", + preferenceType: "string", + previousPreferenceValue: "oldvalue", + preferenceBranchType: "default", + overridden: true, + }, + "fake.preferenceinteger": { + preferenceValue: 2, + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceBranchType: "default", + overridden: true, + }, + }, + }; + const experimentSubset = {}; + const actualExperiment = await PreferenceExperiments.get("test"); + Object.keys(expectedExperiment).forEach( + key => (experimentSubset[key] = actualExperiment[key]) + ); + Assert.deepEqual( + experimentSubset, + expectedExperiment, + "start saved the experiment" + ); + + is( + DefaultPreferences.get("fake.preference"), + "newvalue", + "start modified the default preference" + ); + is( + Preferences.get("fake.preference"), + "uservalue", + "start did not modify the user preference" + ); + is( + Preferences.get(`${startupPrefs}.fake.preference`), + "newvalue", + "start saved the experiment value to the startup prefs tree" + ); + is( + DefaultPreferences.get("fake.preferenceinteger"), + 2, + "start modified the default preference" + ); + is( + Preferences.get("fake.preferenceinteger"), + 101, + "start did not modify the user preference" + ); + is( + Preferences.get(`${startupPrefs}.fake.preferenceinteger`), + 2, + "start saved the experiment value to the startup prefs tree" + ); + } +); + +// start should modify the user preference for the user branch type +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "startObserver"), + async function ({ mockPreferences, startObserverStub }) { + mockPreferences.set("fake.preference", "olddefaultvalue", "default"); + mockPreferences.set("fake.preference", "oldvalue", "user"); + + const experiment = { + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "newvalue", + preferenceType: "string", + preferenceBranchType: "user", + }, + }, + }; + await PreferenceExperiments.start(experiment); + ok( + startObserverStub.calledWith("test", experiment.preferences), + "start registered an observer" + ); + + const expectedExperiment = { + slug: "test", + branch: "branch", + expired: false, + preferences: { + "fake.preference": { + preferenceValue: "newvalue", + preferenceType: "string", + previousPreferenceValue: "oldvalue", + preferenceBranchType: "user", + }, + }, + }; + + const experimentSubset = {}; + const actualExperiment = await PreferenceExperiments.get("test"); + Object.keys(expectedExperiment).forEach( + key => (experimentSubset[key] = actualExperiment[key]) + ); + Assert.deepEqual( + experimentSubset, + expectedExperiment, + "start saved the experiment" + ); + + Assert.notEqual( + DefaultPreferences.get("fake.preference"), + "newvalue", + "start did not modify the default preference" + ); + is( + Preferences.get("fake.preference"), + "newvalue", + "start modified the user preference" + ); + } +); + +// start should detect if a new preference value type matches the previous value type +decorate_task( + withMockPreferences(), + withSendEventSpy(), + async function ({ mockPreferences, sendEventSpy }) { + mockPreferences.set("fake.type_preference", "oldvalue"); + + await Assert.rejects( + PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.type_preference": { + preferenceBranchType: "user", + preferenceValue: 12345, + preferenceType: "integer", + }, + }, + }), + /previous preference value is of type/i, + "start threw error for incompatible preference type" + ); + + sendEventSpy.assertEvents([ + ["enrollFailed", "preference_study", "test", { reason: "invalid-type" }], + ]); + } +); + +// startObserver should throw if an observer for the experiment is already +// active. +decorate_task(withMockExperiments(), async function () { + PreferenceExperiments.startObserver("test", { + "fake.preference": { + preferenceType: "string", + preferenceValue: "newvalue", + }, + }); + Assert.throws( + () => + PreferenceExperiments.startObserver("test", { + "another.fake": { + preferenceType: "string", + preferenceValue: "othervalue", + }, + }), + /observer.*is already active/i, + "startObservers threw due to a conflicting active observer" + ); + PreferenceExperiments.stopAllObservers(); +}); + +// startObserver should register an observer that sends an event when preference +// changes from its experimental value. +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "recordPrefChange"), + async function testObserversCanObserveChanges({ + mockPreferences, + recordPrefChangeStub, + }) { + const preferences = { + "fake.preferencestring": { + preferenceType: "string", + previousPreferenceValue: "startvalue", + preferenceValue: "experimentvalue", + }, + // "newvalue", + "fake.preferenceboolean": { + preferenceType: "boolean", + previousPreferenceValue: false, + preferenceValue: true, + }, // false + "fake.preferenceinteger": { + preferenceType: "integer", + previousPreferenceValue: 1, + preferenceValue: 2, + }, // 42 + }; + const newValues = { + "fake.preferencestring": "newvalue", + "fake.preferenceboolean": false, + "fake.preferenceinteger": 42, + }; + + for (const [testPref, newValue] of Object.entries(newValues)) { + const experimentSlug = "test-" + testPref; + for (const [prefName, prefInfo] of Object.entries(preferences)) { + mockPreferences.set(prefName, prefInfo.previousPreferenceValue); + } + + // NOTE: startObserver does not modify the pref + PreferenceExperiments.startObserver(experimentSlug, preferences); + + // Setting it to the experimental value should not trigger the call. + for (const [prefName, prefInfo] of Object.entries(preferences)) { + mockPreferences.set(prefName, prefInfo.preferenceValue); + ok( + !recordPrefChangeStub.called, + "Changing to the experimental pref value did not trigger the observer" + ); + } + + // Setting it to something different should trigger the call. + mockPreferences.set(testPref, newValue); + Assert.deepEqual( + recordPrefChangeStub.args, + [[{ experimentSlug, preferenceName: testPref, reason: "observer" }]], + "Changing to a different value triggered the observer" + ); + + PreferenceExperiments.stopAllObservers(); + recordPrefChangeStub.resetHistory(); + } + } +); + +// Changes to prefs that have an experimental pref as a prefix should not trigger the observer. +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "recordPrefChange"), + async function testObserversCanObserveChanges({ + mockPreferences, + recordPrefChangeStub, + }) { + const preferences = { + "fake.preference": { + preferenceType: "string", + previousPreferenceValue: "startvalue", + preferenceValue: "experimentvalue", + }, + }; + + const experimentSlug = "test-prefix"; + for (const [prefName, prefInfo] of Object.entries(preferences)) { + mockPreferences.set(prefName, prefInfo.preferenceValue); + } + PreferenceExperiments.startObserver(experimentSlug, preferences); + + // Changing a preference that has the experimental pref as a prefix should + // not trigger the observer. + mockPreferences.set("fake.preference.extra", "value"); + // Setting it to the experimental value should not trigger the call. + ok( + !recordPrefChangeStub.called, + "Changing to the experimental pref value did not trigger the observer" + ); + + PreferenceExperiments.stopAllObservers(); + } +); + +decorate_task(withMockExperiments(), async function testHasObserver() { + PreferenceExperiments.startObserver("test", { + "fake.preference": { + preferenceType: "string", + preferenceValue: "experimentValue", + }, + }); + + ok( + await PreferenceExperiments.hasObserver("test"), + "hasObserver should detect active observers" + ); + ok( + !(await PreferenceExperiments.hasObserver("missing")), + "hasObserver shouldn't detect inactive observers" + ); + + PreferenceExperiments.stopAllObservers(); +}); + +// stopObserver should throw if there is no observer active for it to stop. +decorate_task(withMockExperiments(), async function () { + Assert.throws( + () => PreferenceExperiments.stopObserver("neveractive"), + /no observer.*found/i, + "stopObserver threw because there was not matching active observer" + ); +}); + +// stopObserver should cancel an active observers. +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "stop", { returnValue: Promise.resolve() }), + async function ({ mockPreferences, stopStub }) { + const preferenceInfo = { + "fake.preferencestring": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + "fake.preferenceinteger": { + preferenceType: "integer", + preferenceValue: 2, + }, + }; + mockPreferences.set("fake.preference", "startvalue"); + + PreferenceExperiments.startObserver("test", preferenceInfo); + PreferenceExperiments.stopObserver("test"); + + // Setting the preference now that the observer is stopped should not call + // stop. + mockPreferences.set("fake.preferencestring", "newvalue"); + ok( + !stopStub.called, + "stopObserver successfully removed the observer for string" + ); + + mockPreferences.set("fake.preferenceinteger", 42); + ok( + !stopStub.called, + "stopObserver successfully removed the observer for integer" + ); + + // Now that the observer is stopped, start should be able to start a new one + // without throwing. + try { + PreferenceExperiments.startObserver("test", preferenceInfo); + } catch (err) { + ok( + false, + "startObserver did not throw an error for an observer that was already stopped" + ); + } + + PreferenceExperiments.stopAllObservers(); + } +); + +// stopAllObservers +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "stop", { returnValue: Promise.resolve() }), + async function ({ mockPreferences, stopStub }) { + mockPreferences.set("fake.preference", "startvalue"); + mockPreferences.set("other.fake.preference", "startvalue"); + + PreferenceExperiments.startObserver("test", { + "fake.preference": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + }); + PreferenceExperiments.startObserver("test2", { + "other.fake.preference": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + }); + PreferenceExperiments.stopAllObservers(); + + // Setting the preference now that the observers are stopped should not call + // stop. + mockPreferences.set("fake.preference", "newvalue"); + mockPreferences.set("other.fake.preference", "newvalue"); + ok(!stopStub.called, "stopAllObservers successfully removed all observers"); + + // Now that the observers are stopped, start should be able to start new + // observers without throwing. + try { + PreferenceExperiments.startObserver("test", { + "fake.preference": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + }); + PreferenceExperiments.startObserver("test2", { + "other.fake.preference": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + }); + } catch (err) { + ok( + false, + "startObserver did not throw an error for an observer that was already stopped" + ); + } + + PreferenceExperiments.stopAllObservers(); + } +); + +// markLastSeen should throw if it can't find a matching experiment +decorate_task(withMockExperiments(), async function () { + await Assert.rejects( + PreferenceExperiments.markLastSeen("neveractive"), + /could not find/i, + "markLastSeen threw because there was not a matching experiment" + ); +}); + +// markLastSeen should update the lastSeen date +const oldDate = new Date(1988, 10, 1).toJSON(); +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ slug: "test", lastSeen: oldDate }), + ]), + async function ({ prefExperiments: [experiment] }) { + await PreferenceExperiments.markLastSeen("test"); + Assert.notEqual( + experiment.lastSeen, + oldDate, + "markLastSeen updated the experiment lastSeen date" + ); + } +); + +// stop should throw if an experiment with the given name doesn't exist +decorate_task( + withMockExperiments(), + withSendEventSpy(), + async function ({ sendEventSpy }) { + await Assert.rejects( + PreferenceExperiments.stop("test"), + /could not find/i, + "stop threw an error because there are no experiments with the given name" + ); + + sendEventSpy.assertEvents([ + [ + "unenrollFailed", + "preference_study", + "test", + { reason: "does-not-exist" }, + ], + ]); + } +); + +// stop should throw if the experiment is already expired +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ slug: "test", expired: true }), + ]), + withSendEventSpy(), + async function ({ sendEventSpy }) { + await Assert.rejects( + PreferenceExperiments.stop("test"), + /already expired/, + "stop threw an error because the experiment was already expired" + ); + + sendEventSpy.assertEvents([ + [ + "unenrollFailed", + "preference_study", + "test", + { reason: "already-unenrolled" }, + ], + ]); + } +); + +// stop should mark the experiment as expired, stop its observer, and revert the +// preference value. +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + expired: false, + branch: "fakebranch", + preferences: { + "fake.preference": { + preferenceValue: "experimentvalue", + preferenceType: "string", + previousPreferenceValue: "oldvalue", + preferenceBranchType: "default", + }, + }, + }), + ]), + withMockPreferences(), + withSpy(PreferenceExperiments, "stopObserver"), + withSendEventSpy(), + async function testStop({ mockPreferences, stopObserverSpy, sendEventSpy }) { + // this assertion is mostly useful for --verify test runs, to make + // sure that tests clean up correctly. + ok(!Preferences.get("fake.preference"), "preference should start unset"); + + mockPreferences.set( + `${startupPrefs}.fake.preference`, + "experimentvalue", + "user" + ); + mockPreferences.set("fake.preference", "experimentvalue", "default"); + PreferenceExperiments.startObserver("test", { + "fake.preference": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + }); + + await PreferenceExperiments.stop("test", { reason: "test-reason" }); + ok(stopObserverSpy.calledWith("test"), "stop removed an observer"); + const experiment = await PreferenceExperiments.get("test"); + is(experiment.expired, true, "stop marked the experiment as expired"); + is( + DefaultPreferences.get("fake.preference"), + "oldvalue", + "stop reverted the preference to its previous value" + ); + ok( + !Services.prefs.prefHasUserValue(`${startupPrefs}.fake.preference`), + "stop cleared the startup preference for fake.preference." + ); + + sendEventSpy.assertEvents([ + [ + "unenroll", + "preference_study", + "test", + { + didResetValue: "true", + reason: "test-reason", + branch: "fakebranch", + }, + ], + ]); + + PreferenceExperiments.stopAllObservers(); + } +); + +// stop should also support user pref experiments +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + expired: false, + preferences: { + "fake.preference": { + preferenceValue: "experimentvalue", + preferenceType: "string", + previousPreferenceValue: "oldvalue", + preferenceBranchType: "user", + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "stopObserver"), + withStub(PreferenceExperiments, "hasObserver"), + async function testStopUserPrefs({ + mockPreferences, + stopObserverStub, + hasObserverStub, + }) { + hasObserverStub.returns(true); + + mockPreferences.set("fake.preference", "experimentvalue", "user"); + PreferenceExperiments.startObserver("test", { + "fake.preference": { + preferenceType: "string", + preferenceValue: "experimentvalue", + }, + }); + + await PreferenceExperiments.stop("test"); + ok(stopObserverStub.calledWith("test"), "stop removed an observer"); + const experiment = await PreferenceExperiments.get("test"); + is(experiment.expired, true, "stop marked the experiment as expired"); + is( + Preferences.get("fake.preference"), + "oldvalue", + "stop reverted the preference to its previous value" + ); + stopObserverStub.restore(); + PreferenceExperiments.stopAllObservers(); + } +); + +// stop should remove a preference that had no value prior to an experiment for user prefs +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + expired: false, + preferences: { + "fake.preference": { + preferenceValue: "experimentvalue", + preferenceType: "string", + previousPreferenceValue: null, + preferenceBranchType: "user", + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "stopObserver"), + async function ({ mockPreferences }) { + mockPreferences.set("fake.preference", "experimentvalue", "user"); + + await PreferenceExperiments.stop("test"); + ok( + !Preferences.isSet("fake.preference"), + "stop removed the preference that had no value prior to the experiment" + ); + } +); + +// stop should not modify a preference if resetValue is false +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + expired: false, + branch: "fakebranch", + preferences: { + "fake.preference": { + preferenceValue: "experimentvalue", + preferenceType: "string", + previousPreferenceValue: "oldvalue", + preferenceBranchType: "default", + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "stopObserver"), + withSendEventSpy(), + async function testStopReset({ mockPreferences, sendEventSpy }) { + mockPreferences.set("fake.preference", "customvalue", "default"); + + await PreferenceExperiments.stop("test", { + reason: "test-reason", + resetValue: false, + }); + is( + DefaultPreferences.get("fake.preference"), + "customvalue", + "stop did not modify the preference" + ); + sendEventSpy.assertEvents([ + [ + "unenroll", + "preference_study", + "test", + { + didResetValue: "false", + reason: "test-reason", + branch: "fakebranch", + }, + ], + ]); + } +); + +// stop should include the system that stopped it +decorate_task( + withMockExperiments([preferenceStudyFactory({ expired: true })]), + withSendEventSpy, + async function testStopUserPrefs([experiment], sendEventSpy) { + await Assert.rejects( + PreferenceExperiments.stop(experiment.slug, { + caller: "testCaller", + reason: "original-reason", + }), + /.*already expired.*/, + "Stopped an expired experiment should throw an exception" + ); + + const expectedExtra = { + reason: "already-unenrolled", + originalReason: "original-reason", + }; + if (AppConstants.NIGHTLY_BUILD) { + expectedExtra.caller = "testCaller"; + } + + sendEventSpy.assertEvents([ + ["unenrollFailed", "preference_study", experiment.slug, expectedExtra], + ]); + } +); + +// get should throw if no experiment exists with the given name +decorate_task(withMockExperiments(), async function () { + await Assert.rejects( + PreferenceExperiments.get("neverexisted"), + /could not find/i, + "get rejects if no experiment with the given name is found" + ); +}); + +// get +decorate_task( + withMockExperiments([preferenceStudyFactory({ slug: "test" })]), + async function ({ prefExperiments }) { + const experiment = await PreferenceExperiments.get("test"); + is(experiment.slug, "test", "get fetches the correct experiment"); + + // Modifying the fetched experiment must not edit the data source. + experiment.slug = "othername"; + const refetched = await PreferenceExperiments.get("test"); + is(refetched.slug, "test", "get returns a copy of the experiment"); + } +); + +// get all +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ slug: "experiment1", disabled: false }), + preferenceStudyFactory({ slug: "experiment2", disabled: true }), + ]), + async function testGetAll({ prefExperiments: [experiment1, experiment2] }) { + const fetchedExperiments = await PreferenceExperiments.getAll(); + is( + fetchedExperiments.length, + 2, + "getAll returns a list of all stored experiments" + ); + Assert.deepEqual( + fetchedExperiments.find(e => e.slug === "experiment1"), + experiment1, + "getAll returns a list with the correct experiments" + ); + const fetchedExperiment2 = fetchedExperiments.find( + e => e.slug === "experiment2" + ); + Assert.deepEqual( + fetchedExperiment2, + experiment2, + "getAll returns a list with the correct experiments, including disabled ones" + ); + + fetchedExperiment2.slug = "otherslug"; + is( + experiment2.slug, + "experiment2", + "getAll returns copies of the experiments" + ); + } +); + +// get all active +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "active", + expired: false, + }), + preferenceStudyFactory({ + slug: "inactive", + expired: true, + }), + ]), + withMockPreferences(), + async function testGetAllActive({ + prefExperiments: [activeExperiment, inactiveExperiment], + }) { + let allActiveExperiments = await PreferenceExperiments.getAllActive(); + Assert.deepEqual( + allActiveExperiments, + [activeExperiment], + "getAllActive only returns active experiments" + ); + + allActiveExperiments[0].slug = "newfakename"; + allActiveExperiments = await PreferenceExperiments.getAllActive(); + Assert.notEqual( + allActiveExperiments, + "newfakename", + "getAllActive returns copies of stored experiments" + ); + } +); + +// has +decorate_task( + withMockExperiments([preferenceStudyFactory({ slug: "test" })]), + async function () { + ok( + await PreferenceExperiments.has("test"), + "has returned true for a stored experiment" + ); + ok( + !(await PreferenceExperiments.has("missing")), + "has returned false for a missing experiment" + ); + } +); + +// init should register telemetry experiments +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + branch: "branch", + preferences: { + "fake.pref": { + preferenceValue: "experiment value", + preferenceBranchType: "default", + preferenceType: "string", + }, + }, + }), + ]), + withMockPreferences(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(PreferenceExperiments, "startObserver"), + async function testInit({ + prefExperiments, + mockPreferences, + setExperimentActiveStub, + }) { + mockPreferences.set("fake.pref", "experiment value"); + await PreferenceExperiments.init(); + ok( + setExperimentActiveStub.calledWith("test", "branch", { + type: "normandy-exp", + }), + "Experiment is registered by init" + ); + } +); + +// init should use the provided experiment type +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + branch: "branch", + preferences: { + "fake.pref": { + preferenceValue: "experiment value", + preferenceType: "string", + }, + }, + experimentType: "pref-test", + }), + ]), + withMockPreferences(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(PreferenceExperiments, "startObserver"), + async function testInit({ mockPreferences, setExperimentActiveStub }) { + mockPreferences.set("fake.pref", "experiment value"); + await PreferenceExperiments.init(); + ok( + setExperimentActiveStub.calledWith("test", "branch", { + type: "normandy-pref-test", + }), + "init should use the provided experiment type" + ); + } +); + +// starting and stopping experiments should register in telemetry +decorate_task( + withMockExperiments(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + async function testStartAndStopTelemetry({ + setExperimentActiveStub, + setExperimentInactiveStub, + sendEventSpy, + }) { + await PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "value", + preferenceType: "string", + preferenceBranchType: "default", + }, + }, + }); + + Assert.deepEqual( + setExperimentActiveStub.getCall(0).args, + ["test", "branch", { type: "normandy-exp" }], + "Experiment is registered by start()" + ); + await PreferenceExperiments.stop("test", { reason: "test-reason" }); + Assert.deepEqual( + setExperimentInactiveStub.args, + [["test"]], + "Experiment is unregistered by stop()" + ); + + sendEventSpy.assertEvents([ + [ + "enroll", + "preference_study", + "test", + { + experimentType: "exp", + branch: "branch", + }, + ], + [ + "unenroll", + "preference_study", + "test", + { + reason: "test-reason", + didResetValue: "true", + branch: "branch", + }, + ], + ]); + } +); + +// starting experiments should use the provided experiment type +decorate_task( + withMockExperiments(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + async function testInitTelemetryExperimentType({ + setExperimentActiveStub, + sendEventSpy, + }) { + await PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + "fake.preference": { + preferenceValue: "value", + preferenceType: "string", + preferenceBranchType: "default", + }, + }, + experimentType: "pref-test", + }); + + Assert.deepEqual( + setExperimentActiveStub.getCall(0).args, + ["test", "branch", { type: "normandy-pref-test" }], + "start() should register the experiment with the provided type" + ); + + sendEventSpy.assertEvents([ + [ + "enroll", + "preference_study", + "test", + { + experimentType: "pref-test", + branch: "branch", + }, + ], + ]); + + // start sets the passed preference in a way that is hard to mock. + // Reset the preference so it doesn't interfere with other tests. + Services.prefs.getDefaultBranch("fake.preference").deleteBranch(""); + } +); + +// When a default-branch experiment starts, and some preferences already have +// user set values, they should immediately send telemetry events. +decorate_task( + withMockExperiments(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + withMockPreferences(), + async function testOverriddenAtEnroll({ sendEventSpy, mockPreferences }) { + // consts for preference names to avoid typos + const prefNames = { + defaultNoOverride: "fake.preference.default-no-override", + defaultWithOverride: "fake.preference.default-with-override", + userNoOverride: "fake.preference.user-no-override", + userWithOverride: "fake.preference.user-with-override", + }; + + // Set up preferences for the test. Two preferences with only default + // values, and two preferences with both default and user values. + mockPreferences.set( + prefNames.defaultNoOverride, + "default value", + "default" + ); + mockPreferences.set( + prefNames.defaultWithOverride, + "default value", + "default" + ); + mockPreferences.set(prefNames.defaultWithOverride, "user value", "user"); + mockPreferences.set(prefNames.userNoOverride, "default value", "default"); + mockPreferences.set(prefNames.userWithOverride, "default value", "default"); + mockPreferences.set(prefNames.userWithOverride, "user value", "user"); + + // Start the experiment with two each of default-branch and user-branch + // methods, one each of which will already be overridden. + const { slug } = await PreferenceExperiments.start({ + slug: "test-experiment", + actionName: "someAction", + branch: "experimental-branch", + preferences: { + [prefNames.defaultNoOverride]: { + preferenceValue: "experimental value", + preferenceType: "string", + preferenceBranchType: "default", + }, + [prefNames.defaultWithOverride]: { + preferenceValue: "experimental value", + preferenceType: "string", + preferenceBranchType: "default", + }, + [prefNames.userNoOverride]: { + preferenceValue: "experimental value", + preferenceType: "string", + preferenceBranchType: "user", + }, + [prefNames.userWithOverride]: { + preferenceValue: "experimental value", + preferenceType: "string", + preferenceBranchType: "user", + }, + }, + experimentType: "pref-test", + }); + + sendEventSpy.assertEvents([ + [ + "enroll", + "preference_study", + slug, + { + experimentType: "pref-test", + branch: "experimental-branch", + }, + ], + [ + "expPrefChanged", + "preference_study", + slug, + { + preferenceName: prefNames.defaultWithOverride, + reason: "onEnroll", + }, + ], + ]); + } +); + +// Experiments shouldn't be recorded by init() in telemetry if they are expired +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "expired", + branch: "branch", + expired: true, + }), + ]), + withStub(TelemetryEnvironment, "setExperimentActive"), + async function testInitTelemetryExpired({ setExperimentActiveStub }) { + await PreferenceExperiments.init(); + ok( + !setExperimentActiveStub.called, + "Expired experiment is not registered by init" + ); + } +); + +// Experiments should record if the preference has been changed when init() is +// called and no previous override had been observed. +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + preferences: { + "fake.preference.1": { + preferenceValue: "experiment value 1", + preferenceType: "string", + overridden: false, + }, + "fake.preference.2": { + preferenceValue: "experiment value 2", + preferenceType: "string", + overridden: true, + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "recordPrefChange"), + async function testInitChanges({ + mockPreferences, + recordPrefChangeStub, + prefExperiments: [experiment], + }) { + mockPreferences.set("fake.preference.1", "experiment value 1", "default"); + mockPreferences.set("fake.preference.1", "changed value 1", "user"); + mockPreferences.set("fake.preference.2", "experiment value 2", "default"); + mockPreferences.set("fake.preference.2", "changed value 2", "user"); + await PreferenceExperiments.init(); + + is( + Preferences.get("fake.preference.1"), + "changed value 1", + "Preference value was not changed" + ); + is( + Preferences.get("fake.preference.2"), + "changed value 2", + "Preference value was not changed" + ); + + Assert.deepEqual( + recordPrefChangeStub.args, + [ + [ + { + experiment, + preferenceName: "fake.preference.1", + reason: "sideload", + }, + ], + ], + "Only one experiment preference change should be recorded" + ); + } +); + +// init should register an observer for experiments +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + preferences: { + "fake.preference": { + preferenceValue: "experiment value", + preferenceType: "string", + previousPreferenceValue: "oldfakevalue", + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "startObserver"), + withStub(PreferenceExperiments, "stop"), + withStub(CleanupManager, "addCleanupHandler"), + async function testInitRegistersObserver({ + mockPreferences, + startObserverStub, + stopStub, + }) { + stopStub.throws("Stop should not be called"); + mockPreferences.set("fake.preference", "experiment value", "default"); + is( + Preferences.get("fake.preference"), + "experiment value", + "pref shouldn't have a user value" + ); + await PreferenceExperiments.init(); + + ok(startObserverStub.calledOnce, "init should register an observer"); + Assert.deepEqual( + startObserverStub.getCall(0).args, + [ + "test", + { + "fake.preference": { + preferenceType: "string", + preferenceValue: "experiment value", + previousPreferenceValue: "oldfakevalue", + preferenceBranchType: "default", + overridden: false, + }, + }, + ], + "init should register an observer with the right args" + ); + } +); + +// saveStartupPrefs +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "char", + preferences: { + "fake.char": { + preferenceValue: "string", + preferenceType: "string", + }, + }, + }), + preferenceStudyFactory({ + slug: "int", + preferences: { + "fake.int": { + preferenceValue: 2, + preferenceType: "int", + }, + }, + }), + preferenceStudyFactory({ + slug: "bool", + preferences: { + "fake.bool": { + preferenceValue: true, + preferenceType: "boolean", + }, + }, + }), + ]), + async function testSaveStartupPrefs() { + Services.prefs.deleteBranch(startupPrefs); + Services.prefs.setBoolPref(`${startupPrefs}.fake.old`, true); + await PreferenceExperiments.saveStartupPrefs(); + + ok( + Services.prefs.getBoolPref(`${startupPrefs}.fake.bool`), + "The startup value for fake.bool was saved." + ); + is( + Services.prefs.getCharPref(`${startupPrefs}.fake.char`), + "string", + "The startup value for fake.char was saved." + ); + is( + Services.prefs.getIntPref(`${startupPrefs}.fake.int`), + 2, + "The startup value for fake.int was saved." + ); + ok( + !Services.prefs.prefHasUserValue(`${startupPrefs}.fake.old`), + "saveStartupPrefs deleted old startup pref values." + ); + } +); + +// saveStartupPrefs errors for invalid pref type +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + preferences: { + "fake.invalidValue": { + preferenceValue: new Date(), + }, + }, + }), + ]), + async function testSaveStartupPrefsError() { + await Assert.rejects( + PreferenceExperiments.saveStartupPrefs(), + /invalid preference type/i, + "saveStartupPrefs throws if an experiment has an invalid preference value type" + ); + } +); + +// saveStartupPrefs should not store values for user-branch recipes +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "defaultBranchRecipe", + preferences: { + "fake.default": { + preferenceValue: "experiment value", + preferenceType: "string", + preferenceBranchType: "default", + }, + }, + }), + preferenceStudyFactory({ + slug: "userBranchRecipe", + preferences: { + "fake.user": { + preferenceValue: "experiment value", + preferenceType: "string", + preferenceBranchType: "user", + }, + }, + }), + ]), + async function testSaveStartupPrefsUserBranch() { + Assert.deepEqual( + Services.prefs.getChildList(startupPrefs), + [], + "As a prerequisite no startup prefs are set" + ); + + await PreferenceExperiments.saveStartupPrefs(); + + Assert.deepEqual( + Services.prefs.getChildList(startupPrefs), + [`${startupPrefs}.fake.default`], + "only the expected prefs are set" + ); + is( + Services.prefs.getCharPref( + `${startupPrefs}.fake.default`, + "fallback value" + ), + "experiment value", + "The startup value for fake.default was set" + ); + is( + Services.prefs.getPrefType(`${startupPrefs}.fake.user`), + Services.prefs.PREF_INVALID, + "The startup value for fake.user was not set" + ); + + Services.prefs.deleteBranch(startupPrefs); + } +); + +// test that default branch prefs restore to the right value if the default pref changes +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "startObserver"), + withStub(PreferenceExperiments, "stopObserver"), + async function testDefaultBranchStop({ mockPreferences }) { + const prefName = "fake.preference"; + mockPreferences.set(prefName, "old version's value", "default"); + + // start an experiment + await PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + [prefName]: { + preferenceValue: "experiment value", + preferenceBranchType: "default", + preferenceType: "string", + }, + }, + }); + + is( + Services.prefs.getCharPref(prefName), + "experiment value", + "Starting an experiment should change the pref" + ); + + // Now pretend that firefox has updated and restarted to a version + // where the built-default value of fake.preference is something + // else. Bootstrap has run and changed the pref to the + // experimental value, and produced the call to + // recordOriginalValues below. + PreferenceExperiments.recordOriginalValues({ + [prefName]: "new version's value", + }); + is( + Services.prefs.getCharPref(prefName), + "experiment value", + "Recording original values shouldn't affect the preference." + ); + + // Now stop the experiment. It should revert to the new version's default, not the old. + await PreferenceExperiments.stop("test"); + is( + Services.prefs.getCharPref(prefName), + "new version's value", + "Preference should revert to new default" + ); + } +); + +// test that default branch prefs restore to the right value if the preference is removed +decorate_task( + withMockExperiments(), + withMockPreferences(), + withStub(PreferenceExperiments, "startObserver"), + withStub(PreferenceExperiments, "stopObserver"), + async function testDefaultBranchStop({ mockPreferences }) { + const prefName = "fake.preference"; + mockPreferences.set(prefName, "old version's value", "default"); + + // start an experiment + await PreferenceExperiments.start({ + slug: "test", + actionName: "SomeAction", + branch: "branch", + preferences: { + [prefName]: { + preferenceValue: "experiment value", + preferenceBranchType: "default", + preferenceType: "string", + }, + }, + }); + + is( + Services.prefs.getCharPref(prefName), + "experiment value", + "Starting an experiment should change the pref" + ); + + // Now pretend that firefox has updated and restarted to a version + // where fake.preference has been removed in the default pref set. + // Bootstrap has run and changed the pref to the experimental + // value, and produced the call to recordOriginalValues below. + PreferenceExperiments.recordOriginalValues({ [prefName]: null }); + is( + Services.prefs.getCharPref(prefName), + "experiment value", + "Recording original values shouldn't affect the preference." + ); + + // Now stop the experiment. It should remove the preference + await PreferenceExperiments.stop("test"); + is( + Services.prefs.getCharPref(prefName, "DEFAULT"), + "DEFAULT", + "Preference should be absent" + ); + } +).skip(/* bug 1502410 and bug 1505941 */); + +// stop should pass "unknown" to telemetry event for `reason` if none is specified +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test", + preferences: { + "fake.preference": { + preferenceValue: "experiment value", + preferenceType: "string", + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "stopObserver"), + withSendEventSpy(), + async function testStopUnknownReason({ mockPreferences, sendEventSpy }) { + mockPreferences.set("fake.preference", "default value", "default"); + await PreferenceExperiments.stop("test"); + is( + sendEventSpy.getCall(0).args[3].reason, + "unknown", + "PreferenceExperiments.stop() should use unknown as the default reason" + ); + } +); + +// stop should pass along the value for resetValue to Telemetry Events as didResetValue +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + slug: "test1", + preferences: { + "fake.preference1": { + preferenceValue: "experiment value", + preferenceType: "string", + previousValue: "previous", + }, + }, + }), + preferenceStudyFactory({ + slug: "test2", + preferences: { + "fake.preference2": { + preferenceValue: "experiment value", + preferenceType: "string", + previousValue: "previous", + }, + }, + }), + ]), + withMockPreferences(), + withStub(PreferenceExperiments, "stopObserver"), + withSendEventSpy(), + async function testStopResetValue({ mockPreferences, sendEventSpy }) { + mockPreferences.set("fake.preference1", "default value", "default"); + await PreferenceExperiments.stop("test1", { resetValue: true }); + is(sendEventSpy.callCount, 1); + is( + sendEventSpy.getCall(0).args[3].didResetValue, + "true", + "PreferenceExperiments.stop() should pass true values of resetValue as didResetValue" + ); + + mockPreferences.set("fake.preference2", "default value", "default"); + await PreferenceExperiments.stop("test2", { resetValue: false }); + is(sendEventSpy.callCount, 2); + is( + sendEventSpy.getCall(1).args[3].didResetValue, + "false", + "PreferenceExperiments.stop() should pass false values of resetValue as didResetValue" + ); + } +); + +// `recordPrefChange` should send the right telemetry and mark the pref as +// overridden when passed an experiment +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + preferences: { + "test.pref": {}, + }, + }), + ]), + withSendEventSpy(), + async function testRecordPrefChangeWorks({ + sendEventSpy, + prefExperiments: [experiment], + }) { + is( + experiment.preferences["test.pref"].overridden, + false, + "Precondition: the pref should not be overridden yet" + ); + + await PreferenceExperiments.recordPrefChange({ + experiment, + preferenceName: "test.pref", + reason: "test-run", + }); + + experiment = await PreferenceExperiments.get(experiment.slug); + is( + experiment.preferences["test.pref"].overridden, + true, + "The pref should be marked as overridden" + ); + sendEventSpy.assertEvents([ + [ + "expPrefChanged", + "preference_study", + experiment.slug, + { + preferenceName: "test.pref", + reason: "test-run", + }, + ], + ]); + } +); + +// `recordPrefChange` should send the right telemetry and mark the pref as +// overridden when passed a slug +decorate_task( + withMockExperiments([ + preferenceStudyFactory({ + preferences: { + "test.pref": {}, + }, + }), + ]), + withSendEventSpy(), + async function testRecordPrefChangeWorks({ + sendEventSpy, + prefExperiments: [experiment], + }) { + is( + experiment.preferences["test.pref"].overridden, + false, + "Precondition: the pref should not be overridden yet" + ); + + await PreferenceExperiments.recordPrefChange({ + experimentSlug: experiment.slug, + preferenceName: "test.pref", + reason: "test-run", + }); + + experiment = await PreferenceExperiments.get(experiment.slug); + is( + experiment.preferences["test.pref"].overridden, + true, + "The pref should be marked as overridden" + ); + sendEventSpy.assertEvents([ + [ + "expPrefChanged", + "preference_study", + experiment.slug, + { + preferenceName: "test.pref", + reason: "test-run", + }, + ], + ]); + } +); + +// When a default-branch experiment starts, prefs that already have user values +// should not be changed. +decorate_task( + withMockExperiments(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + withMockPreferences(), + async function testOverriddenAtEnrollNoChange({ mockPreferences }) { + // Set up a situation where the user has changed the value of the pref away + // from the default. Then run a default experiment that changes the + // preference to the same value. + mockPreferences.set("test.pref", "old value", "default"); + mockPreferences.set("test.pref", "new value", "user"); + + await PreferenceExperiments.start({ + slug: "test-experiment", + actionName: "someAction", + branch: "experimental-branch", + preferences: { + "test.pref": { + preferenceValue: "new value", + preferenceType: "string", + preferenceBranchType: "default", + }, + }, + experimentType: "pref-test", + }); + + is( + Services.prefs.getCharPref("test.pref"), + "new value", + "User value should be preserved" + ); + is( + Services.prefs.getDefaultBranch("").getCharPref("test.pref"), + "old value", + "Default value should not have changed" + ); + + const experiment = await PreferenceExperiments.get("test-experiment"); + ok( + experiment.preferences["test.pref"].overridden, + "Pref should be marked as overridden" + ); + } +); + +// When a default-branch experiment starts, prefs that already exist and that +// have user values should not be changed. +// Bug 1735344: +// eslint-disable-next-line mozilla/reject-addtask-only +decorate_task( + withMockExperiments(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + withMockPreferences(), + async function testOverriddenAtEnrollNoChange({ mockPreferences }) { + // Set up a situation where the user has changed the value of the pref away + // from the default. Then run a default experiment that changes the + // preference to the same value. + + // An arbitrary string preference that won't interact with Normandy. + let pref = "extensions.recommendations.privacyPolicyUrl"; + let defaultValue = Services.prefs.getCharPref(pref); + + mockPreferences.set(pref, "user-set-value", "user"); + + await PreferenceExperiments.start({ + slug: "test-experiment", + actionName: "someAction", + branch: "experimental-branch", + preferences: { + [pref]: { + preferenceValue: "experiment-value", + preferenceType: "string", + preferenceBranchType: "default", + }, + }, + experimentType: "pref-test", + }); + + is( + Services.prefs.getCharPref(pref), + "user-set-value", + "User value should be preserved" + ); + is( + Services.prefs.getDefaultBranch("").getCharPref(pref), + defaultValue, + "Default value should not have changed" + ); + + const experiment = await PreferenceExperiments.get("test-experiment"); + ok( + experiment.preferences[pref].overridden, + "Pref should be marked as overridden" + ); + } +).only(); diff --git a/toolkit/components/normandy/test/browser/browser_PreferenceRollouts.js b/toolkit/components/normandy/test/browser/browser_PreferenceRollouts.js new file mode 100644 index 0000000000..52cc981536 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_PreferenceRollouts.js @@ -0,0 +1,288 @@ +"use strict"; + +const { IndexedDB } = ChromeUtils.importESModule( + "resource://gre/modules/IndexedDB.sys.mjs" +); + +const { PreferenceRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceRollouts.sys.mjs" +); +const { + NormandyTestUtils: { + factories: { preferenceRolloutFactory }, + }, +} = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +decorate_task( + PreferenceRollouts.withTestMock(), + async function testGetMissing() { + ok( + !(await PreferenceRollouts.get("does-not-exist")), + "get should return null when the requested rollout does not exist" + ); + } +); + +decorate_task( + PreferenceRollouts.withTestMock(), + async function testAddUpdateAndGet() { + const rollout = { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [], + }; + await PreferenceRollouts.add(rollout); + let storedRollout = await PreferenceRollouts.get(rollout.slug); + Assert.deepEqual( + rollout, + storedRollout, + "get should retrieve a rollout from storage." + ); + + rollout.state = PreferenceRollouts.STATE_GRADUATED; + await PreferenceRollouts.update(rollout); + storedRollout = await PreferenceRollouts.get(rollout.slug); + Assert.deepEqual( + rollout, + storedRollout, + "get should retrieve a rollout from storage." + ); + } +); + +decorate_task( + PreferenceRollouts.withTestMock(), + async function testCantUpdateNonexistent() { + const rollout = { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [], + }; + await Assert.rejects( + PreferenceRollouts.update(rollout), + /doesn't already exist/, + "Update should fail if the rollout doesn't exist" + ); + ok( + !(await PreferenceRollouts.has("test-rollout")), + "rollout should not have been added" + ); + } +); + +decorate_task(PreferenceRollouts.withTestMock(), async function testGetAll() { + const rollout1 = { + slug: "test-rollout-1", + preference: [], + }; + const rollout2 = { + slug: "test-rollout-2", + preference: [], + }; + await PreferenceRollouts.add(rollout1); + await PreferenceRollouts.add(rollout2); + + const storedRollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + storedRollouts.sort((a, b) => a.id - b.id), + [rollout1, rollout2], + "getAll should return every stored rollout." + ); +}); + +decorate_task( + PreferenceRollouts.withTestMock(), + async function testGetAllActive() { + const rollout1 = { + slug: "test-rollout-1", + state: PreferenceRollouts.STATE_ACTIVE, + }; + const rollout2 = { + slug: "test-rollout-2", + state: PreferenceRollouts.STATE_GRADUATED, + }; + const rollout3 = { + slug: "test-rollout-3", + state: PreferenceRollouts.STATE_ROLLED_BACK, + }; + await PreferenceRollouts.add(rollout1); + await PreferenceRollouts.add(rollout2); + await PreferenceRollouts.add(rollout3); + + const activeRollouts = await PreferenceRollouts.getAllActive(); + Assert.deepEqual( + activeRollouts, + [rollout1], + "getAllActive should return only active rollouts" + ); + } +); + +decorate_task(PreferenceRollouts.withTestMock(), async function testHas() { + const rollout = { + slug: "test-rollout", + preferences: [], + }; + await PreferenceRollouts.add(rollout); + ok( + await PreferenceRollouts.has(rollout.slug), + "has should return true for an existing rollout" + ); + ok( + !(await PreferenceRollouts.has("does not exist")), + "has should return false for a missing rollout" + ); +}); + +// recordOriginalValue should update storage to note the original values +decorate_task( + PreferenceRollouts.withTestMock(), + async function testRecordOriginalValuesUpdatesPreviousValues() { + await PreferenceRollouts.add({ + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref", value: 2, previousValue: null }, + ], + }); + + await PreferenceRollouts.recordOriginalValues({ "test.pref": 1 }); + + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref", value: 2, previousValue: 1 }, + ], + }, + ], + "rollout in database should be updated" + ); + } +); + +// recordOriginalValue should graduate a study when all of its preferences are built-in +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function testRecordOriginalValuesGraduates({ sendEventSpy }) { + await PreferenceRollouts.add({ + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref1", value: 2, previousValue: null }, + { preferenceName: "test.pref2", value: 2, previousValue: null }, + ], + }); + + // one pref being the same isn't enough to graduate + await PreferenceRollouts.recordOriginalValues({ + "test.pref1": 1, + "test.pref2": 2, + }); + let rollout = await PreferenceRollouts.get("test-rollout"); + is( + rollout.state, + PreferenceRollouts.STATE_ACTIVE, + "rollouts should remain active when only one pref matches the built-in default" + ); + + sendEventSpy.assertEvents([]); + + // both prefs is enough + await PreferenceRollouts.recordOriginalValues({ + "test.pref1": 2, + "test.pref2": 2, + }); + rollout = await PreferenceRollouts.get("test-rollout"); + is( + rollout.state, + PreferenceRollouts.STATE_GRADUATED, + "rollouts should graduate when all prefs matches the built-in defaults" + ); + + sendEventSpy.assertEvents([ + ["graduate", "preference_rollout", "test-rollout", {}], + ]); + } +); + +// init should mark active rollouts in telemetry +decorate_task( + withStub(TelemetryEnvironment, "setExperimentActive"), + PreferenceRollouts.withTestMock(), + async function testInitTelemetry({ setExperimentActiveStub }) { + await PreferenceRollouts.add({ + slug: "test-rollout-active-1", + state: PreferenceRollouts.STATE_ACTIVE, + }); + await PreferenceRollouts.add({ + slug: "test-rollout-active-2", + state: PreferenceRollouts.STATE_ACTIVE, + }); + await PreferenceRollouts.add({ + slug: "test-rollout-rolled-back", + state: PreferenceRollouts.STATE_ROLLED_BACK, + }); + await PreferenceRollouts.add({ + slug: "test-rollout-graduated", + state: PreferenceRollouts.STATE_GRADUATED, + }); + + await PreferenceRollouts.init(); + + Assert.deepEqual( + setExperimentActiveStub.args, + [ + ["test-rollout-active-1", "active", { type: "normandy-prefrollout" }], + ["test-rollout-active-2", "active", { type: "normandy-prefrollout" }], + ], + "init should set activate a telemetry experiment for active preferences" + ); + } +); + +// init should graduate rollouts in the graduation set +decorate_task( + withStub(TelemetryEnvironment, "setExperimentActive"), + withSendEventSpy(), + PreferenceRollouts.withTestMock({ + graduationSet: new Set(["test-rollout"]), + rollouts: [ + preferenceRolloutFactory({ + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + }), + ], + }), + async function testInitGraduationSet({ + setExperimentActiveStub, + sendEventSpy, + }) { + await PreferenceRollouts.init(); + const newRollout = await PreferenceRollouts.get("test-rollout"); + Assert.equal( + newRollout.state, + PreferenceRollouts.STATE_GRADUATED, + "the rollout should be graduated" + ); + Assert.deepEqual( + setExperimentActiveStub.args, + [], + "setExperimentActive should not be called" + ); + sendEventSpy.assertEvents([ + [ + "graduate", + "preference_rollout", + "test-rollout", + { reason: "in-graduation-set" }, + ], + ]); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_RecipeRunner.js b/toolkit/components/normandy/test/browser/browser_RecipeRunner.js new file mode 100644 index 0000000000..00f0f81c51 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_RecipeRunner.js @@ -0,0 +1,874 @@ +"use strict"; + +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); +const { FilterExpressions } = ChromeUtils.importESModule( + "resource://gre/modules/components-utils/FilterExpressions.sys.mjs" +); + +const { Normandy } = ChromeUtils.importESModule( + "resource://normandy/Normandy.sys.mjs" +); +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { RecipeRunner } = ChromeUtils.importESModule( + "resource://normandy/lib/RecipeRunner.sys.mjs" +); +const { ClientEnvironment } = ChromeUtils.importESModule( + "resource://normandy/lib/ClientEnvironment.sys.mjs" +); +const { CleanupManager } = ChromeUtils.importESModule( + "resource://normandy/lib/CleanupManager.sys.mjs" +); +const { ActionsManager } = ChromeUtils.importESModule( + "resource://normandy/lib/ActionsManager.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); + +const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" +); + +add_task(async function getFilterContext() { + const recipe = { id: 17, arguments: { foo: "bar" }, unrelated: false }; + const context = RecipeRunner.getFilterContext(recipe); + + // Test for expected properties in the filter expression context. + const expectedNormandyKeys = [ + "channel", + "country", + "distribution", + "doNotTrack", + "isDefaultBrowser", + "locale", + "plugins", + "recipe", + "request_time", + "searchEngine", + "syncDesktopDevices", + "syncMobileDevices", + "syncSetup", + "syncTotalDevices", + "telemetry", + "userId", + "version", + ]; + for (const key of expectedNormandyKeys) { + ok(key in context.env, `env.${key} is available`); + ok(key in context.normandy, `normandy.${key} is available`); + } + Assert.deepEqual( + context.normandy, + context.env, + "context offers normandy as backwards-compatible alias for context.environment" + ); + + is( + context.env.recipe.id, + recipe.id, + "environment.recipe is the recipe passed to getFilterContext" + ); + is( + ClientEnvironment.recipe, + undefined, + "ClientEnvironment has not been mutated" + ); + delete recipe.unrelated; + Assert.deepEqual( + context.env.recipe, + recipe, + "environment.recipe drops unrecognized attributes from the recipe" + ); + + // Filter context attributes are cached. + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.user_id", "some id"]], + }); + is(context.env.userId, "some id", "User id is read from prefs when accessed"); + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.user_id", "real id"]], + }); + is(context.env.userId, "some id", "userId was cached"); +}); + +add_task( + withStub(NormandyApi, "verifyObjectSignature"), + async function test_getRecipeSuitability_filterExpressions() { + const check = filter => + RecipeRunner.getRecipeSuitability({ filter_expression: filter }); + + // Errors must result in a false return value. + is( + await check("invalid ( + 5yntax"), + BaseAction.suitability.FILTER_ERROR, + "Invalid filter expressions return false" + ); + + // Non-boolean filter results result in a true return value. + is( + await check("[1, 2, 3]"), + BaseAction.suitability.FILTER_MATCH, + "Non-boolean filter expressions return true" + ); + + // The given recipe must be available to the filter context. + const recipe = { filter_expression: "normandy.recipe.id == 7", id: 7 }; + is( + await RecipeRunner.getRecipeSuitability(recipe), + BaseAction.suitability.FILTER_MATCH, + "The recipe is available in the filter context" + ); + recipe.id = 4; + is( + await RecipeRunner.getRecipeSuitability(recipe), + BaseAction.suitability.FILTER_MISMATCH, + "The recipe is available in the filter context" + ); + } +); + +decorate_task( + withStub(FilterExpressions, "eval"), + withStub(Uptake, "reportRecipe"), + withStub(NormandyApi, "verifyObjectSignature"), + async function test_getRecipeSuitability_canHandleExceptions({ + evalStub, + reportRecipeStub, + }) { + evalStub.throws("this filter was broken somehow"); + const someRecipe = { + id: "1", + action: "action", + filter_expression: "broken", + }; + const result = await RecipeRunner.getRecipeSuitability(someRecipe); + + is( + result, + BaseAction.suitability.FILTER_ERROR, + "broken filters are reported" + ); + Assert.deepEqual(reportRecipeStub.args, [ + [someRecipe, Uptake.RECIPE_FILTER_BROKEN], + ]); + } +); + +decorate_task( + withSpy(FilterExpressions, "eval"), + withStub(RecipeRunner, "getCapabilities"), + withStub(NormandyApi, "verifyObjectSignature"), + async function test_getRecipeSuitability_checksCapabilities({ + evalSpy, + getCapabilitiesStub, + }) { + getCapabilitiesStub.returns(new Set(["test-capability"])); + + is( + await RecipeRunner.getRecipeSuitability({ + filter_expression: "true", + }), + BaseAction.suitability.FILTER_MATCH, + "Recipes with no capabilities should pass" + ); + ok(evalSpy.called, "Filter should be evaluated"); + + evalSpy.resetHistory(); + is( + await RecipeRunner.getRecipeSuitability({ + capabilities: [], + filter_expression: "true", + }), + BaseAction.suitability.FILTER_MATCH, + "Recipes with empty capabilities should pass" + ); + ok(evalSpy.called, "Filter should be evaluated"); + + evalSpy.resetHistory(); + is( + await RecipeRunner.getRecipeSuitability({ + capabilities: ["test-capability"], + filter_expression: "true", + }), + BaseAction.suitability.FILTER_MATCH, + "Recipes with a matching capability should pass" + ); + ok(evalSpy.called, "Filter should be evaluated"); + + evalSpy.resetHistory(); + is( + await RecipeRunner.getRecipeSuitability({ + capabilities: ["impossible-capability"], + filter_expression: "true", + }), + BaseAction.suitability.CAPABILITIES_MISMATCH, + "Recipes with non-matching capabilities should not pass" + ); + ok(!evalSpy.called, "Filter should not be evaluated"); + } +); + +decorate_task( + withMockNormandyApi(), + withStub(ClientEnvironment, "getClientClassification"), + async function testClientClassificationCache({ + mockNormandyApi, + getClientClassificationStub, + }) { + getClientClassificationStub.returns(Promise.resolve(false)); + + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.api_url", "https://example.com/selfsupport-dummy"]], + }); + + // When the experiment pref is false, eagerly call getClientClassification. + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.experiments.lazy_classify", false]], + }); + ok( + !getClientClassificationStub.called, + "getClientClassification hasn't been called" + ); + await RecipeRunner.run(); + ok( + getClientClassificationStub.called, + "getClientClassification was called eagerly" + ); + + // When the experiment pref is true, do not eagerly call getClientClassification. + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.experiments.lazy_classify", true]], + }); + getClientClassificationStub.reset(); + ok( + !getClientClassificationStub.called, + + "getClientClassification hasn't been called" + ); + await RecipeRunner.run(); + ok( + !getClientClassificationStub.called, + + "getClientClassification was not called eagerly" + ); + } +); + +decorate_task( + withStub(Uptake, "reportRunner"), + withStub(ActionsManager.prototype, "finalize"), + NormandyTestUtils.withMockRecipeCollection([]), + async function testRunEvents() { + const startPromise = TestUtils.topicObserved("recipe-runner:start"); + const endPromise = TestUtils.topicObserved("recipe-runner:end"); + + await RecipeRunner.run(); + + // Will timeout if notifications were not received. + await startPromise; + await endPromise; + ok(true, "The test should pass without timing out"); + } +); + +decorate_task( + withStub(RecipeRunner, "getCapabilities"), + withStub(NormandyApi, "verifyObjectSignature"), + NormandyTestUtils.withMockRecipeCollection([{ id: 1 }]), + async function test_run_includesCapabilities({ getCapabilitiesStub }) { + getCapabilitiesStub.returns(new Set(["test-capability"])); + await RecipeRunner.run(); + ok(getCapabilitiesStub.called, "getCapabilities should be called"); + } +); + +decorate_task( + withStub(NormandyApi, "verifyObjectSignature"), + withStub(ActionsManager.prototype, "processRecipe"), + withStub(ActionsManager.prototype, "finalize"), + withStub(Uptake, "reportRecipe"), + async function testReadFromRemoteSettings({ + verifyObjectSignatureStub, + processRecipeStub, + finalizeStub, + reportRecipeStub, + }) { + const matchRecipe = { + id: 1, + name: "match", + action: "matchAction", + filter_expression: "true", + }; + const noMatchRecipe = { + id: 2, + name: "noMatch", + action: "noMatchAction", + filter_expression: "false", + }; + const missingRecipe = { + id: 3, + name: "missing", + action: "missingAction", + filter_expression: "true", + }; + + const db = await RecipeRunner._remoteSettingsClientForTesting.db; + const fakeSig = { signature: "abc" }; + await db.importChanges({}, Date.now(), [ + { id: "match", recipe: matchRecipe, signature: fakeSig }, + { + id: "noMatch", + recipe: noMatchRecipe, + signature: fakeSig, + }, + { + id: "missing", + recipe: missingRecipe, + signature: fakeSig, + }, + ]); + + let recipesFromRS = ( + await RecipeRunner._remoteSettingsClientForTesting.get() + ).map(({ recipe, signature }) => recipe); + // Sort the records by id so that they match the order in the assertion + recipesFromRS.sort((a, b) => a.id - b.id); + Assert.deepEqual( + recipesFromRS, + [matchRecipe, noMatchRecipe, missingRecipe], + "The recipes should be accesible from Remote Settings" + ); + + await RecipeRunner.run(); + + Assert.deepEqual( + verifyObjectSignatureStub.args, + [ + [matchRecipe, fakeSig, "recipe"], + [missingRecipe, fakeSig, "recipe"], + [noMatchRecipe, fakeSig, "recipe"], + ], + "all recipes should have their signature verified" + ); + Assert.deepEqual( + processRecipeStub.args, + [ + [matchRecipe, BaseAction.suitability.FILTER_MATCH], + [missingRecipe, BaseAction.suitability.FILTER_MATCH], + [noMatchRecipe, BaseAction.suitability.FILTER_MISMATCH], + ], + "Recipes should be reported with the correct suitabilities" + ); + Assert.deepEqual( + reportRecipeStub.args, + [[noMatchRecipe, Uptake.RECIPE_DIDNT_MATCH_FILTER]], + "Filtered-out recipes should be reported" + ); + } +); + +decorate_task( + withStub(NormandyApi, "verifyObjectSignature"), + withStub(ActionsManager.prototype, "processRecipe"), + withStub(RecipeRunner, "getCapabilities"), + async function testReadFromRemoteSettings({ + processRecipeStub, + getCapabilitiesStub, + }) { + getCapabilitiesStub.returns(new Set(["compatible"])); + const compatibleRecipe = { + name: "match", + filter_expression: "true", + capabilities: ["compatible"], + }; + const incompatibleRecipe = { + name: "noMatch", + filter_expression: "true", + capabilities: ["incompatible"], + }; + + const db = await RecipeRunner._remoteSettingsClientForTesting.db; + const fakeSig = { signature: "abc" }; + await db.importChanges( + {}, + Date.now(), + [ + { + id: "match", + recipe: compatibleRecipe, + signature: fakeSig, + }, + { + id: "noMatch", + recipe: incompatibleRecipe, + signature: fakeSig, + }, + ], + { + clear: true, + } + ); + + await RecipeRunner.run(); + + Assert.deepEqual( + processRecipeStub.args, + [ + [compatibleRecipe, BaseAction.suitability.FILTER_MATCH], + [incompatibleRecipe, BaseAction.suitability.CAPABILITIES_MISMATCH], + ], + "recipes should be marked if their capabilities aren't compatible" + ); + } +); + +decorate_task( + withStub(ActionsManager.prototype, "processRecipe"), + withStub(NormandyApi, "verifyObjectSignature"), + withStub(Uptake, "reportRecipe"), + NormandyTestUtils.withMockRecipeCollection(), + async function testBadSignatureFromRemoteSettings({ + processRecipeStub, + verifyObjectSignatureStub, + reportRecipeStub, + mockRecipeCollection, + }) { + verifyObjectSignatureStub.throws(new Error("fake signature error")); + const badSigRecipe = { + id: 1, + name: "badSig", + action: "matchAction", + filter_expression: "true", + }; + await mockRecipeCollection.addRecipes([badSigRecipe]); + + await RecipeRunner.run(); + + Assert.deepEqual(processRecipeStub.args, [ + [badSigRecipe, BaseAction.suitability.SIGNATURE_ERROR], + ]); + Assert.deepEqual( + reportRecipeStub.args, + [[badSigRecipe, Uptake.RECIPE_INVALID_SIGNATURE]], + "The recipe should have its uptake status recorded" + ); + } +); + +// Test init() during normal operation +decorate_task( + withPrefEnv({ + set: [ + ["datareporting.healthreport.uploadEnabled", true], // telemetry enabled + ["app.normandy.dev_mode", false], + ["app.normandy.first_run", false], + ], + }), + withStub(RecipeRunner, "run"), + withStub(RecipeRunner, "registerTimer"), + async function testInit({ runStub, registerTimerStub }) { + await RecipeRunner.init(); + ok( + !runStub.called, + "RecipeRunner.run should not be called immediately when not in dev mode or first run" + ); + ok(registerTimerStub.called, "RecipeRunner.init registers a timer"); + } +); + +// test init() in dev mode +decorate_task( + withPrefEnv({ + set: [ + ["datareporting.healthreport.uploadEnabled", true], // telemetry enabled + ["app.normandy.dev_mode", true], + ], + }), + withStub(RecipeRunner, "run"), + withStub(RecipeRunner, "registerTimer"), + withStub(RecipeRunner._remoteSettingsClientForTesting, "sync"), + async function testInitDevMode({ runStub, registerTimerStub, syncStub }) { + await RecipeRunner.init(); + Assert.deepEqual( + runStub.args, + [[{ trigger: "devMode" }]], + "RecipeRunner.run should be called immediately when in dev mode" + ); + ok(registerTimerStub.called, "RecipeRunner.init should register a timer"); + ok( + syncStub.called, + "RecipeRunner.init should sync remote settings in dev mode" + ); + } +); + +// Test init() first run +decorate_task( + withPrefEnv({ + set: [ + ["datareporting.healthreport.uploadEnabled", true], // telemetry enabled + ["app.normandy.dev_mode", false], + ["app.normandy.first_run", true], + ], + }), + withStub(RecipeRunner, "run"), + withStub(RecipeRunner, "registerTimer"), + withStub(RecipeRunner, "watchPrefs"), + async function testInitFirstRun({ + runStub, + registerTimerStub, + watchPrefsStub, + }) { + await RecipeRunner.init(); + Assert.deepEqual( + runStub.args, + [[{ trigger: "firstRun" }]], + "RecipeRunner.run is called immediately on first run" + ); + ok( + !Services.prefs.getBoolPref("app.normandy.first_run"), + "On first run, the first run pref is set to false" + ); + ok( + registerTimerStub.called, + "RecipeRunner.registerTimer registers a timer" + ); + + // RecipeRunner.init() sets this pref to false, but SpecialPowers + // relies on the preferences it manages to actually change when it + // tries to change them. Settings this back to true here allows + // that to happen. Not doing this causes popPrefEnv to hang forever. + Services.prefs.setBoolPref("app.normandy.first_run", true); + } +); + +// Test that prefs are watched correctly +decorate_task( + withPrefEnv({ + set: [ + ["app.normandy.dev_mode", false], + ["app.normandy.first_run", false], + ["app.normandy.enabled", true], + ], + }), + withStub(RecipeRunner, "run"), + withStub(RecipeRunner, "enable"), + withStub(RecipeRunner, "disable"), + withStub(CleanupManager, "addCleanupHandler"), + + async function testPrefWatching({ runStub, enableStub, disableStub }) { + await RecipeRunner.init(); + is(enableStub.callCount, 1, "Enable should be called initially"); + is(disableStub.callCount, 0, "Disable should not be called initially"); + + await SpecialPowers.pushPrefEnv({ set: [["app.normandy.enabled", false]] }); + is(enableStub.callCount, 1, "Enable should not be called again"); + is( + disableStub.callCount, + 1, + "RecipeRunner should disable when Shield is disabled" + ); + + await SpecialPowers.pushPrefEnv({ set: [["app.normandy.enabled", true]] }); + is( + enableStub.callCount, + 2, + "RecipeRunner should re-enable when Shield is enabled" + ); + is(disableStub.callCount, 1, "Disable should not be called again"); + + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.api_url", "http://example.com"]], + }); // does not start with https:// + is(enableStub.callCount, 2, "Enable should not be called again"); + is( + disableStub.callCount, + 2, + "RecipeRunner should disable when an invalid api url is given" + ); + + await SpecialPowers.pushPrefEnv({ + set: [["app.normandy.api_url", "https://example.com"]], + }); // ends with https:// + is( + enableStub.callCount, + 3, + "RecipeRunner should re-enable when a valid api url is given" + ); + is(disableStub.callCount, 2, "Disable should not be called again"); + + is( + runStub.callCount, + 0, + "RecipeRunner.run should not be called during this test" + ); + } +); + +// Test that enable and disable are idempotent +decorate_task( + withStub(RecipeRunner, "registerTimer"), + withStub(RecipeRunner, "unregisterTimer"), + async function testPrefWatching({ registerTimerStub }) { + const originalEnabled = RecipeRunner.enabled; + + try { + RecipeRunner.enabled = false; + RecipeRunner.enable(); + RecipeRunner.enable(); + RecipeRunner.enable(); + is(registerTimerStub.callCount, 1, "Enable should be idempotent"); + + RecipeRunner.enabled = true; + RecipeRunner.disable(); + RecipeRunner.disable(); + RecipeRunner.disable(); + is(registerTimerStub.callCount, 1, "Disable should be idempotent"); + } finally { + RecipeRunner.enabled = originalEnabled; + } + } +); + +decorate_task( + withPrefEnv({ + set: [["app.normandy.onsync_skew_sec", 0]], + }), + withStub(RecipeRunner, "run"), + async function testRunOnSyncRemoteSettings({ runStub }) { + const rsClient = RecipeRunner._remoteSettingsClientForTesting; + await RecipeRunner.init(); + ok( + RecipeRunner._alreadySetUpRemoteSettings, + "remote settings should be set up in the runner" + ); + + // Runner disabled + RecipeRunner.disable(); + await rsClient.emit("sync", {}); + ok(!runStub.called, "run() should not be called if disabled"); + runStub.reset(); + + // Runner enabled + RecipeRunner.enable(); + await rsClient.emit("sync", {}); + ok(runStub.called, "run() should be called if enabled"); + runStub.reset(); + + // Runner disabled + RecipeRunner.disable(); + await rsClient.emit("sync", {}); + ok(!runStub.called, "run() should not be called if disabled"); + runStub.reset(); + + // Runner re-enabled + RecipeRunner.enable(); + await rsClient.emit("sync", {}); + ok(runStub.called, "run() should be called if runner is re-enabled"); + } +); + +decorate_task( + withPrefEnv({ + set: [ + ["app.normandy.onsync_skew_sec", 600], // 10 minutes, much longer than the test will take to run + ], + }), + withStub(RecipeRunner, "run"), + async function testOnSyncRunDelayed({ runStub }) { + ok( + !RecipeRunner._syncSkewTimeout, + "precondition: No timer should be active" + ); + const rsClient = RecipeRunner._remoteSettingsClientForTesting; + await rsClient.emit("sync", {}); + ok(runStub.notCalled, "run() should be not called yet"); + ok(RecipeRunner._syncSkewTimeout, "A timer should be set"); + clearInterval(RecipeRunner._syncSkewTimeout); // cleanup + } +); + +decorate_task( + withStub(RecipeRunner._remoteSettingsClientForTesting, "get"), + async function testRunCanRunOnlyOnce({ getStub }) { + getStub.returns( + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + new Promise(resolve => setTimeout(() => resolve([]), 10)) + ); + + // Run 2 in parallel. + await Promise.all([RecipeRunner.run(), RecipeRunner.run()]); + + is(getStub.callCount, 1, "run() is no-op if already running"); + } +); + +decorate_task( + withPrefEnv({ + set: [ + // Enable update timer logs. + ["app.update.log", true], + ["app.normandy.api_url", "https://example.com"], + ["app.normandy.first_run", false], + ["app.normandy.onsync_skew_sec", 0], + ], + }), + withSpy(RecipeRunner, "run"), + withStub(ActionsManager.prototype, "finalize"), + withStub(Uptake, "reportRunner"), + async function testSyncDelaysTimer({ runSpy }) { + // Mark any existing timer as having run just now. + for (const { value } of Services.catMan.enumerateCategory("update-timer")) { + const timerID = value.split(",")[2]; + console.log(`Mark timer ${timerID} as ran recently`); + // See https://searchfox.org/mozilla-central/rev/11cfa0462/toolkit/components/timermanager/UpdateTimerManager.jsm#8 + const timerLastUpdatePref = `app.update.lastUpdateTime.${timerID}`; + const lastUpdateTime = Math.round(Date.now() / 1000); + Services.prefs.setIntPref(timerLastUpdatePref, lastUpdateTime); + } + + // Give our timer a short duration so that it executes quickly. + // This needs to be more than 1 second as we will call UpdateTimerManager's + // notify method twice in a row and verify that our timer is only called + // once, but because the timestamps are rounded to seconds, just a few + // additional ms could result in a higher value that would cause the timer + // to be called again almost immediately if our timer duration was only 1s. + const kTimerDuration = 2; + Services.prefs.setIntPref( + "app.normandy.run_interval_seconds", + kTimerDuration + ); + // This will refresh the timer interval. + RecipeRunner.unregisterTimer(); + // Ensure our timer is ready to run now. + Services.prefs.setIntPref( + "app.update.lastUpdateTime.recipe-client-addon-run", + Math.round(Date.now() / 1000) - kTimerDuration + ); + RecipeRunner.registerTimer(); + + is(runSpy.callCount, 0, "run() shouldn't have run yet"); + + // Simulate timer notification. + runSpy.resetHistory(); + const service = Cc["@mozilla.org/updates/timer-manager;1"].getService( + Ci.nsITimerCallback + ); + const newTimer = () => { + const t = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); + t.initWithCallback(() => {}, 10, Ci.nsITimer.TYPE_ONE_SHOT); + return t; + }; + + // Run timer once, to make sure this test works as expected. + const startTime = Date.now(); + const endPromise = TestUtils.topicObserved("recipe-runner:end"); + service.notify(newTimer()); + await endPromise; // will timeout if run() not called. + const timerLatency = Math.max(Date.now() - startTime, 1); + is(runSpy.callCount, 1, "run() should be called from timer"); + + // Run once from sync event. + runSpy.resetHistory(); + const rsClient = RecipeRunner._remoteSettingsClientForTesting; + await rsClient.emit("sync", {}); // waits for listeners to run. + is(runSpy.callCount, 1, "run() should be called from sync"); + + // Trigger timer again. This should not run recipes again, since a sync just happened + runSpy.resetHistory(); + is(runSpy.callCount, 0, "run() does not run again from timer"); + service.notify(newTimer()); + // Wait at least as long as the latency we had above. Ten times as a margin. + is(runSpy.callCount, 0, "run() does not run again from timer"); + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + await new Promise(resolve => setTimeout(resolve, timerLatency * 10)); + is(runSpy.callCount, 0, "run() does not run again from timer"); + RecipeRunner.disable(); + } +); + +// Test that the capabilities for context variables are generated correctly. +decorate_task(async function testAutomaticCapabilities() { + const capabilities = await RecipeRunner.getCapabilities(); + + ok( + capabilities.has("jexl.context.env.country"), + "context variables from Normandy's client context should be included" + ); + ok( + capabilities.has("jexl.context.env.version"), + "context variables from the superclass context should be included" + ); + ok( + !capabilities.has("jexl.context.env.getClientClassification"), + "non-getter functions should not be included" + ); + ok( + !capabilities.has("jexl.context.env.prototype"), + "built-in, non-enumerable properties should not be included" + ); +}); + +// Test that recipe runner won't run if Normandy hasn't been initialized. +decorate_task( + withStub(Uptake, "reportRunner"), + withStub(ActionsManager.prototype, "finalize"), + NormandyTestUtils.withMockRecipeCollection([]), + async function testRunEvents({ reportRunnerStub, finalizeStub }) { + const observer = sinon.spy(); + Services.obs.addObserver(observer, "recipe-runner:start"); + + const originalPrefsApplied = Normandy.defaultPrefsHaveBeenApplied; + Normandy.defaultPrefsHaveBeenApplied = Promise.withResolvers(); + + const recipeRunnerPromise = RecipeRunner.run(); + await Promise.resolve(); + ok( + !observer.called, + "RecipeRunner.run shouldn't run if Normandy isn't initialized" + ); + + Normandy.defaultPrefsHaveBeenApplied.resolve(); + await recipeRunnerPromise; + ok( + observer.called, + "RecipeRunner.run should run after Normandy has initialized" + ); + + // cleanup + Services.obs.removeObserver(observer, "recipe-runner:start"); + Normandy.defaultPrefsHaveBeenApplied = originalPrefsApplied; + } +); + +// If no recipes are found on the server, the action manager should be informed of that +decorate_task( + withSpy(ActionsManager.prototype, "finalize"), + NormandyTestUtils.withMockRecipeCollection([]), + async function testNoRecipes({ finalizeSpy }) { + await RecipeRunner.run(); + Assert.deepEqual( + finalizeSpy.args, + [[{ noRecipes: true }]], + "Action manager should know there were no recipes received" + ); + } +); + +// If some recipes are found on the server, the action manager should be informed of that +decorate_task( + withSpy(ActionsManager.prototype, "finalize"), + NormandyTestUtils.withMockRecipeCollection([{ id: 1 }]), + async function testSomeRecipes({ finalizeSpy }) { + await RecipeRunner.run(); + Assert.deepEqual( + finalizeSpy.args, + [[{ noRecipes: false }]], + "Action manager should know there were recipes received" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_ShieldPreferences.js b/toolkit/components/normandy/test/browser/browser_ShieldPreferences.js new file mode 100644 index 0000000000..e455a5f25b --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_ShieldPreferences.js @@ -0,0 +1,91 @@ +"use strict"; + +const { PreferenceExperiments } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceExperiments.sys.mjs" +); +const { ShieldPreferences } = ChromeUtils.importESModule( + "resource://normandy/lib/ShieldPreferences.sys.mjs" +); + +const OPT_OUT_STUDIES_ENABLED_PREF = "app.shield.optoutstudies.enabled"; + +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); +const { addonStudyFactory, preferenceStudyFactory } = + NormandyTestUtils.factories; + +ShieldPreferences.init(); + +decorate_task( + withMockPreferences(), + AddonStudies.withStudies([ + addonStudyFactory({ active: true }), + addonStudyFactory({ active: true }), + ]), + async function testDisableStudiesWhenOptOutDisabled({ + mockPreferences, + addonStudies: [study1, study2], + }) { + mockPreferences.set(OPT_OUT_STUDIES_ENABLED_PREF, true); + const observers = [ + studyEndObserved(study1.recipeId), + studyEndObserved(study2.recipeId), + ]; + Services.prefs.setBoolPref(OPT_OUT_STUDIES_ENABLED_PREF, false); + await Promise.all(observers); + + const newStudy1 = await AddonStudies.get(study1.recipeId); + const newStudy2 = await AddonStudies.get(study2.recipeId); + ok( + !newStudy1.active && !newStudy2.active, + "Setting the opt-out pref to false stops all active opt-out studies." + ); + } +); + +decorate_task( + withMockPreferences(), + PreferenceExperiments.withMockExperiments([ + preferenceStudyFactory({ active: true }), + preferenceStudyFactory({ active: true }), + ]), + withStub(PreferenceExperiments, "stop"), + async function testDisableExperimentsWhenOptOutDisabled({ + mockPreferences, + prefExperiments: [study1, study2], + stopStub, + }) { + mockPreferences.set(OPT_OUT_STUDIES_ENABLED_PREF, true); + let stopArgs = []; + let stoppedBoth = new Promise(resolve => { + let calls = 0; + stopStub.callsFake(function () { + stopArgs.push(Array.from(arguments)); + calls++; + if (calls == 2) { + resolve(); + } + }); + }); + Services.prefs.setBoolPref(OPT_OUT_STUDIES_ENABLED_PREF, false); + await stoppedBoth; + + Assert.deepEqual(stopArgs, [ + [ + study1.slug, + { + reason: "general-opt-out", + caller: "observePrefChange::general-opt-out", + }, + ], + [ + study2.slug, + { + reason: "general-opt-out", + caller: "observePrefChange::general-opt-out", + }, + ], + ]); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_Storage.js b/toolkit/components/normandy/test/browser/browser_Storage.js new file mode 100644 index 0000000000..74272c52d9 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_Storage.js @@ -0,0 +1,43 @@ +"use strict"; + +add_task(async function () { + const store1 = new Storage("prefix1"); + const store2 = new Storage("prefix2"); + + // Make sure values return null before being set + Assert.equal(await store1.getItem("key"), null); + Assert.equal(await store2.getItem("key"), null); + + // Set values to check + await store1.setItem("key", "value1"); + await store2.setItem("key", "value2"); + + // Check that they are available + Assert.equal(await store1.getItem("key"), "value1"); + Assert.equal(await store2.getItem("key"), "value2"); + + // Remove them, and check they are gone + await store1.removeItem("key"); + await store2.removeItem("key"); + Assert.equal(await store1.getItem("key"), null); + Assert.equal(await store2.getItem("key"), null); + + // Check that numbers are stored as numbers (not strings) + await store1.setItem("number", 42); + Assert.equal(await store1.getItem("number"), 42); + + // Check complex types work + const complex = { a: 1, b: [2, 3], c: { d: 4 } }; + await store1.setItem("complex", complex); + Assert.deepEqual(await store1.getItem("complex"), complex); + + // Check that clearing the storage removes data from multiple + // prefixes. + await store1.setItem("removeTest", 1); + await store2.setItem("removeTest", 2); + Assert.equal(await store1.getItem("removeTest"), 1); + Assert.equal(await store2.getItem("removeTest"), 2); + await Storage.clearAllStorage(); + Assert.equal(await store1.getItem("removeTest"), null); + Assert.equal(await store2.getItem("removeTest"), null); +}); diff --git a/toolkit/components/normandy/test/browser/browser_Uptake.js b/toolkit/components/normandy/test/browser/browser_Uptake.js new file mode 100644 index 0000000000..1fa3db3da1 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_Uptake.js @@ -0,0 +1,15 @@ +"use strict"; + +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); + +const Telemetry = Services.telemetry; + +add_task(async function reportRecipeSubmitsFreshness() { + Telemetry.clearScalars(); + const recipe = { id: 17, revision_id: "12" }; + await Uptake.reportRecipe(recipe, Uptake.RECIPE_SUCCESS); + const scalars = Telemetry.getSnapshotForKeyedScalars("main", true); + Assert.deepEqual(scalars.parent["normandy.recipe_freshness"], { 17: 12 }); +}); diff --git a/toolkit/components/normandy/test/browser/browser_about_preferences.js b/toolkit/components/normandy/test/browser/browser_about_preferences.js new file mode 100644 index 0000000000..7b0c706d13 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_about_preferences.js @@ -0,0 +1,106 @@ +"use strict"; + +const OPT_OUT_PREF = "app.shield.optoutstudies.enabled"; + +function withPrivacyPrefs() { + return function (testFunc) { + return async args => + BrowserTestUtils.withNewTab("about:preferences#privacy", async browser => + testFunc({ ...args, browser }) + ); + }; +} + +decorate_task( + withPrefEnv({ + set: [[OPT_OUT_PREF, true]], + }), + withPrivacyPrefs(), + async function testCheckedOnLoad({ browser }) { + const checkbox = browser.contentDocument.getElementById( + "optOutStudiesEnabled" + ); + ok( + checkbox.checked, + "Opt-out checkbox is checked on load when the pref is true" + ); + } +); + +decorate_task( + withPrefEnv({ + set: [[OPT_OUT_PREF, false]], + }), + withPrivacyPrefs(), + async function testUncheckedOnLoad({ browser }) { + const checkbox = browser.contentDocument.getElementById( + "optOutStudiesEnabled" + ); + ok( + !checkbox.checked, + "Opt-out checkbox is unchecked on load when the pref is false" + ); + } +); + +decorate_task( + withPrefEnv({ + set: [[OPT_OUT_PREF, true]], + }), + withPrivacyPrefs(), + async function testCheckboxes({ browser }) { + const optOutCheckbox = browser.contentDocument.getElementById( + "optOutStudiesEnabled" + ); + + optOutCheckbox.click(); + ok( + !Services.prefs.getBoolPref(OPT_OUT_PREF), + "Unchecking the opt-out checkbox sets the pref to false." + ); + optOutCheckbox.click(); + ok( + Services.prefs.getBoolPref(OPT_OUT_PREF), + "Checking the opt-out checkbox sets the pref to true." + ); + } +); + +decorate_task( + withPrefEnv({ + set: [[OPT_OUT_PREF, true]], + }), + withPrivacyPrefs(), + async function testPrefWatchers({ browser }) { + const optOutCheckbox = browser.contentDocument.getElementById( + "optOutStudiesEnabled" + ); + + Services.prefs.setBoolPref(OPT_OUT_PREF, false); + ok( + !optOutCheckbox.checked, + "Disabling the opt-out pref unchecks the opt-out checkbox." + ); + Services.prefs.setBoolPref(OPT_OUT_PREF, true); + ok( + optOutCheckbox.checked, + "Enabling the opt-out pref checks the opt-out checkbox." + ); + } +); + +decorate_task( + withPrivacyPrefs(), + async function testViewStudiesLink({ browser }) { + browser.contentDocument.getElementById("viewShieldStudies").click(); + await BrowserTestUtils.waitForLocationChange(gBrowser); + + is( + gBrowser.currentURI.spec, + "about:studies", + "Clicking the view studies link opens about:studies in a new tab." + ); + + gBrowser.removeCurrentTab(); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_about_studies.js b/toolkit/components/normandy/test/browser/browser_about_studies.js new file mode 100644 index 0000000000..19aed4deb8 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_about_studies.js @@ -0,0 +1,864 @@ +"use strict"; + +const { PreferenceExperiments } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceExperiments.sys.mjs" +); +const { RecipeRunner } = ChromeUtils.importESModule( + "resource://normandy/lib/RecipeRunner.sys.mjs" +); +const { ExperimentFakes } = ChromeUtils.importESModule( + "resource://testing-common/NimbusTestUtils.sys.mjs" +); +const { ExperimentManager } = ChromeUtils.importESModule( + "resource://nimbus/lib/ExperimentManager.sys.mjs" +); +const { ExperimentAPI } = ChromeUtils.importESModule( + "resource://nimbus/ExperimentAPI.sys.mjs" +); +const { RemoteSettingsExperimentLoader } = ChromeUtils.importESModule( + "resource://nimbus/lib/RemoteSettingsExperimentLoader.sys.mjs" +); + +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); +const { addonStudyFactory, preferenceStudyFactory } = + NormandyTestUtils.factories; + +function withAboutStudies() { + return function (testFunc) { + return async args => + BrowserTestUtils.withNewTab("about:studies", async browser => + testFunc({ ...args, browser }) + ); + }; +} + +// Test that the code renders at all +decorate_task( + withAboutStudies(), + async function testAboutStudiesWorks({ browser }) { + const appFound = await SpecialPowers.spawn( + browser, + [], + () => !!content.document.getElementById("app") + ); + ok(appFound, "App element was found"); + } +); + +// Test that the learn more element is displayed correctly +decorate_task( + withPrefEnv({ + set: [["app.normandy.shieldLearnMoreUrl", "http://test/%OS%/"]], + }), + withAboutStudies(), + async function testLearnMore({ browser }) { + SpecialPowers.spawn(browser, [], async () => { + const doc = content.document; + await ContentTaskUtils.waitForCondition(() => + doc.getElementById("shield-studies-learn-more") + ); + doc.getElementById("shield-studies-learn-more").click(); + }); + await BrowserTestUtils.waitForLocationChange(gBrowser); + + const location = browser.currentURI.spec; + is( + location, + AboutPages.aboutStudies.getShieldLearnMoreHref(), + "Clicking Learn More opens the correct page on SUMO." + ); + ok(!location.includes("%OS%"), "The Learn More URL is formatted."); + } +); + +// Test that jumping to preferences worked as expected +decorate_task( + withAboutStudies(), + async function testUpdatePreferences({ browser }) { + let loadPromise = BrowserTestUtils.firstBrowserLoaded(window); + + // We have to use gBrowser instead of browser in most spots since we're + // dealing with a new tab outside of the about:studies tab. + const tab = await BrowserTestUtils.switchTab(gBrowser, () => { + SpecialPowers.spawn(browser, [], async () => { + const doc = content.document; + await ContentTaskUtils.waitForCondition(() => + doc.getElementById("shield-studies-update-preferences") + ); + content.document + .getElementById("shield-studies-update-preferences") + .click(); + }); + }); + + await loadPromise; + + const location = gBrowser.currentURI.spec; + is( + location, + "about:preferences#privacy", + "Clicking Update Preferences opens the privacy section of the new about:preferences." + ); + + BrowserTestUtils.removeTab(tab); + } +); + +// Test that the study listing shows studies in the proper order and grouping +decorate_task( + AddonStudies.withStudies([ + addonStudyFactory({ + slug: "fake-study-a", + userFacingName: "A Fake Add-on Study", + active: true, + userFacingDescription: "A fake description", + studyStartDate: new Date(2018, 0, 4), + }), + addonStudyFactory({ + slug: "fake-study-b", + userFacingName: "B Fake Add-on Study", + active: false, + userFacingDescription: "B fake description", + studyStartDate: new Date(2018, 0, 2), + }), + addonStudyFactory({ + slug: "fake-study-c", + userFacingName: "C Fake Add-on Study", + active: true, + userFacingDescription: "C fake description", + studyStartDate: new Date(2018, 0, 1), + }), + ]), + PreferenceExperiments.withMockExperiments([ + preferenceStudyFactory({ + slug: "fake-study-d", + userFacingName: null, + userFacingDescription: null, + lastSeen: new Date(2018, 0, 3), + expired: false, + }), + preferenceStudyFactory({ + slug: "fake-study-e", + userFacingName: "E Fake Preference Study", + lastSeen: new Date(2018, 0, 5), + expired: true, + }), + preferenceStudyFactory({ + slug: "fake-study-f", + userFacingName: "F Fake Preference Study", + lastSeen: new Date(2018, 0, 6), + expired: false, + }), + ]), + withAboutStudies(), + async function testStudyListing({ addonStudies, prefExperiments, browser }) { + await SpecialPowers.spawn( + browser, + [{ addonStudies, prefExperiments }], + async ({ addonStudies, prefExperiments }) => { + const doc = content.document; + + function getStudyRow(docElem, slug) { + return docElem.querySelector(`.study[data-study-slug="${slug}"]`); + } + + await ContentTaskUtils.waitForCondition( + () => doc.querySelectorAll(".active-study-list .study").length + ); + const activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + const inactiveNames = Array.from( + doc.querySelectorAll(".inactive-study-list .study") + ).map(row => row.dataset.studySlug); + + Assert.deepEqual( + activeNames, + [ + prefExperiments[2].slug, + addonStudies[0].slug, + prefExperiments[0].slug, + addonStudies[2].slug, + ], + "Active studies are grouped by enabled status, and sorted by date" + ); + Assert.deepEqual( + inactiveNames, + [prefExperiments[1].slug, addonStudies[1].slug], + "Inactive studies are grouped by enabled status, and sorted by date" + ); + + const activeAddonStudy = getStudyRow(doc, addonStudies[0].slug); + ok( + activeAddonStudy + .querySelector(".study-description") + .textContent.includes(addonStudies[0].userFacingDescription), + "Study descriptions are shown in about:studies." + ); + is( + activeAddonStudy.querySelector(".study-status").textContent, + "Active", + "Active studies show an 'Active' indicator." + ); + ok( + activeAddonStudy.querySelector(".remove-button"), + "Active studies show a remove button" + ); + is( + activeAddonStudy + .querySelector(".study-icon") + .textContent.toLowerCase(), + "a", + "Study icons use the first letter of the study name." + ); + + const inactiveAddonStudy = getStudyRow(doc, addonStudies[1].slug); + is( + inactiveAddonStudy.querySelector(".study-status").textContent, + "Complete", + "Inactive studies are marked as complete." + ); + ok( + !inactiveAddonStudy.querySelector(".remove-button"), + "Inactive studies do not show a remove button" + ); + + const activePrefStudy = getStudyRow(doc, prefExperiments[0].slug); + const preferenceName = Object.keys(prefExperiments[0].preferences)[0]; + ok( + activePrefStudy + .querySelector(".study-description") + .textContent.includes(preferenceName), + "Preference studies show the preference they are changing" + ); + is( + activePrefStudy.querySelector(".study-status").textContent, + "Active", + "Active studies show an 'Active' indicator." + ); + ok( + activePrefStudy.querySelector(".remove-button"), + "Active studies show a remove button" + ); + + const inactivePrefStudy = getStudyRow(doc, prefExperiments[1].slug); + is( + inactivePrefStudy.querySelector(".study-status").textContent, + "Complete", + "Inactive studies are marked as complete." + ); + ok( + !inactivePrefStudy.querySelector(".remove-button"), + "Inactive studies do not show a remove button" + ); + + activeAddonStudy.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition(() => + getStudyRow(doc, addonStudies[0].slug).matches(".study.disabled") + ); + ok( + getStudyRow(doc, addonStudies[0].slug).matches(".study.disabled"), + "Clicking the remove button updates the UI to show that the study has been disabled." + ); + + activePrefStudy.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition(() => + getStudyRow(doc, prefExperiments[0].slug).matches(".study.disabled") + ); + ok( + getStudyRow(doc, prefExperiments[0].slug).matches(".study.disabled"), + "Clicking the remove button updates the UI to show that the study has been disabled." + ); + } + ); + + const updatedAddonStudy = await AddonStudies.get(addonStudies[0].recipeId); + ok( + !updatedAddonStudy.active, + "Clicking the remove button marks addon studies as inactive in storage." + ); + + const updatedPrefStudy = await PreferenceExperiments.get( + prefExperiments[0].slug + ); + ok( + updatedPrefStudy.expired, + "Clicking the remove button marks preference studies as expired in storage." + ); + } +); + +// Test that a message is shown when no studies have been run +decorate_task( + AddonStudies.withStudies([]), + withAboutStudies(), + async function testStudyListingNoStudies({ browser }) { + await SpecialPowers.spawn(browser, [], async () => { + const doc = content.document; + await ContentTaskUtils.waitForCondition( + () => doc.querySelectorAll(".study-list-info").length + ); + const studyRows = doc.querySelectorAll(".study-list .study"); + is(studyRows.length, 0, "There should be no studies"); + is( + doc.querySelector(".study-list-info").textContent, + "You have not participated in any studies.", + "A message is shown when no studies exist" + ); + }); + } +); + +// Test that the message shown when studies are disabled and studies exist +decorate_task( + withAboutStudies(), + AddonStudies.withStudies([ + addonStudyFactory({ + userFacingName: "A Fake Add-on Study", + slug: "fake-addon-study", + active: false, + userFacingDescription: "A fake description", + studyStartDate: new Date(2018, 0, 4), + }), + ]), + PreferenceExperiments.withMockExperiments([ + preferenceStudyFactory({ + slug: "fake-pref-study", + userFacingName: "B Fake Preference Study", + lastSeen: new Date(2018, 0, 5), + expired: true, + }), + ]), + async function testStudyListingDisabled({ browser }) { + try { + RecipeRunner.disable(); + + await SpecialPowers.spawn(browser, [], async () => { + const doc = content.document; + await ContentTaskUtils.waitForCondition(() => + doc.querySelector(".info-box-content > span") + ); + + is( + doc.querySelector(".info-box-content > span").textContent, + "This is a list of studies that you have participated in. No new studies will run.", + "A message is shown when studies are disabled" + ); + }); + } finally { + // reset RecipeRunner.enabled + RecipeRunner.checkPrefs(); + } + } +); + +// Test for bug 1498940 - detects studies disabled when only study opt-out is set +decorate_task( + withPrefEnv({ + set: [ + ["datareporting.healthreport.uploadEnabled", true], + ["app.normandy.api_url", "https://example.com"], + ["app.shield.optoutstudies.enabled", false], + ], + }), + withAboutStudies(), + AddonStudies.withStudies([]), + PreferenceExperiments.withMockExperiments([]), + async function testStudyListingStudiesOptOut({ browser }) { + RecipeRunner.checkPrefs(); + ok( + RecipeRunner.enabled, + "RecipeRunner should be enabled as a Precondition" + ); + + await SpecialPowers.spawn(browser, [], async () => { + const doc = content.document; + await ContentTaskUtils.waitForCondition(() => { + const span = doc.querySelector(".info-box-content > span"); + return span && span.textContent; + }); + + is( + doc.querySelector(".info-box-content > span").textContent, + "This is a list of studies that you have participated in. No new studies will run.", + "A message is shown when studies are disabled" + ); + }); + } +); + +// Test that clicking remove on a study that was disabled by an outside source +// since the page loaded correctly updates. +decorate_task( + AddonStudies.withStudies([ + addonStudyFactory({ + slug: "fake-addon-study", + userFacingName: "Fake Add-on Study", + active: true, + userFacingDescription: "A fake description", + studyStartDate: new Date(2018, 0, 4), + }), + ]), + PreferenceExperiments.withMockExperiments([ + preferenceStudyFactory({ + slug: "fake-pref-study", + userFacingName: "Fake Preference Study", + lastSeen: new Date(2018, 0, 3), + expired: false, + }), + ]), + withAboutStudies(), + async function testStudyListing({ + addonStudies: [addonStudy], + prefExperiments: [prefStudy], + browser, + }) { + // The content page has already loaded. Disabling the studies here shouldn't + // affect it, since it doesn't live-update. + await AddonStudies.markAsEnded(addonStudy, "disabled-automatically-test"); + await PreferenceExperiments.stop(prefStudy.slug, { + resetValue: false, + reason: "disabled-automatically-test", + }); + + await SpecialPowers.spawn( + browser, + [{ addonStudy, prefStudy }], + async ({ addonStudy, prefStudy }) => { + const doc = content.document; + + function getStudyRow(docElem, slug) { + return docElem.querySelector(`.study[data-study-slug="${slug}"]`); + } + + await ContentTaskUtils.waitForCondition( + () => doc.querySelectorAll(".remove-button").length == 2 + ); + let activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + let inactiveNames = Array.from( + doc.querySelectorAll(".inactive-study-list .study") + ).map(row => row.dataset.studySlug); + + Assert.deepEqual( + activeNames, + [addonStudy.slug, prefStudy.slug], + "Both studies should be listed as active, even though they have been disabled outside of the page" + ); + Assert.deepEqual( + inactiveNames, + [], + "No studies should be listed as inactive" + ); + + const activeAddonStudy = getStudyRow(doc, addonStudy.slug); + const activePrefStudy = getStudyRow(doc, prefStudy.slug); + + activeAddonStudy.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition(() => + getStudyRow(doc, addonStudy.slug).matches(".study.disabled") + ); + ok( + getStudyRow(doc, addonStudy.slug).matches(".study.disabled"), + "Clicking the remove button updates the UI to show that the study has been disabled." + ); + + activePrefStudy.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition(() => + getStudyRow(doc, prefStudy.slug).matches(".study.disabled") + ); + ok( + getStudyRow(doc, prefStudy.slug).matches(".study.disabled"), + "Clicking the remove button updates the UI to show that the study has been disabled." + ); + + activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + + Assert.deepEqual( + activeNames, + [], + "No studies should be listed as active" + ); + } + ); + } +); + +// Test that clicking remove on a study updates even about:studies pages +// that are not currently in focus. +decorate_task( + AddonStudies.withStudies([ + addonStudyFactory({ + slug: "fake-addon-study", + userFacingName: "Fake Add-on Study", + active: true, + userFacingDescription: "A fake description", + studyStartDate: new Date(2018, 0, 4), + }), + ]), + PreferenceExperiments.withMockExperiments([ + preferenceStudyFactory({ + slug: "fake-pref-study", + userFacingName: "Fake Preference Study", + lastSeen: new Date(2018, 0, 3), + expired: false, + }), + ]), + withAboutStudies(), + async function testOtherTabsUpdated({ + addonStudies: [addonStudy], + prefExperiments: [prefStudy], + browser, + }) { + // Ensure that both our studies are active in the current tab. + await SpecialPowers.spawn( + browser, + [{ addonStudy, prefStudy }], + async ({ addonStudy, prefStudy }) => { + const doc = content.document; + await ContentTaskUtils.waitForCondition( + () => doc.querySelectorAll(".remove-button").length == 2, + "waiting for page to load" + ); + let activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + let inactiveNames = Array.from( + doc.querySelectorAll(".inactive-study-list .study") + ).map(row => row.dataset.studySlug); + + Assert.deepEqual( + activeNames, + [addonStudy.slug, prefStudy.slug], + "Both studies should be listed as active" + ); + Assert.deepEqual( + inactiveNames, + [], + "No studies should be listed as inactive" + ); + } + ); + + // Open a new about:studies tab. + await BrowserTestUtils.withNewTab("about:studies", async browser => { + // Delete both studies in this tab; this should pass if previous tests have passed. + await SpecialPowers.spawn( + browser, + [{ addonStudy, prefStudy }], + async ({ addonStudy, prefStudy }) => { + const doc = content.document; + + function getStudyRow(docElem, slug) { + return docElem.querySelector(`.study[data-study-slug="${slug}"]`); + } + + await ContentTaskUtils.waitForCondition( + () => doc.querySelectorAll(".remove-button").length == 2, + "waiting for page to load" + ); + let activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + let inactiveNames = Array.from( + doc.querySelectorAll(".inactive-study-list .study") + ).map(row => row.dataset.studySlug); + + Assert.deepEqual( + activeNames, + [addonStudy.slug, prefStudy.slug], + "Both studies should be listed as active in the new tab" + ); + Assert.deepEqual( + inactiveNames, + [], + "No studies should be listed as inactive in the new tab" + ); + + const activeAddonStudy = getStudyRow(doc, addonStudy.slug); + const activePrefStudy = getStudyRow(doc, prefStudy.slug); + + activeAddonStudy.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition(() => + getStudyRow(doc, addonStudy.slug).matches(".study.disabled") + ); + ok( + getStudyRow(doc, addonStudy.slug).matches(".study.disabled"), + "Clicking the remove button updates the UI in the new tab" + ); + + activePrefStudy.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition(() => + getStudyRow(doc, prefStudy.slug).matches(".study.disabled") + ); + ok( + getStudyRow(doc, prefStudy.slug).matches(".study.disabled"), + "Clicking the remove button updates the UI in the new tab" + ); + + activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + + Assert.deepEqual( + activeNames, + [], + "No studies should be listed as active" + ); + } + ); + }); + + // Ensure that the original tab has updated correctly. + await SpecialPowers.spawn( + browser, + [{ addonStudy, prefStudy }], + async ({ addonStudy, prefStudy }) => { + const doc = content.document; + await ContentTaskUtils.waitForCondition( + () => doc.querySelectorAll(".inactive-study-list .study").length == 2, + "Two studies should load into the inactive list, since they were disabled in a different tab" + ); + let activeNames = Array.from( + doc.querySelectorAll(".active-study-list .study") + ).map(row => row.dataset.studySlug); + let inactiveNames = Array.from( + doc.querySelectorAll(".inactive-study-list .study") + ).map(row => row.dataset.studySlug); + Assert.deepEqual( + activeNames, + [], + "No studies should be listed as active, since they were disabled in a different tab" + ); + Assert.deepEqual( + inactiveNames, + [addonStudy.slug, prefStudy.slug], + "Both studies should be listed as inactive, since they were disabled in a different tab" + ); + } + ); + } +); + +add_task(async function test_nimbus_about_studies_experiment() { + const recipe = ExperimentFakes.recipe("about-studies-foo"); + await ExperimentManager.enroll(recipe); + const activeBranchSlug = ExperimentAPI.getActiveBranch({ + slug: recipe.slug, + })?.slug; + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:studies", activeBranchSlug }, + async browser => { + const [name, renderedBranchSlug] = await SpecialPowers.spawn( + browser, + [], + async () => { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".nimbus .remove-button"), + "waiting for page to load" + ); + return [ + content.document.querySelector(".study-name").innerText, + content.document.querySelector(".study-branch-slug").innerText, + ]; + } + ); + // Make sure strings are properly shown + Assert.equal( + name, + recipe.userFacingName, + "Correct active experiment name" + ); + Assert.equal( + renderedBranchSlug, + activeBranchSlug, + "Correct active experiment branch slug" + ); + } + ); + ExperimentManager.unenroll(recipe.slug); + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:studies", activeBranchSlug }, + async browser => { + const [name, renderedBranchSlug] = await SpecialPowers.spawn( + browser, + [], + async () => { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".nimbus.disabled"), + "waiting for experiment to become disabled" + ); + return [ + content.document.querySelector(".study-name").innerText, + content.document.querySelector(".study-branch-slug").innerText, + ]; + } + ); + // Make sure strings are properly shown + Assert.equal( + name, + recipe.userFacingName, + "Correct disabled experiment name" + ); + Assert.equal( + renderedBranchSlug, + activeBranchSlug, + "Correct disabled experiment branch slug" + ); + } + ); + // Cleanup for multiple test runs + ExperimentManager.store._deleteForTests(recipe.slug); + Assert.equal(ExperimentManager.store.getAll().length, 0, "Cleanup done"); +}); + +add_task(async function test_nimbus_about_studies_rollout() { + let recipe = ExperimentFakes.recipe("test_nimbus_about_studies_rollout"); + let rollout = { + ...recipe, + branches: [recipe.branches[0]], + isRollout: true, + }; + await ExperimentManager.enroll(rollout); + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:studies" }, + async browser => { + const studyCount = await SpecialPowers.spawn(browser, [], async () => { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector("#shield-studies-learn-more"), + "waiting for page/experiment to load" + ); + return content.document.querySelectorAll(".study-name").length; + }); + // Make sure strings are properly shown + Assert.equal(studyCount, 1, "Rollout loaded in non-debug mode"); + } + ); + Services.prefs.setBoolPref("nimbus.debug", true); + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:studies" }, + async browser => { + const [studyName, branchShown] = await SpecialPowers.spawn( + browser, + [], + async () => { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".nimbus .remove-button"), + "waiting for page/experiment to load" + ); + return [ + content.document.querySelector(".study-header").innerText, + !!content.document.querySelector(".study-branch-slug"), + ]; + } + ); + // Make sure strings are properly shown + Assert.ok(studyName.includes("Active"), "Rollout loaded in debug mode"); + // Make sure the branch slug is not shown for rollouts + Assert.ok(!branchShown, "Branch slug not shown for rollouts"); + } + ); + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:studies" }, + async browser => { + const name = await SpecialPowers.spawn(browser, [], async () => { + content.document.querySelector(".remove-button").click(); + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".nimbus.disabled"), + "waiting for experiment to become disabled" + ); + return content.document.querySelector(".study-header").innerText; + }); + // Make sure strings are properly shown + Assert.ok(name.includes("Complete"), "Rollout was removed"); + } + ); + // Cleanup for multiple test runs + ExperimentManager.store._deleteForTests(rollout.slug); + Services.prefs.clearUserPref("nimbus.debug"); +}); + +add_task(async function test_getStudiesEnabled() { + RecipeRunner.initializedPromise = Promise.withResolvers(); + let promise = AboutPages.aboutStudies.getStudiesEnabled(); + + RecipeRunner.initializedPromise.resolve(); + let result = await promise; + + Assert.equal( + result, + Services.prefs.getBoolPref("app.shield.optoutstudies.enabled"), + "about:studies is enabled if the pref is enabled" + ); +}); + +add_task(async function test_forceEnroll() { + let sandbox = sinon.createSandbox(); + + // This simulates a succesful enrollment + let stub = sandbox.stub(RemoteSettingsExperimentLoader, "optInToExperiment"); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: "about:studies?optin_collection=collection123&optin_branch=branch123&optin_slug=slug123", + }, + async browser => { + await SpecialPowers.spawn(browser, [], async () => { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".opt-in-box"), + "Should show the opt in message" + ); + + Assert.equal( + content.document + .querySelector(".opt-in-box") + .classList.contains("opt-in-error"), + false, + "should not have an error class since the enrollment was successful" + ); + + return true; + }); + } + ); + + // Simulates a problem force enrolling + stub.rejects(new Error("Testing error")); + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: "about:studies?optin_collection=collection123&optin_branch=branch123&optin_slug=slug123", + }, + async browser => { + await SpecialPowers.spawn(browser, [], async () => { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".opt-in-box"), + "Should show the opt in message" + ); + + Assert.ok( + content.document + .querySelector(".opt-in-box") + .classList.contains("opt-in-error"), + "should have an error class since the enrollment rejected" + ); + + Assert.equal( + content.document.querySelector(".opt-in-box").textContent, + "Testing error", + "should render the error" + ); + + return true; + }); + } + ); + + sandbox.restore(); +}); diff --git a/toolkit/components/normandy/test/browser/browser_actions_AddonRollbackAction.js b/toolkit/components/normandy/test/browser/browser_actions_AddonRollbackAction.js new file mode 100644 index 0000000000..56d60a7004 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_AddonRollbackAction.js @@ -0,0 +1,236 @@ +"use strict"; + +const { AddonRollbackAction } = ChromeUtils.importESModule( + "resource://normandy/actions/AddonRollbackAction.sys.mjs" +); +const { AddonRolloutAction } = ChromeUtils.importESModule( + "resource://normandy/actions/AddonRolloutAction.sys.mjs" +); +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { AddonRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonRollouts.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +// Test that a simple recipe unenrolls as expected +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + async function simple_recipe_unenrollment({ + mockNormandyApi, + setExperimentInactiveStub, + sendEventSpy, + }) { + const rolloutRecipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [rolloutRecipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: rolloutRecipe.arguments.extensionApiId, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + const rolloutAction = new AddonRolloutAction(); + await rolloutAction.processRecipe( + rolloutRecipe, + BaseAction.suitability.FILTER_MATCH + ); + is(rolloutAction.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + const rollbackRecipe = { + id: 2, + arguments: { + rolloutSlug: "test-rollout", + }, + }; + + const rollbackAction = new AddonRollbackAction(); + ok( + await AddonRollouts.has(rolloutRecipe.arguments.slug), + "Rollout should have been added" + ); + await rollbackAction.processRecipe( + rollbackRecipe, + BaseAction.suitability.FILTER_MATCH + ); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + ok(!addon, "add-on is uninstalled"); + + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: rolloutRecipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ROLLED_BACK, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollback should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "addon_rollout", rollbackRecipe.arguments.rolloutSlug], + ["unenroll", "addon_rollback", rollbackRecipe.arguments.rolloutSlug], + ]); + + Assert.deepEqual( + setExperimentInactiveStub.args, + [["test-rollout"]], + "the telemetry experiment should deactivated" + ); + } +); + +// Add-on already uninstalled +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function addon_already_uninstalled({ mockNormandyApi, sendEventSpy }) { + const rolloutRecipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [rolloutRecipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: rolloutRecipe.arguments.extensionApiId, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + const rolloutAction = new AddonRolloutAction(); + await rolloutAction.processRecipe( + rolloutRecipe, + BaseAction.suitability.FILTER_MATCH + ); + is(rolloutAction.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + const rollbackRecipe = { + id: 2, + arguments: { + rolloutSlug: "test-rollout", + }, + }; + + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + await addon.uninstall(); + + const rollbackAction = new AddonRollbackAction(); + await rollbackAction.processRecipe( + rollbackRecipe, + BaseAction.suitability.FILTER_MATCH + ); + + addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + ok(!addon, "add-on is uninstalled"); + + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: rolloutRecipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ROLLED_BACK, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollback should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "addon_rollout", rollbackRecipe.arguments.rolloutSlug], + ["unenroll", "addon_rollback", rollbackRecipe.arguments.rolloutSlug], + ]); + } +); + +// Already rolled back, do nothing +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function already_rolled_back({ sendEventSpy }) { + const rollout = { + recipeId: 1, + slug: "test-rollout", + state: AddonRollouts.STATE_ROLLED_BACK, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }; + AddonRollouts.add(rollout); + + const action = new AddonRollbackAction(); + await action.processRecipe( + { + id: 2, + arguments: { + rolloutSlug: "test-rollout", + }, + }, + BaseAction.suitability.FILTER_MATCH + ); + + Assert.deepEqual( + await AddonRollouts.getAll(), + [ + { + recipeId: 1, + slug: "test-rollout", + state: AddonRollouts.STATE_ROLLED_BACK, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollback should be stored in db" + ); + + sendEventSpy.assertEvents([]); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_AddonRolloutAction.js b/toolkit/components/normandy/test/browser/browser_actions_AddonRolloutAction.js new file mode 100644 index 0000000000..cbac8524f3 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_AddonRolloutAction.js @@ -0,0 +1,512 @@ +"use strict"; + +const { AddonRolloutAction } = ChromeUtils.importESModule( + "resource://normandy/actions/AddonRolloutAction.sys.mjs" +); +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { AddonRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonRollouts.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +// Test that a simple recipe enrolls as expected +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withStub(TelemetryEnvironment, "setExperimentActive"), + withSendEventSpy(), + async function simple_recipe_enrollment({ + mockNormandyApi, + setExperimentActiveStub, + sendEventSpy, + }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + const action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + // addon was installed + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should be installed"); + + // rollout was stored + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: recipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "addon_rollout", recipe.arguments.slug], + ]); + ok( + setExperimentActiveStub.calledWithExactly("test-rollout", "active", { + type: "normandy-addonrollout", + }), + "a telemetry experiment should be activated" + ); + + // cleanup installed addon + await addon.uninstall(); + } +); + +// Test that a rollout can update the addon +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function update_rollout({ mockNormandyApi, sendEventSpy }) { + // first enrollment + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + }), + 2: extensionDetailsFactory({ + id: 2, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url, + version: "2.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash, + }), + }; + + let webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + let action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + // addon was installed + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // update existing enrollment + recipe.arguments.extensionApiId = 2; + webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should still be installed"); + is(addon.version, "2.0", "addon should be the correct version"); + + // rollout in the DB has been updated + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: recipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensionApiId: 2, + addonId: FIXTURE_ADDON_ID, + addonVersion: "2.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "addon_rollout", "test-rollout"], + ["update", "addon_rollout", "test-rollout"], + ]); + + // Cleanup + await addon.uninstall(); + } +); + +// Re-running a recipe does nothing +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function rerun_recipe({ mockNormandyApi, sendEventSpy }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + let action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + // addon was installed + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // re-run the same recipe + action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should still be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // rollout in the DB has not been updated + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: recipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([["enroll", "addon_rollout", "test-rollout"]]); + + // Cleanup + await addon.uninstall(); + } +); + +// Conflicting rollouts +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function conflicting_rollout({ mockNormandyApi, sendEventSpy }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + let action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + // addon was installed + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // update existing enrollment + action = new AddonRolloutAction(); + await action.processRecipe( + { + ...recipe, + id: 2, + arguments: { + ...recipe.arguments, + slug: "test-conflict", + }, + }, + BaseAction.suitability.FILTER_MATCH + ); + is(action.lastError, null, "lastError should be null"); + + addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should still be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // rollout in the DB has not been updated + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: recipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + [ + "enroll", + "addon_rollout", + "test-rollout", + { addonId: FIXTURE_ADDON_ID }, + ], + [ + "enrollFailed", + "addon_rollout", + "test-conflict", + { reason: "conflict" }, + ], + ]); + + // Cleanup + await addon.uninstall(); + } +); + +// Add-on ID changed +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function enroll_failed_addon_id_changed({ + mockNormandyApi, + sendEventSpy, + }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + }), + 2: extensionDetailsFactory({ + id: 2, + extension_id: "normandydriver-b@example.com", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].url, + version: "1.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].hash, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + let action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + // addon was installed + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // update existing enrollment + recipe.arguments.extensionApiId = 2; + action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should still be installed"); + is(addon.version, "1.0", "addon should be the correct version"); + + // rollout in the DB has not been updated + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: recipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "1.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "addon_rollout", "test-rollout"], + [ + "updateFailed", + "addon_rollout", + "test-rollout", + { reason: "addon-id-changed" }, + ], + ]); + + // Cleanup + await addon.uninstall(); + } +); + +// Add-on upgrade required +decorate_task( + AddonRollouts.withTestMock(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + async function enroll_failed_upgrade_required({ + mockNormandyApi, + sendEventSpy, + }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + extensionApiId: 1, + }, + }; + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url, + version: "2.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash, + }), + 2: extensionDetailsFactory({ + id: 2, + }), + }; + + const webExtStartupPromise = + AddonTestUtils.promiseWebExtensionStartup(FIXTURE_ADDON_ID); + + let action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + await webExtStartupPromise; + + // addon was installed + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should be installed"); + is(addon.version, "2.0", "addon should be the correct version"); + + // update existing enrollment + recipe.arguments.extensionApiId = 2; + action = new AddonRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.id, FIXTURE_ADDON_ID, "addon should still be installed"); + is(addon.version, "2.0", "addon should be the correct version"); + + // rollout in the DB has not been updated + const rollouts = await AddonRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + recipeId: recipe.id, + slug: "test-rollout", + state: AddonRollouts.STATE_ACTIVE, + extensionApiId: 1, + addonId: FIXTURE_ADDON_ID, + addonVersion: "2.0", + xpiUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url, + xpiHash: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash, + xpiHashAlgorithm: "sha256", + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "addon_rollout", "test-rollout"], + [ + "updateFailed", + "addon_rollout", + "test-rollout", + { reason: "upgrade-required" }, + ], + ]); + + // Cleanup + await addon.uninstall(); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_BranchedAddonStudyAction.js b/toolkit/components/normandy/test/browser/browser_actions_BranchedAddonStudyAction.js new file mode 100644 index 0000000000..cce82ae89e --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_BranchedAddonStudyAction.js @@ -0,0 +1,1646 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { BranchedAddonStudyAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BranchedAddonStudyAction.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); + +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); +const { branchedAddonStudyFactory } = NormandyTestUtils.factories; + +function branchedAddonStudyRecipeFactory(overrides = {}) { + let args = { + slug: "fake-slug", + userFacingName: "Fake name", + userFacingDescription: "fake description", + isEnrollmentPaused: false, + branches: [ + { + slug: "a", + ratio: 1, + extensionApiId: 1, + }, + ], + }; + if (Object.hasOwnProperty.call(overrides, "arguments")) { + args = Object.assign(args, overrides.arguments); + delete overrides.arguments; + } + return recipeFactory( + Object.assign( + { + action: "branched-addon-study", + arguments: args, + }, + overrides + ) + ); +} + +function recipeFromStudy(study, overrides = {}) { + let args = { + slug: study.slug, + userFacingName: study.userFacingName, + isEnrollmentPaused: false, + branches: [ + { + slug: "a", + ratio: 1, + extensionApiId: study.extensionApiId, + }, + ], + }; + + if (Object.hasOwnProperty.call(overrides, "arguments")) { + args = Object.assign(args, overrides.arguments); + delete overrides.arguments; + } + + return branchedAddonStudyRecipeFactory( + Object.assign( + { + id: study.recipeId, + arguments: args, + }, + overrides + ) + ); +} + +// Test that enroll is not called if recipe is already enrolled and update does nothing +// if recipe is unchanged +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([branchedAddonStudyFactory()]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "1.0" }), + async function enrollTwiceFail({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + hash: study.extensionHash, + }), + }; + const action = new BranchedAddonStudyAction(); + const enrollSpy = sinon.spy(action, "enroll"); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(!enrollSpy.called, "enroll should not be called"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([]); + } +); + +// Test that if the add-on fails to install, the database is cleaned up and the +// error is correctly reported. +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + AddonStudies.withStudies(), + async function enrollDownloadFail({ mockNormandyApi, sendEventSpy }) { + const recipe = branchedAddonStudyRecipeFactory({ + arguments: { + branches: [{ slug: "missing", ratio: 1, extensionApiId: 404 }], + }, + }); + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.branches[0].extensionApiId, + xpi: "https://example.com/404.xpi", + }), + }; + const action = new BranchedAddonStudyAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + const studies = await AddonStudies.getAll(); + Assert.deepEqual(studies, [], "the study should not be in the database"); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "addon_study", + recipe.arguments.name, + { + reason: "download-failure", + detail: "ERROR_NETWORK_FAILURE", + branch: "missing", + }, + ], + ]); + } +); + +// Ensure that the database is clean and error correctly reported if hash check fails +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + AddonStudies.withStudies(), + async function enrollHashCheckFails({ mockNormandyApi, sendEventSpy }) { + const recipe = branchedAddonStudyRecipeFactory(); + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.branches[0].extensionApiId, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + hash: "badhash", + }), + }; + const action = new BranchedAddonStudyAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + const studies = await AddonStudies.getAll(); + Assert.deepEqual(studies, [], "the study should not be in the database"); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "addon_study", + recipe.arguments.name, + { + reason: "download-failure", + detail: "ERROR_INCORRECT_HASH", + branch: "a", + }, + ], + ]); + } +); + +// Ensure that the database is clean and error correctly reported if there is a metadata mismatch +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + AddonStudies.withStudies(), + async function enrollFailsMetadataMismatch({ + mockNormandyApi, + sendEventSpy, + }) { + const recipe = branchedAddonStudyRecipeFactory(); + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.branches[0].extensionApiId, + version: "1.5", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + }), + }; + const action = new BranchedAddonStudyAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + const studies = await AddonStudies.getAll(); + Assert.deepEqual(studies, [], "the study should not be in the database"); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "addon_study", + recipe.arguments.name, + { + reason: "metadata-mismatch", + branch: "a", + }, + ], + ]); + } +); + +// Test that in the case of a study add-on conflicting with a non-study add-on, the study does not enroll +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + withInstalledWebExtensionSafe({ version: "0.1", id: FIXTURE_ADDON_ID }), + AddonStudies.withStudies(), + async function conflictingEnrollment({ + mockNormandyApi, + sendEventSpy, + installedWebExtensionSafe: { addonId }, + }) { + is( + addonId, + FIXTURE_ADDON_ID, + "Generated, installed add-on should have the same ID as the fixture" + ); + const recipe = branchedAddonStudyRecipeFactory({ + arguments: { slug: "conflicting" }, + }); + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + addonUrl: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + }), + }; + const action = new BranchedAddonStudyAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + is(addon.version, "0.1", "The installed add-on should not be replaced"); + + Assert.deepEqual( + await AddonStudies.getAll(), + [], + "There should be no enrolled studies" + ); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "addon_study", + recipe.arguments.slug, + { reason: "conflicting-addon-id" }, + ], + ]); + } +); + +// Test a successful update +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: FIXTURE_ADDON_ID, + extensionHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + extensionHashAlgorithm: "sha256", + addonVersion: "1.0", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "1.0" }), + async function successfulUpdate({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const addonUrl = FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url; + const recipe = recipeFromStudy(study, { + arguments: { + branches: [ + { slug: "a", extensionApiId: study.extensionApiId, ratio: 1 }, + ], + }, + }); + const hash = FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash; + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.branches[0].extensionApiId, + extension_id: FIXTURE_ADDON_ID, + xpi: addonUrl, + hash, + version: "2.0", + }), + }; + const action = new BranchedAddonStudyAction(); + const enrollSpy = sinon.spy(action, "enroll"); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(!enrollSpy.called, "enroll should not be called"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "update", + "addon_study", + recipe.arguments.name, + { + addonId: FIXTURE_ADDON_ID, + addonVersion: "2.0", + branch: "a", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual( + updatedStudy, + { + ...study, + addonVersion: "2.0", + addonUrl, + extensionApiId: recipe.arguments.branches[0].extensionApiId, + extensionHash: hash, + }, + "study data should be updated" + ); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "2.0", "add-on should be updated"); + } +); + +// Test update fails when addon ID does not match +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: "test@example.com", + extensionHash: "01d", + extensionHashAlgorithm: "sha256", + addonVersion: "0.1", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "0.1" }), + async function updateFailsAddonIdMismatch({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: FIXTURE_ADDON_ID, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + }), + }; + const action = new BranchedAddonStudyAction(); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "updateFailed", + "addon_study", + recipe.arguments.name, + { + reason: "addon-id-mismatch", + branch: "a", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual(updatedStudy, study, "study data should be unchanged"); + + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "0.1", "add-on should be unchanged"); + } +); + +// Test update fails when original addon does not exist +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + extensionHash: "01d", + extensionHashAlgorithm: "sha256", + addonVersion: "0.1", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: "test@example.com", version: "0.1" }), + async function updateFailsAddonDoesNotExist({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + }), + }; + const action = new BranchedAddonStudyAction(); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "updateFailed", + "addon_study", + recipe.arguments.name, + { + reason: "addon-does-not-exist", + branch: "a", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual(updatedStudy, study, "study data should be unchanged"); + + let addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + ok(!addon, "new add-on should not be installed"); + + addon = await AddonManager.getAddonByID("test@example.com"); + ok(addon, "old add-on should still be installed"); + } +); + +// Test update fails when download fails +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: FIXTURE_ADDON_ID, + extensionHash: "01d", + extensionHashAlgorithm: "sha256", + addonVersion: "0.1", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "0.1" }), + async function updateDownloadFailure({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + xpi: "https://example.com/404.xpi", + }), + }; + const action = new BranchedAddonStudyAction(); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "updateFailed", + "addon_study", + recipe.arguments.name, + { + branch: "a", + reason: "download-failure", + detail: "ERROR_NETWORK_FAILURE", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual(updatedStudy, study, "study data should be unchanged"); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "0.1", "add-on should be unchanged"); + } +); + +// Test update fails when hash check fails +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: FIXTURE_ADDON_ID, + extensionHash: "01d", + extensionHashAlgorithm: "sha256", + addonVersion: "0.1", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "0.1" }), + async function updateFailsHashCheckFail({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + hash: "badhash", + }), + }; + const action = new BranchedAddonStudyAction(); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "updateFailed", + "addon_study", + recipe.arguments.name, + { + branch: "a", + reason: "download-failure", + detail: "ERROR_INCORRECT_HASH", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual(updatedStudy, study, "study data should be unchanged"); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "0.1", "add-on should be unchanged"); + } +); + +// Test update fails on downgrade when study version is greater than extension version +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: FIXTURE_ADDON_ID, + extensionHash: "01d", + extensionHashAlgorithm: "sha256", + addonVersion: "2.0", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "2.0" }), + async function upgradeFailsNoDowngrades({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + version: "1.0", + }), + }; + const action = new BranchedAddonStudyAction(); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "updateFailed", + "addon_study", + recipe.arguments.name, + { + reason: "no-downgrade", + branch: "a", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual(updatedStudy, study, "study data should be unchanged"); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "2.0", "add-on should be unchanged"); + } +); + +// Test update fails when there is a version mismatch with metadata +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: FIXTURE_ADDON_ID, + extensionHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + extensionHashAlgorithm: "sha256", + addonVersion: "1.0", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionFromURL( + FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url + ), + async function upgradeFailsMetadataMismatchVersion({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const recipe = recipeFromStudy(study); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url, + version: "3.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash, + }), + }; + const action = new BranchedAddonStudyAction(); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "updateFailed", + "addon_study", + recipe.arguments.name, + { + branch: "a", + reason: "metadata-mismatch", + }, + ], + ]); + + const updatedStudy = await AddonStudies.get(recipe.id); + Assert.deepEqual(updatedStudy, study, "study data should be unchanged"); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "1.0", "add-on should be unchanged"); + + let addonSourceURI = addon.getResourceURI(); + if (addonSourceURI instanceof Ci.nsIJARURI) { + addonSourceURI = addonSourceURI.JARFile; + } + const xpiFile = addonSourceURI.QueryInterface(Ci.nsIFileURL).file; + const installedHash = CryptoUtils.getFileHash( + xpiFile, + study.extensionHashAlgorithm + ); + Assert.strictEqual( + installedHash, + study.extensionHash, + "add-on should be unchanged" + ); + } +); + +// Test that unenrolling fails if the study doesn't exist +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + AddonStudies.withStudies(), + async function unenrollNonexistent() { + const action = new BranchedAddonStudyAction(); + await Assert.rejects( + action.unenroll(42), + /no study found/i, + "unenroll should fail when no study exists" + ); + } +); + +// Test that unenrolling an inactive study fails +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + AddonStudies.withStudies([branchedAddonStudyFactory({ active: false })]), + withSendEventSpy(), + async ({ addonStudies: [study], sendEventSpy }) => { + const action = new BranchedAddonStudyAction(); + await Assert.rejects( + action.unenroll(study.recipeId), + /cannot stop study.*already inactive/i, + "unenroll should fail when the requested study is inactive" + ); + } +); + +// test a successful unenrollment +const testStopId = "testStop@example.com"; +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + active: true, + addonId: testStopId, + studyEndDate: null, + }), + ]), + withInstalledWebExtension({ id: testStopId }, { expectUninstall: true }), + withSendEventSpy(), + withStub(TelemetryEnvironment, "setExperimentInactive"), + async function unenrollTest({ + addonStudies: [study], + installedWebExtension: { addonId }, + sendEventSpy, + setExperimentInactiveStub, + }) { + let addon = await AddonManager.getAddonByID(addonId); + ok(addon, "the add-on should be installed before unenrolling"); + + const action = new BranchedAddonStudyAction(); + await action.unenroll(study.recipeId, "test-reason"); + + const newStudy = AddonStudies.get(study.recipeId); + is(!newStudy, false, "stop should mark the study as inactive"); + Assert.notStrictEqual( + newStudy.studyEndDate, + null, + "the study should have an end date" + ); + + addon = await AddonManager.getAddonByID(addonId); + is(addon, null, "the add-on should be uninstalled after unenrolling"); + + sendEventSpy.assertEvents([ + [ + "unenroll", + "addon_study", + study.name, + { + addonId, + addonVersion: study.addonVersion, + reason: "test-reason", + }, + ], + ]); + + Assert.deepEqual( + setExperimentInactiveStub.args, + [[study.slug]], + "setExperimentInactive should be called" + ); + } +); + +// If the add-on for a study isn't installed, a warning should be logged, but the action is still successful +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + active: true, + addonId: "missingAddon@example.com", + }), + ]), + withSendEventSpy(), + async function unenrollMissingAddonTest({ + addonStudies: [study], + sendEventSpy, + }) { + const action = new BranchedAddonStudyAction(); + + await action.unenroll(study.recipeId); + + sendEventSpy.assertEvents([ + [ + "unenroll", + "addon_study", + study.name, + { + addonId: study.addonId, + addonVersion: study.addonVersion, + reason: "unknown", + }, + ], + ]); + + SimpleTest.endMonitorConsole(); + } +); + +// Test that the action respects the study opt-out +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + withMockPreferences(), + AddonStudies.withStudies(), + async function testOptOut({ + mockNormandyApi, + sendEventSpy, + mockPreferences, + }) { + mockPreferences.set("app.shield.optoutstudies.enabled", false); + const action = new BranchedAddonStudyAction(); + const enrollSpy = sinon.spy(action, "enroll"); + const recipe = branchedAddonStudyRecipeFactory(); + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: extensionDetailsFactory({ + id: recipe.arguments.branches[0].extensionApiId, + }), + }; + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is( + action.state, + BranchedAddonStudyAction.STATE_DISABLED, + "the action should be disabled" + ); + await action.finalize(); + is( + action.state, + BranchedAddonStudyAction.STATE_FINALIZED, + "the action should be finalized" + ); + is(action.lastError, null, "lastError should be null"); + Assert.deepEqual(enrollSpy.args, [], "enroll should not be called"); + sendEventSpy.assertEvents([]); + } +); + +// Test that the action does not enroll paused recipes +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + AddonStudies.withStudies(), + async function testEnrollmentPaused({ mockNormandyApi, sendEventSpy }) { + const action = new BranchedAddonStudyAction(); + const enrollSpy = sinon.spy(action, "enroll"); + const updateSpy = sinon.spy(action, "update"); + const recipe = branchedAddonStudyRecipeFactory({ + arguments: { isEnrollmentPaused: true }, + }); + const extensionDetails = extensionDetailsFactory({ + id: recipe.arguments.extensionApiId, + }); + mockNormandyApi.extensionDetails = { + [recipe.arguments.extensionApiId]: extensionDetails, + }; + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + const addon = await AddonManager.getAddonByID( + extensionDetails.extension_id + ); + is(addon, null, "the add-on should not have been installed"); + await action.finalize(); + ok(!updateSpy.called, "update should not be called"); + ok(enrollSpy.called, "enroll should be called"); + sendEventSpy.assertEvents([]); + } +); + +// Test that the action updates paused recipes +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + addonId: FIXTURE_ADDON_ID, + extensionHash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + extensionHashAlgorithm: "sha256", + addonVersion: "1.0", + }), + ]), + withSendEventSpy(), + withInstalledWebExtensionSafe({ id: FIXTURE_ADDON_ID, version: "1.0" }), + async function testUpdateEnrollmentPaused({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + }) { + const addonUrl = FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].url; + const recipe = recipeFromStudy(study, { + arguments: { isEnrollmentPaused: true }, + }); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + xpi: addonUrl, + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-2.0"].hash, + version: "2.0", + }), + }; + const action = new BranchedAddonStudyAction(); + const enrollSpy = sinon.spy(action, "enroll"); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + ok(!enrollSpy.called, "enroll should not be called"); + ok(updateSpy.called, "update should be called"); + sendEventSpy.assertEvents([ + [ + "update", + "addon_study", + recipe.arguments.name, + { + addonId: FIXTURE_ADDON_ID, + addonVersion: "2.0", + }, + ], + ]); + + const addon = await AddonManager.getAddonByID(FIXTURE_ADDON_ID); + Assert.strictEqual(addon.version, "2.0", "add-on should be updated"); + } +); + +// Test that unenroll called if the study is no longer sent from the server +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + AddonStudies.withStudies([branchedAddonStudyFactory()]), + async function unenroll({ addonStudies: [study] }) { + const action = new BranchedAddonStudyAction(); + const unenrollSpy = sinon.stub(action, "unenroll"); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + Assert.deepEqual( + unenrollSpy.args, + [[study.recipeId, "recipe-not-seen"]], + "unenroll should be called" + ); + } +); + +// A test function that will be parameterized over the argument "branch" below. +// Mocks the branch selector, and then tests that the user correctly gets enrolled in that branch. +const successEnrollBranchedTest = decorate( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + withStub(TelemetryEnvironment, "setExperimentActive"), + AddonStudies.withStudies(), + async function ({ + branch, + mockNormandyApi, + sendEventSpy, + setExperimentActiveStub, + }) { + ok(branch == "a" || branch == "b", "Branch should be either a or b"); + const initialAddonIds = (await AddonManager.getAllAddons()).map( + addon => addon.id + ); + const addonId = `normandydriver-${branch}@example.com`; + const otherBranchAddonId = `normandydriver-${branch == "a" ? "b" : "a"}`; + is( + await AddonManager.getAddonByID(addonId), + null, + "The add-on should not be installed at the beginning of the test" + ); + is( + await AddonManager.getAddonByID(otherBranchAddonId), + null, + "The other branch's add-on should not be installed at the beginning of the test" + ); + + const recipe = branchedAddonStudyRecipeFactory({ + arguments: { + slug: "success", + branches: [ + { slug: "a", ratio: 1, extensionApiId: 1 }, + { slug: "b", ratio: 1, extensionApiId: 2 }, + ], + }, + }); + mockNormandyApi.extensionDetails = { + [recipe.arguments.branches[0].extensionApiId]: { + id: recipe.arguments.branches[0].extensionApiId, + name: "Normandy Fixture A", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + extension_id: "normandydriver-a@example.com", + version: "1.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + hash_algorithm: "sha256", + }, + [recipe.arguments.branches[1].extensionApiId]: { + id: recipe.arguments.branches[1].extensionApiId, + name: "Normandy Fixture B", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].url, + extension_id: "normandydriver-b@example.com", + version: "1.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-b-1.0"].hash, + hash_algorithm: "sha256", + }, + }; + const extensionApiId = + recipe.arguments.branches[branch == "a" ? 0 : 1].extensionApiId; + const extensionDetails = mockNormandyApi.extensionDetails[extensionApiId]; + + const action = new BranchedAddonStudyAction(); + const chooseBranchStub = sinon.stub(action, "chooseBranch"); + chooseBranchStub.callsFake(async ({ branches }) => + branches.find(b => b.slug === branch) + ); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + const study = await AddonStudies.get(recipe.id); + sendEventSpy.assertEvents([ + [ + "enroll", + "addon_study", + recipe.arguments.slug, + { + addonId, + addonVersion: "1.0", + branch, + }, + ], + ]); + + Assert.deepEqual( + setExperimentActiveStub.args, + [[recipe.arguments.slug, branch, { type: "normandy-addonstudy" }]], + "setExperimentActive should be called" + ); + + const addon = await AddonManager.getAddonByID(addonId); + ok(addon, "The chosen branch's add-on should be installed"); + is( + await AddonManager.getAddonByID(otherBranchAddonId), + null, + "The other branch's add-on should not be installed" + ); + + Assert.deepEqual( + study, + { + recipeId: recipe.id, + slug: recipe.arguments.slug, + userFacingName: recipe.arguments.userFacingName, + userFacingDescription: recipe.arguments.userFacingDescription, + addonId, + addonVersion: "1.0", + addonUrl: FIXTURE_ADDON_DETAILS[`normandydriver-${branch}-1.0`].url, + active: true, + branch, + studyStartDate: study.studyStartDate, // This is checked below + studyEndDate: null, + extensionApiId: extensionDetails.id, + extensionHash: extensionDetails.hash, + extensionHashAlgorithm: extensionDetails.hash_algorithm, + temporaryErrorDeadline: null, + }, + "the correct study data should be stored" + ); + + // cleanup + await safeUninstallAddon(addon); + Assert.deepEqual( + (await AddonManager.getAllAddons()).map(addon => addon.id), + initialAddonIds, + "all test add-ons are removed" + ); + } +); + +add_task(args => successEnrollBranchedTest({ ...args, branch: "a" })); +add_task(args => successEnrollBranchedTest({ ...args, branch: "b" })); + +// If the enrolled branch no longer exists, unenroll +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + AddonStudies.withStudies([branchedAddonStudyFactory()]), + withSendEventSpy(), + withInstalledWebExtensionSafe( + { id: FIXTURE_ADDON_ID, version: "1.0" }, + { expectUninstall: true } + ), + async function unenrollIfBranchDisappears({ + mockNormandyApi, + addonStudies: [study], + sendEventSpy, + installedWebExtensionSafe: { addonId }, + }) { + const recipe = recipeFromStudy(study, { + arguments: { + branches: [ + { + slug: "b", // different from enrolled study + ratio: 1, + extensionApiId: study.extensionApiId, + }, + ], + }, + }); + mockNormandyApi.extensionDetails = { + [study.extensionApiId]: extensionDetailsFactory({ + id: study.extensionApiId, + extension_id: study.addonId, + hash: study.extensionHash, + }), + }; + const action = new BranchedAddonStudyAction(); + const enrollSpy = sinon.spy(action, "enroll"); + const unenrollSpy = sinon.spy(action, "unenroll"); + const updateSpy = sinon.spy(action, "update"); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + + ok(!enrollSpy.called, "Enroll should not be called"); + ok(updateSpy.called, "Update should be called"); + ok(unenrollSpy.called, "Unenroll should be called"); + + sendEventSpy.assertEvents([ + [ + "unenroll", + "addon_study", + study.name, + { + addonId, + addonVersion: study.addonVersion, + reason: "branch-removed", + branch: "a", // the original study branch + }, + ], + ]); + + is( + await AddonManager.getAddonByID(addonId), + null, + "the add-on should be uninstalled" + ); + + const storedStudy = await AddonStudies.get(recipe.id); + ok(!storedStudy.active, "Study should be inactive"); + Assert.equal(storedStudy.branch, "a", "Study's branch should not change"); + ok(storedStudy.studyEndDate, "Study's end date should be set"); + } +); + +// Test that branches without an add-on can be enrolled and unenrolled succesfully. +decorate_task( + withStudiesEnabled(), + ensureAddonCleanup(), + withMockNormandyApi(), + withSendEventSpy(), + AddonStudies.withStudies(), + async function noAddonBranches({ sendEventSpy }) { + const initialAddonIds = (await AddonManager.getAllAddons()).map( + addon => addon.id + ); + + const recipe = branchedAddonStudyRecipeFactory({ + arguments: { + slug: "no-op-branch", + branches: [{ slug: "a", ratio: 1, extensionApiId: null }], + }, + }); + + let action = new BranchedAddonStudyAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + let study = await AddonStudies.get(recipe.id); + sendEventSpy.assertEvents([ + [ + "enroll", + "addon_study", + recipe.arguments.name, + { + addonId: AddonStudies.NO_ADDON_MARKER, + addonVersion: AddonStudies.NO_ADDON_MARKER, + branch: "a", + }, + ], + ]); + + Assert.deepEqual( + (await AddonManager.getAllAddons()).map(addon => addon.id), + initialAddonIds, + "No add-on should be installed for the study" + ); + + Assert.deepEqual( + study, + { + recipeId: recipe.id, + slug: recipe.arguments.slug, + userFacingName: recipe.arguments.userFacingName, + userFacingDescription: recipe.arguments.userFacingDescription, + addonId: null, + addonVersion: null, + addonUrl: null, + active: true, + branch: "a", + studyStartDate: study.studyStartDate, // This is checked below + studyEndDate: null, + extensionApiId: null, + extensionHash: null, + extensionHashAlgorithm: null, + temporaryErrorDeadline: null, + }, + "the correct study data should be stored" + ); + ok(study.studyStartDate, "studyStartDate should have a value"); + + // Now unenroll + action = new BranchedAddonStudyAction(); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + sendEventSpy.assertEvents([ + // The event from before + [ + "enroll", + "addon_study", + recipe.arguments.name, + { + addonId: AddonStudies.NO_ADDON_MARKER, + addonVersion: AddonStudies.NO_ADDON_MARKER, + branch: "a", + }, + ], + // And a new unenroll event + [ + "unenroll", + "addon_study", + recipe.arguments.name, + { + addonId: AddonStudies.NO_ADDON_MARKER, + addonVersion: AddonStudies.NO_ADDON_MARKER, + branch: "a", + }, + ], + ]); + + Assert.deepEqual( + (await AddonManager.getAllAddons()).map(addon => addon.id), + initialAddonIds, + "The set of add-ons should not change" + ); + + study = await AddonStudies.get(recipe.id); + Assert.deepEqual( + study, + { + recipeId: recipe.id, + slug: recipe.arguments.slug, + userFacingName: recipe.arguments.userFacingName, + userFacingDescription: recipe.arguments.userFacingDescription, + addonId: null, + addonVersion: null, + addonUrl: null, + active: false, + branch: "a", + studyStartDate: study.studyStartDate, // This is checked below + studyEndDate: study.studyEndDate, // This is checked below + extensionApiId: null, + extensionHash: null, + extensionHashAlgorithm: null, + temporaryErrorDeadline: null, + }, + "the correct study data should be stored" + ); + ok(study.studyStartDate, "studyStartDate should have a value"); + ok(study.studyEndDate, "studyEndDate should have a value"); + } +); + +// Check that the appropriate set of suitabilities are considered temporary errors +decorate_task( + withStudiesEnabled(), + async function test_temporary_errors_set_deadline() { + let suitabilities = [ + { + suitability: BaseAction.suitability.SIGNATURE_ERROR, + isTemporaryError: true, + }, + { + suitability: BaseAction.suitability.CAPABILITIES_MISMATCH, + isTemporaryError: false, + }, + { + suitability: BaseAction.suitability.FILTER_MATCH, + isTemporaryError: false, + }, + { + suitability: BaseAction.suitability.FILTER_MISMATCH, + isTemporaryError: false, + }, + { + suitability: BaseAction.suitability.FILTER_ERROR, + isTemporaryError: true, + }, + { + suitability: BaseAction.suitability.ARGUMENTS_INVALID, + isTemporaryError: false, + }, + ]; + + Assert.deepEqual( + suitabilities.map(({ suitability }) => suitability).sort(), + Array.from(Object.values(BaseAction.suitability)).sort(), + "This test covers all suitabilities" + ); + + // The action should set a deadline 1 week from now. To avoid intermittent + // failures, give this a generous bound of 2 hours on either side. + let now = Date.now(); + let hour = 60 * 60 * 1000; + let expectedDeadline = now + 7 * 24 * hour; + let minDeadline = new Date(expectedDeadline - 2 * hour); + let maxDeadline = new Date(expectedDeadline + 2 * hour); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const { suitability, isTemporaryError } of suitabilities) { + const decorator = AddonStudies.withStudies([ + branchedAddonStudyFactory({ + slug: `test-for-suitability-${suitability}`, + }), + ]); + await decorator(async ({ addonStudies: [study] }) => { + let action = new BranchedAddonStudyAction(); + let recipe = recipeFromStudy(study); + await action.processRecipe(recipe, suitability); + let modifiedStudy = await AddonStudies.get(recipe.id); + + if (isTemporaryError) { + Assert.equal( + // The constructor of this object is a Date, but is not the same as + // the Date that we have in our global scope, because it got sent + // through IndexedDB. Check the name of the constructor instead. + modifiedStudy.temporaryErrorDeadline.constructor.name, + "Date", + `A temporary failure deadline should be set as a date for suitability ${suitability}` + ); + let deadline = modifiedStudy.temporaryErrorDeadline; + ok( + deadline >= minDeadline && deadline <= maxDeadline, + `The temporary failure deadline should be in the expected range for ` + + `suitability ${suitability} (got ${deadline}, expected between ${minDeadline} and ${maxDeadline})` + ); + } else { + ok( + !modifiedStudy.temporaryErrorDeadline, + `No temporary failure deadline should be set for suitability ${suitability}` + ); + } + })(); + } + } +); + +// Check that if there is an existing deadline, temporary errors don't overwrite it +decorate_task( + withStudiesEnabled(), + async function test_temporary_errors_dont_overwrite_deadline() { + let temporaryFailureSuitabilities = [ + BaseAction.suitability.SIGNATURE_ERROR, + BaseAction.suitability.FILTER_ERROR, + ]; + + // A deadline two hours in the future won't be hit during the test. + let now = Date.now(); + let hour = 2 * 60 * 60 * 1000; + let unhitDeadline = new Date(now + hour); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of temporaryFailureSuitabilities) { + const decorator = AddonStudies.withStudies([ + branchedAddonStudyFactory({ + slug: `test-for-suitability-${suitability}`, + active: true, + temporaryErrorDeadline: unhitDeadline, + }), + ]); + await decorator(async ({ addonStudies: [study] }) => { + let action = new BranchedAddonStudyAction(); + let recipe = recipeFromStudy(study); + await action.processRecipe(recipe, suitability); + let modifiedStudy = await AddonStudies.get(recipe.id); + is( + modifiedStudy.temporaryErrorDeadline.toJSON(), + unhitDeadline.toJSON(), + `The temporary failure deadline should not be cleared for suitability ${suitability}` + ); + })(); + } + } +); + +// Check that if the deadline is past, temporary errors end the study. +decorate_task( + withStudiesEnabled(), + async function test_temporary_errors_hit_deadline() { + let temporaryFailureSuitabilities = [ + BaseAction.suitability.SIGNATURE_ERROR, + BaseAction.suitability.FILTER_ERROR, + ]; + + // Set a deadline of two hours in the past, so that the deadline is triggered. + let now = Date.now(); + let hour = 60 * 60 * 1000; + let hitDeadline = new Date(now - 2 * hour); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of temporaryFailureSuitabilities) { + const decorator = AddonStudies.withStudies([ + branchedAddonStudyFactory({ + slug: `test-for-suitability-${suitability}`, + active: true, + temporaryErrorDeadline: hitDeadline, + }), + ]); + await decorator(async ({ addonStudies: [study] }) => { + let action = new BranchedAddonStudyAction(); + let recipe = recipeFromStudy(study); + await action.processRecipe(recipe, suitability); + let modifiedStudy = await AddonStudies.get(recipe.id); + ok( + !modifiedStudy.active, + `The study should end for suitability ${suitability}` + ); + })(); + } + } +); + +// Check that non-temporary-error suitabilities clear the temporary deadline +decorate_task( + withStudiesEnabled(), + async function test_non_temporary_error_clears_temporary_error_deadline() { + let suitabilitiesThatShouldClearDeadline = [ + BaseAction.suitability.CAPABILITIES_MISMATCH, + BaseAction.suitability.FILTER_MATCH, + BaseAction.suitability.FILTER_MISMATCH, + BaseAction.suitability.ARGUMENTS_INVALID, + ]; + + // Use a deadline in the past to demonstrate that even if the deadline has + // passed, only a temporary error suitability ends the study. + let now = Date.now(); + let hour = 60 * 60 * 1000; + let hitDeadline = new Date(now - 2 * hour); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of suitabilitiesThatShouldClearDeadline) { + const decorator = AddonStudies.withStudies([ + branchedAddonStudyFactory({ + slug: `test-for-suitability-${suitability}`.toLocaleLowerCase(), + active: true, + temporaryErrorDeadline: hitDeadline, + }), + ]); + await decorator(async ({ addonStudies: [study] }) => { + let action = new BranchedAddonStudyAction(); + let recipe = recipeFromStudy(study); + await action.processRecipe(recipe, suitability); + let modifiedStudy = await AddonStudies.get(recipe.id); + ok( + !modifiedStudy.temporaryErrorDeadline, + `The temporary failure deadline should be cleared for suitabilitiy ${suitability}` + ); + })(); + } + } +); + +// Check that invalid deadlines are reset +decorate_task( + withStudiesEnabled(), + async function test_non_temporary_error_clears_temporary_error_deadline() { + let temporaryFailureSuitabilities = [ + BaseAction.suitability.SIGNATURE_ERROR, + BaseAction.suitability.FILTER_ERROR, + ]; + + // The action should set a deadline 1 week from now. To avoid intermittent + // failures, give this a generous bound of 2 hours on either side. + let invalidDeadline = new Date("not a valid date"); + let now = Date.now(); + let hour = 60 * 60 * 1000; + let expectedDeadline = now + 7 * 24 * hour; + let minDeadline = new Date(expectedDeadline - 2 * hour); + let maxDeadline = new Date(expectedDeadline + 2 * hour); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of temporaryFailureSuitabilities) { + const decorator = AddonStudies.withStudies([ + branchedAddonStudyFactory({ + slug: `test-for-suitability-${suitability}`.toLocaleLowerCase(), + active: true, + temporaryErrorDeadline: invalidDeadline, + }), + ]); + await decorator(async ({ addonStudies: [study] }) => { + let action = new BranchedAddonStudyAction(); + let recipe = recipeFromStudy(study); + await action.processRecipe(recipe, suitability); + is(action.lastError, null, "No errors should be reported"); + let modifiedStudy = await AddonStudies.get(recipe.id); + Assert.notEqual( + modifiedStudy.temporaryErrorDeadline, + invalidDeadline, + `The temporary failure deadline should be reset for suitabilitiy ${suitability}` + ); + let deadline = new Date(modifiedStudy.temporaryErrorDeadline); + ok( + deadline >= minDeadline && deadline <= maxDeadline, + `The temporary failure deadline should be reset to a valid deadline for ${suitability}` + ); + })(); + } + } +); + +// Check that an already unenrolled study doesn't try to unenroll again if +// the recipe doesn't apply the client anymore. +decorate_task( + withStudiesEnabled(), + async function test_unenroll_when_already_expired() { + // Use a deadline that is already past + const now = new Date(); + const hour = 1000 * 60 * 60; + const temporaryErrorDeadline = new Date(now - hour * 2).toJSON(); + + const suitabilitiesToCheck = Object.values(BaseAction.suitability); + + const subtest = decorate( + AddonStudies.withStudies([ + branchedAddonStudyFactory({ + active: false, + temporaryErrorDeadline, + }), + ]), + + async ({ addonStudies: [study], suitability }) => { + const recipe = recipeFromStudy(study); + const action = new BranchedAddonStudyAction(); + const unenrollSpy = sinon.spy(action.unenroll); + await action.processRecipe(recipe, suitability); + Assert.deepEqual( + unenrollSpy.args, + [], + `Stop should not be called for ${suitability}` + ); + } + ); + + for (const suitability of suitabilitiesToCheck) { + await subtest({ suitability }); + } + } +); + +// If no recipes are received, it should be considered a temporary error +decorate_task( + withStudiesEnabled(), + AddonStudies.withStudies([branchedAddonStudyFactory({ active: true })]), + withSpy(BranchedAddonStudyAction.prototype, "unenroll"), + withStub(BranchedAddonStudyAction.prototype, "_considerTemporaryError"), + async function testNoRecipes({ + unenrollSpy, + _considerTemporaryErrorStub, + addonStudies: [study], + }) { + let action = new BranchedAddonStudyAction(); + await action.finalize({ noRecipes: true }); + + Assert.deepEqual(unenrollSpy.args, [], "Unenroll should not be called"); + Assert.deepEqual( + _considerTemporaryErrorStub.args, + [[{ study, reason: "no-recipes" }]], + "The experiment should accumulate a temporary error" + ); + } +); + +// If recipes are received, but the flag that none were received is set, an error should be thrown +decorate_task( + withStudiesEnabled(), + AddonStudies.withStudies([branchedAddonStudyFactory({ active: true })]), + async function testNoRecipes({ addonStudies: [study] }) { + let action = new BranchedAddonStudyAction(); + let recipe = recipeFromStudy(study); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MISMATCH); + await action.finalize({ noRecipes: true }); + ok( + action.lastError instanceof BranchedAddonStudyAction.BadNoRecipesArg, + "An error should be logged since some recipes were received" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_ConsoleLogAction.js b/toolkit/components/normandy/test/browser/browser_actions_ConsoleLogAction.js new file mode 100644 index 0000000000..910de357f6 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_ConsoleLogAction.js @@ -0,0 +1,62 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { ConsoleLogAction } = ChromeUtils.importESModule( + "resource://normandy/actions/ConsoleLogAction.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); + +// Test that logging works +add_task(async function logging_works() { + const action = new ConsoleLogAction(); + const infoStub = sinon.stub(action.log, "info"); + try { + const recipe = { id: 1, arguments: { message: "Hello, world!" } }; + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + Assert.deepEqual( + infoStub.args, + ["Hello, world!"], + "the message should be logged" + ); + } finally { + infoStub.restore(); + } +}); + +// test that argument validation works +decorate_task( + withStub(Uptake, "reportRecipe"), + async function arguments_are_validated({ reportRecipeStub }) { + const action = new ConsoleLogAction(); + const infoStub = sinon.stub(action.log, "info"); + + try { + // message is required + let recipe = { id: 1, arguments: {} }; + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + Assert.deepEqual(infoStub.args, [], "no message should be logged"); + Assert.deepEqual(reportRecipeStub.args, [ + [recipe, Uptake.RECIPE_EXECUTION_ERROR], + ]); + + reportRecipeStub.reset(); + + // message must be a string + recipe = { id: 1, arguments: { message: 1 } }; + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "lastError should be null"); + Assert.deepEqual(infoStub.args, [], "no message should be logged"); + Assert.deepEqual(reportRecipeStub.args, [ + [recipe, Uptake.RECIPE_EXECUTION_ERROR], + ]); + } finally { + infoStub.restore(); + } + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_MessagingExperimentAction.js b/toolkit/components/normandy/test/browser/browser_actions_MessagingExperimentAction.js new file mode 100644 index 0000000000..0f16ff1436 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_MessagingExperimentAction.js @@ -0,0 +1,67 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); +const { MessagingExperimentAction } = ChromeUtils.importESModule( + "resource://normandy/actions/MessagingExperimentAction.sys.mjs" +); + +const { _ExperimentManager, ExperimentManager } = ChromeUtils.importESModule( + "resource://nimbus/lib/ExperimentManager.sys.mjs" +); + +decorate_task( + withStudiesEnabled(), + withStub(Uptake, "reportRecipe"), + async function arguments_are_validated({ reportRecipeStub }) { + const action = new MessagingExperimentAction(); + + is( + action.manager, + ExperimentManager, + "should set .manager to ExperimentManager singleton" + ); + // Override this for the purposes of the test + action.manager = new _ExperimentManager(); + await action.manager.onStartup(); + const onRecipeStub = sinon.spy(action.manager, "onRecipe"); + + const recipe = { + id: 1, + arguments: { + slug: "foo", + isEnrollmentPaused: false, + branches: [ + { + slug: "control", + ratio: 1, + groups: ["green"], + value: { title: "hello" }, + }, + { + slug: "variant", + ratio: 1, + groups: ["green"], + value: { title: "world" }, + }, + ], + }, + }; + + ok(action.validateArguments(recipe.arguments), "should validate arguments"); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(reportRecipeStub.args, [[recipe, Uptake.RECIPE_SUCCESS]]); + Assert.deepEqual( + onRecipeStub.args, + [[recipe.arguments, "normandy"]], + "should call onRecipe with recipe args and 'normandy' source" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_PreferenceExperimentAction.js b/toolkit/components/normandy/test/browser/browser_actions_PreferenceExperimentAction.js new file mode 100644 index 0000000000..5359174169 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_PreferenceExperimentAction.js @@ -0,0 +1,914 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +const { Sampling } = ChromeUtils.importESModule( + "resource://gre/modules/components-utils/Sampling.sys.mjs" +); + +const { ClientEnvironment } = ChromeUtils.importESModule( + "resource://normandy/lib/ClientEnvironment.sys.mjs" +); +const { PreferenceExperiments } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceExperiments.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { PreferenceExperimentAction } = ChromeUtils.importESModule( + "resource://normandy/actions/PreferenceExperimentAction.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +function branchFactory(opts = {}) { + const defaultPreferences = { + "fake.preference": {}, + }; + const defaultPrefInfo = { + preferenceType: "string", + preferenceBranchType: "default", + preferenceValue: "foo", + }; + const preferences = {}; + for (const [prefName, prefInfo] of Object.entries( + opts.preferences || defaultPreferences + )) { + preferences[prefName] = { ...defaultPrefInfo, ...prefInfo }; + } + return { + slug: "test", + ratio: 1, + ...opts, + preferences, + }; +} + +function argumentsFactory(args) { + const defaultBranches = (args && args.branches) || [{ preferences: [] }]; + const branches = defaultBranches.map(branchFactory); + return { + slug: "test", + userFacingName: "Super Cool Test Experiment", + userFacingDescription: + "Test experiment from browser_actions_PreferenceExperimentAction.", + isHighPopulation: false, + isEnrollmentPaused: false, + ...args, + branches, + }; +} + +function prefExperimentRecipeFactory(args) { + return recipeFactory({ + name: "preference-experiment", + arguments: argumentsFactory(args), + }); +} + +decorate_task( + withStudiesEnabled(), + withStub(Uptake, "reportRecipe"), + async function run_without_errors({ reportRecipeStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + // Errors thrown in actions are caught and silenced, so instead check for an + // explicit success here. + Assert.deepEqual(reportRecipeStub.args, [[recipe, Uptake.RECIPE_SUCCESS]]); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(Uptake, "reportRecipe"), + withStub(Uptake, "reportAction"), + withPrefEnv({ set: [["app.shield.optoutstudies.enabled", false]] }), + async function checks_disabled({ reportRecipeStub, reportActionStub }) { + const action = new PreferenceExperimentAction(); + action.log = mockLogger(); + + const recipe = prefExperimentRecipeFactory(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + + Assert.ok(action.log.debug.args.length === 1); + Assert.deepEqual(action.log.debug.args[0], [ + "User has opted-out of opt-out experiments, disabling action.", + ]); + Assert.deepEqual(action.log.warn.args, [ + [ + "Skipping recipe preference-experiment because PreferenceExperimentAction " + + "was disabled during preExecution.", + ], + ]); + + await action.finalize(); + Assert.ok(action.log.debug.args.length === 2); + Assert.deepEqual(action.log.debug.args[1], [ + "Skipping post-execution hook for PreferenceExperimentAction because it is disabled.", + ]); + Assert.deepEqual(reportRecipeStub.args, [ + [recipe, Uptake.RECIPE_ACTION_DISABLED], + ]); + Assert.deepEqual(reportActionStub.args, [ + [action.name, Uptake.ACTION_SUCCESS], + ]); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "start"), + PreferenceExperiments.withMockExperiments([]), + async function enroll_user_if_never_been_in_experiment({ startStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + slug: "test", + branches: [ + { + slug: "branch1", + preferences: { + "fake.preference": { + preferenceBranchType: "user", + preferenceValue: "branch1", + }, + }, + ratio: 1, + }, + { + slug: "branch2", + preferences: { + "fake.preference": { + preferenceBranchType: "user", + preferenceValue: "branch2", + }, + }, + ratio: 1, + }, + ], + }); + sinon + .stub(action, "chooseBranch") + .callsFake(async function (slug, branches) { + return branches[0]; + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(startStub.args, [ + [ + { + slug: "test", + actionName: "PreferenceExperimentAction", + branch: "branch1", + preferences: { + "fake.preference": { + preferenceValue: "branch1", + preferenceBranchType: "user", + preferenceType: "string", + }, + }, + experimentType: "exp", + userFacingName: "Super Cool Test Experiment", + userFacingDescription: + "Test experiment from browser_actions_PreferenceExperimentAction.", + }, + ], + ]); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "markLastSeen"), + PreferenceExperiments.withMockExperiments([{ slug: "test", expired: false }]), + async function markSeen_if_experiment_active({ markLastSeenStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + name: "test", + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(markLastSeenStub.args, [["test"]]); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "markLastSeen"), + PreferenceExperiments.withMockExperiments([{ slug: "test", expired: true }]), + async function dont_markSeen_if_experiment_expired({ markLastSeenStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + name: "test", + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(markLastSeenStub.args, [], "markLastSeen was not called"); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "start"), + async function do_nothing_if_enrollment_paused({ startStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + isEnrollmentPaused: true, + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(startStub.args, [], "start was not called"); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "stop"), + PreferenceExperiments.withMockExperiments([ + { slug: "seen", expired: false, actionName: "PreferenceExperimentAction" }, + { + slug: "unseen", + expired: false, + actionName: "PreferenceExperimentAction", + }, + ]), + async function stop_experiments_not_seen({ stopStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + slug: "seen", + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(stopStub.args, [ + [ + "unseen", + { + resetValue: true, + reason: "recipe-not-seen", + caller: "PreferenceExperimentAction._finalize", + }, + ], + ]); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "stop"), + PreferenceExperiments.withMockExperiments([ + { + slug: "seen", + expired: false, + actionName: "SinglePreferenceExperimentAction", + }, + { + slug: "unseen", + expired: false, + actionName: "SinglePreferenceExperimentAction", + }, + ]), + async function dont_stop_experiments_for_other_action({ stopStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + name: "seen", + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual( + stopStub.args, + [], + "stop not called for other action's experiments" + ); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "start"), + withStub(Uptake, "reportRecipe"), + PreferenceExperiments.withMockExperiments([ + { + slug: "conflict", + preferences: { + "conflict.pref": {}, + }, + expired: false, + }, + ]), + async function do_nothing_if_preference_is_already_being_tested({ + startStub, + reportRecipeStub, + }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + name: "new", + branches: [ + { + preferences: { "conflict.pref": {} }, + }, + ], + }); + action.chooseBranch = sinon + .stub() + .callsFake(async function (slug, branches) { + return branches[0]; + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(reportRecipeStub.args, [ + [recipe, Uptake.RECIPE_EXECUTION_ERROR], + ]); + Assert.deepEqual(startStub.args, [], "start not called"); + // No way to get access to log message/Error thrown + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "start"), + PreferenceExperiments.withMockExperiments([]), + async function experimentType_with_isHighPopulation_false({ startStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + isHighPopulation: false, + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(startStub.args[0][0].experimentType, "exp"); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(PreferenceExperiments, "start"), + PreferenceExperiments.withMockExperiments([]), + async function experimentType_with_isHighPopulation_true({ startStub }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ + isHighPopulation: true, + }); + + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + Assert.deepEqual(startStub.args[0][0].experimentType, "exp-highpop"); + } +); + +decorate_task( + withStudiesEnabled(), + withStub(Sampling, "ratioSample"), + async function chooseBranch_uses_ratioSample({ ratioSampleStub }) { + ratioSampleStub.returns(Promise.resolve(1)); + const action = new PreferenceExperimentAction(); + const branches = [ + { + preferences: { + "fake.preference": { + preferenceValue: "branch0", + }, + }, + ratio: 1, + }, + { + preferences: { + "fake.preference": { + preferenceValue: "branch1", + }, + }, + ratio: 2, + }, + ]; + const sandbox = sinon.createSandbox(); + let result; + try { + sandbox.stub(ClientEnvironment, "userId").get(() => "fake-id"); + result = await action.chooseBranch("exp-slug", branches); + } finally { + sandbox.restore(); + } + + Assert.deepEqual(ratioSampleStub.args, [ + ["fake-id-exp-slug-branch", [1, 2]], + ]); + Assert.deepEqual(result, branches[1]); + } +); + +decorate_task( + withStudiesEnabled(), + withMockPreferences(), + PreferenceExperiments.withMockExperiments([]), + async function integration_test_enroll_and_unenroll({ mockPreferences }) { + mockPreferences.set("fake.preference", "oldvalue", "user"); + const recipe = prefExperimentRecipeFactory({ + slug: "integration test experiment", + branches: [ + { + slug: "branch1", + preferences: { + "fake.preference": { + preferenceBranchType: "user", + preferenceValue: "branch1", + }, + }, + ratio: 1, + }, + { + slug: "branch2", + preferences: { + "fake.preference": { + preferenceBranchType: "user", + preferenceValue: "branch2", + }, + }, + ratio: 1, + }, + ], + userFacingName: "userFacingName", + userFacingDescription: "userFacingDescription", + }); + + // Session 1: we see the above recipe and enroll in the experiment. + const action = new PreferenceExperimentAction(); + sinon + .stub(action, "chooseBranch") + .callsFake(async function (slug, branches) { + return branches[0]; + }); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + + const activeExperiments = await PreferenceExperiments.getAllActive(); + ok(!!activeExperiments.length); + Assert.deepEqual(activeExperiments, [ + { + slug: "integration test experiment", + actionName: "PreferenceExperimentAction", + branch: "branch1", + preferences: { + "fake.preference": { + preferenceBranchType: "user", + preferenceValue: "branch1", + preferenceType: "string", + previousPreferenceValue: "oldvalue", + }, + }, + expired: false, + lastSeen: activeExperiments[0].lastSeen, // can't predict date + experimentType: "exp", + userFacingName: "userFacingName", + userFacingDescription: "userFacingDescription", + }, + ]); + + // Session 2: recipe is filtered out and so does not run. + const action2 = new PreferenceExperimentAction(); + await action2.finalize(); + + // Experiment should be unenrolled + Assert.deepEqual(await PreferenceExperiments.getAllActive(), []); + } +); + +// Check that the appropriate set of suitabilities are considered temporary errors +decorate_task( + withStudiesEnabled(), + async function test_temporary_errors_set_deadline() { + let suitabilities = [ + { + suitability: BaseAction.suitability.SIGNATURE_ERROR, + isTemporaryError: true, + }, + { + suitability: BaseAction.suitability.CAPABILITIES_MISMATCH, + isTemporaryError: false, + }, + { + suitability: BaseAction.suitability.FILTER_MATCH, + isTemporaryError: false, + }, + { + suitability: BaseAction.suitability.FILTER_MISMATCH, + isTemporaryError: false, + }, + { + suitability: BaseAction.suitability.FILTER_ERROR, + isTemporaryError: true, + }, + { + suitability: BaseAction.suitability.ARGUMENTS_INVALID, + isTemporaryError: false, + }, + ]; + + Assert.deepEqual( + suitabilities.map(({ suitability }) => suitability).sort(), + Array.from(Object.values(BaseAction.suitability)).sort(), + "This test covers all suitabilities" + ); + + // The action should set a deadline 1 week from now. To avoid intermittent + // failures, give this a generous bound of 2 hour on either side. + let now = Date.now(); + let hour = 60 * 60 * 1000; + let expectedDeadline = now + 7 * 24 * hour; + let minDeadline = new Date(expectedDeadline - 2 * hour); + let maxDeadline = new Date(expectedDeadline + 2 * hour); + + // For each suitability, build a decorator that sets up a suitable + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const { suitability, isTemporaryError } of suitabilities) { + const decorator = PreferenceExperiments.withMockExperiments([ + { slug: `test-for-suitability-${suitability}` }, + ]); + await decorator(async ({ prefExperiments: [experiment] }) => { + let action = new PreferenceExperimentAction(); + let recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + await action.processRecipe(recipe, suitability); + let modifiedExperiment = await PreferenceExperiments.get( + experiment.slug + ); + if (isTemporaryError) { + is( + typeof modifiedExperiment.temporaryErrorDeadline, + "string", + `A temporary failure deadline should be set as a string for suitability ${suitability}` + ); + let deadline = new Date(modifiedExperiment.temporaryErrorDeadline); + ok( + deadline >= minDeadline && deadline <= maxDeadline, + `The temporary failure deadline should be in the expected range for ` + + `suitability ${suitability} (got ${deadline})` + ); + } else { + ok( + !modifiedExperiment.temporaryErrorDeadline, + `No temporary failure deadline should be set for suitability ${suitability}` + ); + } + })(); + } + } +); + +// Check that if there is an existing deadline, temporary errors don't overwrite it +decorate_task( + withStudiesEnabled(), + PreferenceExperiments.withMockExperiments([]), + async function test_temporary_errors_dont_overwrite_deadline() { + let temporaryFailureSuitabilities = [ + BaseAction.suitability.SIGNATURE_ERROR, + BaseAction.suitability.FILTER_ERROR, + ]; + + // A deadline two hours in the future won't be hit during the test. + let now = Date.now(); + let hour = 2 * 60 * 60 * 1000; + let unhitDeadline = new Date(now + hour).toJSON(); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of temporaryFailureSuitabilities) { + const decorator = PreferenceExperiments.withMockExperiments([ + { + slug: `test-for-suitability-${suitability}`, + expired: false, + temporaryErrorDeadline: unhitDeadline, + }, + ]); + await decorator(async ({ prefExperiments: [experiment] }) => { + let action = new PreferenceExperimentAction(); + let recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + await action.processRecipe(recipe, suitability); + let modifiedExperiment = await PreferenceExperiments.get( + experiment.slug + ); + is( + modifiedExperiment.temporaryErrorDeadline, + unhitDeadline, + `The temporary failure deadline should not be cleared for suitability ${suitability}` + ); + })(); + } + } +); + +// Check that if the deadline is past, temporary errors end the experiment. +decorate_task( + withStudiesEnabled(), + async function test_temporary_errors_hit_deadline() { + let temporaryFailureSuitabilities = [ + BaseAction.suitability.SIGNATURE_ERROR, + BaseAction.suitability.FILTER_ERROR, + ]; + + // Set a deadline of two hours in the past, so that the experiment expires. + let now = Date.now(); + let hour = 2 * 60 * 60 * 1000; + let hitDeadline = new Date(now - hour).toJSON(); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of temporaryFailureSuitabilities) { + const decorator = PreferenceExperiments.withMockExperiments([ + { + slug: `test-for-suitability-${suitability}`, + expired: false, + temporaryErrorDeadline: hitDeadline, + preferences: [], + branch: "test-branch", + }, + ]); + await decorator(async ({ prefExperiments: [experiment] }) => { + let action = new PreferenceExperimentAction(); + let recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + await action.processRecipe(recipe, suitability); + let modifiedExperiment = await PreferenceExperiments.get( + experiment.slug + ); + ok( + modifiedExperiment.expired, + `The experiment should be expired for suitability ${suitability}` + ); + })(); + } + } +); + +// Check that non-temporary-error suitabilities clear the temporary deadline +decorate_task( + withStudiesEnabled(), + PreferenceExperiments.withMockExperiments([]), + async function test_non_temporary_error_clears_temporary_error_deadline() { + let suitabilitiesThatShouldClearDeadline = [ + BaseAction.suitability.CAPABILITIES_MISMATCH, + BaseAction.suitability.FILTER_MATCH, + BaseAction.suitability.FILTER_MISMATCH, + BaseAction.suitability.ARGUMENTS_INVALID, + ]; + + // Use a deadline in the past to demonstrate that even if the deadline has + // passed, only a temporary error suitability ends the experiment. + let now = Date.now(); + let hour = 60 * 60 * 1000; + let hitDeadline = new Date(now - 2 * hour).toJSON(); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of suitabilitiesThatShouldClearDeadline) { + const decorator = PreferenceExperiments.withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ + slug: `test-for-suitability-${suitability}`.toLocaleLowerCase(), + expired: false, + temporaryErrorDeadline: hitDeadline, + }), + ]); + await decorator(async ({ prefExperiments: [experiment] }) => { + let action = new PreferenceExperimentAction(); + let recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + await action.processRecipe(recipe, suitability); + let modifiedExperiment = await PreferenceExperiments.get( + experiment.slug + ); + ok( + !modifiedExperiment.temporaryErrorDeadline, + `The temporary failure deadline should be cleared for suitabilitiy ${suitability}` + ); + })(); + } + } +); + +// Check that invalid deadlines are reset +decorate_task( + withStudiesEnabled(), + PreferenceExperiments.withMockExperiments([]), + async function test_non_temporary_error_clears_temporary_error_deadline() { + let temporaryFailureSuitabilities = [ + BaseAction.suitability.SIGNATURE_ERROR, + BaseAction.suitability.FILTER_ERROR, + ]; + + // The action should set a deadline 1 week from now. To avoid intermittent + // failures, give this a generous bound of 2 hours on either side. + let invalidDeadline = "not a valid date"; + let now = Date.now(); + let hour = 60 * 60 * 1000; + let expectedDeadline = now + 7 * 24 * hour; + let minDeadline = new Date(expectedDeadline - 2 * hour); + let maxDeadline = new Date(expectedDeadline + 2 * hour); + + // For each suitability, build a decorator that sets up a suitabilty + // environment, and then call that decorator with a sub-test that asserts + // the suitability is handled correctly. + for (const suitability of temporaryFailureSuitabilities) { + const decorator = PreferenceExperiments.withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ + slug: `test-for-suitability-${suitability}`.toLocaleLowerCase(), + expired: false, + temporaryErrorDeadline: invalidDeadline, + }), + ]); + await decorator(async ({ prefExperiments: [experiment] }) => { + let action = new PreferenceExperimentAction(); + let recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + await action.processRecipe(recipe, suitability); + is(action.lastError, null, "No errors should be reported"); + let modifiedExperiment = await PreferenceExperiments.get( + experiment.slug + ); + Assert.notEqual( + modifiedExperiment.temporaryErrorDeadline, + invalidDeadline, + `The temporary failure deadline should be reset for suitabilitiy ${suitability}` + ); + let deadline = new Date(modifiedExperiment.temporaryErrorDeadline); + ok( + deadline >= minDeadline && deadline <= maxDeadline, + `The temporary failure deadline should be reset to a valid deadline for ${suitability}` + ); + })(); + } + } +); + +// Check that an already unenrolled experiment doesn't try to unenroll again if +// the filter does not match. +decorate_task( + withStudiesEnabled(), + withSpy(PreferenceExperiments, "stop"), + async function test_stop_when_already_expired({ stopSpy }) { + // Use a deadline that is already past + const now = new Date(); + const hour = 1000 * 60 * 60; + const temporaryErrorDeadline = new Date(now - hour * 2).toJSON(); + + const suitabilitiesToCheck = Object.values(BaseAction.suitability); + + const subtest = decorate( + PreferenceExperiments.withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ + expired: true, + temporaryErrorDeadline, + }), + ]), + + async ({ prefExperiments: [experiment], suitability }) => { + const recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + const action = new PreferenceExperimentAction(); + await action.processRecipe(recipe, suitability); + Assert.deepEqual( + stopSpy.args, + [], + `Stop should not be called for ${suitability}` + ); + } + ); + + for (const suitability of suitabilitiesToCheck) { + await subtest({ suitability }); + } + } +); + +// If no recipes are received, it should be considered a temporary error +decorate_task( + withStudiesEnabled(), + PreferenceExperiments.withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ expired: false }), + ]), + withSpy(PreferenceExperiments, "stop"), + withStub(PreferenceExperimentAction.prototype, "_considerTemporaryError"), + async function testNoRecipes({ + stopSpy, + _considerTemporaryErrorStub, + prefExperiments: [experiment], + }) { + let action = new PreferenceExperimentAction(); + await action.finalize({ noRecipes: true }); + + Assert.deepEqual(stopSpy.args, [], "Stop should not be called"); + Assert.deepEqual( + _considerTemporaryErrorStub.args, + [[{ experiment, reason: "no-recipes" }]], + "The experiment should accumulate a temporary error" + ); + } +); + +// If recipes are received, but the flag that none were received is set, an error should be thrown +decorate_task( + withStudiesEnabled(), + PreferenceExperiments.withMockExperiments([ + NormandyTestUtils.factories.preferenceStudyFactory({ expired: false }), + ]), + withSpy(PreferenceExperiments, "stop"), + withStub(PreferenceExperimentAction.prototype, "_considerTemporaryError"), + async function testNoRecipes({ + stopSpy, + _considerTemporaryErrorStub, + prefExperiments: [experiment], + }) { + const action = new PreferenceExperimentAction(); + const recipe = prefExperimentRecipeFactory({ slug: experiment.slug }); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MISMATCH); + await action.finalize({ noRecipes: true }); + ok( + action.lastError instanceof PreferenceExperimentAction.BadNoRecipesArg, + "An error should be logged since some recipes were received" + ); + } +); + +// Unenrolling from an experiment where a user has changed some prefs should not override user choice +decorate_task( + withStudiesEnabled(), + withMockPreferences(), + PreferenceExperiments.withMockExperiments(), + async function testUserPrefNoReset({ mockPreferences }) { + mockPreferences.set("test.pref.should-reset", "builtin value", "default"); + mockPreferences.set("test.pref.user-override", "builtin value", "default"); + + await PreferenceExperiments.start({ + slug: "test-experiment", + actionName: "PreferenceExperimentAction", + isHighPopulation: false, + isEnrollmentPaused: false, + userFacingName: "Test Experiment", + userFacingDescription: "Test description", + branch: "test", + preferences: { + "test.pref.should-reset": { + preferenceValue: "experiment value", + preferenceType: "string", + previousPreferenceValue: "builtin value", + preferenceBranchType: "user", + overridden: false, + }, + "test.pref.user-override": { + preferenceValue: "experiment value", + preferenceType: "string", + previousPreferenceValue: "builtin value", + preferenceBranchType: "user", + overridden: false, + }, + }, + }); + + mockPreferences.set("test.pref.user-override", "user value", "user"); + + let exp = await PreferenceExperiments.get("test-experiment"); + is( + exp.preferences["test.pref.user-override"].overridden, + true, + "Changed pref should be marked as overridden" + ); + is( + exp.preferences["test.pref.should-reset"].overridden, + false, + "Unchanged pref should not be marked as overridden" + ); + + await PreferenceExperiments.stop("test-experiment", { + resetValue: true, + reason: "test-reason", + }); + + is( + Services.prefs.getCharPref("test.pref.should-reset"), + "builtin value", + "pref that was not overridden should reset to builtin" + ); + is( + Services.prefs.getCharPref("test.pref.user-override"), + "user value", + "pref that was overridden should keep user value" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_PreferenceRollbackAction.js b/toolkit/components/normandy/test/browser/browser_actions_PreferenceRollbackAction.js new file mode 100644 index 0000000000..ac28ba05ad --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_PreferenceRollbackAction.js @@ -0,0 +1,348 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { PreferenceRollbackAction } = ChromeUtils.importESModule( + "resource://normandy/actions/PreferenceRollbackAction.sys.mjs" +); +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); +const { PreferenceRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceRollouts.sys.mjs" +); + +// Test that a simple recipe rollsback as expected +decorate_task( + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function simple_rollback({ setExperimentInactiveStub, sendEventSpy }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref1", 2); + Services.prefs + .getDefaultBranch("") + .setCharPref("test.pref2", "rollout value"); + Services.prefs.getDefaultBranch("").setBoolPref("test.pref3", true); + + await PreferenceRollouts.add({ + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref1", value: 2, previousValue: 1 }, + { + preferenceName: "test.pref2", + value: "rollout value", + previousValue: "builtin value", + }, + { preferenceName: "test.pref3", value: true, previousValue: false }, + ], + }); + + const recipe = { id: 1, arguments: { rolloutSlug: "test-rollout" } }; + + const action = new PreferenceRollbackAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + // rollout prefs are reset + is( + Services.prefs.getIntPref("test.pref1"), + 1, + "integer pref should be rolled back" + ); + is( + Services.prefs.getCharPref("test.pref2"), + "builtin value", + "string pref should be rolled back" + ); + is( + Services.prefs.getBoolPref("test.pref3"), + false, + "boolean pref should be rolled back" + ); + + // start up prefs are unset + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref1"), + Services.prefs.PREF_INVALID, + "integer startup pref should be unset" + ); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref2"), + Services.prefs.PREF_INVALID, + "string startup pref should be unset" + ); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref3"), + Services.prefs.PREF_INVALID, + "boolean startup pref should be unset" + ); + + // rollout in db was updated + const rollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ROLLED_BACK, + preferences: [ + { preferenceName: "test.pref1", value: 2, previousValue: 1 }, + { + preferenceName: "test.pref2", + value: "rollout value", + previousValue: "builtin value", + }, + { preferenceName: "test.pref3", value: true, previousValue: false }, + ], + }, + ], + "Rollout should be updated in db" + ); + + // Telemetry is updated + sendEventSpy.assertEvents([ + [ + "unenroll", + "preference_rollback", + recipe.arguments.rolloutSlug, + { reason: "rollback" }, + ], + ]); + Assert.deepEqual( + setExperimentInactiveStub.args, + [["test-rollout"]], + "the telemetry experiment should deactivated" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref1"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref2"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref3"); + } +); + +// Test that a graduated rollout can't be rolled back +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function cant_rollback_graduated({ sendEventSpy }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref", 1); + await PreferenceRollouts.add({ + slug: "graduated-rollout", + state: PreferenceRollouts.STATE_GRADUATED, + preferences: [ + { preferenceName: "test.pref", value: 1, previousValue: 1 }, + ], + }); + + let recipe = { id: 1, arguments: { rolloutSlug: "graduated-rollout" } }; + + const action = new PreferenceRollbackAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is(Services.prefs.getIntPref("test.pref"), 1, "pref should not change"); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref"), + Services.prefs.PREF_INVALID, + "no startup pref should be added" + ); + + // rollout in the DB hasn't changed + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [ + { + slug: "graduated-rollout", + state: PreferenceRollouts.STATE_GRADUATED, + preferences: [ + { preferenceName: "test.pref", value: 1, previousValue: 1 }, + ], + }, + ], + "Rollout should not change in db" + ); + + sendEventSpy.assertEvents([ + [ + "unenrollFailed", + "preference_rollback", + "graduated-rollout", + { reason: "graduated" }, + ], + ]); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// Test that a rollback without a matching rollout does not send telemetry +decorate_task( + withSendEventSpy(), + withStub(Uptake, "reportRecipe"), + PreferenceRollouts.withTestMock(), + async function rollback_without_rollout({ sendEventSpy, reportRecipeStub }) { + let recipe = { id: 1, arguments: { rolloutSlug: "missing-rollout" } }; + + const action = new PreferenceRollbackAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + sendEventSpy.assertEvents([]); + Assert.deepEqual( + reportRecipeStub.args, + [[recipe, Uptake.RECIPE_SUCCESS]], + "recipe should be reported as succesful" + ); + } +); + +// Test that rolling back an already rolled back recipe doesn't do anything +decorate_task( + withStub(TelemetryEnvironment, "setExperimentInactive"), + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function rollback_already_rolled_back({ + setExperimentInactiveStub, + sendEventSpy, + }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref", 1); + + const recipe = { id: 1, arguments: { rolloutSlug: "test-rollout" } }; + const rollout = { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ROLLED_BACK, + preferences: [ + { preferenceName: "test.pref", value: 2, previousValue: 1 }, + ], + }; + await PreferenceRollouts.add(rollout); + + const action = new PreferenceRollbackAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is(Services.prefs.getIntPref("test.pref"), 1, "pref shouldn't change"); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref"), + Services.prefs.PREF_INVALID, + "startup pref should not be set" + ); + + // rollout in db was updated + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [rollout], + "Rollout shouldn't change in db" + ); + + // Telemetry is updated + sendEventSpy.assertEvents([]); + Assert.deepEqual( + setExperimentInactiveStub.args, + [], + "telemetry experiments should not be updated" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// Test that a rollback doesn't affect user prefs +decorate_task( + PreferenceRollouts.withTestMock(), + async function simple_rollback() { + Services.prefs + .getDefaultBranch("") + .setCharPref("test.pref", "rollout value"); + Services.prefs.setCharPref("test.pref", "user value"); + + await PreferenceRollouts.add({ + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { + preferenceName: "test.pref", + value: "rollout value", + previousValue: "builtin value", + }, + ], + }); + + const recipe = { id: 1, arguments: { rolloutSlug: "test-rollout" } }; + + const action = new PreferenceRollbackAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is( + Services.prefs.getDefaultBranch("").getCharPref("test.pref"), + "builtin value", + "default branch should be reset" + ); + is( + Services.prefs.getCharPref("test.pref"), + "user value", + "user branch should remain the same" + ); + + // Cleanup + Services.prefs.deleteBranch("test.pref"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// Test that a rollouts in the graduation set can't be rolled back +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock({ + graduationSet: new Set(["graduated-rollout"]), + }), + async function cant_rollback_graduation_set({ sendEventSpy }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref", 1); + + let recipe = { id: 1, arguments: { rolloutSlug: "graduated-rollout" } }; + + const action = new PreferenceRollbackAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is(Services.prefs.getIntPref("test.pref"), 1, "pref should not change"); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref"), + Services.prefs.PREF_INVALID, + "no startup pref should be added" + ); + + // No entry in the DB + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [], + "Rollout should be in the db" + ); + + sendEventSpy.assertEvents([ + [ + "unenrollFailed", + "preference_rollback", + "graduated-rollout", + { + reason: "in-graduation-set", + }, + ], + ]); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_PreferenceRolloutAction.js b/toolkit/components/normandy/test/browser/browser_actions_PreferenceRolloutAction.js new file mode 100644 index 0000000000..6ae6b02172 --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_PreferenceRolloutAction.js @@ -0,0 +1,700 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { PreferenceRolloutAction } = ChromeUtils.importESModule( + "resource://normandy/actions/PreferenceRolloutAction.sys.mjs" +); +const { PreferenceRollouts } = ChromeUtils.importESModule( + "resource://normandy/lib/PreferenceRollouts.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +// Test that a simple recipe enrolls as expected +decorate_task( + withStub(TelemetryEnvironment, "setExperimentActive"), + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function simple_recipe_enrollment({ + setExperimentActiveStub, + sendEventSpy, + }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [ + { preferenceName: "test.pref1", value: 1 }, + { preferenceName: "test.pref2", value: true }, + { preferenceName: "test.pref3", value: "it works" }, + ], + }, + }; + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + // rollout prefs are set + is( + Services.prefs.getIntPref("test.pref1"), + 1, + "integer pref should be set" + ); + is( + Services.prefs.getBoolPref("test.pref2"), + true, + "boolean pref should be set" + ); + is( + Services.prefs.getCharPref("test.pref3"), + "it works", + "string pref should be set" + ); + + // start up prefs are set + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref1"), + 1, + "integer startup pref should be set" + ); + is( + Services.prefs.getBoolPref("app.normandy.startupRolloutPrefs.test.pref2"), + true, + "boolean startup pref should be set" + ); + is( + Services.prefs.getCharPref("app.normandy.startupRolloutPrefs.test.pref3"), + "it works", + "string startup pref should be set" + ); + + // rollout was stored + let rollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref1", value: 1, previousValue: null }, + { preferenceName: "test.pref2", value: true, previousValue: null }, + { + preferenceName: "test.pref3", + value: "it works", + previousValue: null, + }, + ], + }, + ], + "Rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "preference_rollout", recipe.arguments.slug, {}], + ]); + ok( + setExperimentActiveStub.calledWithExactly("test-rollout", "active", { + type: "normandy-prefrollout", + }), + "a telemetry experiment should be activated" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref1"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref2"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref3"); + } +); + +// Test that an enrollment's values can change, be removed, and be added +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function update_enrollment({ sendEventSpy }) { + // first enrollment + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [ + { preferenceName: "test.pref1", value: 1 }, + { preferenceName: "test.pref2", value: 1 }, + ], + }, + }; + + let action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + const defaultBranch = Services.prefs.getDefaultBranch(""); + is(defaultBranch.getIntPref("test.pref1"), 1, "pref1 should be set"); + is(defaultBranch.getIntPref("test.pref2"), 1, "pref2 should be set"); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref1"), + 1, + "startup pref1 should be set" + ); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref2"), + 1, + "startup pref2 should be set" + ); + + // update existing enrollment + recipe.arguments.preferences = [ + // pref1 is removed + // pref2's value is updated + { preferenceName: "test.pref2", value: 2 }, + // pref3 is added + { preferenceName: "test.pref3", value: 2 }, + ]; + action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + /* Todo because of bug 1502410 and bug 1505941 */ + todo_is( + Services.prefs.getPrefType("test.pref1"), + Services.prefs.PREF_INVALID, + "pref1 should be removed" + ); + is(Services.prefs.getIntPref("test.pref2"), 2, "pref2 should be updated"); + is(Services.prefs.getIntPref("test.pref3"), 2, "pref3 should be added"); + + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref1"), + Services.prefs.PREF_INVALID, + "startup pref1 should be removed" + ); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref2"), + 2, + "startup pref2 should be updated" + ); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref3"), + 2, + "startup pref3 should be added" + ); + + // rollout in the DB has been updated + const rollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref2", value: 2, previousValue: null }, + { preferenceName: "test.pref3", value: 2, previousValue: null }, + ], + }, + ], + "Rollout should be updated in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "preference_rollout", "test-rollout", {}], + [ + "update", + "preference_rollout", + "test-rollout", + { previousState: "active" }, + ], + ]); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref1"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref2"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref3"); + } +); + +// Test that a graduated rollout can be ungraduated +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function ungraduate_enrollment({ sendEventSpy }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref", 1); + await PreferenceRollouts.add({ + slug: "test-rollout", + state: PreferenceRollouts.STATE_GRADUATED, + preferences: [ + { preferenceName: "test.pref", value: 1, previousValue: 1 }, + ], + }); + + let recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: 2 }], + }, + }; + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is(Services.prefs.getIntPref("test.pref"), 2, "pref should be updated"); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref"), + 2, + "startup pref should be set" + ); + + // rollout in the DB has been ungraduated + const rollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref", value: 2, previousValue: 1 }, + ], + }, + ], + "Rollout should be updated in db" + ); + + sendEventSpy.assertEvents([ + [ + "update", + "preference_rollout", + "test-rollout", + { previousState: "graduated" }, + ], + ]); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// Test when recipes conflict, only one is applied +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function conflicting_recipes({ sendEventSpy }) { + // create two recipes that each share a pref and have a unique pref. + const recipe1 = { + id: 1, + arguments: { + slug: "test-rollout-1", + preferences: [ + { preferenceName: "test.pref1", value: 1 }, + { preferenceName: "test.pref2", value: 1 }, + ], + }, + }; + const recipe2 = { + id: 2, + arguments: { + slug: "test-rollout-2", + preferences: [ + { preferenceName: "test.pref1", value: 2 }, + { preferenceName: "test.pref3", value: 2 }, + ], + }, + }; + + // running both in the same session + let action = new PreferenceRolloutAction(); + await action.processRecipe(recipe1, BaseAction.suitability.FILTER_MATCH); + await action.processRecipe(recipe2, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + // running recipe2 in a separate session shouldn't change things + action = new PreferenceRolloutAction(); + await action.processRecipe(recipe2, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is( + Services.prefs.getIntPref("test.pref1"), + 1, + "pref1 is set to recipe1's value" + ); + is( + Services.prefs.getIntPref("test.pref2"), + 1, + "pref2 is set to recipe1's value" + ); + is( + Services.prefs.getPrefType("test.pref3"), + Services.prefs.PREF_INVALID, + "pref3 is not set" + ); + + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref1"), + 1, + "startup pref1 is set to recipe1's value" + ); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref2"), + 1, + "startup pref2 is set to recipe1's value" + ); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref3"), + Services.prefs.PREF_INVALID, + "startup pref3 is not set" + ); + + // only successful rollout was stored + const rollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout-1", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref1", value: 1, previousValue: null }, + { preferenceName: "test.pref2", value: 1, previousValue: null }, + ], + }, + ], + "Only recipe1's rollout should be stored in db" + ); + + sendEventSpy.assertEvents([ + ["enroll", "preference_rollout", recipe1.arguments.slug], + [ + "enrollFailed", + "preference_rollout", + recipe2.arguments.slug, + { reason: "conflict", preference: "test.pref1" }, + ], + [ + "enrollFailed", + "preference_rollout", + recipe2.arguments.slug, + { reason: "conflict", preference: "test.pref1" }, + ], + ]); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref1"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref2"); + Services.prefs.getDefaultBranch("").deleteBranch("test.pref3"); + } +); + +// Test when the wrong value type is given, the recipe is not applied +decorate_task( + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function wrong_preference_value({ sendEventSpy }) { + Services.prefs.getDefaultBranch("").setCharPref("test.pref", "not an int"); + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: 1 }], + }, + }; + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is( + Services.prefs.getCharPref("test.pref"), + "not an int", + "the pref should not be modified" + ); + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref"), + Services.prefs.PREF_INVALID, + "startup pref is not set" + ); + + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [], + "no rollout is stored in the db" + ); + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "preference_rollout", + recipe.arguments.slug, + { reason: "invalid type", preference: "test.pref" }, + ], + ]); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// Test that even when applying a rollout, user prefs are preserved +decorate_task( + PreferenceRollouts.withTestMock(), + async function preserves_user_prefs() { + Services.prefs + .getDefaultBranch("") + .setCharPref("test.pref", "builtin value"); + Services.prefs.setCharPref("test.pref", "user value"); + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: "rollout value" }], + }, + }; + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is( + Services.prefs.getCharPref("test.pref"), + "user value", + "user branch value should be preserved" + ); + is( + Services.prefs.getDefaultBranch("").getCharPref("test.pref"), + "rollout value", + "default branch value should change" + ); + + const rollouts = await PreferenceRollouts.getAll(); + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { + preferenceName: "test.pref", + value: "rollout value", + previousValue: "builtin value", + }, + ], + }, + ], + "the rollout is added to the db with the correct previous value" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + Services.prefs.deleteBranch("test.pref"); + } +); + +// Enrollment works for prefs with only a user branch value, and no default value. +decorate_task( + PreferenceRollouts.withTestMock(), + async function simple_recipe_enrollment() { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: 1 }], + }, + }; + + // Set a pref on the user branch only + Services.prefs.setIntPref("test.pref", 2); + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is( + Services.prefs.getIntPref("test.pref"), + 2, + "original user branch value still visible" + ); + is( + Services.prefs.getDefaultBranch("").getIntPref("test.pref"), + 1, + "default branch was set" + ); + is( + Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref"), + 1, + "startup pref is est" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// When running a rollout a second time on a pref that doesn't have an existing +// value, the previous value is handled correctly. +decorate_task( + PreferenceRollouts.withTestMock(), + withSendEventSpy(), + async function ({ sendEventSpy }) { + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: 1 }], + }, + }; + + // run once + let action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + // run a second time + action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + const rollouts = await PreferenceRollouts.getAll(); + + Assert.deepEqual( + rollouts, + [ + { + slug: "test-rollout", + state: PreferenceRollouts.STATE_ACTIVE, + preferences: [ + { preferenceName: "test.pref", value: 1, previousValue: null }, + ], + }, + ], + "the DB should have the correct value stored for previousValue" + ); + + sendEventSpy.assertEvents([ + ["enroll", "preference_rollout", "test-rollout", {}], + ]); + } +); + +// New rollouts that are no-ops should send errors +decorate_task( + withStub(TelemetryEnvironment, "setExperimentActive"), + withSendEventSpy(), + PreferenceRollouts.withTestMock(), + async function no_op_new_recipe({ setExperimentActiveStub, sendEventSpy }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref", 1); + + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: 1 }], + }, + }; + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is(Services.prefs.getIntPref("test.pref"), 1, "pref should not change"); + + // start up pref isn't set + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref"), + Services.prefs.PREF_INVALID, + "startup pref1 should not be set" + ); + + // rollout was not stored + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [], + "Rollout should not be stored in db" + ); + + sendEventSpy.assertEvents([ + [ + "enrollFailed", + "preference_rollout", + recipe.arguments.slug, + { reason: "would-be-no-op" }, + ], + ]); + Assert.deepEqual( + setExperimentActiveStub.args, + [], + "a telemetry experiment should not be activated" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); + +// New rollouts in the graduation set should silently do nothing +decorate_task( + withStub(TelemetryEnvironment, "setExperimentActive"), + withSendEventSpy(), + PreferenceRollouts.withTestMock({ graduationSet: new Set(["test-rollout"]) }), + async function graduationSetNewRecipe({ + setExperimentActiveStub, + sendEventSpy, + }) { + Services.prefs.getDefaultBranch("").setIntPref("test.pref", 1); + + const recipe = { + id: 1, + arguments: { + slug: "test-rollout", + preferences: [{ preferenceName: "test.pref", value: 1 }], + }, + }; + + const action = new PreferenceRolloutAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is(action.lastError, null, "lastError should be null"); + + is(Services.prefs.getIntPref("test.pref"), 1, "pref should not change"); + + // start up pref isn't set + is( + Services.prefs.getPrefType("app.normandy.startupRolloutPrefs.test.pref"), + Services.prefs.PREF_INVALID, + "startup pref1 should not be set" + ); + + // rollout was not stored + Assert.deepEqual( + await PreferenceRollouts.getAll(), + [], + "Rollout should not be stored in db" + ); + + sendEventSpy.assertEvents([]); + Assert.deepEqual( + setExperimentActiveStub.args, + [], + "a telemetry experiment should not be activated" + ); + + // Cleanup + Services.prefs.getDefaultBranch("").deleteBranch("test.pref"); + } +); diff --git a/toolkit/components/normandy/test/browser/browser_actions_ShowHeartbeatAction.js b/toolkit/components/normandy/test/browser/browser_actions_ShowHeartbeatAction.js new file mode 100644 index 0000000000..393f31b5ae --- /dev/null +++ b/toolkit/components/normandy/test/browser/browser_actions_ShowHeartbeatAction.js @@ -0,0 +1,377 @@ +"use strict"; + +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { ClientEnvironment } = ChromeUtils.importESModule( + "resource://normandy/lib/ClientEnvironment.sys.mjs" +); +const { Heartbeat } = ChromeUtils.importESModule( + "resource://normandy/lib/Heartbeat.sys.mjs" +); + +const { Uptake } = ChromeUtils.importESModule( + "resource://normandy/lib/Uptake.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +const HOUR_IN_MS = 60 * 60 * 1000; + +function heartbeatRecipeFactory(overrides = {}) { + const defaults = { + revision_id: 1, + name: "Test Recipe", + action: "show-heartbeat", + arguments: { + surveyId: "a survey", + message: "test message", + engagementButtonLabel: "", + thanksMessage: "thanks!", + postAnswerUrl: "http://example.com", + learnMoreMessage: "Learn More", + learnMoreUrl: "http://example.com", + repeatOption: "once", + }, + }; + + if (overrides.arguments) { + defaults.arguments = Object.assign(defaults.arguments, overrides.arguments); + delete overrides.arguments; + } + + return recipeFactory(Object.assign(defaults, overrides)); +} + +// Test that a normal heartbeat works as expected +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testHappyPath({ heartbeatClassStub, heartbeatInstanceStub }) { + const recipe = heartbeatRecipeFactory(); + const action = new ShowHeartbeatAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + await action.finalize(); + is( + action.state, + ShowHeartbeatAction.STATE_FINALIZED, + "Action should be finalized" + ); + is(action.lastError, null, "No errors should have been thrown"); + + const options = heartbeatClassStub.args[0][1]; + Assert.deepEqual( + heartbeatClassStub.args, + [ + [ + heartbeatClassStub.args[0][0], // target window + { + surveyId: options.surveyId, + message: recipe.arguments.message, + engagementButtonLabel: recipe.arguments.engagementButtonLabel, + thanksMessage: recipe.arguments.thanksMessage, + learnMoreMessage: recipe.arguments.learnMoreMessage, + learnMoreUrl: recipe.arguments.learnMoreUrl, + postAnswerUrl: options.postAnswerUrl, + flowId: options.flowId, + surveyVersion: recipe.revision_id, + }, + ], + ], + "expected arguments were passed" + ); + + ok(NormandyTestUtils.isUuid(options.flowId, "flowId should be a uuid")); + + // postAnswerUrl gains several query string parameters. Check that the prefix is right + ok(options.postAnswerUrl.startsWith(recipe.arguments.postAnswerUrl)); + + ok( + heartbeatInstanceStub.eventEmitter.once.calledWith("Voted"), + "Voted event handler should be registered" + ); + ok( + heartbeatInstanceStub.eventEmitter.once.calledWith("Engaged"), + "Engaged event handler should be registered" + ); + } +); + +/* Test that heartbeat doesn't show if an unrelated heartbeat has shown recently. */ +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testRepeatGeneral({ heartbeatClassStub }) { + const allHeartbeatStorage = new Storage("normandy-heartbeat"); + await allHeartbeatStorage.setItem("lastShown", Date.now()); + const recipe = heartbeatRecipeFactory(); + + const action = new ShowHeartbeatAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + + is( + heartbeatClassStub.args.length, + 0, + "Heartbeat should not be called once" + ); + } +); + +/* Test that a heartbeat shows if an unrelated heartbeat showed more than 24 hours ago. */ +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testRepeatUnrelated({ heartbeatClassStub }) { + const allHeartbeatStorage = new Storage("normandy-heartbeat"); + await allHeartbeatStorage.setItem( + "lastShown", + Date.now() - 25 * HOUR_IN_MS + ); + const recipe = heartbeatRecipeFactory(); + + const action = new ShowHeartbeatAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + + is(heartbeatClassStub.args.length, 1, "Heartbeat should be called once"); + } +); + +/* Test that a repeat=once recipe is not shown again, even more than 24 hours ago. */ +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testRepeatTypeOnce({ heartbeatClassStub }) { + const recipe = heartbeatRecipeFactory({ + arguments: { repeatOption: "once" }, + }); + const recipeStorage = new Storage(recipe.id); + await recipeStorage.setItem("lastShown", Date.now() - 25 * HOUR_IN_MS); + + const action = new ShowHeartbeatAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + + is(heartbeatClassStub.args.length, 0, "Heartbeat should not be called"); + } +); + +/* Test that a repeat=xdays recipe is shown again, only after the expected number of days. */ +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testRepeatTypeXdays({ heartbeatClassStub }) { + const recipe = heartbeatRecipeFactory({ + arguments: { + repeatOption: "xdays", + repeatEvery: 2, + }, + }); + const recipeStorage = new Storage(recipe.id); + const allHeartbeatStorage = new Storage("normandy-heartbeat"); + + await recipeStorage.setItem("lastShown", Date.now() - 25 * HOUR_IN_MS); + await allHeartbeatStorage.setItem( + "lastShown", + Date.now() - 25 * HOUR_IN_MS + ); + const action = new ShowHeartbeatAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + is(heartbeatClassStub.args.length, 0, "Heartbeat should not be called"); + + await recipeStorage.setItem("lastShown", Date.now() - 50 * HOUR_IN_MS); + await allHeartbeatStorage.setItem( + "lastShown", + Date.now() - 50 * HOUR_IN_MS + ); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + is( + heartbeatClassStub.args.length, + 1, + "Heartbeat should have been called once" + ); + } +); + +/* Test that a repeat=nag recipe is shown again until lastInteraction is set */ +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testRepeatTypeNag({ heartbeatClassStub }) { + const recipe = heartbeatRecipeFactory({ + arguments: { repeatOption: "nag" }, + }); + const recipeStorage = new Storage(recipe.id); + const allHeartbeatStorage = new Storage("normandy-heartbeat"); + + await allHeartbeatStorage.setItem( + "lastShown", + Date.now() - 25 * HOUR_IN_MS + ); + await recipeStorage.setItem("lastShown", Date.now() - 25 * HOUR_IN_MS); + const action = new ShowHeartbeatAction(); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + is(heartbeatClassStub.args.length, 1, "Heartbeat should be called"); + + await allHeartbeatStorage.setItem( + "lastShown", + Date.now() - 50 * HOUR_IN_MS + ); + await recipeStorage.setItem("lastShown", Date.now() - 50 * HOUR_IN_MS); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + is(heartbeatClassStub.args.length, 2, "Heartbeat should be called again"); + + await allHeartbeatStorage.setItem( + "lastShown", + Date.now() - 75 * HOUR_IN_MS + ); + await recipeStorage.setItem("lastShown", Date.now() - 75 * HOUR_IN_MS); + await recipeStorage.setItem( + "lastInteraction", + Date.now() - 50 * HOUR_IN_MS + ); + await action.processRecipe(recipe, BaseAction.suitability.FILTER_MATCH); + is(action.lastError, null, "No errors should have been thrown"); + is( + heartbeatClassStub.args.length, + 2, + "Heartbeat should not be called again" + ); + } +); + +/* generatePostAnswerURL shouldn't annotate empty strings */ +add_task(async function postAnswerEmptyString() { + const recipe = heartbeatRecipeFactory({ arguments: { postAnswerUrl: "" } }); + const action = new ShowHeartbeatAction(); + is( + await action.generatePostAnswerURL(recipe), + "", + "an empty string should not be annotated" + ); +}); + +/* generatePostAnswerURL should include the right details */ +add_task(async function postAnswerUrl() { + const recipe = heartbeatRecipeFactory({ + arguments: { + postAnswerUrl: "https://example.com/survey?survey_id=42", + includeTelemetryUUID: false, + message: "Hello, World!", + }, + }); + const action = new ShowHeartbeatAction(); + const url = new URL(await action.generatePostAnswerURL(recipe)); + + is( + url.searchParams.get("survey_id"), + "42", + "Pre-existing search parameters should be preserved" + ); + is( + url.searchParams.get("fxVersion"), + Services.appinfo.version, + "Firefox version should be included" + ); + is( + url.searchParams.get("surveyversion"), + Services.appinfo.version, + "Survey version should also be the Firefox version" + ); + ok( + ["0", "1"].includes(url.searchParams.get("syncSetup")), + `syncSetup should be 0 or 1, got ${url.searchParams.get("syncSetup")}` + ); + is( + url.searchParams.get("updateChannel"), + UpdateUtils.getUpdateChannel("false"), + "Update channel should be included" + ); + ok(!url.searchParams.has("userId"), "no user id should be included"); + is( + url.searchParams.get("utm_campaign"), + "Hello%2CWorld!", + "utm_campaign should be an encoded version of the message" + ); + is( + url.searchParams.get("utm_medium"), + "show-heartbeat", + "utm_medium should be the action name" + ); + is( + url.searchParams.get("utm_source"), + "firefox", + "utm_source should be firefox" + ); +}); + +/* generatePostAnswerURL shouldn't override existing values in the url */ +add_task(async function postAnswerUrlNoOverwite() { + const recipe = heartbeatRecipeFactory({ + arguments: { + postAnswerUrl: + "https://example.com/survey?utm_source=shady_tims_firey_fox", + }, + }); + const action = new ShowHeartbeatAction(); + const url = new URL(await action.generatePostAnswerURL(recipe)); + is( + url.searchParams.get("utm_source"), + "shady_tims_firey_fox", + "utm_source should not be overwritten" + ); +}); + +/* generatePostAnswerURL should only include userId if requested */ +add_task(async function postAnswerUrlUserIdIfRequested() { + const recipeWithId = heartbeatRecipeFactory({ + arguments: { includeTelemetryUUID: true }, + }); + const recipeWithoutId = heartbeatRecipeFactory({ + arguments: { includeTelemetryUUID: false }, + }); + const action = new ShowHeartbeatAction(); + + const urlWithId = new URL(await action.generatePostAnswerURL(recipeWithId)); + is( + urlWithId.searchParams.get("userId"), + ClientEnvironment.userId, + "clientId should be included" + ); + + const urlWithoutId = new URL( + await action.generatePostAnswerURL(recipeWithoutId) + ); + ok(!urlWithoutId.searchParams.has("userId"), "userId should not be included"); +}); + +/* generateSurveyId should include userId only if requested */ +decorate_task( + withStubbedHeartbeat(), + withClearStorage(), + async function testGenerateSurveyId() { + const recipeWithoutId = heartbeatRecipeFactory({ + arguments: { surveyId: "test-id", includeTelemetryUUID: false }, + }); + const recipeWithId = heartbeatRecipeFactory({ + arguments: { surveyId: "test-id", includeTelemetryUUID: true }, + }); + const action = new ShowHeartbeatAction(); + is( + action.generateSurveyId(recipeWithoutId), + "test-id", + "userId should not be included if not requested" + ); + is( + action.generateSurveyId(recipeWithId), + `test-id::${ClientEnvironment.userId}`, + "userId should be included if requested" + ); + } +); diff --git a/toolkit/components/normandy/test/browser/head.js b/toolkit/components/normandy/test/browser/head.js new file mode 100644 index 0000000000..354c38647e --- /dev/null +++ b/toolkit/components/normandy/test/browser/head.js @@ -0,0 +1,642 @@ +const { Preferences } = ChromeUtils.importESModule( + "resource://gre/modules/Preferences.sys.mjs" +); +const { AddonTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/AddonTestUtils.sys.mjs" +); +const { AboutPages } = ChromeUtils.importESModule( + "resource://normandy-content/AboutPages.sys.mjs" +); +const { AddonStudies } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonStudies.sys.mjs" +); +const { NormandyApi } = ChromeUtils.importESModule( + "resource://normandy/lib/NormandyApi.sys.mjs" +); +const { TelemetryEvents } = ChromeUtils.importESModule( + "resource://normandy/lib/TelemetryEvents.sys.mjs" +); +const { ShowHeartbeatAction } = ChromeUtils.importESModule( + "resource://normandy/actions/ShowHeartbeatAction.sys.mjs" +); + +// The name of this module conflicts with the window.Storage +// DOM global - https://developer.mozilla.org/en-US/docs/Web/API/Storage . +// eslint-disable-next-line mozilla/no-redeclare-with-import-autofix +const { Storage } = ChromeUtils.importESModule( + "resource://normandy/lib/Storage.sys.mjs" +); + +ChromeUtils.defineESModuleGetters(this, { + TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs", +}); + +const CryptoHash = Components.Constructor( + "@mozilla.org/security/hash;1", + "nsICryptoHash", + "initWithString" +); +const FileInputStream = Components.Constructor( + "@mozilla.org/network/file-input-stream;1", + "nsIFileInputStream", + "init" +); + +const { sinon } = ChromeUtils.importESModule( + "resource://testing-common/Sinon.sys.mjs" +); + +// Make sinon assertions fail in a way that mochitest understands +sinon.assert.fail = function (message) { + ok(false, message); +}; + +// Prep Telemetry to receive events from tests +TelemetryEvents.init(); + +this.TEST_XPI_URL = (function () { + const dir = getChromeDir(getResolvedURI(gTestPath)); + dir.append("addons"); + dir.append("normandydriver-a-1.0.xpi"); + return Services.io.newFileURI(dir).spec; +})(); + +this.withWebExtension = function ( + manifestOverrides = {}, + { as = "webExtension" } = {} +) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const random = Math.random().toString(36).replace(/0./, "").substr(-3); + let addonId = `normandydriver_${random}@example.com`; + if ("id" in manifestOverrides) { + addonId = manifestOverrides.id; + delete manifestOverrides.id; + } + + const manifest = Object.assign( + { + manifest_version: 2, + name: "normandy_fixture", + version: "1.0", + description: "Dummy test fixture that's a webextension", + browser_specific_settings: { + gecko: { id: addonId }, + }, + }, + manifestOverrides + ); + + const addonFile = AddonTestUtils.createTempWebExtensionFile({ manifest }); + + // Workaround: Add-on files are cached by URL, and + // createTempWebExtensionFile re-uses filenames if the previous file has + // been deleted. So we need to flush the cache to avoid it. + Services.obs.notifyObservers(addonFile, "flush-cache-entry"); + + try { + await testFunction({ ...args, [as]: { addonId, addonFile } }); + } finally { + AddonTestUtils.cleanupTempXPIs(); + } + }; + }; +}; + +this.withCorruptedWebExtension = function (options) { + // This should be an invalid manifest version, so that installing this add-on fails. + return this.withWebExtension({ manifest_version: -1 }, options); +}; + +this.withInstalledWebExtension = function ( + manifestOverrides = {}, + { expectUninstall = false, as = "installedWebExtension" } = {} +) { + return function wrapper(testFunction) { + return decorate( + withWebExtension(manifestOverrides, { as }), + async function wrappedTestFunction(args) { + const { addonId, addonFile } = args[as]; + const startupPromise = + AddonTestUtils.promiseWebExtensionStartup(addonId); + const addonInstall = await AddonManager.getInstallForFile( + addonFile, + "application/x-xpinstall" + ); + await addonInstall.install(); + await startupPromise; + + try { + await testFunction(args); + } finally { + const addonToUninstall = await AddonManager.getAddonByID(addonId); + if (addonToUninstall) { + await addonToUninstall.uninstall(); + } else { + ok( + expectUninstall, + "Add-on should not be unexpectedly uninstalled during test" + ); + } + } + } + ); + }; +}; + +this.withMockNormandyApi = function () { + return function (testFunction) { + return async function inner(args) { + const mockNormandyApi = { + actions: [], + recipes: [], + implementations: {}, + extensionDetails: {}, + }; + + // Use callsFake instead of resolves so that the current values in mockApi are used. + mockNormandyApi.fetchExtensionDetails = sinon + .stub(NormandyApi, "fetchExtensionDetails") + .callsFake(async extensionId => { + const details = mockNormandyApi.extensionDetails[extensionId]; + if (!details) { + throw new Error(`Missing extension details for ${extensionId}`); + } + return details; + }); + + try { + await testFunction({ ...args, mockNormandyApi }); + } finally { + mockNormandyApi.fetchExtensionDetails.restore(); + } + }; + }; +}; + +const preferenceBranches = { + user: Preferences, + default: new Preferences({ defaultBranch: true }), +}; + +this.withMockPreferences = function () { + return function (testFunction) { + return async function inner(args) { + const mockPreferences = new MockPreferences(); + try { + await testFunction({ ...args, mockPreferences }); + } finally { + mockPreferences.cleanup(); + } + }; + }; +}; + +class MockPreferences { + constructor() { + this.oldValues = { user: {}, default: {} }; + } + + set(name, value, branch = "user") { + this.preserve(name, branch); + preferenceBranches[branch].set(name, value); + } + + preserve(name, branch) { + if (branch !== "user" && branch !== "default") { + throw new Error(`Unexpected branch ${branch}`); + } + if (!(name in this.oldValues[branch])) { + const preferenceBranch = preferenceBranches[branch]; + let oldValue; + let existed; + try { + oldValue = preferenceBranch.get(name); + existed = preferenceBranch.has(name); + } catch (e) { + oldValue = null; + existed = false; + } + this.oldValues[branch][name] = { oldValue, existed }; + } + } + + cleanup() { + for (const [branchName, values] of Object.entries(this.oldValues)) { + const preferenceBranch = preferenceBranches[branchName]; + for (const [name, { oldValue, existed }] of Object.entries(values)) { + const before = preferenceBranch.get(name); + + if (before === oldValue) { + continue; + } + + if (existed) { + preferenceBranch.set(name, oldValue); + } else if (branchName === "default") { + Services.prefs.getDefaultBranch(name).deleteBranch(""); + } else { + preferenceBranch.reset(name); + } + + const after = preferenceBranch.get(name); + if (before === after && before !== undefined) { + throw new Error( + `Couldn't reset pref "${name}" to "${oldValue}" on "${branchName}" branch ` + + `(value stayed "${before}", did ${existed ? "" : "not "}exist)` + ); + } + } + } + } +} + +this.withPrefEnv = function (inPrefs) { + return function wrapper(testFunc) { + return async function inner(args) { + await SpecialPowers.pushPrefEnv(inPrefs); + try { + await testFunc(args); + } finally { + await SpecialPowers.popPrefEnv(); + } + }; + }; +}; + +this.withStudiesEnabled = function () { + return function (testFunc) { + return async function inner(args) { + await SpecialPowers.pushPrefEnv({ + set: [["app.shield.optoutstudies.enabled", true]], + }); + try { + await testFunc(args); + } finally { + await SpecialPowers.popPrefEnv(); + } + }; + }; +}; + +/** + * Combine a list of functions right to left. The rightmost function is passed + * to the preceding function as the argument; the result of this is passed to + * the next function until all are exhausted. For example, this: + * + * decorate(func1, func2, func3); + * + * is equivalent to this: + * + * func1(func2(func3)); + */ +this.decorate = function (...args) { + const funcs = Array.from(args); + let decorated = funcs.pop(); + const origName = decorated.name; + funcs.reverse(); + for (const func of funcs) { + decorated = func(decorated); + } + Object.defineProperty(decorated, "name", { value: origName }); + return decorated; +}; + +/** + * Wrapper around add_task for declaring tests that use several with-style + * wrappers. The last argument should be your test function; all other arguments + * should be functions that accept a single test function argument. + * + * The arguments are combined using decorate and passed to add_task as a single + * test function. + * + * @param {[Function]} args + * @example + * decorate_task( + * withMockPreferences(), + * withMockNormandyApi(), + * async function myTest(mockPreferences, mockApi) { + * // Do a test + * } + * ); + */ +this.decorate_task = function (...args) { + return add_task(decorate(...args)); +}; + +this.withStub = function ( + object, + method, + { returnValue, as = `${method}Stub` } = {} +) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const stub = sinon.stub(object, method); + stub.returnValue = returnValue; + try { + await testFunction({ ...args, [as]: stub }); + } finally { + stub.restore(); + } + }; + }; +}; + +this.withSpy = function (object, method, { as = `${method}Spy` } = {}) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + const spy = sinon.spy(object, method); + try { + await testFunction({ ...args, [as]: spy }); + } finally { + spy.restore(); + } + }; + }; +}; + +this.studyEndObserved = function (recipeId) { + return TestUtils.topicObserved( + "shield-study-ended", + (subject, endedRecipeId) => Number.parseInt(endedRecipeId) === recipeId + ); +}; + +this.withSendEventSpy = function () { + return function (testFunction) { + return async function wrappedTestFunction(args) { + const sendEventSpy = sinon.spy(TelemetryEvents, "sendEvent"); + sendEventSpy.assertEvents = expected => { + expected = expected.map(event => ["normandy"].concat(event)); + TelemetryTestUtils.assertEvents( + expected, + { category: "normandy" }, + { clear: false } + ); + }; + Services.telemetry.clearEvents(); + try { + await testFunction({ ...args, sendEventSpy }); + } finally { + sendEventSpy.restore(); + Assert.ok(!sendEventSpy.threw(), "Telemetry events should not fail"); + } + }; + }; +}; + +let _recipeId = 1; +this.recipeFactory = function (overrides = {}) { + return Object.assign( + { + id: _recipeId++, + arguments: overrides.arguments || {}, + }, + overrides + ); +}; + +function mockLogger() { + const logStub = sinon.stub(); + logStub.fatal = sinon.stub(); + logStub.error = sinon.stub(); + logStub.warn = sinon.stub(); + logStub.info = sinon.stub(); + logStub.config = sinon.stub(); + logStub.debug = sinon.stub(); + logStub.trace = sinon.stub(); + return logStub; +} + +this.CryptoUtils = { + _getHashStringForCrypto(aCrypto) { + // return the two-digit hexadecimal code for a byte + let toHexString = charCode => ("0" + charCode.toString(16)).slice(-2); + + // convert the binary hash data to a hex string. + let binary = aCrypto.finish(false); + let hash = Array.from(binary, c => toHexString(c.charCodeAt(0))); + return hash.join("").toLowerCase(); + }, + + /** + * Get the computed hash for a given file + * @param {nsIFile} file The file to be hashed + * @param {string} [algorithm] The hashing algorithm to use + */ + getFileHash(file, algorithm = "sha256") { + const crypto = CryptoHash(algorithm); + const fis = new FileInputStream(file, -1, -1, false); + crypto.updateFromStream(fis, file.fileSize); + const hash = this._getHashStringForCrypto(crypto); + fis.close(); + return hash; + }, +}; + +const FIXTURE_ADDON_ID = "normandydriver-a@example.com"; +const FIXTURE_ADDON_BASE_URL = + getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "http://example.com" + ) + "/addons/"; + +const FIXTURE_ADDONS = [ + "normandydriver-a-1.0", + "normandydriver-b-1.0", + "normandydriver-a-2.0", +]; + +// Generate fixture add-on details +this.FIXTURE_ADDON_DETAILS = {}; +FIXTURE_ADDONS.forEach(addon => { + const filename = `${addon}.xpi`; + const dir = getChromeDir(getResolvedURI(gTestPath)); + dir.append("addons"); + dir.append(filename); + const xpiFile = Services.io + .newFileURI(dir) + .QueryInterface(Ci.nsIFileURL).file; + + FIXTURE_ADDON_DETAILS[addon] = { + url: `${FIXTURE_ADDON_BASE_URL}${filename}`, + hash: CryptoUtils.getFileHash(xpiFile, "sha256"), + }; +}); + +this.extensionDetailsFactory = function (overrides = {}) { + return Object.assign( + { + id: 1, + name: "Normandy Fixture", + xpi: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].url, + extension_id: FIXTURE_ADDON_ID, + version: "1.0", + hash: FIXTURE_ADDON_DETAILS["normandydriver-a-1.0"].hash, + hash_algorithm: "sha256", + }, + overrides + ); +}; + +/** + * Utility function to uninstall addons safely. Preventing the issue mentioned + * in bug 1485569. + * + * addon.uninstall is async, but it also triggers the AddonStudies onUninstall + * listener, which is not awaited. Wrap it here and trigger a promise once it's + * done so we can wait until AddonStudies cleanup is finished. + */ +this.safeUninstallAddon = async function (addon) { + const activeStudies = (await AddonStudies.getAll()).filter( + study => study.active + ); + const matchingStudy = activeStudies.find(study => study.addonId === addon.id); + + let studyEndedPromise; + if (matchingStudy) { + studyEndedPromise = TestUtils.topicObserved( + "shield-study-ended", + (subject, message) => { + return message === `${matchingStudy.recipeId}`; + } + ); + } + + const addonUninstallPromise = addon.uninstall(); + + return Promise.all([studyEndedPromise, addonUninstallPromise]); +}; + +/** + * Test decorator that is a modified version of the withInstalledWebExtension + * decorator that safely uninstalls the created addon. + */ +this.withInstalledWebExtensionSafe = function ( + manifestOverrides = {}, + { as = "installedWebExtensionSafe" } = {} +) { + return testFunction => { + return async function wrappedTestFunction(args) { + const decorated = withInstalledWebExtension(manifestOverrides, { + expectUninstall: true, + as, + })(async ({ [as]: { addonId, addonFile } }) => { + try { + await testFunction({ ...args, [as]: { addonId, addonFile } }); + } finally { + let addon = await AddonManager.getAddonByID(addonId); + if (addon) { + await safeUninstallAddon(addon); + addon = await AddonManager.getAddonByID(addonId); + ok(!addon, "add-on should be uninstalled"); + } + } + }); + await decorated(); + }; + }; +}; + +/** + * Test decorator to provide a web extension installed from a URL. + */ +this.withInstalledWebExtensionFromURL = function ( + url, + { as = "installedWebExtension" } = {} +) { + return function wrapper(testFunction) { + return async function wrappedTestFunction(args) { + let startupPromise; + let addonId; + + const install = await AddonManager.getInstallForURL(url); + const listener = { + onInstallStarted(cbInstall) { + addonId = cbInstall.addon.id; + startupPromise = AddonTestUtils.promiseWebExtensionStartup(addonId); + }, + }; + install.addListener(listener); + + await install.install(); + await startupPromise; + + try { + await testFunction({ ...args, [as]: { addonId, url } }); + } finally { + const addonToUninstall = await AddonManager.getAddonByID(addonId); + await safeUninstallAddon(addonToUninstall); + } + }; + }; +}; + +/** + * Test decorator that checks that the test cleans up all add-ons installed + * during the test. Likely needs to be the first decorator used. + */ +this.ensureAddonCleanup = function () { + return function (testFunction) { + return async function wrappedTestFunction(args) { + const beforeAddons = new Set(await AddonManager.getAllAddons()); + + try { + await testFunction(args); + } finally { + const afterAddons = new Set(await AddonManager.getAllAddons()); + Assert.deepEqual( + beforeAddons, + afterAddons, + "The add-ons should be same before and after the test" + ); + } + }; + }; +}; + +class MockHeartbeat { + constructor() { + this.eventEmitter = new MockEventEmitter(); + } +} + +class MockEventEmitter { + constructor() { + this.once = sinon.stub(); + } +} + +function withStubbedHeartbeat() { + return function (testFunction) { + return async function wrappedTestFunction(args) { + const heartbeatInstanceStub = new MockHeartbeat(); + const heartbeatClassStub = sinon.stub(); + heartbeatClassStub.returns(heartbeatInstanceStub); + ShowHeartbeatAction.overrideHeartbeatForTests(heartbeatClassStub); + + try { + await testFunction({ + ...args, + heartbeatClassStub, + heartbeatInstanceStub, + }); + } finally { + ShowHeartbeatAction.overrideHeartbeatForTests(); + } + }; + }; +} + +function withClearStorage() { + return function (testFunction) { + return async function wrappedTestFunction(args) { + Storage.clearAllStorage(); + try { + await testFunction(args); + } finally { + Storage.clearAllStorage(); + } + }; + }; +} diff --git a/toolkit/components/normandy/test/browser/moz.build b/toolkit/components/normandy/test/browser/moz.build new file mode 100644 index 0000000000..5397cf859a --- /dev/null +++ b/toolkit/components/normandy/test/browser/moz.build @@ -0,0 +1,27 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +BROWSER_CHROME_MANIFESTS += [ + "browser.toml", +] + +addons = [ + "normandydriver-a-1.0", + "normandydriver-b-1.0", + "normandydriver-a-2.0", +] + +output_dir = ( + OBJDIR_FILES._tests.testing.mochitest.browser.toolkit.components.normandy.test.browser.addons +) + +for addon in addons: + indir = "addons/%s" % addon + xpi = "%s.xpi" % indir + + GeneratedFile(xpi, script="../create_xpi.py", inputs=[indir]) + + output_dir += ["!%s" % xpi] diff --git a/toolkit/components/normandy/test/create_xpi.py b/toolkit/components/normandy/test/create_xpi.py new file mode 100644 index 0000000000..a34f25a2ed --- /dev/null +++ b/toolkit/components/normandy/test/create_xpi.py @@ -0,0 +1,12 @@ +# 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/. + +from os.path import abspath + +from mozbuild.action.zip import main as create_zip + + +def main(output, input_dir): + output.close() + return create_zip(["-C", input_dir, abspath(output.name), "**"]) diff --git a/toolkit/components/normandy/test/unit/cookie_server.sjs b/toolkit/components/normandy/test/unit/cookie_server.sjs new file mode 100644 index 0000000000..ab6099f6a4 --- /dev/null +++ b/toolkit/components/normandy/test/unit/cookie_server.sjs @@ -0,0 +1,12 @@ +/** + * Sends responses that sets a cookie. + */ +function handleRequest(request, response) { + // Allow cross-origin, so you can XHR to it! + response.setHeader("Access-Control-Allow-Origin", "*", false); + // Avoid confusing cache behaviors + response.setHeader("Cache-Control", "no-cache", false); + // Set a cookie + response.setHeader("Set-Cookie", "type=chocolate-chip", false); + response.write(""); +} diff --git a/toolkit/components/normandy/test/unit/echo_server.sjs b/toolkit/components/normandy/test/unit/echo_server.sjs new file mode 100644 index 0000000000..012f2b406e --- /dev/null +++ b/toolkit/components/normandy/test/unit/echo_server.sjs @@ -0,0 +1,21 @@ +/** + * Reads an HTTP status code and response body from the querystring and sends + * back a matching response. + */ +function handleRequest(request, response) { + // Allow cross-origin, so you can XHR to it! + response.setHeader("Access-Control-Allow-Origin", "*", false); + // Avoid confusing cache behaviors + response.setHeader("Cache-Control", "no-cache", false); + + const params = request.queryString.split("&"); + for (const param of params) { + const [key, value] = param.split("="); + if (key === "status") { + response.setStatusLine(null, value); + } else if (key === "body") { + response.write(value); + } + } + response.write(""); +} diff --git a/toolkit/components/normandy/test/unit/head_xpc.js b/toolkit/components/normandy/test/unit/head_xpc.js new file mode 100644 index 0000000000..ad2192be4b --- /dev/null +++ b/toolkit/components/normandy/test/unit/head_xpc.js @@ -0,0 +1,5 @@ +"use strict"; + +var { sinon } = ChromeUtils.importESModule( + "resource://testing-common/Sinon.sys.mjs" +); diff --git a/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/api/v1/index.json b/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/api/v1/index.json new file mode 100644 index 0000000000..5bef8d1302 --- /dev/null +++ b/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/api/v1/index.json @@ -0,0 +1,4 @@ +{ + "recipe-signed": "/api/v1/recipe/signed/", + "classify-client": "/api/v1/classify_client/" +} diff --git a/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/api/v1/recipe/signed/index.json b/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/api/v1/recipe/signed/index.json new file mode 100644 index 0000000000..d5495fa87f --- /dev/null +++ b/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/api/v1/recipe/signed/index.json @@ -0,0 +1,24 @@ +[ + { + "recipe": { + "action": "console-log", + "arguments": { "message": "this signature does not match this recipe" }, + "channels": [], + "countries": [], + "enabled": true, + "extra_filter_expression": "true || true", + "filter_expression": "true || true", + "id": 1, + "last_updated": "2017-02-17T18:29:09.839239Z", + "locales": [], + "name": "system-addon-test", + "revision_id": "b2cb8a26e132182d7d02cf50695d2c7f06cf3b954ff2ff63bca49d724ee91950" + }, + "signature": { + "public_key": "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEVEKiCAIkwRg1VFsP8JOYdSF6a3qvgbRPoEK9eTuLbrB6QixozscKR4iWJ8ZOOX6RPCRgFdfVDoZqjFBFNJN9QtRBk0mVtHbnErx64d2vMF0oWencS1hyLW2whgOgOz7p", + "signature": "p4g3eurmPsJK5UcGT97BRyKstpwZ_2mNJkDGpd6QXlkXfvgwprjeyb5yeIEkKUXqc6krWid4obB_OP9-CwOi9tvKY1pV8p98CT5BhF0IVgpF3b7KBW1a0BVdg5owoG5W", + "timestamp": "2017-02-17T18:29:09.847614Z", + "x5u": "/normandy.content-signature.mozilla.org-20210705.dev.chain" + } + } +] diff --git a/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/normandy.content-signature.mozilla.org-20210705.dev.chain b/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/normandy.content-signature.mozilla.org-20210705.dev.chain new file mode 100644 index 0000000000..5bf53787d8 --- /dev/null +++ b/toolkit/components/normandy/test/unit/invalid_recipe_signature_api/normandy.content-signature.mozilla.org-20210705.dev.chain @@ -0,0 +1,123 @@ +-----BEGIN CERTIFICATE----- +MIIGRTCCBC2gAwIBAgIEAQAABTANBgkqhkiG9w0BAQwFADBrMQswCQYDVQQGEwJV +UzEQMA4GA1UEChMHQWxsaXpvbTEXMBUGA1UECxMOQ2xvdWQgU2VydmljZXMxMTAv +BgNVBAMTKERldnppbGxhIFNpZ25pbmcgU2VydmljZXMgSW50ZXJtZWRpYXRlIDEw +HhcNMTYwNzA2MjE1NzE1WhcNMjEwNzA1MjE1NzE1WjCBrzELMAkGA1UEBhMCVVMx +EzARBgNVBAgTCkNhbGlmb3JuaWExHDAaBgNVBAoTE01vemlsbGEgQ29ycG9yYXRp +b24xFzAVBgNVBAsTDkNsb3VkIFNlcnZpY2VzMS8wLQYDVQQDEyZub3JtYW5keS5j +b250ZW50LXNpZ25hdHVyZS5tb3ppbGxhLm9yZzEjMCEGCSqGSIb3DQEJARYUc2Vj +dXJpdHlAbW96aWxsYS5vcmcwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAARUQqIIAiTB +GDVUWw/wk5h1IXpreq+BtE+gQr15O4tusHpCLGjOxwpHiJYnxk45fpE8JGAV19UO +hmqMUEU0k31C1EGTSZW0ducSvHrh3a8wXShZ6dxLWHItbbCGA6A7PumjggJYMIIC +VDAdBgNVHQ4EFgQUVfksSjlZ0i1TBiS1vcoObaMeXn0wge8GA1UdIwSB5zCB5IAU +/YboUIXAovChEpudDBuodHKbjUuhgcWkgcIwgb8xCzAJBgNVBAYTAlVTMQswCQYD +VQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEmMCQGA1UEChMdQ29udGVu +dCBTaWduYXR1cmUgRGV2IFNpZ25pbmcxJjAkBgNVBAMTHWRldi5jb250ZW50LXNp +Z25hdHVyZS5yb290LmNhMTswOQYJKoZIhvcNAQkBFixjbG91ZHNlYytkZXZyb290 +Y29udGVudHNpZ25hdHVyZUBtb3ppbGxhLmNvbYIEAQAABDAMBgNVHRMBAf8EAjAA +MA4GA1UdDwEB/wQEAwIHgDAWBgNVHSUBAf8EDDAKBggrBgEFBQcDAzBEBgNVHR8E +PTA7MDmgN6A1hjNodHRwczovL2NvbnRlbnQtc2lnbmF0dXJlLmRldi5tb3phd3Mu +bmV0L2NhL2NybC5wZW0wQgYJYIZIAYb4QgEEBDUWM2h0dHBzOi8vY29udGVudC1z +aWduYXR1cmUuZGV2Lm1vemF3cy5uZXQvY2EvY3JsLnBlbTBOBggrBgEFBQcBAQRC +MEAwPgYIKwYBBQUHMAKGMmh0dHBzOi8vY29udGVudC1zaWduYXR1cmUuZGV2Lm1v +emF3cy5uZXQvY2EvY2EucGVtMDEGA1UdEQQqMCiCJm5vcm1hbmR5LmNvbnRlbnQt +c2lnbmF0dXJlLm1vemlsbGEub3JnMA0GCSqGSIb3DQEBDAUAA4ICAQCwb+8JTAB7 +ZfQmFqPUIV2cQQv696AaDPQCtA9YS4zmUfcLMvfZVAbK397zFr0RMDdLiTUQDoeq +rBEmPXhJRPiv6JAK4n7Jf6Y6XfXcNxx+q3garR09Vm/0CnEq/iV+ZAtPkoKIO9kr +Nkzecd894yQCF4hIuPQ5qtMySeqJmH3Dp13eq4T0Oew1Bu32rNHuBJh2xYBkWdun +aAw/YX0I5EqZBP/XA6gbiA160tTK+hnpnlMtw/ljkvfhHbWpICD4aSiTL8L3vABQ +j7bqjMKR5xDkuGWshZfcmonpvQhGTye/RZ1vz5IzA3VOJt1mz5bdZlitpaOm/Yv0 +x6aODz8GP/PiRWFQ5CW8Uf/7pGc5rSyvnfZV2ix8EzFlo8cUtuN1fjrPFPOFOLvG +iiB6S9nlXiKBGYIDdd8V8iC5xJpzjiAWJQigwSNzuc2K30+iPo3w0zazkwe5V8jW +gj6gItYxh5xwVQTPHD0EOd9HvV1ou42+rH5Y+ISFUm25zz02UtUHEK0BKtL0lmdt +DwVq5jcHn6bx2/iwUtlKvPXtfM/6JjTJlkLZLtS7U5/pwcS0owo9zAL0qg3bdm16 ++v/olmPqQFLUHmamJTzv3rojj5X/uVdx1HMM3wBjV9tRYoYaZw9RIInRmM8Z1pHv +JJ+CIZgCyd5vgp57BKiodRZcgHoCH+BkOQ== +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIHijCCBXKgAwIBAgIEAQAABDANBgkqhkiG9w0BAQwFADCBvzELMAkGA1UEBhMC +VVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MSYwJAYDVQQK +Ex1Db250ZW50IFNpZ25hdHVyZSBEZXYgU2lnbmluZzEmMCQGA1UEAxMdZGV2LmNv +bnRlbnQtc2lnbmF0dXJlLnJvb3QuY2ExOzA5BgkqhkiG9w0BCQEWLGNsb3Vkc2Vj +K2RldnJvb3Rjb250ZW50c2lnbmF0dXJlQG1vemlsbGEuY29tMB4XDTE2MDcwNjIx +NDkyNloXDTIxMDcwNTIxNDkyNlowazELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0Fs +bGl6b20xFzAVBgNVBAsTDkNsb3VkIFNlcnZpY2VzMTEwLwYDVQQDEyhEZXZ6aWxs +YSBTaWduaW5nIFNlcnZpY2VzIEludGVybWVkaWF0ZSAxMIICIjANBgkqhkiG9w0B +AQEFAAOCAg8AMIICCgKCAgEAypIfUURYgWzGw8G/1Pz9zW+Tsjirx2owThiv2gys +wJiWL/9/2gzKOrYDEqlDUudfA/BjVRtT9+NbYgnhaCkNfADOAacWS83aMhedAqhP +bVd5YhGQdpijI7f1AVTSb0ehrU2nhOZHvHX5Tk2fbRx3ryefIazNTLFGpiMBbsNv +tSI/+fjW8s0MhKNqlLnk6a9mZKo0mEy7HjGYV8nzsgI17rKLx/s2HG4TFG0+JQzc +UGlum3Tg58ritDzWdyKIkmKNZ48oLBX99Qc8j8B1UyiLv6TZmjVX0I+Ds7eSWHZk +0axLEpTyf2r7fHvN4iDNCPajw+ZpuuBfbs80Ha8b8MHvnpf9fbwiirodNQOVpY4c +t5E3Us3eYwBKdqDEbECWxCKGAS2/iVVUCNKHsg0sSxgqcwxrxyrddQRUQ0EM38DZ +F/vHt+vTdHt07kezbjJe0Kklel59uSpghA0iL4vxzbTns1fuwYOgVrNGs3acTkiB +GhFOxRXUPGtpdYmv+AaR9WlWJQY1GIEoVrinPVH7bcCwyh1CcUbHL+oAFTcmc6kZ +7azNg21tWILIRL7R0IZYQm0tF5TTwCsjVC7FuHaBtkxtVrrZqeKjvVXQ8TK5VoI0 +BUQ6BKHGeTtm+0HBpheYBDy3wkOsEGbGHLEM6cMeiz6PyCXF8wXli8Qb/TjN3LHZ +e30CAwEAAaOCAd8wggHbMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgGG +MBYGA1UdJQEB/wQMMAoGCCsGAQUFBwMDMB0GA1UdDgQWBBT9huhQhcCi8KESm50M +G6h0cpuNSzCB7AYDVR0jBIHkMIHhgBSDx8s0qJaMyQCehKcuzgzVNRA75qGBxaSB +wjCBvzELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFp +biBWaWV3MSYwJAYDVQQKEx1Db250ZW50IFNpZ25hdHVyZSBEZXYgU2lnbmluZzEm +MCQGA1UEAxMdZGV2LmNvbnRlbnQtc2lnbmF0dXJlLnJvb3QuY2ExOzA5BgkqhkiG +9w0BCQEWLGNsb3Vkc2VjK2RldnJvb3Rjb250ZW50c2lnbmF0dXJlQG1vemlsbGEu +Y29tggEBMEIGCWCGSAGG+EIBBAQ1FjNodHRwczovL2NvbnRlbnQtc2lnbmF0dXJl +LmRldi5tb3phd3MubmV0L2NhL2NybC5wZW0wTgYIKwYBBQUHAQEEQjBAMD4GCCsG +AQUFBzAChjJodHRwczovL2NvbnRlbnQtc2lnbmF0dXJlLmRldi5tb3phd3MubmV0 +L2NhL2NhLnBlbTANBgkqhkiG9w0BAQwFAAOCAgEAbum0z0ccqI1Wp49VtsGmUPHA +gjPPy2Xa5NePmqY87WrGdhjN3xbLVb3hx8T2N6pqDjMY2rEynXKEOek3oJkQ3C6J +8AFP6Y93gaAlNz6EA0J0mqdW5TMI8YEYsu2ma+dQQ8wm9f/5b+/Y8qwfhztP06W5 +H6IG04/RvgUwYMnSR4QvT309fu5UmCRUDzsO53ZmQCfmN94u3NxHc4S6n0Q6AKAM +kh5Ld9SQnlqqqDykzn7hYDi8nTLWc7IYqkGfNMilDEKbAl4CjnSfyEvpdFAJ9sPR +UL+kaWFSMvaqIPNpxS5OpoPZjmxEc9HHnCHxtfDHWdXTJILjijPrCdMaxOCHfIqV +5roOJggI4RZ0YM68IL1MfN4IEVOrHhKjDHtd1gcmy2KU4jfj9LQq9YTnyvZ2z1yS +lS310HG3or1K8Nnu5Utfe7T6ppX8bLRMkS1/w0p7DKxHaf4D/GJcCtM9lcSt9JpW +6ACKFikjWR4ZxczYKgApc0wcjd7XBuO5777xtOeyEUDHdDft3jiXA91dYM5UAzc3 +69z/3zmaELzo0gWcrjLXh7fU9AvbU4EUF6rwzxbPGF78jJcGK+oBf8uWUCkBykDt +VsAEZI1u4EDg8e/C1nFqaH9gNMArAgquYIB9rve+hdprIMnva0S147pflWopBWcb +jwzgpfquuYnnxe0CNBA= +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIH3DCCBcSgAwIBAgIBATANBgkqhkiG9w0BAQwFADCBvzELMAkGA1UEBhMCVVMx +CzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MSYwJAYDVQQKEx1D +b250ZW50IFNpZ25hdHVyZSBEZXYgU2lnbmluZzEmMCQGA1UEAxMdZGV2LmNvbnRl +bnQtc2lnbmF0dXJlLnJvb3QuY2ExOzA5BgkqhkiG9w0BCQEWLGNsb3Vkc2VjK2Rl +dnJvb3Rjb250ZW50c2lnbmF0dXJlQG1vemlsbGEuY29tMB4XDTE2MDcwNjE4MTUy +MloXDTI2MDcwNDE4MTUyMlowgb8xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEW +MBQGA1UEBxMNTW91bnRhaW4gVmlldzEmMCQGA1UEChMdQ29udGVudCBTaWduYXR1 +cmUgRGV2IFNpZ25pbmcxJjAkBgNVBAMTHWRldi5jb250ZW50LXNpZ25hdHVyZS5y +b290LmNhMTswOQYJKoZIhvcNAQkBFixjbG91ZHNlYytkZXZyb290Y29udGVudHNp +Z25hdHVyZUBtb3ppbGxhLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC +ggIBAJcPcXhD8MzF6OTn5qZ0L7lX1+PEgLKhI9g1HxxDYDVup4Zm0kZhPGmFSlml +6eVO99OvvHdAlHhQGCIG7h+w1cp66mWjfpcvtQH23uRoKZfiW3jy1jUWrvdXolxR +t1taZosjzo+9OP8TvG6LpJj7AvqUiYD4wYnQJtt0jNRN4d6MUfQwiavSS5uTBuxd +ZJ4TsPvEI+Iv4A4PSobSzxkg79LTMvsGtDLQv7nN5hMs9T18EL5GnIKoqnSQCU0d +n2CN7S3QiQ+cbORWsSYqCTj1gUbFh6X3duEB/ypLcyWFbqeJmPHikjY8q8pLjZSB +IYiTJYLyvYlKdM5QleC/xuBNnMPCftrwwLHjWE4Dd7C9t7k0R5xyOetuiHLCwOcQ +tuckp7RgFKoviMNv3gdkzwVapOklcsaRkRscv6OMTKJNsdJVIDLrPF1wMABhbEQB +64BL0uL4lkFtpXXbJzQ6mgUNQveJkfUWOoB+cA/6GtI4J0aQfvQgloCYI6jxNn/e +Nvk5OV9KFOhXS2dnDft3wHU46sg5yXOuds1u6UrOoATBNFlkS95m4zIX1Svu091+ +CKTiLK85+ZiFtAlU2bPr3Bk3GhL3Z586ae6a4QUEx6SPQVXc18ezB4qxKqFc+avI +ylccYMRhVP+ruADxtUM5Vy6x3U8BwBK5RLdecRx2FEBDImY1AgMBAAGjggHfMIIB +2zAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjAWBgNVHSUBAf8EDDAK +BggrBgEFBQcDAzAdBgNVHQ4EFgQUg8fLNKiWjMkAnoSnLs4M1TUQO+YwgewGA1Ud +IwSB5DCB4YAUg8fLNKiWjMkAnoSnLs4M1TUQO+ahgcWkgcIwgb8xCzAJBgNVBAYT +AlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEmMCQGA1UE +ChMdQ29udGVudCBTaWduYXR1cmUgRGV2IFNpZ25pbmcxJjAkBgNVBAMTHWRldi5j +b250ZW50LXNpZ25hdHVyZS5yb290LmNhMTswOQYJKoZIhvcNAQkBFixjbG91ZHNl +YytkZXZyb290Y29udGVudHNpZ25hdHVyZUBtb3ppbGxhLmNvbYIBATBCBglghkgB +hvhCAQQENRYzaHR0cHM6Ly9jb250ZW50LXNpZ25hdHVyZS5kZXYubW96YXdzLm5l +dC9jYS9jcmwucGVtME4GCCsGAQUFBwEBBEIwQDA+BggrBgEFBQcwAoYyaHR0cHM6 +Ly9jb250ZW50LXNpZ25hdHVyZS5kZXYubW96YXdzLm5ldC9jYS9jYS5wZW0wDQYJ +KoZIhvcNAQEMBQADggIBAAAQ+fotZE79FfZ8Lz7eiTUzlwHXCdSE2XD3nMROu6n6 +uLTBPrf2C+k+U1FjKVvL5/WCUj6hIzP2X6Sb8+o0XHX9mKN0yoMORTEYJOnazYPK +KSUUFnE4vGgQkr6k/31gGRMTICdnf3VOOAlUCQ5bOmGIuWi401E3sbd85U+TJe0A +nHlU+XjtfzlqcBvQivdbA0s+GEG55uRPvn952aTBEMHfn+2JqKeLShl4AtUAfu+h +6md3Z2HrEC7B3GK8ekWPu0G/ZuWTuFvOimZ+5C8IPRJXcIR/siPQl1x6dpTCew6t +lPVcVuvg6SQwzvxetkNrGUe2Wb2s9+PK2PUvfOS8ee25SNmfG3XK9qJpqGUhzSBX +T8QQgyxd0Su5G7Wze7aaHZd/fqIm/G8YFR0HiC2xni/lnDTXFDPCe+HCnSk0bH6U +wpr6I1yK8oZ2IdnNVfuABGMmGOhvSQ8r7//ea9WKhCsGNQawpVWVioY7hpyNAJ0O +Vn4xqG5f6allz8lgpwAQ+AeEEClHca6hh6mj9KhD1Of1CC2Vx52GHNh/jMYEc3/g +zLKniencBqn3Y2XH2daITGJddcleN09+a1NaTkT3hgr7LumxM8EVssPkC+z9j4Vf +Gbste+8S5QCMhh00g5vR9QF8EaFqdxCdSxrsA4GmpCa5UQl8jtCnpp2DLKXuOh72 +-----END CERTIFICATE----- diff --git a/toolkit/components/normandy/test/unit/mock_api/api/v1/classify_client/index.json b/toolkit/components/normandy/test/unit/mock_api/api/v1/classify_client/index.json new file mode 100644 index 0000000000..a9b6239e48 --- /dev/null +++ b/toolkit/components/normandy/test/unit/mock_api/api/v1/classify_client/index.json @@ -0,0 +1,4 @@ +{ + "country": "US", + "request_time": "2017-02-22T17:43:24.657841Z" +} diff --git a/toolkit/components/normandy/test/unit/mock_api/api/v1/extension/1/index.json b/toolkit/components/normandy/test/unit/mock_api/api/v1/extension/1/index.json new file mode 100644 index 0000000000..f088592a9b --- /dev/null +++ b/toolkit/components/normandy/test/unit/mock_api/api/v1/extension/1/index.json @@ -0,0 +1,9 @@ +{ + "id": 1, + "name": "Normandy Fixture", + "xpi": "http://example.com/browser/toolkit/components/normandy/test/browser/fixtures/normandy.xpi", + "extension_id": "normandydriver@example.com", + "version": "1.0", + "hash": "ade1c14196ec4fe0aa0a6ba40ac433d7c8d1ec985581a8a94d43dc58991b5171", + "hash_algorithm": "sha256" +} diff --git a/toolkit/components/normandy/test/unit/mock_api/api/v1/extension/index.json b/toolkit/components/normandy/test/unit/mock_api/api/v1/extension/index.json new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/toolkit/components/normandy/test/unit/mock_api/api/v1/extension/index.json diff --git a/toolkit/components/normandy/test/unit/mock_api/api/v1/index.json b/toolkit/components/normandy/test/unit/mock_api/api/v1/index.json new file mode 100644 index 0000000000..d2414056c0 --- /dev/null +++ b/toolkit/components/normandy/test/unit/mock_api/api/v1/index.json @@ -0,0 +1,5 @@ +{ + "classify-client": "/api/v1/classify_client/", + "extension-list": "/api/v1/extension/", + "recipe-signed": "/api/v1/recipe/signed/" +} diff --git a/toolkit/components/normandy/test/unit/mock_api/api/v1/recipe/signed/index.json b/toolkit/components/normandy/test/unit/mock_api/api/v1/recipe/signed/index.json new file mode 100644 index 0000000000..5f3515dc97 --- /dev/null +++ b/toolkit/components/normandy/test/unit/mock_api/api/v1/recipe/signed/index.json @@ -0,0 +1,24 @@ +[ + { + "recipe": { + "action": "console-log", + "arguments": { "message": "asdfasfda sdf sa" }, + "channels": [], + "countries": [], + "enabled": true, + "extra_filter_expression": "true || true", + "filter_expression": "true || true", + "id": 1, + "last_updated": "2017-02-17T18:29:09.839239Z", + "locales": [], + "name": "system-addon-test", + "revision_id": "b2cb8a26e132182d7d02cf50695d2c7f06cf3b954ff2ff63bca49d724ee91950" + }, + "signature": { + "public_key": "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEVEKiCAIkwRg1VFsP8JOYdSF6a3qvgbRPoEK9eTuLbrB6QixozscKR4iWJ8ZOOX6RPCRgFdfVDoZqjFBFNJN9QtRBk0mVtHbnErx64d2vMF0oWencS1hyLW2whgOgOz7p", + "signature": "p4g3eurmPsJK5UcGT97BRyKstpwZ_2mNJkDGpd6QXlkXfvgwprjeyb5yeIEkKUXqc6krWid4obB_OP9-CwOi9tvKY1pV8p98CT5BhF0IVgpF3b7KBW1a0BVdg5owoG5W", + "timestamp": "2017-02-17T18:29:09.847614Z", + "x5u": "/normandy.content-signature.mozilla.org-20210705.dev.chain" + } + } +] diff --git a/toolkit/components/normandy/test/unit/mock_api/normandy.content-signature.mozilla.org-20210705.dev.chain b/toolkit/components/normandy/test/unit/mock_api/normandy.content-signature.mozilla.org-20210705.dev.chain new file mode 100644 index 0000000000..5bf53787d8 --- /dev/null +++ b/toolkit/components/normandy/test/unit/mock_api/normandy.content-signature.mozilla.org-20210705.dev.chain @@ -0,0 +1,123 @@ +-----BEGIN CERTIFICATE----- +MIIGRTCCBC2gAwIBAgIEAQAABTANBgkqhkiG9w0BAQwFADBrMQswCQYDVQQGEwJV +UzEQMA4GA1UEChMHQWxsaXpvbTEXMBUGA1UECxMOQ2xvdWQgU2VydmljZXMxMTAv +BgNVBAMTKERldnppbGxhIFNpZ25pbmcgU2VydmljZXMgSW50ZXJtZWRpYXRlIDEw +HhcNMTYwNzA2MjE1NzE1WhcNMjEwNzA1MjE1NzE1WjCBrzELMAkGA1UEBhMCVVMx +EzARBgNVBAgTCkNhbGlmb3JuaWExHDAaBgNVBAoTE01vemlsbGEgQ29ycG9yYXRp +b24xFzAVBgNVBAsTDkNsb3VkIFNlcnZpY2VzMS8wLQYDVQQDEyZub3JtYW5keS5j +b250ZW50LXNpZ25hdHVyZS5tb3ppbGxhLm9yZzEjMCEGCSqGSIb3DQEJARYUc2Vj +dXJpdHlAbW96aWxsYS5vcmcwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAARUQqIIAiTB +GDVUWw/wk5h1IXpreq+BtE+gQr15O4tusHpCLGjOxwpHiJYnxk45fpE8JGAV19UO +hmqMUEU0k31C1EGTSZW0ducSvHrh3a8wXShZ6dxLWHItbbCGA6A7PumjggJYMIIC +VDAdBgNVHQ4EFgQUVfksSjlZ0i1TBiS1vcoObaMeXn0wge8GA1UdIwSB5zCB5IAU +/YboUIXAovChEpudDBuodHKbjUuhgcWkgcIwgb8xCzAJBgNVBAYTAlVTMQswCQYD +VQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEmMCQGA1UEChMdQ29udGVu +dCBTaWduYXR1cmUgRGV2IFNpZ25pbmcxJjAkBgNVBAMTHWRldi5jb250ZW50LXNp +Z25hdHVyZS5yb290LmNhMTswOQYJKoZIhvcNAQkBFixjbG91ZHNlYytkZXZyb290 +Y29udGVudHNpZ25hdHVyZUBtb3ppbGxhLmNvbYIEAQAABDAMBgNVHRMBAf8EAjAA +MA4GA1UdDwEB/wQEAwIHgDAWBgNVHSUBAf8EDDAKBggrBgEFBQcDAzBEBgNVHR8E +PTA7MDmgN6A1hjNodHRwczovL2NvbnRlbnQtc2lnbmF0dXJlLmRldi5tb3phd3Mu +bmV0L2NhL2NybC5wZW0wQgYJYIZIAYb4QgEEBDUWM2h0dHBzOi8vY29udGVudC1z +aWduYXR1cmUuZGV2Lm1vemF3cy5uZXQvY2EvY3JsLnBlbTBOBggrBgEFBQcBAQRC +MEAwPgYIKwYBBQUHMAKGMmh0dHBzOi8vY29udGVudC1zaWduYXR1cmUuZGV2Lm1v +emF3cy5uZXQvY2EvY2EucGVtMDEGA1UdEQQqMCiCJm5vcm1hbmR5LmNvbnRlbnQt +c2lnbmF0dXJlLm1vemlsbGEub3JnMA0GCSqGSIb3DQEBDAUAA4ICAQCwb+8JTAB7 +ZfQmFqPUIV2cQQv696AaDPQCtA9YS4zmUfcLMvfZVAbK397zFr0RMDdLiTUQDoeq +rBEmPXhJRPiv6JAK4n7Jf6Y6XfXcNxx+q3garR09Vm/0CnEq/iV+ZAtPkoKIO9kr +Nkzecd894yQCF4hIuPQ5qtMySeqJmH3Dp13eq4T0Oew1Bu32rNHuBJh2xYBkWdun +aAw/YX0I5EqZBP/XA6gbiA160tTK+hnpnlMtw/ljkvfhHbWpICD4aSiTL8L3vABQ +j7bqjMKR5xDkuGWshZfcmonpvQhGTye/RZ1vz5IzA3VOJt1mz5bdZlitpaOm/Yv0 +x6aODz8GP/PiRWFQ5CW8Uf/7pGc5rSyvnfZV2ix8EzFlo8cUtuN1fjrPFPOFOLvG +iiB6S9nlXiKBGYIDdd8V8iC5xJpzjiAWJQigwSNzuc2K30+iPo3w0zazkwe5V8jW +gj6gItYxh5xwVQTPHD0EOd9HvV1ou42+rH5Y+ISFUm25zz02UtUHEK0BKtL0lmdt +DwVq5jcHn6bx2/iwUtlKvPXtfM/6JjTJlkLZLtS7U5/pwcS0owo9zAL0qg3bdm16 ++v/olmPqQFLUHmamJTzv3rojj5X/uVdx1HMM3wBjV9tRYoYaZw9RIInRmM8Z1pHv +JJ+CIZgCyd5vgp57BKiodRZcgHoCH+BkOQ== +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIHijCCBXKgAwIBAgIEAQAABDANBgkqhkiG9w0BAQwFADCBvzELMAkGA1UEBhMC +VVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MSYwJAYDVQQK +Ex1Db250ZW50IFNpZ25hdHVyZSBEZXYgU2lnbmluZzEmMCQGA1UEAxMdZGV2LmNv +bnRlbnQtc2lnbmF0dXJlLnJvb3QuY2ExOzA5BgkqhkiG9w0BCQEWLGNsb3Vkc2Vj +K2RldnJvb3Rjb250ZW50c2lnbmF0dXJlQG1vemlsbGEuY29tMB4XDTE2MDcwNjIx +NDkyNloXDTIxMDcwNTIxNDkyNlowazELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0Fs +bGl6b20xFzAVBgNVBAsTDkNsb3VkIFNlcnZpY2VzMTEwLwYDVQQDEyhEZXZ6aWxs +YSBTaWduaW5nIFNlcnZpY2VzIEludGVybWVkaWF0ZSAxMIICIjANBgkqhkiG9w0B +AQEFAAOCAg8AMIICCgKCAgEAypIfUURYgWzGw8G/1Pz9zW+Tsjirx2owThiv2gys +wJiWL/9/2gzKOrYDEqlDUudfA/BjVRtT9+NbYgnhaCkNfADOAacWS83aMhedAqhP +bVd5YhGQdpijI7f1AVTSb0ehrU2nhOZHvHX5Tk2fbRx3ryefIazNTLFGpiMBbsNv +tSI/+fjW8s0MhKNqlLnk6a9mZKo0mEy7HjGYV8nzsgI17rKLx/s2HG4TFG0+JQzc +UGlum3Tg58ritDzWdyKIkmKNZ48oLBX99Qc8j8B1UyiLv6TZmjVX0I+Ds7eSWHZk +0axLEpTyf2r7fHvN4iDNCPajw+ZpuuBfbs80Ha8b8MHvnpf9fbwiirodNQOVpY4c +t5E3Us3eYwBKdqDEbECWxCKGAS2/iVVUCNKHsg0sSxgqcwxrxyrddQRUQ0EM38DZ +F/vHt+vTdHt07kezbjJe0Kklel59uSpghA0iL4vxzbTns1fuwYOgVrNGs3acTkiB +GhFOxRXUPGtpdYmv+AaR9WlWJQY1GIEoVrinPVH7bcCwyh1CcUbHL+oAFTcmc6kZ +7azNg21tWILIRL7R0IZYQm0tF5TTwCsjVC7FuHaBtkxtVrrZqeKjvVXQ8TK5VoI0 +BUQ6BKHGeTtm+0HBpheYBDy3wkOsEGbGHLEM6cMeiz6PyCXF8wXli8Qb/TjN3LHZ +e30CAwEAAaOCAd8wggHbMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgGG +MBYGA1UdJQEB/wQMMAoGCCsGAQUFBwMDMB0GA1UdDgQWBBT9huhQhcCi8KESm50M +G6h0cpuNSzCB7AYDVR0jBIHkMIHhgBSDx8s0qJaMyQCehKcuzgzVNRA75qGBxaSB +wjCBvzELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFp +biBWaWV3MSYwJAYDVQQKEx1Db250ZW50IFNpZ25hdHVyZSBEZXYgU2lnbmluZzEm +MCQGA1UEAxMdZGV2LmNvbnRlbnQtc2lnbmF0dXJlLnJvb3QuY2ExOzA5BgkqhkiG +9w0BCQEWLGNsb3Vkc2VjK2RldnJvb3Rjb250ZW50c2lnbmF0dXJlQG1vemlsbGEu +Y29tggEBMEIGCWCGSAGG+EIBBAQ1FjNodHRwczovL2NvbnRlbnQtc2lnbmF0dXJl +LmRldi5tb3phd3MubmV0L2NhL2NybC5wZW0wTgYIKwYBBQUHAQEEQjBAMD4GCCsG +AQUFBzAChjJodHRwczovL2NvbnRlbnQtc2lnbmF0dXJlLmRldi5tb3phd3MubmV0 +L2NhL2NhLnBlbTANBgkqhkiG9w0BAQwFAAOCAgEAbum0z0ccqI1Wp49VtsGmUPHA +gjPPy2Xa5NePmqY87WrGdhjN3xbLVb3hx8T2N6pqDjMY2rEynXKEOek3oJkQ3C6J +8AFP6Y93gaAlNz6EA0J0mqdW5TMI8YEYsu2ma+dQQ8wm9f/5b+/Y8qwfhztP06W5 +H6IG04/RvgUwYMnSR4QvT309fu5UmCRUDzsO53ZmQCfmN94u3NxHc4S6n0Q6AKAM +kh5Ld9SQnlqqqDykzn7hYDi8nTLWc7IYqkGfNMilDEKbAl4CjnSfyEvpdFAJ9sPR +UL+kaWFSMvaqIPNpxS5OpoPZjmxEc9HHnCHxtfDHWdXTJILjijPrCdMaxOCHfIqV +5roOJggI4RZ0YM68IL1MfN4IEVOrHhKjDHtd1gcmy2KU4jfj9LQq9YTnyvZ2z1yS +lS310HG3or1K8Nnu5Utfe7T6ppX8bLRMkS1/w0p7DKxHaf4D/GJcCtM9lcSt9JpW +6ACKFikjWR4ZxczYKgApc0wcjd7XBuO5777xtOeyEUDHdDft3jiXA91dYM5UAzc3 +69z/3zmaELzo0gWcrjLXh7fU9AvbU4EUF6rwzxbPGF78jJcGK+oBf8uWUCkBykDt +VsAEZI1u4EDg8e/C1nFqaH9gNMArAgquYIB9rve+hdprIMnva0S147pflWopBWcb +jwzgpfquuYnnxe0CNBA= +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIH3DCCBcSgAwIBAgIBATANBgkqhkiG9w0BAQwFADCBvzELMAkGA1UEBhMCVVMx +CzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MSYwJAYDVQQKEx1D +b250ZW50IFNpZ25hdHVyZSBEZXYgU2lnbmluZzEmMCQGA1UEAxMdZGV2LmNvbnRl +bnQtc2lnbmF0dXJlLnJvb3QuY2ExOzA5BgkqhkiG9w0BCQEWLGNsb3Vkc2VjK2Rl +dnJvb3Rjb250ZW50c2lnbmF0dXJlQG1vemlsbGEuY29tMB4XDTE2MDcwNjE4MTUy +MloXDTI2MDcwNDE4MTUyMlowgb8xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEW +MBQGA1UEBxMNTW91bnRhaW4gVmlldzEmMCQGA1UEChMdQ29udGVudCBTaWduYXR1 +cmUgRGV2IFNpZ25pbmcxJjAkBgNVBAMTHWRldi5jb250ZW50LXNpZ25hdHVyZS5y +b290LmNhMTswOQYJKoZIhvcNAQkBFixjbG91ZHNlYytkZXZyb290Y29udGVudHNp +Z25hdHVyZUBtb3ppbGxhLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC +ggIBAJcPcXhD8MzF6OTn5qZ0L7lX1+PEgLKhI9g1HxxDYDVup4Zm0kZhPGmFSlml +6eVO99OvvHdAlHhQGCIG7h+w1cp66mWjfpcvtQH23uRoKZfiW3jy1jUWrvdXolxR +t1taZosjzo+9OP8TvG6LpJj7AvqUiYD4wYnQJtt0jNRN4d6MUfQwiavSS5uTBuxd +ZJ4TsPvEI+Iv4A4PSobSzxkg79LTMvsGtDLQv7nN5hMs9T18EL5GnIKoqnSQCU0d +n2CN7S3QiQ+cbORWsSYqCTj1gUbFh6X3duEB/ypLcyWFbqeJmPHikjY8q8pLjZSB +IYiTJYLyvYlKdM5QleC/xuBNnMPCftrwwLHjWE4Dd7C9t7k0R5xyOetuiHLCwOcQ +tuckp7RgFKoviMNv3gdkzwVapOklcsaRkRscv6OMTKJNsdJVIDLrPF1wMABhbEQB +64BL0uL4lkFtpXXbJzQ6mgUNQveJkfUWOoB+cA/6GtI4J0aQfvQgloCYI6jxNn/e +Nvk5OV9KFOhXS2dnDft3wHU46sg5yXOuds1u6UrOoATBNFlkS95m4zIX1Svu091+ +CKTiLK85+ZiFtAlU2bPr3Bk3GhL3Z586ae6a4QUEx6SPQVXc18ezB4qxKqFc+avI +ylccYMRhVP+ruADxtUM5Vy6x3U8BwBK5RLdecRx2FEBDImY1AgMBAAGjggHfMIIB +2zAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjAWBgNVHSUBAf8EDDAK +BggrBgEFBQcDAzAdBgNVHQ4EFgQUg8fLNKiWjMkAnoSnLs4M1TUQO+YwgewGA1Ud +IwSB5DCB4YAUg8fLNKiWjMkAnoSnLs4M1TUQO+ahgcWkgcIwgb8xCzAJBgNVBAYT +AlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEmMCQGA1UE +ChMdQ29udGVudCBTaWduYXR1cmUgRGV2IFNpZ25pbmcxJjAkBgNVBAMTHWRldi5j +b250ZW50LXNpZ25hdHVyZS5yb290LmNhMTswOQYJKoZIhvcNAQkBFixjbG91ZHNl +YytkZXZyb290Y29udGVudHNpZ25hdHVyZUBtb3ppbGxhLmNvbYIBATBCBglghkgB +hvhCAQQENRYzaHR0cHM6Ly9jb250ZW50LXNpZ25hdHVyZS5kZXYubW96YXdzLm5l +dC9jYS9jcmwucGVtME4GCCsGAQUFBwEBBEIwQDA+BggrBgEFBQcwAoYyaHR0cHM6 +Ly9jb250ZW50LXNpZ25hdHVyZS5kZXYubW96YXdzLm5ldC9jYS9jYS5wZW0wDQYJ +KoZIhvcNAQEMBQADggIBAAAQ+fotZE79FfZ8Lz7eiTUzlwHXCdSE2XD3nMROu6n6 +uLTBPrf2C+k+U1FjKVvL5/WCUj6hIzP2X6Sb8+o0XHX9mKN0yoMORTEYJOnazYPK +KSUUFnE4vGgQkr6k/31gGRMTICdnf3VOOAlUCQ5bOmGIuWi401E3sbd85U+TJe0A +nHlU+XjtfzlqcBvQivdbA0s+GEG55uRPvn952aTBEMHfn+2JqKeLShl4AtUAfu+h +6md3Z2HrEC7B3GK8ekWPu0G/ZuWTuFvOimZ+5C8IPRJXcIR/siPQl1x6dpTCew6t +lPVcVuvg6SQwzvxetkNrGUe2Wb2s9+PK2PUvfOS8ee25SNmfG3XK9qJpqGUhzSBX +T8QQgyxd0Su5G7Wze7aaHZd/fqIm/G8YFR0HiC2xni/lnDTXFDPCe+HCnSk0bH6U +wpr6I1yK8oZ2IdnNVfuABGMmGOhvSQ8r7//ea9WKhCsGNQawpVWVioY7hpyNAJ0O +Vn4xqG5f6allz8lgpwAQ+AeEEClHca6hh6mj9KhD1Of1CC2Vx52GHNh/jMYEc3/g +zLKniencBqn3Y2XH2daITGJddcleN09+a1NaTkT3hgr7LumxM8EVssPkC+z9j4Vf +Gbste+8S5QCMhh00g5vR9QF8EaFqdxCdSxrsA4GmpCa5UQl8jtCnpp2DLKXuOh72 +-----END CERTIFICATE----- diff --git a/toolkit/components/normandy/test/unit/query_server.sjs b/toolkit/components/normandy/test/unit/query_server.sjs new file mode 100644 index 0000000000..dd00d74bf6 --- /dev/null +++ b/toolkit/components/normandy/test/unit/query_server.sjs @@ -0,0 +1,34 @@ +const CC = Components.Constructor; +const BinaryInputStream = CC( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); + +// Returns a JSON string containing the query string arguments and the +// request body parsed as JSON. +function handleRequest(request, response) { + // Allow cross-origin, so you can XHR to it! + response.setHeader("Access-Control-Allow-Origin", "*", false); + // Avoid confusing cache behaviors + response.setHeader("Cache-Control", "no-cache", false); + response.setHeader("Content-Type", "application/json", false); + + // Read request body + const inputStream = new BinaryInputStream(request.bodyInputStream); + let bytes = []; + let available; + while ((available = inputStream.available()) > 0) { + bytes = bytes.concat(inputStream.readByteArray(available)); + } + const body = String.fromCharCode.apply(null, bytes); + + // Write response body + const data = { queryString: {}, body: body ? JSON.parse(body) : {} }; + const params = request.queryString.split("&"); + for (const param of params) { + const [key, value] = param.split("="); + data.queryString[key] = value; + } + response.write(JSON.stringify(data)); +} diff --git a/toolkit/components/normandy/test/unit/test_Normandy.js b/toolkit/components/normandy/test/unit/test_Normandy.js new file mode 100644 index 0000000000..53814734eb --- /dev/null +++ b/toolkit/components/normandy/test/unit/test_Normandy.js @@ -0,0 +1,92 @@ +/* Any copyright is dedicated to the Public Domain. +http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { Normandy } = ChromeUtils.importESModule( + "resource://normandy/Normandy.sys.mjs" +); +const { NormandyMigrations } = ChromeUtils.importESModule( + "resource://normandy/NormandyMigrations.sys.mjs" +); +ChromeUtils.defineESModuleGetters(this, { + TestUtils: "resource://testing-common/TestUtils.sys.mjs", +}); + +/* import-globals-from utils.js */ +load("utils.js"); + +NormandyTestUtils.init({ add_task }); +const { decorate_task } = NormandyTestUtils; + +// Normandy's initialization function should set the start preferences before +// its first `await`. +decorate_task( + NormandyTestUtils.withStub(Normandy, "finishInit"), + NormandyTestUtils.withStub(NormandyMigrations, "applyAll"), + NormandyTestUtils.withMockPreferences(), + async function test_normandy_init_applies_startup_prefs_synchronously({ + mockPreferences, + }) { + const experimentPref = "test.experiment"; + const rolloutPref = "test.rollout"; + const experimentStartupPref = `app.normandy.startupExperimentPrefs.${experimentPref}`; + const rolloutStartupPref = `app.normandy.startupRolloutPrefs.${rolloutPref}`; + + mockPreferences.preserve(experimentPref, "default"); + mockPreferences.preserve(rolloutPref, "default"); + mockPreferences.set(experimentStartupPref, "experiment"); + mockPreferences.set(rolloutStartupPref, "rollout"); + + Assert.equal( + Services.prefs.getCharPref(experimentPref, "default"), + "default" + ); + Assert.equal(Services.prefs.getCharPref(rolloutPref, "default"), "default"); + + let initPromise = Normandy.init({ runAsync: false }); + + // note: There are no awaits before these asserts, so only the part of + // Normandy's initialization before its first await can run. + Assert.equal( + Services.prefs.getCharPref(experimentPref, "default"), + "experiment" + ); + Assert.equal(Services.prefs.getCharPref(rolloutPref, "default"), "rollout"); + + await initPromise; + await Normandy.uninit(); + } +); + +// Normandy's initialization function should register the observer for UI +// startup before it's first await. +decorate_task( + NormandyTestUtils.withStub(Normandy, "finishInit"), + NormandyTestUtils.withStub(NormandyMigrations, "applyAll"), + async function test_normandy_init_applies_startup_prefs_synchronously({ + applyAllStub, + }) { + let originalDeferred = Normandy.uiAvailableNotificationObserved; + let mockUiAvailableDeferred = Promise.withResolvers(); + Normandy.uiAvailableNotificationObserved = mockUiAvailableDeferred; + + let applyAllDeferred = Promise.withResolvers(); + applyAllStub.returns(applyAllStub); + + let promiseResolvedCount = 0; + mockUiAvailableDeferred.promise.then(() => promiseResolvedCount++); + + let initPromise = Normandy.init(); + + Assert.equal(promiseResolvedCount, 0); + Normandy.observe(null, "sessionstore-windows-restored"); + await TestUtils.waitForCondition(() => promiseResolvedCount === 1); + + applyAllDeferred.resolve(); + + await initPromise; + await Normandy.uninit(); + Normandy.uiAvailableNotificationObserved = originalDeferred; + } +); diff --git a/toolkit/components/normandy/test/unit/test_NormandyApi.js b/toolkit/components/normandy/test/unit/test_NormandyApi.js new file mode 100644 index 0000000000..c0c826b045 --- /dev/null +++ b/toolkit/components/normandy/test/unit/test_NormandyApi.js @@ -0,0 +1,251 @@ +/* globals sinon */ +"use strict"; + +/* import-globals-from utils.js */ +load("utils.js"); + +NormandyTestUtils.init({ add_task }); +const { decorate_task } = NormandyTestUtils; + +decorate_task(withMockApiServer(), async function test_get({ serverUrl }) { + // Test that NormandyApi can fetch from the test server. + const response = await NormandyApi.get(`${serverUrl}/api/v1/`); + const data = await response.json(); + equal( + data["recipe-signed"], + "/api/v1/recipe/signed/", + "Expected data in response" + ); +}); + +decorate_task( + withMockApiServer(), + async function test_getApiUrl({ serverUrl }) { + const apiBase = `${serverUrl}/api/v1`; + // Test that NormandyApi can use the self-describing API's index + const recipeListUrl = await NormandyApi.getApiUrl("extension-list"); + equal( + recipeListUrl, + `${apiBase}/extension/`, + "Can retrieve extension-list URL from API" + ); + } +); + +decorate_task( + withMockApiServer(), + async function test_getApiUrlSlashes({ serverUrl, mockPreferences }) { + const fakeResponse = new MockResponse( + JSON.stringify({ "test-endpoint": `${serverUrl}/test/` }) + ); + const mockGet = sinon + .stub(NormandyApi, "get") + .callsFake(async () => fakeResponse); + + // without slash + { + NormandyApi.clearIndexCache(); + mockPreferences.set("app.normandy.api_url", `${serverUrl}/api/v1`); + const endpoint = await NormandyApi.getApiUrl("test-endpoint"); + equal(endpoint, `${serverUrl}/test/`); + ok( + mockGet.calledWithExactly(`${serverUrl}/api/v1/`), + "trailing slash was added" + ); + mockGet.resetHistory(); + } + + // with slash + { + NormandyApi.clearIndexCache(); + mockPreferences.set("app.normandy.api_url", `${serverUrl}/api/v1/`); + const endpoint = await NormandyApi.getApiUrl("test-endpoint"); + equal(endpoint, `${serverUrl}/test/`); + ok( + mockGet.calledWithExactly(`${serverUrl}/api/v1/`), + "existing trailing slash was preserved" + ); + mockGet.resetHistory(); + } + + NormandyApi.clearIndexCache(); + mockGet.restore(); + } +); + +// Test validation errors due to validation throwing an exception (e.g. when +// parameters passed to validation are malformed). +decorate_task( + withMockApiServer(), + async function test_validateSignedObject_validation_error() { + // Mock the x5u URL + const getStub = sinon.stub(NormandyApi, "get").callsFake(async url => { + ok(url.endsWith("x5u/"), "the only request should be to fetch the x5u"); + return new MockResponse("certchain"); + }); + + const signedObject = { a: 1, b: 2 }; + const signature = { + signature: "invalidsignature", + x5u: "http://localhost/x5u/", + }; + + // Validation should fail due to a malformed x5u and signature. + try { + await NormandyApi.verifyObjectSignature( + signedObject, + signature, + "object" + ); + ok(false, "validateSignedObject did not throw for a validation error"); + } catch (err) { + ok( + err instanceof NormandyApi.InvalidSignatureError, + "Error is an InvalidSignatureError" + ); + ok(/signature/.test(err), "Error is due to a validation error"); + } + + getStub.restore(); + } +); + +// Test validation errors due to validation returning false (e.g. when parameters +// passed to validation are correctly formed, but not valid for the data). +decorate_task( + withMockApiServer("invalid_recipe_signature_api"), + async function test_verifySignedObject_invalid_signature() { + // Get the test recipe and signature from the mock server. + const recipesUrl = await NormandyApi.getApiUrl("recipe-signed"); + const recipeResponse = await NormandyApi.get(recipesUrl); + const recipes = await recipeResponse.json(); + equal(recipes.length, 1, "Test data has one recipe"); + const [{ recipe, signature }] = recipes; + + try { + await NormandyApi.verifyObjectSignature(recipe, signature, "recipe"); + ok(false, "verifyObjectSignature did not throw for an invalid signature"); + } catch (err) { + ok( + err instanceof NormandyApi.InvalidSignatureError, + "Error is an InvalidSignatureError" + ); + ok(/signature/.test(err), "Error is due to an invalid signature"); + } + } +); + +decorate_task(withMockApiServer(), async function test_classifyClient() { + const classification = await NormandyApi.classifyClient(); + Assert.deepEqual(classification, { + country: "US", + request_time: new Date("2017-02-22T17:43:24.657841Z"), + }); +}); + +decorate_task(withMockApiServer(), async function test_fetchExtensionDetails() { + const extensionDetails = await NormandyApi.fetchExtensionDetails(1); + deepEqual(extensionDetails, { + id: 1, + name: "Normandy Fixture", + xpi: "http://example.com/browser/toolkit/components/normandy/test/browser/fixtures/normandy.xpi", + extension_id: "normandydriver@example.com", + version: "1.0", + hash: "ade1c14196ec4fe0aa0a6ba40ac433d7c8d1ec985581a8a94d43dc58991b5171", + hash_algorithm: "sha256", + }); +}); + +decorate_task( + withScriptServer("query_server.sjs"), + async function test_getTestServer({ serverUrl }) { + // Test that NormandyApi can fetch from the test server. + const response = await NormandyApi.get(serverUrl); + const data = await response.json(); + Assert.deepEqual( + data, + { queryString: {}, body: {} }, + "NormandyApi returned incorrect server data." + ); + } +); + +decorate_task( + withScriptServer("query_server.sjs"), + async function test_getQueryString({ serverUrl }) { + // Test that NormandyApi can send query string parameters to the test server. + const response = await NormandyApi.get(serverUrl, { + foo: "bar", + baz: "biff", + }); + const data = await response.json(); + Assert.deepEqual( + data, + { queryString: { foo: "bar", baz: "biff" }, body: {} }, + "NormandyApi sent an incorrect query string." + ); + } +); + +// Test that no credentials are sent, even if the cookie store contains them. +decorate_task( + withScriptServer("cookie_server.sjs"), + async function test_sendsNoCredentials({ serverUrl }) { + // This test uses cookie_server.sjs, which responds to all requests with a + // response that sets a cookie. + + // send a request, to store a cookie in the cookie store + await fetch(serverUrl); + + // A normal request should send that cookie + const cookieExpectedDeferred = Promise.withResolvers(); + function cookieExpectedObserver(aSubject, aTopic, aData) { + equal( + aTopic, + "http-on-modify-request", + "Only the expected topic should be observed" + ); + let httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel); + equal( + httpChannel.getRequestHeader("Cookie"), + "type=chocolate-chip", + "The header should be sent" + ); + Services.obs.removeObserver( + cookieExpectedObserver, + "http-on-modify-request" + ); + cookieExpectedDeferred.resolve(); + } + Services.obs.addObserver(cookieExpectedObserver, "http-on-modify-request"); + await fetch(serverUrl); + await cookieExpectedDeferred.promise; + + // A request through the NormandyApi method should not send that cookie + const cookieNotExpectedDeferred = Promise.withResolvers(); + function cookieNotExpectedObserver(aSubject, aTopic, aData) { + equal( + aTopic, + "http-on-modify-request", + "Only the expected topic should be observed" + ); + let httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel); + Assert.throws( + () => httpChannel.getRequestHeader("Cookie"), + /NS_ERROR_NOT_AVAILABLE/, + "The cookie header should not be sent" + ); + Services.obs.removeObserver( + cookieNotExpectedObserver, + "http-on-modify-request" + ); + cookieNotExpectedDeferred.resolve(); + } + Services.obs.addObserver( + cookieNotExpectedObserver, + "http-on-modify-request" + ); + await NormandyApi.get(serverUrl); + await cookieNotExpectedDeferred.promise; + } +); diff --git a/toolkit/components/normandy/test/unit/test_PrefUtils.js b/toolkit/components/normandy/test/unit/test_PrefUtils.js new file mode 100644 index 0000000000..57130d8783 --- /dev/null +++ b/toolkit/components/normandy/test/unit/test_PrefUtils.js @@ -0,0 +1,223 @@ +/* Any copyright is dedicated to the Public Domain. +http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { PrefUtils } = ChromeUtils.importESModule( + "resource://normandy/lib/PrefUtils.sys.mjs" +); + +add_task(function getPrefGetsValues() { + const defaultBranch = Services.prefs.getDefaultBranch(""); + const userBranch = Services.prefs; + + defaultBranch.setBoolPref("test.bool", false); + userBranch.setBoolPref("test.bool", true); + defaultBranch.setIntPref("test.int", 1); + userBranch.setIntPref("test.int", 2); + defaultBranch.setStringPref("test.string", "default"); + userBranch.setStringPref("test.string", "user"); + + equal( + PrefUtils.getPref("test.bool", { branch: "user" }), + true, + "should read user branch bools" + ); + equal( + PrefUtils.getPref("test.int", { branch: "user" }), + 2, + "should read user branch ints" + ); + equal( + PrefUtils.getPref("test.string", { branch: "user" }), + "user", + "should read user branch strings" + ); + + equal( + PrefUtils.getPref("test.bool", { branch: "default" }), + false, + "should read default branch bools" + ); + equal( + PrefUtils.getPref("test.int", { branch: "default" }), + 1, + "should read default branch ints" + ); + equal( + PrefUtils.getPref("test.string", { branch: "default" }), + "default", + "should read default branch strings" + ); + + equal( + PrefUtils.getPref("test.bool"), + true, + "should read bools from the user branch by default" + ); + equal( + PrefUtils.getPref("test.int"), + 2, + "should read ints from the user branch by default" + ); + equal( + PrefUtils.getPref("test.string"), + "user", + "should read strings from the user branch by default" + ); + + equal( + PrefUtils.getPref("test.does_not_exist"), + null, + "Should return null for non-existent prefs by default" + ); + let defaultValue = Symbol(); + equal( + PrefUtils.getPref("test.does_not_exist", { defaultValue }), + defaultValue, + "Should use the passed default value" + ); +}); + +// This is an important test because the pref system can behave in strange ways +// when the user branch has a value, but the default branch does not. +add_task(function getPrefHandlesUserValueNoDefaultValue() { + Services.prefs.setStringPref("test.only-user-value", "user"); + + let defaultValue = Symbol(); + equal( + PrefUtils.getPref("test.only-user-value", { + branch: "default", + defaultValue, + }), + defaultValue + ); + equal(PrefUtils.getPref("test.only-user-value", { branch: "default" }), null); + equal(PrefUtils.getPref("test.only-user-value", { branch: "user" }), "user"); + equal(PrefUtils.getPref("test.only-user-value"), "user"); +}); + +add_task(function getPrefInvalidBranch() { + Assert.throws( + () => PrefUtils.getPref("test.pref", { branch: "invalid" }), + PrefUtils.UnexpectedPreferenceBranch + ); +}); + +add_task(function setPrefSetsValues() { + const defaultBranch = Services.prefs.getDefaultBranch(""); + const userBranch = Services.prefs; + + defaultBranch.setIntPref("test.int", 1); + userBranch.setIntPref("test.int", 2); + defaultBranch.setStringPref("test.string", "default"); + userBranch.setStringPref("test.string", "user"); + defaultBranch.setBoolPref("test.bool", false); + userBranch.setBoolPref("test.bool", true); + + PrefUtils.setPref("test.int", 3); + equal( + userBranch.getIntPref("test.int"), + 3, + "the user branch should change for ints" + ); + PrefUtils.setPref("test.int", 4, { branch: "default" }); + equal( + userBranch.getIntPref("test.int"), + 3, + "changing the default branch shouldn't affect the user branch for ints" + ); + PrefUtils.setPref("test.int", null, { branch: "user" }); + equal( + userBranch.getIntPref("test.int"), + 4, + "clearing the user branch should reveal the default value for ints" + ); + + PrefUtils.setPref("test.string", "user override"); + equal( + userBranch.getStringPref("test.string"), + "user override", + "the user branch should change for strings" + ); + PrefUtils.setPref("test.string", "default override", { branch: "default" }); + equal( + userBranch.getStringPref("test.string"), + "user override", + "changing the default branch shouldn't affect the user branch for strings" + ); + PrefUtils.setPref("test.string", null, { branch: "user" }); + equal( + userBranch.getStringPref("test.string"), + "default override", + "clearing the user branch should reveal the default value for strings" + ); + + PrefUtils.setPref("test.bool", false); + equal( + userBranch.getBoolPref("test.bool"), + false, + "the user branch should change for bools" + ); + // The above effectively unsets the user branch, since it is now the same as the default branch + PrefUtils.setPref("test.bool", true, { branch: "default" }); + equal( + userBranch.getBoolPref("test.bool"), + true, + "the default branch should change for bools" + ); + + defaultBranch.setBoolPref("test.bool", false); + userBranch.setBoolPref("test.bool", true); + equal( + userBranch.getBoolPref("test.bool"), + true, + "the precondition should hold" + ); + PrefUtils.setPref("test.bool", null, { branch: "user" }); + equal( + userBranch.getBoolPref("test.bool"), + false, + "setting the user branch to null should reveal the default value for bools" + ); +}); + +add_task(function setPrefInvalidBranch() { + Assert.throws( + () => PrefUtils.setPref("test.pref", "value", { branch: "invalid" }), + PrefUtils.UnexpectedPreferenceBranch + ); +}); + +add_task(function clearPrefClearsValues() { + const defaultBranch = Services.prefs.getDefaultBranch(""); + const userBranch = Services.prefs; + + defaultBranch.setStringPref("test.string", "default"); + userBranch.setStringPref("test.string", "user"); + equal( + userBranch.getStringPref("test.string"), + "user", + "the precondition should hold" + ); + PrefUtils.clearPref("test.string"); + equal( + userBranch.getStringPref("test.string"), + "default", + "clearing the user branch should reveal the default value for bools" + ); + + PrefUtils.clearPref("test.string", { branch: "default" }); + equal( + userBranch.getStringPref("test.string"), + "default", + "clearing the default branch shouldn't do anything" + ); +}); + +add_task(function clearPrefInvalidBranch() { + Assert.throws( + () => PrefUtils.clearPref("test.pref", { branch: "invalid" }), + PrefUtils.UnexpectedPreferenceBranch + ); +}); diff --git a/toolkit/components/normandy/test/unit/test_RecipeRunner.js b/toolkit/components/normandy/test/unit/test_RecipeRunner.js new file mode 100644 index 0000000000..710ac4d507 --- /dev/null +++ b/toolkit/components/normandy/test/unit/test_RecipeRunner.js @@ -0,0 +1,34 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const { updateAppInfo } = ChromeUtils.importESModule( + "resource://testing-common/AppInfo.sys.mjs" +); +const { RecipeRunner } = ChromeUtils.importESModule( + "resource://normandy/lib/RecipeRunner.sys.mjs" +); + +// Test that new build IDs trigger immediate recipe runs +add_task(async () => { + updateAppInfo({ + appBuildID: "new-build-id", + lastAppBuildID: "old-build-id", + }); + const runStub = sinon.stub(RecipeRunner, "run"); + const registerTimerStub = sinon.stub(RecipeRunner, "registerTimer"); + sinon.stub(RecipeRunner, "watchPrefs"); + + Services.prefs.setBoolPref("app.normandy.first_run", false); + + await RecipeRunner.init(); + Assert.deepEqual( + runStub.args, + [[{ trigger: "newBuildID" }]], + "RecipeRunner.run is called immediately on a new build ID" + ); + ok(registerTimerStub.called, "RecipeRunner.registerTimer registers a timer"); + + sinon.restore(); +}); diff --git a/toolkit/components/normandy/test/unit/test_addon_unenroll.js b/toolkit/components/normandy/test/unit/test_addon_unenroll.js new file mode 100644 index 0000000000..3dd3c12c4b --- /dev/null +++ b/toolkit/components/normandy/test/unit/test_addon_unenroll.js @@ -0,0 +1,307 @@ +const { AddonTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/AddonTestUtils.sys.mjs" +); +const { ExtensionTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/ExtensionXPCShellUtils.sys.mjs" +); +const { BranchedAddonStudyAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BranchedAddonStudyAction.sys.mjs" +); +const { BaseAction } = ChromeUtils.importESModule( + "resource://normandy/actions/BaseAction.sys.mjs" +); +const { TelemetryEvents } = ChromeUtils.importESModule( + "resource://normandy/lib/TelemetryEvents.sys.mjs" +); +const { AddonManager } = ChromeUtils.importESModule( + "resource://gre/modules/AddonManager.sys.mjs" +); +const { AddonStudies } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonStudies.sys.mjs" +); + +/* import-globals-from utils.js */ +load("utils.js"); + +NormandyTestUtils.init({ add_task }); +const { decorate_task } = NormandyTestUtils; + +const global = this; + +add_task(async () => { + ExtensionTestUtils.init(global); + AddonTestUtils.init(global); + AddonTestUtils.createAppInfo( + "xpcshell@tests.mozilla.org", + "XPCShell", + "1", + "1.9.2" + ); + AddonTestUtils.overrideCertDB(); + await AddonTestUtils.promiseStartupManager(); + + TelemetryEvents.init(); +}); + +decorate_task( + withMockApiServer(), + AddonStudies.withStudies([]), + async function test_addon_unenroll({ server: apiServer }) { + const ID = "study@tests.mozilla.org"; + + // Create a test extension that uses webextension experiments to install + // an unenroll listener. + let xpi = AddonTestUtils.createTempWebExtensionFile({ + manifest: { + version: "1.0", + + browser_specific_settings: { + gecko: { id: ID }, + }, + + experiment_apis: { + study: { + schema: "schema.json", + parent: { + scopes: ["addon_parent"], + script: "api.js", + paths: [["study"]], + }, + }, + }, + }, + + files: { + "schema.json": JSON.stringify([ + { + namespace: "study", + events: [ + { + name: "onStudyEnded", + type: "function", + }, + ], + }, + ]), + + "api.js": () => { + // The code below is serialized into a file embedded in an extension. + // But by including it here as code, eslint can analyze it. However, + // this code runs in a different environment with different globals, + // the following two lines avoid false eslint warnings: + /* globals browser, ExtensionAPI */ + /* eslint-disable-next-line no-shadow */ + const { AddonStudies } = ChromeUtils.importESModule( + "resource://normandy/lib/AddonStudies.sys.mjs" + ); + const { ExtensionCommon } = ChromeUtils.importESModule( + "resource://gre/modules/ExtensionCommon.sys.mjs" + ); + this.study = class extends ExtensionAPI { + getAPI(context) { + return { + study: { + onStudyEnded: new ExtensionCommon.EventManager({ + context, + name: "study.onStudyEnded", + register: fire => { + AddonStudies.addUnenrollListener( + this.extension.id, + reason => fire.sync(reason) + ); + return () => {}; + }, + }).api(), + }, + }; + } + }; + }, + }, + + background() { + browser.study.onStudyEnded.addListener(reason => { + browser.test.sendMessage("got-event", reason); + return new Promise(resolve => { + browser.test.onMessage.addListener(resolve); + }); + }); + }, + }); + + const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] }); + server.registerFile("/study.xpi", xpi); + + const API_ID = 999; + apiServer.registerPathHandler( + `/api/v1/extension/${API_ID}/`, + (request, response) => { + response.setStatusLine(request.httpVersion, 200, "OK"); + response.write( + JSON.stringify({ + id: API_ID, + name: "Addon Unenroll Fixture", + xpi: "http://example.com/study.xpi", + extension_id: ID, + version: "1.0", + hash: CryptoUtils.getFileHash(xpi, "sha256"), + hash_algorithm: "sha256", + }) + ); + } + ); + + // Begin by telling Normandy to install the test extension above + // that uses a webextension experiment to register a blocking callback + // to be invoked when the study ends. + let extension = ExtensionTestUtils.expectExtension(ID); + + const RECIPE_ID = 1; + const UNENROLL_REASON = "test-ending"; + let action = new BranchedAddonStudyAction(); + await action.processRecipe( + { + id: RECIPE_ID, + type: "addon-study", + arguments: { + slug: "addon-unenroll-test", + userFacingDescription: "A recipe to test add-on unenrollment", + userFacingName: "Add-on Unenroll Test", + isEnrollmentPaused: false, + branches: [ + { + ratio: 1, + slug: "only", + extensionApiId: API_ID, + }, + ], + }, + }, + BaseAction.suitability.FILTER_MATCH + ); + + await extension.awaitStartup(); + + let addon = await AddonManager.getAddonByID(ID); + ok(addon, "Extension is installed"); + + // Tell Normandy to end the study, the extension event should be fired. + let unenrollPromise = action.unenroll(RECIPE_ID, UNENROLL_REASON); + + let receivedReason = await extension.awaitMessage("got-event"); + info("Got onStudyEnded event in extension"); + equal(receivedReason, UNENROLL_REASON, "Unenroll reason should be passed"); + + // The extension has not yet finished its unenrollment tasks, so it + // should not yet be uninstalled. + addon = await AddonManager.getAddonByID(ID); + ok(addon, "Extension has not yet been uninstalled"); + + // Once the extension does resolve the promise returned from the + // event listener, the uninstall can proceed. + extension.sendMessage("resolve"); + await unenrollPromise; + + addon = await AddonManager.getAddonByID(ID); + equal( + addon, + null, + "After resolving studyEnded promise, extension is uninstalled" + ); + } +); + +/* Test that a broken unenroll listener doesn't stop the add-on from being removed */ +decorate_task( + withMockApiServer(), + AddonStudies.withStudies([]), + async function test_addon_unenroll({ server: apiServer }) { + const ID = "study@tests.mozilla.org"; + + // Create a dummy webextension + // an unenroll listener that throws an error. + let xpi = AddonTestUtils.createTempWebExtensionFile({ + manifest: { + version: "1.0", + + browser_specific_settings: { + gecko: { id: ID }, + }, + }, + }); + + const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] }); + server.registerFile("/study.xpi", xpi); + + const API_ID = 999; + apiServer.registerPathHandler( + `/api/v1/extension/${API_ID}/`, + (request, response) => { + response.setStatusLine(request.httpVersion, 200, "OK"); + response.write( + JSON.stringify({ + id: API_ID, + name: "Addon Fixture", + xpi: "http://example.com/study.xpi", + extension_id: ID, + version: "1.0", + hash: CryptoUtils.getFileHash(xpi, "sha256"), + hash_algorithm: "sha256", + }) + ); + } + ); + + // Begin by telling Normandy to install the test extension above that uses a + // webextension experiment to register a callback when the study ends that + // throws an error. + let extension = ExtensionTestUtils.expectExtension(ID); + + const RECIPE_ID = 1; + const UNENROLL_REASON = "test-ending"; + let action = new BranchedAddonStudyAction(); + await action.processRecipe( + { + id: RECIPE_ID, + type: "addon-study", + arguments: { + slug: "addon-unenroll-test", + userFacingDescription: "A recipe to test add-on unenrollment", + userFacingName: "Add-on Unenroll Test", + isEnrollmentPaused: false, + branches: [ + { + ratio: 1, + slug: "only", + extensionApiId: API_ID, + }, + ], + }, + }, + BaseAction.suitability.FILTER_MATCH + ); + + await extension.startupPromise; + + let addon = await AddonManager.getAddonByID(ID); + ok(addon, "Extension is installed"); + + let listenerDeferred = Promise.withResolvers(); + + AddonStudies.addUnenrollListener(ID, () => { + listenerDeferred.resolve(); + throw new Error("This listener is busted"); + }); + + // Tell Normandy to end the study, the extension event should be fired. + await action.unenroll(RECIPE_ID, UNENROLL_REASON); + await listenerDeferred; + + addon = await AddonManager.getAddonByID(ID); + equal( + addon, + null, + "Extension is uninstalled even though it threw an exception in the callback" + ); + } +); diff --git a/toolkit/components/normandy/test/unit/utils.js b/toolkit/components/normandy/test/unit/utils.js new file mode 100644 index 0000000000..b1273d1d5e --- /dev/null +++ b/toolkit/components/normandy/test/unit/utils.js @@ -0,0 +1,137 @@ +"use strict"; +/* eslint-disable no-unused-vars */ + +// Loaded into the same scope as head_xpc.js +/* import-globals-from head_xpc.js */ + +const { Preferences } = ChromeUtils.importESModule( + "resource://gre/modules/Preferences.sys.mjs" +); +const { HttpServer } = ChromeUtils.importESModule( + "resource://testing-common/httpd.sys.mjs" +); + +const { NormandyApi } = ChromeUtils.importESModule( + "resource://normandy/lib/NormandyApi.sys.mjs" +); +const { NormandyTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/NormandyTestUtils.sys.mjs" +); + +const CryptoHash = Components.Constructor( + "@mozilla.org/security/hash;1", + "nsICryptoHash", + "initWithString" +); +const FileInputStream = Components.Constructor( + "@mozilla.org/network/file-input-stream;1", + "nsIFileInputStream", + "init" +); + +class MockResponse { + constructor(content) { + this.content = content; + } + + async text() { + return this.content; + } + + async json() { + return JSON.parse(this.content); + } +} + +function withServer(server) { + return function (testFunction) { + return NormandyTestUtils.decorate( + NormandyTestUtils.withMockPreferences(), + async function inner({ mockPreferences, ...args }) { + const serverUrl = `http://localhost:${server.identity.primaryPort}`; + mockPreferences.set("app.normandy.api_url", `${serverUrl}/api/v1`); + NormandyApi.clearIndexCache(); + + try { + await testFunction({ ...args, serverUrl, mockPreferences, server }); + } finally { + await new Promise(resolve => server.stop(resolve)); + } + } + ); + }; +} + +function makeScriptServer(scriptPath) { + const server = new HttpServer(); + server.registerContentType("sjs", "sjs"); + server.registerFile("/", do_get_file(scriptPath)); + server.start(-1); + return server; +} + +function withScriptServer(scriptPath) { + return withServer(makeScriptServer(scriptPath)); +} + +function makeMockApiServer(directory) { + const server = new HttpServer(); + server.registerDirectory("/", directory); + + server.setIndexHandler(async function (request, response) { + response.processAsync(); + const dir = request.getProperty("directory"); + const index = dir.clone(); + index.append("index.json"); + + if (!index.exists()) { + response.setStatusLine("1.1", 404, "Not Found"); + response.write(`Cannot find path ${index.path}`); + response.finish(); + return; + } + + try { + const contents = await IOUtils.readUTF8(index.path); + response.write(contents); + } catch (e) { + response.setStatusLine("1.1", 500, "Server error"); + response.write(e.toString()); + } finally { + response.finish(); + } + }); + + server.start(-1); + return server; +} + +function withMockApiServer(apiName = "mock_api") { + return withServer(makeMockApiServer(do_get_file(apiName))); +} + +const CryptoUtils = { + _getHashStringForCrypto(aCrypto) { + // return the two-digit hexadecimal code for a byte + let toHexString = charCode => ("0" + charCode.toString(16)).slice(-2); + + // convert the binary hash data to a hex string. + let binary = aCrypto.finish(false); + let hash = Array.from(binary, c => toHexString(c.charCodeAt(0))); + return hash.join("").toLowerCase(); + }, + + /** + * Get the computed hash for a given file + * @param {nsIFile} file The file to be hashed + * @param {string} [algorithm] The hashing algorithm to use + */ + getFileHash(file, algorithm = "sha256") { + const crypto = CryptoHash(algorithm); + const fis = new FileInputStream(file, -1, -1, false); + crypto.updateFromStream(fis, file.fileSize); + const hash = this._getHashStringForCrypto(crypto); + fis.close(); + return hash; + }, +}; diff --git a/toolkit/components/normandy/test/unit/xpcshell.toml b/toolkit/components/normandy/test/unit/xpcshell.toml new file mode 100644 index 0000000000..af2acb760c --- /dev/null +++ b/toolkit/components/normandy/test/unit/xpcshell.toml @@ -0,0 +1,22 @@ +[DEFAULT] +head = "head_xpc.js" +firefox-appdir = "browser" +support-files = [ + "mock_api/**", + "invalid_recipe_signature_api/**", + "query_server.sjs", + "echo_server.sjs", + "cookie_server.sjs", + "utils.js", +] +tags = "normandy" + +["test_Normandy.js"] + +["test_NormandyApi.js"] + +["test_PrefUtils.js"] + +["test_RecipeRunner.js"] + +["test_addon_unenroll.js"] diff --git a/toolkit/components/normandy/vendor/LICENSE_THIRDPARTY b/toolkit/components/normandy/vendor/LICENSE_THIRDPARTY new file mode 100644 index 0000000000..13a9d68f50 --- /dev/null +++ b/toolkit/components/normandy/vendor/LICENSE_THIRDPARTY @@ -0,0 +1,241 @@ +fbjs@0.8.16 MIT +MIT License + +Copyright (c) 2013-present, Facebook, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +react-dom@15.6.1 BSD-3-Clause +BSD License + +For React software + +Copyright (c) 2013-present, Facebook, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name Facebook nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +object-assign@4.1.1 MIT +The MIT License (MIT) + +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +react@15.6.1 BSD-3-Clause +BSD License + +For React software + +Copyright (c) 2013-present, Facebook, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name Facebook nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +prop-types@15.5.10 BSD-3-Clause +BSD License + +For React software + +Copyright (c) 2013-present, Facebook, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name Facebook nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +create-react-class@15.6.2 MIT +MIT License + +Copyright (c) 2013-present, Facebook, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +mozjexl@1.1.5 MIT +Copyright for portions of mozJexl are held by TechnologyAdvice, 2015 as part of Jexl. +All other copyright for mozJexl are held by the Mozilla Foundation, 2017. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +process@0.11.10 MIT +(The MIT License) + +Copyright (c) 2013 Roman Shtylman <shtylman@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +classnames@2.2.5 MIT +The MIT License (MIT) + +Copyright (c) 2016 Jed Watson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/toolkit/components/normandy/vendor/PropTypes.js b/toolkit/components/normandy/vendor/PropTypes.js new file mode 100644 index 0000000000..b91a68a86f --- /dev/null +++ b/toolkit/components/normandy/vendor/PropTypes.js @@ -0,0 +1 @@ +/* eslint-disable */this.PropTypes=function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={i:d,l:!1,exports:{}};return a[d].call(e.exports,e,e.exports,b),e.l=!0,e.exports}var c={};return b.m=a,b.c=c,b.d=function(a,c,d){b.o(a,c)||Object.defineProperty(a,c,{configurable:!1,enumerable:!0,get:d})},b.n=function(a){var c=a&&a.__esModule?function(){return a['default']}:function(){return a};return b.d(c,'a',c),c},b.o=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)},b.p='',b(b.s=100)}({0:function(a){'use strict';var g=function(){};!1,a.exports=function(h,i,j,a,b,c,d,e){if(g(i),!h){var f;if(void 0===i)f=new Error('Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.');else{var k=[j,a,b,c,d,e],l=0;f=new Error(i.replace(/%s/g,function(){return k[l++]})),f.name='Invariant Violation'}throw f.framesToPop=1,f}}},100:function(a,b,c){a.exports=c(101)()},101:function(a,b,c){'use strict';var d=c(5),e=c(0),f=c(19);a.exports=function(){function a(a,b,c,d,g,h){h===f||e(!1,'Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types')}function b(){return a}a.isRequired=a;var c={array:a,bool:a,func:a,number:a,object:a,string:a,symbol:a,any:a,arrayOf:b,element:a,instanceOf:b,node:a,objectOf:b,oneOf:b,oneOfType:b,shape:b};return c.checkPropTypes=d,c.PropTypes=c,c}},19:function(a){'use strict';a.exports='SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED'},5:function(a){'use strict';function b(a){return function(){return a}}var c=function(){};c.thatReturns=b,c.thatReturnsFalse=b(!1),c.thatReturnsTrue=b(!0),c.thatReturnsNull=b(null),c.thatReturnsThis=function(){return this},c.thatReturnsArgument=function(a){return a},a.exports=c}}); diff --git a/toolkit/components/normandy/vendor/React.js b/toolkit/components/normandy/vendor/React.js new file mode 100644 index 0000000000..4a8468746b --- /dev/null +++ b/toolkit/components/normandy/vendor/React.js @@ -0,0 +1,5 @@ +/* eslint-disable */this.React=function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var n=e&&e.__esModule?function(){return e['default']}:function(){return e};return t.d(n,'a',n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p='',t(t.s=102)}([function(e){'use strict';var t=function(){};!1,e.exports=function(n,o,r,a,i,p,s,e){if(t(o),!n){var d;if(void 0===o)d=new Error('Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.');else{var l=[r,a,i,p,s,e],u=0;d=new Error(o.replace(/%s/g,function(){return l[u++]})),d.name='Invariant Violation'}throw d.framesToPop=1,d}}},function(e,t,n){'use strict';var o=n(5);e.exports=o},,function(e){'use strict';/* +object-assign +(c) Sindre Sorhus +@license MIT +*/function t(e){if(null===e||e===void 0)throw new TypeError('Object.assign cannot be called with null or undefined');return Object(e)}var n=Object.getOwnPropertySymbols,o=Object.prototype.hasOwnProperty,r=Object.prototype.propertyIsEnumerable;e.exports=function(){try{if(!Object.assign)return!1;var e=new String('abc');if(e[5]='de','5'===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},n=0;10>n;n++)t['_'+String.fromCharCode(n)]=n;var o=Object.getOwnPropertyNames(t).map(function(e){return t[e]});if('0123456789'!==o.join(''))return!1;var r={};return['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'].forEach(function(e){r[e]=e}),'abcdefghijklmnopqrst'===Object.keys(Object.assign({},r)).join('')}catch(e){return!1}}()?Object.assign:function(e){for(var a,p,d=t(e),l=1;l<arguments.length;l++){for(var s in a=Object(arguments[l]),a)o.call(a,s)&&(d[s]=a[s]);if(n){p=n(a);for(var u=0;u<p.length;u++)r.call(a,p[u])&&(d[p[u]]=a[p[u]])}}return d}},,function(e){'use strict';function t(e){return function(){return e}}var n=function(){};n.thatReturns=t,n.thatReturnsFalse=t(!1),n.thatReturnsTrue=t(!0),n.thatReturnsNull=t(null),n.thatReturnsThis=function(){return this},n.thatReturnsArgument=function(e){return e},e.exports=n},,function(e,t,n){'use strict';function o(e){return e.ref!==void 0}function r(e){return e.key!==void 0}var a,i,p=n(3),s=n(8),d=n(1),l=n(22),u=Object.prototype.hasOwnProperty,c=n(23),m={key:!0,ref:!0,__self:!0,__source:!0},f=function(e,t,n,o,r,a,i){return!1,{$$typeof:c,type:e,key:t,ref:n,props:i,_owner:a}};f.createElement=function(e,t,n){var a,p={},d=null,l=null,c=null,y=null;if(null!=t)for(a in o(t)&&(l=t.ref),r(t)&&(d=''+t.key),c=void 0===t.__self?null:t.__self,y=void 0===t.__source?null:t.__source,t)u.call(t,a)&&!m.hasOwnProperty(a)&&(p[a]=t[a]);var h=arguments.length-2;if(1==h)p.children=n;else if(1<h){for(var g=Array(h),b=0;b<h;b++)g[b]=arguments[b+2];!1,p.children=g}if(e&&e.defaultProps){var i=e.defaultProps;for(a in i)void 0===p[a]&&(p[a]=i[a])}return f(e,d,l,c,y,s.current,p)},f.createFactory=function(e){var t=f.createElement.bind(null,e);return t.type=e,t},f.cloneAndReplaceKey=function(e,t){var n=f(e.type,t,e.ref,e._self,e._source,e._owner,e.props);return n},f.cloneElement=function(e,t,n){var a,d=p({},e.props),l=e.key,c=e.ref,y=e._self,h=e._source,g=e._owner;if(null!=t){o(t)&&(c=t.ref,g=s.current),r(t)&&(l=''+t.key);var b;for(a in e.type&&e.type.defaultProps&&(b=e.type.defaultProps),t)u.call(t,a)&&!m.hasOwnProperty(a)&&(d[a]=void 0===t[a]&&void 0!==b?b[a]:t[a])}var E=arguments.length-2;if(1==E)d.children=n;else if(1<E){for(var x=Array(E),P=0;P<E;P++)x[P]=arguments[P+2];d.children=x}return f(e.type,l,c,y,h,g,d)},f.isValidElement=function(e){return'object'==typeof e&&null!==e&&e.$$typeof===c},e.exports=f},function(e){'use strict';e.exports={current:null}},,function(e){'use strict';e.exports=function(e){for(var t=arguments.length-1,n='Minified React error #'+e+'; visit http://facebook.github.io/react/docs/error-decoder.html?invariant='+e,o=0;o<t;o++)n+='&args[]='+encodeURIComponent(arguments[o+1]);n+=' for the full message or use the non-minified dev environment for full errors and additional helpful warnings.';var r=new Error(n);throw r.name='Invariant Violation',r.framesToPop=1,r}},,,function(e,t,n){'use strict';var o=n(3),r=n(20),a=n(35),i=n(40),p=n(7),s=n(41),d=n(44),l=n(45),u=n(47),c=p.createElement,m=p.createFactory,f=p.cloneElement;var y={Children:{map:a.map,forEach:a.forEach,count:a.count,toArray:a.toArray,only:u},Component:r.Component,PureComponent:r.PureComponent,createElement:c,cloneElement:f,isValidElement:p.isValidElement,PropTypes:s,createClass:l,createFactory:m,createMixin:function(e){return e},DOM:i,version:d,__spread:o};e.exports=y},function(e){'use strict';!1,e.exports={}},,,,,function(e){'use strict';e.exports='SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED'},function(e,t,n){'use strict';function o(e,t,n){this.props=e,this.context=t,this.refs=l,this.updater=n||s}function r(e,t,n){this.props=e,this.context=t,this.refs=l,this.updater=n||s}function a(){}var i=n(10),p=n(3),s=n(21),d=n(22),l=n(14),u=n(0),c=n(34);o.prototype.isReactComponent={},o.prototype.setState=function(e,t){'object'==typeof e||'function'==typeof e||null==e?void 0:i('85'),this.updater.enqueueSetState(this,e),t&&this.updater.enqueueCallback(this,t,'setState')},o.prototype.forceUpdate=function(e){this.updater.enqueueForceUpdate(this),e&&this.updater.enqueueCallback(this,e,'forceUpdate')};a.prototype=o.prototype,r.prototype=new a,r.prototype.constructor=r,p(r.prototype,o.prototype),r.prototype.isPureReactComponent=!0,e.exports={Component:o,PureComponent:r}},function(e,t,n){'use strict';function o(){}var r=n(1);e.exports={isMounted:function(){return!1},enqueueCallback:function(){},enqueueForceUpdate:function(e){o(e,'forceUpdate')},enqueueReplaceState:function(e){o(e,'replaceState')},enqueueSetState:function(e){o(e,'setState')}}},function(e){'use strict';e.exports=!1},function(e){'use strict';var t='function'==typeof Symbol&&Symbol['for']&&Symbol['for']('react.element')||60103;e.exports=t},,,,,function(e,t,n){'use strict';var o=n(42);e.exports=function(e){return o(e,!1)}},,,,,,function(e){'use strict';e.exports=function(){}},function(e,t,n){'use strict';function o(e){return(''+e).replace(h,'$&/')}function r(e,t){this.func=e,this.context=t,this.count=0}function a(e,t){var n=e.func,o=e.context;n.call(o,t,e.count++)}function i(e,t,n,o){this.result=e,this.keyPrefix=t,this.func=n,this.context=o,this.count=0}function p(e,t,n){var r=e.result,a=e.keyPrefix,i=e.func,p=e.context,d=i.call(p,t,e.count++);Array.isArray(d)?s(d,r,n,c.thatReturnsArgument):null!=d&&(u.isValidElement(d)&&(d=u.cloneAndReplaceKey(d,a+(d.key&&(!t||t.key!==d.key)?o(d.key)+'/':'')+n)),r.push(d))}function s(e,t,n,r,a){var s='';null!=n&&(s=o(n)+'/');var d=i.getPooled(t,s,r,a);m(e,p,d),i.release(d)}function d(){return null}var l=n(36),u=n(7),c=n(5),m=n(37),f=l.twoArgumentPooler,y=l.fourArgumentPooler,h=/\/+/g;r.prototype.destructor=function(){this.func=null,this.context=null,this.count=0},l.addPoolingTo(r,f),i.prototype.destructor=function(){this.result=null,this.keyPrefix=null,this.func=null,this.context=null,this.count=0},l.addPoolingTo(i,y);e.exports={forEach:function(e,t,n){if(null==e)return e;var o=r.getPooled(t,n);m(e,a,o),r.release(o)},map:function(e,t,n){if(null==e)return e;var o=[];return s(e,o,null,t,n),o},mapIntoWithKeyPrefixInternal:s,count:function(e){return m(e,d,null)},toArray:function(e){var t=[];return s(e,t,null,c.thatReturnsArgument),t}}},function(e,t,n){'use strict';var o=n(10),r=n(0),a=function(e){var t=this;if(t.instancePool.length){var n=t.instancePool.pop();return t.call(n,e),n}return new t(e)},i=function(e){var t=this;e instanceof t?void 0:o('25'),e.destructor(),t.instancePool.length<t.poolSize&&t.instancePool.push(e)};e.exports={addPoolingTo:function(e,t){var n=e;return n.instancePool=[],n.getPooled=t||a,n.poolSize||(n.poolSize=10),n.release=i,n},oneArgumentPooler:a,twoArgumentPooler:function(e,t){var n=this;if(n.instancePool.length){var o=n.instancePool.pop();return n.call(o,e,t),o}return new n(e,t)},threeArgumentPooler:function(e,t,n){var o=this;if(o.instancePool.length){var r=o.instancePool.pop();return o.call(r,e,t,n),r}return new o(e,t,n)},fourArgumentPooler:function(e,t,n,o){var r=this;if(r.instancePool.length){var a=r.instancePool.pop();return r.call(a,e,t,n,o),a}return new r(e,t,n,o)}}},function(e,t,n){'use strict';function o(e,t){return e&&'object'==typeof e&&null!=e.key?l.escape(e.key):t.toString(36)}function r(e,t,n,d){var u=typeof e;if(('undefined'==u||'boolean'==u)&&(e=null),null===e||'string'==u||'number'==u||'object'==u&&e.$$typeof===p)return n(d,e,''===t?c+o(e,0):t),1;var f,y,h=0,g=''===t?c:t+m;if(Array.isArray(e))for(var b=0;b<e.length;b++)f=e[b],y=g+o(f,b),h+=r(f,y,n,d);else{var i=s(e);if(i){var E,x=i.call(e);if(i!==e.entries)for(var P=0;!(E=x.next()).done;)f=E.value,y=g+o(f,P++),h+=r(f,y,n,d);else for(var _;!(E=x.next()).done;)_=E.value,_&&(f=_[1],y=g+l.escape(_[0])+m+o(f,0),h+=r(f,y,n,d))}else if('object'==u){var N='',I=e+'';a('31','[object Object]'===I?'object with keys {'+Object.keys(e).join(', ')+'}':I,N)}}return h}var a=n(10),i=n(8),p=n(23),s=n(38),d=n(0),l=n(39),u=n(1),c='.',m=':';e.exports=function(e,t,n){return null==e?0:r(e,'',t,n)}},function(e){'use strict';var t='function'==typeof Symbol&&Symbol.iterator;e.exports=function(e){var n=e&&(t&&e[t]||e['@@iterator']);if('function'==typeof n)return n}},function(e){'use strict';e.exports={escape:function(e){var t=/[=:]/g,n={"=":'=0',":":'=2'},o=(''+e).replace(t,function(e){return n[e]});return'$'+o},unescape:function(e){var t=/(=0|=2)/g,n={"=0":'=',"=2":':'},o='.'===e[0]&&'$'===e[1]?e.substring(2):e.substring(1);return(''+o).replace(t,function(e){return n[e]})}}},function(e,t,n){'use strict';var o=n(7),r=o.createFactory;var a={a:r('a'),abbr:r('abbr'),address:r('address'),area:r('area'),article:r('article'),aside:r('aside'),audio:r('audio'),b:r('b'),base:r('base'),bdi:r('bdi'),bdo:r('bdo'),big:r('big'),blockquote:r('blockquote'),body:r('body'),br:r('br'),button:r('button'),canvas:r('canvas'),caption:r('caption'),cite:r('cite'),code:r('code'),col:r('col'),colgroup:r('colgroup'),data:r('data'),datalist:r('datalist'),dd:r('dd'),del:r('del'),details:r('details'),dfn:r('dfn'),dialog:r('dialog'),div:r('div'),dl:r('dl'),dt:r('dt'),em:r('em'),embed:r('embed'),fieldset:r('fieldset'),figcaption:r('figcaption'),figure:r('figure'),footer:r('footer'),form:r('form'),h1:r('h1'),h2:r('h2'),h3:r('h3'),h4:r('h4'),h5:r('h5'),h6:r('h6'),head:r('head'),header:r('header'),hgroup:r('hgroup'),hr:r('hr'),html:r('html'),i:r('i'),iframe:r('iframe'),img:r('img'),input:r('input'),ins:r('ins'),kbd:r('kbd'),keygen:r('keygen'),label:r('label'),legend:r('legend'),li:r('li'),link:r('link'),main:r('main'),map:r('map'),mark:r('mark'),menu:r('menu'),menuitem:r('menuitem'),meta:r('meta'),meter:r('meter'),nav:r('nav'),noscript:r('noscript'),object:r('object'),ol:r('ol'),optgroup:r('optgroup'),option:r('option'),output:r('output'),p:r('p'),param:r('param'),picture:r('picture'),pre:r('pre'),progress:r('progress'),q:r('q'),rp:r('rp'),rt:r('rt'),ruby:r('ruby'),s:r('s'),samp:r('samp'),script:r('script'),section:r('section'),select:r('select'),small:r('small'),source:r('source'),span:r('span'),strong:r('strong'),style:r('style'),sub:r('sub'),summary:r('summary'),sup:r('sup'),table:r('table'),tbody:r('tbody'),td:r('td'),textarea:r('textarea'),tfoot:r('tfoot'),th:r('th'),thead:r('thead'),time:r('time'),title:r('title'),tr:r('tr'),track:r('track'),u:r('u'),ul:r('ul'),var:r('var'),video:r('video'),wbr:r('wbr'),circle:r('circle'),clipPath:r('clipPath'),defs:r('defs'),ellipse:r('ellipse'),g:r('g'),image:r('image'),line:r('line'),linearGradient:r('linearGradient'),mask:r('mask'),path:r('path'),pattern:r('pattern'),polygon:r('polygon'),polyline:r('polyline'),radialGradient:r('radialGradient'),rect:r('rect'),stop:r('stop'),svg:r('svg'),text:r('text'),tspan:r('tspan')};e.exports=a},function(e,t,n){'use strict';var o=n(7),r=o.isValidElement,a=n(28);e.exports=a(r)},function(e,t,n){'use strict';var o=n(5),r=n(0),a=n(1),p=n(19),i=n(43);e.exports=function(e,t){function n(e){var t=e&&(b&&e[b]||e[E]);if('function'==typeof t)return t}function s(e,t){return e===t?0!==e||1/e==1/t:e!==e&&t!==t}function d(e){this.message=e,this.stack=''}function l(e){function n(n,o,a,i,s,l,u){if(i=i||x,l=l||a,u!==p)if(t)r(!1,'Calling PropTypes validators directly is not supported by the `prop-types` package. Use `PropTypes.checkPropTypes()` to call them. Read more at http://fb.me/use-check-prop-types');else;return null==o[a]?n?null===o[a]?new d('The '+s+' `'+l+'` is marked as required '+('in `'+i+'`, but its value is `null`.')):new d('The '+s+' `'+l+'` is marked as required in '+('`'+i+'`, but its value is `undefined`.')):null:e(o,a,i,s,l)}var o=n.bind(null,!1);return o.isRequired=n.bind(null,!0),o}function u(e){return l(function(t,n,o,r,a){var i=t[n],p=f(i);if(p!==e){var s=y(i);return new d('Invalid '+r+' `'+a+'` of type '+('`'+s+'` supplied to `'+o+'`, expected ')+('`'+e+'`.'))}return null})}function c(t){switch(typeof t){case'number':case'string':case'undefined':return!0;case'boolean':return!t;case'object':if(Array.isArray(t))return t.every(c);if(null===t||e(t))return!0;var o=n(t);if(o){var r,a=o.call(t);if(o!==t.entries){for(;!(r=a.next()).done;)if(!c(r.value))return!1;}else for(;!(r=a.next()).done;){var i=r.value;if(i&&!c(i[1]))return!1}}else return!1;return!0;default:return!1;}}function m(e,t){return'symbol'===e||'Symbol'===t['@@toStringTag']||'function'==typeof Symbol&&t instanceof Symbol}function f(e){var t=typeof e;return Array.isArray(e)?'array':e instanceof RegExp?'object':m(t,e)?'symbol':t}function y(e){if('undefined'==typeof e||null===e)return''+e;var t=f(e);if('object'===t){if(e instanceof Date)return'date';if(e instanceof RegExp)return'regexp'}return t}function h(e){var t=y(e);return'array'===t||'object'===t?'an '+t:'boolean'===t||'date'===t||'regexp'===t?'a '+t:t}function g(e){return e.constructor&&e.constructor.name?e.constructor.name:x}var b='function'==typeof Symbol&&Symbol.iterator,E='@@iterator',x='<<anonymous>>',P={array:u('array'),bool:u('boolean'),func:u('function'),number:u('number'),object:u('object'),string:u('string'),symbol:u('symbol'),any:function(){return l(o.thatReturnsNull)}(),arrayOf:function(e){return l(function(t,n,o,r,a){if('function'!=typeof e)return new d('Property `'+a+'` of component `'+o+'` has invalid PropType notation inside arrayOf.');var s=t[n];if(!Array.isArray(s)){var l=f(s);return new d('Invalid '+r+' `'+a+'` of type '+('`'+l+'` supplied to `'+o+'`, expected an array.'))}for(var u,c=0;c<s.length;c++)if(u=e(s,c,o,r,a+'['+c+']',p),u instanceof Error)return u;return null})},element:function(){return l(function(t,n,o,r,a){var i=t[n];if(!e(i)){var p=f(i);return new d('Invalid '+r+' `'+a+'` of type '+('`'+p+'` supplied to `'+o+'`, expected a single ReactElement.'))}return null})}(),instanceOf:function(e){return l(function(t,n,o,r,a){if(!(t[n]instanceof e)){var i=e.name||x,p=g(t[n]);return new d('Invalid '+r+' `'+a+'` of type '+('`'+p+'` supplied to `'+o+'`, expected ')+('instance of `'+i+'`.'))}return null})},node:function(){return l(function(e,t,n,o,r){return c(e[t])?null:new d('Invalid '+o+' `'+r+'` supplied to '+('`'+n+'`, expected a ReactNode.'))})}(),objectOf:function(e){return l(function(t,n,o,r,a){if('function'!=typeof e)return new d('Property `'+a+'` of component `'+o+'` has invalid PropType notation inside objectOf.');var i=t[n],s=f(i);if('object'!==s)return new d('Invalid '+r+' `'+a+'` of type '+('`'+s+'` supplied to `'+o+'`, expected an object.'));for(var l in i)if(i.hasOwnProperty(l)){var u=e(i,l,o,r,a+'.'+l,p);if(u instanceof Error)return u}return null})},oneOf:function(e){return Array.isArray(e)?l(function(t,n,o,r,a){for(var p=t[n],l=0;l<e.length;l++)if(s(p,e[l]))return null;var i=JSON.stringify(e);return new d('Invalid '+r+' `'+a+'` of value `'+p+'` '+('supplied to `'+o+'`, expected one of '+i+'.'))}):(void 0,o.thatReturnsNull)},oneOfType:function(e){if(!Array.isArray(e))return void 0,o.thatReturnsNull;for(var t,n=0;n<e.length;n++)if(t=e[n],'function'!=typeof t)return a(!1,'Invalid argument supplid to oneOfType. Expected an array of check functions, but received %s at index %s.',h(t),n),o.thatReturnsNull;return l(function(t,n,o,r,a){for(var s,l=0;l<e.length;l++)if(s=e[l],null==s(t,n,o,r,a,p))return null;return new d('Invalid '+r+' `'+a+'` supplied to '+('`'+o+'`.'))})},shape:function(e){return l(function(t,n,o,r,a){var i=t[n],s=f(i);if('object'!==s)return new d('Invalid '+r+' `'+a+'` of type `'+s+'` '+('supplied to `'+o+'`, expected `object`.'));for(var l in e){var u=e[l];if(u){var c=u(i,l,o,r,a+'.'+l,p);if(c)return c}}return null})}};return d.prototype=Error.prototype,P.checkPropTypes=i,P.PropTypes=P,P}},function(e){'use strict';e.exports=function(){}},function(e){'use strict';e.exports='15.6.1'},function(e,t,n){'use strict';var o=n(20),r=o.Component,a=n(7),i=a.isValidElement,p=n(21),s=n(46);e.exports=s(r,i,p)},function(e,t,n){'use strict';function o(e){return e}var r=n(3),a=n(14),i=n(0);var p,s='mixins';p={},e.exports=function(e,t,n){function p(e,t){var n=g.hasOwnProperty(t)?g[t]:null;P.hasOwnProperty(t)&&i('OVERRIDE_BASE'===n,'ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.',t),e&&i('DEFINE_MANY'===n||'DEFINE_MANY_MERGED'===n,'ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.',t)}function d(e,n){if(!n){return}i('function'!=typeof n,'ReactClass: You\'re attempting to use a component class or function as a mixin. Instead, just use a regular object.'),i(!t(n),'ReactClass: You\'re attempting to use a component as a mixin. Instead, just use a regular object.');var o=e.prototype,r=o.__reactAutoBindPairs;for(var a in n.hasOwnProperty(s)&&b.mixins(e,n.mixins),n)if(n.hasOwnProperty(a)&&a!=s){var d=n[a],l=o.hasOwnProperty(a);if(p(l,a),b.hasOwnProperty(a))b[a](e,d);else{var u=g.hasOwnProperty(a),f='function'==typeof d&&!u&&!l&&!1!==n.autobind;if(f)r.push(a,d),o[a]=d;else if(l){var y=g[a];i(u&&('DEFINE_MANY_MERGED'===y||'DEFINE_MANY'===y),'ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.',y,a),'DEFINE_MANY_MERGED'===y?o[a]=c(o[a],d):'DEFINE_MANY'===y&&(o[a]=m(o[a],d))}else o[a]=d,!1}}}function l(e,t){if(t)for(var n in t){var o=t[n];if(t.hasOwnProperty(n)){i(!(n in b),'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.',n);i(!(n in e),'ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.',n),e[n]=o}}}function u(e,t){for(var n in i(e&&t&&'object'==typeof e&&'object'==typeof t,'mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.'),t)t.hasOwnProperty(n)&&(i(void 0===e[n],'mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.',n),e[n]=t[n]);return e}function c(e,t){return function(){var n=e.apply(this,arguments),o=t.apply(this,arguments);if(null==n)return o;if(null==o)return n;var r={};return u(r,n),u(r,o),r}}function m(e,t){return function(){e.apply(this,arguments),t.apply(this,arguments)}}function f(e,t){var n=t.bind(e);return n}function y(e){for(var t=e.__reactAutoBindPairs,n=0;n<t.length;n+=2){var o=t[n],r=t[n+1];e[o]=f(e,r)}}var h=[],g={mixins:'DEFINE_MANY',statics:'DEFINE_MANY',propTypes:'DEFINE_MANY',contextTypes:'DEFINE_MANY',childContextTypes:'DEFINE_MANY',getDefaultProps:'DEFINE_MANY_MERGED',getInitialState:'DEFINE_MANY_MERGED',getChildContext:'DEFINE_MANY_MERGED',render:'DEFINE_ONCE',componentWillMount:'DEFINE_MANY',componentDidMount:'DEFINE_MANY',componentWillReceiveProps:'DEFINE_MANY',shouldComponentUpdate:'DEFINE_ONCE',componentWillUpdate:'DEFINE_MANY',componentDidUpdate:'DEFINE_MANY',componentWillUnmount:'DEFINE_MANY',updateComponent:'OVERRIDE_BASE'},b={displayName:function(e,t){e.displayName=t},mixins:function(e,t){if(t)for(var n=0;n<t.length;n++)d(e,t[n])},childContextTypes:function(e,t){!1,e.childContextTypes=r({},e.childContextTypes,t)},contextTypes:function(e,t){!1,e.contextTypes=r({},e.contextTypes,t)},getDefaultProps:function(e,t){e.getDefaultProps=e.getDefaultProps?c(e.getDefaultProps,t):t},propTypes:function(e,t){!1,e.propTypes=r({},e.propTypes,t)},statics:function(e,t){l(e,t)},autobind:function(){}},E={componentDidMount:function(){this.__isMounted=!0}},x={componentWillUnmount:function(){this.__isMounted=!1}},P={replaceState:function(e,t){this.updater.enqueueReplaceState(this,e,t)},isMounted:function(){return!1,!!this.__isMounted}},_=function(){};return r(_.prototype,e.prototype,P),function(e){var t=o(function(e,o,r){!1,this.__reactAutoBindPairs.length&&y(this),this.props=e,this.context=o,this.refs=a,this.updater=r||n,this.state=null;var p=this.getInitialState?this.getInitialState():null;!1,i('object'==typeof p&&!Array.isArray(p),'%s.getInitialState(): must return an object or null',t.displayName||'ReactCompositeComponent'),this.state=p});for(var r in t.prototype=new _,t.prototype.constructor=t,t.prototype.__reactAutoBindPairs=[],h.forEach(d.bind(null,t)),d(t,E),d(t,e),d(t,x),t.getDefaultProps&&(t.defaultProps=t.getDefaultProps()),!1,i(t.prototype.render,'createClass(...): Class specification must implement a `render` method.'),!1,g)t.prototype[r]||(t.prototype[r]=null);return t}}},function(e,t,n){'use strict';var o=n(10),r=n(7),a=n(0);e.exports=function(e){return r.isValidElement(e)?void 0:o('143'),e}},,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,function(e,t,n){'use strict';e.exports=n(13)}]); diff --git a/toolkit/components/normandy/vendor/ReactDOM.js b/toolkit/components/normandy/vendor/ReactDOM.js new file mode 100644 index 0000000000..9c046de6be --- /dev/null +++ b/toolkit/components/normandy/vendor/ReactDOM.js @@ -0,0 +1,18 @@ +/* eslint-disable */this.ReactDOM=function(e){function t(o){if(n[o])return n[o].exports;var a=n[o]={i:o,l:!1,exports:{}};return e[o].call(a.exports,a,a.exports,t),a.l=!0,a.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var n=e&&e.__esModule?function(){return e['default']}:function(){return e};return t.d(n,'a',n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p='',t(t.s=103)}([function(e){'use strict';var t=function(){};!1,e.exports=function(n,o,r,a,i,s,d,e){if(t(o),!n){var p;if(void 0===o)p=new Error('Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.');else{var l=[r,a,i,s,d,e],u=0;p=new Error(o.replace(/%s/g,function(){return l[u++]})),p.name='Invariant Violation'}throw p.framesToPop=1,p}}},function(e,t,n){'use strict';var o=n(5);e.exports=o},function(e){'use strict';e.exports=function(e){for(var t=arguments.length-1,n='Minified React error #'+e+'; visit http://facebook.github.io/react/docs/error-decoder.html?invariant='+e,o=0;o<t;o++)n+='&args[]='+encodeURIComponent(arguments[o+1]);n+=' for the full message or use the non-minified dev environment for full errors and additional helpful warnings.';var a=new Error(n);throw a.name='Invariant Violation',a.framesToPop=1,a}},function(e){'use strict';/* +object-assign +(c) Sindre Sorhus +@license MIT +*/function t(e){if(null===e||e===void 0)throw new TypeError('Object.assign cannot be called with null or undefined');return Object(e)}var n=Object.getOwnPropertySymbols,o=Object.prototype.hasOwnProperty,a=Object.prototype.propertyIsEnumerable;e.exports=function(){try{if(!Object.assign)return!1;var e=new String('abc');if(e[5]='de','5'===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},n=0;10>n;n++)t['_'+String.fromCharCode(n)]=n;var o=Object.getOwnPropertyNames(t).map(function(e){return t[e]});if('0123456789'!==o.join(''))return!1;var a={};return['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'].forEach(function(e){a[e]=e}),'abcdefghijklmnopqrst'===Object.keys(Object.assign({},a)).join('')}catch(e){return!1}}()?Object.assign:function(e){for(var r=t(e),d=1,s,p;d<arguments.length;d++){for(var l in s=Object(arguments[d]),s)o.call(s,l)&&(r[l]=s[l]);if(n){p=n(s);for(var u=0;u<p.length;u++)a.call(s,p[u])&&(r[p[u]]=s[p[u]])}}return r}},function(e,t,n){'use strict';function o(e,t){return 1===e.nodeType&&e.getAttribute(c)===t+''||8===e.nodeType&&e.nodeValue===' react-text: '+t+' '||8===e.nodeType&&e.nodeValue===' react-empty: '+t+' '}function a(e){for(var t;t=e._renderedComponent;)e=t;return e}function r(e,t){var n=a(e);n._hostNode=t,t[h]=n}function i(e,t){if(!(e._flags&m.hasCachedChildNodes)){var n=e._renderedChildren,i=t.firstChild;outer:for(var d in n)if(n.hasOwnProperty(d)){var p=n[d],l=a(p)._domID;if(0!==l){for(;null!==i;i=i.nextSibling)if(o(i,l)){r(p,i);continue outer}s('32',l)}}e._flags|=m.hasCachedChildNodes}}function d(e){if(e[h])return e[h];for(var t=[];!e[h];)if(t.push(e),e.parentNode)e=e.parentNode;else return null;for(var n,o;e&&(o=e[h]);e=t.pop())n=o,t.length&&i(o,e);return n}var s=n(2),p=n(16),l=n(66),u=n(0),c=p.ID_ATTRIBUTE_NAME,m=l,h='__reactInternalInstance$'+Math.random().toString(36).slice(2);e.exports={getClosestInstanceFromNode:d,getInstanceFromNode:function(e){var t=d(e);return null!=t&&t._hostNode===e?t:null},getNodeFromInstance:function(e){if(void 0===e._hostNode?s('33'):void 0,e._hostNode)return e._hostNode;for(var t=[];!e._hostNode;)t.push(e),e._hostParent?void 0:s('34'),e=e._hostParent;for(;t.length;e=t.pop())i(e,e._hostNode);return e._hostNode},precacheChildNodes:i,precacheNode:r,uncacheNode:function(e){var t=e._hostNode;t&&(delete t[h],e._hostNode=null)}}},function(e){'use strict';function t(e){return function(){return e}}var n=function(){};n.thatReturns=t,n.thatReturnsFalse=t(!1),n.thatReturnsTrue=t(!0),n.thatReturnsNull=t(null),n.thatReturnsThis=function(){return this},n.thatReturnsArgument=function(e){return e},e.exports=n},function(e){'use strict';var t=!!('undefined'!=typeof window&&window.document&&window.document.createElement),n={canUseDOM:t,canUseWorkers:'undefined'!=typeof Worker,canUseEventListeners:t&&!!(window.addEventListener||window.attachEvent),canUseViewport:t&&!!window.screen,isInWorker:!t};e.exports=n},function(e,t,n){'use strict';function o(e){return e.ref!==void 0}function a(e){return e.key!==void 0}var r=n(3),d=n(8),i=n(1),s=n(22),p=Object.prototype.hasOwnProperty,l=n(23),u={key:!0,ref:!0,__self:!0,__source:!0},c=function(e,t,n,o,a,r,i){return!1,{$$typeof:l,type:e,key:t,ref:n,props:i,_owner:r}},m,h;c.createElement=function(e,t,n){var r={},s=null,l=null,m=null,h=null,g;if(null!=t)for(g in o(t)&&(l=t.ref),a(t)&&(s=''+t.key),m=void 0===t.__self?null:t.__self,h=void 0===t.__source?null:t.__source,t)p.call(t,g)&&!u.hasOwnProperty(g)&&(r[g]=t[g]);var f=arguments.length-2;if(1==f)r.children=n;else if(1<f){for(var y=Array(f),_=0;_<f;_++)y[_]=arguments[_+2];!1,r.children=y}if(e&&e.defaultProps){var i=e.defaultProps;for(g in i)void 0===r[g]&&(r[g]=i[g])}return c(e,s,l,m,h,d.current,r)},c.createFactory=function(e){var t=c.createElement.bind(null,e);return t.type=e,t},c.cloneAndReplaceKey=function(e,t){var n=c(e.type,t,e.ref,e._self,e._source,e._owner,e.props);return n},c.cloneElement=function(e,t,n){var s=r({},e.props),l=e.key,m=e.ref,h=e._self,g=e._source,f=e._owner,y;if(null!=t){o(t)&&(m=t.ref,f=d.current),a(t)&&(l=''+t.key);var _;for(y in e.type&&e.type.defaultProps&&(_=e.type.defaultProps),t)p.call(t,y)&&!u.hasOwnProperty(y)&&(s[y]=void 0===t[y]&&void 0!==_?_[y]:t[y])}var C=arguments.length-2;if(1==C)s.children=n;else if(1<C){for(var b=Array(C),E=0;E<C;E++)b[E]=arguments[E+2];s.children=b}return c(e.type,l,m,h,g,f,s)},c.isValidElement=function(e){return'object'==typeof e&&null!==e&&e.$$typeof===l},e.exports=c},function(e){'use strict';e.exports={current:null}},function(e){'use strict';e.exports={debugTool:null}},function(e){'use strict';e.exports=function(e){for(var t=arguments.length-1,n='Minified React error #'+e+'; visit http://facebook.github.io/react/docs/error-decoder.html?invariant='+e,o=0;o<t;o++)n+='&args[]='+encodeURIComponent(arguments[o+1]);n+=' for the full message or use the non-minified dev environment for full errors and additional helpful warnings.';var a=new Error(n);throw a.name='Invariant Violation',a.framesToPop=1,a}},function(e,t,n){'use strict';function o(){x.ReactReconcileTransaction&&E?void 0:s('123')}function a(){this.reinitializeTransaction(),this.dirtyComponentsLength=null,this.callbackQueue=l.getPooled(),this.reconcileTransaction=x.ReactReconcileTransaction.getPooled(!0)}function r(e,t){return e._mountOrder-t._mountOrder}function i(e){var t=e.dirtyComponentsLength;t===f.length?void 0:s('124',t,f.length),f.sort(r),y++;for(var n=0;n<t;n++){var o=f[n],a=o._pendingCallbacks;o._pendingCallbacks=null;var i;if(c.logTopLevelRenders){var d=o;o._currentElement.type.isReactTopLevelWrapper&&(d=o._renderedComponent),i='React update: '+d.getName(),console.time(i)}if(m.performUpdateIfNecessary(o,e.reconcileTransaction,y),i&&console.timeEnd(i),a)for(var p=0;p<a.length;p++)e.callbackQueue.enqueue(a[p],o.getPublicInstance())}}function d(e){return o(),E.isBatchingUpdates?void(f.push(e),null==e._updateBatchNumber&&(e._updateBatchNumber=y+1)):void E.batchedUpdates(d,e)}var s=n(2),p=n(3),l=n(70),u=n(15),c=n(71),m=n(17),h=n(29),g=n(0),f=[],y=0,_=l.getPooled(),C=!1,E=null,b=[{initialize:function(){this.dirtyComponentsLength=f.length},close:function(){this.dirtyComponentsLength===f.length?f.length=0:(f.splice(0,this.dirtyComponentsLength),v())}},{initialize:function(){this.callbackQueue.reset()},close:function(){this.callbackQueue.notifyAll()}}];p(a.prototype,h,{getTransactionWrappers:function(){return b},destructor:function(){this.dirtyComponentsLength=null,l.release(this.callbackQueue),this.callbackQueue=null,x.ReactReconcileTransaction.release(this.reconcileTransaction),this.reconcileTransaction=null},perform:function(e,t,n){return h.perform.call(this,this.reconcileTransaction.perform,this.reconcileTransaction,e,t,n)}}),u.addPoolingTo(a);var v=function(){for(;f.length||C;){if(f.length){var e=a.getPooled();e.perform(i,null,e),a.release(e)}if(C){C=!1;var t=_;_=l.getPooled(),t.notifyAll(),l.release(t)}}},x={ReactReconcileTransaction:null,batchedUpdates:function(t,n,a,r,i,d){return o(),E.batchedUpdates(t,n,a,r,i,d)},enqueueUpdate:d,flushBatchedUpdates:v,injection:{injectReconcileTransaction:function(e){e?void 0:s('126'),x.ReactReconcileTransaction=e},injectBatchingStrategy:function(e){e?void 0:s('127'),'function'==typeof e.batchedUpdates?void 0:s('128'),'boolean'==typeof e.isBatchingUpdates?void 0:s('129'),E=e}},asap:function(e,t){E.isBatchingUpdates?void 0:s('125'),_.enqueue(e,t),C=!0}};e.exports=x},function(e,t,n){'use strict';function o(e,t,n,o){!1,this.dispatchConfig=e,this._targetInst=t,this.nativeEvent=n;var a=this.constructor.Interface;for(var r in a)if(a.hasOwnProperty(r)){var d=a[r];d?this[r]=d(n):'target'==r?this.target=o:this[r]=n[r]}var s=null==n.defaultPrevented?!1===n.returnValue:n.defaultPrevented;return this.isDefaultPrevented=s?i.thatReturnsTrue:i.thatReturnsFalse,this.isPropagationStopped=i.thatReturnsFalse,this}var a=n(3),r=n(15),i=n(5),d=n(1),s='function'==typeof Proxy,p=['dispatchConfig','_targetInst','nativeEvent','isDefaultPrevented','isPropagationStopped','_dispatchListeners','_dispatchInstances'],l={type:null,target:null,currentTarget:i.thatReturnsNull,eventPhase:null,bubbles:null,cancelable:null,timeStamp:function(e){return e.timeStamp||Date.now()},defaultPrevented:null,isTrusted:null};a(o.prototype,{preventDefault:function(){this.defaultPrevented=!0;var e=this.nativeEvent;e&&(e.preventDefault?e.preventDefault():'unknown'!=typeof e.returnValue&&(e.returnValue=!1),this.isDefaultPrevented=i.thatReturnsTrue)},stopPropagation:function(){var e=this.nativeEvent;e&&(e.stopPropagation?e.stopPropagation():'unknown'!=typeof e.cancelBubble&&(e.cancelBubble=!0),this.isPropagationStopped=i.thatReturnsTrue)},persist:function(){this.isPersistent=i.thatReturnsTrue},isPersistent:i.thatReturnsFalse,destructor:function(){var e=this.constructor.Interface;for(var t in e)this[t]=null;for(var n=0;n<p.length;n++)this[p[n]]=null}}),o.Interface=l,!1,o.augmentClass=function(e,t){var n=this,o=function(){};o.prototype=n.prototype;var i=new o;a(i,e.prototype),e.prototype=i,e.prototype.constructor=e,e.Interface=a({},n.Interface,t),e.augmentClass=n.augmentClass,r.addPoolingTo(e,r.fourArgumentPooler)},r.addPoolingTo(o,r.fourArgumentPooler),e.exports=o},function(e,t,n){'use strict';var o=n(3),a=n(20),r=n(35),i=n(40),d=n(7),s=n(41),p=n(44),l=n(45),u=n(47),c=d.createElement,m=d.createFactory,h=d.cloneElement;var g={Children:{map:r.map,forEach:r.forEach,count:r.count,toArray:r.toArray,only:u},Component:a.Component,PureComponent:a.PureComponent,createElement:c,cloneElement:h,isValidElement:d.isValidElement,PropTypes:s,createClass:l,createFactory:m,createMixin:function(e){return e},DOM:i,version:p,__spread:o};e.exports=g},function(e){'use strict';!1,e.exports={}},function(e,t,n){'use strict';var o=n(2),a=n(0),r=function(e){var t=this;if(t.instancePool.length){var n=t.instancePool.pop();return t.call(n,e),n}return new t(e)},i=function(e){var t=this;e instanceof t?void 0:o('25'),e.destructor(),t.instancePool.length<t.poolSize&&t.instancePool.push(e)};e.exports={addPoolingTo:function(e,t){var n=e;return n.instancePool=[],n.getPooled=t||r,n.poolSize||(n.poolSize=10),n.release=i,n},oneArgumentPooler:r,twoArgumentPooler:function(e,t){var n=this;if(n.instancePool.length){var o=n.instancePool.pop();return n.call(o,e,t),o}return new n(e,t)},threeArgumentPooler:function(e,t,n){var o=this;if(o.instancePool.length){var a=o.instancePool.pop();return o.call(a,e,t,n),a}return new o(e,t,n)},fourArgumentPooler:function(e,t,n,o){var a=this;if(a.instancePool.length){var r=a.instancePool.pop();return a.call(r,e,t,n,o),r}return new a(e,t,n,o)}}},function(e,t,n){'use strict';function o(e,t){return(e&t)===t}var a=n(2),r=n(0),i={MUST_USE_PROPERTY:1,HAS_BOOLEAN_VALUE:4,HAS_NUMERIC_VALUE:8,HAS_POSITIVE_NUMERIC_VALUE:24,HAS_OVERLOADED_BOOLEAN_VALUE:32,injectDOMPropertyConfig:function(e){var t=i,n=e.Properties||{},r=e.DOMAttributeNamespaces||{},d=e.DOMAttributeNames||{},p=e.DOMPropertyNames||{},l=e.DOMMutationMethods||{};for(var u in e.isCustomAttribute&&s._isCustomAttributeFunctions.push(e.isCustomAttribute),n){s.properties.hasOwnProperty(u)?a('48',u):void 0;var c=u.toLowerCase(),m=n[u],h={attributeName:c,attributeNamespace:null,propertyName:u,mutationMethod:null,mustUseProperty:o(m,t.MUST_USE_PROPERTY),hasBooleanValue:o(m,t.HAS_BOOLEAN_VALUE),hasNumericValue:o(m,t.HAS_NUMERIC_VALUE),hasPositiveNumericValue:o(m,t.HAS_POSITIVE_NUMERIC_VALUE),hasOverloadedBooleanValue:o(m,t.HAS_OVERLOADED_BOOLEAN_VALUE)};if(1>=h.hasBooleanValue+h.hasNumericValue+h.hasOverloadedBooleanValue?void 0:a('50',u),!1,d.hasOwnProperty(u)){var g=d[u];h.attributeName=g,!1}r.hasOwnProperty(u)&&(h.attributeNamespace=r[u]),p.hasOwnProperty(u)&&(h.propertyName=p[u]),l.hasOwnProperty(u)&&(h.mutationMethod=l[u]),s.properties[u]=h}}},d=':A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD',s={ID_ATTRIBUTE_NAME:'data-reactid',ROOT_ATTRIBUTE_NAME:'data-reactroot',ATTRIBUTE_NAME_START_CHAR:d,ATTRIBUTE_NAME_CHAR:d+'\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040',properties:{},getPossibleStandardName:null,_isCustomAttributeFunctions:[],isCustomAttribute:function(e){for(var t=0,n;t<s._isCustomAttributeFunctions.length;t++)if(n=s._isCustomAttributeFunctions[t],n(e))return!0;return!1},injection:i};e.exports=s},function(e,t,n){'use strict';function o(){a.attachRefs(this,this._currentElement)}var a=n(112),r=n(9),i=n(1);e.exports={mountComponent:function(e,t,n,a,r,i){var d=e.mountComponent(t,n,a,r,i);return e._currentElement&&null!=e._currentElement.ref&&t.getReactMountReady().enqueue(o,e),!1,d},getHostNode:function(e){return e.getHostNode()},unmountComponent:function(e,t){!1,a.detachRefs(e,e._currentElement),e.unmountComponent(t),!1},receiveComponent:function(e,t,n,r){var i=e._currentElement;if(t!==i||r!==e._context){var d=a.shouldUpdateRefs(i,t);d&&a.detachRefs(e,i),e.receiveComponent(t,n,r),d&&e._currentElement&&null!=e._currentElement.ref&&n.getReactMountReady().enqueue(o,e),!1}},performUpdateIfNecessary:function(e,t,n){return e._updateBatchNumber===n?void(!1,e.performUpdateIfNecessary(t),!1):void void 0}}},function(e,t,n){'use strict';function o(e){if(l){var t=e.node,n=e.children;if(n.length)for(var o=0;o<n.length;o++)u(t,n[o],null);else null==e.html?null!=e.text&&p(t,e.text):d(t,e.html)}}function a(){return this.node.nodeName}function r(e){return{node:e,children:[],html:null,text:null,toString:a}}var i=n(55),d=n(31),s=n(56),p=n(75),l='undefined'!=typeof document&&'number'==typeof document.documentMode||'undefined'!=typeof navigator&&'string'==typeof navigator.userAgent&&/\bEdge\/\d/.test(navigator.userAgent),u=s(function(e,t,n){t.node.nodeType===11||t.node.nodeType===1&&'object'===t.node.nodeName.toLowerCase()&&(null==t.node.namespaceURI||t.node.namespaceURI===i.html)?(o(t),e.insertBefore(t.node,n)):(e.insertBefore(t.node,n),o(t))});r.insertTreeBefore=u,r.replaceChildWithTree=function(e,t){e.parentNode.replaceChild(t.node,e),o(t)},r.queueChild=function(e,t){l?e.children.push(t):e.node.appendChild(t.node)},r.queueHTML=function(e,t){l?e.html=t:d(e.node,t)},r.queueText=function(e,t){l?e.text=t:p(e.node,t)},e.exports=r},function(e){'use strict';e.exports='SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED'},function(e,t,n){'use strict';function o(e,t,n){this.props=e,this.context=t,this.refs=l,this.updater=n||s}function a(e,t,n){this.props=e,this.context=t,this.refs=l,this.updater=n||s}function r(){}var i=n(10),d=n(3),s=n(21),p=n(22),l=n(14),u=n(0),c=n(34);o.prototype.isReactComponent={},o.prototype.setState=function(e,t){'object'==typeof e||'function'==typeof e||null==e?void 0:i('85'),this.updater.enqueueSetState(this,e),t&&this.updater.enqueueCallback(this,t,'setState')},o.prototype.forceUpdate=function(e){this.updater.enqueueForceUpdate(this),e&&this.updater.enqueueCallback(this,e,'forceUpdate')};r.prototype=o.prototype,a.prototype=new r,a.prototype.constructor=a,d(a.prototype,o.prototype),a.prototype.isPureReactComponent=!0,e.exports={Component:o,PureComponent:a}},function(e,t,n){'use strict';function o(){}var a=n(1);e.exports={isMounted:function(){return!1},enqueueCallback:function(){},enqueueForceUpdate:function(e){o(e,'forceUpdate')},enqueueReplaceState:function(e){o(e,'replaceState')},enqueueSetState:function(e){o(e,'setState')}}},function(e){'use strict';e.exports=!1},function(e){'use strict';var t='function'==typeof Symbol&&Symbol['for']&&Symbol['for']('react.element')||60103;e.exports=t},function(e,t,n){'use strict';function o(e,t,n){var o=t.dispatchConfig.phasedRegistrationNames[n];return h(e,o)}function a(e,t,n){var a=o(e,n,t);a&&(n._dispatchListeners=u(n._dispatchListeners,a),n._dispatchInstances=u(n._dispatchInstances,e))}function r(e){e&&e.dispatchConfig.phasedRegistrationNames&&l.traverseTwoPhase(e._targetInst,a,e)}function i(e){if(e&&e.dispatchConfig.phasedRegistrationNames){var t=e._targetInst,n=t?l.getParentInstance(t):null;l.traverseTwoPhase(n,a,e)}}function d(e,t,n){if(n&&n.dispatchConfig.registrationName){var o=n.dispatchConfig.registrationName,a=h(e,o);a&&(n._dispatchListeners=u(n._dispatchListeners,a),n._dispatchInstances=u(n._dispatchInstances,e))}}function s(e){e&&e.dispatchConfig.registrationName&&d(e._targetInst,null,e)}var p=n(25),l=n(49),u=n(67),c=n(68),m=n(1),h=p.getListener;e.exports={accumulateTwoPhaseDispatches:function(e){c(e,r)},accumulateTwoPhaseDispatchesSkipTarget:function(e){c(e,i)},accumulateDirectDispatches:function(e){c(e,s)},accumulateEnterLeaveDispatches:function(e,t,n,o){l.traverseEnterLeave(n,o,d,e,t)}}},function(e,t,n){'use strict';function o(e){return'button'===e||'input'===e||'select'===e||'textarea'===e}function a(e,t,n){return('onClick'===e||'onClickCapture'===e||'onDoubleClick'===e||'onDoubleClickCapture'===e||'onMouseDown'===e||'onMouseDownCapture'===e||'onMouseMove'===e||'onMouseMoveCapture'===e||'onMouseUp'===e||'onMouseUpCapture'===e)&&!!(n.disabled&&o(t))}var r=n(2),d=n(48),i=n(49),s=n(50),p=n(67),l=n(68),u=n(0),c={},m=null,h=function(e,t){e&&(i.executeDispatchesInOrder(e,t),!e.isPersistent()&&e.constructor.release(e))},g=function(t){return h(t,!0)},f=function(t){return h(t,!1)},y=function(e){return'.'+e._rootNodeID},_={injection:{injectEventPluginOrder:d.injectEventPluginOrder,injectEventPluginsByName:d.injectEventPluginsByName},putListener:function(e,t,n){'function'==typeof n?void 0:r('94',t,typeof n);var o=y(e),a=c[t]||(c[t]={});a[o]=n;var i=d.registrationNameModules[t];i&&i.didPutListener&&i.didPutListener(e,t,n)},getListener:function(e,t){var n=c[t];if(a(t,e._currentElement.type,e._currentElement.props))return null;var o=y(e);return n&&n[o]},deleteListener:function(e,t){var n=d.registrationNameModules[t];n&&n.willDeleteListener&&n.willDeleteListener(e,t);var o=c[t];if(o){var a=y(e);delete o[a]}},deleteAllListeners:function(e){var t=y(e);for(var n in c)if(c.hasOwnProperty(n)&&c[n][t]){var o=d.registrationNameModules[n];o&&o.willDeleteListener&&o.willDeleteListener(e,n),delete c[n][t]}},extractEvents:function(e,t,n,o){for(var a=d.plugins,r=0,i,s;r<a.length;r++)if(s=a[r],s){var l=s.extractEvents(e,t,n,o);l&&(i=p(i,l))}return i},enqueueEvents:function(e){e&&(m=p(m,e))},processEventQueue:function(e){var t=m;m=null,e?l(t,g):l(t,f),!m?void 0:r('95'),s.rethrowCaughtError()},__purge:function(){c={}},__getListenerBank:function(){return c}};e.exports=_},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(12),r=n(51);a.augmentClass(o,{view:function(e){if(e.view)return e.view;var t=r(e);if(t.window===t)return t;var n=t.ownerDocument;return n?n.defaultView||n.parentWindow:window},detail:function(e){return e.detail||0}}),e.exports=o},function(e){'use strict';e.exports={remove:function(e){e._reactInternalInstance=void 0},get:function(e){return e._reactInternalInstance},has:function(e){return e._reactInternalInstance!==void 0},set:function(e,t){e._reactInternalInstance=t}}},function(e,t,n){'use strict';var o=n(42);e.exports=function(e){return o(e,!1)}},function(e,t,n){'use strict';var o=n(2),a=n(0),r={};e.exports={reinitializeTransaction:function(){this.transactionWrappers=this.getTransactionWrappers(),this.wrapperInitData?this.wrapperInitData.length=0:this.wrapperInitData=[],this._isInTransaction=!1},_isInTransaction:!1,getTransactionWrappers:null,isInTransaction:function(){return!!this._isInTransaction},perform:function(t,n,r,a,i,s,d,e){!this.isInTransaction()?void 0:o('27');var p,l;try{this._isInTransaction=!0,p=!0,this.initializeAll(0),l=t.call(n,r,a,i,s,d,e),p=!1}finally{try{if(p)try{this.closeAll(0)}catch(e){}else this.closeAll(0)}finally{this._isInTransaction=!1}}return l},initializeAll:function(e){for(var t=this.transactionWrappers,n=e,o;n<t.length;n++){o=t[n];try{this.wrapperInitData[n]=r,this.wrapperInitData[n]=o.initialize?o.initialize.call(this):null}finally{if(this.wrapperInitData[n]===r)try{this.initializeAll(n+1)}catch(e){}}}},closeAll:function(e){this.isInTransaction()?void 0:o('28');for(var t=this.transactionWrappers,n=e;n<t.length;n++){var a=t[n],i=this.wrapperInitData[n],d;try{d=!0,i!==r&&a.close&&a.close.call(this,i),d=!1}finally{if(d)try{this.closeAll(n+1)}catch(t){}}}this.wrapperInitData.length=0}}},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(26),r=n(74),i=n(53);a.augmentClass(o,{screenX:null,screenY:null,clientX:null,clientY:null,ctrlKey:null,shiftKey:null,altKey:null,metaKey:null,getModifierState:i,button:function(e){var t=e.button;return'which'in e?t:2===t?2:4===t?1:0},buttons:null,relatedTarget:function(e){return e.relatedTarget||(e.fromElement===e.srcElement?e.toElement:e.fromElement)},pageX:function(e){return'pageX'in e?e.pageX:e.clientX+r.currentScrollLeft},pageY:function(e){return'pageY'in e?e.pageY:e.clientY+r.currentScrollTop}}),e.exports=o},function(e,t,n){'use strict';var o=n(6),a=n(55),r=/^[ \r\n\t\f]/,i=/<(!--|link|noscript|meta|script|style)[ \r\n\t\f\/>]/,d=n(56),s=d(function(e,t){if(e.namespaceURI===a.svg&&!('innerHTML'in e)){p=p||document.createElement('div'),p.innerHTML='<svg>'+t+'</svg>';for(var n=p.firstChild;n.firstChild;)e.appendChild(n.firstChild)}else e.innerHTML=t}),p;if(o.canUseDOM){var l=document.createElement('div');l.innerHTML=' ',''===l.innerHTML&&(s=function(e,t){if(e.parentNode&&e.parentNode.replaceChild(e,e),r.test(t)||'<'===t[0]&&i.test(t)){e.innerHTML='\uFEFF'+t;var n=e.firstChild;1===n.data.length?e.removeChild(n):n.deleteData(0,1)}else e.innerHTML=t}),l=null}e.exports=s},function(e){'use strict';function t(e){var t=''+e,o=n.exec(t);if(!o)return t;var a='',r=0,i=0,d;for(r=o.index;r<t.length;r++){switch(t.charCodeAt(r)){case 34:d='"';break;case 38:d='&';break;case 39:d=''';break;case 60:d='<';break;case 62:d='>';break;default:continue;}i!==r&&(a+=t.substring(i,r)),i=r+1,a+=d}return i===r?a:a+t.substring(i,r)}var n=/["'&<>]/;e.exports=function(e){return'boolean'==typeof e||'number'==typeof e?''+e:t(e)}},function(e,t,n){'use strict';function o(e){return Object.prototype.hasOwnProperty.call(e,h)||(e[h]=c++,l[e[h]]={}),l[e[h]]}var a=n(3),r=n(48),i=n(133),d=n(74),s=n(134),p=n(52),l={},u=!1,c=0,m={topAbort:'abort',topAnimationEnd:s('animationend')||'animationend',topAnimationIteration:s('animationiteration')||'animationiteration',topAnimationStart:s('animationstart')||'animationstart',topBlur:'blur',topCanPlay:'canplay',topCanPlayThrough:'canplaythrough',topChange:'change',topClick:'click',topCompositionEnd:'compositionend',topCompositionStart:'compositionstart',topCompositionUpdate:'compositionupdate',topContextMenu:'contextmenu',topCopy:'copy',topCut:'cut',topDoubleClick:'dblclick',topDrag:'drag',topDragEnd:'dragend',topDragEnter:'dragenter',topDragExit:'dragexit',topDragLeave:'dragleave',topDragOver:'dragover',topDragStart:'dragstart',topDrop:'drop',topDurationChange:'durationchange',topEmptied:'emptied',topEncrypted:'encrypted',topEnded:'ended',topError:'error',topFocus:'focus',topInput:'input',topKeyDown:'keydown',topKeyPress:'keypress',topKeyUp:'keyup',topLoadedData:'loadeddata',topLoadedMetadata:'loadedmetadata',topLoadStart:'loadstart',topMouseDown:'mousedown',topMouseMove:'mousemove',topMouseOut:'mouseout',topMouseOver:'mouseover',topMouseUp:'mouseup',topPaste:'paste',topPause:'pause',topPlay:'play',topPlaying:'playing',topProgress:'progress',topRateChange:'ratechange',topScroll:'scroll',topSeeked:'seeked',topSeeking:'seeking',topSelectionChange:'selectionchange',topStalled:'stalled',topSuspend:'suspend',topTextInput:'textInput',topTimeUpdate:'timeupdate',topTouchCancel:'touchcancel',topTouchEnd:'touchend',topTouchMove:'touchmove',topTouchStart:'touchstart',topTransitionEnd:s('transitionend')||'transitionend',topVolumeChange:'volumechange',topWaiting:'waiting',topWheel:'wheel'},h='_reactListenersID'+(Math.random()+'').slice(2),g=a({},i,{ReactEventListener:null,injection:{injectReactEventListener:function(e){e.setHandleTopLevel(g.handleTopLevel),g.ReactEventListener=e}},setEnabled:function(e){g.ReactEventListener&&g.ReactEventListener.setEnabled(e)},isEnabled:function(){return!!(g.ReactEventListener&&g.ReactEventListener.isEnabled())},listenTo:function(e,t){for(var n=t,a=o(n),d=r.registrationNameDependencies[e],s=0,i;s<d.length;s++)i=d[s],a.hasOwnProperty(i)&&a[i]||('topWheel'===i?p('wheel')?g.ReactEventListener.trapBubbledEvent('topWheel','wheel',n):p('mousewheel')?g.ReactEventListener.trapBubbledEvent('topWheel','mousewheel',n):g.ReactEventListener.trapBubbledEvent('topWheel','DOMMouseScroll',n):'topScroll'===i?p('scroll',!0)?g.ReactEventListener.trapCapturedEvent('topScroll','scroll',n):g.ReactEventListener.trapBubbledEvent('topScroll','scroll',g.ReactEventListener.WINDOW_HANDLE):'topFocus'===i||'topBlur'===i?(p('focus',!0)?(g.ReactEventListener.trapCapturedEvent('topFocus','focus',n),g.ReactEventListener.trapCapturedEvent('topBlur','blur',n)):p('focusin')&&(g.ReactEventListener.trapBubbledEvent('topFocus','focusin',n),g.ReactEventListener.trapBubbledEvent('topBlur','focusout',n)),a.topBlur=!0,a.topFocus=!0):m.hasOwnProperty(i)&&g.ReactEventListener.trapBubbledEvent(i,m[i],n),a[i]=!0)},trapBubbledEvent:function(e,t,n){return g.ReactEventListener.trapBubbledEvent(e,t,n)},trapCapturedEvent:function(e,t,n){return g.ReactEventListener.trapCapturedEvent(e,t,n)},supportsEventPageXY:function(){if(!document.createEvent)return!1;var e=document.createEvent('MouseEvent');return null!=e&&'pageX'in e},ensureScrollValueMonitoring:function(){if(void 0==f&&(f=g.supportsEventPageXY()),!f&&!u){var e=d.refreshScrollValues;g.ReactEventListener.monitorScrollValue(e),u=!0}}}),f;e.exports=g},function(e){'use strict';e.exports=function(){}},function(e,t,n){'use strict';function o(e){return(''+e).replace(f,'$&/')}function a(e,t){this.func=e,this.context=t,this.count=0}function r(e,t){var n=e.func,o=e.context;n.call(o,t,e.count++)}function i(e,t,n,o){this.result=e,this.keyPrefix=t,this.func=n,this.context=o,this.count=0}function d(e,t,n){var a=e.result,r=e.keyPrefix,i=e.func,d=e.context,p=i.call(d,t,e.count++);Array.isArray(p)?s(p,a,n,c.thatReturnsArgument):null!=p&&(u.isValidElement(p)&&(p=u.cloneAndReplaceKey(p,r+(p.key&&(!t||t.key!==p.key)?o(p.key)+'/':'')+n)),a.push(p))}function s(e,t,n,a,r){var s='';null!=n&&(s=o(n)+'/');var p=i.getPooled(t,s,a,r);m(e,d,p),i.release(p)}function p(){return null}var l=n(36),u=n(7),c=n(5),m=n(37),h=l.twoArgumentPooler,g=l.fourArgumentPooler,f=/\/+/g;a.prototype.destructor=function(){this.func=null,this.context=null,this.count=0},l.addPoolingTo(a,h),i.prototype.destructor=function(){this.result=null,this.keyPrefix=null,this.func=null,this.context=null,this.count=0},l.addPoolingTo(i,g);e.exports={forEach:function(e,t,n){if(null==e)return e;var o=a.getPooled(t,n);m(e,r,o),a.release(o)},map:function(e,t,n){if(null==e)return e;var o=[];return s(e,o,null,t,n),o},mapIntoWithKeyPrefixInternal:s,count:function(e){return m(e,p,null)},toArray:function(e){var t=[];return s(e,t,null,c.thatReturnsArgument),t}}},function(e,t,n){'use strict';var o=n(10),a=n(0),r=function(e){var t=this;if(t.instancePool.length){var n=t.instancePool.pop();return t.call(n,e),n}return new t(e)},i=function(e){var t=this;e instanceof t?void 0:o('25'),e.destructor(),t.instancePool.length<t.poolSize&&t.instancePool.push(e)};e.exports={addPoolingTo:function(e,t){var n=e;return n.instancePool=[],n.getPooled=t||r,n.poolSize||(n.poolSize=10),n.release=i,n},oneArgumentPooler:r,twoArgumentPooler:function(e,t){var n=this;if(n.instancePool.length){var o=n.instancePool.pop();return n.call(o,e,t),o}return new n(e,t)},threeArgumentPooler:function(e,t,n){var o=this;if(o.instancePool.length){var a=o.instancePool.pop();return o.call(a,e,t,n),a}return new o(e,t,n)},fourArgumentPooler:function(e,t,n,o){var a=this;if(a.instancePool.length){var r=a.instancePool.pop();return a.call(r,e,t,n,o),r}return new a(e,t,n,o)}}},function(e,t,n){'use strict';function o(e,t){return e&&'object'==typeof e&&null!=e.key?l.escape(e.key):t.toString(36)}function a(e,t,n,p){var u=typeof e;if(('undefined'==u||'boolean'==u)&&(e=null),null===e||'string'==u||'number'==u||'object'==u&&e.$$typeof===d)return n(p,e,''===t?c+o(e,0):t),1;var h=0,g=''===t?c:t+m,f,y;if(Array.isArray(e))for(var _=0;_<e.length;_++)f=e[_],y=g+o(f,_),h+=a(f,y,n,p);else{var i=s(e);if(i){var C=i.call(e),b;if(i!==e.entries)for(var E=0;!(b=C.next()).done;)f=b.value,y=g+o(f,E++),h+=a(f,y,n,p);else for(var v;!(b=C.next()).done;)v=b.value,v&&(f=v[1],y=g+l.escape(v[0])+m+o(f,0),h+=a(f,y,n,p))}else if('object'==u){var x='',N=e+'';r('31','[object Object]'===N?'object with keys {'+Object.keys(e).join(', ')+'}':N,x)}}return h}var r=n(10),i=n(8),d=n(23),s=n(38),p=n(0),l=n(39),u=n(1),c='.',m=':';e.exports=function(e,t,n){return null==e?0:a(e,'',t,n)}},function(e){'use strict';var t='function'==typeof Symbol&&Symbol.iterator;e.exports=function(e){var n=e&&(t&&e[t]||e['@@iterator']);if('function'==typeof n)return n}},function(e){'use strict';e.exports={escape:function(e){var t=/[=:]/g,n={"=":'=0',":":'=2'},o=(''+e).replace(t,function(e){return n[e]});return'$'+o},unescape:function(e){var t=/(=0|=2)/g,n={"=0":'=',"=2":':'},o='.'===e[0]&&'$'===e[1]?e.substring(2):e.substring(1);return(''+o).replace(t,function(e){return n[e]})}}},function(e,t,n){'use strict';var o=n(7),a=o.createFactory;var r={a:a('a'),abbr:a('abbr'),address:a('address'),area:a('area'),article:a('article'),aside:a('aside'),audio:a('audio'),b:a('b'),base:a('base'),bdi:a('bdi'),bdo:a('bdo'),big:a('big'),blockquote:a('blockquote'),body:a('body'),br:a('br'),button:a('button'),canvas:a('canvas'),caption:a('caption'),cite:a('cite'),code:a('code'),col:a('col'),colgroup:a('colgroup'),data:a('data'),datalist:a('datalist'),dd:a('dd'),del:a('del'),details:a('details'),dfn:a('dfn'),dialog:a('dialog'),div:a('div'),dl:a('dl'),dt:a('dt'),em:a('em'),embed:a('embed'),fieldset:a('fieldset'),figcaption:a('figcaption'),figure:a('figure'),footer:a('footer'),form:a('form'),h1:a('h1'),h2:a('h2'),h3:a('h3'),h4:a('h4'),h5:a('h5'),h6:a('h6'),head:a('head'),header:a('header'),hgroup:a('hgroup'),hr:a('hr'),html:a('html'),i:a('i'),iframe:a('iframe'),img:a('img'),input:a('input'),ins:a('ins'),kbd:a('kbd'),keygen:a('keygen'),label:a('label'),legend:a('legend'),li:a('li'),link:a('link'),main:a('main'),map:a('map'),mark:a('mark'),menu:a('menu'),menuitem:a('menuitem'),meta:a('meta'),meter:a('meter'),nav:a('nav'),noscript:a('noscript'),object:a('object'),ol:a('ol'),optgroup:a('optgroup'),option:a('option'),output:a('output'),p:a('p'),param:a('param'),picture:a('picture'),pre:a('pre'),progress:a('progress'),q:a('q'),rp:a('rp'),rt:a('rt'),ruby:a('ruby'),s:a('s'),samp:a('samp'),script:a('script'),section:a('section'),select:a('select'),small:a('small'),source:a('source'),span:a('span'),strong:a('strong'),style:a('style'),sub:a('sub'),summary:a('summary'),sup:a('sup'),table:a('table'),tbody:a('tbody'),td:a('td'),textarea:a('textarea'),tfoot:a('tfoot'),th:a('th'),thead:a('thead'),time:a('time'),title:a('title'),tr:a('tr'),track:a('track'),u:a('u'),ul:a('ul'),var:a('var'),video:a('video'),wbr:a('wbr'),circle:a('circle'),clipPath:a('clipPath'),defs:a('defs'),ellipse:a('ellipse'),g:a('g'),image:a('image'),line:a('line'),linearGradient:a('linearGradient'),mask:a('mask'),path:a('path'),pattern:a('pattern'),polygon:a('polygon'),polyline:a('polyline'),radialGradient:a('radialGradient'),rect:a('rect'),stop:a('stop'),svg:a('svg'),text:a('text'),tspan:a('tspan')};e.exports=r},function(e,t,n){'use strict';var o=n(7),a=o.isValidElement,r=n(28);e.exports=r(a)},function(e,t,n){'use strict';var o=n(5),a=n(0),r=n(1),d=n(19),i=n(43);e.exports=function(e,t){function n(e){var t=e&&(_&&e[_]||e[C]);if('function'==typeof t)return t}function s(e,t){return e===t?0!==e||1/e==1/t:e!==e&&t!==t}function p(e){this.message=e,this.stack=''}function l(e){function n(n,o,r,i,s,l,u){if(i=i||b,l=l||r,u!==d)if(t)a(!1,'Calling PropTypes validators directly is not supported by the `prop-types` package. Use `PropTypes.checkPropTypes()` to call them. Read more at http://fb.me/use-check-prop-types');else;return null==o[r]?n?null===o[r]?new p('The '+s+' `'+l+'` is marked as required '+('in `'+i+'`, but its value is `null`.')):new p('The '+s+' `'+l+'` is marked as required in '+('`'+i+'`, but its value is `undefined`.')):null:e(o,r,i,s,l)}var o=n.bind(null,!1);return o.isRequired=n.bind(null,!0),o}function u(e){return l(function(t,n,o,a,r){var i=t[n],d=h(i);if(d!==e){var s=g(i);return new p('Invalid '+a+' `'+r+'` of type '+('`'+s+'` supplied to `'+o+'`, expected ')+('`'+e+'`.'))}return null})}function c(t){switch(typeof t){case'number':case'string':case'undefined':return!0;case'boolean':return!t;case'object':if(Array.isArray(t))return t.every(c);if(null===t||e(t))return!0;var o=n(t);if(o){var a=o.call(t),r;if(o!==t.entries){for(;!(r=a.next()).done;)if(!c(r.value))return!1;}else for(;!(r=a.next()).done;){var i=r.value;if(i&&!c(i[1]))return!1}}else return!1;return!0;default:return!1;}}function m(e,t){return'symbol'===e||'Symbol'===t['@@toStringTag']||'function'==typeof Symbol&&t instanceof Symbol}function h(e){var t=typeof e;return Array.isArray(e)?'array':e instanceof RegExp?'object':m(t,e)?'symbol':t}function g(e){if('undefined'==typeof e||null===e)return''+e;var t=h(e);if('object'===t){if(e instanceof Date)return'date';if(e instanceof RegExp)return'regexp'}return t}function f(e){var t=g(e);return'array'===t||'object'===t?'an '+t:'boolean'===t||'date'===t||'regexp'===t?'a '+t:t}function y(e){return e.constructor&&e.constructor.name?e.constructor.name:b}var _='function'==typeof Symbol&&Symbol.iterator,C='@@iterator',b='<<anonymous>>',E={array:u('array'),bool:u('boolean'),func:u('function'),number:u('number'),object:u('object'),string:u('string'),symbol:u('symbol'),any:function(){return l(o.thatReturnsNull)}(),arrayOf:function(e){return l(function(t,n,o,a,r){if('function'!=typeof e)return new p('Property `'+r+'` of component `'+o+'` has invalid PropType notation inside arrayOf.');var s=t[n];if(!Array.isArray(s)){var l=h(s);return new p('Invalid '+a+' `'+r+'` of type '+('`'+l+'` supplied to `'+o+'`, expected an array.'))}for(var u=0,i;u<s.length;u++)if(i=e(s,u,o,a,r+'['+u+']',d),i instanceof Error)return i;return null})},element:function(){return l(function(t,n,o,a,r){var i=t[n];if(!e(i)){var d=h(i);return new p('Invalid '+a+' `'+r+'` of type '+('`'+d+'` supplied to `'+o+'`, expected a single ReactElement.'))}return null})}(),instanceOf:function(e){return l(function(t,n,o,a,r){if(!(t[n]instanceof e)){var i=e.name||b,d=y(t[n]);return new p('Invalid '+a+' `'+r+'` of type '+('`'+d+'` supplied to `'+o+'`, expected ')+('instance of `'+i+'`.'))}return null})},node:function(){return l(function(e,t,n,o,a){return c(e[t])?null:new p('Invalid '+o+' `'+a+'` supplied to '+('`'+n+'`, expected a ReactNode.'))})}(),objectOf:function(e){return l(function(t,n,o,a,r){if('function'!=typeof e)return new p('Property `'+r+'` of component `'+o+'` has invalid PropType notation inside objectOf.');var i=t[n],s=h(i);if('object'!==s)return new p('Invalid '+a+' `'+r+'` of type '+('`'+s+'` supplied to `'+o+'`, expected an object.'));for(var l in i)if(i.hasOwnProperty(l)){var u=e(i,l,o,a,r+'.'+l,d);if(u instanceof Error)return u}return null})},oneOf:function(e){return Array.isArray(e)?l(function(t,n,o,a,r){for(var d=t[n],l=0;l<e.length;l++)if(s(d,e[l]))return null;var i=JSON.stringify(e);return new p('Invalid '+a+' `'+r+'` of value `'+d+'` '+('supplied to `'+o+'`, expected one of '+i+'.'))}):(void 0,o.thatReturnsNull)},oneOfType:function(e){if(!Array.isArray(e))return void 0,o.thatReturnsNull;for(var t=0,n;t<e.length;t++)if(n=e[t],'function'!=typeof n)return r(!1,'Invalid argument supplid to oneOfType. Expected an array of check functions, but received %s at index %s.',f(n),t),o.thatReturnsNull;return l(function(t,n,o,a,r){for(var s=0,i;s<e.length;s++)if(i=e[s],null==i(t,n,o,a,r,d))return null;return new p('Invalid '+a+' `'+r+'` supplied to '+('`'+o+'`.'))})},shape:function(e){return l(function(t,n,o,a,r){var i=t[n],s=h(i);if('object'!==s)return new p('Invalid '+a+' `'+r+'` of type `'+s+'` '+('supplied to `'+o+'`, expected `object`.'));for(var l in e){var u=e[l];if(u){var c=u(i,l,o,a,r+'.'+l,d);if(c)return c}}return null})}};return p.prototype=Error.prototype,E.checkPropTypes=i,E.PropTypes=E,E}},function(e){'use strict';e.exports=function(){}},function(e){'use strict';e.exports='15.6.1'},function(e,t,n){'use strict';var o=n(20),a=o.Component,r=n(7),i=r.isValidElement,d=n(21),s=n(46);e.exports=s(a,i,d)},function(e,t,n){'use strict';function o(e){return e}var a=n(3),r=n(14),i=n(0);var d='mixins',s;s={},e.exports=function(e,t,n){function s(e,t){var n=y.hasOwnProperty(t)?y[t]:null;E.hasOwnProperty(t)&&i('OVERRIDE_BASE'===n,'ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.',t),e&&i('DEFINE_MANY'===n||'DEFINE_MANY_MERGED'===n,'ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.',t)}function p(e,n){if(!n){return}i('function'!=typeof n,'ReactClass: You\'re attempting to use a component class or function as a mixin. Instead, just use a regular object.'),i(!t(n),'ReactClass: You\'re attempting to use a component as a mixin. Instead, just use a regular object.');var o=e.prototype,a=o.__reactAutoBindPairs;for(var r in n.hasOwnProperty(d)&&_.mixins(e,n.mixins),n)if(n.hasOwnProperty(r)&&r!=d){var p=n[r],l=o.hasOwnProperty(r);if(s(l,r),_.hasOwnProperty(r))_[r](e,p);else{var u=y.hasOwnProperty(r),h='function'==typeof p&&!u&&!l&&!1!==n.autobind;if(h)a.push(r,p),o[r]=p;else if(l){var g=y[r];i(u&&('DEFINE_MANY_MERGED'===g||'DEFINE_MANY'===g),'ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.',g,r),'DEFINE_MANY_MERGED'===g?o[r]=c(o[r],p):'DEFINE_MANY'===g&&(o[r]=m(o[r],p))}else o[r]=p,!1}}}function l(e,t){if(t)for(var n in t){var o=t[n];if(t.hasOwnProperty(n)){i(!(n in _),'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.',n);i(!(n in e),'ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.',n),e[n]=o}}}function u(e,t){for(var n in i(e&&t&&'object'==typeof e&&'object'==typeof t,'mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.'),t)t.hasOwnProperty(n)&&(i(void 0===e[n],'mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.',n),e[n]=t[n]);return e}function c(e,t){return function(){var n=e.apply(this,arguments),o=t.apply(this,arguments);if(null==n)return o;if(null==o)return n;var a={};return u(a,n),u(a,o),a}}function m(e,t){return function(){e.apply(this,arguments),t.apply(this,arguments)}}function h(e,t){var n=t.bind(e);return n}function g(e){for(var t=e.__reactAutoBindPairs,n=0;n<t.length;n+=2){var o=t[n],a=t[n+1];e[o]=h(e,a)}}var f=[],y={mixins:'DEFINE_MANY',statics:'DEFINE_MANY',propTypes:'DEFINE_MANY',contextTypes:'DEFINE_MANY',childContextTypes:'DEFINE_MANY',getDefaultProps:'DEFINE_MANY_MERGED',getInitialState:'DEFINE_MANY_MERGED',getChildContext:'DEFINE_MANY_MERGED',render:'DEFINE_ONCE',componentWillMount:'DEFINE_MANY',componentDidMount:'DEFINE_MANY',componentWillReceiveProps:'DEFINE_MANY',shouldComponentUpdate:'DEFINE_ONCE',componentWillUpdate:'DEFINE_MANY',componentDidUpdate:'DEFINE_MANY',componentWillUnmount:'DEFINE_MANY',updateComponent:'OVERRIDE_BASE'},_={displayName:function(e,t){e.displayName=t},mixins:function(e,t){if(t)for(var n=0;n<t.length;n++)p(e,t[n])},childContextTypes:function(e,t){!1,e.childContextTypes=a({},e.childContextTypes,t)},contextTypes:function(e,t){!1,e.contextTypes=a({},e.contextTypes,t)},getDefaultProps:function(e,t){e.getDefaultProps=e.getDefaultProps?c(e.getDefaultProps,t):t},propTypes:function(e,t){!1,e.propTypes=a({},e.propTypes,t)},statics:function(e,t){l(e,t)},autobind:function(){}},C={componentDidMount:function(){this.__isMounted=!0}},b={componentWillUnmount:function(){this.__isMounted=!1}},E={replaceState:function(e,t){this.updater.enqueueReplaceState(this,e,t)},isMounted:function(){return!1,!!this.__isMounted}},v=function(){};return a(v.prototype,e.prototype,E),function(e){var t=o(function(e,o,a){!1,this.__reactAutoBindPairs.length&&g(this),this.props=e,this.context=o,this.refs=r,this.updater=a||n,this.state=null;var d=this.getInitialState?this.getInitialState():null;!1,i('object'==typeof d&&!Array.isArray(d),'%s.getInitialState(): must return an object or null',t.displayName||'ReactCompositeComponent'),this.state=d});for(var a in t.prototype=new v,t.prototype.constructor=t,t.prototype.__reactAutoBindPairs=[],f.forEach(p.bind(null,t)),p(t,C),p(t,e),p(t,b),t.getDefaultProps&&(t.defaultProps=t.getDefaultProps()),!1,i(t.prototype.render,'createClass(...): Class specification must implement a `render` method.'),!1,y)t.prototype[a]||(t.prototype[a]=null);return t}}},function(e,t,n){'use strict';var o=n(10),a=n(7),r=n(0);e.exports=function(e){return a.isValidElement(e)?void 0:o('143'),e}},function(e,t,n){'use strict';function o(){if(s)for(var e in p){var t=p[e],n=s.indexOf(e);if(-1<n?void 0:i('96',e),!l.plugins[n]){t.extractEvents?void 0:i('97',e),l.plugins[n]=t;var o=t.eventTypes;for(var r in o)a(o[r],t,r)?void 0:i('98',r,e)}}}function a(e,t,n){!l.eventNameDispatchConfigs.hasOwnProperty(n)?void 0:i('99',n),l.eventNameDispatchConfigs[n]=e;var o=e.phasedRegistrationNames;if(o){for(var a in o)if(o.hasOwnProperty(a)){var d=o[a];r(d,t,n)}return!0}return!!e.registrationName&&(r(e.registrationName,t,n),!0)}function r(e,t,n){!l.registrationNameModules[e]?void 0:i('100',e),l.registrationNameModules[e]=t,l.registrationNameDependencies[e]=t.eventTypes[n].dependencies}var i=n(2),d=n(0),s=null,p={},l={plugins:[],eventNameDispatchConfigs:{},registrationNameModules:{},registrationNameDependencies:{},possibleRegistrationNames:null,injectEventPluginOrder:function(e){!s?void 0:i('101'),s=Array.prototype.slice.call(e),o()},injectEventPluginsByName:function(e){var t=!1;for(var n in e)if(e.hasOwnProperty(n)){var a=e[n];p.hasOwnProperty(n)&&p[n]===a||(p[n]?i('102',n):void 0,p[n]=a,t=!0)}t&&o()},getPluginModuleForEvent:function(e){var t=e.dispatchConfig;if(t.registrationName)return l.registrationNameModules[t.registrationName]||null;if(void 0!==t.phasedRegistrationNames){var n=t.phasedRegistrationNames;for(var o in n)if(n.hasOwnProperty(o)){var a=l.registrationNameModules[n[o]];if(a)return a}}return null},_resetEventPlugins:function(){for(var e in s=null,p)p.hasOwnProperty(e)&&delete p[e];l.plugins.length=0;var t=l.eventNameDispatchConfigs;for(var n in t)t.hasOwnProperty(n)&&delete t[n];var o=l.registrationNameModules;for(var a in o)o.hasOwnProperty(a)&&delete o[a]}};e.exports=l},function(e,t,n){'use strict';function o(e,t,n,o){var a=e.type||'unknown-event';e.currentTarget=u.getNodeFromInstance(o),t?i.invokeGuardedCallbackWithCatch(a,n,e):i.invokeGuardedCallback(a,n,e),e.currentTarget=null}function a(e){var t=e._dispatchListeners,n=e._dispatchInstances;if(!1,Array.isArray(t)){for(var o=0;o<t.length&&!e.isPropagationStopped();o++)if(t[o](e,n[o]))return n[o];}else if(t&&t(e,n))return n;return null}var r=n(2),i=n(50),d=n(0),s=n(1),p,l;var u={isEndish:function(e){return'topMouseUp'===e||'topTouchEnd'===e||'topTouchCancel'===e},isMoveish:function(e){return'topMouseMove'===e||'topTouchMove'===e},isStartish:function(e){return'topMouseDown'===e||'topTouchStart'===e},executeDirectDispatch:function(e){var t=e._dispatchListeners,n=e._dispatchInstances;Array.isArray(t)?r('103'):void 0,e.currentTarget=t?u.getNodeFromInstance(n):null;var o=t?t(e):null;return e.currentTarget=null,e._dispatchListeners=null,e._dispatchInstances=null,o},executeDispatchesInOrder:function(e,t){var n=e._dispatchListeners,a=e._dispatchInstances;if(!1,Array.isArray(n))for(var r=0;r<n.length&&!e.isPropagationStopped();r++)o(e,t,n[r],a[r]);else n&&o(e,t,n,a);e._dispatchListeners=null,e._dispatchInstances=null},executeDispatchesInOrderStopAtTrue:function(e){var t=a(e);return e._dispatchInstances=null,e._dispatchListeners=null,t},hasDispatches:function(e){return!!e._dispatchListeners},getInstanceFromNode:function(e){return p.getInstanceFromNode(e)},getNodeFromInstance:function(e){return p.getNodeFromInstance(e)},isAncestor:function(e,t){return l.isAncestor(e,t)},getLowestCommonAncestor:function(e,t){return l.getLowestCommonAncestor(e,t)},getParentInstance:function(e){return l.getParentInstance(e)},traverseTwoPhase:function(e,t,n){return l.traverseTwoPhase(e,t,n)},traverseEnterLeave:function(e,t,n,o,a){return l.traverseEnterLeave(e,t,n,o,a)},injection:{injectComponentTree:function(e){p=e,!1},injectTreeTraversal:function(e){l=e,!1}}};e.exports=u},function(e){'use strict';function t(e,t,o){try{t(o)}catch(e){null==n&&(n=e)}}var n=null;e.exports={invokeGuardedCallback:t,invokeGuardedCallbackWithCatch:t,rethrowCaughtError:function(){if(n){var e=n;throw n=null,e}}}},function(e){'use strict';e.exports=function(e){var t=e.target||e.srcElement||window;return t.correspondingUseElement&&(t=t.correspondingUseElement),3===t.nodeType?t.parentNode:t}},function(e,t,n){'use strict';var o=n(6),a;/** + * Checks if an event is supported in the current execution environment. + * + * NOTE: This will not work correctly for non-generic events such as `change`, + * `reset`, `load`, `error`, and `select`. + * + * Borrows from Modernizr. + * + * @param {string} eventNameSuffix Event name, e.g. "click". + * @param {?boolean} capture Check if the capture phase is supported. + * @return {boolean} True if the event is supported. + * @internal + * @license Modernizr 3.0.0pre (Custom Build) | MIT + */o.canUseDOM&&(a=document.implementation&&document.implementation.hasFeature&&!0!==document.implementation.hasFeature('','')),e.exports=function(e,t){if(!o.canUseDOM||t&&!('addEventListener'in document))return!1;var n='on'+e,r=n in document;if(!r){var i=document.createElement('div');i.setAttribute(n,'return;'),r='function'==typeof i[n]}return!r&&a&&'wheel'===e&&(r=document.implementation.hasFeature('Events.wheel','3.0')),r}},function(e){'use strict';function t(e){var t=this,o=t.nativeEvent;if(o.getModifierState)return o.getModifierState(e);var a=n[e];return!!a&&!!o[a]}var n={Alt:'altKey',Control:'ctrlKey',Meta:'metaKey',Shift:'shiftKey'};e.exports=function(){return t}},function(e,t,n){'use strict';function o(e,t){return Array.isArray(t)&&(t=t[1]),t?t.nextSibling:e.firstChild}function a(e,t,n){p.insertTreeBefore(e,t,n)}function r(e,t,n){Array.isArray(t)?d(e,t[0],t[1],n):f(e,t,n)}function i(e,t){if(Array.isArray(t)){var n=t[1];t=t[0],s(e,t,n),e.removeChild(n)}e.removeChild(t)}function d(e,t,n,o){for(var a=t,r;r=a.nextSibling,f(e,a,o),a!==n;)a=r}function s(e,t,n){for(;;){var o=t.nextSibling;if(o===n)break;else e.removeChild(o)}}var p=n(18),l=n(118),u=n(4),c=n(9),m=n(56),h=n(31),g=n(75),f=m(function(e,t,n){e.insertBefore(t,n)}),y=l.dangerouslyReplaceNodeWithMarkup;e.exports={dangerouslyReplaceNodeWithMarkup:y,replaceDelimitedText:function(e,t,n){var o=e.parentNode,a=e.nextSibling;a===t?n&&f(o,document.createTextNode(n),a):n?(g(a,n),s(o,a,t)):s(o,e,t),!1},processUpdates:function(e,t){for(var n=0,d;n<t.length;n++)switch(d=t[n],d.type){case'INSERT_MARKUP':a(e,d.content,o(e,d.afterNode)),!1;break;case'MOVE_EXISTING':r(e,d.fromNode,o(e,d.afterNode)),!1;break;case'SET_MARKUP':h(e,d.content),!1;break;case'TEXT_CONTENT':g(e,d.content),!1;break;case'REMOVE_NODE':i(e,d.fromNode),!1;}}}},function(e){'use strict';e.exports={html:'http://www.w3.org/1999/xhtml',mathml:'http://www.w3.org/1998/Math/MathML',svg:'http://www.w3.org/2000/svg'}},function(e){'use strict';e.exports=function(e){return'undefined'!=typeof MSApp&&MSApp.execUnsafeLocalFunction?function(t,n,o,a){MSApp.execUnsafeLocalFunction(function(){return e(t,n,o,a)})}:e}},function(e,t,n){'use strict';function o(e){null==e.checkedLink||null==e.valueLink?void 0:d('87')}function a(e){o(e),null==e.value&&null==e.onChange?void 0:d('88')}function r(e){o(e),null==e.checked&&null==e.onChange?void 0:d('89')}function i(e){if(e){var t=e.getName();if(t)return' Check the render method of `'+t+'`.'}return''}var d=n(2),s=n(136),p=n(28),l=n(13),u=p(l.isValidElement),c=n(0),m=n(1),h={button:!0,checkbox:!0,image:!0,hidden:!0,radio:!0,reset:!0,submit:!0},g={value:function(e,t){return!e[t]||h[e.type]||e.onChange||e.readOnly||e.disabled?null:new Error('You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.')},checked:function(e,t){return!e[t]||e.onChange||e.readOnly||e.disabled?null:new Error('You provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultChecked`. Otherwise, set either `onChange` or `readOnly`.')},onChange:u.func},f={};e.exports={checkPropTypes:function(e,t,n){for(var o in g){if(g.hasOwnProperty(o))var a=g[o](t,o,e,'prop',null,s);if(a instanceof Error&&!(a.message in f)){f[a.message]=!0;var r=i(n);void 0}}},getValue:function(e){return e.valueLink?(a(e),e.valueLink.value):e.value},getChecked:function(e){return e.checkedLink?(r(e),e.checkedLink.value):e.checked},executeOnChange:function(e,t){return e.valueLink?(a(e),e.valueLink.requestChange(t.target.value)):e.checkedLink?(r(e),e.checkedLink.requestChange(t.target.checked)):e.onChange?e.onChange.call(void 0,t):void 0}}},function(e,t,n){'use strict';var o=n(2),a=n(0),r=!1,i={replaceNodeWithMarkup:null,processChildrenUpdates:null,injection:{injectEnvironment:function(e){!r?void 0:o('104'),i.replaceNodeWithMarkup=e.replaceNodeWithMarkup,i.processChildrenUpdates=e.processChildrenUpdates,r=!0}}};e.exports=i},function(e){'use strict';function t(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!==e&&t!==t}var n=Object.prototype.hasOwnProperty;e.exports=function(e,o){if(t(e,o))return!0;if('object'!=typeof e||null===e||'object'!=typeof o||null===o)return!1;var a=Object.keys(e),r=Object.keys(o);if(a.length!==r.length)return!1;for(var d=0;d<a.length;d++)if(!n.call(o,a[d])||!t(e[a[d]],o[a[d]]))return!1;return!0}},function(e){'use strict';e.exports=function(e,t){var n=null===e||!1===e,o=null===t||!1===t;if(n||o)return n==o;var a=typeof e,r=typeof t;return'string'==a||'number'==a?'string'==r||'number'==r:'object'==r&&e.type===t.type&&e.key===t.key}},function(e){'use strict';e.exports={escape:function(e){var t=/[=:]/g,n={"=":'=0',":":'=2'},o=(''+e).replace(t,function(e){return n[e]});return'$'+o},unescape:function(e){var t=/(=0|=2)/g,n={"=0":'=',"=2":':'},o='.'===e[0]&&'$'===e[1]?e.substring(2):e.substring(1);return(''+o).replace(t,function(e){return n[e]})}}},function(e,t,n){'use strict';function o(e){l.enqueueUpdate(e)}function a(e){var t=typeof e;if('object'!=t)return t;var n=e.constructor&&e.constructor.name||t,o=Object.keys(e);return 0<o.length&&20>o.length?n+' (keys: '+o.join(', ')+')':n}function r(e){var t=s.get(e);if(!t){return null}return!1,t}var i=n(2),d=n(8),s=n(27),p=n(9),l=n(11),u=n(0),c=n(1),m={isMounted:function(e){var t=s.get(e);return!!t&&!!t._renderedComponent},enqueueCallback:function(e,t,n){m.validateCallback(t,n);var a=r(e);return a?void(a._pendingCallbacks?a._pendingCallbacks.push(t):a._pendingCallbacks=[t],o(a)):null},enqueueCallbackInternal:function(e,t){e._pendingCallbacks?e._pendingCallbacks.push(t):e._pendingCallbacks=[t],o(e)},enqueueForceUpdate:function(e){var t=r(e,'forceUpdate');t&&(t._pendingForceUpdate=!0,o(t))},enqueueReplaceState:function(e,t,n){var a=r(e,'replaceState');a&&(a._pendingStateQueue=[t],a._pendingReplaceState=!0,n!==void 0&&null!==n&&(m.validateCallback(n,'replaceState'),a._pendingCallbacks?a._pendingCallbacks.push(n):a._pendingCallbacks=[n]),o(a))},enqueueSetState:function(e,t){var n=r(e,'setState');if(n){var a=n._pendingStateQueue||(n._pendingStateQueue=[]);a.push(t),o(n)}},enqueueElementInternal:function(e,t,n){e._pendingElement=t,e._context=n,o(e)},validateCallback:function(e,t){!e||'function'==typeof e?void 0:i('122',t,a(e))}};e.exports=m},function(e,t,n){'use strict';var o=n(3),a=n(5),r=n(1);e.exports=a},function(e){'use strict';e.exports=function(e){var t=e.keyCode,n;return'charCode'in e?(n=e.charCode,0===n&&13===t&&(n=13)):n=t,32<=n||13===n?n:0}},,function(e){'use strict';e.exports={hasCachedChildNodes:1}},function(e,t,n){'use strict';var o=n(2),a=n(0);e.exports=function(e,t){return null==t?o('30'):void 0,null==e?t:Array.isArray(e)?Array.isArray(t)?(e.push.apply(e,t),e):(e.push(t),e):Array.isArray(t)?[e].concat(t):[e,t]}},function(e){'use strict';e.exports=function(e,t,n){Array.isArray(e)?e.forEach(t,n):e&&t.call(n,e)}},function(e,t,n){'use strict';var o=n(6),a=null;e.exports=function(){return!a&&o.canUseDOM&&(a='textContent'in document.documentElement?'textContent':'innerText'),a}},function(e,t,n){'use strict';function o(e,t){if(!(e instanceof t))throw new TypeError('Cannot call a class as a function')}var a=n(2),r=n(15),i=n(0),d=function(){function e(t){o(this,e),this._callbacks=null,this._contexts=null,this._arg=t}return e.prototype.enqueue=function(e,t){this._callbacks=this._callbacks||[],this._callbacks.push(e),this._contexts=this._contexts||[],this._contexts.push(t)},e.prototype.notifyAll=function(){var e=this._callbacks,t=this._contexts,n=this._arg;if(e&&t){e.length===t.length?void 0:a('24'),this._callbacks=null,this._contexts=null;for(var o=0;o<e.length;o++)e[o].call(t[o],n);e.length=0,t.length=0}},e.prototype.checkpoint=function(){return this._callbacks?this._callbacks.length:0},e.prototype.rollback=function(e){this._callbacks&&this._contexts&&(this._callbacks.length=e,this._contexts.length=e)},e.prototype.reset=function(){this._callbacks=null,this._contexts=null},e.prototype.destructor=function(){this.reset()},e}();e.exports=r.addPoolingTo(d)},function(e){'use strict';e.exports={logTopLevelRenders:!1}},function(e,t,n){'use strict';function o(e){var t=e.type,n=e.nodeName;return n&&'input'===n.toLowerCase()&&('checkbox'===t||'radio'===t)}function a(e){return e._wrapperState.valueTracker}function r(e,t){e._wrapperState.valueTracker=t}function i(e){delete e._wrapperState.valueTracker}function d(e){var t;return e&&(t=o(e)?''+e.checked:e.value),t}var s=n(4),p={_getTrackerFromNode:function(e){return a(s.getInstanceFromNode(e))},track:function(e){if(!a(e)){var t=s.getNodeFromInstance(e),n=o(t)?'checked':'value',d=Object.getOwnPropertyDescriptor(t.constructor.prototype,n),p=''+t[n];t.hasOwnProperty(n)||'function'!=typeof d.get||'function'!=typeof d.set||(Object.defineProperty(t,n,{enumerable:d.enumerable,configurable:!0,get:function(){return d.get.call(this)},set:function(e){p=''+e,d.set.call(this,e)}}),r(e,{getValue:function(){return p},setValue:function(e){p=''+e},stopTracking:function(){i(e),delete t[n]}}))}},updateValueIfChanged:function(e){if(!e)return!1;var t=a(e);if(!t)return p.track(e),!0;var n=t.getValue(),o=d(s.getNodeFromInstance(e));return o!==n&&(t.setValue(o),!0)},stopTracking:function(e){var t=a(e);t&&t.stopTracking()}};e.exports=p},function(e){'use strict';var t={color:!0,date:!0,datetime:!0,"datetime-local":!0,email:!0,month:!0,number:!0,password:!0,range:!0,search:!0,tel:!0,text:!0,time:!0,url:!0,week:!0};e.exports=function(e){var n=e&&e.nodeName&&e.nodeName.toLowerCase();return'input'===n?!!t[e.type]:!('textarea'!==n)}},function(e){'use strict';var t={currentScrollLeft:0,currentScrollTop:0,refreshScrollValues:function(e){t.currentScrollLeft=e.x,t.currentScrollTop=e.y}};e.exports=t},function(e,t,n){'use strict';var o=n(6),a=n(32),r=n(31),i=function(e,t){if(t){var n=e.firstChild;if(n&&n===e.lastChild&&3===n.nodeType)return void(n.nodeValue=t)}e.textContent=t};o.canUseDOM&&!('textContent'in document.documentElement)&&(i=function(e,t){return 3===e.nodeType?void(e.nodeValue=t):void r(e,a(t))}),e.exports=i},function(e){'use strict';e.exports=function(e){try{e.focus()}catch(t){}}},function(e){'use strict';function t(e,t){return e+t.charAt(0).toUpperCase()+t.substring(1)}var n={animationIterationCount:!0,borderImageOutset:!0,borderImageSlice:!0,borderImageWidth:!0,boxFlex:!0,boxFlexGroup:!0,boxOrdinalGroup:!0,columnCount:!0,flex:!0,flexGrow:!0,flexPositive:!0,flexShrink:!0,flexNegative:!0,flexOrder:!0,gridRow:!0,gridRowEnd:!0,gridRowSpan:!0,gridRowStart:!0,gridColumn:!0,gridColumnEnd:!0,gridColumnSpan:!0,gridColumnStart:!0,fontWeight:!0,lineClamp:!0,lineHeight:!0,opacity:!0,order:!0,orphans:!0,tabSize:!0,widows:!0,zIndex:!0,zoom:!0,fillOpacity:!0,floodOpacity:!0,stopOpacity:!0,strokeDasharray:!0,strokeDashoffset:!0,strokeMiterlimit:!0,strokeOpacity:!0,strokeWidth:!0},o=['Webkit','ms','Moz','O'];Object.keys(n).forEach(function(e){o.forEach(function(o){n[t(o,e)]=n[e]})});e.exports={isUnitlessNumber:n,shorthandPropertyExpansions:{background:{backgroundAttachment:!0,backgroundColor:!0,backgroundImage:!0,backgroundPositionX:!0,backgroundPositionY:!0,backgroundRepeat:!0},backgroundPosition:{backgroundPositionX:!0,backgroundPositionY:!0},border:{borderWidth:!0,borderStyle:!0,borderColor:!0},borderBottom:{borderBottomWidth:!0,borderBottomStyle:!0,borderBottomColor:!0},borderLeft:{borderLeftWidth:!0,borderLeftStyle:!0,borderLeftColor:!0},borderRight:{borderRightWidth:!0,borderRightStyle:!0,borderRightColor:!0},borderTop:{borderTopWidth:!0,borderTopStyle:!0,borderTopColor:!0},font:{fontStyle:!0,fontVariant:!0,fontWeight:!0,fontSize:!0,lineHeight:!0,fontFamily:!0},outline:{outlineWidth:!0,outlineStyle:!0,outlineColor:!0}}}},function(e,t,n){'use strict';function o(e){return!!c.hasOwnProperty(e)||!u.hasOwnProperty(e)&&(l.test(e)?(c[e]=!0,!0):(u[e]=!0,void 0,!1))}function a(e,t){return null==t||e.hasBooleanValue&&!t||e.hasNumericValue&&isNaN(t)||e.hasPositiveNumericValue&&1>t||e.hasOverloadedBooleanValue&&!1===t}var r=n(16),i=n(4),d=n(9),s=n(132),p=n(1),l=new RegExp('^['+r.ATTRIBUTE_NAME_START_CHAR+']['+r.ATTRIBUTE_NAME_CHAR+']*$'),u={},c={},m={createMarkupForID:function(e){return r.ID_ATTRIBUTE_NAME+'='+s(e)},setAttributeForID:function(e,t){e.setAttribute(r.ID_ATTRIBUTE_NAME,t)},createMarkupForRoot:function(){return r.ROOT_ATTRIBUTE_NAME+'=""'},setAttributeForRoot:function(e){e.setAttribute(r.ROOT_ATTRIBUTE_NAME,'')},createMarkupForProperty:function(e,t){var n=r.properties.hasOwnProperty(e)?r.properties[e]:null;if(n){if(a(n,t))return'';var o=n.attributeName;return n.hasBooleanValue||n.hasOverloadedBooleanValue&&!0===t?o+'=""':o+'='+s(t)}return r.isCustomAttribute(e)?null==t?'':e+'='+s(t):null},createMarkupForCustomAttribute:function(e,t){return o(e)&&null!=t?e+'='+s(t):''},setValueForProperty:function(e,t,n){var o=r.properties.hasOwnProperty(t)?r.properties[t]:null;if(o){var i=o.mutationMethod;if(i)i(e,n);else{if(a(o,n))return void this.deleteValueForProperty(e,t);if(o.mustUseProperty)e[o.propertyName]=n;else{var d=o.attributeName,s=o.attributeNamespace;s?e.setAttributeNS(s,d,''+n):o.hasBooleanValue||o.hasOverloadedBooleanValue&&!0===n?e.setAttribute(d,''):e.setAttribute(d,''+n)}}}else if(r.isCustomAttribute(t))return void m.setValueForAttribute(e,t,n)},setValueForAttribute:function(e,t,n){if(o(t)){null==n?e.removeAttribute(t):e.setAttribute(t,''+n)}},deleteValueForAttribute:function(e,t){e.removeAttribute(t),!1},deleteValueForProperty:function(e,t){var n=r.properties.hasOwnProperty(t)?r.properties[t]:null;if(n){var o=n.mutationMethod;if(o)o(e,void 0);else if(n.mustUseProperty){var a=n.propertyName;e[a]=!n.hasBooleanValue&&''}else e.removeAttribute(n.attributeName)}else r.isCustomAttribute(t)&&e.removeAttribute(t)}};e.exports=m},function(e,t,n){'use strict';function o(){if(this._rootNodeID&&this._wrapperState.pendingUpdate){this._wrapperState.pendingUpdate=!1;var e=this._currentElement.props,t=d.getValue(e);null!=t&&a(this,!!e.multiple,t)}}function a(e,t,n){var o=s.getNodeFromInstance(e).options,a,r;if(t){for(a={},r=0;r<n.length;r++)a[''+n[r]]=!0;for(r=0;r<o.length;r++){var i=a.hasOwnProperty(o[r].value);o[r].selected!==i&&(o[r].selected=i)}}else{for(a=''+n,r=0;r<o.length;r++)if(o[r].value===a)return void(o[r].selected=!0);o.length&&(o[0].selected=!0)}}function r(e){var t=this._currentElement.props,n=d.executeOnChange(t,e);return this._rootNodeID&&(this._wrapperState.pendingUpdate=!0),p.asap(o,this),n}var i=n(3),d=n(57),s=n(4),p=n(11),l=n(1),u=!1,c=!1,m=['value','defaultValue'];e.exports={getHostProps:function(e,t){return i({},t,{onChange:e._wrapperState.onChange,value:void 0})},mountWrapper:function(e,t){var n=d.getValue(t);e._wrapperState={pendingUpdate:!1,initialValue:null==n?t.defaultValue:n,listeners:null,onChange:r.bind(e),wasMultiple:!!t.multiple},t.value===void 0||t.defaultValue===void 0||c||(void 0,c=!0)},getSelectValueContext:function(e){return e._wrapperState.initialValue},postUpdateWrapper:function(e){var t=e._currentElement.props;e._wrapperState.initialValue=void 0;var n=e._wrapperState.wasMultiple;e._wrapperState.wasMultiple=!!t.multiple;var o=d.getValue(t);null==o?n!==!!t.multiple&&(null==t.defaultValue?a(e,!!t.multiple,t.multiple?[]:''):a(e,!!t.multiple,t.defaultValue)):(e._wrapperState.pendingUpdate=!1,a(e,!!t.multiple,o))}}},function(e){function t(){throw new Error('setTimeout has not been defined')}function n(){throw new Error('clearTimeout has not been defined')}function o(e){if(l===setTimeout)return setTimeout(e,0);if((l===t||!l)&&setTimeout)return l=setTimeout,setTimeout(e,0);try{return l(e,0)}catch(t){try{return l.call(null,e,0)}catch(t){return l.call(this,e,0)}}}function a(e){if(u===clearTimeout)return clearTimeout(e);if((u===n||!u)&&clearTimeout)return u=clearTimeout,clearTimeout(e);try{return u(e)}catch(t){try{return u.call(null,e)}catch(t){return u.call(this,e)}}}function r(){m&&g&&(m=!1,g.length?c=g.concat(c):h=-1,c.length&&d())}function d(){if(!m){var e=o(r);m=!0;for(var t=c.length;t;){for(g=c,c=[];++h<t;)g&&g[h].run();h=-1,t=c.length}g=null,m=!1,a(e)}}function s(e,t){this.fun=e,this.array=t}function i(){}var p=e.exports={},l,u;(function(){try{l='function'==typeof setTimeout?setTimeout:t}catch(n){l=t}try{u='function'==typeof clearTimeout?clearTimeout:n}catch(t){u=n}})();var c=[],m=!1,h=-1,g;p.nextTick=function(e){var t=Array(arguments.length-1);if(1<arguments.length)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];c.push(new s(e,t)),1!==c.length||m||o(d)},s.prototype.run=function(){this.fun.apply(null,this.array)},p.title='browser',p.browser=!0,p.env={},p.argv=[],p.version='',p.versions={},p.on=i,p.addListener=i,p.once=i,p.off=i,p.removeListener=i,p.removeAllListeners=i,p.emit=i,p.prependListener=i,p.prependOnceListener=i,p.listeners=function(){return[]},p.binding=function(){throw new Error('process.binding is not supported')},p.cwd=function(){return'/'},p.chdir=function(){throw new Error('process.chdir is not supported')},p.umask=function(){return 0}},function(e,t,n){'use strict';function o(e){if(e){var t=e.getName();if(t)return' Check the render method of `'+t+'`.'}return''}function a(e){return'function'==typeof e&&'undefined'!=typeof e.prototype&&'function'==typeof e.prototype.mountComponent&&'function'==typeof e.prototype.receiveComponent}function r(e){var t;if(null===e||!1===e)t=p.create(r);else if('object'==typeof e){var n=e,d=n.type;if('function'!=typeof d&&'string'!=typeof d){var s='';!1,s+=o(n._owner),i('130',null==d?d:typeof d,s)}'string'==typeof n.type?t=l.createInternalComponent(n):a(n.type)?(t=new n.type(n),!t.getHostNode&&(t.getHostNode=t.getNativeNode)):t=new h(n)}else'string'==typeof e||'number'==typeof e?t=l.createInstanceForText(e):i('131',typeof e);return!1,t._mountIndex=0,t._mountImage=null,!1,!1,t}var i=n(2),d=n(3),s=n(141),p=n(83),l=n(84),u=n(142),c=n(0),m=n(1),h=function(e){this.construct(e)};d(h.prototype,s,{_instantiateReactComponent:r}),e.exports=r},function(e,t,n){'use strict';var o=n(2),a=n(13),r=n(0),i={HOST:0,COMPOSITE:1,EMPTY:2,getType:function(e){if(null===e||!1===e)return i.EMPTY;return a.isValidElement(e)?'function'==typeof e.type?i.COMPOSITE:i.HOST:void o('26',e)}};e.exports=i},function(e){'use strict';var t={create:function(e){return n(e)}},n;t.injection={injectEmptyComponentFactory:function(e){n=e}},e.exports=t},function(e,t,n){'use strict';var o=n(2),a=n(0),r=null,i=null;e.exports={createInternalComponent:function(e){return r?void 0:o('111',e.type),new r(e)},createInstanceForText:function(e){return new i(e)},isTextComponent:function(e){return e instanceof i},injection:{injectGenericComponentClass:function(e){r=e},injectTextComponentClass:function(e){i=e}}}},function(e,t,n){'use strict';function o(e,t){return e&&'object'==typeof e&&null!=e.key?l.escape(e.key):t.toString(36)}function a(e,t,n,p){var u=typeof e;if(('undefined'==u||'boolean'==u)&&(e=null),null===e||'string'==u||'number'==u||'object'==u&&e.$$typeof===d)return n(p,e,''===t?c+o(e,0):t),1;var h=0,g=''===t?c:t+m,f,y;if(Array.isArray(e))for(var _=0;_<e.length;_++)f=e[_],y=g+o(f,_),h+=a(f,y,n,p);else{var i=s(e);if(i){var C=i.call(e),b;if(i!==e.entries)for(var E=0;!(b=C.next()).done;)f=b.value,y=g+o(f,E++),h+=a(f,y,n,p);else for(var v;!(b=C.next()).done;)v=b.value,v&&(f=v[1],y=g+l.escape(v[0])+m+o(f,0),h+=a(f,y,n,p))}else if('object'==u){var x='',N=e+'';r('31','[object Object]'===N?'object with keys {'+Object.keys(e).join(', ')+'}':N,x)}}return h}var r=n(2),i=n(8),d=n(143),s=n(144),p=n(0),l=n(61),u=n(1),c='.',m=':';e.exports=function(e,t,n){return null==e?0:a(e,'',t,n)}},function(e,t,n){'use strict';function o(e){var t=Function.prototype.toString,n=Object.prototype.hasOwnProperty,o=RegExp('^'+t.call(n).replace(/[\\^$.*+?()[\]{}|]/g,'\\$&').replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,'$1.*?')+'$');try{var a=t.call(e);return o.test(a)}catch(e){return!1}}function a(e){var t=h(e);if(t){var n=t.childIDs;g(e),n.forEach(a)}}function r(e,t,n){return'\n in '+(e||'Unknown')+(t?' (at '+t.fileName.replace(/^.*[\\\/]/,'')+':'+t.lineNumber+')':n?' (created by '+n+')':'')}function i(e){return null==e?'#empty':'string'==typeof e||'number'==typeof e?'#text':'string'==typeof e.type?e.type:e.type.displayName||e.type.name||'Unknown'}function d(e){var t=P.getDisplayName(e),n=P.getElement(e),o=P.getOwnerID(e),a;return o&&(a=P.getDisplayName(o)),void 0,r(t,n&&n._source,a)}var s=n(10),p=n(8),l=n(0),u=n(1),c='function'==typeof Array.from&&'function'==typeof Map&&o(Map)&&null!=Map.prototype&&'function'==typeof Map.prototype.keys&&o(Map.prototype.keys)&&'function'==typeof Set&&o(Set)&&null!=Set.prototype&&'function'==typeof Set.prototype.keys&&o(Set.prototype.keys),m,h,g,f,y,_,C;if(c){var b=new Map,E=new Set;m=function(e,t){b.set(e,t)},h=function(e){return b.get(e)},g=function(e){b['delete'](e)},f=function(){return Array.from(b.keys())},y=function(e){E.add(e)},_=function(e){E['delete'](e)},C=function(){return Array.from(E.keys())}}else{var v={},x={},N=function(e){return'.'+e},T=function(e){return parseInt(e.substr(1),10)};m=function(e,t){var n=N(e);v[n]=t},h=function(e){var t=N(e);return v[t]},g=function(e){var t=N(e);delete v[t]},f=function(){return Object.keys(v).map(T)},y=function(e){var t=N(e);x[t]=!0},_=function(e){var t=N(e);delete x[t]},C=function(){return Object.keys(x).map(T)}}var k=[],P={onSetChildren:function(e,t){var n=h(e);n?void 0:s('144'),n.childIDs=t;for(var o=0;o<t.length;o++){var a=t[o],r=h(a);r?void 0:s('140'),null!=r.childIDs||'object'!=typeof r.element||null==r.element?void 0:s('141'),r.isMounted?void 0:s('71'),null==r.parentID&&(r.parentID=e),r.parentID===e?void 0:s('142',a,r.parentID,e)}},onBeforeMountComponent:function(e,t,n){m(e,{element:t,parentID:n,text:null,childIDs:[],isMounted:!1,updateCount:0})},onBeforeUpdateComponent:function(e,t){var n=h(e);n&&n.isMounted&&(n.element=t)},onMountComponent:function(e){var t=h(e);t?void 0:s('144'),t.isMounted=!0;var n=0===t.parentID;n&&y(e)},onUpdateComponent:function(e){var t=h(e);t&&t.isMounted&&t.updateCount++},onUnmountComponent:function(e){var t=h(e);if(t){t.isMounted=!1;var n=0===t.parentID;n&&_(e)}k.push(e)},purgeUnmountedComponents:function(){if(!P._preventPurging){for(var e=0,t;e<k.length;e++)t=k[e],a(t);k.length=0}},isMounted:function(e){var t=h(e);return!!t&&t.isMounted},getCurrentStackAddendum:function(e){var t='';if(e){var n=i(e),o=e._owner;t+=r(n,e._source,o&&o.getName())}var a=p.current,d=a&&a._debugID;return t+=P.getStackAddendumByID(d),t},getStackAddendumByID:function(e){for(var t='';e;)t+=d(e),e=P.getParentID(e);return t},getChildIDs:function(e){var t=h(e);return t?t.childIDs:[]},getDisplayName:function(e){var t=P.getElement(e);return t?i(t):null},getElement:function(e){var t=h(e);return t?t.element:null},getOwnerID:function(e){var t=P.getElement(e);return t&&t._owner?t._owner._debugID:null},getParentID:function(e){var t=h(e);return t?t.parentID:null},getSource:function(e){var t=h(e),n=t?t.element:null,o=null==n?null:n._source;return o},getText:function(e){var t=P.getElement(e);return'string'==typeof t?t:'number'==typeof t?''+t:null},getUpdateCount:function(e){var t=h(e);return t?t.updateCount:0},getRootIDs:C,getRegisteredIDs:f,pushNonStandardWarningStack:function(e,t){if('function'==typeof console.reactStack){var n=[],o=p.current,a=o&&o._debugID;try{for(e&&n.push({name:a?P.getDisplayName(a):null,fileName:t?t.fileName:null,lineNumber:t?t.lineNumber:null});a;){var r=P.getElement(a),i=P.getParentID(a),d=P.getOwnerID(a),s=d?P.getDisplayName(d):null,l=r&&r._source;n.push({name:s,fileName:l?l.fileName:null,lineNumber:l?l.lineNumber:null}),a=i}}catch(e){}console.reactStack(n)}},popNonStandardWarningStack:function(){'function'!=typeof console.reactStackEnd||console.reactStackEnd()}};e.exports=P},function(e,t,n){'use strict';var o=n(5);e.exports={listen:function(e,t,n){return e.addEventListener?(e.addEventListener(t,n,!1),{remove:function(){e.removeEventListener(t,n,!1)}}):e.attachEvent?(e.attachEvent('on'+t,n),{remove:function(){e.detachEvent('on'+t,n)}}):void 0},capture:function(e,t,n){return e.addEventListener?(e.addEventListener(t,n,!0),{remove:function(){e.removeEventListener(t,n,!0)}}):(!1,{remove:o})},registerDefault:function(){}}},function(e,t,n){'use strict';function o(e){return r(document.documentElement,e)}var a=n(156),r=n(158),i=n(76),d=n(89),s={hasSelectionCapabilities:function(e){var t=e&&e.nodeName&&e.nodeName.toLowerCase();return t&&('input'===t&&'text'===e.type||'textarea'===t||'true'===e.contentEditable)},getSelectionInformation:function(){var e=d();return{focusedElem:e,selectionRange:s.hasSelectionCapabilities(e)?s.getSelection(e):null}},restoreSelection:function(e){var t=d(),n=e.focusedElem,a=e.selectionRange;t!==n&&o(n)&&(s.hasSelectionCapabilities(n)&&s.setSelection(n,a),i(n))},getSelection:function(e){var t;if('selectionStart'in e)t={start:e.selectionStart,end:e.selectionEnd};else if(document.selection&&e.nodeName&&'input'===e.nodeName.toLowerCase()){var n=document.selection.createRange();n.parentElement()===e&&(t={start:-n.moveStart('character',-e.value.length),end:-n.moveEnd('character',-e.value.length)})}else t=a.getOffsets(e);return t||{start:0,end:0}},setSelection:function(e,t){var n=t.start,o=t.end;if(void 0===o&&(o=n),'selectionStart'in e)e.selectionStart=n,e.selectionEnd=Math.min(o,e.value.length);else if(document.selection&&e.nodeName&&'input'===e.nodeName.toLowerCase()){var r=e.createTextRange();r.collapse(!0),r.moveStart('character',n),r.moveEnd('character',o-n),r.select()}else a.setOffsets(e,t)}};e.exports=s},function(e){'use strict';e.exports=function(e){if(e=e||('undefined'==typeof document?void 0:document),'undefined'==typeof e)return null;try{return e.activeElement||e.body}catch(t){return e.body}}},function(e,t,n){'use strict';function o(e,t){for(var n=Math.min(e.length,t.length),o=0;o<n;o++)if(e.charAt(o)!==t.charAt(o))return o;return e.length===t.length?-1:n}function a(e){return e?e.nodeType===F?e.documentElement:e.firstChild:null}function r(e){return e.getAttribute&&e.getAttribute(O)||''}function i(e,t,n,o,a){var r;if(v.logTopLevelRenders){var i=e._currentElement.props.child,d=i.type;r='React mount: '+('string'==typeof d?d:d.displayName||d.name),console.time(r)}var s=k.mountComponent(e,n,null,b(e,t),a,0);r&&console.timeEnd(r),e._renderedComponent._topLevelWrapper=e,H._mountImageIntoNode(s,t,e,o,n)}function d(e,t,n,o){var a=I.ReactReconcileTransaction.getPooled(!n&&E.useCreateElement);a.perform(i,null,e,t,a,n,o),I.ReactReconcileTransaction.release(a)}function s(e,t,n){for(!1,k.unmountComponent(e,n),!1,t.nodeType===F&&(t=t.documentElement);t.lastChild;)t.removeChild(t.lastChild)}function p(e){var t=a(e);if(t){var n=C.getInstanceFromNode(t);return!!(n&&n._hostParent)}}function l(e){return!!(e&&(e.nodeType===U||e.nodeType===F||e.nodeType===V))}function u(e){var t=a(e),n=t&&C.getInstanceFromNode(t);return n&&!n._hostParent?n:null}function c(e){var t=u(e);return t?t._hostContainerInfo._topLevelWrapper:null}var m=n(2),h=n(18),g=n(16),f=n(13),y=n(33),_=n(8),C=n(4),b=n(173),E=n(174),v=n(71),x=n(27),N=n(9),T=n(175),k=n(17),P=n(62),I=n(11),M=n(14),S=n(81),w=n(0),R=n(31),D=n(60),A=n(1),O=g.ID_ATTRIBUTE_NAME,L=g.ROOT_ATTRIBUTE_NAME,U=1,F=9,V=11,j={},B=1,W=function(){this.rootID=B++};W.prototype.isReactComponent={},!1,W.prototype.render=function(){return this.props.child},W.isReactTopLevelWrapper=!0;var H={TopLevelWrapper:W,_instancesByReactRootID:j,scrollMonitor:function(e,t){t()},_updateRootComponent:function(e,t,n,o,a){return H.scrollMonitor(o,function(){P.enqueueElementInternal(e,t,n),a&&P.enqueueCallbackInternal(e,a)}),e},_renderNewRootComponent:function(e,t,n,o){void 0,l(t)?void 0:m('37'),y.ensureScrollValueMonitoring();var a=S(e,!1);I.batchedUpdates(d,a,t,n,o);var r=a._instance.rootID;return j[r]=a,a},renderSubtreeIntoContainer:function(e,t,n,o){return null!=e&&x.has(e)?void 0:m('38'),H._renderSubtreeIntoContainer(e,t,n,o)},_renderSubtreeIntoContainer:function(e,t,n,o){P.validateCallback(o,'ReactDOM.render'),f.isValidElement(t)?void 0:m('39','string'==typeof t?' Instead of passing a string like \'div\', pass React.createElement(\'div\') or <div />.':'function'==typeof t?' Instead of passing a class like Foo, pass React.createElement(Foo) or <Foo />.':null!=t&&void 0!==t.props?' This may be caused by unintentionally loading two independent copies of React.':''),void 0;var i=f.createElement(W,{child:t}),d;if(e){var s=x.get(e);d=s._processChildContext(s._context)}else d=M;var l=c(n);if(l){var u=l._currentElement,h=u.props.child;if(D(h,t)){var g=l._renderedComponent.getPublicInstance(),y=o&&function(){o.call(g)};return H._updateRootComponent(l,i,d,n,y),g}H.unmountComponentAtNode(n)}var _=a(n),C=_&&!!r(_),b=p(n),E=H._renderNewRootComponent(i,n,C&&!l&&!b,d)._renderedComponent.getPublicInstance();return o&&o.call(E),E},render:function(e,t,n){return H._renderSubtreeIntoContainer(null,e,t,n)},unmountComponentAtNode:function(e){void 0,l(e)?void 0:m('40'),!1;var t=c(e);if(!t){var n=p(e),o=1===e.nodeType&&e.hasAttribute(L);return!1,!1}return delete j[t._instance.rootID],I.batchedUpdates(s,t,e,!1),!0},_mountImageIntoNode:function(e,t,n,r,i){if(l(t)?void 0:m('41'),r){var d=a(t);if(T.canReuseMarkup(e,d))return void C.precacheNode(n,d);var s=d.getAttribute(T.CHECKSUM_ATTR_NAME);d.removeAttribute(T.CHECKSUM_ATTR_NAME);var p=d.outerHTML;d.setAttribute(T.CHECKSUM_ATTR_NAME,s);var u=e,c=o(u,p),g=' (client) '+u.substring(c-20,c+20)+'\n (server) '+p.substring(c-20,c+20);t.nodeType===F?m('42',g):void 0,!1}if(t.nodeType===F?m('43'):void 0,i.useCreateElement){for(;t.lastChild;)t.removeChild(t.lastChild);h.insertTreeBefore(t,e,null)}else R(t,e),C.precacheNode(n,t.firstChild)}};e.exports=H},function(e,t,n){'use strict';var o=n(82);e.exports=function(e){for(var t;(t=e._renderedNodeType)===o.COMPOSITE;)e=e._renderedComponent;if(t===o.HOST)return e._renderedComponent;return t===o.EMPTY?null:void 0}},,,,,,,,,,,,function(e,t,n){'use strict';e.exports=n(104)},function(e,t,n){'use strict';var o=n(4),a=n(105),r=n(90),i=n(17),d=n(11),s=n(177),p=n(178),l=n(91),u=n(179),c=n(1);a.inject();var m={findDOMNode:p,render:r.render,unmountComponentAtNode:r.unmountComponentAtNode,version:s,unstable_batchedUpdates:d.batchedUpdates,unstable_renderSubtreeIntoContainer:u};'undefined'!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&'function'==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.inject&&__REACT_DEVTOOLS_GLOBAL_HOOK__.inject({ComponentTree:{getClosestInstanceFromNode:o.getClosestInstanceFromNode,getNodeFromInstance:function(e){return e._renderedComponent&&(e=l(e)),e?o.getNodeFromInstance(e):null}},Mount:r,Reconciler:i});e.exports=m},function(e,t,n){'use strict';var o=n(106),a=n(107),r=n(111),i=n(114),d=n(115),s=n(116),p=n(117),l=n(123),u=n(4),c=n(148),m=n(149),h=n(150),g=n(151),f=n(152),y=n(154),_=n(155),C=n(161),b=n(162),E=n(163),v=!1;e.exports={inject:function(){v||(v=!0,y.EventEmitter.injectReactEventListener(f),y.EventPluginHub.injectEventPluginOrder(i),y.EventPluginUtils.injectComponentTree(u),y.EventPluginUtils.injectTreeTraversal(m),y.EventPluginHub.injectEventPluginsByName({SimpleEventPlugin:E,EnterLeaveEventPlugin:d,ChangeEventPlugin:r,SelectEventPlugin:b,BeforeInputEventPlugin:a}),y.HostComponent.injectGenericComponentClass(l),y.HostComponent.injectTextComponentClass(h),y.DOMProperty.injectDOMPropertyConfig(o),y.DOMProperty.injectDOMPropertyConfig(s),y.DOMProperty.injectDOMPropertyConfig(C),y.EmptyComponent.injectEmptyComponentFactory(function(e){return new c(e)}),y.Updates.injectReconcileTransaction(_),y.Updates.injectBatchingStrategy(g),y.Component.injectEnvironment(p))}}},function(e){'use strict';e.exports={Properties:{"aria-current":0,"aria-details":0,"aria-disabled":0,"aria-hidden":0,"aria-invalid":0,"aria-keyshortcuts":0,"aria-label":0,"aria-roledescription":0,"aria-autocomplete":0,"aria-checked":0,"aria-expanded":0,"aria-haspopup":0,"aria-level":0,"aria-modal":0,"aria-multiline":0,"aria-multiselectable":0,"aria-orientation":0,"aria-placeholder":0,"aria-pressed":0,"aria-readonly":0,"aria-required":0,"aria-selected":0,"aria-sort":0,"aria-valuemax":0,"aria-valuemin":0,"aria-valuenow":0,"aria-valuetext":0,"aria-atomic":0,"aria-busy":0,"aria-live":0,"aria-relevant":0,"aria-dropeffect":0,"aria-grabbed":0,"aria-activedescendant":0,"aria-colcount":0,"aria-colindex":0,"aria-colspan":0,"aria-controls":0,"aria-describedby":0,"aria-errormessage":0,"aria-flowto":0,"aria-labelledby":0,"aria-owns":0,"aria-posinset":0,"aria-rowcount":0,"aria-rowindex":0,"aria-rowspan":0,"aria-setsize":0},DOMAttributeNames:{},DOMPropertyNames:{}}},function(e,t,n){'use strict';function o(e){return(e.ctrlKey||e.altKey||e.metaKey)&&!(e.ctrlKey&&e.altKey)}function a(e){return'topCompositionStart'===e?T.compositionStart:'topCompositionEnd'===e?T.compositionEnd:'topCompositionUpdate'===e?T.compositionUpdate:void 0}function r(e,t){return'topKeyDown'===e&&t.keyCode===_}function i(e,t){return'topKeyUp'===e?-1!==y.indexOf(t.keyCode):'topKeyDown'===e?t.keyCode!==_:'topKeyPress'==e||'topMouseDown'==e||'topBlur'==e}function d(e){var t=e.detail;return'object'==typeof t&&'data'in t?t.data:null}function s(e,t,n,o){var s,p;if(C?s=a(e):P?i(e,n)&&(s=T.compositionEnd):r(e,n)&&(s=T.compositionStart),!s)return null;v&&(P||s!==T.compositionStart?s===T.compositionEnd&&P&&(p=P.getData()):P=h.getPooled(o));var l=g.getPooled(s,t,n,o);if(p)l.data=p;else{var u=d(n);null!==u&&(l.data=u)}return c.accumulateTwoPhaseDispatches(l),l}function p(e,t){switch(e){case'topCompositionEnd':return d(t);case'topKeyPress':var n=t.which;return n===x?(k=!0,N):null;case'topTextInput':var o=t.data;return o===N&&k?null:o;default:return null;}}function l(e,t){if(P){if('topCompositionEnd'===e||!C&&i(e,t)){var n=P.getData();return h.release(P),P=null,n}return null}return'topPaste'===e?null:'topKeyPress'===e?t.which&&!o(t)?String.fromCharCode(t.which):null:'topCompositionEnd'===e?v?null:t.data:null}function u(e,t,n,o){var a;if(a=E?p(e,n):l(e,n),!a)return null;var r=f.getPooled(T.beforeInput,t,n,o);return r.data=a,c.accumulateTwoPhaseDispatches(r),r}var c=n(24),m=n(6),h=n(108),g=n(109),f=n(110),y=[9,13,27,32],_=229,C=m.canUseDOM&&'CompositionEvent'in window,b=null;m.canUseDOM&&'documentMode'in document&&(b=document.documentMode);var E=m.canUseDOM&&'TextEvent'in window&&!b&&!function(){var e=window.opera;return'object'==typeof e&&'function'==typeof e.version&&12>=parseInt(e.version(),10)}(),v=m.canUseDOM&&(!C||b&&8<b&&11>=b),x=32,N=' ',T={beforeInput:{phasedRegistrationNames:{bubbled:'onBeforeInput',captured:'onBeforeInputCapture'},dependencies:['topCompositionEnd','topKeyPress','topTextInput','topPaste']},compositionEnd:{phasedRegistrationNames:{bubbled:'onCompositionEnd',captured:'onCompositionEndCapture'},dependencies:['topBlur','topCompositionEnd','topKeyDown','topKeyPress','topKeyUp','topMouseDown']},compositionStart:{phasedRegistrationNames:{bubbled:'onCompositionStart',captured:'onCompositionStartCapture'},dependencies:['topBlur','topCompositionStart','topKeyDown','topKeyPress','topKeyUp','topMouseDown']},compositionUpdate:{phasedRegistrationNames:{bubbled:'onCompositionUpdate',captured:'onCompositionUpdateCapture'},dependencies:['topBlur','topCompositionUpdate','topKeyDown','topKeyPress','topKeyUp','topMouseDown']}},k=!1,P=null;e.exports={eventTypes:T,extractEvents:function(e,t,n,o){return[s(e,t,n,o),u(e,t,n,o)]}}},function(e,t,n){'use strict';function o(e){this._root=e,this._startText=this.getText(),this._fallbackText=null}var a=n(3),r=n(15),i=n(69);a(o.prototype,{destructor:function(){this._root=null,this._startText=null,this._fallbackText=null},getText:function(){return'value'in this._root?this._root.value:this._root[i()]},getData:function(){if(this._fallbackText)return this._fallbackText;var e=this._startText,t=e.length,n=this.getText(),o=n.length,a,r;for(a=0;a<t&&e[a]===n[a];a++);var i=t-a;for(r=1;r<=i&&e[t-r]===n[o-r];r++);var d=1<r?1-r:void 0;return this._fallbackText=n.slice(a,d),this._fallbackText}}),r.addPoolingTo(o),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(12);a.augmentClass(o,{data:null}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(12);a.augmentClass(o,{data:null}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n){var o=k.getPooled(w.change,e,t,n);return o.type='change',v.accumulateTwoPhaseDispatches(o),o}function a(e){var t=e.nodeName&&e.nodeName.toLowerCase();return'select'===t||'input'===t&&'file'===e.type}function r(e){var t=o(D,e,I(e));T.batchedUpdates(i,t)}function i(e){E.enqueueEvents(e),E.processEventQueue(!1)}function d(e,t){R=e,D=t,R.attachEvent('onchange',r)}function s(){R&&(R.detachEvent('onchange',r),R=null,D=null)}function p(e,t){var n=P.updateValueIfChanged(e),o=!0===t.simulated&&L._allowSimulatedPassThrough;if(n||o)return e}function l(e,t){if('topChange'===e)return t}function u(e,t,n){'topFocus'===e?(s(),d(t,n)):'topBlur'===e&&s()}function c(e,t){R=e,D=t,R.attachEvent('onpropertychange',h)}function m(){R&&(R.detachEvent('onpropertychange',h),R=null,D=null)}function h(e){'value'!==e.propertyName||p(D,e)&&r(e)}function g(e,t,n){'topFocus'===e?(m(),c(t,n)):'topBlur'===e&&m()}function f(e,t,n){if('topSelectionChange'===e||'topKeyUp'===e||'topKeyDown'===e)return p(D,n)}function y(e){var t=e.nodeName;return t&&'input'===t.toLowerCase()&&('checkbox'===e.type||'radio'===e.type)}function _(e,t,n){if('topClick'===e)return p(t,n)}function C(e,t,n){if('topInput'===e||'topChange'===e)return p(t,n)}function b(e,t){if(null!=e){var n=e._wrapperState||t._wrapperState;if(n&&n.controlled&&'number'===t.type){var o=''+t.value;t.getAttribute('value')!==o&&t.setAttribute('value',o)}}}var E=n(25),v=n(24),x=n(6),N=n(4),T=n(11),k=n(12),P=n(72),I=n(51),M=n(52),S=n(73),w={change:{phasedRegistrationNames:{bubbled:'onChange',captured:'onChangeCapture'},dependencies:['topBlur','topChange','topClick','topFocus','topInput','topKeyDown','topKeyUp','topSelectionChange']}},R=null,D=null,A=!1;x.canUseDOM&&(A=M('change')&&(!document.documentMode||8<document.documentMode));var O=!1;x.canUseDOM&&(O=M('input')&&(!('documentMode'in document)||9<document.documentMode));var L={eventTypes:w,_allowSimulatedPassThrough:!0,_isInputEventSupported:O,extractEvents:function(e,t,n,r){var i=t?N.getNodeFromInstance(t):window,d,s;if(a(i)?A?d=l:s=u:S(i)?O?d=C:(d=f,s=g):y(i)&&(d=_),d){var p=d(e,t,n);if(p){var c=o(p,n,r);return c}}s&&s(e,i,t),'topBlur'===e&&b(t,i)}};e.exports=L},function(e,t,n){'use strict';function o(e,t,n){'function'==typeof e?e(t.getPublicInstance()):r.addComponentAsRefTo(t,e,n)}function a(e,t,n){'function'==typeof e?e(null):r.removeComponentAsRefFrom(t,e,n)}var r=n(113),i={};i.attachRefs=function(e,t){if(null!==t&&'object'==typeof t){var n=t.ref;null!=n&&o(n,e,t._owner)}},i.shouldUpdateRefs=function(e,t){var n=null,o=null;null!==e&&'object'==typeof e&&(n=e.ref,o=e._owner);var a=null,r=null;return null!==t&&'object'==typeof t&&(a=t.ref,r=t._owner),n!==a||'string'==typeof a&&r!==o},i.detachRefs=function(e,t){if(null!==t&&'object'==typeof t){var n=t.ref;null!=n&&a(n,e,t._owner)}},e.exports=i},function(e,t,n){'use strict';function o(e){return!!(e&&'function'==typeof e.attachRef&&'function'==typeof e.detachRef)}var a=n(2),r=n(0);e.exports={addComponentAsRefTo:function(e,t,n){o(n)?void 0:a('119'),n.attachRef(t,e)},removeComponentAsRefFrom:function(e,t,n){o(n)?void 0:a('120');var r=n.getPublicInstance();r&&r.refs[t]===e.getPublicInstance()&&n.detachRef(t)}}},function(e){'use strict';e.exports=['ResponderEventPlugin','SimpleEventPlugin','TapEventPlugin','EnterLeaveEventPlugin','ChangeEventPlugin','SelectEventPlugin','BeforeInputEventPlugin']},function(e,t,n){'use strict';var o=n(24),a=n(4),r=n(30),i={mouseEnter:{registrationName:'onMouseEnter',dependencies:['topMouseOut','topMouseOver']},mouseLeave:{registrationName:'onMouseLeave',dependencies:['topMouseOut','topMouseOver']}};e.exports={eventTypes:i,extractEvents:function(e,t,n,d){if('topMouseOver'===e&&(n.relatedTarget||n.fromElement))return null;if('topMouseOut'!==e&&'topMouseOver'!==e)return null;var s;if(d.window===d)s=d;else{var p=d.ownerDocument;s=p?p.defaultView||p.parentWindow:window}var l,u;if('topMouseOut'===e){l=t;var c=n.relatedTarget||n.toElement;u=c?a.getClosestInstanceFromNode(c):null}else l=null,u=t;if(l===u)return null;var m=null==l?s:a.getNodeFromInstance(l),h=null==u?s:a.getNodeFromInstance(u),g=r.getPooled(i.mouseLeave,l,n,d);g.type='mouseleave',g.target=m,g.relatedTarget=h;var f=r.getPooled(i.mouseEnter,u,n,d);return f.type='mouseenter',f.target=h,f.relatedTarget=m,o.accumulateEnterLeaveDispatches(g,f,l,u),[g,f]}}},function(e,t,n){'use strict';var o=n(16),a=o.injection.MUST_USE_PROPERTY,r=o.injection.HAS_BOOLEAN_VALUE,i=o.injection.HAS_NUMERIC_VALUE,d=o.injection.HAS_POSITIVE_NUMERIC_VALUE,s=o.injection.HAS_OVERLOADED_BOOLEAN_VALUE,p={isCustomAttribute:RegExp.prototype.test.bind(new RegExp('^(data|aria)-['+o.ATTRIBUTE_NAME_CHAR+']*$')),Properties:{accept:0,acceptCharset:0,accessKey:0,action:0,allowFullScreen:r,allowTransparency:0,alt:0,as:0,async:r,autoComplete:0,autoPlay:r,capture:r,cellPadding:0,cellSpacing:0,charSet:0,challenge:0,checked:a|r,cite:0,classID:0,className:0,cols:d,colSpan:0,content:0,contentEditable:0,contextMenu:0,controls:r,coords:0,crossOrigin:0,data:0,dateTime:0,default:r,defer:r,dir:0,disabled:r,download:s,draggable:0,encType:0,form:0,formAction:0,formEncType:0,formMethod:0,formNoValidate:r,formTarget:0,frameBorder:0,headers:0,height:0,hidden:r,high:0,href:0,hrefLang:0,htmlFor:0,httpEquiv:0,icon:0,id:0,inputMode:0,integrity:0,is:0,keyParams:0,keyType:0,kind:0,label:0,lang:0,list:0,loop:r,low:0,manifest:0,marginHeight:0,marginWidth:0,max:0,maxLength:0,media:0,mediaGroup:0,method:0,min:0,minLength:0,multiple:a|r,muted:a|r,name:0,nonce:0,noValidate:r,open:r,optimum:0,pattern:0,placeholder:0,playsInline:r,poster:0,preload:0,profile:0,radioGroup:0,readOnly:r,referrerPolicy:0,rel:0,required:r,reversed:r,role:0,rows:d,rowSpan:i,sandbox:0,scope:0,scoped:r,scrolling:0,seamless:r,selected:a|r,shape:0,size:d,sizes:0,span:d,spellCheck:0,src:0,srcDoc:0,srcLang:0,srcSet:0,start:i,step:0,style:0,summary:0,tabIndex:0,target:0,title:0,type:0,useMap:0,value:0,width:0,wmode:0,wrap:0,about:0,datatype:0,inlist:0,prefix:0,property:0,resource:0,typeof:0,vocab:0,autoCapitalize:0,autoCorrect:0,autoSave:0,color:0,itemProp:0,itemScope:r,itemType:0,itemID:0,itemRef:0,results:0,security:0,unselectable:0},DOMAttributeNames:{acceptCharset:'accept-charset',className:'class',htmlFor:'for',httpEquiv:'http-equiv'},DOMPropertyNames:{},DOMMutationMethods:{value:function(e,t){return null==t?e.removeAttribute('value'):void('number'!==e.type||!1===e.hasAttribute('value')?e.setAttribute('value',''+t):e.validity&&!e.validity.badInput&&e.ownerDocument.activeElement!==e&&e.setAttribute('value',''+t))}}};e.exports=p},function(e,t,n){'use strict';var o=n(54),a=n(122),r={processChildrenUpdates:a.dangerouslyProcessChildrenUpdates,replaceNodeWithMarkup:o.dangerouslyReplaceNodeWithMarkup};e.exports=r},function(e,t,n){'use strict';var o=n(2),a=n(18),r=n(6),i=n(119),d=n(5),s=n(0);e.exports={dangerouslyReplaceNodeWithMarkup:function(e,t){if(r.canUseDOM?void 0:o('56'),t?void 0:o('57'),'HTML'===e.nodeName?o('58'):void 0,'string'==typeof t){var n=i(t,d)[0];e.parentNode.replaceChild(n,e)}else a.replaceChildWithTree(e,t)}}},function(e,t,n){'use strict';function o(e){var t=e.match(p);return t&&t[1].toLowerCase()}var a=n(6),r=n(120),i=n(121),d=n(0),s=a.canUseDOM?document.createElement('div'):null,p=/^\s*<(\w+)/;e.exports=function(e,t){var n=s;!!s?void 0:d(!1);var a=o(e),p=a&&i(a);if(p){n.innerHTML=p[1]+e+p[2];for(var l=p[0];l--;)n=n.lastChild}else n.innerHTML=e;var u=n.getElementsByTagName('script');u.length&&(t?void 0:d(!1),r(u).forEach(t));for(var c=Array.from(n.childNodes);n.lastChild;)n.removeChild(n.lastChild);return c}},function(e,t,n){'use strict';function o(e){var t=e.length;if(Array.isArray(e)||'object'!=typeof e&&'function'!=typeof e?r(!1):void 0,'number'==typeof t?void 0:r(!1),0===t||t-1 in e?void 0:r(!1),'function'==typeof e.callee?r(!1):void 0,e.hasOwnProperty)try{return Array.prototype.slice.call(e)}catch(t){}for(var n=Array(t),o=0;o<t;o++)n[o]=e[o];return n}function a(e){return!!e&&('object'==typeof e||'function'==typeof e)&&'length'in e&&!('setInterval'in e)&&'number'!=typeof e.nodeType&&(Array.isArray(e)||'callee'in e||'item'in e)}var r=n(0);e.exports=function(e){return a(e)?Array.isArray(e)?e.slice():o(e):[e]}},function(e,t,n){'use strict';var o=n(6),a=n(0),r=o.canUseDOM?document.createElement('div'):null,i={},d=[1,'<select multiple="true">','</select>'],s=[1,'<table>','</table>'],p=[3,'<table><tbody><tr>','</tr></tbody></table>'],l=[1,'<svg xmlns="http://www.w3.org/2000/svg">','</svg>'],u={"*":[1,'?<div>','</div>'],area:[1,'<map>','</map>'],col:[2,'<table><tbody></tbody><colgroup>','</colgroup></table>'],legend:[1,'<fieldset>','</fieldset>'],param:[1,'<object>','</object>'],tr:[2,'<table><tbody>','</tbody></table>'],optgroup:d,option:d,caption:s,colgroup:s,tbody:s,tfoot:s,thead:s,td:p,th:p};['circle','clipPath','defs','ellipse','g','image','line','linearGradient','mask','path','pattern','polygon','polyline','radialGradient','rect','stop','text','tspan'].forEach(function(e){u[e]=l,i[e]=!0}),e.exports=function(e){return r?void 0:a(!1),u.hasOwnProperty(e)||(e='*'),i.hasOwnProperty(e)||(r.innerHTML='*'===e?'<link />':'<'+e+'></'+e+'>',i[e]=!r.firstChild),i[e]?u[e]:null}},function(e,t,n){'use strict';var o=n(54),a=n(4);e.exports={dangerouslyProcessChildrenUpdates:function(e,t){var n=a.getNodeFromInstance(e);o.processUpdates(n,t)}}},function(e,t,n){'use strict';function o(e){if(e){var t=e._currentElement._owner||null;if(t){var n=t.getName();if(n)return' This DOM node was rendered by `'+n+'`.'}}return''}function a(e){if('object'==typeof e){if(Array.isArray(e))return'['+e.map(a).join(', ')+']';var t=[];for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n)){var o=/^[a-z$_][\w$_]*$/i.test(n)?n:JSON.stringify(n);t.push(o+': '+a(e[n]))}return'{'+t.join(', ')+'}'}return'string'==typeof e?JSON.stringify(e):'function'==typeof e?'[function object]':e+''}function r(e,t){t&&(ae[e._tag]&&(null==t.children&&null==t.dangerouslySetInnerHTML?void 0:y('137',e._tag,e._currentElement._owner?' Check the render method of '+e._currentElement._owner.getName()+'.':'')),null!=t.dangerouslySetInnerHTML&&(null==t.children?void 0:y('60'),'object'==typeof t.dangerouslySetInnerHTML&&$ in t.dangerouslySetInnerHTML?void 0:y('61')),!1,null==t.style||'object'==typeof t.style?void 0:y('62',o(e)))}function i(e,t,n,o){if(!(o instanceof L)){var a=e._hostContainerInfo,r=a._node&&a._node.nodeType===J,i=r?a._node:a._ownerDocument;z(t,i),o.getReactMountReady().enqueue(d,{inst:e,registrationName:t,listener:n})}}function d(){var e=this;T.putListener(e.inst,e.registrationName,e.listener)}function s(){var e=this;S.postMountWrapper(e)}function p(){var e=this;D.postMountWrapper(e)}function l(){var e=this;w.postMountWrapper(e)}function u(){W.track(this)}function c(){var e=this;e._rootNodeID?void 0:y('63');var t=Y(e);switch(t?void 0:y('64'),e._tag){case'iframe':case'object':e._wrapperState.listeners=[P.trapBubbledEvent('topLoad','load',t)];break;case'video':case'audio':for(var n in e._wrapperState.listeners=[],te)te.hasOwnProperty(n)&&e._wrapperState.listeners.push(P.trapBubbledEvent(n,te[n],t));break;case'source':e._wrapperState.listeners=[P.trapBubbledEvent('topError','error',t)];break;case'img':e._wrapperState.listeners=[P.trapBubbledEvent('topError','error',t),P.trapBubbledEvent('topLoad','load',t)];break;case'form':e._wrapperState.listeners=[P.trapBubbledEvent('topReset','reset',t),P.trapBubbledEvent('topSubmit','submit',t)];break;case'input':case'select':case'textarea':e._wrapperState.listeners=[P.trapBubbledEvent('topInvalid','invalid',t)];}}function m(){R.postUpdateWrapper(this)}function h(e){de.call(ie,e)||(re.test(e)?void 0:y('65',e),ie[e]=!0)}function g(e,t){return 0<=e.indexOf('-')||null!=t.is}function f(e){var t=e.type;h(t),this._currentElement=e,this._tag=t.toLowerCase(),this._namespaceURI=null,this._renderedChildren=null,this._previousStyle=null,this._previousStyleCopy=null,this._hostNode=null,this._hostParent=null,this._rootNodeID=0,this._domID=0,this._hostContainerInfo=null,this._wrapperState=null,this._topLevelWrapper=null,this._flags=0,!1}var y=n(2),_=n(3),C=n(124),b=n(125),E=n(18),v=n(55),x=n(16),N=n(78),T=n(25),k=n(48),P=n(33),I=n(66),M=n(4),S=n(135),w=n(137),R=n(79),D=n(138),A=n(9),O=n(139),L=n(146),U=n(5),F=n(32),V=n(0),j=n(52),B=n(59),W=n(72),H=n(63),q=n(1),K=T.deleteListener,Y=M.getNodeFromInstance,z=P.listenTo,X=k.registrationNameModules,G={string:!0,number:!0},Q='style',$='__html',Z={children:null,dangerouslySetInnerHTML:null,suppressContentEditableWarning:null},J=11,ee={};var te={topAbort:'abort',topCanPlay:'canplay',topCanPlayThrough:'canplaythrough',topDurationChange:'durationchange',topEmptied:'emptied',topEncrypted:'encrypted',topEnded:'ended',topError:'error',topLoadedData:'loadeddata',topLoadedMetadata:'loadedmetadata',topLoadStart:'loadstart',topPause:'pause',topPlay:'play',topPlaying:'playing',topProgress:'progress',topRateChange:'ratechange',topSeeked:'seeked',topSeeking:'seeking',topStalled:'stalled',topSuspend:'suspend',topTimeUpdate:'timeupdate',topVolumeChange:'volumechange',topWaiting:'waiting'},ne={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},oe={listing:!0,pre:!0,textarea:!0},ae=_({menuitem:!0},ne),re=/^[a-zA-Z][a-zA-Z:_\.\-\d]*$/,ie={},de={}.hasOwnProperty,se=1;f.displayName='ReactDOMComponent',f.Mixin={mountComponent:function(e,t,n,o){this._rootNodeID=se++,this._domID=n._idCounter++,this._hostParent=t,this._hostContainerInfo=n;var a=this._currentElement.props;switch(this._tag){case'audio':case'form':case'iframe':case'img':case'link':case'object':case'source':case'video':this._wrapperState={listeners:null},e.getReactMountReady().enqueue(c,this);break;case'input':S.mountWrapper(this,a,t),a=S.getHostProps(this,a),e.getReactMountReady().enqueue(u,this),e.getReactMountReady().enqueue(c,this);break;case'option':w.mountWrapper(this,a,t),a=w.getHostProps(this,a);break;case'select':R.mountWrapper(this,a,t),a=R.getHostProps(this,a),e.getReactMountReady().enqueue(c,this);break;case'textarea':D.mountWrapper(this,a,t),a=D.getHostProps(this,a),e.getReactMountReady().enqueue(u,this),e.getReactMountReady().enqueue(c,this);}r(this,a);var i,d;null==t?n._tag&&(i=n._namespaceURI,d=n._tag):(i=t._namespaceURI,d=t._tag),(null==i||i===v.svg&&'foreignobject'===d)&&(i=v.html),i===v.html&&('svg'===this._tag?i=v.svg:'math'===this._tag&&(i=v.mathml)),this._namespaceURI=i;var m;if(e.useCreateElement){var h=n._ownerDocument,g;if(!(i===v.html))g=h.createElementNS(i,this._currentElement.type);else if('script'===this._tag){var f=h.createElement('div'),y=this._currentElement.type;f.innerHTML='<'+y+'></'+y+'>',g=f.removeChild(f.firstChild)}else g=a.is?h.createElement(this._currentElement.type,a.is):h.createElement(this._currentElement.type);M.precacheNode(this,g),this._flags|=I.hasCachedChildNodes,this._hostParent||N.setAttributeForRoot(g),this._updateDOMProperties(null,a,e);var _=E(g);this._createInitialChildren(e,a,o,_),m=_}else{var b=this._createOpenTagMarkupAndPutListeners(e,a),x=this._createContentMarkup(e,a,o);m=!x&&ne[this._tag]?b+'/>':b+'>'+x+'</'+this._currentElement.type+'>'}switch(this._tag){case'input':e.getReactMountReady().enqueue(s,this),a.autoFocus&&e.getReactMountReady().enqueue(C.focusDOMComponent,this);break;case'textarea':e.getReactMountReady().enqueue(p,this),a.autoFocus&&e.getReactMountReady().enqueue(C.focusDOMComponent,this);break;case'select':a.autoFocus&&e.getReactMountReady().enqueue(C.focusDOMComponent,this);break;case'button':a.autoFocus&&e.getReactMountReady().enqueue(C.focusDOMComponent,this);break;case'option':e.getReactMountReady().enqueue(l,this);}return m},_createOpenTagMarkupAndPutListeners:function(e,t){var n='<'+this._currentElement.type;for(var o in t)if(t.hasOwnProperty(o)){var a=t[o];if(null!=a)if(X.hasOwnProperty(o))a&&i(this,o,a,e);else{o==Q&&(a&&(!1,a=this._previousStyleCopy=_({},t.style)),a=b.createMarkupForStyles(a,this));var r=null;null!=this._tag&&g(this._tag,t)?!Z.hasOwnProperty(o)&&(r=N.createMarkupForCustomAttribute(o,a)):r=N.createMarkupForProperty(o,a),r&&(n+=' '+r)}}return e.renderToStaticMarkup?n:(this._hostParent||(n+=' '+N.createMarkupForRoot()),n+=' '+N.createMarkupForID(this._domID),n)},_createContentMarkup:function(e,t,n){var o='',a=t.dangerouslySetInnerHTML;if(null!=a)null!=a.__html&&(o=a.__html);else{var r=G[typeof t.children]?t.children:null,i=null==r?t.children:null;if(null!=r)o=F(r),!1;else if(null!=i){var d=this.mountChildren(i,e,n);o=d.join('')}}return oe[this._tag]&&'\n'===o.charAt(0)?'\n'+o:o},_createInitialChildren:function(e,t,n,o){var a=t.dangerouslySetInnerHTML;if(null!=a)null!=a.__html&&E.queueHTML(o,a.__html);else{var r=G[typeof t.children]?t.children:null,d=null==r?t.children:null;if(null!=r)''!==r&&(!1,E.queueText(o,r));else if(null!=d)for(var s=this.mountChildren(d,e,n),p=0;p<s.length;p++)E.queueChild(o,s[p])}},receiveComponent:function(e,t,n){var o=this._currentElement;this._currentElement=e,this.updateComponent(t,o,e,n)},updateComponent:function(e,t,n,o){var a=t.props,i=this._currentElement.props;switch(this._tag){case'input':a=S.getHostProps(this,a),i=S.getHostProps(this,i);break;case'option':a=w.getHostProps(this,a),i=w.getHostProps(this,i);break;case'select':a=R.getHostProps(this,a),i=R.getHostProps(this,i);break;case'textarea':a=D.getHostProps(this,a),i=D.getHostProps(this,i);}switch(r(this,i),this._updateDOMProperties(a,i,e),this._updateDOMChildren(a,i,e,o),this._tag){case'input':S.updateWrapper(this);break;case'textarea':D.updateWrapper(this);break;case'select':e.getReactMountReady().enqueue(m,this);}},_updateDOMProperties:function(e,t,n){var o,a,r;for(o in e)if(!t.hasOwnProperty(o)&&e.hasOwnProperty(o)&&null!=e[o])if(o===Q){var d=this._previousStyleCopy;for(a in d)d.hasOwnProperty(a)&&(r=r||{},r[a]='');this._previousStyleCopy=null}else X.hasOwnProperty(o)?e[o]&&K(this,o):g(this._tag,e)?Z.hasOwnProperty(o)||N.deleteValueForAttribute(Y(this),o):(x.properties[o]||x.isCustomAttribute(o))&&N.deleteValueForProperty(Y(this),o);for(o in t){var s=t[o],p=o===Q?this._previousStyleCopy:null==e?void 0:e[o];if(t.hasOwnProperty(o)&&s!==p&&(null!=s||null!=p))if(o===Q){if(s?(!1,s=this._previousStyleCopy=_({},s)):this._previousStyleCopy=null,p){for(a in p)!p.hasOwnProperty(a)||s&&s.hasOwnProperty(a)||(r=r||{},r[a]='');for(a in s)s.hasOwnProperty(a)&&p[a]!==s[a]&&(r=r||{},r[a]=s[a])}else r=s;}else if(X.hasOwnProperty(o))s?i(this,o,s,n):p&&K(this,o);else if(g(this._tag,t))Z.hasOwnProperty(o)||N.setValueForAttribute(Y(this),o,s);else if(x.properties[o]||x.isCustomAttribute(o)){var l=Y(this);null==s?N.deleteValueForProperty(l,o):N.setValueForProperty(l,o,s)}}r&&b.setValueForStyles(Y(this),r,this)},_updateDOMChildren:function(e,t,n,o){var a=G[typeof e.children]?e.children:null,r=G[typeof t.children]?t.children:null,i=e.dangerouslySetInnerHTML&&e.dangerouslySetInnerHTML.__html,d=t.dangerouslySetInnerHTML&&t.dangerouslySetInnerHTML.__html,s=null==a?e.children:null,p=null==r?t.children:null;null!=s&&null==p?this.updateChildren(null,n,o):(null!=a||null!=i)&&!(null!=r||null!=d)&&(this.updateTextContent(''),!1),null==r?null==d?null!=p&&(!1,this.updateChildren(p,n,o)):(i!==d&&this.updateMarkup(''+d),!1):a!==r&&(this.updateTextContent(''+r),!1)},getHostNode:function(){return Y(this)},unmountComponent:function(e){switch(this._tag){case'audio':case'form':case'iframe':case'img':case'link':case'object':case'source':case'video':var t=this._wrapperState.listeners;if(t)for(var n=0;n<t.length;n++)t[n].remove();break;case'input':case'textarea':W.stopTracking(this);break;case'html':case'head':case'body':y('66',this._tag);}this.unmountChildren(e),M.uncacheNode(this),T.deleteAllListeners(this),this._rootNodeID=0,this._domID=0,this._wrapperState=null,!1},getPublicInstance:function(){return Y(this)}},_(f.prototype,f.Mixin,O.Mixin),e.exports=f},function(e,t,n){'use strict';var o=n(4),a=n(76);e.exports={focusDOMComponent:function(){a(o.getNodeFromInstance(this))}}},function(e,t,n){'use strict';var o=n(77),a=n(6),r=n(9),i=n(126),d=n(128),s=n(129),p=n(131),l=n(1),u=p(function(e){return s(e)}),c=!1,m='cssFloat';if(a.canUseDOM){var h=document.createElement('div').style;try{h.font=''}catch(t){c=!0}document.documentElement.style.cssFloat===void 0&&(m='styleFloat')}e.exports={createMarkupForStyles:function(e,t){var n='';for(var o in e)if(e.hasOwnProperty(o)){var a=0===o.indexOf('--'),r=e[o];!1,null!=r&&(n+=u(o)+':',n+=d(o,r,t,a)+';')}return n||null},setValueForStyles:function(e,t,n){var a=e.style;for(var r in t)if(t.hasOwnProperty(r)){var i=0===r.indexOf('--');var s=d(r,t[r],n,i);if(('float'==r||'cssFloat'==r)&&(r=m),i)a.setProperty(r,s);else if(s)a[r]=s;else{var p=c&&o.shorthandPropertyExpansions[r];if(p)for(var l in p)a[l]='';else a[r]=''}}}}},function(e,t,n){'use strict';var o=n(127),a=/^-ms-/;e.exports=function(e){return o(e.replace(a,'ms-'))}},function(e){'use strict';var t=/-(.)/g;e.exports=function(e){return e.replace(t,function(e,t){return t.toUpperCase()})}},function(e,t,n){'use strict';var o=n(77),a=n(1),r=o.isUnitlessNumber;e.exports=function(e,t,n,o){var a=null==t||'boolean'==typeof t||''===t;if(a)return'';var i=isNaN(t);if(o||i||0===t||r.hasOwnProperty(e)&&r[e])return''+t;if('string'==typeof t){t=t.trim()}return t+'px'}},function(e,t,n){'use strict';var o=n(130),a=/^ms-/;e.exports=function(e){return o(e).replace(a,'-ms-')}},function(e){'use strict';var t=/([A-Z])/g;e.exports=function(e){return e.replace(t,'-$1').toLowerCase()}},function(e){'use strict';e.exports=function(e){var t={};return function(n){return t.hasOwnProperty(n)||(t[n]=e.call(this,n)),t[n]}}},function(e,t,n){'use strict';var o=n(32);e.exports=function(e){return'"'+o(e)+'"'}},function(e,t,n){'use strict';function o(e){a.enqueueEvents(e),a.processEventQueue(!1)}var a=n(25);e.exports={handleTopLevel:function(e,t,n,r){var i=a.extractEvents(e,t,n,r);o(i)}}},function(e,t,n){'use strict';function o(e,t){var n={};return n[e.toLowerCase()]=t.toLowerCase(),n['Webkit'+e]='webkit'+t,n['Moz'+e]='moz'+t,n['ms'+e]='MS'+t,n['O'+e]='o'+t.toLowerCase(),n}var a=n(6),r={animationend:o('Animation','AnimationEnd'),animationiteration:o('Animation','AnimationIteration'),animationstart:o('Animation','AnimationStart'),transitionend:o('Transition','TransitionEnd')},i={},d={};a.canUseDOM&&(d=document.createElement('div').style,!('AnimationEvent'in window)&&(delete r.animationend.animation,delete r.animationiteration.animation,delete r.animationstart.animation),!('TransitionEvent'in window)&&delete r.transitionend.transition),e.exports=function(e){if(i[e])return i[e];if(!r[e])return e;var t=r[e];for(var n in t)if(t.hasOwnProperty(n)&&n in d)return i[e]=t[n];return''}},function(e,t,n){'use strict';function o(){this._rootNodeID&&h.updateWrapper(this)}function a(e){var t='checkbox'===e.type||'radio'===e.type;return t?null!=e.checked:null!=e.value}function r(e){var t=this._currentElement.props,n=p.executeOnChange(t,e);u.asap(o,this);var a=t.name;if('radio'===t.type&&null!=a){for(var r=l.getNodeFromInstance(this),d=r;d.parentNode;)d=d.parentNode;for(var s=d.querySelectorAll('input[name='+JSON.stringify(''+a)+'][type="radio"]'),c=0,m;c<s.length;c++)if(m=s[c],m!==r&&m.form===r.form){var h=l.getInstanceFromNode(m);h?void 0:i('90'),u.asap(o,h)}}return n}var i=n(2),d=n(3),s=n(78),p=n(57),l=n(4),u=n(11),c=n(0),m=n(1),h={getHostProps:function(e,t){var n=p.getValue(t),o=p.getChecked(t),a=d({type:void 0,step:void 0,min:void 0,max:void 0},t,{defaultChecked:void 0,defaultValue:void 0,value:null==n?e._wrapperState.initialValue:n,checked:null==o?e._wrapperState.initialChecked:o,onChange:e._wrapperState.onChange});return a},mountWrapper:function(e,t){var n=t.defaultValue;e._wrapperState={initialChecked:null==t.checked?t.defaultChecked:t.checked,initialValue:null==t.value?n:t.value,listeners:null,onChange:r.bind(e),controlled:a(t)}},updateWrapper:function(e){var t=e._currentElement.props;var n=t.checked;null!=n&&s.setValueForProperty(l.getNodeFromInstance(e),'checked',n||!1);var o=l.getNodeFromInstance(e),a=p.getValue(t);if(!(null!=a))null==t.value&&null!=t.defaultValue&&o.defaultValue!==''+t.defaultValue&&(o.defaultValue=''+t.defaultValue),null==t.checked&&null!=t.defaultChecked&&(o.defaultChecked=!!t.defaultChecked);else if(0===a&&''===o.value)o.value='0';else if('number'===t.type){var r=parseFloat(o.value,10)||0;(a!=r||a==r&&o.value!=a)&&(o.value=''+a)}else o.value!==''+a&&(o.value=''+a)},postMountWrapper:function(e){var t=e._currentElement.props,n=l.getNodeFromInstance(e);switch(t.type){case'submit':case'reset':break;case'color':case'date':case'datetime':case'datetime-local':case'month':case'time':case'week':n.value='',n.value=n.defaultValue;break;default:n.value=n.value;}var o=n.name;''!==o&&(n.name=''),n.defaultChecked=!n.defaultChecked,n.defaultChecked=!n.defaultChecked,''!==o&&(n.name=o)}};e.exports=h},function(e){'use strict';e.exports='SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED'},function(e,t,n){'use strict';function o(e){var t='';return r.Children.forEach(e,function(e){null==e||('string'==typeof e||'number'==typeof e?t+=e:!p&&(p=!0,void 0))}),t}var a=n(3),r=n(13),i=n(4),d=n(79),s=n(1),p=!1;e.exports={mountWrapper:function(e,t,n){var a=null;if(null!=n){var r=n;'optgroup'===r._tag&&(r=r._hostParent),null!=r&&'select'===r._tag&&(a=d.getSelectValueContext(r))}var s=null;if(null!=a){var p;if(p=null==t.value?o(t.children):t.value+'',s=!1,Array.isArray(a)){for(var l=0;l<a.length;l++)if(''+a[l]===p){s=!0;break}}else s=''+a===p}e._wrapperState={selected:s}},postMountWrapper:function(e){var t=e._currentElement.props;if(null!=t.value){var n=i.getNodeFromInstance(e);n.setAttribute('value',t.value)}},getHostProps:function(e,t){var n=a({selected:void 0,children:void 0},t);null!=e._wrapperState.selected&&(n.selected=e._wrapperState.selected);var r=o(t.children);return r&&(n.children=r),n}}},function(e,t,n){'use strict';function o(){this._rootNodeID&&c.updateWrapper(this)}function a(e){var t=this._currentElement.props,n=d.executeOnChange(t,e);return p.asap(o,this),n}var r=n(2),i=n(3),d=n(57),s=n(4),p=n(11),l=n(0),u=n(1),c={getHostProps:function(e,t){null==t.dangerouslySetInnerHTML?void 0:r('91');var n=i({},t,{value:void 0,defaultValue:void 0,children:''+e._wrapperState.initialValue,onChange:e._wrapperState.onChange});return n},mountWrapper:function(e,t){var n=d.getValue(t),o=n;if(null==n){var i=t.defaultValue,s=t.children;null!=s&&(!1,null==i?void 0:r('92'),Array.isArray(s)&&(1>=s.length?void 0:r('93'),s=s[0]),i=''+s),null==i&&(i=''),o=i}e._wrapperState={initialValue:''+o,listeners:null,onChange:a.bind(e)}},updateWrapper:function(e){var t=e._currentElement.props,n=s.getNodeFromInstance(e),o=d.getValue(t);if(null!=o){var a=''+o;a!==n.value&&(n.value=a),null==t.defaultValue&&(n.defaultValue=a)}null!=t.defaultValue&&(n.defaultValue=t.defaultValue)},postMountWrapper:function(e){var t=s.getNodeFromInstance(e),n=t.textContent;n===e._wrapperState.initialValue&&(t.value=n)}};e.exports=c},function(e,t,n){'use strict';function o(e,t,n){return{type:'INSERT_MARKUP',content:e,fromIndex:null,fromNode:null,toIndex:n,afterNode:t}}function a(e,t,n){return{type:'MOVE_EXISTING',content:null,fromIndex:e._mountIndex,fromNode:g.getHostNode(e),toIndex:n,afterNode:t}}function r(e,t){return{type:'REMOVE_NODE',content:null,fromIndex:e._mountIndex,fromNode:t,toIndex:null,afterNode:null}}function i(e){return{type:'SET_MARKUP',content:e,fromIndex:null,fromNode:null,toIndex:null,afterNode:null}}function d(e){return{type:'TEXT_CONTENT',content:e,fromIndex:null,fromNode:null,toIndex:null,afterNode:null}}function s(e,t){return t&&(e=e||[],e.push(t)),e}function p(e,t){u.processChildrenUpdates(e,t)}var l=n(2),u=n(58),c=n(27),m=n(9),h=n(8),g=n(17),f=n(140),y=n(5),_=n(145),C=n(0);e.exports={Mixin:{_reconcilerInstantiateChildren:function(e,t,n){return f.instantiateChildren(e,t,n)},_reconcilerUpdateChildren:function(e,t,n,o,a,r){var i=0,d;return d=_(t,i),f.updateChildren(e,d,n,o,a,this,this._hostContainerInfo,r,i),d},mountChildren:function(e,t,n){var o=this._reconcilerInstantiateChildren(e,t,n);this._renderedChildren=o;var a=[],r=0;for(var i in o)if(o.hasOwnProperty(i)){var d=o[i];var s=g.mountComponent(d,t,this,this._hostContainerInfo,n,0);d._mountIndex=r++,a.push(s)}return!1,a},updateTextContent:function(e){var t=this._renderedChildren;for(var n in f.unmountChildren(t,!1),t)t.hasOwnProperty(n)&&l('118');var o=[d(e)];p(this,o)},updateMarkup:function(e){var t=this._renderedChildren;for(var n in f.unmountChildren(t,!1),t)t.hasOwnProperty(n)&&l('118');var o=[i(e)];p(this,o)},updateChildren:function(e,t,n){this._updateChildren(e,t,n)},_updateChildren:function(e,t,n){var o=Math.max,a=this._renderedChildren,r={},i=[],d=this._reconcilerUpdateChildren(a,e,i,r,t,n);if(d||a){var l=null,u=0,c=0,m=0,h=null,f;for(f in d)if(d.hasOwnProperty(f)){var y=a&&a[f],_=d[f];y===_?(l=s(l,this.moveChild(y,h,u,c)),c=o(y._mountIndex,c),y._mountIndex=u):(y&&(c=o(y._mountIndex,c)),l=s(l,this._mountChildAtIndex(_,i[m],h,u,t,n)),m++),u++,h=g.getHostNode(_)}for(f in r)r.hasOwnProperty(f)&&(l=s(l,this._unmountChild(a[f],r[f])));l&&p(this,l),this._renderedChildren=d,!1}},unmountChildren:function(e){var t=this._renderedChildren;f.unmountChildren(t,e),this._renderedChildren=null},moveChild:function(e,t,n,o){if(e._mountIndex<o)return a(e,t,n)},createChild:function(e,t,n){return o(n,t,e._mountIndex)},removeChild:function(e,t){return r(e,t)},_mountChildAtIndex:function(e,t,n,o){return e._mountIndex=o,this.createChild(e,n,t)},_unmountChild:function(e,t){var n=this.removeChild(e,t);return e._mountIndex=null,n}}}},function(e,t,n){'use strict';(function(t){function o(e,t,n){var o=e[n]===void 0;!1,null!=t&&o&&(e[n]=r(t,!0))}var a=n(17),r=n(81),i=n(61),d=n(60),s=n(85),p=n(1);e.exports={instantiateChildren:function(e,t,n,a){if(null==e)return null;var r={};return s(e,o,r),r},updateChildren:function(e,t,n,o,i,s,p,l,u){if(t||e){var c,m;for(c in t)if(t.hasOwnProperty(c)){m=e&&e[c];var h=m&&m._currentElement,g=t[c];if(null!=m&&d(h,g))a.receiveComponent(m,g,i,l),t[c]=m;else{m&&(o[c]=a.getHostNode(m),a.unmountComponent(m,!1));var f=r(g,!0);t[c]=f;var y=a.mountComponent(f,i,s,p,l,u);n.push(y)}}for(c in e)e.hasOwnProperty(c)&&!(t&&t.hasOwnProperty(c))&&(m=e[c],o[c]=a.getHostNode(m),a.unmountComponent(m,!1))}},unmountChildren:function(e,t){for(var n in e)if(e.hasOwnProperty(n)){var o=e[n];a.unmountComponent(o,t)}}}}).call(t,n(80))},function(e,t,n){'use strict';function o(){}function a(){}function r(e){return!!(e.prototype&&e.prototype.isReactComponent)}function i(e){return!!(e.prototype&&e.prototype.isPureReactComponent)}function d(e,t,n){if(0===t)return e();g.debugTool.onBeginLifeCycleTimer(t,n);try{return e()}finally{g.debugTool.onEndLifeCycleTimer(t,n)}}var s=n(2),p=n(3),l=n(13),u=n(58),c=n(8),m=n(50),h=n(27),g=n(9),f=n(82),y=n(17);var _=n(14),C=n(0),b=n(59),E=n(60),v=n(1),x={ImpureClass:0,PureClass:1,StatelessFunctional:2};o.prototype.render=function(){var e=h.get(this)._currentElement.type,t=e(this.props,this.context,this.updater);return a(e,t),t};var N=1;e.exports={construct:function(e){this._currentElement=e,this._rootNodeID=0,this._compositeType=null,this._instance=null,this._hostParent=null,this._hostContainerInfo=null,this._updateBatchNumber=null,this._pendingElement=null,this._pendingStateQueue=null,this._pendingReplaceState=!1,this._pendingForceUpdate=!1,this._renderedNodeType=null,this._renderedComponent=null,this._context=null,this._mountOrder=0,this._topLevelWrapper=null,this._pendingCallbacks=null,this._calledComponentWillUnmount=!1,!1},mountComponent:function(e,t,n,d){var p=this;this._context=d,this._mountOrder=N++,this._hostParent=t,this._hostContainerInfo=n;var u=this._currentElement.props,c=this._processContext(d),m=this._currentElement.type,g=e.getUpdateQueue(),f=r(m),y=this._constructComponent(f,u,c,g),C;f||null!=y&&null!=y.render?i(m)?this._compositeType=x.PureClass:this._compositeType=x.ImpureClass:(C=y,a(m,C),null===y||!1===y||l.isValidElement(y)?void 0:s('105',m.displayName||m.name||'Component'),y=new o(m),this._compositeType=x.StatelessFunctional);y.props=u,y.context=c,y.refs=_,y.updater=g,this._instance=y,h.set(y,this),!1;var b=y.state;void 0===b&&(y.state=b=null),'object'!=typeof b||Array.isArray(b)?s('106',this.getName()||'ReactCompositeComponent'):void 0,this._pendingStateQueue=null,this._pendingReplaceState=!1,this._pendingForceUpdate=!1;var E;return E=y.unstable_handleError?this.performInitialMountWithErrorHandling(C,t,n,e,d):this.performInitialMount(C,t,n,e,d),y.componentDidMount&&e.getReactMountReady().enqueue(y.componentDidMount,y),E},_constructComponent:function(e,t,n,o){return this._constructComponentWithoutOwner(e,t,n,o)},_constructComponentWithoutOwner:function(e,t,n,o){var a=this._currentElement.type;return e?new a(t,n,o):a(t,n,o)},performInitialMountWithErrorHandling:function(t,n,o,a,r){var i=a.checkpoint(),d;try{d=this.performInitialMount(t,n,o,a,r)}catch(s){a.rollback(i),this._instance.unstable_handleError(s),this._pendingStateQueue&&(this._instance.state=this._processPendingState(this._instance.props,this._instance.context)),i=a.checkpoint(),this._renderedComponent.unmountComponent(!0),a.rollback(i),d=this.performInitialMount(t,n,o,a,r)}return d},performInitialMount:function(e,t,n,o,a){var r=this._instance,i=0;!1,r.componentWillMount&&(r.componentWillMount(),this._pendingStateQueue&&(r.state=this._processPendingState(r.props,r.context))),e===void 0&&(e=this._renderValidatedComponent());var d=f.getType(e);this._renderedNodeType=d;var s=this._instantiateReactComponent(e,d!==f.EMPTY);this._renderedComponent=s;var p=y.mountComponent(s,o,t,n,this._processChildContext(a),i);return p},getHostNode:function(){return y.getHostNode(this._renderedComponent)},unmountComponent:function(e){if(this._renderedComponent){var t=this._instance;if(t.componentWillUnmount&&!t._calledComponentWillUnmount)if(t._calledComponentWillUnmount=!0,e){var n=this.getName()+'.componentWillUnmount()';m.invokeGuardedCallback(n,t.componentWillUnmount.bind(t))}else t.componentWillUnmount();this._renderedComponent&&(y.unmountComponent(this._renderedComponent,e),this._renderedNodeType=null,this._renderedComponent=null,this._instance=null),this._pendingStateQueue=null,this._pendingReplaceState=!1,this._pendingForceUpdate=!1,this._pendingCallbacks=null,this._pendingElement=null,this._context=null,this._rootNodeID=0,this._topLevelWrapper=null,h.remove(t)}},_maskContext:function(e){var t=this._currentElement.type,n=t.contextTypes;if(!n)return _;var o={};for(var a in n)o[a]=e[a];return o},_processContext:function(e){var t=this._maskContext(e);return t},_processChildContext:function(e){var t=this._currentElement.type,n=this._instance,o;if(n.getChildContext&&(o=n.getChildContext()),o){for(var a in'object'==typeof t.childContextTypes?void 0:s('107',this.getName()||'ReactCompositeComponent'),!1,o)a in t.childContextTypes?void 0:s('108',this.getName()||'ReactCompositeComponent',a);return p({},e,o)}return e},_checkContextTypes:function(){},receiveComponent:function(e,t,n){var o=this._currentElement,a=this._context;this._pendingElement=null,this.updateComponent(t,o,e,a,n)},performUpdateIfNecessary:function(e){null==this._pendingElement?null!==this._pendingStateQueue||this._pendingForceUpdate?this.updateComponent(e,this._currentElement,this._currentElement,this._context,this._context):this._updateBatchNumber=null:y.receiveComponent(this,this._pendingElement,e,this._context)},updateComponent:function(e,t,n,o,a){var r=this._instance;null!=r?void 0:s('136',this.getName()||'ReactCompositeComponent');var i=!1,d;this._context===a?d=r.context:(d=this._processContext(a),i=!0);var p=t.props,l=n.props;t!==n&&(i=!0),i&&r.componentWillReceiveProps&&r.componentWillReceiveProps(l,d);var u=this._processPendingState(l,d),c=!0;this._pendingForceUpdate||(r.shouldComponentUpdate?c=r.shouldComponentUpdate(l,u,d):this._compositeType===x.PureClass&&(c=!b(p,l)||!b(r.state,u))),!1,this._updateBatchNumber=null,c?(this._pendingForceUpdate=!1,this._performComponentUpdate(n,l,u,d,e,a)):(this._currentElement=n,this._context=a,r.props=l,r.state=u,r.context=d)},_processPendingState:function(e,t){var n=this._instance,o=this._pendingStateQueue,a=this._pendingReplaceState;if(this._pendingReplaceState=!1,this._pendingStateQueue=null,!o)return n.state;if(a&&1===o.length)return o[0];for(var r=p({},a?o[0]:n.state),d=a?1:0,i;d<o.length;d++)i=o[d],p(r,'function'==typeof i?i.call(n,r,e,t):i);return r},_performComponentUpdate:function(e,t,n,o,a,r){var i=this,d=this._instance,s=!!d.componentDidUpdate,p,l,u;s&&(p=d.props,l=d.state,u=d.context),d.componentWillUpdate&&d.componentWillUpdate(t,n,o),this._currentElement=e,this._context=r,d.props=t,d.state=n,d.context=o,this._updateRenderedComponent(a,r),s&&a.getReactMountReady().enqueue(d.componentDidUpdate.bind(d,p,l,u),d)},_updateRenderedComponent:function(e,t){var n=this._renderedComponent,o=n._currentElement,a=this._renderValidatedComponent();if(!1,E(o,a))y.receiveComponent(n,a,e,this._processChildContext(t));else{var r=y.getHostNode(n);y.unmountComponent(n,!1);var i=f.getType(a);this._renderedNodeType=i;var d=this._instantiateReactComponent(a,i!==f.EMPTY);this._renderedComponent=d;var s=y.mountComponent(d,e,this._hostParent,this._hostContainerInfo,this._processChildContext(t),0);this._replaceNodeWithMarkup(r,s,n)}},_replaceNodeWithMarkup:function(e,t,n){u.replaceNodeWithMarkup(e,t,n)},_renderValidatedComponentWithoutOwnerOrContext:function(){var e=this._instance,t;return t=e.render(),!1,t},_renderValidatedComponent:function(){var e;if(this._compositeType!==x.StatelessFunctional){c.current=this;try{e=this._renderValidatedComponentWithoutOwnerOrContext()}finally{c.current=null}}else e=this._renderValidatedComponentWithoutOwnerOrContext();return null===e||!1===e||l.isValidElement(e)?void 0:s('109',this.getName()||'ReactCompositeComponent'),e},attachRef:function(e,t){var n=this.getPublicInstance();null!=n?void 0:s('110');var o=t.getPublicInstance();var a=n.refs===_?n.refs={}:n.refs;a[e]=o},detachRef:function(e){var t=this.getPublicInstance().refs;delete t[e]},getName:function(){var e=this._currentElement.type,t=this._instance&&this._instance.constructor;return e.displayName||t&&t.displayName||e.name||t&&t.name||null},getPublicInstance:function(){var e=this._instance;return this._compositeType===x.StatelessFunctional?null:e},_instantiateReactComponent:null}},function(e){'use strict';var t=1;e.exports=function(){return t++}},function(e){'use strict';var t='function'==typeof Symbol&&Symbol['for']&&Symbol['for']('react.element')||60103;e.exports=t},function(e){'use strict';var t='function'==typeof Symbol&&Symbol.iterator;e.exports=function(e){var n=e&&(t&&e[t]||e['@@iterator']);if('function'==typeof n)return n}},function(e,t,n){'use strict';(function(t){function o(e,t,n){if(e&&'object'==typeof e){var o=e,a=o[n]===void 0;!1,a&&null!=t&&(o[n]=t)}}var a=n(61),r=n(85),i=n(1);'undefined'!=typeof t&&{NODE_ENV:'production'}&&!1,e.exports=function(e,t){if(null==e)return e;var n={};return r(e,o,n),n}}).call(t,n(80))},function(e,t,n){'use strict';function o(e){this.reinitializeTransaction(),this.renderToStaticMarkup=e,this.useCreateElement=!1,this.updateQueue=new s(this)}var a=n(3),r=n(15),i=n(29),d=n(9),s=n(147),p=[];var l={enqueue:function(){}};a(o.prototype,i,{getTransactionWrappers:function(){return p},getReactMountReady:function(){return l},getUpdateQueue:function(){return this.updateQueue},destructor:function(){},checkpoint:function(){},rollback:function(){}}),r.addPoolingTo(o),e.exports=o},function(e,t,n){'use strict';function o(e,t){if(!(e instanceof t))throw new TypeError('Cannot call a class as a function')}function a(){}var r=n(62),i=n(1),d=function(){function e(t){o(this,e),this.transaction=t}return e.prototype.isMounted=function(){return!1},e.prototype.enqueueCallback=function(e,t,n){this.transaction.isInTransaction()&&r.enqueueCallback(e,t,n)},e.prototype.enqueueForceUpdate=function(e){this.transaction.isInTransaction()?r.enqueueForceUpdate(e):a(e,'forceUpdate')},e.prototype.enqueueReplaceState=function(e,t){this.transaction.isInTransaction()?r.enqueueReplaceState(e,t):a(e,'replaceState')},e.prototype.enqueueSetState=function(e,t){this.transaction.isInTransaction()?r.enqueueSetState(e,t):a(e,'setState')},e}();e.exports=d},function(e,t,n){'use strict';var o=n(3),a=n(18),r=n(4),i=function(){this._currentElement=null,this._hostNode=null,this._hostParent=null,this._hostContainerInfo=null,this._domID=0};o(i.prototype,{mountComponent:function(e,t,n){var o=n._idCounter++;this._domID=o,this._hostParent=t,this._hostContainerInfo=n;var i=' react-empty: '+this._domID+' ';if(e.useCreateElement){var d=n._ownerDocument,s=d.createComment(i);return r.precacheNode(this,s),a(s)}return e.renderToStaticMarkup?'':'<!--'+i+'-->'},receiveComponent:function(){},getHostNode:function(){return r.getNodeFromInstance(this)},unmountComponent:function(){r.uncacheNode(this)}}),e.exports=i},function(e,t,n){'use strict';function o(e,t){'_hostNode'in e?void 0:a('33'),'_hostNode'in t?void 0:a('33');for(var n=0,o=e;o;o=o._hostParent)n++;for(var r=0,i=t;i;i=i._hostParent)r++;for(;0<n-r;)e=e._hostParent,n--;for(;0<r-n;)t=t._hostParent,r--;for(var d=n;d--;){if(e===t)return e;e=e._hostParent,t=t._hostParent}return null}var a=n(2),r=n(0);e.exports={isAncestor:function(e,t){for(('_hostNode'in e)?void 0:a('35'),('_hostNode'in t)?void 0:a('35');t;){if(t===e)return!0;t=t._hostParent}return!1},getLowestCommonAncestor:o,getParentInstance:function(e){return'_hostNode'in e?void 0:a('36'),e._hostParent},traverseTwoPhase:function(e,t,n){for(var o=[];e;)o.push(e),e=e._hostParent;var a;for(a=o.length;0<a--;)t(o[a],'captured',n);for(a=0;a<o.length;a++)t(o[a],'bubbled',n)},traverseEnterLeave:function(e,t,n,a,r){for(var d=e&&t?o(e,t):null,s=[];e&&e!==d;)s.push(e),e=e._hostParent;for(var p=[];t&&t!==d;)p.push(t),t=t._hostParent;var l;for(l=0;l<s.length;l++)n(s[l],'bubbled',a);for(l=p.length;0<l--;)n(p[l],'captured',r)}}},function(e,t,n){'use strict';var o=n(2),a=n(3),r=n(54),i=n(18),d=n(4),s=n(32),p=n(0),l=n(63),u=function(e){this._currentElement=e,this._stringText=''+e,this._hostNode=null,this._hostParent=null,this._domID=0,this._mountIndex=0,this._closingComment=null,this._commentNodes=null};a(u.prototype,{mountComponent:function(e,t,n){var o=n._idCounter++,a=' react-text: '+o+' ',r=' /react-text ';if(this._domID=o,this._hostParent=t,e.useCreateElement){var p=n._ownerDocument,l=p.createComment(a),u=p.createComment(r),c=i(p.createDocumentFragment());return i.queueChild(c,i(l)),this._stringText&&i.queueChild(c,i(p.createTextNode(this._stringText))),i.queueChild(c,i(u)),d.precacheNode(this,l),this._closingComment=u,c}var m=s(this._stringText);return e.renderToStaticMarkup?m:'<!--'+a+'-->'+m+'<!--'+r+'-->'},receiveComponent:function(e){if(e!==this._currentElement){this._currentElement=e;var t=''+e;if(t!==this._stringText){this._stringText=t;var n=this.getHostNode();r.replaceDelimitedText(n[0],n[1],t)}}},getHostNode:function(){var e=this._commentNodes;if(e)return e;if(!this._closingComment)for(var t=d.getNodeFromInstance(this),n=t.nextSibling;;){if(null==n?o('67',this._domID):void 0,8===n.nodeType&&' /react-text '===n.nodeValue){this._closingComment=n;break}n=n.nextSibling}return e=[this._hostNode,this._closingComment],this._commentNodes=e,e},unmountComponent:function(){this._closingComment=null,this._commentNodes=null,d.uncacheNode(this)}}),e.exports=u},function(e,t,n){'use strict';function o(){this.reinitializeTransaction()}var a=n(3),r=n(11),i=n(29),d=n(5),s={initialize:d,close:r.flushBatchedUpdates.bind(r)},p=[s,{initialize:d,close:function(){u.isBatchingUpdates=!1}}];a(o.prototype,i,{getTransactionWrappers:function(){return p}});var l=new o,u={isBatchingUpdates:!1,batchedUpdates:function(t,n,o,a,r,i){var e=u.isBatchingUpdates;return u.isBatchingUpdates=!0,e?t(n,o,a,r,i):l.perform(t,null,n,o,a,r,i)}};e.exports=u},function(e,t,n){'use strict';function o(e){for(;e._hostParent;)e=e._hostParent;var t=u.getNodeFromInstance(e),n=t.parentNode;return u.getClosestInstanceFromNode(n)}function a(e,t){this.topLevelType=e,this.nativeEvent=t,this.ancestors=[]}function r(e){var t=m(e.nativeEvent),n=u.getClosestInstanceFromNode(t),a=n;do e.ancestors.push(a),a=a&&o(a);while(a);for(var r=0;r<e.ancestors.length;r++)n=e.ancestors[r],g._handleTopLevel(e.topLevelType,n,e.nativeEvent,m(e.nativeEvent))}function i(e){var t=h(window);e(t)}var d=n(3),s=n(87),p=n(6),l=n(15),u=n(4),c=n(11),m=n(51),h=n(153);d(a.prototype,{destructor:function(){this.topLevelType=null,this.nativeEvent=null,this.ancestors.length=0}}),l.addPoolingTo(a,l.twoArgumentPooler);var g={_enabled:!0,_handleTopLevel:null,WINDOW_HANDLE:p.canUseDOM?window:null,setHandleTopLevel:function(e){g._handleTopLevel=e},setEnabled:function(e){g._enabled=!!e},isEnabled:function(){return g._enabled},trapBubbledEvent:function(e,t,n){return n?s.listen(n,t,g.dispatchEvent.bind(null,e)):null},trapCapturedEvent:function(e,t,n){return n?s.capture(n,t,g.dispatchEvent.bind(null,e)):null},monitorScrollValue:function(e){var t=i.bind(null,e);s.listen(window,'scroll',t)},dispatchEvent:function(e,t){if(g._enabled){var n=a.getPooled(e,t);try{c.batchedUpdates(r,n)}finally{a.release(n)}}}};e.exports=g},function(e){'use strict';e.exports=function(e){return e.Window&&e instanceof e.Window?{x:e.pageXOffset||e.document.documentElement.scrollLeft,y:e.pageYOffset||e.document.documentElement.scrollTop}:{x:e.scrollLeft,y:e.scrollTop}}},function(e,t,n){'use strict';var o=n(16),a=n(25),r=n(49),i=n(58),d=n(83),s=n(33),p=n(84),l=n(11),u={Component:i.injection,DOMProperty:o.injection,EmptyComponent:d.injection,EventPluginHub:a.injection,EventPluginUtils:r.injection,EventEmitter:s.injection,HostComponent:p.injection,Updates:l.injection};e.exports=u},function(e,t,n){'use strict';function o(e){this.reinitializeTransaction(),this.renderToStaticMarkup=!1,this.reactMountReady=r.getPooled(null),this.useCreateElement=e}var a=n(3),r=n(70),i=n(15),d=n(33),s=n(88),p=n(9),l=n(29),u=n(62),c={initialize:s.getSelectionInformation,close:s.restoreSelection},m=[c,{initialize:function(){var e=d.isEnabled();return d.setEnabled(!1),e},close:function(e){d.setEnabled(e)}},{initialize:function(){this.reactMountReady.reset()},close:function(){this.reactMountReady.notifyAll()}}];a(o.prototype,l,{getTransactionWrappers:function(){return m},getReactMountReady:function(){return this.reactMountReady},getUpdateQueue:function(){return u},checkpoint:function(){return this.reactMountReady.checkpoint()},rollback:function(e){this.reactMountReady.rollback(e)},destructor:function(){r.release(this.reactMountReady),this.reactMountReady=null}}),i.addPoolingTo(o),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return e===n&&t===o}var a=Math.min,r=n(6),i=n(157),d=n(69),s=r.canUseDOM&&'selection'in document&&!('getSelection'in window),p={getOffsets:s?function(e){var t=document.selection,n=t.createRange(),o=n.text.length,a=n.duplicate();a.moveToElementText(e),a.setEndPoint('EndToStart',n);var r=a.text.length;return{start:r,end:r+o}}:function(e){var t=window.getSelection&&window.getSelection();if(!t||0===t.rangeCount)return null;var n=t.anchorNode,a=t.anchorOffset,r=t.focusNode,i=t.focusOffset,d=t.getRangeAt(0);try{d.startContainer.nodeType,d.endContainer.nodeType}catch(t){return null}var s=o(t.anchorNode,t.anchorOffset,t.focusNode,t.focusOffset),p=s?0:d.toString().length,l=d.cloneRange();l.selectNodeContents(e),l.setEnd(d.startContainer,d.startOffset);var u=o(l.startContainer,l.startOffset,l.endContainer,l.endOffset),c=u?0:l.toString().length,m=c+p,h=document.createRange();h.setStart(n,a),h.setEnd(r,i);var g=h.collapsed;return{start:g?m:c,end:g?c:m}},setOffsets:s?function(e,t){var n=document.selection.createRange().duplicate(),o,a;void 0===t.end?(o=t.start,a=o):t.start>t.end?(o=t.end,a=t.start):(o=t.start,a=t.end),n.moveToElementText(e),n.moveStart('character',o),n.setEndPoint('EndToStart',n),n.moveEnd('character',a-o),n.select()}:function(e,t){if(window.getSelection){var n=window.getSelection(),o=e[d()].length,r=a(t.start,o),s=void 0===t.end?r:a(t.end,o);if(!n.extend&&r>s){var p=s;s=r,r=p}var l=i(e,r),u=i(e,s);if(l&&u){var c=document.createRange();c.setStart(l.node,l.offset),n.removeAllRanges(),r>s?(n.addRange(c),n.extend(u.node,u.offset)):(c.setEnd(u.node,u.offset),n.addRange(c))}}}};e.exports=p},function(e){'use strict';function t(e){for(;e&&e.firstChild;)e=e.firstChild;return e}function n(e){for(;e;){if(e.nextSibling)return e.nextSibling;e=e.parentNode}}e.exports=function(e,o){for(var a=t(e),r=0,i=0;a;){if(3===a.nodeType){if(i=r+a.textContent.length,r<=o&&i>=o)return{node:a,offset:o-r};r=i}a=t(n(a))}}},function(e,t,n){'use strict';function o(e,t){return e&&t&&(e===t||!a(e)&&(a(t)?o(e,t.parentNode):'contains'in e?e.contains(t):!!e.compareDocumentPosition&&!!(16&e.compareDocumentPosition(t))))}var a=n(159);e.exports=o},function(e,t,n){'use strict';var o=n(160);e.exports=function(e){return o(e)&&3==e.nodeType}},function(e){'use strict';e.exports=function(e){var t=e?e.ownerDocument||e:document,n=t.defaultView||window;return!!(e&&('function'==typeof n.Node?e instanceof n.Node:'object'==typeof e&&'number'==typeof e.nodeType&&'string'==typeof e.nodeName))}},function(e){'use strict';var t={xlink:'http://www.w3.org/1999/xlink',xml:'http://www.w3.org/XML/1998/namespace'},n={accentHeight:'accent-height',accumulate:0,additive:0,alignmentBaseline:'alignment-baseline',allowReorder:'allowReorder',alphabetic:0,amplitude:0,arabicForm:'arabic-form',ascent:0,attributeName:'attributeName',attributeType:'attributeType',autoReverse:'autoReverse',azimuth:0,baseFrequency:'baseFrequency',baseProfile:'baseProfile',baselineShift:'baseline-shift',bbox:0,begin:0,bias:0,by:0,calcMode:'calcMode',capHeight:'cap-height',clip:0,clipPath:'clip-path',clipRule:'clip-rule',clipPathUnits:'clipPathUnits',colorInterpolation:'color-interpolation',colorInterpolationFilters:'color-interpolation-filters',colorProfile:'color-profile',colorRendering:'color-rendering',contentScriptType:'contentScriptType',contentStyleType:'contentStyleType',cursor:0,cx:0,cy:0,d:0,decelerate:0,descent:0,diffuseConstant:'diffuseConstant',direction:0,display:0,divisor:0,dominantBaseline:'dominant-baseline',dur:0,dx:0,dy:0,edgeMode:'edgeMode',elevation:0,enableBackground:'enable-background',end:0,exponent:0,externalResourcesRequired:'externalResourcesRequired',fill:0,fillOpacity:'fill-opacity',fillRule:'fill-rule',filter:0,filterRes:'filterRes',filterUnits:'filterUnits',floodColor:'flood-color',floodOpacity:'flood-opacity',focusable:0,fontFamily:'font-family',fontSize:'font-size',fontSizeAdjust:'font-size-adjust',fontStretch:'font-stretch',fontStyle:'font-style',fontVariant:'font-variant',fontWeight:'font-weight',format:0,from:0,fx:0,fy:0,g1:0,g2:0,glyphName:'glyph-name',glyphOrientationHorizontal:'glyph-orientation-horizontal',glyphOrientationVertical:'glyph-orientation-vertical',glyphRef:'glyphRef',gradientTransform:'gradientTransform',gradientUnits:'gradientUnits',hanging:0,horizAdvX:'horiz-adv-x',horizOriginX:'horiz-origin-x',ideographic:0,imageRendering:'image-rendering',in:0,in2:0,intercept:0,k:0,k1:0,k2:0,k3:0,k4:0,kernelMatrix:'kernelMatrix',kernelUnitLength:'kernelUnitLength',kerning:0,keyPoints:'keyPoints',keySplines:'keySplines',keyTimes:'keyTimes',lengthAdjust:'lengthAdjust',letterSpacing:'letter-spacing',lightingColor:'lighting-color',limitingConeAngle:'limitingConeAngle',local:0,markerEnd:'marker-end',markerMid:'marker-mid',markerStart:'marker-start',markerHeight:'markerHeight',markerUnits:'markerUnits',markerWidth:'markerWidth',mask:0,maskContentUnits:'maskContentUnits',maskUnits:'maskUnits',mathematical:0,mode:0,numOctaves:'numOctaves',offset:0,opacity:0,operator:0,order:0,orient:0,orientation:0,origin:0,overflow:0,overlinePosition:'overline-position',overlineThickness:'overline-thickness',paintOrder:'paint-order',panose1:'panose-1',pathLength:'pathLength',patternContentUnits:'patternContentUnits',patternTransform:'patternTransform',patternUnits:'patternUnits',pointerEvents:'pointer-events',points:0,pointsAtX:'pointsAtX',pointsAtY:'pointsAtY',pointsAtZ:'pointsAtZ',preserveAlpha:'preserveAlpha',preserveAspectRatio:'preserveAspectRatio',primitiveUnits:'primitiveUnits',r:0,radius:0,refX:'refX',refY:'refY',renderingIntent:'rendering-intent',repeatCount:'repeatCount',repeatDur:'repeatDur',requiredExtensions:'requiredExtensions',requiredFeatures:'requiredFeatures',restart:0,result:0,rotate:0,rx:0,ry:0,scale:0,seed:0,shapeRendering:'shape-rendering',slope:0,spacing:0,specularConstant:'specularConstant',specularExponent:'specularExponent',speed:0,spreadMethod:'spreadMethod',startOffset:'startOffset',stdDeviation:'stdDeviation',stemh:0,stemv:0,stitchTiles:'stitchTiles',stopColor:'stop-color',stopOpacity:'stop-opacity',strikethroughPosition:'strikethrough-position',strikethroughThickness:'strikethrough-thickness',string:0,stroke:0,strokeDasharray:'stroke-dasharray',strokeDashoffset:'stroke-dashoffset',strokeLinecap:'stroke-linecap',strokeLinejoin:'stroke-linejoin',strokeMiterlimit:'stroke-miterlimit',strokeOpacity:'stroke-opacity',strokeWidth:'stroke-width',surfaceScale:'surfaceScale',systemLanguage:'systemLanguage',tableValues:'tableValues',targetX:'targetX',targetY:'targetY',textAnchor:'text-anchor',textDecoration:'text-decoration',textRendering:'text-rendering',textLength:'textLength',to:0,transform:0,u1:0,u2:0,underlinePosition:'underline-position',underlineThickness:'underline-thickness',unicode:0,unicodeBidi:'unicode-bidi',unicodeRange:'unicode-range',unitsPerEm:'units-per-em',vAlphabetic:'v-alphabetic',vHanging:'v-hanging',vIdeographic:'v-ideographic',vMathematical:'v-mathematical',values:0,vectorEffect:'vector-effect',version:0,vertAdvY:'vert-adv-y',vertOriginX:'vert-origin-x',vertOriginY:'vert-origin-y',viewBox:'viewBox',viewTarget:'viewTarget',visibility:0,widths:0,wordSpacing:'word-spacing',writingMode:'writing-mode',x:0,xHeight:'x-height',x1:0,x2:0,xChannelSelector:'xChannelSelector',xlinkActuate:'xlink:actuate',xlinkArcrole:'xlink:arcrole',xlinkHref:'xlink:href',xlinkRole:'xlink:role',xlinkShow:'xlink:show',xlinkTitle:'xlink:title',xlinkType:'xlink:type',xmlBase:'xml:base',xmlns:0,xmlnsXlink:'xmlns:xlink',xmlLang:'xml:lang',xmlSpace:'xml:space',y:0,y1:0,y2:0,yChannelSelector:'yChannelSelector',z:0,zoomAndPan:'zoomAndPan'},o={Properties:{},DOMAttributeNamespaces:{xlinkActuate:t.xlink,xlinkArcrole:t.xlink,xlinkHref:t.xlink,xlinkRole:t.xlink,xlinkShow:t.xlink,xlinkTitle:t.xlink,xlinkType:t.xlink,xmlBase:t.xml,xmlLang:t.xml,xmlSpace:t.xml},DOMAttributeNames:{}};Object.keys(n).forEach(function(e){o.Properties[e]=0,n[e]&&(o.DOMAttributeNames[e]=n[e])}),e.exports=o},function(e,t,n){'use strict';function o(e){if('selectionStart'in e&&s.hasSelectionCapabilities(e))return{start:e.selectionStart,end:e.selectionEnd};if(window.getSelection){var t=window.getSelection();return{anchorNode:t.anchorNode,anchorOffset:t.anchorOffset,focusNode:t.focusNode,focusOffset:t.focusOffset}}if(document.selection){var n=document.selection.createRange();return{parentElement:n.parentElement(),text:n.text,top:n.boundingTop,left:n.boundingLeft}}}function a(e,t){if(_||null==g||g!==l())return null;var n=o(g);if(!y||!c(y,n)){y=n;var a=p.getPooled(h.select,f,e,t);return a.type='select',a.target=g,r.accumulateTwoPhaseDispatches(a),a}return null}var r=n(24),i=n(6),d=n(4),s=n(88),p=n(12),l=n(89),u=n(73),c=n(59),m=i.canUseDOM&&'documentMode'in document&&11>=document.documentMode,h={select:{phasedRegistrationNames:{bubbled:'onSelect',captured:'onSelectCapture'},dependencies:['topBlur','topContextMenu','topFocus','topKeyDown','topKeyUp','topMouseDown','topMouseUp','topSelectionChange']}},g=null,f=null,y=null,_=!1,C=!1;e.exports={eventTypes:h,extractEvents:function(e,t,n,o){if(!C)return null;var r=t?d.getNodeFromInstance(t):window;switch(e){case'topFocus':(u(r)||'true'===r.contentEditable)&&(g=r,f=t,y=null);break;case'topBlur':g=null,f=null,y=null;break;case'topMouseDown':_=!0;break;case'topContextMenu':case'topMouseUp':return _=!1,a(n,o);case'topSelectionChange':if(m)break;case'topKeyDown':case'topKeyUp':return a(n,o);}return null},didPutListener:function(e,t){'onSelect'===t&&(C=!0)}}},function(e,t,n){'use strict';function o(e){return'.'+e._rootNodeID}function a(e){return'button'===e||'input'===e||'select'===e||'textarea'===e}var r=n(2),i=n(87),d=n(24),s=n(4),p=n(164),l=n(165),u=n(12),c=n(166),m=n(167),h=n(30),g=n(169),f=n(170),y=n(171),_=n(26),C=n(172),b=n(5),E=n(64),v=n(0),x={},N={};['abort','animationEnd','animationIteration','animationStart','blur','canPlay','canPlayThrough','click','contextMenu','copy','cut','doubleClick','drag','dragEnd','dragEnter','dragExit','dragLeave','dragOver','dragStart','drop','durationChange','emptied','encrypted','ended','error','focus','input','invalid','keyDown','keyPress','keyUp','load','loadedData','loadedMetadata','loadStart','mouseDown','mouseMove','mouseOut','mouseOver','mouseUp','paste','pause','play','playing','progress','rateChange','reset','scroll','seeked','seeking','stalled','submit','suspend','timeUpdate','touchCancel','touchEnd','touchMove','touchStart','transitionEnd','volumeChange','waiting','wheel'].forEach(function(e){var t=e[0].toUpperCase()+e.slice(1),n='on'+t,o='top'+t,a={phasedRegistrationNames:{bubbled:n,captured:n+'Capture'},dependencies:[o]};x[e]=a,N[o]=a});var T={};e.exports={eventTypes:x,extractEvents:function(e,t,n,o){var a=N[e];if(!a)return null;var i;switch(e){case'topAbort':case'topCanPlay':case'topCanPlayThrough':case'topDurationChange':case'topEmptied':case'topEncrypted':case'topEnded':case'topError':case'topInput':case'topInvalid':case'topLoad':case'topLoadedData':case'topLoadedMetadata':case'topLoadStart':case'topPause':case'topPlay':case'topPlaying':case'topProgress':case'topRateChange':case'topReset':case'topSeeked':case'topSeeking':case'topStalled':case'topSubmit':case'topSuspend':case'topTimeUpdate':case'topVolumeChange':case'topWaiting':i=u;break;case'topKeyPress':if(0===E(n))return null;case'topKeyDown':case'topKeyUp':i=m;break;case'topBlur':case'topFocus':i=c;break;case'topClick':if(2===n.button)return null;case'topDoubleClick':case'topMouseDown':case'topMouseMove':case'topMouseUp':case'topMouseOut':case'topMouseOver':case'topContextMenu':i=h;break;case'topDrag':case'topDragEnd':case'topDragEnter':case'topDragExit':case'topDragLeave':case'topDragOver':case'topDragStart':case'topDrop':i=g;break;case'topTouchCancel':case'topTouchEnd':case'topTouchMove':case'topTouchStart':i=f;break;case'topAnimationEnd':case'topAnimationIteration':case'topAnimationStart':i=p;break;case'topTransitionEnd':i=y;break;case'topScroll':i=_;break;case'topWheel':i=C;break;case'topCopy':case'topCut':case'topPaste':i=l;}i?void 0:r('86',e);var s=i.getPooled(a,t,n,o);return d.accumulateTwoPhaseDispatches(s),s},didPutListener:function(e,t){if('onClick'===t&&!a(e._tag)){var n=o(e),r=s.getNodeFromInstance(e);T[n]||(T[n]=i.listen(r,'click',b))}},willDeleteListener:function(e,t){if('onClick'===t&&!a(e._tag)){var n=o(e);T[n].remove(),delete T[n]}}}},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(12);a.augmentClass(o,{animationName:null,elapsedTime:null,pseudoElement:null}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(12);a.augmentClass(o,{clipboardData:function(e){return'clipboardData'in e?e.clipboardData:window.clipboardData}}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(26);a.augmentClass(o,{relatedTarget:null}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(26),r=n(64),i=n(168),d=n(53);a.augmentClass(o,{key:i,location:null,ctrlKey:null,shiftKey:null,altKey:null,metaKey:null,repeat:null,locale:null,getModifierState:d,charCode:function(e){return'keypress'===e.type?r(e):0},keyCode:function(e){return'keydown'===e.type||'keyup'===e.type?e.keyCode:0},which:function(e){return'keypress'===e.type?r(e):'keydown'===e.type||'keyup'===e.type?e.keyCode:0}}),e.exports=o},function(e,t,n){'use strict';var o=n(64),a={Esc:'Escape',Spacebar:' ',Left:'ArrowLeft',Up:'ArrowUp',Right:'ArrowRight',Down:'ArrowDown',Del:'Delete',Win:'OS',Menu:'ContextMenu',Apps:'ContextMenu',Scroll:'ScrollLock',MozPrintableKey:'Unidentified'},r={8:'Backspace',9:'Tab',12:'Clear',13:'Enter',16:'Shift',17:'Control',18:'Alt',19:'Pause',20:'CapsLock',27:'Escape',32:' ',33:'PageUp',34:'PageDown',35:'End',36:'Home',37:'ArrowLeft',38:'ArrowUp',39:'ArrowRight',40:'ArrowDown',45:'Insert',46:'Delete',112:'F1',113:'F2',114:'F3',115:'F4',116:'F5',117:'F6',118:'F7',119:'F8',120:'F9',121:'F10',122:'F11',123:'F12',144:'NumLock',145:'ScrollLock',224:'Meta'};e.exports=function(e){if(e.key){var t=a[e.key]||e.key;if('Unidentified'!==t)return t}if('keypress'===e.type){var n=o(e);return 13===n?'Enter':String.fromCharCode(n)}return'keydown'===e.type||'keyup'===e.type?r[e.keyCode]||'Unidentified':''}},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(30);a.augmentClass(o,{dataTransfer:null}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(26),r=n(53);a.augmentClass(o,{touches:null,targetTouches:null,changedTouches:null,altKey:null,metaKey:null,ctrlKey:null,shiftKey:null,getModifierState:r}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(12);a.augmentClass(o,{propertyName:null,elapsedTime:null,pseudoElement:null}),e.exports=o},function(e,t,n){'use strict';function o(e,t,n,o){return a.call(this,e,t,n,o)}var a=n(30);a.augmentClass(o,{deltaX:function(e){return'deltaX'in e?e.deltaX:'wheelDeltaX'in e?-e.wheelDeltaX:0},deltaY:function(e){return'deltaY'in e?e.deltaY:'wheelDeltaY'in e?-e.wheelDeltaY:'wheelDelta'in e?-e.wheelDelta:0},deltaZ:null,deltaMode:null}),e.exports=o},function(e,t,n){'use strict';var o=n(63);e.exports=function(e,t){var n={_topLevelWrapper:e,_idCounter:1,_ownerDocument:t?t.nodeType===9?t:t.ownerDocument:null,_node:t,_tag:t?t.nodeName.toLowerCase():null,_namespaceURI:t?t.namespaceURI:null};return!1,n}},function(e){'use strict';e.exports={useCreateElement:!0,useFiber:!1}},function(e,t,n){'use strict';var o=n(176),a=/\/?>/,r=/^<\!\-\-/,i={CHECKSUM_ATTR_NAME:'data-react-checksum',addChecksumToMarkup:function(e){var t=o(e);return r.test(e)?e:e.replace(a,' '+i.CHECKSUM_ATTR_NAME+'="'+t+'"$&')},canReuseMarkup:function(e,t){var n=t.getAttribute(i.CHECKSUM_ATTR_NAME);n=n&&parseInt(n,10);var a=o(e);return a===n}};e.exports=i},function(e){'use strict';var t=65521;e.exports=function(e){for(var o=1,a=0,r=0,i=e.length,d=-4&i;r<d;){for(var s=Math.min(r+4096,d);r<s;r+=4)a+=(o+=e.charCodeAt(r))+(o+=e.charCodeAt(r+1))+(o+=e.charCodeAt(r+2))+(o+=e.charCodeAt(r+3));o%=t,a%=t}for(;r<i;r++)a+=o+=e.charCodeAt(r);return o%=t,a%=t,o|a<<16}},function(e){'use strict';e.exports='15.6.1'},function(e,t,n){'use strict';var o=n(2),a=n(8),r=n(4),i=n(27),d=n(91),s=n(0),p=n(1);e.exports=function(e){if(null==e)return null;if(1===e.nodeType)return e;var t=i.get(e);return t?(t=d(t),t?r.getNodeFromInstance(t):null):void('function'==typeof e.render?o('44'):o('45',Object.keys(e)))}},function(e,t,n){'use strict';var o=n(90);e.exports=o.renderSubtreeIntoContainer}]); diff --git a/toolkit/components/normandy/vendor/classnames.js b/toolkit/components/normandy/vendor/classnames.js new file mode 100644 index 0000000000..8325147afa --- /dev/null +++ b/toolkit/components/normandy/vendor/classnames.js @@ -0,0 +1 @@ +/* eslint-disable */this.classnames=function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={i:d,l:!1,exports:{}};return a[d].call(e.exports,e,e.exports,b),e.l=!0,e.exports}var c={};return b.m=a,b.c=c,b.d=function(a,c,d){b.o(a,c)||Object.defineProperty(a,c,{configurable:!1,enumerable:!0,get:d})},b.n=function(a){var c=a&&a.__esModule?function(){return a['default']}:function(){return a};return b.d(c,'a',c),c},b.o=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)},b.p='',b(b.s=92)}({92:function(a,b){var c,d;(function(){'use strict';function e(){for(var a,b=[],c=0;c<arguments.length;c++)if(a=arguments[c],a){var d=typeof a;if('string'==d||'number'==d)b.push(a);else if(Array.isArray(a))b.push(e.apply(null,a));else if('object'==d)for(var g in a)f.call(a,g)&&a[g]&&b.push(g)}return b.join(' ')}var f={}.hasOwnProperty;'undefined'!=typeof a&&a.exports?a.exports=e:(c=[],d=function(){return e}.apply(b,c),!(d!==void 0&&(a.exports=d)))})()}}); |