File: NavigableManager.sys.mjs

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (265 lines) | stat: -rw-r--r-- 8,450 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* 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, {
  BrowsingContextListener:
    "chrome://remote/content/shared/listeners/BrowsingContextListener.sys.mjs",
  generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
  TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
});

/**
 * The navigable manager is intended to be used as a singleton and is
 * responsible for tracking open browsing contexts by assigning each a
 * unique identifier. This allows them to be referenced unambiguously.
 * For top-level browsing contexts, the content browser instance itself
 * is used as the anchor, since cross-origin navigations can result in
 * browsing context replacements. Using the browser as a stable reference
 * ensures that protocols like WebDriver BiDi and Marionette can reliably
 * point to the intended "navigable" — a concept from the HTML specification
 * that is not implemented in Firefox.
 */
class NavigableManagerClass {
  #tracking;
  #browserIds;
  #contextListener;
  #navigableIds;

  constructor() {
    this.#tracking = false;

    // Maps browser's `permanentKey` to an uuid: WeakMap.<Object, string>
    //
    // It's required as a fallback, since in the case when a context was
    // discarded embedderElement is gone, and we cannot retrieve the
    // context id from the formerly known browser.
    this.#browserIds = new WeakMap();

    // Maps browsing contexts to uuid: WeakMap.<BrowsingContext, string>.
    this.#navigableIds = new WeakMap();

    // Start tracking by default when the class gets instantiated.
    this.startTracking();
  }

  /**
   * Retrieve the browser element corresponding to the provided unique id,
   * previously generated via getIdForBrowser.
   *
   * TODO: To avoid creating strong references on browser elements and
   * potentially leaking those elements, this method loops over all windows and
   * all tabs. It should be replaced by a faster implementation in Bug 1750065.
   *
   * @param {string} id
   *     A browser unique id created by getIdForBrowser.
   *
   * @returns {XULBrowser}
   *     The <xul:browser> corresponding to the provided id. Will return
   *     `null` if no matching browser element is found.
   */
  getBrowserById(id) {
    for (const tab of lazy.TabManager.allTabs) {
      const contentBrowser = lazy.TabManager.getBrowserForTab(tab);
      if (this.getIdForBrowser(contentBrowser) == id) {
        return contentBrowser;
      }
    }

    return null;
  }

  /**
   * Retrieve the browsing context corresponding to the provided navigabl id.
   *
   * @param {string} id
   *     A browsing context unique id (created by getIdForBrowsingContext).
   *
   * @returns {BrowsingContext=}
   *     The browsing context found for this id, null if none was found or
   *     browsing context is discarded.
   */
  getBrowsingContextById(id) {
    let browsingContext;

    const browser = this.getBrowserById(id);
    if (browser) {
      // top-level browsing context
      browsingContext = browser.browsingContext;
    } else {
      browsingContext = BrowsingContext.get(id);
    }

    if (!browsingContext || browsingContext.isDiscarded) {
      return null;
    }

    return browsingContext;
  }

  /**
   * Retrieve the unique id for the given xul browser element. The id is a
   * dynamically generated uuid associated with the permanentKey property of the
   * given browser element. This method is preferable over getIdForBrowsingContext
   * in case of working with browser element of a tab, since we can not guarantee
   * that browsing context is attached to it.
   *
   * @param {XULBrowser} browser
   *     The <xul:browser> for which we want to retrieve the id.
   *
   * @returns {string|null}
   *     The unique id for this browser or `null` if invalid.
   */
  getIdForBrowser(browser) {
    if (!(XULElement.isInstance(browser) && browser.permanentKey)) {
      // Ignore those browsers that do not have a permanentKey
      // attached like the print preview (bug 1990485), but which
      // we need to uniquely identify a top-level browsing context.
      return null;
    }

    const key = browser.permanentKey;
    if (!this.#browserIds.has(key)) {
      this.#browserIds.set(key, lazy.generateUUID());
    }
    return this.#browserIds.get(key);
  }

  /**
   * Retrieve the id of a Browsing Context.
   *
   * For a top-level browsing context a custom unique id will be returned.
   *
   * @param {BrowsingContext=} browsingContext
   *     The browsing context to get the id from.
   *
   * @returns {string|null}
   *     The unique id of the browsing context or `null` if invalid.
   */
  getIdForBrowsingContext(browsingContext) {
    if (!BrowsingContext.isInstance(browsingContext)) {
      return null;
    }

    if (!browsingContext.parent) {
      // For top-level browsing contexts always try to use the browser
      // as navigable first because it survives a cross-process navigation.
      const browser = this.#getBrowserForBrowsingContext(browsingContext);
      if (browser) {
        return this.getIdForBrowser(browser);
      }

      // If no browser can be found fallback to use the navigable id instead.
      return this.#navigableIds.has(browsingContext)
        ? this.#navigableIds.get(browsingContext)
        : null;
    }

    // Child browsing context (frame)
    return browsingContext.id.toString();
  }

  /**
   * Get the navigable for the given browsing context.
   *
   * Because Gecko doesn't support the Navigable concept in content
   * scope the content browser could be used to uniquely identify
   * top-level browsing contexts.
   *
   * @param {BrowsingContext} browsingContext
   *
   * @returns {BrowsingContext|XULBrowser} The navigable
   *
   * @throws {TypeError}
   *     If `browsingContext` is not a CanonicalBrowsingContext instance.
   */
  getNavigableForBrowsingContext(browsingContext) {
    if (!lazy.TabManager.isValidCanonicalBrowsingContext(browsingContext)) {
      throw new TypeError(
        `Expected browsingContext to be a CanonicalBrowsingContext, got ${browsingContext}`
      );
    }

    if (browsingContext.isContent && browsingContext.parent === null) {
      return this.#getBrowserForBrowsingContext(browsingContext);
    }

    return browsingContext;
  }

  startTracking() {
    if (this.#tracking) {
      return;
    }

    lazy.TabManager.getBrowsers().forEach(browser =>
      this.#setIdForBrowsingContext(browser.browsingContext)
    );

    this.#contextListener = new lazy.BrowsingContextListener();
    this.#contextListener.on("attached", this.#onContextAttached);
    this.#contextListener.startListening();

    this.#tracking = true;
  }

  stopTracking() {
    if (!this.#tracking) {
      return;
    }

    this.#contextListener.stopListening();
    this.#contextListener = null;

    this.#browserIds = new WeakMap();
    this.#navigableIds = new WeakMap();

    this.#tracking = false;
  }

  /** Private methods */

  /**
   * Try to find the browser element to browsing context is attached to.
   *
   * @param {BrowsingContext} browsingContext
   *     The browsing context to find the related browser for.
   *
   * @returns {XULBrowser|null}
   *     The <xul:browser> element, or `null` if no browser exists.
   */
  #getBrowserForBrowsingContext(browsingContext) {
    return browsingContext.top.embedderElement
      ? browsingContext.top.embedderElement
      : null;
  }

  /**
   * Update the internal maps for a new browsing context.
   *
   * @param {BrowsingContext} browsingContext
   *     The browsing context that needs to be observed.
   */
  #setIdForBrowsingContext(browsingContext) {
    const id = this.getIdForBrowsingContext(browsingContext);

    // Add a fallback to the navigable weak map so that an id can
    // also be retrieved when the related browser was closed.
    this.#navigableIds.set(browsingContext, id);
  }

  /** Event handlers */

  #onContextAttached = (_, data = {}) => {
    const { browsingContext } = data;

    if (lazy.TabManager.isValidCanonicalBrowsingContext(browsingContext)) {
      this.#setIdForBrowsingContext(browsingContext);
    }
  };
}

// Expose a shared singleton.
export const NavigableManager = new NavigableManagerClass();