From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../normandy/lib/ShieldPreferences.sys.mjs | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 toolkit/components/normandy/lib/ShieldPreferences.sys.mjs (limited to 'toolkit/components/normandy/lib/ShieldPreferences.sys.mjs') 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; + } + } + }, +}; -- cgit v1.2.3