request-blocking.js (3529B)
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 TOGGLE_BLOCKING_ENABLED, 10 TOGGLE_BLOCKED_URL, 11 UPDATE_BLOCKED_URL, 12 REMOVE_BLOCKED_URL, 13 REMOVE_ALL_BLOCKED_URLS, 14 ENABLE_ALL_BLOCKED_URLS, 15 DISABLE_ALL_BLOCKED_URLS, 16 DISABLE_MATCHING_URLS, 17 SYNCED_BLOCKED_URLS, 18 OPEN_ACTION_BAR, 19 SELECT_ACTION_BAR_TAB, 20 PANELS, 21 } = require("resource://devtools/client/netmonitor/src/constants.js"); 22 23 function toggleRequestBlockingPanel() { 24 return async ({ dispatch, getState }) => { 25 const state = getState(); 26 if ( 27 state.ui.networkActionOpen && 28 state.ui.selectedActionBarTabId === PANELS.BLOCKING 29 ) { 30 dispatch(closeRequestBlocking()); 31 } else { 32 dispatch(await openRequestBlocking()); 33 } 34 }; 35 } 36 37 function toggleBlockingEnabled(enabled) { 38 return { 39 type: TOGGLE_BLOCKING_ENABLED, 40 enabled, 41 }; 42 } 43 44 function removeBlockedUrl(url) { 45 return { 46 type: REMOVE_BLOCKED_URL, 47 url, 48 }; 49 } 50 51 function removeAllBlockedUrls() { 52 return { type: REMOVE_ALL_BLOCKED_URLS }; 53 } 54 55 function enableAllBlockedUrls() { 56 return { type: ENABLE_ALL_BLOCKED_URLS }; 57 } 58 59 function disableAllBlockedUrls() { 60 return { type: DISABLE_ALL_BLOCKED_URLS }; 61 } 62 63 function addBlockedUrl(url) { 64 return { 65 type: ADD_BLOCKED_URL, 66 url, 67 }; 68 } 69 70 function toggleBlockedUrl(url) { 71 return { 72 type: TOGGLE_BLOCKED_URL, 73 url, 74 }; 75 } 76 77 function updateBlockedUrl(oldUrl, newUrl) { 78 return { 79 type: UPDATE_BLOCKED_URL, 80 oldUrl, 81 newUrl, 82 }; 83 } 84 85 async function openRequestBlocking() { 86 return async ({ dispatch, getState, commands }) => { 87 const state = getState(); 88 if (!state.requestBlocking.blockingSynced) { 89 const blockedUrls = state.requestBlocking.blockedUrls; 90 const responses = await commands.networkCommand.getBlockedUrls(); 91 const urls = responses.flat(); 92 if (urls.length !== blockedUrls.length) { 93 urls.forEach(url => dispatch(addBlockedUrl(url))); 94 } 95 dispatch({ type: SYNCED_BLOCKED_URLS, synced: true }); 96 } 97 98 dispatch({ type: OPEN_ACTION_BAR, open: true }); 99 dispatch({ 100 type: SELECT_ACTION_BAR_TAB, 101 id: PANELS.BLOCKING, 102 }); 103 }; 104 } 105 106 function closeRequestBlocking() { 107 return ({ dispatch }) => { 108 dispatch({ type: OPEN_ACTION_BAR, open: false }); 109 dispatch({ 110 type: SELECT_ACTION_BAR_TAB, 111 id: PANELS.BLOCKING, 112 }); 113 }; 114 } 115 116 function openRequestBlockingAndAddUrl(url) { 117 return async ({ dispatch }) => { 118 const showBlockingPanel = Services.prefs.getBoolPref( 119 "devtools.netmonitor.features.requestBlocking" 120 ); 121 122 if (showBlockingPanel) { 123 dispatch(await openRequestBlocking()); 124 } 125 dispatch({ type: ADD_BLOCKED_URL, url }); 126 }; 127 } 128 129 function openRequestBlockingAndDisableUrls(url) { 130 return async ({ dispatch }) => { 131 const showBlockingPanel = Services.prefs.getBoolPref( 132 "devtools.netmonitor.features.requestBlocking" 133 ); 134 135 if (showBlockingPanel) { 136 dispatch(await openRequestBlocking()); 137 } 138 139 dispatch({ type: DISABLE_MATCHING_URLS, url }); 140 }; 141 } 142 143 module.exports = { 144 toggleRequestBlockingPanel, 145 addBlockedUrl, 146 toggleBlockingEnabled, 147 toggleBlockedUrl, 148 removeBlockedUrl, 149 removeAllBlockedUrls, 150 enableAllBlockedUrls, 151 disableAllBlockedUrls, 152 updateBlockedUrl, 153 openRequestBlockingAndAddUrl, 154 openRequestBlockingAndDisableUrls, 155 };