summaryrefslogtreecommitdiffstats
path: root/toolkit/components/normandy/actions/AddonRollbackAction.sys.mjs
blob: d4b2d4c4239bc54bc589cf6d7b1a6096f84dda01 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
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/. */

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",
                enrollmentId:
                  rollout.enrollmentId ||
                  lazy.TelemetryEvents.NO_ENROLLMENT_ID_MARKER,
              }
            );
            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",
            enrollmentId:
              rollout.enrollmentId ||
              lazy.TelemetryEvents.NO_ENROLLMENT_ID_MARKER,
          }
        );
        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}`
        );
      }
    }
  }
}