forked from mirrors/gecko-dev
Backed out changeset 2e388b7f6f91 (bug 1898565) Backed out changeset 3147811b0446 (bug 1850680) Backed out changeset 9a98ace82049 (bug 1850680) Backed out changeset 8ff3dc16fcf9 (bug 1850680) Backed out changeset 1c4084febc9c (bug 1850680) Backed out changeset aeeda14ca643 (bug 1850680) Backed out changeset fb750d3b365d (bug 1880803) Backed out changeset 4d63b73b4125 (bug 1880803)
85 lines
2 KiB
JavaScript
85 lines
2 KiB
JavaScript
/* 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/. */
|
|
|
|
const lazy = {};
|
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
NetworkObserver:
|
|
"resource://devtools/shared/network-observer/NetworkObserver.sys.mjs",
|
|
});
|
|
|
|
/**
|
|
* The NetworkThrottler uses the dev tools NetworkObserver to provide api to throttle all network activity.
|
|
* This can be used to fix network conditions in browsertime pageload tests.
|
|
*
|
|
*/
|
|
|
|
// A minimal struct for onNetworkEvent handling
|
|
class NetworkEventRecord {
|
|
addRequestPostData() {}
|
|
addResponseStart() {}
|
|
addSecurityInfo() {}
|
|
addEventTimings() {}
|
|
addResponseCache() {}
|
|
addResponseContent() {}
|
|
addServerTimings() {}
|
|
addServiceWorkerTimings() {}
|
|
}
|
|
|
|
class NetworkThrottler {
|
|
#devtoolsNetworkObserver;
|
|
#throttling;
|
|
|
|
constructor() {
|
|
this.#throttling = false;
|
|
}
|
|
|
|
destroy() {
|
|
this.stop();
|
|
}
|
|
|
|
start(throttleData) {
|
|
if (this.#throttling) {
|
|
console.error("NetworkThrottler already started");
|
|
return;
|
|
}
|
|
|
|
this.#devtoolsNetworkObserver = new lazy.NetworkObserver({
|
|
ignoreChannelFunction: this.#ignoreChannelFunction,
|
|
onNetworkEvent: this.#onNetworkEvent,
|
|
});
|
|
|
|
this.#devtoolsNetworkObserver.setThrottleData(throttleData);
|
|
|
|
this.#throttling = true;
|
|
}
|
|
|
|
stop() {
|
|
if (!this.#throttling) {
|
|
return;
|
|
}
|
|
|
|
this.#devtoolsNetworkObserver.destroy();
|
|
this.#devtoolsNetworkObserver = null;
|
|
|
|
this.#throttling = false;
|
|
}
|
|
|
|
#ignoreChannelFunction = channel => {
|
|
// Ignore chrome-privileged or DevTools-initiated requests
|
|
if (
|
|
channel.loadInfo?.loadingDocument === null &&
|
|
(channel.loadInfo.loadingPrincipal ===
|
|
Services.scriptSecurityManager.getSystemPrincipal() ||
|
|
channel.loadInfo.isInDevToolsContext)
|
|
) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
#onNetworkEvent = (networkEvent, channel) => {
|
|
return new NetworkEventRecord(networkEvent, channel, this);
|
|
};
|
|
}
|