Address comments

This commit is contained in:
Yury Semikhatsky 2024-07-19 15:38:02 -07:00
parent 578847d525
commit 4bce51ec74
3 changed files with 22 additions and 30 deletions

View file

@ -40,9 +40,9 @@ const errorReasons: { [reason: string]: Protocol.Network.ResourceErrorType } = {
}; };
export class WKInterceptableRequest { export class WKInterceptableRequest {
private readonly _session: WKSession; private _session: WKSession;
readonly request: network.Request;
private _requestId: string; private _requestId: string;
readonly request: network.Request;
_timestamp: number; _timestamp: number;
_wallTime: number; _wallTime: number;
@ -59,7 +59,8 @@ export class WKInterceptableRequest {
resourceType, event.request.method, postDataBuffer, headersObjectToArray(event.request.headers)); resourceType, event.request.method, postDataBuffer, headersObjectToArray(event.request.headers));
} }
changeRequestId(requestId: string) { adoptRequestFromNewProcess(newSession: WKSession, requestId: string) {
this._session = newSession;
this._requestId = requestId; this._requestId = requestId;
} }

View file

@ -250,11 +250,10 @@ export class WKPage implements PageDelegate {
private _onTargetDestroyed(event: Protocol.Target.targetDestroyedPayload) { private _onTargetDestroyed(event: Protocol.Target.targetDestroyedPayload) {
const { targetId, crashed } = event; const { targetId, crashed } = event;
if (this._provisionalPage && this._provisionalPage._session.sessionId === targetId) { if (this._provisionalPage && this._provisionalPage._session.sessionId === targetId) {
this._maybeCancelCoopNavigationRequest(this._provisionalPage);
this._provisionalPage._session.dispose(); this._provisionalPage._session.dispose();
this._provisionalPage.dispose(); this._provisionalPage.dispose();
const provisionalPage = this._provisionalPage;
this._provisionalPage = null; this._provisionalPage = null;
this._maybeCancelNavigationRequest(provisionalPage);
} else if (this._session.sessionId === targetId) { } else if (this._session.sessionId === targetId) {
this._session.dispose(); this._session.dispose();
eventsHelper.removeEventListeners(this._sessionListeners); eventsHelper.removeEventListeners(this._sessionListeners);
@ -1017,31 +1016,28 @@ export class WKPage implements PageDelegate {
return context.createHandle(result.object) as dom.ElementHandle; return context.createHandle(result.object) as dom.ElementHandle;
} }
private _maybeCancelNavigationRequest(provisionalPage: WKProvisionalPage) { private _maybeCancelCoopNavigationRequest(provisionalPage: WKProvisionalPage) {
const navigationRequest = provisionalPage.pendingNavigationRequest(); const navigationRequest = provisionalPage.coopNavigationRequest();
for (const [requestId, request] of this._requestIdToRequest) { for (const [requestId, request] of this._requestIdToRequest) {
if (request.request === navigationRequest) { if (request.request === navigationRequest) {
// Make sure the request completes if the provisional navigation is canceled. // Make sure the request completes if the provisional navigation is canceled.
this._onLoadingFailed(provisionalPage._session, { this._onLoadingFailed(provisionalPage._session, {
requestId: requestId, requestId: requestId,
errorText: 'Provisiolal navigation canceled.', errorText: 'Provisiolal navigation canceled.',
timestamp: 0, timestamp: request._timestamp,
canceled: true canceled: true,
}); });
return; return;
} }
} }
} }
_changeRequestId(navigationRequest: network.Request, newRequestId: string) { _adoptRequestFromNewProcess(navigationRequest: network.Request, newSession: WKSession, newRequestId: string) {
for (const [requestId, request] of this._requestIdToRequest) { for (const [requestId, request] of this._requestIdToRequest) {
if (request.request === navigationRequest) { if (request.request === navigationRequest) {
this._requestIdToRequest.delete(requestId); this._requestIdToRequest.delete(requestId);
request.changeRequestId(newRequestId); request.adoptRequestFromNewProcess(newSession, newRequestId);
this._requestIdToRequest.set(newRequestId, request); this._requestIdToRequest.set(newRequestId, request);
// Simply ignore this event as it has already been dispatched from the original process
// and there will ne no requestIntercepted event from the provisional process as it resumes
// existing network load (that has already received reponse headers).
return; return;
} }
} }
@ -1051,7 +1047,6 @@ export class WKPage implements PageDelegate {
if (event.request.url.startsWith('data:')) if (event.request.url.startsWith('data:'))
return; return;
// navigation too?
// We do not support intercepting redirects. // We do not support intercepting redirects.
if (this._page.needsRequestInterception() && !event.redirectResponse) if (this._page.needsRequestInterception() && !event.redirectResponse)
this._requestIdToRequestWillBeSentEvent.set(event.requestId, event); this._requestIdToRequestWillBeSentEvent.set(event.requestId, event);
@ -1190,12 +1185,6 @@ export class WKPage implements PageDelegate {
if (!request) if (!request)
return; return;
// If a provisional page was created after Cross-Origin-Opener-Policy headers were received, it
// will take over the request (with a new id). We'll only fail it if the provisional navigation
// fails/get canceled.
if (this._provisionalPage?.pendingNavigationRequest() === request.request)
return;
const response = request.request._existingResponse(); const response = request.request._existingResponse();
if (response) { if (response) {
response._serverAddrFinished(); response._serverAddrFinished();

View file

@ -22,11 +22,10 @@ import type { Protocol } from './protocol';
import { assert } from '../../utils'; import { assert } from '../../utils';
import type * as network from '../network'; import type * as network from '../network';
export class WKProvisionalPage { export class WKProvisionalPage {
readonly _session: WKSession; readonly _session: WKSession;
private readonly _wkPage: WKPage; private readonly _wkPage: WKPage;
private _existingNavigationRequest: network.Request | undefined; private _coopNavigationRequest: network.Request | undefined;
private _sessionListeners: RegisteredListener[] = []; private _sessionListeners: RegisteredListener[] = [];
private _mainFrameId: string | null = null; private _mainFrameId: string | null = null;
readonly initializationPromise: Promise<void>; readonly initializationPromise: Promise<void>;
@ -34,13 +33,16 @@ export class WKProvisionalPage {
constructor(session: WKSession, page: WKPage) { constructor(session: WKSession, page: WKPage) {
this._session = session; this._session = session;
this._wkPage = page; this._wkPage = page;
// Cross-Origin-Opener-Policy (COOP) request starts in one process and once response headers
// have been received, continues in another.
//
// Network.requestWillBeSent and requestIntercepted (if intercepting) from the original web process // Network.requestWillBeSent and requestIntercepted (if intercepting) from the original web process
// will always come before a provisional page is created based on the response COOP headers. // will always come before a provisional page is created based on the response COOP headers.
// Thereafter we'll receive targetCreated (provisional) and later on in some order loadingFailed from the // Thereafter we'll receive targetCreated (provisional) and later on in some order loadingFailed from the
// original process and requestWillBeSent from the provisional one. We should ignore loadingFailed // original process and requestWillBeSent from the provisional one. We should ignore loadingFailed
// as the original request continues in the provisional process. But if the provisional load is later // as the original request continues in the provisional process. But if the provisional load is later
// canceled we should dispatch loadingFailed to the client. // canceled we should dispatch loadingFailed to the client.
this._existingNavigationRequest = page._page.mainFrame().pendingDocument()?.request; this._coopNavigationRequest = page._page.mainFrame().pendingDocument()?.request;
const overrideFrameId = (handler: (p: any) => void) => { const overrideFrameId = (handler: (p: any) => void) => {
return (payload: any) => { return (payload: any) => {
@ -63,8 +65,8 @@ export class WKProvisionalPage {
this.initializationPromise = this._wkPage._initializeSession(session, true, ({ frameTree }) => this._handleFrameTree(frameTree)); this.initializationPromise = this._wkPage._initializeSession(session, true, ({ frameTree }) => this._handleFrameTree(frameTree));
} }
pendingNavigationRequest(): network.Request | undefined { coopNavigationRequest(): network.Request | undefined {
return this._existingNavigationRequest; return this._coopNavigationRequest;
} }
dispose() { dispose() {
@ -77,10 +79,10 @@ export class WKProvisionalPage {
} }
private _onRequestWillBeSent(event: Protocol.Network.requestWillBeSentPayload) { private _onRequestWillBeSent(event: Protocol.Network.requestWillBeSentPayload) {
if (this._existingNavigationRequest && this._existingNavigationRequest.url() === event.request.url) { if (this._coopNavigationRequest && this._coopNavigationRequest.url() === event.request.url) {
// If it's a continuation of the main frame navigation request after COOP headers were received, // If it's a continuation of the main frame navigation request after COOP headers were received,
// take over original request, and replace its request id with the new one. // take over original request, and replace its request id with the new one.
this._wkPage._changeRequestId(this._existingNavigationRequest, event.requestId); this._wkPage._adoptRequestFromNewProcess(this._coopNavigationRequest, this._session, event.requestId);
// Simply ignore this event as it has already been dispatched from the original process // Simply ignore this event as it has already been dispatched from the original process
// and there will ne no requestIntercepted event from the provisional process as it resumes // and there will ne no requestIntercepted event from the provisional process as it resumes
// existing network load (that has already received reponse headers). // existing network load (that has already received reponse headers).
@ -90,12 +92,12 @@ export class WKProvisionalPage {
} }
private _onLoadingFinished(event: Protocol.Network.loadingFinishedPayload): void { private _onLoadingFinished(event: Protocol.Network.loadingFinishedPayload): void {
this._existingNavigationRequest = undefined; this._coopNavigationRequest = undefined;
this._wkPage._onLoadingFinished(event); this._wkPage._onLoadingFinished(event);
} }
private _onLoadingFailed(event: Protocol.Network.loadingFailedPayload) { private _onLoadingFailed(event: Protocol.Network.loadingFailedPayload) {
this._existingNavigationRequest = undefined; this._coopNavigationRequest = undefined;
this._wkPage._onLoadingFailed(this._session, event); this._wkPage._onLoadingFailed(this._session, event);
} }