2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2020-02-05 12:41:55 -08:00
|
|
|
import { Browser, collectPages } from '../browser';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { BrowserContext, BrowserContextOptions } from '../browserContext';
|
2019-12-20 13:07:14 -08:00
|
|
|
import { Events } from '../events';
|
|
|
|
import { assert, helper, RegisteredListener } from '../helper';
|
|
|
|
import * as network from '../network';
|
2020-01-03 10:14:50 -08:00
|
|
|
import * as types from '../types';
|
2019-12-20 13:07:14 -08:00
|
|
|
import { Page } from '../page';
|
|
|
|
import { ConnectionEvents, FFConnection, FFSessionEvents } from './ffConnection';
|
2019-12-23 11:39:57 -08:00
|
|
|
import { FFPage } from './ffPage';
|
2020-01-07 16:13:49 -08:00
|
|
|
import * as platform from '../platform';
|
2020-01-13 22:08:35 -08:00
|
|
|
import { Protocol } from './protocol';
|
2020-02-04 19:41:38 -08:00
|
|
|
import { ConnectionTransport, SlowMoTransport } from '../transport';
|
2020-01-07 16:13:49 -08:00
|
|
|
|
2020-01-08 14:04:33 -08:00
|
|
|
export class FFBrowser extends platform.EventEmitter implements Browser {
|
2019-12-19 16:53:24 -08:00
|
|
|
_connection: FFConnection;
|
|
|
|
_targets: Map<string, Target>;
|
2020-02-05 12:41:55 -08:00
|
|
|
readonly _defaultContext: BrowserContext;
|
2019-12-19 16:53:24 -08:00
|
|
|
private _contexts: Map<string, BrowserContext>;
|
|
|
|
private _eventListeners: RegisteredListener[];
|
|
|
|
|
2020-02-04 19:41:38 -08:00
|
|
|
static async connect(transport: ConnectionTransport, slowMo?: number): Promise<FFBrowser> {
|
|
|
|
const connection = new FFConnection(SlowMoTransport.wrap(transport, slowMo));
|
2020-02-06 12:41:43 -08:00
|
|
|
const browser = new FFBrowser(connection);
|
2019-12-19 16:53:24 -08:00
|
|
|
await connection.send('Target.enable');
|
2020-01-07 16:13:49 -08:00
|
|
|
await browser._waitForTarget(t => t.type() === 'page');
|
2019-12-19 16:53:24 -08:00
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:41:43 -08:00
|
|
|
constructor(connection: FFConnection) {
|
2019-12-19 16:53:24 -08:00
|
|
|
super();
|
|
|
|
this._connection = connection;
|
|
|
|
this._targets = new Map();
|
|
|
|
|
|
|
|
this._defaultContext = this._createBrowserContext(null, {});
|
|
|
|
this._contexts = new Map();
|
2020-02-11 10:27:19 -08:00
|
|
|
this._connection.on(ConnectionEvents.Disconnected, () => {
|
|
|
|
for (const context of this.contexts())
|
|
|
|
context._browserClosed();
|
|
|
|
this.emit(Events.Browser.Disconnected);
|
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
this._eventListeners = [
|
|
|
|
helper.addEventListener(this._connection, 'Target.targetCreated', this._onTargetCreated.bind(this)),
|
|
|
|
helper.addEventListener(this._connection, 'Target.targetDestroyed', this._onTargetDestroyed.bind(this)),
|
|
|
|
helper.addEventListener(this._connection, 'Target.targetInfoChanged', this._onTargetInfoChanged.bind(this)),
|
2020-02-06 19:01:03 -08:00
|
|
|
helper.addEventListener(this._connection, 'Target.attachedToTarget', this._onAttachedToTarget.bind(this)),
|
2019-12-19 16:53:24 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
return !this._connection._closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2020-02-06 19:01:03 -08:00
|
|
|
const {browserContextId} = await this._connection.send('Target.createBrowserContext', {
|
|
|
|
userAgent: options.userAgent
|
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
// TODO: move ignoreHTTPSErrors to browser context level.
|
|
|
|
if (options.ignoreHTTPSErrors)
|
|
|
|
await this._connection.send('Browser.setIgnoreHTTPSErrors', { enabled: true });
|
|
|
|
const context = this._createBrowserContext(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-05 12:41:55 -08:00
|
|
|
async pages(): Promise<Page[]> {
|
|
|
|
return collectPages(this);
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
async newPage(options?: BrowserContextOptions): Promise<Page> {
|
|
|
|
const context = await this.newContext(options);
|
|
|
|
return context.newPage();
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async _waitForTarget(predicate: (target: Target) => boolean, options: { timeout?: number; } = {}): Promise<Target> {
|
|
|
|
const {
|
|
|
|
timeout = 30000
|
|
|
|
} = options;
|
|
|
|
const existingTarget = this._allTargets().find(predicate);
|
|
|
|
if (existingTarget)
|
|
|
|
return existingTarget;
|
|
|
|
let resolve: (t: Target) => void;
|
|
|
|
const targetPromise = new Promise<Target>(x => resolve = x);
|
|
|
|
this.on('targetchanged', check);
|
|
|
|
try {
|
|
|
|
if (!timeout)
|
|
|
|
return await targetPromise;
|
|
|
|
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
|
|
|
|
} finally {
|
|
|
|
this.removeListener('targetchanged', check);
|
|
|
|
}
|
|
|
|
|
|
|
|
function check(target: Target) {
|
|
|
|
if (predicate(target))
|
|
|
|
resolve(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_allTargets() {
|
|
|
|
return Array.from(this._targets.values());
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
async _onTargetCreated(payload: Protocol.Target.targetCreatedPayload) {
|
|
|
|
const {targetId, url, browserContextId, openerId, type} = payload;
|
|
|
|
const context = browserContextId ? this._contexts.get(browserContextId)! : this._defaultContext;
|
2019-12-19 16:53:24 -08:00
|
|
|
const target = new Target(this._connection, this, context, targetId, type, url, openerId);
|
|
|
|
this._targets.set(targetId, target);
|
2020-01-13 22:08:35 -08:00
|
|
|
const opener = target.opener();
|
|
|
|
if (opener && opener._pagePromise) {
|
|
|
|
const openerPage = await opener._pagePromise;
|
2019-12-19 16:53:24 -08:00
|
|
|
if (openerPage.listenerCount(Events.Page.Popup)) {
|
|
|
|
const popupPage = await target.page();
|
|
|
|
openerPage.emit(Events.Page.Popup, popupPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onTargetDestroyed(payload: Protocol.Target.targetDestroyedPayload) {
|
|
|
|
const {targetId} = payload;
|
|
|
|
const target = this._targets.get(targetId)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._targets.delete(targetId);
|
|
|
|
target._didClose();
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onTargetInfoChanged(payload: Protocol.Target.targetInfoChangedPayload) {
|
|
|
|
const {targetId, url} = payload;
|
|
|
|
const target = this._targets.get(targetId)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
target._url = url;
|
|
|
|
}
|
|
|
|
|
2020-02-06 19:01:03 -08:00
|
|
|
_onAttachedToTarget(payload: Protocol.Target.attachedToTargetPayload) {
|
|
|
|
const {targetId, type} = payload.targetInfo;
|
|
|
|
const target = this._targets.get(targetId)!;
|
|
|
|
if (type === 'page')
|
|
|
|
target.page();
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async close() {
|
2020-02-10 10:41:45 -08:00
|
|
|
await Promise.all(this.contexts().map(context => context.close()));
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2020-02-06 12:41:43 -08:00
|
|
|
const disconnected = new Promise(f => this.once(Events.Browser.Disconnected, f));
|
|
|
|
this._connection.close();
|
2020-01-08 13:55:38 -08:00
|
|
|
await disconnected;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_createBrowserContext(browserContextId: string | null, options: BrowserContextOptions): BrowserContext {
|
2020-01-13 17:16:05 -08:00
|
|
|
BrowserContext.validateOptions(options);
|
2019-12-19 16:53:24 -08:00
|
|
|
const context = new BrowserContext({
|
|
|
|
pages: async (): Promise<Page[]> => {
|
2020-02-10 10:41:45 -08:00
|
|
|
const targets = this._allTargets().filter(target => target.context() === context && target.type() === 'page');
|
2019-12-19 16:53:24 -08:00
|
|
|
const pages = await Promise.all(targets.map(target => target.page()));
|
|
|
|
return pages.filter(page => !!page);
|
|
|
|
},
|
|
|
|
|
2020-01-27 11:43:43 -08:00
|
|
|
existingPages: (): Page[] => {
|
|
|
|
const pages: Page[] = [];
|
|
|
|
for (const target of this._allTargets()) {
|
2020-02-10 10:41:45 -08:00
|
|
|
if (target.context() === context && target._ffPage)
|
2020-01-27 11:43:43 -08:00
|
|
|
pages.push(target._ffPage._page);
|
|
|
|
}
|
|
|
|
return pages;
|
|
|
|
},
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
newPage: async (): Promise<Page> => {
|
|
|
|
const {targetId} = await this._connection.send('Target.newPage', {
|
|
|
|
browserContextId: browserContextId || undefined
|
|
|
|
});
|
2020-01-13 22:08:35 -08:00
|
|
|
const target = this._targets.get(targetId)!;
|
2020-01-02 15:06:28 -08:00
|
|
|
return target.page();
|
2019-12-19 16:53:24 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
close: async (): Promise<void> => {
|
|
|
|
assert(browserContextId, 'Non-incognito profiles cannot be closed!');
|
2020-02-05 16:53:36 -08:00
|
|
|
await this._connection.send('Target.removeBrowserContext', { browserContextId });
|
|
|
|
this._contexts.delete(browserContextId);
|
2019-12-19 16:53:24 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
cookies: async (): Promise<network.NetworkCookie[]> => {
|
|
|
|
const { cookies } = await this._connection.send('Browser.getCookies', { browserContextId: browserContextId || undefined });
|
|
|
|
return cookies.map(c => {
|
|
|
|
const copy: any = { ... c };
|
|
|
|
delete copy.size;
|
|
|
|
return copy as network.NetworkCookie;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearCookies: async (): Promise<void> => {
|
|
|
|
await this._connection.send('Browser.clearCookies', { browserContextId: browserContextId || undefined });
|
|
|
|
},
|
|
|
|
|
|
|
|
setCookies: async (cookies: network.SetNetworkCookieParam[]): Promise<void> => {
|
|
|
|
await this._connection.send('Browser.setCookies', { browserContextId: browserContextId || undefined, cookies });
|
|
|
|
},
|
2019-12-20 13:07:14 -08:00
|
|
|
|
|
|
|
setPermissions: async (origin: string, permissions: string[]): Promise<void> => {
|
2020-01-13 22:08:35 -08:00
|
|
|
const webPermissionToProtocol = new Map<string, 'geo' | 'microphone' | 'camera' | 'desktop-notifications'>([
|
2019-12-20 13:07:14 -08:00
|
|
|
['geolocation', 'geo'],
|
|
|
|
['microphone', 'microphone'],
|
|
|
|
['camera', 'camera'],
|
|
|
|
['notifications', 'desktop-notifications'],
|
|
|
|
]);
|
|
|
|
const filtered = permissions.map(permission => {
|
|
|
|
const protocolPermission = webPermissionToProtocol.get(permission);
|
|
|
|
if (!protocolPermission)
|
|
|
|
throw new Error('Unknown permission: ' + permission);
|
|
|
|
return protocolPermission;
|
|
|
|
});
|
|
|
|
await this._connection.send('Browser.grantPermissions', {origin, browserContextId: browserContextId || undefined, permissions: filtered});
|
|
|
|
},
|
2019-12-20 15:32:30 -08:00
|
|
|
|
2019-12-20 13:07:14 -08:00
|
|
|
clearPermissions: async () => {
|
|
|
|
await this._connection.send('Browser.resetPermissions', { browserContextId: browserContextId || undefined });
|
2020-01-03 10:14:50 -08:00
|
|
|
},
|
2019-12-20 15:32:30 -08:00
|
|
|
|
2020-01-03 10:14:50 -08:00
|
|
|
setGeolocation: async (geolocation: types.Geolocation | null): Promise<void> => {
|
|
|
|
throw new Error('Geolocation emulation is not supported in Firefox');
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}, options);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:49:22 -08:00
|
|
|
class Target {
|
2019-12-19 16:53:24 -08:00
|
|
|
_pagePromise?: Promise<Page>;
|
2019-12-23 12:10:07 -08:00
|
|
|
_ffPage: FFPage | null = null;
|
|
|
|
private readonly _browser: FFBrowser;
|
|
|
|
private readonly _context: BrowserContext;
|
|
|
|
private readonly _connection: FFConnection;
|
|
|
|
private readonly _targetId: string;
|
|
|
|
private readonly _type: 'page' | 'browser';
|
2019-12-19 16:53:24 -08:00
|
|
|
_url: string;
|
2020-01-13 22:08:35 -08:00
|
|
|
private readonly _openerId: string | undefined;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
|
|
|
constructor(connection: any, browser: FFBrowser, context: BrowserContext, targetId: string, type: 'page' | 'browser', url: string, openerId: string | undefined) {
|
|
|
|
this._browser = browser;
|
|
|
|
this._context = context;
|
|
|
|
this._connection = connection;
|
|
|
|
this._targetId = targetId;
|
|
|
|
this._type = type;
|
|
|
|
this._url = url;
|
|
|
|
this._openerId = openerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
_didClose() {
|
2019-12-23 11:39:57 -08:00
|
|
|
if (this._ffPage)
|
|
|
|
this._ffPage.didClose();
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
opener(): Target | null {
|
2020-01-13 22:08:35 -08:00
|
|
|
return this._openerId ? this._browser._targets.get(this._openerId)! : null;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type(): 'page' | 'browser' {
|
|
|
|
return this._type;
|
|
|
|
}
|
|
|
|
|
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
context(): BrowserContext {
|
2019-12-19 16:53:24 -08:00
|
|
|
return this._context;
|
|
|
|
}
|
|
|
|
|
|
|
|
page(): Promise<Page> {
|
2020-01-13 22:08:35 -08:00
|
|
|
if (this._type !== 'page')
|
|
|
|
throw new Error(`Cannot create page for "${this._type}" target`);
|
|
|
|
if (!this._pagePromise) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._pagePromise = new Promise(async f => {
|
|
|
|
const session = await this._connection.createSession(this._targetId);
|
2020-01-31 18:38:45 -08:00
|
|
|
this._ffPage = new FFPage(session, this._context, async () => {
|
|
|
|
const openerTarget = this.opener();
|
|
|
|
if (!openerTarget)
|
|
|
|
return null;
|
|
|
|
return await openerTarget.page();
|
|
|
|
});
|
2019-12-23 11:39:57 -08:00
|
|
|
const page = this._ffPage._page;
|
2019-12-19 16:53:24 -08:00
|
|
|
session.once(FFSessionEvents.Disconnected, () => page._didDisconnect());
|
2019-12-23 11:39:57 -08:00
|
|
|
await this._ffPage._initialize();
|
2019-12-19 16:53:24 -08:00
|
|
|
f(page);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
browser() {
|
|
|
|
return this._browser;
|
|
|
|
}
|
|
|
|
}
|