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
|
/* 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 {
ADD_BLOCKED_URL,
REMOVE_BLOCKED_URL,
TOGGLE_BLOCKED_URL,
UPDATE_BLOCKED_URL,
TOGGLE_BLOCKING_ENABLED,
DISABLE_MATCHING_URLS,
ENABLE_ALL_BLOCKED_URLS,
DISABLE_ALL_BLOCKED_URLS,
REMOVE_ALL_BLOCKED_URLS,
REQUEST_BLOCKING_UPDATE_COMPLETE,
} = require("resource://devtools/client/netmonitor/src/constants.js");
const BLOCKING_EVENTS = [
ADD_BLOCKED_URL,
REMOVE_BLOCKED_URL,
TOGGLE_BLOCKED_URL,
UPDATE_BLOCKED_URL,
TOGGLE_BLOCKING_ENABLED,
DISABLE_MATCHING_URLS,
ENABLE_ALL_BLOCKED_URLS,
DISABLE_ALL_BLOCKED_URLS,
REMOVE_ALL_BLOCKED_URLS,
];
/**
* This middleware is responsible for syncing the list of blocking patterns/urls with the backed.
* It utilizes the NetworkCommand and `setBlockedUrls` function to sent the current list to the server
* every time it's been modified.
*/
function requestBlockingMiddleware(commands) {
return store => next => async action => {
const res = next(action);
if (BLOCKING_EVENTS.includes(action.type)) {
const { blockedUrls, blockingEnabled } = store.getState().requestBlocking;
const urls = blockingEnabled
? blockedUrls.reduce((arr, { enabled, url }) => {
if (enabled) {
arr.push(url);
}
return arr;
}, [])
: [];
await commands.networkCommand.setBlockedUrls(urls);
store.dispatch({ type: REQUEST_BLOCKING_UPDATE_COMPLETE });
}
return res;
};
}
module.exports = requestBlockingMiddleware;
|