summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/MailExtensionShortcuts.jsm
blob: f3c4d8eef7e0c4280f538c397caa8b49edfdf6aa (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
/* 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/. */
"use strict";

const EXPORTED_SYMBOLS = ["MailExtensionShortcuts"];

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { ExtensionShortcuts } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionShortcuts.sys.mjs"
);

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  ExtensionParent: "resource://gre/modules/ExtensionParent.sys.mjs",
});

XPCOMUtils.defineLazyGetter(lazy, "browserActionFor", () => {
  return lazy.ExtensionParent.apiManager.global.browserActionFor;
});

XPCOMUtils.defineLazyGetter(lazy, "composeActionFor", () => {
  return lazy.ExtensionParent.apiManager.global.composeActionFor;
});

XPCOMUtils.defineLazyGetter(lazy, "messageDisplayActionFor", () => {
  return lazy.ExtensionParent.apiManager.global.messageDisplayActionFor;
});

const EXECUTE_ACTION = "_execute_action";
const EXECUTE_BROWSER_ACTION = "_execute_browser_action";
const EXECUTE_MSG_DISPLAY_ACTION = "_execute_message_display_action";
const EXECUTE_COMPOSE_ACTION = "_execute_compose_action";

class MailExtensionShortcuts extends ExtensionShortcuts {
  /**
   * Builds a XUL Key element and attaches an onCommand listener which
   * emits a command event with the provided name when fired.
   *
   * @param {Document} doc The XUL document.
   * @param {string} name The name of the command.
   * @param {string} shortcut The shortcut provided in the manifest.
   * @see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/key
   *
   * @returns {Document} The newly created Key element.
   */
  buildKey(doc, name, shortcut) {
    let keyElement = this.buildKeyFromShortcut(doc, name, shortcut);

    // We need to have the attribute "oncommand" for the "command" listener to fire,
    // and it is currently ignored when set to the empty string.
    keyElement.setAttribute("oncommand", "//");

    /* eslint-disable mozilla/balanced-listeners */
    // We remove all references to the key elements when the extension is shutdown,
    // therefore the listeners for these elements will be garbage collected.
    keyElement.addEventListener("command", event => {
      let action;
      if (
        name == EXECUTE_BROWSER_ACTION &&
        this.extension.manifestVersion < 3
      ) {
        action = lazy.browserActionFor(this.extension);
      } else if (name == EXECUTE_ACTION && this.extension.manifestVersion > 2) {
        action = lazy.browserActionFor(this.extension);
      } else if (name == EXECUTE_COMPOSE_ACTION) {
        action = lazy.composeActionFor(this.extension);
      } else if (name == EXECUTE_MSG_DISPLAY_ACTION) {
        action = lazy.messageDisplayActionFor(this.extension);
      } else {
        this.extension.tabManager.addActiveTabPermission();
        this.onCommand(name);
        return;
      }
      if (action) {
        let win = event.target.ownerGlobal;
        action.triggerAction(win);
      }
    });
    /* eslint-enable mozilla/balanced-listeners */

    return keyElement;
  }
}