request-blocking.js (1716B)
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/. */ 4 5 "use strict"; 6 7 const { 8 ADD_BLOCKED_URL, 9 REMOVE_BLOCKED_URL, 10 TOGGLE_BLOCKED_URL, 11 UPDATE_BLOCKED_URL, 12 TOGGLE_BLOCKING_ENABLED, 13 DISABLE_MATCHING_URLS, 14 ENABLE_ALL_BLOCKED_URLS, 15 DISABLE_ALL_BLOCKED_URLS, 16 REMOVE_ALL_BLOCKED_URLS, 17 REQUEST_BLOCKING_UPDATE_COMPLETE, 18 } = require("resource://devtools/client/netmonitor/src/constants.js"); 19 20 const BLOCKING_EVENTS = [ 21 ADD_BLOCKED_URL, 22 REMOVE_BLOCKED_URL, 23 TOGGLE_BLOCKED_URL, 24 UPDATE_BLOCKED_URL, 25 TOGGLE_BLOCKING_ENABLED, 26 DISABLE_MATCHING_URLS, 27 ENABLE_ALL_BLOCKED_URLS, 28 DISABLE_ALL_BLOCKED_URLS, 29 REMOVE_ALL_BLOCKED_URLS, 30 ]; 31 32 /** 33 * This middleware is responsible for syncing the list of blocking patterns/urls with the backed. 34 * It utilizes the NetworkCommand and `setBlockedUrls` function to sent the current list to the server 35 * every time it's been modified. 36 */ 37 function requestBlockingMiddleware(commands) { 38 return store => next => async action => { 39 const res = next(action); 40 41 if (BLOCKING_EVENTS.includes(action.type)) { 42 const { blockedUrls, blockingEnabled } = store.getState().requestBlocking; 43 const urls = blockingEnabled 44 ? blockedUrls.reduce((arr, { enabled, url }) => { 45 if (enabled) { 46 arr.push(url); 47 } 48 return arr; 49 }, []) 50 : []; 51 await commands.networkCommand.setBlockedUrls(urls); 52 store.dispatch({ type: REQUEST_BLOCKING_UPDATE_COMPLETE }); 53 } 54 return res; 55 }; 56 } 57 58 module.exports = requestBlockingMiddleware;