Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser(firefox): use browsingContextID for frame IDs #3999

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions browser_patches/firefox/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1173
Changed: yurys@chromium.org Thu Sep 10 12:35:25 PDT 2020
1174
Changed: lushnikov@chromium.org Tue Sep 29 02:02:37 MDT 2020
7 changes: 7 additions & 0 deletions browser_patches/firefox/juggler/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const uuidGen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerat
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

class Helper {

addObserver(handler, topic) {
Services.obs.addObserver(handler, topic);
return () => Services.obs.removeObserver(handler, topic);
Expand Down Expand Up @@ -112,6 +113,12 @@ class Helper {
}
return '<unknown error>';
}

browsingContextToFrameId(browsingContext) {
if (!browsingContext)
return undefined;
return 'frame-' + browsingContext.id;
}
}

var EXPORTED_SYMBOLS = [ "Helper" ];
Expand Down
18 changes: 16 additions & 2 deletions browser_patches/firefox/juggler/NetworkObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,24 @@ class NetworkRequest {
const pageNetwork = this._activePageNetwork();
if (!pageNetwork)
return;
const causeType = this.httpChannel.loadInfo ? this.httpChannel.loadInfo.externalContentPolicyType : Ci.nsIContentPolicy.TYPE_OTHER;
const internalCauseType = this.httpChannel.loadInfo ? this.httpChannel.loadInfo.internalContentPolicyType : Ci.nsIContentPolicy.TYPE_OTHER;
const loadInfo = this.httpChannel.loadInfo;
const causeType = loadInfo?.externalContentPolicyType || Ci.nsIContentPolicy.TYPE_OTHER;
const internalCauseType = loadInfo?.internalContentPolicyType || Ci.nsIContentPolicy.TYPE_OTHER;

let browsingContext = loadInfo?.frameBrowsingContext || loadInfo?.browsingContext;
// TODO: Unfortunately, requests from web workers don't have frameBrowsingContext or
// browsingContext.
//
// We fail to attribute them to the original frames on the browser side, but we
// can use load context top frame to attribute them to the top frame at least.
if (!browsingContext) {
const loadContext = helper.getLoadContext(this.httpChannel);
browsingContext = loadContext?.topFrameElement?.browsingContext;
}

pageNetwork.emit(PageNetwork.Events.Request, {
url: this.httpChannel.URI.spec,
frameId: helper.browsingContextToFrameId(browsingContext),
isIntercepted,
requestId: this.requestId,
redirectedFrom: this.redirectedFromId,
Expand Down
2 changes: 1 addition & 1 deletion browser_patches/firefox/juggler/content/FrameTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class Frame {
this._runtime = runtime;
this._docShell = docShell;
this._children = new Set();
this._frameId = helper.generateId();
this._frameId = helper.browsingContextToFrameId(this._docShell.browsingContext);
this._parentFrame = null;
this._url = '';
if (docShell.domWindow && docShell.domWindow.location)
Expand Down
61 changes: 0 additions & 61 deletions browser_patches/firefox/juggler/content/NetworkMonitor.js

This file was deleted.

8 changes: 1 addition & 7 deletions browser_patches/firefox/juggler/content/PageAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ class FrameData {
}

class PageAgent {
constructor(messageManager, browserChannel, sessionId, frameTree, networkMonitor) {
constructor(messageManager, browserChannel, sessionId, frameTree) {
this._messageManager = messageManager;
this._browserChannel = browserChannel;
this._sessionId = sessionId;
this._browserPage = browserChannel.connect(sessionId + 'page');
this._browserRuntime = browserChannel.connect(sessionId + 'runtime');
this._frameTree = frameTree;
this._runtime = frameTree.runtime();
this._networkMonitor = networkMonitor;

this._frameData = new Map();
this._workerData = new Map();
Expand All @@ -146,7 +145,6 @@ class PageAgent {
navigate: this._navigate.bind(this),
reload: this._reload.bind(this),
removeScriptToEvaluateOnNewDocument: this._removeScriptToEvaluateOnNewDocument.bind(this),
requestDetails: this._requestDetails.bind(this),
screenshot: this._screenshot.bind(this),
scrollIntoViewIfNeeded: this._scrollIntoViewIfNeeded.bind(this),
setCacheDisabled: this._setCacheDisabled.bind(this),
Expand All @@ -170,10 +168,6 @@ class PageAgent {
this._dataTransfer = null;
}

_requestDetails({channelKey}) {
return this._networkMonitor.requestDetails(channelKey);
}

async _setEmulatedMedia({type, colorScheme}) {
const docShell = this._frameTree.mainFrame().docShell();
const cv = docShell.contentViewer;
Expand Down
6 changes: 1 addition & 5 deletions browser_patches/firefox/juggler/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {Helper} = ChromeUtils.import('chrome://juggler/content/Helper.js');
const {FrameTree} = ChromeUtils.import('chrome://juggler/content/content/FrameTree.js');
const {NetworkMonitor} = ChromeUtils.import('chrome://juggler/content/content/NetworkMonitor.js');
const {SimpleChannel} = ChromeUtils.import('chrome://juggler/content/SimpleChannel.js');
const {PageAgent} = ChromeUtils.import('chrome://juggler/content/content/PageAgent.js');

let frameTree;
let networkMonitor;
const helper = new Helper();
const messageManager = this;

const sessions = new Map();

function createContentSession(channel, sessionId) {
const pageAgent = new PageAgent(messageManager, channel, sessionId, frameTree, networkMonitor);
const pageAgent = new PageAgent(messageManager, channel, sessionId, frameTree);
sessions.set(sessionId, [pageAgent]);
pageAgent.enable();
}
Expand Down Expand Up @@ -118,7 +116,6 @@ function initialize() {
frameTree.addScriptToEvaluateOnNewDocument(script);
for (const { name, script } of bindings)
frameTree.addBinding(name, script);
networkMonitor = new NetworkMonitor(docShell, frameTree);

const channel = SimpleChannel.createForMessageManager('content::page', messageManager);

Expand Down Expand Up @@ -180,7 +177,6 @@ function initialize() {
for (const sessionId of sessions.keys())
disposeContentSession(sessionId);

networkMonitor.dispose();
frameTree.dispose();
}),
];
Expand Down
1 change: 0 additions & 1 deletion browser_patches/firefox/juggler/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ juggler.jar:
content/protocol/AccessibilityHandler.js (protocol/AccessibilityHandler.js)
content/content/main.js (content/main.js)
content/content/FrameTree.js (content/FrameTree.js)
content/content/NetworkMonitor.js (content/NetworkMonitor.js)
content/content/PageAgent.js (content/PageAgent.js)
content/content/Runtime.js (content/Runtime.js)
content/content/WorkerMain.js (content/WorkerMain.js)
Expand Down
81 changes: 4 additions & 77 deletions browser_patches/firefox/juggler/protocol/NetworkHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ const helper = new Helper();
class NetworkHandler {
constructor(target, session, contentChannel) {
this._session = session;
this._contentPage = contentChannel.connect(session.sessionId() + 'page');
this._httpActivity = new Map();
this._enabled = false;
this._pageNetwork = NetworkObserver.instance().pageNetworkForTarget(target);
this._requestInterception = false;
this._eventListeners = [];
this._pendingRequstWillBeSentEvents = new Set();
}

async enable() {
Expand Down Expand Up @@ -51,9 +47,6 @@ class NetworkHandler {
this._pageNetwork.enableRequestInterception();
else
this._pageNetwork.disableRequestInterception();
// Right after we enable/disable request interception we need to await all pending
// requestWillBeSent events before successfully returning from the method.
await Promise.all(Array.from(this._pendingRequstWillBeSentEvents));
}

async resumeInterceptedRequest({requestId, method, headers, postData}) {
Expand All @@ -69,89 +62,23 @@ class NetworkHandler {
}

dispose() {
this._contentPage.dispose();
helper.removeListeners(this._eventListeners);
}

_ensureHTTPActivity(requestId) {
let activity = this._httpActivity.get(requestId);
if (!activity) {
activity = {
_id: requestId,
_lastSentEvent: null,
request: null,
response: null,
complete: null,
failed: null,
};
this._httpActivity.set(requestId, activity);
}
return activity;
}

_reportHTTPAcitivityEvents(activity) {
// State machine - sending network events.
if (!activity._lastSentEvent && activity.request) {
this._session.emitEvent('Network.requestWillBeSent', activity.request);
activity._lastSentEvent = 'requestWillBeSent';
}
if (activity._lastSentEvent === 'requestWillBeSent' && activity.response) {
this._session.emitEvent('Network.responseReceived', activity.response);
activity._lastSentEvent = 'responseReceived';
}
if (activity._lastSentEvent === 'responseReceived' && activity.complete) {
this._session.emitEvent('Network.requestFinished', activity.complete);
activity._lastSentEvent = 'requestFinished';
}
if (activity._lastSentEvent && activity.failed) {
this._session.emitEvent('Network.requestFailed', activity.failed);
activity._lastSentEvent = 'requestFailed';
}

// Clean up if request lifecycle is over.
if (activity._lastSentEvent === 'requestFinished' || activity._lastSentEvent === 'requestFailed')
this._httpActivity.delete(activity._id);
}

async _onRequest(eventDetails, channelKey) {
let pendingRequestCallback;
let pendingRequestPromise = new Promise(x => pendingRequestCallback = x);
this._pendingRequstWillBeSentEvents.add(pendingRequestPromise);
let details = null;
try {
details = await this._contentPage.send('requestDetails', {channelKey});
} catch (e) {
pendingRequestCallback();
this._pendingRequstWillBeSentEvents.delete(pendingRequestPromise);
return;
}
const frameId = details ? details.frameId : undefined;
const activity = this._ensureHTTPActivity(eventDetails.requestId);
activity.request = {
frameId,
...eventDetails,
};
this._reportHTTPAcitivityEvents(activity);
pendingRequestCallback();
this._pendingRequstWillBeSentEvents.delete(pendingRequestPromise);
this._session.emitEvent('Network.requestWillBeSent', eventDetails);
}

async _onResponse(eventDetails) {
const activity = this._ensureHTTPActivity(eventDetails.requestId);
activity.response = eventDetails;
this._reportHTTPAcitivityEvents(activity);
this._session.emitEvent('Network.responseReceived', eventDetails);
}

async _onRequestFinished(eventDetails) {
const activity = this._ensureHTTPActivity(eventDetails.requestId);
activity.complete = eventDetails;
this._reportHTTPAcitivityEvents(activity);
this._session.emitEvent('Network.requestFinished', eventDetails);
}

async _onRequestFailed(eventDetails) {
const activity = this._ensureHTTPActivity(eventDetails.requestId);
activity.failed = eventDetails;
this._reportHTTPAcitivityEvents(activity);
this._session.emitEvent('Network.requestFailed', eventDetails);
}
}

Expand Down