diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs')
-rw-r--r-- | browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs b/browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs new file mode 100644 index 0000000000..715ab650a4 --- /dev/null +++ b/browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs @@ -0,0 +1,74 @@ +/* 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, { + ASRouter: "resource:///modules/asrouter/ASRouter.sys.mjs", + ASRouterTargeting: "resource:///modules/asrouter/ASRouterTargeting.sys.mjs", +}); + +export const AWScreenUtils = { + /** + * Filter the given screens in place with a predicate. + * + * @param {object[]} screens - The screens to filter. + * @param {Function} callback - The predicate for filtering the screens. + */ + async removeScreens(screens, callback) { + for (let i = 0; i < screens?.length; i++) { + if (await callback(screens[i], i)) { + screens.splice(i--, 1); + } + } + }, + /** + * Given a JEXL expression, returns the evaluation of the expression or returns + * true if the expression did not evaluate successfully + * + * @param {string} targeting - The JEXL expression that will be evaluated + * @returns {boolean} + */ + async evaluateScreenTargeting(targeting) { + const result = await lazy.ASRouter.evaluateExpression({ + expression: targeting, + context: lazy.ASRouterTargeting.Environment, + }); + if (result?.evaluationStatus?.success) { + return result.evaluationStatus.result; + } + + return true; + }, + /** + * Filter out screens whose targeting do not match. + * + * Given an array of screens, each screen will have it's `targeting` property + * evaluated, and removed if it's targeting evaluates to false + * + * @param {object[]} screens - An array of screens that will be looped + * through to be evaluated for removal + * @returns {object[]} - A new array containing the screens that were not removed + */ + async evaluateTargetingAndRemoveScreens(screens) { + const filteredScreens = [...screens]; + await this.removeScreens(filteredScreens, async screen => { + if (screen.targeting === undefined) { + // Don't remove the screen if we don't have a targeting property + return false; + } + + const result = await this.evaluateScreenTargeting(screen.targeting); + // Flipping the value because a true evaluation means we + // don't want to remove the screen, while false means we do + return !result; + }); + + return filteredScreens; + }, + + async addScreenImpression(screen) { + await lazy.ASRouter.addScreenImpression(screen); + }, +}; |