2020-06-25 16:05:36 -07: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 * as frames from './frame';
|
2020-06-26 11:51:47 -07:00
|
|
|
import { Page, BindingCall, waitForEvent } from './page';
|
2020-06-25 16:05:36 -07:00
|
|
|
import * as types from '../../types';
|
|
|
|
import * as network from './network';
|
2020-06-26 12:28:27 -07:00
|
|
|
import { BrowserContextChannel, BrowserContextInitializer } from '../channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { helper } from '../../helper';
|
|
|
|
import { Browser } from './browser';
|
2020-07-01 13:55:29 -07:00
|
|
|
import { ConnectionScope } from './connection';
|
2020-06-26 12:28:27 -07:00
|
|
|
import { Events } from '../../events';
|
2020-06-26 21:22:03 -07:00
|
|
|
import { TimeoutSettings } from '../../timeoutSettings';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserContextInitializer> {
|
2020-06-25 16:05:36 -07:00
|
|
|
_pages = new Set<Page>();
|
|
|
|
private _routes: { url: types.URLMatch, handler: network.RouteHandler }[] = [];
|
|
|
|
_browser: Browser | undefined;
|
2020-06-26 11:51:47 -07:00
|
|
|
readonly _bindings = new Map<string, frames.FunctionWithSource>();
|
2020-06-26 21:22:03 -07:00
|
|
|
private _pendingWaitForEvents = new Map<(error: Error) => void, string>();
|
|
|
|
_timeoutSettings = new TimeoutSettings();
|
2020-06-29 16:37:38 -07:00
|
|
|
_ownerPage: Page | undefined;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
static from(context: BrowserContextChannel): BrowserContext {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (context as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromNullable(context: BrowserContextChannel | null): BrowserContext | null {
|
|
|
|
return context ? BrowserContext.from(context) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:55:29 -07:00
|
|
|
constructor(scope: ConnectionScope, guid: string, initializer: BrowserContextInitializer) {
|
|
|
|
super(scope, guid, initializer, true);
|
2020-06-26 17:24:21 -07:00
|
|
|
initializer.pages.map(p => {
|
|
|
|
const page = Page.from(p);
|
|
|
|
this._pages.add(page);
|
2020-06-26 21:22:03 -07:00
|
|
|
page._setBrowserContext(this);
|
2020-06-26 17:24:21 -07:00
|
|
|
});
|
2020-06-26 22:38:21 -07:00
|
|
|
this._channel.on('bindingCall', bindingCall => this._onBinding(BindingCall.from(bindingCall)));
|
2020-06-30 10:55:11 -07:00
|
|
|
this._channel.on('close', () => this._onClose());
|
2020-06-26 22:38:21 -07:00
|
|
|
this._channel.on('page', page => this._onPage(Page.from(page)));
|
2020-06-30 10:55:11 -07:00
|
|
|
this._channel.on('route', ({ route, request }) => this._onRoute(network.Route.from(route), network.Request.from(request)));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
private _onPage(page: Page): void {
|
2020-06-26 21:22:03 -07:00
|
|
|
page._setBrowserContext(this);
|
2020-06-26 12:28:27 -07:00
|
|
|
this._pages.add(page);
|
|
|
|
this.emit(Events.BrowserContext.Page, page);
|
|
|
|
}
|
2020-06-26 11:51:47 -07:00
|
|
|
|
|
|
|
_onRoute(route: network.Route, request: network.Request) {
|
|
|
|
for (const {url, handler} of this._routes) {
|
|
|
|
if (helper.urlMatches(request.url(), url)) {
|
|
|
|
handler(route, request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
route.continue();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _onBinding(bindingCall: BindingCall) {
|
2020-06-26 12:28:27 -07:00
|
|
|
const func = this._bindings.get(bindingCall._initializer.name);
|
2020-06-26 11:51:47 -07:00
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
bindingCall.call(func);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._channel.setDefaultNavigationTimeoutNoReply({ timeout });
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
2020-06-26 21:22:03 -07:00
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
2020-06-25 16:05:36 -07:00
|
|
|
this._channel.setDefaultTimeoutNoReply({ timeout });
|
|
|
|
}
|
|
|
|
|
|
|
|
pages(): Page[] {
|
|
|
|
return [...this._pages];
|
|
|
|
}
|
|
|
|
|
|
|
|
async newPage(): Promise<Page> {
|
2020-06-29 16:37:38 -07:00
|
|
|
if (this._ownerPage)
|
|
|
|
throw new Error('Please use browser.newContext()');
|
2020-06-25 16:05:36 -07:00
|
|
|
return Page.from(await this._channel.newPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
|
|
|
|
if (!urls)
|
|
|
|
urls = [];
|
|
|
|
if (urls && typeof urls === 'string')
|
|
|
|
urls = [ urls ];
|
|
|
|
return this._channel.cookies({ urls: urls as string[] });
|
|
|
|
}
|
|
|
|
|
|
|
|
async addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void> {
|
|
|
|
await this._channel.addCookies({ cookies });
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearCookies(): Promise<void> {
|
|
|
|
await this._channel.clearCookies();
|
|
|
|
}
|
|
|
|
|
|
|
|
async grantPermissions(permissions: string[], options?: { origin?: string }): Promise<void> {
|
|
|
|
await this._channel.grantPermissions({ permissions, options });
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions(): Promise<void> {
|
|
|
|
await this._channel.clearPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
|
|
|
await this._channel.setGeolocation({ geolocation });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setExtraHTTPHeaders(headers: types.Headers): Promise<void> {
|
|
|
|
await this._channel.setExtraHTTPHeaders({ headers });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setOffline(offline: boolean): Promise<void> {
|
|
|
|
await this._channel.setOffline({ offline });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void> {
|
|
|
|
await this._channel.setHTTPCredentials({ httpCredentials });
|
|
|
|
}
|
|
|
|
|
|
|
|
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any): Promise<void> {
|
|
|
|
const source = await helper.evaluationScript(script, arg);
|
|
|
|
await this._channel.addInitScript({ source });
|
|
|
|
}
|
|
|
|
|
2020-06-26 11:51:47 -07:00
|
|
|
async exposeBinding(name: string, binding: frames.FunctionWithSource): Promise<void> {
|
|
|
|
for (const page of this.pages()) {
|
|
|
|
if (page._bindings.has(name))
|
|
|
|
throw new Error(`Function "${name}" has been already registered in one of the pages`);
|
|
|
|
}
|
|
|
|
if (this._bindings.has(name))
|
|
|
|
throw new Error(`Function "${name}" has been already registered`);
|
|
|
|
this._bindings.set(name, binding);
|
|
|
|
await this._channel.exposeBinding({ name });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async exposeFunction(name: string, playwrightFunction: Function): Promise<void> {
|
|
|
|
await this.exposeBinding(name, (source, ...args) => playwrightFunction(...args));
|
|
|
|
}
|
|
|
|
|
|
|
|
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
|
|
|
|
this._routes.push({ url, handler });
|
|
|
|
if (this._routes.length === 1)
|
|
|
|
await this._channel.setNetworkInterceptionEnabled({ enabled: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
async unroute(url: types.URLMatch, handler?: network.RouteHandler): Promise<void> {
|
|
|
|
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
|
|
|
|
if (this._routes.length === 0)
|
|
|
|
await this._channel.setNetworkInterceptionEnabled({ enabled: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitForEvent(event: string, optionsOrPredicate?: Function | (types.TimeoutOptions & { predicate?: Function })): Promise<any> {
|
2020-06-26 21:22:03 -07:00
|
|
|
const hasTimeout = optionsOrPredicate && !(optionsOrPredicate instanceof Function);
|
|
|
|
let reject: () => void;
|
|
|
|
const result = await Promise.race([
|
|
|
|
waitForEvent(this, event, optionsOrPredicate, this._timeoutSettings.timeout(hasTimeout ? optionsOrPredicate as any : {})),
|
|
|
|
new Promise((f, r) => { reject = r; this._pendingWaitForEvents.set(reject, event); })
|
|
|
|
]);
|
|
|
|
this._pendingWaitForEvents.delete(reject!);
|
|
|
|
return result;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-30 10:55:11 -07:00
|
|
|
private async _onClose() {
|
2020-06-26 17:24:21 -07:00
|
|
|
if (this._browser)
|
|
|
|
this._browser._contexts.delete(this);
|
2020-06-26 21:22:03 -07:00
|
|
|
|
|
|
|
for (const [listener, event] of this._pendingWaitForEvents) {
|
2020-07-03 18:04:08 -07:00
|
|
|
if (event === Events.BrowserContext.Close)
|
2020-06-26 21:22:03 -07:00
|
|
|
continue;
|
|
|
|
listener(new Error('Context closed'));
|
|
|
|
}
|
|
|
|
this._pendingWaitForEvents.clear();
|
2020-06-26 12:28:27 -07:00
|
|
|
this.emit(Events.BrowserContext.Close);
|
2020-07-01 13:55:29 -07:00
|
|
|
this._scope.dispose();
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-06-30 10:55:11 -07:00
|
|
|
|
|
|
|
async close(): Promise<void> {
|
|
|
|
await this._channel.close();
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|