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

chore(bidi): retrieve default context id from the browser #35286

Merged
merged 1 commit into from
Mar 19, 2025
Merged
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
29 changes: 17 additions & 12 deletions packages/playwright-core/src/server/bidi/bidiBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ export class BidiBrowser extends Browser {
});

if (options.persistent) {
browser._defaultContext = new BidiBrowserContext(browser, undefined, options.persistent);
const { userContexts } = await browser._browserSession.send('browser.getUserContexts', {});
if (!userContexts.length)
throw new Error('Cannot dermine default context id, no contexts found.');
const context = new BidiBrowserContext(browser, undefined, options.persistent);
context._defaultUserContext = userContexts[0].userContext;
browser._defaultContext = context;
await (browser._defaultContext as BidiBrowserContext)._initialize();
// Create default page as we cannot get access to the existing one.
const page = await browser._defaultContext.doCreateNewPage();
Expand Down Expand Up @@ -204,6 +209,7 @@ export class BidiBrowser extends Browser {
export class BidiBrowserContext extends BrowserContext {
declare readonly _browser: BidiBrowser;
private _initScriptIds: bidi.Script.PreloadScript[] = [];
_defaultUserContext: bidi.Browser.UserContext | undefined;

constructor(browser: BidiBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
super(browser, options, browserContextId);
Expand All @@ -219,25 +225,21 @@ export class BidiBrowserContext extends BrowserContext {
super._initialize(),
this._installMainBinding(),
];
// FIXME: Persistent context doesn't have an id, so we can't set the command for it.
if (this._options.viewport && this._browserContextId) {
if (this._options.viewport) {
promises.push(this._browser._browserSession.send('browsingContext.setViewport', {
viewport: {
width: this._options.viewport.width,
height: this._options.viewport.height
},
devicePixelRatio: this._options.deviceScaleFactor || 1,
userContexts: [this._browserContextId],
userContexts: [this._userContextId()],
}));
}
await Promise.all(promises);
}

// TODO: consider calling this only when bindings are added.
private async _installMainBinding() {
// TODO: Cannot install main binding for persistent context as it doesn't have an id.
if (!this._browserContextId)
return;
const functionDeclaration = addMainBinding.toString();
const args: bidi.Script.ChannelValue[] = [{
type: 'channel',
Expand All @@ -249,7 +251,7 @@ export class BidiBrowserContext extends BrowserContext {
await this._browser._browserSession.send('script.addPreloadScript', {
functionDeclaration,
arguments: args,
userContexts: [this._browserContextId],
userContexts: [this._userContextId()],
});
}

Expand Down Expand Up @@ -333,13 +335,10 @@ export class BidiBrowserContext extends BrowserContext {
}

async doAddInitScript(initScript: InitScript) {
// TODO: support persistent context.
if (!this._browserContextId)
return;
const { script } = await this._browser._browserSession.send('script.addPreloadScript', {
// TODO: remove function call from the source.
functionDeclaration: `() => { return ${initScript.source} }`,
userContexts: [this._browserContextId],
userContexts: [this._browserContextId || 'default'],
});
if (!initScript.internal)
this._initScriptIds.push(script);
Expand Down Expand Up @@ -373,6 +372,12 @@ export class BidiBrowserContext extends BrowserContext {

async cancelDownload(uuid: string) {
}

private _userContextId(): bidi.Browser.UserContext {
if (this._browserContextId)
return this._browserContextId;
return this._defaultUserContext!;
}
}

function fromBidiSameSite(sameSite: bidi.Network.SameSite): channels.NetworkCookie['sameSite'] {
Expand Down
Loading