mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: extract BrowserContext class (#12053)
This commit is contained in:
parent
5174f2b448
commit
5b0adda5b9
@ -12,20 +12,18 @@ import type {DebugInfo} from '../api/Browser.js';
|
||||
import {
|
||||
Browser as BrowserBase,
|
||||
BrowserEvent,
|
||||
WEB_PERMISSION_TO_PROTOCOL_PERMISSION,
|
||||
type BrowserCloseCallback,
|
||||
type BrowserContextOptions,
|
||||
type IsPageTargetCallback,
|
||||
type Permission,
|
||||
type TargetFilterCallback,
|
||||
} from '../api/Browser.js';
|
||||
import {BrowserContext, BrowserContextEvent} from '../api/BrowserContext.js';
|
||||
import {BrowserContextEvent} from '../api/BrowserContext.js';
|
||||
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
|
||||
import type {Page} from '../api/Page.js';
|
||||
import type {Target} from '../api/Target.js';
|
||||
import type {Viewport} from '../common/Viewport.js';
|
||||
import {assert} from '../util/assert.js';
|
||||
|
||||
import {CdpBrowserContext} from './BrowserContext.js';
|
||||
import {ChromeTargetManager} from './ChromeTargetManager.js';
|
||||
import type {Connection} from './Connection.js';
|
||||
import {FirefoxTargetManager} from './FirefoxTargetManager.js';
|
||||
@ -424,90 +422,3 @@ export class CdpBrowser extends BrowserBase {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export class CdpBrowserContext extends BrowserContext {
|
||||
#connection: Connection;
|
||||
#browser: CdpBrowser;
|
||||
#id?: string;
|
||||
|
||||
constructor(connection: Connection, browser: CdpBrowser, contextId?: string) {
|
||||
super();
|
||||
this.#connection = connection;
|
||||
this.#browser = browser;
|
||||
this.#id = contextId;
|
||||
}
|
||||
|
||||
override get id(): string | undefined {
|
||||
return this.#id;
|
||||
}
|
||||
|
||||
override targets(): CdpTarget[] {
|
||||
return this.#browser.targets().filter(target => {
|
||||
return target.browserContext() === this;
|
||||
});
|
||||
}
|
||||
|
||||
override async pages(): Promise<Page[]> {
|
||||
const pages = await Promise.all(
|
||||
this.targets()
|
||||
.filter(target => {
|
||||
return (
|
||||
target.type() === 'page' ||
|
||||
(target.type() === 'other' &&
|
||||
this.#browser._getIsPageTargetCallback()?.(target))
|
||||
);
|
||||
})
|
||||
.map(target => {
|
||||
return target.page();
|
||||
})
|
||||
);
|
||||
return pages.filter((page): page is Page => {
|
||||
return !!page;
|
||||
});
|
||||
}
|
||||
|
||||
override isIncognito(): boolean {
|
||||
return !!this.#id;
|
||||
}
|
||||
|
||||
override async overridePermissions(
|
||||
origin: string,
|
||||
permissions: Permission[]
|
||||
): Promise<void> {
|
||||
const protocolPermissions = permissions.map(permission => {
|
||||
const protocolPermission =
|
||||
WEB_PERMISSION_TO_PROTOCOL_PERMISSION.get(permission);
|
||||
if (!protocolPermission) {
|
||||
throw new Error('Unknown permission: ' + permission);
|
||||
}
|
||||
return protocolPermission;
|
||||
});
|
||||
await this.#connection.send('Browser.grantPermissions', {
|
||||
origin,
|
||||
browserContextId: this.#id || undefined,
|
||||
permissions: protocolPermissions,
|
||||
});
|
||||
}
|
||||
|
||||
override async clearPermissionOverrides(): Promise<void> {
|
||||
await this.#connection.send('Browser.resetPermissions', {
|
||||
browserContextId: this.#id || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
override newPage(): Promise<Page> {
|
||||
return this.#browser._createPageInContext(this.#id);
|
||||
}
|
||||
|
||||
override browser(): CdpBrowser {
|
||||
return this.#browser;
|
||||
}
|
||||
|
||||
override async close(): Promise<void> {
|
||||
assert(this.#id, 'Non-incognito profiles cannot be closed!');
|
||||
await this.#browser._disposeContext(this.#id);
|
||||
}
|
||||
}
|
||||
|
104
packages/puppeteer-core/src/cdp/BrowserContext.ts
Normal file
104
packages/puppeteer-core/src/cdp/BrowserContext.ts
Normal file
@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2024 Google Inc.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {
|
||||
WEB_PERMISSION_TO_PROTOCOL_PERMISSION,
|
||||
type Permission,
|
||||
} from '../api/Browser.js';
|
||||
import {BrowserContext} from '../api/BrowserContext.js';
|
||||
import type {Page} from '../api/Page.js';
|
||||
import {assert} from '../util/assert.js';
|
||||
|
||||
import type {CdpBrowser} from './Browser.js';
|
||||
import type {Connection} from './Connection.js';
|
||||
import type {CdpTarget} from './Target.js';
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export class CdpBrowserContext extends BrowserContext {
|
||||
#connection: Connection;
|
||||
#browser: CdpBrowser;
|
||||
#id?: string;
|
||||
|
||||
constructor(connection: Connection, browser: CdpBrowser, contextId?: string) {
|
||||
super();
|
||||
this.#connection = connection;
|
||||
this.#browser = browser;
|
||||
this.#id = contextId;
|
||||
}
|
||||
|
||||
override get id(): string | undefined {
|
||||
return this.#id;
|
||||
}
|
||||
|
||||
override targets(): CdpTarget[] {
|
||||
return this.#browser.targets().filter(target => {
|
||||
return target.browserContext() === this;
|
||||
});
|
||||
}
|
||||
|
||||
override async pages(): Promise<Page[]> {
|
||||
const pages = await Promise.all(
|
||||
this.targets()
|
||||
.filter(target => {
|
||||
return (
|
||||
target.type() === 'page' ||
|
||||
(target.type() === 'other' &&
|
||||
this.#browser._getIsPageTargetCallback()?.(target))
|
||||
);
|
||||
})
|
||||
.map(target => {
|
||||
return target.page();
|
||||
})
|
||||
);
|
||||
return pages.filter((page): page is Page => {
|
||||
return !!page;
|
||||
});
|
||||
}
|
||||
|
||||
override isIncognito(): boolean {
|
||||
return !!this.#id;
|
||||
}
|
||||
|
||||
override async overridePermissions(
|
||||
origin: string,
|
||||
permissions: Permission[]
|
||||
): Promise<void> {
|
||||
const protocolPermissions = permissions.map(permission => {
|
||||
const protocolPermission =
|
||||
WEB_PERMISSION_TO_PROTOCOL_PERMISSION.get(permission);
|
||||
if (!protocolPermission) {
|
||||
throw new Error('Unknown permission: ' + permission);
|
||||
}
|
||||
return protocolPermission;
|
||||
});
|
||||
await this.#connection.send('Browser.grantPermissions', {
|
||||
origin,
|
||||
browserContextId: this.#id || undefined,
|
||||
permissions: protocolPermissions,
|
||||
});
|
||||
}
|
||||
|
||||
override async clearPermissionOverrides(): Promise<void> {
|
||||
await this.#connection.send('Browser.resetPermissions', {
|
||||
browserContextId: this.#id || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
override newPage(): Promise<Page> {
|
||||
return this.#browser._createPageInContext(this.#id);
|
||||
}
|
||||
|
||||
override browser(): CdpBrowser {
|
||||
return this.#browser;
|
||||
}
|
||||
|
||||
override async close(): Promise<void> {
|
||||
assert(this.#id, 'Non-incognito profiles cannot be closed!');
|
||||
await this.#browser._disposeContext(this.#id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user