Skip to content

Commit 31d901f

Browse files
authored
chore(bidi): improve request interception (#39750)
1 parent f00452c commit 31d901f

4 files changed

Lines changed: 26 additions & 13 deletions

File tree

packages/playwright-core/src/server/bidi/bidiBrowser.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class BidiBrowser extends Browser {
4141
readonly _contexts = new Map<string, BidiBrowserContext>();
4242
readonly _bidiPages = new Map<bidi.BrowsingContext.BrowsingContext, BidiPage>();
4343
private readonly _eventListeners: RegisteredListener[];
44+
private _cacheBehavior: bidi.Network.SetCacheBehaviorParameters['cacheBehavior'] = 'default';
4445

4546
static async connect(parent: SdkObject, transport: ConnectionTransport, options: BrowserOptions): Promise<BidiBrowser> {
4647
const browser = new BidiBrowser(parent, transport, options);
@@ -74,6 +75,8 @@ export class BidiBrowser extends Browser {
7475
],
7576
});
7677

78+
await browser._browserSession.send('network.addIntercept', { phases: [bidi.Network.InterceptPhase.AuthRequired] });
79+
7780
await browser._browserSession.send('network.addDataCollector', {
7881
dataTypes: [bidi.Network.DataType.Response],
7982
maxEncodedDataSize: 20_000_000, // same default as in CDP: https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/inspector/inspector_network_agent.cc;l=134;drc=4128411589187a396829a827f59a655bed876aa7
@@ -132,6 +135,14 @@ export class BidiBrowser extends Browser {
132135
return !this._connection.isClosed();
133136
}
134137

138+
async updateCacheBehavior() {
139+
const cacheBehavior = [...this._contexts.values()].some(context => context.requestInterceptors.length > 0) ? 'bypass' : 'default';
140+
if (this._cacheBehavior !== cacheBehavior) {
141+
await this._browserSession.send('network.setCacheBehavior', { cacheBehavior });
142+
this._cacheBehavior = cacheBehavior;
143+
}
144+
}
145+
135146
private _onBrowsingContextCreated(event: bidi.BrowsingContext.Info) {
136147
if (event.parent) {
137148
const parentFrameId = event.parent;
@@ -415,18 +426,18 @@ export class BidiBrowserContext extends BrowserContext {
415426
}
416427

417428
async doUpdateRequestInterception(): Promise<void> {
429+
let interceptPromise = Promise.resolve<any>(undefined);
418430
if (this.requestInterceptors.length > 0 && !this._interceptId) {
419-
const { intercept } = await this._browser._browserSession.send('network.addIntercept', {
431+
interceptPromise = this._browser._browserSession.send('network.addIntercept', {
420432
phases: [bidi.Network.InterceptPhase.BeforeRequestSent],
421-
urlPatterns: [{ type: 'pattern' }],
422-
});
423-
this._interceptId = intercept;
433+
}).then(({ intercept }) => this._interceptId = intercept);
424434
}
425435
if (this.requestInterceptors.length === 0 && this._interceptId) {
426436
const intercept = this._interceptId;
427437
this._interceptId = undefined;
428-
await this._browser._browserSession.send('network.removeIntercept', { intercept });
438+
interceptPromise = this._browser._browserSession.send('network.removeIntercept', { intercept });
429439
}
440+
await Promise.all([this._browser.updateCacheBehavior(), interceptPromise]);
430441
}
431442

432443
override async doUpdateDefaultViewport() {

packages/playwright-core/src/server/bidi/bidiNetworkManager.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type * as frames from '../frames';
2424
import type { Page } from '../page';
2525
import type * as types from '../types';
2626
import type { BidiSession } from './bidiConnection';
27+
import type { BidiPage } from './bidiPage';
2728

2829
const REQUEST_BODY_HEADERS = new Set(['content-encoding', 'content-language', 'content-location', 'content-type']);
2930

@@ -245,13 +246,13 @@ export class BidiNetworkManager {
245246
this._protocolRequestInterceptionEnabled = enabled;
246247
if (initial && !enabled)
247248
return;
248-
const cachePromise = this._session.send('network.setCacheBehavior', { cacheBehavior: enabled ? 'bypass' : 'default' });
249+
const contexts: [string] = [(this._page.delegate as BidiPage)._session.sessionId];
250+
const cachePromise = this._session.send('network.setCacheBehavior', { cacheBehavior: enabled ? 'bypass' : 'default', contexts });
249251
let interceptPromise = Promise.resolve<any>(undefined);
250252
if (enabled) {
251253
interceptPromise = this._session.send('network.addIntercept', {
252-
phases: [bidi.Network.InterceptPhase.AuthRequired, bidi.Network.InterceptPhase.BeforeRequestSent],
253-
urlPatterns: [{ type: 'pattern' }],
254-
// urlPatterns: [{ type: 'string', pattern: '*' }],
254+
phases: [bidi.Network.InterceptPhase.BeforeRequestSent],
255+
contexts,
255256
}).then(r => {
256257
this._intercepId = r.intercept;
257258
});

tests/bidi/expectations/moz-firefox-nightly-library.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ library/browsercontext-cookies-third-party.spec.ts › save/load third party non
99
library/browsercontext-cookies-third-party.spec.ts › save/load third party 'Partitioned;' cookies [fail]
1010
library/browsercontext-cookies-third-party.spec.ts › should be able to send third party cookies via an iframe [fail]
1111
library/browsercontext-cookies.spec.ts › should support requestStorageAccess [fail]
12-
library/browsercontext-credentials.spec.ts › should fail without credentials [timeout]
13-
library/browsercontext-credentials.spec.ts › should work with setHTTPCredentials [timeout]
1412
library/browsercontext-csp.spec.ts › should bypass after cross-process navigation [fail]
1513
library/browsercontext-csp.spec.ts › should bypass CSP header [fail]
1614
library/browsercontext-csp.spec.ts › should bypass CSP in iframes as well [fail]
@@ -155,7 +153,10 @@ library/popup.spec.ts › should use viewport size from window features [timeout
155153
library/role-utils.spec.ts › axe-core accessible-text [timeout]
156154
library/role-utils.spec.ts › wpt accname #2 [timeout]
157155
library/route-web-socket.spec.ts › should emit close upon frame detach [timeout]
158-
library/screencast.spec.ts › screencast.start emits screencastframe events [fail]
156+
library/screencast.spec.ts › screencast.start delivers frames via onFrame callback [fail]
157+
library/screencast.spec.ts › start returns a disposable that stops screencast [fail]
158+
library/screencast.spec.ts › start reuses existing screencast when video recording is running [fail]
159+
library/screencast.spec.ts › supports multiple onFrame listeners [fail]
159160
library/screenshot.spec.ts › page screenshot › should work with device scale factor and scale:css [fail]
160161
library/screenshot.spec.ts › page screenshot › should work with device scale factor, clip and scale:css [fail]
161162
library/selector-generator.spec.ts › selector generator › should work in dynamic iframes without navigation [fail]
@@ -209,6 +210,7 @@ library/video.spec.ts › screencast › should work with old options [timeout]
209210
library/video.spec.ts › screencast › should work with relative path for recordVideo.dir [timeout]
210211
library/video.spec.ts › screencast › should work with video+trace [timeout]
211212
library/video.spec.ts › screencast › should work with weird screen resolution [timeout]
213+
library/video.spec.ts › screencast › video.start dispose stops recording [fail]
212214
library/video.spec.ts › screencast › video.start should fail when recordVideo is set, but stop should work [fail]
213215
library/video.spec.ts › screencast › video.start/stop twice [timeout]
214216
library/video.spec.ts › should saveAs video [timeout]

tests/bidi/expectations/moz-firefox-nightly-page.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ page/page-network-request.spec.ts › should return multipart/form-data [fail]
122122
page/page-network-request.spec.ts › should return postData [fail]
123123
page/page-network-request.spec.ts › should work with binary post data [fail]
124124
page/page-network-request.spec.ts › should work with binary post data and interception [fail]
125-
page/page-network-response.spec.ts › should bypass disk cache when context interception is enabled [fail]
126125
page/page-network-response.spec.ts › should reject response.finished if context closes [timeout]
127126
page/page-network-response.spec.ts › should report all headers [fail]
128127
page/page-network-response.spec.ts › should report if request was fromServiceWorker [fail]

0 commit comments

Comments
 (0)