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 "navigation-started", "location-changed" and
33 * "navigation-stopped" events, with the following object as payload:
34 * - {string} navigationId - The UUID for the navigation.
35 * - {string} navigableId - The UUID for the navigable.
36 * - {string} url - The target url for the navigation.
38 export class NavigationListener {
43 * Create a new NavigationListener instance.
45 * @param {NavigationManager} navigationManager
46 * The underlying NavigationManager for this listener.
48 constructor(navigationManager) {
49 lazy.EventEmitter.decorate(this);
51 this.#listening = false;
52 this.#navigationManager = navigationManager;
56 return this.#listening;
64 if (this.#listening) {
68 this.#navigationManager.on("navigation-started", this.#forwardEvent);
69 this.#navigationManager.on("navigation-stopped", this.#forwardEvent);
70 this.#navigationManager.on("location-changed", this.#forwardEvent);
72 this.#listening = true;
76 if (!this.#listening) {
80 this.#navigationManager.off("navigation-started", this.#forwardEvent);
81 this.#navigationManager.off("navigation-stopped", this.#forwardEvent);
82 this.#navigationManager.off("location-changed", this.#forwardEvent);
84 this.#listening = false;
87 #forwardEvent = (name, data) => {
88 this.emit(name, data);