summaryrefslogtreecommitdiffstats
path: root/services/sync/tps/extensions/tps/resource/modules/addons.sys.mjs
blob: 596f942a06ebdbfea89bf2795999261c409b4911 (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
89
90
91
92
93
/* 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 { AddonManager } from "resource://gre/modules/AddonManager.sys.mjs";
import { AddonUtils } from "resource://services-sync/addonutils.sys.mjs";
import { Logger } from "resource://tps/logger.sys.mjs";

export const STATE_ENABLED = 1;
export const STATE_DISABLED = 2;

export function Addon(TPS, id) {
  this.TPS = TPS;
  this.id = id;
}

Addon.prototype = {
  addon: null,

  async uninstall() {
    // find our addon locally
    let addon = await AddonManager.getAddonByID(this.id);
    Logger.AssertTrue(
      !!addon,
      "could not find addon " + this.id + " to uninstall"
    );
    await AddonUtils.uninstallAddon(addon);
  },

  async find(state) {
    let addon = await AddonManager.getAddonByID(this.id);

    if (!addon) {
      Logger.logInfo("Could not find add-on with ID: " + this.id);
      return false;
    }

    this.addon = addon;

    Logger.logInfo(
      "add-on found: " + addon.id + ", enabled: " + !addon.userDisabled
    );
    if (state == STATE_ENABLED) {
      Logger.AssertFalse(addon.userDisabled, "add-on is disabled: " + addon.id);
      return true;
    } else if (state == STATE_DISABLED) {
      Logger.AssertTrue(addon.userDisabled, "add-on is enabled: " + addon.id);
      return true;
    } else if (state) {
      throw new Error("Don't know how to handle state: " + state);
    } else {
      // No state, so just checking that it exists.
      return true;
    }
  },

  async install() {
    // For Install, the id parameter initially passed is really the filename
    // for the addon's install .xml; we'll read the actual id from the .xml.

    const result = await AddonUtils.installAddons([
      { id: this.id, requireSecureURI: false },
    ]);

    Logger.AssertEqual(
      1,
      result.installedIDs.length,
      "Exactly 1 add-on was installed."
    );
    Logger.AssertEqual(
      this.id,
      result.installedIDs[0],
      "Add-on was installed successfully: " + this.id
    );
  },

  async setEnabled(flag) {
    Logger.AssertTrue(await this.find(), "Add-on is available.");

    let userDisabled;
    if (flag == STATE_ENABLED) {
      userDisabled = false;
    } else if (flag == STATE_DISABLED) {
      userDisabled = true;
    } else {
      throw new Error("Unknown flag to setEnabled: " + flag);
    }

    AddonUtils.updateUserDisabled(this.addon, userDisabled);

    return true;
  },
};