2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications 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 { Events } from './events';
|
|
|
|
import { Events as CommonEvents } from '../events';
|
|
|
|
import { assert, helper } from '../helper';
|
2020-02-24 08:53:30 -08:00
|
|
|
import { BrowserContext, BrowserContextOptions, validateBrowserContextOptions, assertBrowserContextIsNotOwned, verifyGeolocation } from '../browserContext';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { CRConnection, ConnectionEvents, CRSession } from './crConnection';
|
2020-02-24 08:31:58 -08:00
|
|
|
import { Page } from '../page';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { CRTarget } from './crTarget';
|
|
|
|
import { Protocol } from './protocol';
|
2019-12-23 11:39:57 -08:00
|
|
|
import { CRPage } from './crPage';
|
2020-02-11 12:06:58 -08:00
|
|
|
import { Browser, createPageInNewContext } from '../browser';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as network from '../network';
|
2020-01-03 10:14:50 -08:00
|
|
|
import * as types from '../types';
|
2020-01-07 11:55:24 -08:00
|
|
|
import * as platform from '../platform';
|
2019-12-19 17:03:27 -08:00
|
|
|
import { readProtocolStream } from './crProtocolHelper';
|
2020-02-04 19:41:38 -08:00
|
|
|
import { ConnectionTransport, SlowMoTransport } from '../transport';
|
2020-02-24 08:53:30 -08:00
|
|
|
import { TimeoutSettings } from '../timeoutSettings';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-08 14:04:33 -08:00
|
|
|
export class CRBrowser extends platform.EventEmitter implements Browser {
|
2019-12-19 16:53:24 -08:00
|
|
|
_connection: CRConnection;
|
|
|
|
_client: CRSession;
|
2020-02-24 14:35:51 -08:00
|
|
|
readonly _defaultContext: CRBrowserContext;
|
2020-02-24 08:53:30 -08:00
|
|
|
readonly _contexts = new Map<string, CRBrowserContext>();
|
2019-12-19 16:53:24 -08:00
|
|
|
_targets = new Map<string, CRTarget>();
|
|
|
|
|
|
|
|
private _tracingRecording = false;
|
2020-01-13 13:33:25 -08:00
|
|
|
private _tracingPath: string | null = '';
|
2019-12-19 16:53:24 -08:00
|
|
|
private _tracingClient: CRSession | undefined;
|
|
|
|
|
2020-02-04 19:41:38 -08:00
|
|
|
static async connect(transport: ConnectionTransport, slowMo?: number): Promise<CRBrowser> {
|
|
|
|
const connection = new CRConnection(SlowMoTransport.wrap(transport, slowMo));
|
2020-02-06 12:41:43 -08:00
|
|
|
const browser = new CRBrowser(connection);
|
2019-12-19 16:53:24 -08:00
|
|
|
await connection.rootSession.send('Target.setDiscoverTargets', { discover: true });
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:41:43 -08:00
|
|
|
constructor(connection: CRConnection) {
|
2019-12-19 16:53:24 -08:00
|
|
|
super();
|
|
|
|
this._connection = connection;
|
|
|
|
this._client = connection.rootSession;
|
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({}));
|
2020-02-11 10:27:19 -08:00
|
|
|
this._connection.on(ConnectionEvents.Disconnected, () => {
|
2020-02-24 08:53:30 -08:00
|
|
|
for (const context of this._contexts.values())
|
2020-02-11 10:27:19 -08:00
|
|
|
context._browserClosed();
|
|
|
|
this.emit(CommonEvents.Browser.Disconnected);
|
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
this._client.on('Target.targetCreated', this._targetCreated.bind(this));
|
|
|
|
this._client.on('Target.targetDestroyed', this._targetDestroyed.bind(this));
|
|
|
|
this._client.on('Target.targetInfoChanged', this._targetInfoChanged.bind(this));
|
|
|
|
}
|
2020-02-10 10:41:45 -08:00
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2020-02-24 08:53:30 -08:00
|
|
|
options = validateBrowserContextOptions(options);
|
2020-02-26 11:01:01 -08:00
|
|
|
const { browserContextId } = await this._client.send('Target.createBrowserContext', { disposeOnDetach: true });
|
2020-02-24 08:53:30 -08:00
|
|
|
const context = new CRBrowserContext(this, browserContextId, options);
|
2020-01-13 13:32:44 -08:00
|
|
|
await context._initialize();
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contexts.set(browserContextId, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
contexts(): BrowserContext[] {
|
2020-02-05 12:41:55 -08:00
|
|
|
return Array.from(this._contexts.values());
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
async newPage(options?: BrowserContextOptions): Promise<Page> {
|
2020-02-11 12:06:58 -08:00
|
|
|
return createPageInNewContext(this, options);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async _targetCreated(event: Protocol.Target.targetCreatedPayload) {
|
|
|
|
const targetInfo = event.targetInfo;
|
|
|
|
const {browserContextId} = targetInfo;
|
2020-01-13 13:33:25 -08:00
|
|
|
const context = (browserContextId && this._contexts.has(browserContextId)) ? this._contexts.get(browserContextId)! : this._defaultContext;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
|
|
|
const target = new CRTarget(this, targetInfo, context, () => this._connection.createSession(targetInfo));
|
|
|
|
assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
|
|
|
|
this._targets.set(event.targetInfo.targetId, target);
|
|
|
|
|
|
|
|
if (target._isInitialized || await target._initializedPromise)
|
2020-02-24 14:35:51 -08:00
|
|
|
context.emit(Events.CRBrowserContext.TargetCreated, target);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async _targetDestroyed(event: { targetId: string; }) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const target = this._targets.get(event.targetId)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
target._initializedCallback(false);
|
|
|
|
this._targets.delete(event.targetId);
|
|
|
|
target._didClose();
|
|
|
|
if (await target._initializedPromise)
|
2020-02-24 14:35:51 -08:00
|
|
|
target.context().emit(Events.CRBrowserContext.TargetDestroyed, target);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_targetInfoChanged(event: Protocol.Target.targetInfoChangedPayload) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const target = this._targets.get(event.targetInfo.targetId)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
assert(target, 'target should exist before targetInfoChanged');
|
|
|
|
const previousURL = target.url();
|
|
|
|
const wasInitialized = target._isInitialized;
|
|
|
|
target._targetInfoChanged(event.targetInfo);
|
|
|
|
if (wasInitialized && previousURL !== target.url())
|
2020-02-24 14:35:51 -08:00
|
|
|
target.context().emit(Events.CRBrowserContext.TargetChanged, target);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async _closePage(page: Page) {
|
|
|
|
await this._client.send('Target.closeTarget', { targetId: CRTarget.fromPage(page)._targetId });
|
|
|
|
}
|
|
|
|
|
|
|
|
_allTargets(): CRTarget[] {
|
|
|
|
return Array.from(this._targets.values()).filter(target => target._isInitialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2020-01-08 13:55:38 -08:00
|
|
|
const disconnected = new Promise(f => this._connection.once(ConnectionEvents.Disconnected, f));
|
2020-02-10 10:41:45 -08:00
|
|
|
await Promise.all(this.contexts().map(context => context.close()));
|
2020-02-06 12:41:43 -08:00
|
|
|
this._connection.close();
|
2020-01-08 13:55:38 -08:00
|
|
|
await disconnected;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
browserTarget(): CRTarget {
|
2020-01-13 13:33:25 -08:00
|
|
|
return [...this._targets.values()].find(t => t.type() === 'browser')!;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async startTracing(page: Page | undefined, options: { path?: string; screenshots?: boolean; categories?: string[]; } = {}) {
|
|
|
|
assert(!this._tracingRecording, 'Cannot start recording trace while already recording trace.');
|
2019-12-23 11:39:57 -08:00
|
|
|
this._tracingClient = page ? (page._delegate as CRPage)._client : this._client;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
|
|
|
const defaultCategories = [
|
|
|
|
'-*', 'devtools.timeline', 'v8.execute', 'disabled-by-default-devtools.timeline',
|
|
|
|
'disabled-by-default-devtools.timeline.frame', 'toplevel',
|
|
|
|
'blink.console', 'blink.user_timing', 'latencyInfo', 'disabled-by-default-devtools.timeline.stack',
|
|
|
|
'disabled-by-default-v8.cpu_profiler', 'disabled-by-default-v8.cpu_profiler.hires'
|
|
|
|
];
|
|
|
|
const {
|
|
|
|
path = null,
|
|
|
|
screenshots = false,
|
|
|
|
categories = defaultCategories,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
if (screenshots)
|
|
|
|
categories.push('disabled-by-default-devtools.screenshot');
|
|
|
|
|
|
|
|
this._tracingPath = path;
|
|
|
|
this._tracingRecording = true;
|
|
|
|
await this._tracingClient.send('Tracing.start', {
|
|
|
|
transferMode: 'ReturnAsStream',
|
|
|
|
categories: categories.join(',')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-07 11:55:24 -08:00
|
|
|
async stopTracing(): Promise<platform.BufferType> {
|
2019-12-19 16:53:24 -08:00
|
|
|
assert(this._tracingClient, 'Tracing was not started.');
|
2020-01-07 11:55:24 -08:00
|
|
|
let fulfill: (buffer: platform.BufferType) => void;
|
|
|
|
const contentPromise = new Promise<platform.BufferType>(x => fulfill = x);
|
2020-02-05 16:53:36 -08:00
|
|
|
this._tracingClient.once('Tracing.tracingComplete', event => {
|
2020-01-13 13:33:25 -08:00
|
|
|
readProtocolStream(this._tracingClient!, event.stream!, this._tracingPath).then(fulfill);
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
2020-02-07 13:38:50 -08:00
|
|
|
await this._tracingClient.send('Tracing.end');
|
2019-12-19 16:53:24 -08:00
|
|
|
this._tracingRecording = false;
|
|
|
|
return contentPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
return !this._connection._closed;
|
|
|
|
}
|
2020-02-12 22:35:06 -08:00
|
|
|
|
|
|
|
_setDebugFunction(debugFunction: (message: string) => void) {
|
|
|
|
this._connection._debugProtocol = debugFunction;
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-02-24 08:53:30 -08:00
|
|
|
|
|
|
|
export class CRBrowserContext extends platform.EventEmitter implements BrowserContext {
|
|
|
|
readonly _browser: CRBrowser;
|
|
|
|
readonly _browserContextId: string | null;
|
|
|
|
readonly _options: BrowserContextOptions;
|
|
|
|
readonly _timeoutSettings: TimeoutSettings;
|
|
|
|
private _closed = false;
|
|
|
|
|
|
|
|
constructor(browser: CRBrowser, browserContextId: string | null, options: BrowserContextOptions) {
|
|
|
|
super();
|
|
|
|
this._browser = browser;
|
|
|
|
this._browserContextId = browserContextId;
|
|
|
|
this._timeoutSettings = new TimeoutSettings();
|
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _initialize() {
|
|
|
|
const entries = Object.entries(this._options.permissions || {});
|
|
|
|
await Promise.all(entries.map(entry => this.setPermissions(entry[0], entry[1])));
|
|
|
|
if (this._options.geolocation)
|
|
|
|
await this.setGeolocation(this._options.geolocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
_existingPages(): Page[] {
|
|
|
|
const pages: Page[] = [];
|
|
|
|
for (const target of this._browser._allTargets()) {
|
|
|
|
if (target.context() === this && target._crPage)
|
|
|
|
pages.push(target._crPage.page());
|
|
|
|
}
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async pages(): Promise<Page[]> {
|
|
|
|
const targets = this._browser._allTargets().filter(target => target.context() === this && target.type() === 'page');
|
|
|
|
const pages = await Promise.all(targets.map(target => target.page()));
|
|
|
|
return pages.filter(page => !!page) as Page[];
|
|
|
|
}
|
|
|
|
|
|
|
|
async newPage(): Promise<Page> {
|
|
|
|
assertBrowserContextIsNotOwned(this);
|
|
|
|
const { targetId } = await this._browser._client.send('Target.createTarget', { url: 'about:blank', browserContextId: this._browserContextId || undefined });
|
|
|
|
const target = this._browser._targets.get(targetId)!;
|
|
|
|
assert(await target._initializedPromise, 'Failed to create target for page');
|
|
|
|
const page = await target.page();
|
|
|
|
return page!;
|
|
|
|
}
|
|
|
|
|
|
|
|
async cookies(...urls: string[]): Promise<network.NetworkCookie[]> {
|
|
|
|
const { cookies } = await this._browser._client.send('Storage.getCookies', { browserContextId: this._browserContextId || undefined });
|
|
|
|
return network.filterCookies(cookies.map(c => {
|
|
|
|
const copy: any = { sameSite: 'None', ...c };
|
|
|
|
delete copy.size;
|
|
|
|
delete copy.priority;
|
|
|
|
return copy as network.NetworkCookie;
|
|
|
|
}), urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setCookies(cookies: network.SetNetworkCookieParam[]) {
|
|
|
|
await this._browser._client.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId || undefined });
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearCookies() {
|
|
|
|
await this._browser._client.send('Storage.clearCookies', { browserContextId: this._browserContextId || undefined });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setPermissions(origin: string, permissions: string[]): Promise<void> {
|
|
|
|
const webPermissionToProtocol = new Map<string, Protocol.Browser.PermissionType>([
|
|
|
|
['geolocation', 'geolocation'],
|
|
|
|
['midi', 'midi'],
|
|
|
|
['notifications', 'notifications'],
|
|
|
|
['camera', 'videoCapture'],
|
|
|
|
['microphone', 'audioCapture'],
|
|
|
|
['background-sync', 'backgroundSync'],
|
|
|
|
['ambient-light-sensor', 'sensors'],
|
|
|
|
['accelerometer', 'sensors'],
|
|
|
|
['gyroscope', 'sensors'],
|
|
|
|
['magnetometer', 'sensors'],
|
|
|
|
['accessibility-events', 'accessibilityEvents'],
|
|
|
|
['clipboard-read', 'clipboardReadWrite'],
|
|
|
|
['clipboard-write', 'clipboardSanitizedWrite'],
|
|
|
|
['payment-handler', 'paymentHandler'],
|
|
|
|
// chrome-specific permissions we have.
|
|
|
|
['midi-sysex', 'midiSysex'],
|
|
|
|
]);
|
|
|
|
const filtered = permissions.map(permission => {
|
|
|
|
const protocolPermission = webPermissionToProtocol.get(permission);
|
|
|
|
if (!protocolPermission)
|
|
|
|
throw new Error('Unknown permission: ' + permission);
|
|
|
|
return protocolPermission;
|
|
|
|
});
|
|
|
|
await this._browser._client.send('Browser.grantPermissions', { origin, browserContextId: this._browserContextId || undefined, permissions: filtered });
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions() {
|
|
|
|
await this._browser._client.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
|
|
|
if (geolocation)
|
|
|
|
geolocation = verifyGeolocation(geolocation);
|
|
|
|
this._options.geolocation = geolocation || undefined;
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage)._client.send('Emulation.setGeolocationOverride', geolocation || {});
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
if (this._closed)
|
|
|
|
return;
|
|
|
|
assert(this._browserContextId, 'Non-incognito profiles cannot be closed!');
|
|
|
|
await this._browser._client.send('Target.disposeBrowserContext', { browserContextId: this._browserContextId });
|
|
|
|
this._browser._contexts.delete(this._browserContextId);
|
|
|
|
this._closed = true;
|
|
|
|
this.emit(CommonEvents.BrowserContext.Close);
|
|
|
|
}
|
|
|
|
|
2020-02-24 14:35:51 -08:00
|
|
|
pageTarget(page: Page): CRTarget {
|
|
|
|
return CRTarget.fromPage(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
targets(): CRTarget[] {
|
|
|
|
return this._browser._allTargets().filter(t => t.context() === this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitForTarget(predicate: (arg0: CRTarget) => boolean, options: { timeout?: number; } = {}): Promise<CRTarget> {
|
|
|
|
const { timeout = 30000 } = options;
|
|
|
|
const existingTarget = this._browser._allTargets().find(predicate);
|
|
|
|
if (existingTarget)
|
|
|
|
return existingTarget;
|
|
|
|
let resolve: (target: CRTarget) => void;
|
|
|
|
const targetPromise = new Promise<CRTarget>(x => resolve = x);
|
|
|
|
this.on(Events.CRBrowserContext.TargetCreated, check);
|
|
|
|
this.on(Events.CRBrowserContext.TargetChanged, check);
|
|
|
|
try {
|
|
|
|
if (!timeout)
|
|
|
|
return await targetPromise;
|
|
|
|
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
|
|
|
|
} finally {
|
|
|
|
this.removeListener(Events.CRBrowserContext.TargetCreated, check);
|
|
|
|
this.removeListener(Events.CRBrowserContext.TargetChanged, check);
|
|
|
|
}
|
|
|
|
|
|
|
|
function check(target: CRTarget) {
|
|
|
|
if (predicate(target))
|
|
|
|
resolve(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
_browserClosed() {
|
|
|
|
this._closed = true;
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
page._didClose();
|
|
|
|
this.emit(CommonEvents.BrowserContext.Close);
|
|
|
|
}
|
|
|
|
}
|