2020-06-26 01:05:36 +02:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-25 02:05:16 +02:00
|
|
|
import * as channels from '../protocol/channels';
|
2020-06-26 01:05:36 +02:00
|
|
|
import { BrowserContext } from './browserContext';
|
|
|
|
|
import { Page } from './page';
|
|
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-07-30 02:26:59 +02:00
|
|
|
import { Events } from './events';
|
|
|
|
|
import { BrowserContextOptions } from './types';
|
2020-08-19 00:38:29 +02:00
|
|
|
import { validateHeaders } from './network';
|
2020-08-23 00:13:51 +02:00
|
|
|
import { headersObjectToArray } from '../utils/utils';
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2020-08-25 02:05:16 +02:00
|
|
|
export class Browser extends ChannelOwner<channels.BrowserChannel, channels.BrowserInitializer> {
|
2020-06-26 01:05:36 +02:00
|
|
|
readonly _contexts = new Set<BrowserContext>();
|
|
|
|
|
private _isConnected = true;
|
2020-07-01 07:21:17 +02:00
|
|
|
private _isClosedOrClosing = false;
|
2020-07-10 00:33:01 +02:00
|
|
|
private _closedPromise: Promise<void>;
|
2020-08-26 21:46:30 +02:00
|
|
|
_isRemote = false;
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2020-08-25 02:05:16 +02:00
|
|
|
static from(browser: channels.BrowserChannel): Browser {
|
2020-07-02 03:36:09 +02:00
|
|
|
return (browser as any)._object;
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-25 02:05:16 +02:00
|
|
|
static fromNullable(browser: channels.BrowserChannel | null): Browser | null {
|
2020-06-26 01:05:36 +02:00
|
|
|
return browser ? Browser.from(browser) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 02:05:16 +02:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserInitializer) {
|
2020-07-27 19:21:39 +02:00
|
|
|
super(parent, type, guid, initializer);
|
2020-08-13 22:24:49 +02:00
|
|
|
this._channel.on('close', () => this._didClose());
|
2020-07-10 00:33:01 +02:00
|
|
|
this._closedPromise = new Promise(f => this.once(Events.Browser.Disconnected, f));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-30 02:26:59 +02:00
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2020-07-11 03:00:10 +02:00
|
|
|
const logger = options.logger;
|
2020-07-16 23:32:21 +02:00
|
|
|
return this._wrapApiCall('browser.newContext', async () => {
|
2020-08-19 00:38:29 +02:00
|
|
|
if (options.extraHTTPHeaders)
|
|
|
|
|
validateHeaders(options.extraHTTPHeaders);
|
2020-08-25 02:05:16 +02:00
|
|
|
const contextOptions: channels.BrowserNewContextParams = {
|
2020-07-16 23:32:21 +02:00
|
|
|
...options,
|
2020-07-21 17:26:48 +02:00
|
|
|
viewport: options.viewport === null ? undefined : options.viewport,
|
|
|
|
|
noDefaultViewport: options.viewport === null,
|
2020-07-16 23:32:21 +02:00
|
|
|
extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,
|
|
|
|
|
};
|
|
|
|
|
const context = BrowserContext.from((await this._channel.newContext(contextOptions)).context);
|
|
|
|
|
this._contexts.add(context);
|
|
|
|
|
context._logger = logger || this._logger;
|
|
|
|
|
return context;
|
|
|
|
|
});
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contexts(): BrowserContext[] {
|
|
|
|
|
return [...this._contexts];
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 22:41:35 +02:00
|
|
|
version(): string {
|
|
|
|
|
return this._initializer.version;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 02:26:59 +02:00
|
|
|
async newPage(options: BrowserContextOptions = {}): Promise<Page> {
|
2020-06-27 02:24:21 +02:00
|
|
|
const context = await this.newContext(options);
|
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
page._ownedContext = context;
|
2020-06-30 01:37:38 +02:00
|
|
|
context._ownerPage = page;
|
2020-06-27 02:24:21 +02:00
|
|
|
return page;
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
|
return this._isConnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
2020-07-16 23:32:21 +02:00
|
|
|
return this._wrapApiCall('browser.close', async () => {
|
|
|
|
|
if (!this._isClosedOrClosing) {
|
|
|
|
|
this._isClosedOrClosing = true;
|
|
|
|
|
await this._channel.close();
|
|
|
|
|
}
|
|
|
|
|
await this._closedPromise;
|
|
|
|
|
});
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
2020-08-13 22:24:49 +02:00
|
|
|
|
|
|
|
|
_didClose() {
|
|
|
|
|
this._isConnected = false;
|
|
|
|
|
this.emit(Events.Browser.Disconnected);
|
|
|
|
|
this._isClosedOrClosing = true;
|
|
|
|
|
}
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|