2019-12-11 02:13:56 +03: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 { Page } from './page';
|
|
|
|
import * as network from './network';
|
2019-12-18 23:23:33 +03:00
|
|
|
import * as types from './types';
|
2020-01-14 02:39:13 +03:00
|
|
|
import { helper } from './helper';
|
2020-02-11 21:27:19 +03:00
|
|
|
import * as platform from './platform';
|
|
|
|
import { Events } from './events';
|
2020-02-14 01:18:18 +03:00
|
|
|
import { TimeoutSettings } from './timeoutSettings';
|
2019-12-11 18:18:43 +03:00
|
|
|
|
2019-12-19 03:44:02 +03:00
|
|
|
export interface BrowserContextDelegate {
|
|
|
|
pages(): Promise<Page[]>;
|
2020-01-27 22:43:43 +03:00
|
|
|
existingPages(): Page[];
|
2019-12-19 03:44:02 +03:00
|
|
|
newPage(): Promise<Page>;
|
|
|
|
close(): Promise<void>;
|
2019-12-21 00:07:14 +03:00
|
|
|
|
2019-12-19 03:44:02 +03:00
|
|
|
cookies(): Promise<network.NetworkCookie[]>;
|
|
|
|
setCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
|
2019-12-21 00:07:14 +03:00
|
|
|
clearCookies(): Promise<void>;
|
|
|
|
|
|
|
|
setPermissions(origin: string, permissions: string[]): Promise<void>;
|
|
|
|
clearPermissions(): Promise<void>;
|
2020-01-03 21:14:50 +03:00
|
|
|
|
|
|
|
setGeolocation(geolocation: types.Geolocation | null): Promise<void>;
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
2019-12-18 23:23:33 +03:00
|
|
|
export type BrowserContextOptions = {
|
|
|
|
viewport?: types.Viewport | null,
|
|
|
|
ignoreHTTPSErrors?: boolean,
|
|
|
|
javaScriptEnabled?: boolean,
|
|
|
|
bypassCSP?: boolean,
|
|
|
|
userAgent?: string,
|
2020-02-14 00:37:59 +03:00
|
|
|
locale?: string,
|
2020-01-03 21:14:50 +03:00
|
|
|
timezoneId?: string,
|
2020-01-14 00:32:44 +03:00
|
|
|
geolocation?: types.Geolocation,
|
|
|
|
permissions?: { [key: string]: string[] };
|
2019-12-18 23:23:33 +03:00
|
|
|
};
|
|
|
|
|
2020-02-11 21:27:19 +03:00
|
|
|
export class BrowserContext extends platform.EventEmitter {
|
2019-12-19 03:44:02 +03:00
|
|
|
private readonly _delegate: BrowserContextDelegate;
|
2019-12-18 23:23:33 +03:00
|
|
|
readonly _options: BrowserContextOptions;
|
2020-02-14 01:18:18 +03:00
|
|
|
readonly _timeoutSettings: TimeoutSettings;
|
2019-12-19 03:23:05 +03:00
|
|
|
private _closed = false;
|
2019-12-11 02:13:56 +03:00
|
|
|
|
2019-12-19 03:44:02 +03:00
|
|
|
constructor(delegate: BrowserContextDelegate, options: BrowserContextOptions) {
|
2020-02-11 21:27:19 +03:00
|
|
|
super();
|
2019-12-11 02:13:56 +03:00
|
|
|
this._delegate = delegate;
|
2020-02-14 01:18:18 +03:00
|
|
|
this._timeoutSettings = new TimeoutSettings();
|
2020-01-04 01:42:18 +03:00
|
|
|
this._options = { ...options };
|
|
|
|
if (!this._options.viewport && this._options.viewport !== null)
|
|
|
|
this._options.viewport = { width: 800, height: 600 };
|
|
|
|
if (this._options.viewport)
|
|
|
|
this._options.viewport = { ...this._options.viewport };
|
|
|
|
if (this._options.geolocation)
|
2020-01-14 02:39:13 +03:00
|
|
|
this._options.geolocation = verifyGeolocation(this._options.geolocation);
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 00:32:44 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:43:43 +03:00
|
|
|
_existingPages(): Page[] {
|
|
|
|
return this._delegate.existingPages();
|
|
|
|
}
|
|
|
|
|
2020-02-14 01:18:18 +03:00
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2019-12-11 18:18:43 +03:00
|
|
|
async pages(): Promise<Page[]> {
|
2019-12-19 03:44:02 +03:00
|
|
|
return this._delegate.pages();
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 21:41:45 +03:00
|
|
|
async newPage(): Promise<Page> {
|
2020-02-11 23:06:58 +03:00
|
|
|
const pages = this._delegate.existingPages();
|
|
|
|
for (const page of pages) {
|
|
|
|
if (page._ownedContext)
|
|
|
|
throw new Error('Please use browser.newContext() for multi-page scripts that share the context.');
|
|
|
|
}
|
2020-02-10 21:41:45 +03:00
|
|
|
return this._delegate.newPage();
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async cookies(...urls: string[]): Promise<network.NetworkCookie[]> {
|
2019-12-19 03:44:02 +03:00
|
|
|
return network.filterCookies(await this._delegate.cookies(), urls);
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:07:14 +03:00
|
|
|
async setCookies(cookies: network.SetNetworkCookieParam[]) {
|
|
|
|
await this._delegate.setCookies(network.rewriteCookies(cookies));
|
|
|
|
}
|
|
|
|
|
2019-12-11 02:13:56 +03:00
|
|
|
async clearCookies() {
|
2019-12-19 03:44:02 +03:00
|
|
|
await this._delegate.clearCookies();
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:07:14 +03:00
|
|
|
async setPermissions(origin: string, permissions: string[]): Promise<void> {
|
|
|
|
await this._delegate.setPermissions(origin, permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions() {
|
|
|
|
await this._delegate.clearPermissions();
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-01-03 21:14:50 +03:00
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
2020-01-14 02:39:13 +03:00
|
|
|
if (geolocation)
|
|
|
|
geolocation = verifyGeolocation(geolocation);
|
2020-01-14 00:33:25 +03:00
|
|
|
this._options.geolocation = geolocation || undefined;
|
2020-01-03 21:14:50 +03:00
|
|
|
await this._delegate.setGeolocation(geolocation);
|
|
|
|
}
|
|
|
|
|
2019-12-11 02:13:56 +03:00
|
|
|
async close() {
|
2019-12-19 03:23:05 +03:00
|
|
|
if (this._closed)
|
|
|
|
return;
|
2019-12-19 03:44:02 +03:00
|
|
|
await this._delegate.close();
|
2019-12-19 03:23:05 +03:00
|
|
|
this._closed = true;
|
2020-02-11 21:27:19 +03:00
|
|
|
this.emit(Events.BrowserContext.Close);
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
2020-01-14 04:16:05 +03:00
|
|
|
|
|
|
|
static validateOptions(options: BrowserContextOptions) {
|
|
|
|
if (options.geolocation)
|
|
|
|
verifyGeolocation(options.geolocation);
|
|
|
|
}
|
2020-02-11 21:27:19 +03:00
|
|
|
|
|
|
|
_browserClosed() {
|
|
|
|
this._closed = true;
|
2020-02-11 22:13:08 +03:00
|
|
|
for (const page of this._delegate.existingPages())
|
|
|
|
page._didClose();
|
2020-02-11 21:27:19 +03:00
|
|
|
this.emit(Events.BrowserContext.Close);
|
|
|
|
}
|
2019-12-11 02:13:56 +03:00
|
|
|
}
|
2020-01-14 02:39:13 +03:00
|
|
|
|
|
|
|
function verifyGeolocation(geolocation: types.Geolocation): types.Geolocation {
|
|
|
|
const result = { ...geolocation };
|
|
|
|
result.accuracy = result.accuracy || 0;
|
|
|
|
const { longitude, latitude, accuracy } = result;
|
|
|
|
if (!helper.isNumber(longitude) || longitude < -180 || longitude > 180)
|
|
|
|
throw new Error(`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`);
|
|
|
|
if (!helper.isNumber(latitude) || latitude < -90 || latitude > 90)
|
|
|
|
throw new Error(`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`);
|
|
|
|
if (!helper.isNumber(accuracy) || accuracy < 0)
|
|
|
|
throw new Error(`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`);
|
|
|
|
return result;
|
|
|
|
}
|