summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/actions/messages.js
blob: 407cfa012198e0ddce7dedcd108604c2289cd46b (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
/* 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";

const {
  prepareMessage,
  getNaturalOrder,
} = require("resource://devtools/client/webconsole/utils/messages.js");
const {
  IdGenerator,
} = require("resource://devtools/client/webconsole/utils/id-generator.js");
const {
  batchActions,
} = require("resource://devtools/client/shared/redux/middleware/debounce.js");

const {
  CSS_MESSAGE_ADD_MATCHING_ELEMENTS,
  MESSAGE_CLOSE,
  MESSAGE_OPEN,
  MESSAGE_REMOVE,
  MESSAGE_TYPE,
  MESSAGES_ADD,
  MESSAGES_CLEAR,
  MESSAGES_DISABLE,
  NETWORK_MESSAGES_UPDATE,
  NETWORK_UPDATES_REQUEST,
  PRIVATE_MESSAGES_CLEAR,
  TARGET_MESSAGES_REMOVE,
} = require("resource://devtools/client/webconsole/constants.js");

const defaultIdGenerator = new IdGenerator();

function messagesAdd(packets, idGenerator = null, persistLogs = false) {
  if (idGenerator == null) {
    idGenerator = defaultIdGenerator;
  }
  const messages = packets.map(packet =>
    prepareMessage(packet, idGenerator, persistLogs)
  );
  // Sort the messages by their timestamps.
  messages.sort(getNaturalOrder);

  if (!persistLogs) {
    for (let i = messages.length - 1; i >= 0; i--) {
      if (messages[i].type === MESSAGE_TYPE.CLEAR) {
        return batchActions([
          messagesClear(),
          {
            type: MESSAGES_ADD,
            messages: messages.slice(i),
          },
        ]);
      }
    }
  }

  // When this is used for non-cached messages then handle clear message and
  // split up into batches
  return {
    type: MESSAGES_ADD,
    messages,
  };
}

function messagesClear() {
  return {
    type: MESSAGES_CLEAR,
  };
}

function messagesDisable(ids) {
  return {
    type: MESSAGES_DISABLE,
    ids,
  };
}

function privateMessagesClear() {
  return {
    type: PRIVATE_MESSAGES_CLEAR,
  };
}

function targetMessagesRemove(targetFront) {
  return {
    type: TARGET_MESSAGES_REMOVE,
    targetFront,
  };
}

function messageOpen(id) {
  return {
    type: MESSAGE_OPEN,
    id,
  };
}

function messageClose(id) {
  return {
    type: MESSAGE_CLOSE,
    id,
  };
}

/**
 * Make a query on the server to get a list of DOM elements matching the given
 * CSS selectors and store the information in the state.
 *
 * @param {Message} message
 *        The CSSWarning message
 */
function messageGetMatchingElements(message) {
  return async ({ dispatch, commands }) => {
    try {
      // We need to do the querySelectorAll using the target the message is coming from,
      // as well as with the window the warning message was emitted from.
      const selectedTargetFront = message?.targetFront;

      const response = await commands.scriptCommand.execute(
        `document.querySelectorAll('${message.cssSelectors}')`,
        {
          selectedTargetFront,
          innerWindowID: message.innerWindowID,
          disableBreaks: true,
        }
      );
      dispatch({
        type: CSS_MESSAGE_ADD_MATCHING_ELEMENTS,
        id: message.id,
        elements: response.result,
      });
    } catch (err) {
      console.error(err);
    }
  };
}

function messageRemove(id) {
  return {
    type: MESSAGE_REMOVE,
    id,
  };
}

function networkMessageUpdates(packets, idGenerator = null) {
  if (idGenerator == null) {
    idGenerator = defaultIdGenerator;
  }

  const messages = packets.map(packet => prepareMessage(packet, idGenerator));

  return {
    type: NETWORK_MESSAGES_UPDATE,
    messages,
  };
}

function networkUpdateRequests(updates) {
  return {
    type: NETWORK_UPDATES_REQUEST,
    updates,
  };
}

module.exports = {
  messagesAdd,
  messagesClear,
  messagesDisable,
  messageOpen,
  messageClose,
  messageRemove,
  messageGetMatchingElements,
  networkMessageUpdates,
  networkUpdateRequests,
  privateMessagesClear,
  targetMessagesRemove,
};