Bug 1946184 - Fix computing the CSD margin right after calling HideWindowChrome(...
[gecko.git] / devtools / client / webconsole / actions / messages.js
blobebc371e82df28a457ed26cf15d3a7b335a873d89
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const {
8 prepareMessage,
9 getNaturalOrder,
10 } = require("resource://devtools/client/webconsole/utils/messages.js");
11 const {
12 IdGenerator,
13 } = require("resource://devtools/client/webconsole/utils/id-generator.js");
14 const {
15 batchActions,
16 } = require("resource://devtools/client/shared/redux/middleware/debounce.js");
18 const {
19 CSS_MESSAGE_ADD_MATCHING_ELEMENTS,
20 MESSAGE_CLOSE,
21 MESSAGE_OPEN,
22 MESSAGE_REMOVE,
23 MESSAGE_TYPE,
24 MESSAGES_ADD,
25 MESSAGES_CLEAR,
26 MESSAGES_DISABLE,
27 NETWORK_MESSAGES_UPDATE,
28 NETWORK_UPDATES_REQUEST,
29 PRIVATE_MESSAGES_CLEAR,
30 TARGET_MESSAGES_REMOVE,
31 } = require("resource://devtools/client/webconsole/constants.js");
33 const defaultIdGenerator = new IdGenerator();
35 function messagesAdd(resources, idGenerator = null, persistLogs = false) {
36 if (idGenerator == null) {
37 idGenerator = defaultIdGenerator;
39 const messages = [];
40 for (const resource of resources) {
41 const message = prepareMessage(resource, idGenerator, persistLogs);
42 if (message) {
43 messages.push(message);
46 // Sort the messages by their timestamps.
47 messages.sort(getNaturalOrder);
49 if (!persistLogs) {
50 for (let i = messages.length - 1; i >= 0; i--) {
51 if (messages[i].type === MESSAGE_TYPE.CLEAR) {
52 return batchActions([
53 messagesClear(),
55 type: MESSAGES_ADD,
56 messages: messages.slice(i),
58 ]);
63 // When this is used for non-cached messages then handle clear message and
64 // split up into batches
65 return {
66 type: MESSAGES_ADD,
67 messages,
71 function messagesClear() {
72 return {
73 type: MESSAGES_CLEAR,
77 function messagesDisable(ids) {
78 return {
79 type: MESSAGES_DISABLE,
80 ids,
84 function privateMessagesClear() {
85 return {
86 type: PRIVATE_MESSAGES_CLEAR,
90 function targetMessagesRemove(targetFront) {
91 return {
92 type: TARGET_MESSAGES_REMOVE,
93 targetFront,
97 function messageOpen(id) {
98 return {
99 type: MESSAGE_OPEN,
104 function messageClose(id) {
105 return {
106 type: MESSAGE_CLOSE,
112 * Make a query on the server to get a list of DOM elements matching the given
113 * CSS selectors and store the information in the state.
115 * @param {Message} message
116 * The CSSWarning message
118 function messageGetMatchingElements(message) {
119 return async ({ dispatch, commands }) => {
120 try {
121 // We need to do the querySelectorAll using the target the message is coming from,
122 // as well as with the window the warning message was emitted from.
123 const selectedTargetFront = message?.targetFront;
125 const response = await commands.scriptCommand.execute(
126 `document.querySelectorAll('${message.cssSelectors}')`,
128 selectedTargetFront,
129 innerWindowID: message.innerWindowID,
130 disableBreaks: true,
133 dispatch({
134 type: CSS_MESSAGE_ADD_MATCHING_ELEMENTS,
135 id: message.id,
136 elements: response.result,
138 } catch (err) {
139 console.error(err);
144 function messageRemove(id) {
145 return {
146 type: MESSAGE_REMOVE,
151 function networkMessageUpdates(packets, idGenerator = null) {
152 if (idGenerator == null) {
153 idGenerator = defaultIdGenerator;
156 const messages = packets.map(packet => prepareMessage(packet, idGenerator));
158 return {
159 type: NETWORK_MESSAGES_UPDATE,
160 messages,
164 function networkUpdateRequests(updates) {
165 return {
166 type: NETWORK_UPDATES_REQUEST,
167 updates,
171 module.exports = {
172 messagesAdd,
173 messagesClear,
174 messagesDisable,
175 messageOpen,
176 messageClose,
177 messageRemove,
178 messageGetMatchingElements,
179 networkMessageUpdates,
180 networkUpdateRequests,
181 privateMessagesClear,
182 targetMessagesRemove,