2020-01-07 03:22:35 +01: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.
|
|
|
|
|
*/
|
2019-12-19 23:51:49 +01:00
|
|
|
|
2020-06-25 17:30:56 +02:00
|
|
|
import * as types from './types';
|
2020-09-05 07:37:38 +02:00
|
|
|
import { BrowserContext, Video } from './browserContext';
|
2020-02-05 21:41:55 +01:00
|
|
|
import { Page } from './page';
|
2020-04-01 23:42:47 +02:00
|
|
|
import { EventEmitter } from 'events';
|
2020-04-03 02:56:14 +02:00
|
|
|
import { Download } from './download';
|
2020-06-05 22:50:15 +02:00
|
|
|
import { ProxySettings } from './types';
|
2020-08-14 22:19:12 +02:00
|
|
|
import { ChildProcess } from 'child_process';
|
2020-12-08 18:35:28 +01:00
|
|
|
import { RecentLogsCollector } from '../utils/debugLogger';
|
2020-08-14 22:19:12 +02:00
|
|
|
|
|
|
|
|
export interface BrowserProcess {
|
|
|
|
|
onclose: ((exitCode: number | null, signal: string | null) => void) | undefined;
|
2020-11-07 01:31:11 +01:00
|
|
|
process?: ChildProcess;
|
2020-08-14 22:19:12 +02:00
|
|
|
kill(): Promise<void>;
|
|
|
|
|
close(): Promise<void>;
|
|
|
|
|
}
|
2020-05-14 22:22:33 +02:00
|
|
|
|
2020-08-19 04:13:40 +02:00
|
|
|
export type BrowserOptions = types.UIOptions & {
|
2020-07-09 06:36:03 +02:00
|
|
|
name: string,
|
2020-05-22 00:13:16 +02:00
|
|
|
downloadsPath?: string,
|
2020-05-14 22:22:33 +02:00
|
|
|
headful?: boolean,
|
2020-06-25 17:30:56 +02:00
|
|
|
persistent?: types.BrowserContextOptions, // Undefined means no persistent context.
|
2020-08-14 22:19:12 +02:00
|
|
|
browserProcess: BrowserProcess,
|
2020-06-05 22:50:15 +02:00
|
|
|
proxy?: ProxySettings,
|
2020-11-12 00:12:10 +01:00
|
|
|
protocolLogger: types.ProtocolLogger,
|
2020-12-08 18:35:28 +01:00
|
|
|
browserLogsCollector: RecentLogsCollector,
|
2020-05-14 22:22:33 +02:00
|
|
|
};
|
2019-12-19 23:51:49 +01:00
|
|
|
|
2020-08-19 19:31:59 +02:00
|
|
|
export abstract class Browser extends EventEmitter {
|
2020-08-22 01:26:33 +02:00
|
|
|
static Events = {
|
|
|
|
|
Disconnected: 'disconnected',
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-14 22:22:33 +02:00
|
|
|
readonly _options: BrowserOptions;
|
2020-04-03 02:56:14 +02:00
|
|
|
private _downloads = new Map<string, Download>();
|
2020-08-19 19:31:59 +02:00
|
|
|
_defaultContext: BrowserContext | null = null;
|
2020-06-30 01:26:32 +02:00
|
|
|
private _startedClosing = false;
|
2020-09-19 02:36:43 +02:00
|
|
|
readonly _idToVideo = new Map<string, Video>();
|
2020-04-20 16:52:26 +02:00
|
|
|
|
2020-05-14 22:22:33 +02:00
|
|
|
constructor(options: BrowserOptions) {
|
2020-04-20 16:52:26 +02:00
|
|
|
super();
|
2020-05-14 22:22:33 +02:00
|
|
|
this._options = options;
|
2020-04-20 16:52:26 +02:00
|
|
|
}
|
2020-04-03 02:56:14 +02:00
|
|
|
|
2020-08-18 18:37:40 +02:00
|
|
|
abstract newContext(options?: types.BrowserContextOptions): Promise<BrowserContext>;
|
2020-04-03 02:56:14 +02:00
|
|
|
abstract contexts(): BrowserContext[];
|
|
|
|
|
abstract isConnected(): boolean;
|
2020-07-27 22:41:35 +02:00
|
|
|
abstract version(): string;
|
2020-04-03 02:56:14 +02:00
|
|
|
|
2020-08-18 18:37:40 +02:00
|
|
|
async newPage(options?: types.BrowserContextOptions): Promise<Page> {
|
2020-04-03 02:56:14 +02:00
|
|
|
const context = await this.newContext(options);
|
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
page._ownedContext = context;
|
|
|
|
|
return page;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 04:23:08 +02:00
|
|
|
_downloadCreated(page: Page, uuid: string, url: string, suggestedFilename?: string) {
|
2020-05-22 00:13:16 +02:00
|
|
|
const download = new Download(page, this._options.downloadsPath || '', uuid, url, suggestedFilename);
|
2020-04-03 02:56:14 +02:00
|
|
|
this._downloads.set(uuid, download);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 04:23:08 +02:00
|
|
|
_downloadFilenameSuggested(uuid: string, suggestedFilename: string) {
|
|
|
|
|
const download = this._downloads.get(uuid);
|
|
|
|
|
if (!download)
|
|
|
|
|
return;
|
|
|
|
|
download._filenameSuggested(suggestedFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 00:01:42 +02:00
|
|
|
_downloadFinished(uuid: string, error?: string) {
|
2020-04-03 02:56:14 +02:00
|
|
|
const download = this._downloads.get(uuid);
|
|
|
|
|
if (!download)
|
|
|
|
|
return;
|
|
|
|
|
download._reportFinished(error);
|
|
|
|
|
this._downloads.delete(uuid);
|
|
|
|
|
}
|
2020-04-04 01:34:07 +02:00
|
|
|
|
2020-09-19 02:36:43 +02:00
|
|
|
_videoStarted(context: BrowserContext, videoId: string, path: string, pageOrError: Promise<Page | Error>) {
|
|
|
|
|
const video = new Video(context, videoId, path);
|
2020-09-05 07:37:38 +02:00
|
|
|
this._idToVideo.set(videoId, video);
|
2020-10-03 02:27:56 +02:00
|
|
|
context.emit(BrowserContext.Events.VideoStarted, video);
|
2020-09-09 02:01:00 +02:00
|
|
|
pageOrError.then(pageOrError => {
|
|
|
|
|
if (pageOrError instanceof Page)
|
2020-10-19 23:35:18 +02:00
|
|
|
pageOrError.videoStarted(video);
|
2020-09-09 02:01:00 +02:00
|
|
|
});
|
2020-08-26 02:18:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-05 07:37:38 +02:00
|
|
|
_videoFinished(videoId: string) {
|
2020-09-19 02:36:43 +02:00
|
|
|
const video = this._idToVideo.get(videoId)!;
|
2020-09-05 07:37:38 +02:00
|
|
|
this._idToVideo.delete(videoId);
|
2020-10-03 02:27:56 +02:00
|
|
|
video._finish();
|
2020-08-26 02:18:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-30 01:26:32 +02:00
|
|
|
_didClose() {
|
|
|
|
|
for (const context of this.contexts())
|
2020-08-19 19:31:59 +02:00
|
|
|
context._browserClosed();
|
2020-07-09 17:34:07 +02:00
|
|
|
if (this._defaultContext)
|
|
|
|
|
this._defaultContext._browserClosed();
|
2020-08-22 01:26:33 +02:00
|
|
|
this.emit(Browser.Events.Disconnected);
|
2020-06-30 01:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 01:34:07 +02:00
|
|
|
async close() {
|
2020-06-30 01:26:32 +02:00
|
|
|
if (!this._startedClosing) {
|
|
|
|
|
this._startedClosing = true;
|
2020-08-14 22:19:12 +02:00
|
|
|
await this._options.browserProcess.close();
|
2020-04-04 01:34:07 +02:00
|
|
|
}
|
|
|
|
|
if (this.isConnected())
|
2020-08-22 01:26:33 +02:00
|
|
|
await new Promise(x => this.once(Browser.Events.Disconnected, x));
|
2020-04-04 01:34:07 +02:00
|
|
|
}
|
2020-02-05 21:41:55 +01:00
|
|
|
}
|
|
|
|
|
|