summaryrefslogtreecommitdiffstats
path: root/remote/shared/listeners/BrowsingContextListener.sys.mjs
blob: d4e3539ca92967a4bf10ab201567570fd6c9052e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 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, {
  EventEmitter: "resource://gre/modules/EventEmitter.sys.mjs",
});

const OBSERVER_TOPIC_ATTACHED = "browsing-context-attached";
const OBSERVER_TOPIC_DISCARDED = "browsing-context-discarded";

const OBSERVER_TOPIC_SET_EMBEDDER = "browsing-context-did-set-embedder";

/**
 * The BrowsingContextListener can be used to listen for notifications coming
 * from browsing contexts that get attached or discarded.
 *
 * Example:
 * ```
 * const listener = new BrowsingContextListener();
 * listener.on("attached", onAttached);
 * listener.startListening();
 *
 * const onAttached = (eventName, data = {}) => {
 *   const { browsingContext, why } = data;
 *   ...
 * };
 * ```
 *
 * @fires message
 *    The BrowsingContextListener emits "attached" and "discarded" events,
 *    with the following object as payload:
 *      - {BrowsingContext} browsingContext
 *            Browsing context the notification relates to.
 *      - {string} why
 *            Usually "attach" or "discard", but will contain "replace" if the
 *            browsing context gets replaced by a cross-group navigation.
 */
export class BrowsingContextListener {
  #listening;
  #topContextsToAttach;

  /**
   * Create a new BrowsingContextListener instance.
   */
  constructor() {
    lazy.EventEmitter.decorate(this);

    // A map that temporarily holds attached top-level browsing contexts until
    // their embedder element is set, which is required to successfully
    // retrieve a unique id for the content browser by the TabManager.
    this.#topContextsToAttach = new Map();

    this.#listening = false;
  }

  destroy() {
    this.stopListening();
    this.#topContextsToAttach = null;
  }

  observe(subject, topic, data) {
    switch (topic) {
      case OBSERVER_TOPIC_ATTACHED:
        // Delay emitting the event for top-level browsing contexts until
        // the embedder element has been set.
        if (!subject.parent) {
          this.#topContextsToAttach.set(subject, data);
          return;
        }

        this.emit("attached", { browsingContext: subject, why: data });
        break;

      case OBSERVER_TOPIC_DISCARDED:
        // Remove a recently attached top-level browsing context if it's
        // immediately discarded.
        if (this.#topContextsToAttach.has(subject)) {
          this.#topContextsToAttach.delete(subject);
        }

        this.emit("discarded", { browsingContext: subject, why: data });
        break;

      case OBSERVER_TOPIC_SET_EMBEDDER:
        const why = this.#topContextsToAttach.get(subject);
        if (why !== undefined) {
          this.emit("attached", { browsingContext: subject, why });
          this.#topContextsToAttach.delete(subject);
        }
        break;
    }
  }

  startListening() {
    if (this.#listening) {
      return;
    }

    Services.obs.addObserver(this, OBSERVER_TOPIC_ATTACHED);
    Services.obs.addObserver(this, OBSERVER_TOPIC_DISCARDED);
    Services.obs.addObserver(this, OBSERVER_TOPIC_SET_EMBEDDER);

    this.#listening = true;
  }

  stopListening() {
    if (!this.#listening) {
      return;
    }

    Services.obs.removeObserver(this, OBSERVER_TOPIC_ATTACHED);
    Services.obs.removeObserver(this, OBSERVER_TOPIC_DISCARDED);
    Services.obs.removeObserver(this, OBSERVER_TOPIC_SET_EMBEDDER);

    this.#topContextsToAttach.clear();

    this.#listening = false;
  }
}