summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/components/Output/message-types/JSTracerTrace.js
blob: 241fa15bd11178722dc50f21b6ada195dfb432d0 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* 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";

// React & Redux
const {
  createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const GripMessageBody = require("resource://devtools/client/webconsole/components/Output/GripMessageBody.js");

const {
  MESSAGE_TYPE,
} = require("resource://devtools/client/webconsole/constants.js");

const Message = createFactory(
  require("resource://devtools/client/webconsole/components/Output/Message.js")
);

JSTracerTrace.displayName = "JSTracerTrace";

JSTracerTrace.propTypes = {
  dispatch: PropTypes.func.isRequired,
  message: PropTypes.object.isRequired,
  serviceContainer: PropTypes.object.isRequired,
  timestampsVisible: PropTypes.bool.isRequired,
  maybeScrollToBottom: PropTypes.func,
};

function JSTracerTrace(props) {
  const {
    dispatch,
    message,
    serviceContainer,
    timestampsVisible,
    repeat,
    maybeScrollToBottom,
    setExpanded,
  } = props;

  const {
    // List of common attributes for all tracer messages
    timeStamp,
    prefix,
    depth,
    source,

    // Attribute specific to DOM event
    eventName,

    // Attributes specific to function calls
    frame,
    implementation,
    displayName,
    parameters,

    // Attributes specific to function call returns
    returnedValue,
    relatedTraceId,
    // See tracer.jsm FRAME_EXIT_REASONS
    why,

    // Attributes specific to DOM Mutations
    mutationType,
    mutationElement,
  } = message;

  let messageBodyConfig;
  if (parameters || why || mutationType) {
    messageBodyConfig = {
      dispatch,
      serviceContainer,
      maybeScrollToBottom,
      setExpanded,
      type: "",
      useQuotes: true,

      // Disable custom formatter for now in traces
      customFormat: false,
    };
  }

  // When we are logging a DOM event, we have the `eventName` defined.
  let messageBody;
  if (eventName) {
    messageBody = [dom.span({ className: "jstracer-dom-event" }, eventName)];
  } else if (typeof relatedTraceId == "number") {
    messageBody = [
      dom.span({ className: "jstracer-io" }, "⟵ "),
      dom.span({ className: "jstracer-display-name" }, displayName),
    ];
  } else if (mutationType) {
    messageBody = [
      dom.span(
        { className: "jstracer-dom-mutation" },
        // Add an extra space at the end to have nice copy-paste messages
        "— DOM Mutation | " + mutationType + " "
      ),
      formatRep(messageBodyConfig, mutationElement),
    ];
  } else if (displayName) {
    messageBody = [
      dom.span({ className: "jstracer-io" }, "⟶ "),
      dom.span({ className: "jstracer-implementation" }, implementation),
      // Add a space in order to improve copy paste rendering
      dom.span({ className: "jstracer-display-name" }, " " + displayName),
    ];
  } else {
    messageBody = [dom.span({ className: "jstracer-io" }, "—")];
  }

  // Arguments will only be passed on-demand
  if (parameters) {
    messageBody.push("(", ...formatReps(messageBodyConfig, parameters), ")");
  }
  // Returned value will also only be passed on-demand
  if (why) {
    messageBody.push(
      // Add a spaces in order to improve copy paste rendering
      dom.span({ className: "jstracer-exit-frame-reason" }, " " + why + " ")
    );
    if (returnedValue !== undefined) {
      messageBody.push(formatRep(messageBodyConfig, returnedValue));
    }
  }

  if (prefix) {
    messageBody.unshift(
      dom.span(
        {
          className: "console-message-prefix",
        },
        `${prefix}`
      )
    );
  }

  const topLevelClasses = ["cm-s-mozilla"];

  return Message({
    collapsible: false,
    source,
    level: MESSAGE_TYPE.JSTRACER,
    topLevelClasses,
    messageBody,
    repeat,
    frame,
    stacktrace: null,
    attachment: null,
    serviceContainer,
    dispatch,
    indent: depth,
    timeStamp,
    timestampsVisible,
    parameters,
    message,
    maybeScrollToBottom,
  });
}

/**
 * Generated the list of GripMessageBody for a list of objects.
 * GripMessageBody is Rep's rendering for a given Object, via its object actor's front.
 */
function formatReps(messageBodyConfig, objects) {
  const elements = [];
  const length = objects.length;
  for (let i = 0; i < length; i++) {
    elements.push(formatRep(messageBodyConfig, objects[i], i));

    // We need to interleave a comma if we are not on the last element
    if (i !== length - 1) {
      elements.push(", ");
    }
  }

  return elements;
}

function formatRep(messageBodyConfig, grip, key) {
  return GripMessageBody({
    ...messageBodyConfig,
    grip,
    key,
  });
}

module.exports = JSTracerTrace;