network-parent.js (4740B)
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 { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 networkParentSpec, 10 } = require("resource://devtools/shared/specs/network-parent.js"); 11 12 const { 13 TYPES: { NETWORK_EVENT }, 14 getResourceWatcher, 15 } = require("resource://devtools/server/actors/resources/index.js"); 16 17 /** 18 * This actor manages all network functionality running 19 * in the parent process. 20 * 21 * @class 22 */ 23 class NetworkParentActor extends Actor { 24 constructor(watcherActor) { 25 super(watcherActor.conn, networkParentSpec); 26 this.watcherActor = watcherActor; 27 } 28 29 // Caches the throttling data so that on clearing the 30 // current network throttling it can be reset to the previous. 31 defaultThrottleData = undefined; 32 33 isEqual(next, current) { 34 // If both objects, check all entries 35 if (current && next && next == current) { 36 return Object.entries(current).every(([k, v]) => { 37 return next[k] === v; 38 }); 39 } 40 return false; 41 } 42 43 get networkEventWatcher() { 44 return getResourceWatcher(this.watcherActor, NETWORK_EVENT); 45 } 46 47 setNetworkThrottling(throttleData) { 48 if (!this.networkEventWatcher) { 49 throw new Error("Not listening for network events"); 50 } 51 52 if (throttleData !== null) { 53 throttleData = { 54 latencyMean: throttleData.latency, 55 latencyMax: throttleData.latency, 56 downloadBPSMean: throttleData.downloadThroughput, 57 downloadBPSMax: throttleData.downloadThroughput, 58 uploadBPSMean: throttleData.uploadThroughput, 59 uploadBPSMax: throttleData.uploadThroughput, 60 }; 61 } 62 63 const currentThrottleData = this.networkEventWatcher.getThrottleData(); 64 if (this.isEqual(throttleData, currentThrottleData)) { 65 return; 66 } 67 68 if (this.defaultThrottleData === undefined) { 69 this.defaultThrottleData = currentThrottleData; 70 } 71 72 this.networkEventWatcher.setThrottleData(throttleData); 73 } 74 75 getNetworkThrottling() { 76 if (!this.networkEventWatcher) { 77 throw new Error("Not listening for network events"); 78 } 79 const throttleData = this.networkEventWatcher.getThrottleData(); 80 if (!throttleData) { 81 return null; 82 } 83 return { 84 downloadThroughput: throttleData.downloadBPSMax, 85 uploadThroughput: throttleData.uploadBPSMax, 86 latency: throttleData.latencyMax, 87 }; 88 } 89 90 clearNetworkThrottling() { 91 if (this.defaultThrottleData !== undefined) { 92 this.setNetworkThrottling(this.defaultThrottleData); 93 } 94 } 95 96 setSaveRequestAndResponseBodies(save) { 97 if (!this.networkEventWatcher) { 98 throw new Error("Not listening for network events"); 99 } 100 this.networkEventWatcher.setSaveRequestAndResponseBodies(save); 101 } 102 103 /** 104 * Sets the urls to block. 105 * 106 * @param Array urls 107 * The response packet - stack trace. 108 */ 109 setBlockedUrls(urls) { 110 if (!this.networkEventWatcher) { 111 throw new Error("Not listening for network events"); 112 } 113 this.networkEventWatcher.setBlockedUrls(urls); 114 return {}; 115 } 116 117 /** 118 * Returns the urls that are block 119 */ 120 getBlockedUrls() { 121 if (!this.networkEventWatcher) { 122 throw new Error("Not listening for network events"); 123 } 124 return this.networkEventWatcher.getBlockedUrls(); 125 } 126 127 /** 128 * Blocks the requests based on the filters 129 * 130 * @param {object} filters 131 */ 132 blockRequest(filters) { 133 if (!this.networkEventWatcher) { 134 throw new Error("Not listening for network events"); 135 } 136 this.networkEventWatcher.blockRequest(filters); 137 } 138 139 /** 140 * Unblocks requests based on the filters 141 * 142 * @param {object} filters 143 */ 144 unblockRequest(filters) { 145 if (!this.networkEventWatcher) { 146 throw new Error("Not listening for network events"); 147 } 148 this.networkEventWatcher.unblockRequest(filters); 149 } 150 151 setPersist(enabled) { 152 // We will always call this method, even if we are still using legacy listener. 153 // Do not throw, we will always persist in that deprecated codepath. 154 if (!this.networkEventWatcher) { 155 return; 156 } 157 this.networkEventWatcher.setPersist(enabled); 158 } 159 160 override(url, path) { 161 if (!this.networkEventWatcher) { 162 throw new Error("Not listening for network events"); 163 } 164 this.networkEventWatcher.override(url, path); 165 return {}; 166 } 167 168 removeOverride(url) { 169 if (!this.networkEventWatcher) { 170 throw new Error("Not listening for network events"); 171 } 172 this.networkEventWatcher.removeOverride(url); 173 } 174 } 175 176 exports.NetworkParentActor = NetworkParentActor;