Andrey Lushnikov 2631e1a809
browser(firefox): use browsingContextID for frame IDs (#3999)
BrowsingContextIDs are consistent across the processes, so we can use
them to target frames in both browser and content processes. This will
aid browser-side navigation.

As a nice side-effect, we can drop a round-trip to the content process
for every `requestWillBeSent` event since we *almost* always can
attribute all network events to the proper parent frames.

I say "almost", because we in fact **fail** to correctly attribute requests
from workers that are instantiated by subframes. This, however, is
not working in Chromium ATM, so I consider this to be a minor regression
that is worth the simplification.
2020-09-29 11:22:00 -07:00

87 lines
2.8 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/. */
"use strict";
const {Helper} = ChromeUtils.import('chrome://juggler/content/Helper.js');
const {NetworkObserver, PageNetwork} = ChromeUtils.import('chrome://juggler/content/NetworkObserver.js');
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const XUL_NS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
const helper = new Helper();
class NetworkHandler {
constructor(target, session, contentChannel) {
this._session = session;
this._enabled = false;
this._pageNetwork = NetworkObserver.instance().pageNetworkForTarget(target);
this._eventListeners = [];
}
async enable() {
if (this._enabled)
return;
this._enabled = true;
this._eventListeners = [
helper.on(this._pageNetwork, PageNetwork.Events.Request, this._onRequest.bind(this)),
helper.on(this._pageNetwork, PageNetwork.Events.Response, this._onResponse.bind(this)),
helper.on(this._pageNetwork, PageNetwork.Events.RequestFinished, this._onRequestFinished.bind(this)),
helper.on(this._pageNetwork, PageNetwork.Events.RequestFailed, this._onRequestFailed.bind(this)),
this._pageNetwork.addSession(),
];
}
async getResponseBody({requestId}) {
return this._pageNetwork.getResponseBody(requestId);
}
async setExtraHTTPHeaders({headers}) {
this._pageNetwork.setExtraHTTPHeaders(headers);
}
async setRequestInterception({enabled}) {
if (enabled)
this._pageNetwork.enableRequestInterception();
else
this._pageNetwork.disableRequestInterception();
}
async resumeInterceptedRequest({requestId, method, headers, postData}) {
this._pageNetwork.resumeInterceptedRequest(requestId, method, headers, postData);
}
async abortInterceptedRequest({requestId, errorCode}) {
this._pageNetwork.abortInterceptedRequest(requestId, errorCode);
}
async fulfillInterceptedRequest({requestId, status, statusText, headers, base64body}) {
this._pageNetwork.fulfillInterceptedRequest(requestId, status, statusText, headers, base64body);
}
dispose() {
helper.removeListeners(this._eventListeners);
}
async _onRequest(eventDetails, channelKey) {
this._session.emitEvent('Network.requestWillBeSent', eventDetails);
}
async _onResponse(eventDetails) {
this._session.emitEvent('Network.responseReceived', eventDetails);
}
async _onRequestFinished(eventDetails) {
this._session.emitEvent('Network.requestFinished', eventDetails);
}
async _onRequestFailed(eventDetails) {
this._session.emitEvent('Network.requestFailed', eventDetails);
}
}
var EXPORTED_SYMBOLS = ['NetworkHandler'];
this.NetworkHandler = NetworkHandler;