summaryrefslogtreecommitdiffstats
path: root/devtools/shared/security/DevToolsSocketStatus.sys.mjs
blob: 8acfbdd8c42634f26c397d9de1afcd0c73f2e65e (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
/* 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/. */

/**
 * Singleton that should be updated whenever a socket is opened or closed for
 * incoming connections.
 *
 * Notifies observers via "devtools-socket" when the status might have have
 * changed.
 *
 * Currently observed by browser/base/content/browser.js in order to display
 * the "remote control" visual cue also used for Marionette and Remote Agent.
 */
export const DevToolsSocketStatus = {
  _browserToolboxSockets: 0,
  _otherSockets: 0,

  /**
   * Check if there are opened DevTools sockets.
   *
   * @param {Object=} options
   * @param {boolean=} options.excludeBrowserToolboxSockets
   *     Exclude sockets opened by local Browser Toolbox sessions. Defaults to
   *     false.
   *
   * @return {boolean}
   *     Returns true if there are DevTools sockets opened and matching the
   *     provided options if any.
   */
  hasSocketOpened(options = {}) {
    const { excludeBrowserToolboxSockets = false } = options;
    if (excludeBrowserToolboxSockets) {
      return this._otherSockets > 0;
    }
    return this._browserToolboxSockets + this._otherSockets > 0;
  },

  notifySocketOpened(options) {
    const { fromBrowserToolbox } = options;
    if (fromBrowserToolbox) {
      this._browserToolboxSockets++;
    } else {
      this._otherSockets++;
    }

    Services.obs.notifyObservers(this, "devtools-socket");
  },

  notifySocketClosed(options) {
    const { fromBrowserToolbox } = options;
    if (fromBrowserToolbox) {
      this._browserToolboxSockets--;
    } else {
      this._otherSockets--;
    }

    Services.obs.notifyObservers(this, "devtools-socket");
  },
};