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(context): unify browser context id handling #3629

Merged
merged 1 commit into from
Aug 25, 2020
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
6 changes: 4 additions & 2 deletions src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ export abstract class BrowserContext extends EventEmitter {
readonly _downloads = new Set<Download>();
readonly _idToScreencast = new Map<string, Screencast>();
readonly _browser: Browser;
readonly _browserContextId: string | undefined;

constructor(browser: Browser, options: types.BrowserContextOptions, isPersistentContext: boolean) {
constructor(browser: Browser, options: types.BrowserContextOptions, browserContextId: string | undefined) {
super();
this._browser = browser;
this._options = options;
this._isPersistentContext = isPersistentContext;
this._browserContextId = browserContextId;
this._isPersistentContext = !browserContextId;
this._closePromise = new Promise(fulfill => this._closePromiseFulfill = fulfill);
}

Expand Down
22 changes: 10 additions & 12 deletions src/server/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class CRBrowser extends Browser {
await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });
return browser;
}
browser._defaultContext = new CRBrowserContext(browser, null, options.persistent);
browser._defaultContext = new CRBrowserContext(browser, undefined, options.persistent);

const existingTargetAttachPromises: Promise<any>[] = [];
// First page, background pages and their service workers in the persistent context
Expand Down Expand Up @@ -284,13 +284,11 @@ export class CRBrowserContext extends BrowserContext {
};

readonly _browser: CRBrowser;
readonly _browserContextId: string | null;
readonly _evaluateOnNewDocumentSources: string[];

constructor(browser: CRBrowser, browserContextId: string | null, options: types.BrowserContextOptions) {
super(browser, options, !browserContextId);
constructor(browser: CRBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
super(browser, options, browserContextId);
this._browser = browser;
this._browserContextId = browserContextId;
this._evaluateOnNewDocumentSources = [];
this._authenticateProxyViaCredentials();
}
Expand All @@ -301,7 +299,7 @@ export class CRBrowserContext extends BrowserContext {
if (this._browser._options.downloadsPath) {
promises.push(this._browser._session.send('Browser.setDownloadBehavior', {
behavior: this._options.acceptDownloads ? 'allowAndName' : 'deny',
browserContextId: this._browserContextId || undefined,
browserContextId: this._browserContextId,
downloadPath: this._browser._options.downloadsPath
}));
}
Expand All @@ -321,7 +319,7 @@ export class CRBrowserContext extends BrowserContext {

async newPage(): Promise<Page> {
assertBrowserContextIsNotOwned(this);
const { targetId } = await this._browser._session.send('Target.createTarget', { url: 'about:blank', browserContextId: this._browserContextId || undefined });
const { targetId } = await this._browser._session.send('Target.createTarget', { url: 'about:blank', browserContextId: this._browserContextId });
const crPage = this._browser._crPages.get(targetId)!;
const result = await crPage.pageOrError();
if (result instanceof Page) {
Expand All @@ -333,7 +331,7 @@ export class CRBrowserContext extends BrowserContext {
}

async _doCookies(urls: string[]): Promise<types.NetworkCookie[]> {
const { cookies } = await this._browser._session.send('Storage.getCookies', { browserContextId: this._browserContextId || undefined });
const { cookies } = await this._browser._session.send('Storage.getCookies', { browserContextId: this._browserContextId });
return network.filterCookies(cookies.map(c => {
const copy: any = { sameSite: 'None', ...c };
delete copy.size;
Expand All @@ -344,11 +342,11 @@ export class CRBrowserContext extends BrowserContext {
}

async addCookies(cookies: types.SetNetworkCookieParam[]) {
await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId || undefined });
await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId });
}

async clearCookies() {
await this._browser._session.send('Storage.clearCookies', { browserContextId: this._browserContextId || undefined });
await this._browser._session.send('Storage.clearCookies', { browserContextId: this._browserContextId });
}

async _doGrantPermissions(origin: string, permissions: string[]) {
Expand Down Expand Up @@ -376,11 +374,11 @@ export class CRBrowserContext extends BrowserContext {
throw new Error('Unknown permission: ' + permission);
return protocolPermission;
});
await this._browser._session.send('Browser.grantPermissions', { origin: origin === '*' ? undefined : origin, browserContextId: this._browserContextId || undefined, permissions: filtered });
await this._browser._session.send('Browser.grantPermissions', { origin: origin === '*' ? undefined : origin, browserContextId: this._browserContextId, permissions: filtered });
}

async _doClearPermissions() {
await this._browser._session.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
await this._browser._session.send('Browser.resetPermissions', { browserContextId: this._browserContextId });
}

async setGeolocation(geolocation?: types.Geolocation): Promise<void> {
Expand Down
38 changes: 18 additions & 20 deletions src/server/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class FFBrowser extends Browser {
browser._initVersion(),
];
if (options.persistent) {
browser._defaultContext = new FFBrowserContext(browser, null, options.persistent);
browser._defaultContext = new FFBrowserContext(browser, undefined, options.persistent);
promises.push((browser._defaultContext as FFBrowserContext)._initialize());
}
if (options.proxy) {
Expand Down Expand Up @@ -165,17 +165,15 @@ export class FFBrowser extends Browser {

export class FFBrowserContext extends BrowserContext {
readonly _browser: FFBrowser;
readonly _browserContextId: string | null;

constructor(browser: FFBrowser, browserContextId: string | null, options: types.BrowserContextOptions) {
super(browser, options, !browserContextId);
constructor(browser: FFBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
super(browser, options, browserContextId);
this._browser = browser;
this._browserContextId = browserContextId;
}

async _initialize() {
assert(!this._ffPages().length);
const browserContextId = this._browserContextId || undefined;
const browserContextId = this._browserContextId;
const promises: Promise<any>[] = [ super._initialize() ];
if (this._browser._options.downloadsPath) {
promises.push(this._browser._connection.send('Browser.setDownloadOptions', {
Expand Down Expand Up @@ -233,7 +231,7 @@ export class FFBrowserContext extends BrowserContext {
async newPage(): Promise<Page> {
assertBrowserContextIsNotOwned(this);
const { targetId } = await this._browser._connection.send('Browser.newPage', {
browserContextId: this._browserContextId || undefined
browserContextId: this._browserContextId
}).catch(e => {
if (e.message.includes('Failed to override timezone'))
throw new Error(`Invalid timezone ID: ${this._options.timezoneId}`);
Expand All @@ -250,7 +248,7 @@ export class FFBrowserContext extends BrowserContext {
}

async _doCookies(urls: string[]): Promise<types.NetworkCookie[]> {
const { cookies } = await this._browser._connection.send('Browser.getCookies', { browserContextId: this._browserContextId || undefined });
const { cookies } = await this._browser._connection.send('Browser.getCookies', { browserContextId: this._browserContextId });
return network.filterCookies(cookies.map(c => {
const copy: any = { ... c };
delete copy.size;
Expand All @@ -260,11 +258,11 @@ export class FFBrowserContext extends BrowserContext {
}

async addCookies(cookies: types.SetNetworkCookieParam[]) {
await this._browser._connection.send('Browser.setCookies', { browserContextId: this._browserContextId || undefined, cookies: network.rewriteCookies(cookies) });
await this._browser._connection.send('Browser.setCookies', { browserContextId: this._browserContextId, cookies: network.rewriteCookies(cookies) });
}

async clearCookies() {
await this._browser._connection.send('Browser.clearCookies', { browserContextId: this._browserContextId || undefined });
await this._browser._connection.send('Browser.clearCookies', { browserContextId: this._browserContextId });
}

async _doGrantPermissions(origin: string, permissions: string[]) {
Expand All @@ -280,52 +278,52 @@ export class FFBrowserContext extends BrowserContext {
throw new Error('Unknown permission: ' + permission);
return protocolPermission;
});
await this._browser._connection.send('Browser.grantPermissions', { origin: origin, browserContextId: this._browserContextId || undefined, permissions: filtered});
await this._browser._connection.send('Browser.grantPermissions', { origin: origin, browserContextId: this._browserContextId, permissions: filtered});
}

async _doClearPermissions() {
await this._browser._connection.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
await this._browser._connection.send('Browser.resetPermissions', { browserContextId: this._browserContextId });
}

async setGeolocation(geolocation?: types.Geolocation): Promise<void> {
verifyGeolocation(geolocation);
this._options.geolocation = geolocation;
await this._browser._connection.send('Browser.setGeolocationOverride', { browserContextId: this._browserContextId || undefined, geolocation: geolocation || null });
await this._browser._connection.send('Browser.setGeolocationOverride', { browserContextId: this._browserContextId, geolocation: geolocation || null });
}

async setExtraHTTPHeaders(headers: types.HeadersArray): Promise<void> {
this._options.extraHTTPHeaders = headers;
let allHeaders = this._options.extraHTTPHeaders;
if (this._options.locale)
allHeaders = network.mergeHeaders([allHeaders, network.singleHeader('Accept-Language', this._options.locale)]);
await this._browser._connection.send('Browser.setExtraHTTPHeaders', { browserContextId: this._browserContextId || undefined, headers: allHeaders });
await this._browser._connection.send('Browser.setExtraHTTPHeaders', { browserContextId: this._browserContextId, headers: allHeaders });
}

async setOffline(offline: boolean): Promise<void> {
this._options.offline = offline;
await this._browser._connection.send('Browser.setOnlineOverride', { browserContextId: this._browserContextId || undefined, override: offline ? 'offline' : 'online' });
await this._browser._connection.send('Browser.setOnlineOverride', { browserContextId: this._browserContextId, override: offline ? 'offline' : 'online' });
}

async _doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> {
this._options.httpCredentials = httpCredentials;
await this._browser._connection.send('Browser.setHTTPCredentials', { browserContextId: this._browserContextId || undefined, credentials: httpCredentials || null });
await this._browser._connection.send('Browser.setHTTPCredentials', { browserContextId: this._browserContextId, credentials: httpCredentials || null });
}

async _doAddInitScript(source: string) {
await this._browser._connection.send('Browser.addScriptToEvaluateOnNewDocument', { browserContextId: this._browserContextId || undefined, script: source });
await this._browser._connection.send('Browser.addScriptToEvaluateOnNewDocument', { browserContextId: this._browserContextId, script: source });
}

async _doExposeBinding(binding: PageBinding) {
await this._browser._connection.send('Browser.addBinding', { browserContextId: this._browserContextId || undefined, name: binding.name, script: binding.source });
await this._browser._connection.send('Browser.addBinding', { browserContextId: this._browserContextId, name: binding.name, script: binding.source });
}

async _doUpdateRequestInterception(): Promise<void> {
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId || undefined, enabled: !!this._requestInterceptor });
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId, enabled: !!this._requestInterceptor });
}

async _enableScreencast(options: types.ContextScreencastOptions): Promise<void> {
await super._enableScreencast(options);
await this._browser._connection.send('Browser.setScreencastOptions', Object.assign({}, options, { browserContextId: this._browserContextId || undefined}));
await this._browser._connection.send('Browser.setScreencastOptions', Object.assign({}, options, { browserContextId: this._browserContextId}));
}

async _doClose() {
Expand Down
3 changes: 1 addition & 2 deletions src/server/webkit/wkBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,8 @@ export class WKBrowserContext extends BrowserContext {
readonly _evaluateOnNewDocumentSources: string[];

constructor(browser: WKBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
super(browser, options, !browserContextId);
super(browser, options, browserContextId);
this._browser = browser;
this._browserContextId = browserContextId;
this._evaluateOnNewDocumentSources = [];
this._authenticateProxyViaHeader();
}
Expand Down