summaryrefslogtreecommitdiffstats
path: root/devtools/server/connectors/js-window-actor/WindowGlobalLogger.sys.mjs
blob: ae15c030fe08d725e76dc8b7b600e8e152e902bb (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
/* 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/. */

function getWindowGlobalUri(windowGlobal) {
  let windowGlobalUri = "";

  if (windowGlobal.documentURI) {
    // If windowGlobal is a WindowGlobalParent documentURI should be available.
    windowGlobalUri = windowGlobal.documentURI.spec;
  } else if (windowGlobal.browsingContext?.window) {
    // If windowGlobal is a WindowGlobalChild, this code runs in the same
    // process as the document and we can directly access the window.location
    // object.
    windowGlobalUri = windowGlobal.browsingContext.window.location.href;
    if (!windowGlobalUri) {
      windowGlobalUri =
        windowGlobal.browsingContext.window.document.documentURI;
    }
  }

  return windowGlobalUri;
}

export const WindowGlobalLogger = {
  /**
   * This logger can run from the content or parent process, and windowGlobal
   * will either be of type `WindowGlobalParent` or `WindowGlobalChild`.
   *
   * The interface for each type can be found in WindowGlobalActors.webidl
   * (https://searchfox.org/mozilla-central/source/dom/chrome-webidl/WindowGlobalActors.webidl)
   *
   * @param {WindowGlobalParent|WindowGlobalChild} windowGlobal
   *        The window global to log. See WindowGlobalActors.webidl for details
   *        about the types.
   * @param {String} message
   *        A custom message that will be displayed at the beginning of the log.
   */
  logWindowGlobal(windowGlobal, message) {
    const { browsingContext } = windowGlobal;
    const { parent } = browsingContext;
    const windowGlobalUri = getWindowGlobalUri(windowGlobal);
    const isInitialDocument =
      "isInitialDocument" in windowGlobal
        ? windowGlobal.isInitialDocument
        : windowGlobal.browsingContext.window?.document.isInitialDocument;

    const details = [];
    details.push(
      "BrowsingContext.browserId: " + browsingContext.browserId,
      "BrowsingContext.id: " + browsingContext.id,
      "innerWindowId: " + windowGlobal.innerWindowId,
      "opener.id: " + browsingContext.opener?.id,
      "pid: " + windowGlobal.osPid,
      "isClosed: " + windowGlobal.isClosed,
      "isInProcess: " + windowGlobal.isInProcess,
      "isCurrentGlobal: " + windowGlobal.isCurrentGlobal,
      "isProcessRoot: " + windowGlobal.isProcessRoot,
      "currentRemoteType: " + browsingContext.currentRemoteType,
      "hasParent: " + (parent ? parent.id : "no"),
      "uri: " + (windowGlobalUri ? windowGlobalUri : "no uri"),
      "isProcessRoot: " + windowGlobal.isProcessRoot,
      "BrowsingContext.isContent: " + windowGlobal.browsingContext.isContent,
      "isInitialDocument: " + isInitialDocument
    );

    const header = "[WindowGlobalLogger] " + message;

    // Use a padding for multiline display.
    const padding = "    ";
    const formattedDetails = details.map(s => padding + s);
    const detailsString = formattedDetails.join("\n");

    dump(header + "\n" + detailsString + "\n");
  },
};