1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { eventsHelper } from '../utils/eventsHelper';
import { Browser } from '../browser';
import { BrowserContext, verifyGeolocation } from '../browserContext';
import * as network from '../network';
import { BidiConnection } from './bidiConnection';
import { bidiBytesValueToString } from './bidiNetworkManager';
import { BidiPage, kPlaywrightBindingChannel } from './bidiPage';
import { PageBinding } from '../page';
import * as bidi from './third_party/bidiProtocol';
import type { RegisteredListener } from '../utils/eventsHelper';
import type { BrowserOptions } from '../browser';
import type { SdkObject } from '../instrumentation';
import type { InitScript, Page } from '../page';
import type { ConnectionTransport } from '../transport';
import type * as types from '../types';
import type { BidiSession } from './bidiConnection';
import type * as channels from '@protocol/channels';
export class BidiBrowser extends Browser {
private readonly _connection: BidiConnection;
readonly _browserSession: BidiSession;
private _bidiSessionInfo!: bidi.Session.NewResult;
readonly _contexts = new Map<string, BidiBrowserContext>();
readonly _bidiPages = new Map<bidi.BrowsingContext.BrowsingContext, BidiPage>();
private readonly _eventListeners: RegisteredListener[];
static async connect(parent: SdkObject, transport: ConnectionTransport, options: BrowserOptions): Promise<BidiBrowser> {
const browser = new BidiBrowser(parent, transport, options);
if ((options as any).__testHookOnConnectToBrowser)
await (options as any).__testHookOnConnectToBrowser();
browser._bidiSessionInfo = await browser._browserSession.send('session.new', {
capabilities: {
alwaysMatch: {
'acceptInsecureCerts': options.persistent?.internalIgnoreHTTPSErrors || options.persistent?.ignoreHTTPSErrors,
'proxy': getProxyConfiguration(options.originalLaunchOptions.proxyOverride ?? options.proxy),
'unhandledPromptBehavior': {
default: bidi.Session.UserPromptHandlerType.Ignore,
},
'webSocketUrl': true,
// Chrome with WebDriver BiDi does not support prerendering
// yet because WebDriver BiDi behavior is not specified. See
// https://github.com/w3c/webdriver-bidi/issues/321.
'goog:prerenderingDisabled': true,
},
}
});
await browser._browserSession.send('session.subscribe', {
events: [
'browsingContext',
'network',
'log',
'script',
'input',
],
});
await browser._browserSession.send('network.addDataCollector', {
dataTypes: [bidi.Network.DataType.Response],
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
});
if (options.persistent) {
const context = new BidiBrowserContext(browser, undefined, options.persistent);
browser._defaultContext = context;
await context._initialize();
// Create default page as we cannot get access to the existing one.
const page = await browser._defaultContext.doCreateNewPage();
await page.waitForInitializedOrError();
}
return browser;
}
constructor(parent: SdkObject, transport: ConnectionTransport, options: BrowserOptions) {
super(parent, options);
this._connection = new BidiConnection(transport, this._onDisconnect.bind(this), options.protocolLogger, options.browserLogsCollector);
this._browserSession = this._connection.browserSession;
this._eventListeners = [
eventsHelper.addEventListener(this._browserSession, 'browsingContext.contextCreated', this._onBrowsingContextCreated.bind(this)),
eventsHelper.addEventListener(this._browserSession, 'script.realmDestroyed', this._onScriptRealmDestroyed.bind(this)),
];
}
_onDisconnect() {
this._didClose();
}
async doCreateNewContext(options: types.BrowserContextOptions): Promise<BrowserContext> {
const proxy = options.proxyOverride || options.proxy;
const { userContext } = await this._browserSession.send('browser.createUserContext', {
acceptInsecureCerts: options.internalIgnoreHTTPSErrors || options.ignoreHTTPSErrors,
proxy: getProxyConfiguration(proxy),
});
const context = new BidiBrowserContext(this, userContext, options);
await context._initialize();
this._contexts.set(userContext, context);
return context;
}
contexts(): BrowserContext[] {
return Array.from(this._contexts.values());
}
version(): string {
return this._bidiSessionInfo.capabilities.browserVersion;
}
userAgent(): string {
return this._bidiSessionInfo.capabilities.userAgent;
}
isConnected(): boolean {
return !this._connection.isClosed();
}
private _onBrowsingContextCreated(event: bidi.BrowsingContext.Info) {
if (event.parent) {
const parentFrameId = event.parent;
const page = this._findPageForFrame(parentFrameId);
if (page) {
page._session.addFrameBrowsingContext(event.context);
const frame = page._page.frameManager.frameAttached(event.context, parentFrameId);
frame._url = event.url;
page._getFrameNode(frame).then(node => {
const attributes = node?.value?.attributes;
frame._name = attributes?.name ?? attributes?.id ?? '';
});
return;
}
return;
}
let context = this._contexts.get(event.userContext);
if (!context)
context = this._defaultContext as BidiBrowserContext;
if (!context)
return;
context.doGrantGlobalPermissionsForURL(event.url);
const session = this._connection.createMainFrameBrowsingContextSession(event.context);
const opener = event.originalOpener && this._findPageForFrame(event.originalOpener);
const page = new BidiPage(context, session, opener || null);
page._page.mainFrame()._url = event.url;
this._bidiPages.set(event.context, page);
}
_onBrowsingContextDestroyed(event: bidi.BrowsingContext.Info) {
if (event.parent) {
this._browserSession.removeFrameBrowsingContext(event.context);
const parentFrameId = event.parent;
for (const page of this._bidiPages.values()) {
const parentFrame = page._page.frameManager.frame(parentFrameId);
if (!parentFrame)
continue;
page._page.frameManager.frameDetached(event.context);
return;
}
return;
}
const bidiPage = this._bidiPages.get(event.context);
if (!bidiPage)
return;
bidiPage.didClose();
this._bidiPages.delete(event.context);
}
private _onScriptRealmDestroyed(event: bidi.Script.RealmDestroyedParameters) {
for (const page of this._bidiPages.values()) {
if (page._onRealmDestroyed(event))
return;
}
}
private _findPageForFrame(frameId: string) {
for (const page of this._bidiPages.values()) {
if (page._page.frameManager.frame(frameId))
return page;
}
}
}
export class BidiBrowserContext extends BrowserContext {
declare readonly _browser: BidiBrowser;
private _originToPermissions = new Map<string, string[]>();
private _initScriptIds = new Map<InitScript, string>();
private _interceptId: bidi.Network.Intercept | undefined;
constructor(browser: BidiBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
super(browser, options, browserContextId);
this._authenticateProxyViaHeader();
}
private _bidiPages() {
return [...this._browser._bidiPages.values()].filter(bidiPage => bidiPage._browserContext === this);
}
override async _initialize() {
const promises: Promise<any>[] = [
super._initialize(),
];
promises.push(this.doUpdateDefaultViewport());
if (this._options.geolocation)
promises.push(this.setGeolocation(this._options.geolocation));
if (this._options.locale) {
promises.push(this._browser._browserSession.send('emulation.setLocaleOverride', {
locale: this._options.locale,
userContexts: [this._userContextId()],
}));
}
if (this._options.timezoneId) {
promises.push(this._browser._browserSession.send('emulation.setTimezoneOverride', {
timezone: this._options.timezoneId,
userContexts: [this._userContextId()],
}));
}
if (this._options.userAgent) {
promises.push(this._browser._browserSession.send('emulation.setUserAgentOverride', {
userAgent: this._options.userAgent,
userContexts: [this._userContextId()],
}));
}
if (this._options.extraHTTPHeaders)
promises.push(this.doUpdateExtraHTTPHeaders());
if (this._options.permissions)
promises.push(this.doGrantPermissions('*', this._options.permissions));
await Promise.all(promises);
}
override possiblyUninitializedPages(): Page[] {
return this._bidiPages().map(bidiPage => bidiPage._page);
}
override async doCreateNewPage(): Promise<Page> {
const { context } = await this._browser._browserSession.send('browsingContext.create', {
type: bidi.BrowsingContext.CreateType.Window,
userContext: this._browserContextId,
});
return this._browser._bidiPages.get(context)!._page;
}
async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {
const { cookies } = await this._browser._browserSession.send('storage.getCookies',
{ partition: { type: 'storageKey', userContext: this._browserContextId } });
return network.filterCookies(cookies.map((c: bidi.Network.Cookie) => {
const copy: channels.NetworkCookie = {
name: c.name,
value: bidiBytesValueToString(c.value),
domain: c.domain,
path: c.path,
httpOnly: c.httpOnly,
secure: c.secure,
expires: c.expiry ?? -1,
sameSite: c.sameSite ? fromBidiSameSite(c.sameSite) : 'None',
};
return copy;
}), urls);
}
async addCookies(cookies: channels.SetNetworkCookie[]) {
cookies = network.rewriteCookies(cookies);
const promises = cookies.map((c: channels.SetNetworkCookie) => {
const cookie: bidi.Storage.PartialCookie = {
name: c.name,
value: { type: 'string', value: c.value },
domain: c.domain!,
path: c.path,
httpOnly: c.httpOnly,
secure: c.secure,
sameSite: c.sameSite && toBidiSameSite(c.sameSite),
expiry: (c.expires === -1 || c.expires === undefined) ? undefined : Math.round(c.expires),
};
return this._browser._browserSession.send('storage.setCookie',
{ cookie, partition: { type: 'storageKey', userContext: this._browserContextId } });
});
await Promise.all(promises);
}
async doClearCookies() {
await this._browser._browserSession.send('storage.deleteCookies',
{ partition: { type: 'storageKey', userContext: this._browserContextId } });
}
async doGrantPermissions(origin: string, permissions: string[]) {
if (origin === 'null')
return;
const currentPermissions = this._originToPermissions.get(origin) || [];
const toGrant = permissions.filter(permission => !currentPermissions.includes(permission));
this._originToPermissions.set(origin, [...currentPermissions, ...toGrant]);
if (origin === '*') {
await Promise.all(this._bidiPages().flatMap(page =>
page._page.frames().map(frame =>
this.doGrantPermissions(new URL(frame._url).origin, permissions)
)
));
} else {
await Promise.all(toGrant.map(permission => this._setPermission(origin, permission, bidi.Permissions.PermissionState.Granted)));
}
}
async doGrantGlobalPermissionsForURL(url: string) {
const permissions = this._originToPermissions.get('*');
if (!permissions)
return;
await this.doGrantPermissions(new URL(url).origin, permissions);
}
async doClearPermissions() {
const currentPermissions = [...this._originToPermissions.entries()];
this._originToPermissions = new Map();
await Promise.all(currentPermissions.flatMap(([origin, permissions]) => {
if (origin !== '*')
return permissions.map(p => this._setPermission(origin, p, bidi.Permissions.PermissionState.Prompt));
}));
}
private async _setPermission(origin: string, permission: string, state: bidi.Permissions.PermissionState) {
await this._browser._browserSession.send('permissions.setPermission', {
descriptor: {
name: permission,
},
state,
origin,
userContext: this._userContextId(),
});
}
async setGeolocation(geolocation?: types.Geolocation): Promise<void> {
verifyGeolocation(geolocation);
this._options.geolocation = geolocation;
// Setting geolocation on the user context automatically applies it to all existing
// pages in the context in Bidi.
await this._browser._browserSession.send('emulation.setGeolocationOverride', {
coordinates: geolocation ? {
latitude: geolocation.latitude,
longitude: geolocation.longitude,
accuracy: geolocation.accuracy,
} : null,
userContexts: [this._userContextId()],
});
}
async doUpdateExtraHTTPHeaders(): Promise<void> {
const allHeaders = this._options.extraHTTPHeaders || [];
await this._browser._browserSession.send('network.setExtraHeaders', {
headers: allHeaders.map(({ name, value }) => ({ name, value: { type: 'string' as 'string', value } })),
userContexts: [this._userContextId()],
});
}
async setUserAgent(userAgent: string | undefined): Promise<void> {
this._options.userAgent = userAgent;
await this._browser._browserSession.send('emulation.setUserAgentOverride', {
userAgent: userAgent ?? null,
userContexts: [this._userContextId()],
});
}
async doUpdateOffline(): Promise<void> {
}
async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> {
this._options.httpCredentials = httpCredentials;
for (const page of this.pages())
await (page.delegate as BidiPage).updateHttpCredentials();
}
async doAddInitScript(initScript: InitScript) {
const { script } = await this._browser._browserSession.send('script.addPreloadScript', {
// TODO: remove function call from the source.
functionDeclaration: `() => { return ${initScript.source} }`,
userContexts: [this._userContextId()],
});
this._initScriptIds.set(initScript, script);
}
async doRemoveInitScripts(initScripts: InitScript[]) {
const ids: string[] = [];
for (const script of initScripts) {
const id = this._initScriptIds.get(script);
if (id)
ids.push(id);
this._initScriptIds.delete(script);
}
await Promise.all(ids.map(script => this._browser._browserSession.send('script.removePreloadScript', { script })));
}
async doUpdateRequestInterception(): Promise<void> {
if (this.requestInterceptors.length > 0 && !this._interceptId) {
const { intercept } = await this._browser._browserSession.send('network.addIntercept', {
phases: [bidi.Network.InterceptPhase.BeforeRequestSent],
urlPatterns: [{ type: 'pattern' }],
});
this._interceptId = intercept;
}
if (this.requestInterceptors.length === 0 && this._interceptId) {
const intercept = this._interceptId;
this._interceptId = undefined;
await this._browser._browserSession.send('network.removeIntercept', { intercept });
}
}
override async doUpdateDefaultViewport() {
if (!this._options.viewport && !this._options.screen)
return;
const screenSize = (this._options.screen || this._options.viewport)!;
const viewportSize = (this._options.viewport || this._options.screen)!;
await Promise.all([
this._browser._browserSession.send('browsingContext.setViewport', {
viewport: {
width: viewportSize.width,
height: viewportSize.height
},
devicePixelRatio: this._options.deviceScaleFactor || 1,
userContexts: [this._userContextId()],
}),
this._browser._browserSession.send('emulation.setScreenOrientationOverride', {
screenOrientation: getScreenOrientation(!!this._options.isMobile, screenSize),
userContexts: [this._userContextId()],
}),
this._browser._browserSession.send('emulation.setScreenSettingsOverride', {
screenArea: {
width: screenSize.width,
height: screenSize.height,
},
userContexts: [this._userContextId()],
})
]);
}
override async doUpdateDefaultEmulatedMedia() {
}
override async doExposePlaywrightBinding() {
const args: bidi.Script.ChannelValue[] = [{
type: 'channel',
value: {
channel: kPlaywrightBindingChannel,
ownership: bidi.Script.ResultOwnership.Root,
}
}];
const functionDeclaration = `function addMainBinding(callback) { globalThis['${PageBinding.kBindingName}'] = callback; }`;
const promises = [];
promises.push(this._browser._browserSession.send('script.addPreloadScript', {
functionDeclaration,
arguments: args,
userContexts: [this._userContextId()],
}));
promises.push(...this._bidiPages().map(page => {
const realms = [...page._realmToContext].filter(([realm, context]) => context.world === 'main').map(([realm, context]) => realm);
return Promise.all(realms.map(realm => {
return page._session.send('script.callFunction', {
functionDeclaration,
arguments: args,
target: { realm },
awaitPromise: false,
userActivation: false,
});
}));
}));
await Promise.all(promises);
}
onClosePersistent() {}
override async clearCache(): Promise<void> {
}
async doClose(reason: string | undefined) {
if (!this._browserContextId) {
// Closing persistent context should close the browser.
await this._browser.close({ reason });
return;
}
await this._browser._browserSession.send('browser.removeUserContext', {
userContext: this._browserContextId
});
this._browser._contexts.delete(this._browserContextId);
}
async cancelDownload(uuid: string) {
}
private _userContextId(): bidi.Browser.UserContext {
if (this._browserContextId)
return this._browserContextId;
// Default context always has same id, see
// https://w3c.github.io/webdriver-bidi/#default-user-context
return 'default';
}
}
function fromBidiSameSite(sameSite: bidi.Network.SameSite): channels.NetworkCookie['sameSite'] {
switch (sameSite) {
case 'strict': return 'Strict';
case 'lax': return 'Lax';
case 'none': return 'None';
case 'default': return 'Lax';
}
return 'None';
}
function toBidiSameSite(sameSite: channels.SetNetworkCookie['sameSite']): bidi.Network.SameSite {
switch (sameSite) {
case 'Strict': return bidi.Network.SameSite.Strict;
case 'Lax': return bidi.Network.SameSite.Lax;
case 'None': return bidi.Network.SameSite.None;
}
return bidi.Network.SameSite.None;
}
function getProxyConfiguration(proxySettings?: types.ProxySettings): bidi.Session.ManualProxyConfiguration | undefined {
if (!proxySettings)
return undefined;
const proxy: bidi.Session.ManualProxyConfiguration = {
proxyType: 'manual',
};
const url = new URL(proxySettings.server); // Validate proxy server.
switch (url.protocol) {
case 'http:':
proxy.httpProxy = url.host;
break;
case 'https:':
proxy.sslProxy = url.host;
break;
case 'socks4:':
proxy.socksProxy = url.host;
proxy.socksVersion = 4;
break;
case 'socks5:':
proxy.socksProxy = url.host;
proxy.socksVersion = 5;
break;
default:
throw new Error('Invalid proxy server protocol: ' + proxySettings.server);
}
const bypass = proxySettings.bypass ?? process.env.PLAYWRIGHT_PROXY_BYPASS_FOR_TESTING;
if (bypass)
proxy.noProxy = bypass.split(',');
// TODO: support authentication.
return proxy;
}
export function getScreenOrientation(isMobile: boolean, viewportSize: types.Size) {
const screenOrientation: bidi.Emulation.ScreenOrientation = {
type: 'landscape-primary',
natural: bidi.Emulation.ScreenOrientationNatural.Landscape
};
if (isMobile) {
screenOrientation.natural = bidi.Emulation.ScreenOrientationNatural.Portrait;
if (viewportSize.width <= viewportSize.height)
screenOrientation.type = 'portrait-primary';
}
return screenOrientation;
}
export namespace Network {
export const enum SameSite {
Strict = 'strict',
Lax = 'lax',
None = 'none',
}
}