1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 ChromeUtils.defineESModuleGetters(lazy, {
8 EventEmitter: "resource://gre/modules/EventEmitter.sys.mjs",
12 * The NavigationListener simply wraps a NavigationManager instance and exposes
13 * it with a convenient listener API, more consistent with the rest of the
14 * remote codebase. NavigationManager is a singleton per session so it can't
15 * be instanciated for each and every consumer.
19 * const onNavigationStarted = (eventName, data = {}) => {
20 * const { level, message, stacktrace, timestamp } = data;
24 * const listener = new NavigationListener(this.messageHandler.navigationManager);
25 * listener.on("navigation-started", onNavigationStarted);
26 * listener.startListening();
28 * listener.stopListening();
32 * The NavigationListener emits "fragment-navigated", "navigation-started",
33 * "navigation-stopped", and "same-document-changed" events,
34 * with the following object as payload:
35 * - {string} navigationId - The UUID for the navigation.
36 * - {string} navigableId - The UUID for the navigable.
37 * - {string} url - The target url for the navigation.
39 export class NavigationListener {
44 * Create a new NavigationListener instance.
46 * @param {NavigationManager} navigationManager
47 * The underlying NavigationManager for this listener.
49 constructor(navigationManager) {
50 lazy.EventEmitter.decorate(this);
52 this.#listening = false;
53 this.#navigationManager = navigationManager;
57 return this.#listening;
65 if (this.#listening) {
69 this.#navigationManager.on("fragment-navigated", this.#forwardEvent);
70 this.#navigationManager.on("navigation-started", this.#forwardEvent);
71 this.#navigationManager.on("navigation-stopped", this.#forwardEvent);
72 this.#navigationManager.on("same-document-changed", this.#forwardEvent);
74 this.#listening = true;
78 if (!this.#listening) {
82 this.#navigationManager.off("fragment-navigated", this.#forwardEvent);
83 this.#navigationManager.off("navigation-started", this.#forwardEvent);
84 this.#navigationManager.off("navigation-stopped", this.#forwardEvent);
85 this.#navigationManager.off("same-document-changed", this.#forwardEvent);
87 this.#listening = false;
90 #forwardEvent = (name, data) => {
91 this.emit(name, data);