summaryrefslogtreecommitdiffstats
path: root/browser/extensions/report-site-issue/experimentalAPIs/actors/tabExtrasActor.jsm
blob: 52bc6c56c69407e00df9aa95bf9ba4ca719ddf0b (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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/. */

"use strict";

var EXPORTED_SYMBOLS = ["ReportSiteIssueHelperChild"];

const PREVIEW_MAX_ITEMS = 10;
const LOG_LEVELS = ["debug", "info", "warn", "error"];

function getPreview(value) {
  switch (typeof value) {
    case "symbol":
      return value.toString();

    case "function":
      return "function ()";

    case "object":
      if (value === null) {
        return null;
      }

      if (Array.isArray(value)) {
        return `(${value.length})[...]`;
      }

      return "{...}";

    case "undefined":
      return "undefined";

    default:
      try {
        structuredClone(value);
      } catch (_) {
        return `${value}` || "?";
      }

      return value;
  }
}

function getArrayPreview(arr) {
  const preview = [];
  let count = 0;
  for (const value of arr) {
    if (++count > PREVIEW_MAX_ITEMS) {
      break;
    }
    preview.push(getPreview(value));
  }

  return preview;
}

function getObjectPreview(obj) {
  const preview = {};
  let count = 0;
  for (const key of Object.keys(obj)) {
    if (++count > PREVIEW_MAX_ITEMS) {
      break;
    }
    preview[key] = getPreview(obj[key]);
  }

  return preview;
}

function getArgs(value) {
  if (typeof value === "object" && value !== null) {
    if (Array.isArray(value)) {
      return getArrayPreview(value);
    }

    return getObjectPreview(value);
  }

  return getPreview(value);
}

class ReportSiteIssueHelperChild extends JSWindowActorChild {
  _getConsoleMessages(windowId) {
    const ConsoleAPIStorage = Cc[
      "@mozilla.org/consoleAPI-storage;1"
    ].getService(Ci.nsIConsoleAPIStorage);
    let messages = ConsoleAPIStorage.getEvents(windowId);
    return messages.map(evt => {
      const { columnNumber, filename, level, lineNumber, timeStamp } = evt;
      const args = evt.arguments.map(getArgs);

      const message = {
        level,
        log: args,
        uri: filename,
        pos: `${lineNumber}:${columnNumber}`,
      };

      return { timeStamp, message };
    });
  }

  _getScriptErrors(windowId, includePrivate) {
    const messages = Services.console.getMessageArray();
    return messages
      .filter(message => {
        if (message instanceof Ci.nsIScriptError) {
          if (!includePrivate && message.isFromPrivateWindow) {
            return false;
          }

          if (windowId && windowId !== message.innerWindowID) {
            return false;
          }

          return true;
        }

        // If this is not an nsIScriptError and we need to do window-based
        // filtering we skip this message.
        return false;
      })
      .map(error => {
        const {
          timeStamp,
          errorMessage,
          sourceName,
          lineNumber,
          columnNumber,
          logLevel,
        } = error;
        const message = {
          level: LOG_LEVELS[logLevel],
          log: [errorMessage],
          uri: sourceName,
          pos: `${lineNumber}:${columnNumber}`,
        };
        return { timeStamp, message };
      });
  }

  _getLoggedMessages(includePrivate = false) {
    const windowId = this.contentWindow.windowGlobalChild.innerWindowId;
    return this._getConsoleMessages(windowId).concat(
      this._getScriptErrors(windowId, includePrivate)
    );
  }

  receiveMessage(msg) {
    switch (msg.name) {
      case "GetLog":
        return this._getLoggedMessages();
      case "GetBlockingStatus":
        const { docShell } = this;
        return {
          hasTrackingContentBlocked: docShell.hasTrackingContentBlocked,
        };
    }
    return null;
  }
}