summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutwelcome/modules/AWScreenUtils.sys.mjs
blob: 715ab650a4657ece8d45c0e8ca8a14fe74a1dccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
  },
};