summaryrefslogtreecommitdiffstats
path: root/remote/shared/js-window-actors/NavigationListenerChild.sys.mjs
blob: 728d9b6e8c3f394b7e6a4f3e42e2ef7ef43cbf01 (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
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
/* 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, {
  Log: "chrome://remote/content/shared/Log.sys.mjs",
  truncate: "chrome://remote/content/shared/Format.sys.mjs",
});

ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());

export class NavigationListenerChild extends JSWindowActorChild {
  #listener;
  #webProgress;

  constructor() {
    super();

    this.#listener = {
      onLocationChange: this.#onLocationChange,
      onStateChange: this.#onStateChange,
      QueryInterface: ChromeUtils.generateQI([
        "nsIWebProgressListener",
        "nsISupportsWeakReference",
      ]),
    };
    this.#webProgress = null;
  }

  actorCreated() {
    this.#webProgress = this.manager.browsingContext.docShell
      .QueryInterface(Ci.nsIInterfaceRequestor)
      .getInterface(Ci.nsIWebProgress);

    this.#webProgress.addProgressListener(
      this.#listener,
      Ci.nsIWebProgress.NOTIFY_LOCATION |
        Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT
    );
  }

  didDestroy() {
    try {
      this.#webProgress.removeProgressListener(this.#listener);
    } catch (e) {
      // Ignore potential errors if the window global was already destroyed.
    }
  }

  // Note: we rely on events and messages to trigger the actor creation, but
  // all the logic is in the actorCreated callback. The handleEvent and
  // receiveMessage methods are only there as placeholders to avoid errors.

  /**
   * See note above
   */
  handleEvent() {}

  /**
   * See note above
   */
  receiveMessage() {}

  /**
   * A browsing context might be replaced before reaching the parent process,
   * instead we serialize enough information to retrieve the navigable in the
   * parent process.
   *
   * If the browsing context is top level, then the browserId can be used to
   * find the browser element and the new browsing context.
   * Otherwise (frames) the browsing context should not be replaced and the
   * browsing context id should be enough to find the browsing context.
   *
   * @param {BrowsingContext} browsingContext
   *     The browsing context for which we want to get details.
   * @returns {object}
   *     An object that returns the following properties:
   *       - browserId: browser id for this browsing context
   *       - browsingContextId: browsing context id
   *       - isTopBrowsingContext: flag that indicates if the browsing context is
   *         top level
   *
   */
  #getBrowsingContextDetails(browsingContext) {
    return {
      browserId: browsingContext.browserId,
      browsingContextId: browsingContext.id,
      isTopBrowsingContext: browsingContext.parent === null,
    };
  }

  #getTargetURI(request) {
    try {
      return request.QueryInterface(Ci.nsIChannel).originalURI;
    } catch (e) {}

    return null;
  }

  #onLocationChange = (progress, request, location, stateFlags) => {
    if (stateFlags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT) {
      const context = progress.browsingContext;

      lazy.logger.trace(
        `[${context.id}] NavigationListener onLocationChange,` +
          lazy.truncate` location: ${location.spec}`
      );

      this.sendAsyncMessage("NavigationListenerChild:locationChanged", {
        contextDetails: this.#getBrowsingContextDetails(context),
        url: location.spec,
      });
    }
  };

  #onStateChange = (progress, request, stateFlags, status) => {
    const context = progress.browsingContext;
    const targetURI = this.#getTargetURI(request);

    const isBindingAborted = status == Cr.NS_BINDING_ABORTED;
    const isStart = !!(stateFlags & Ci.nsIWebProgressListener.STATE_START);
    const isStop = !!(stateFlags & Ci.nsIWebProgressListener.STATE_STOP);

    if (lazy.Log.isTraceLevelOrMore) {
      const isNetwork = !!(
        stateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK
      );
      lazy.logger.trace(
        `[${context.id}] NavigationListener onStateChange,` +
          ` stateFlags: ${stateFlags}, status: ${status}, isStart: ${isStart},` +
          ` isStop: ${isStop}, isNetwork: ${isNetwork},` +
          ` isBindingAborted: ${isBindingAborted},` +
          lazy.truncate` targetURI: ${targetURI?.spec}`
      );
    }

    try {
      if (isStart) {
        this.sendAsyncMessage("NavigationListenerChild:navigationStarted", {
          contextDetails: this.#getBrowsingContextDetails(context),
          url: targetURI?.spec,
        });

        return;
      }

      if (isStop && !isBindingAborted) {
        // Skip NS_BINDING_ABORTED state changes as this can happen during a
        // browsing context + process change and we should get the real stop state
        // change from the correct process later.
        this.sendAsyncMessage("NavigationListenerChild:navigationStopped", {
          contextDetails: this.#getBrowsingContextDetails(context),
          url: targetURI?.spec,
        });
      }
    } catch (e) {
      if (e.name === "InvalidStateError") {
        // We'll arrive here if we no longer have our manager, so we can
        // just swallow this error.
        return;
      }
      throw e;
    }
  };
}