Skip to content

Commit

Permalink
chore: merge Browser{Context,}Base into Browser{Context,} (#3524)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Aug 19, 2020
1 parent 56da4bb commit 745dc33
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 82 deletions.
17 changes: 4 additions & 13 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as types from './types';
import { BrowserContext, BrowserContextBase } from './browserContext';
import { BrowserContext } from './browserContext';
import { Page } from './page';
import { EventEmitter } from 'events';
import { Download } from './download';
Expand All @@ -39,19 +39,10 @@ export type BrowserOptions = types.UIOptions & {
proxy?: ProxySettings,
};

export interface Browser extends EventEmitter {
newContext(options?: types.BrowserContextOptions): Promise<BrowserContext>;
contexts(): BrowserContext[];
newPage(options?: types.BrowserContextOptions): Promise<Page>;
isConnected(): boolean;
close(): Promise<void>;
version(): string;
}

export abstract class BrowserBase extends EventEmitter implements Browser {
export abstract class Browser extends EventEmitter {
readonly _options: BrowserOptions;
private _downloads = new Map<string, Download>();
_defaultContext: BrowserContextBase | null = null;
_defaultContext: BrowserContext | null = null;
private _startedClosing = false;

constructor(options: BrowserOptions) {
Expand Down Expand Up @@ -93,7 +84,7 @@ export abstract class BrowserBase extends EventEmitter implements Browser {

_didClose() {
for (const context of this.contexts())
(context as BrowserContextBase)._browserClosed();
context._browserClosed();
if (this._defaultContext)
this._defaultContext._browserClosed();
this.emit(Events.Browser.Disconnected);
Expand Down
38 changes: 9 additions & 29 deletions src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,12 @@ import * as frames from './frames';
import * as types from './types';
import { Events } from './events';
import { Download } from './download';
import { BrowserBase } from './browser';
import { Browser } from './browser';
import { EventEmitter } from 'events';
import { Progress } from './progress';
import { DebugController } from './debug/debugController';

export interface BrowserContext extends EventEmitter {
setDefaultNavigationTimeout(timeout: number): void;
setDefaultTimeout(timeout: number): void;
pages(): Page[];
newPage(): Promise<Page>;
cookies(urls?: string | string[]): Promise<types.NetworkCookie[]>;
addCookies(cookies: types.SetNetworkCookieParam[]): Promise<void>;
clearCookies(): Promise<void>;
grantPermissions(permissions: string[], options?: { origin?: string }): Promise<void>;
clearPermissions(): Promise<void>;
setGeolocation(geolocation?: types.Geolocation): Promise<void>;
setExtraHTTPHeaders(headers: types.HeadersArray): Promise<void>;
setOffline(offline: boolean): Promise<void>;
setHTTPCredentials(httpCredentials?: types.Credentials): Promise<void>;
addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any): Promise<void>;
exposeBinding(name: string, playwrightBinding: frames.FunctionWithSource): Promise<void>;
_setRequestInterceptor(handler: network.RouteHandler | undefined): Promise<void>;
close(): Promise<void>;
}

export abstract class BrowserContextBase extends EventEmitter implements BrowserContext {
export abstract class BrowserContext extends EventEmitter {
readonly _timeoutSettings = new TimeoutSettings();
readonly _pageBindings = new Map<string, PageBinding>();
readonly _options: types.BrowserContextOptions;
Expand All @@ -59,11 +39,11 @@ export abstract class BrowserContextBase extends EventEmitter implements Browser
private _closePromiseFulfill: ((error: Error) => void) | undefined;
readonly _permissions = new Map<string, string[]>();
readonly _downloads = new Set<Download>();
readonly _browserBase: BrowserBase;
readonly _browser: Browser;

constructor(browserBase: BrowserBase, options: types.BrowserContextOptions, isPersistentContext: boolean) {
constructor(browser: Browser, options: types.BrowserContextOptions, isPersistentContext: boolean) {
super();
this._browserBase = browserBase;
this._browser = browser;
this._options = options;
this._isPersistentContext = isPersistentContext;
this._closePromise = new Promise(fulfill => this._closePromiseFulfill = fulfill);
Expand Down Expand Up @@ -183,7 +163,7 @@ export abstract class BrowserContextBase extends EventEmitter implements Browser
}

protected _authenticateProxyViaHeader() {
const proxy = this._browserBase._options.proxy || { username: undefined, password: undefined };
const proxy = this._browser._options.proxy || { username: undefined, password: undefined };
const { username, password } = proxy;
if (username) {
this._options.httpCredentials = { username, password: password! };
Expand All @@ -196,7 +176,7 @@ export abstract class BrowserContextBase extends EventEmitter implements Browser
}

protected _authenticateProxyViaCredentials() {
const proxy = this._browserBase._options.proxy;
const proxy = this._browser._options.proxy;
if (!proxy)
return;
const { username, password } = proxy;
Expand All @@ -213,7 +193,7 @@ export abstract class BrowserContextBase extends EventEmitter implements Browser
if (this._isPersistentContext) {
// Default context is only created in 'persistent' mode and closing it should close
// the browser.
await this._browserBase.close();
await this._browser.close();
return;
}
if (this._closedStatus === 'open') {
Expand All @@ -226,7 +206,7 @@ export abstract class BrowserContextBase extends EventEmitter implements Browser
}
}

export function assertBrowserContextIsNotOwned(context: BrowserContextBase) {
export function assertBrowserContextIsNotOwned(context: BrowserContext) {
for (const page of context.pages()) {
if (page._ownedContext)
throw new Error('Please use browser.newContext() for multi-page scripts that share the context.');
Expand Down
8 changes: 4 additions & 4 deletions src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

import { BrowserBase, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { Browser, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { Events as CommonEvents } from '../events';
import { assert } from '../helper';
import * as network from '../network';
Expand All @@ -31,7 +31,7 @@ import { Protocol } from './protocol';
import { CRExecutionContext } from './crExecutionContext';
import { CRDevTools } from './crDevTools';

export class CRBrowser extends BrowserBase {
export class CRBrowser extends Browser {
readonly _connection: CRConnection;
_session: CRSession;
private _clientRootSessionPromise: Promise<CRSession> | null = null;
Expand Down Expand Up @@ -279,7 +279,7 @@ class CRServiceWorker extends Worker {
}
}

export class CRBrowserContext extends BrowserContextBase {
export class CRBrowserContext extends BrowserContext {
readonly _browser: CRBrowser;
readonly _browserContextId: string | null;
readonly _evaluateOnNewDocumentSources: string[];
Expand Down
4 changes: 2 additions & 2 deletions src/debug/debugController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

import { BrowserContextBase } from '../browserContext';
import { BrowserContext } from '../browserContext';
import { Events } from '../events';
import * as frames from '../frames';
import * as js from '../javascript';
import { Page } from '../page';
import DebugScript from './injected/debugScript';

export class DebugController {
constructor(context: BrowserContextBase) {
constructor(context: BrowserContext) {
context.on(Events.BrowserContext.Page, (page: Page) => {
for (const frame of page.frames())
this.ensureInstalledInFrame(frame);
Expand Down
8 changes: 4 additions & 4 deletions src/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

import { BrowserBase, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { Browser, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { Events } from '../events';
import { assert, helper, RegisteredListener } from '../helper';
import * as network from '../network';
Expand All @@ -27,7 +27,7 @@ import { ConnectionEvents, FFConnection } from './ffConnection';
import { FFPage } from './ffPage';
import { Protocol } from './protocol';

export class FFBrowser extends BrowserBase {
export class FFBrowser extends Browser {
_connection: FFConnection;
readonly _ffPages: Map<string, FFPage>;
readonly _contexts: Map<string, FFBrowserContext>;
Expand Down Expand Up @@ -163,7 +163,7 @@ export class FFBrowser extends BrowserBase {
}
}

export class FFBrowserContext extends BrowserContextBase {
export class FFBrowserContext extends BrowserContext {
readonly _browser: FFBrowser;
readonly _browserContextId: string | null;

Expand Down
8 changes: 4 additions & 4 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { Screenshotter } from './screenshotter';
import { TimeoutSettings } from './timeoutSettings';
import * as types from './types';
import { Events } from './events';
import { BrowserContext, BrowserContextBase } from './browserContext';
import { BrowserContext } from './browserContext';
import { ConsoleMessage } from './console';
import * as accessibility from './accessibility';
import { EventEmitter } from 'events';
Expand Down Expand Up @@ -99,7 +99,7 @@ export class Page extends EventEmitter {
readonly _disconnectedPromise: Promise<Error>;
private _crashedCallback: (e: Error) => void;
readonly _crashedPromise: Promise<Error>;
readonly _browserContext: BrowserContextBase;
readonly _browserContext: BrowserContext;
readonly keyboard: input.Keyboard;
readonly mouse: input.Mouse;
readonly _timeoutSettings: TimeoutSettings;
Expand All @@ -116,7 +116,7 @@ export class Page extends EventEmitter {
private _requestInterceptor?: network.RouteHandler;
_ownedContext: BrowserContext | undefined;

constructor(delegate: PageDelegate, browserContext: BrowserContextBase) {
constructor(delegate: PageDelegate, browserContext: BrowserContext) {
super();
this._delegate = delegate;
this._closedCallback = () => {};
Expand Down Expand Up @@ -144,7 +144,7 @@ export class Page extends EventEmitter {
}

async _doSlowMo() {
const slowMo = this._browserContext._browserBase._options.slowMo;
const slowMo = this._browserContext._browser._options.slowMo;
if (!slowMo)
return;
await new Promise(x => setTimeout(x, slowMo));
Expand Down
10 changes: 5 additions & 5 deletions src/rpc/browserServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { LaunchServerOptions } from './client/types';
import { BrowserTypeBase } from '../server/browserType';
import * as ws from 'ws';
import { helper } from '../helper';
import { BrowserBase } from '../browser';
import { Browser } from '../browser';
import { ChildProcess } from 'child_process';
import { EventEmitter } from 'ws';
import { DispatcherScope, DispatcherConnection } from './server/dispatcher';
Expand All @@ -43,18 +43,18 @@ export class BrowserServerLauncherImpl implements BrowserServerLauncher {
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
env: options.env ? envObjectToArray(options.env) : undefined,
});
return new BrowserServerImpl(this._browserType, browser as BrowserBase, options.port);
return new BrowserServerImpl(this._browserType, browser, options.port);
}
}

export class BrowserServerImpl extends EventEmitter implements BrowserServer {
private _server: ws.Server;
private _browserType: BrowserTypeBase;
private _browser: BrowserBase;
private _browser: Browser;
private _wsEndpoint: string;
private _process: ChildProcess;

constructor(browserType: BrowserTypeBase, browser: BrowserBase, port: number = 0) {
constructor(browserType: BrowserTypeBase, browser: Browser, port: number = 0) {
super();

this._browserType = browserType;
Expand Down Expand Up @@ -121,7 +121,7 @@ class ConnectedBrowser extends BrowserDispatcher {
private _contexts: BrowserContextDispatcher[] = [];
_closed = false;

constructor(scope: DispatcherScope, browser: BrowserBase) {
constructor(scope: DispatcherScope, browser: Browser) {
super(scope, browser, 'connectedBrowser');
}

Expand Down
8 changes: 4 additions & 4 deletions src/rpc/server/browserContextDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as types from '../../types';
import { BrowserContextBase, BrowserContext } from '../../browserContext';
import { BrowserContext } from '../../browserContext';
import { Events } from '../../events';
import { Dispatcher, DispatcherScope, lookupDispatcher } from './dispatcher';
import { PageDispatcher, BindingCallDispatcher, WorkerDispatcher } from './pageDispatcher';
Expand All @@ -26,9 +26,9 @@ import { CDPSessionDispatcher } from './cdpSessionDispatcher';
import { Events as ChromiumEvents } from '../../chromium/events';

export class BrowserContextDispatcher extends Dispatcher<BrowserContext, BrowserContextInitializer> implements BrowserContextChannel {
private _context: BrowserContextBase;
private _context: BrowserContext;

constructor(scope: DispatcherScope, context: BrowserContextBase) {
constructor(scope: DispatcherScope, context: BrowserContext) {
super(scope, context, 'BrowserContext', {}, true);
this._context = context;

Expand All @@ -40,7 +40,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, Browser
this._dispose();
});

if (context._browserBase._options.name === 'chromium') {
if (context._browser._options.name === 'chromium') {
for (const page of (context as CRBrowserContext).backgroundPages())
this._dispatchEvent('crBackgroundPage', { page: new PageDispatcher(this._scope, page) });
context.on(ChromiumEvents.ChromiumBrowserContext.BackgroundPage, page => this._dispatchEvent('crBackgroundPage', { page: new PageDispatcher(this._scope, page) }));
Expand Down
7 changes: 3 additions & 4 deletions src/rpc/server/browserDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { Browser, BrowserBase } from '../../browser';
import { BrowserContextBase } from '../../browserContext';
import { Browser } from '../../browser';
import { Events } from '../../events';
import { BrowserChannel, BrowserContextChannel, BrowserInitializer, CDPSessionChannel, Binary, BrowserNewContextParams } from '../channels';
import { BrowserContextDispatcher } from './browserContextDispatcher';
Expand All @@ -25,7 +24,7 @@ import { CRBrowser } from '../../chromium/crBrowser';
import { PageDispatcher } from './pageDispatcher';

export class BrowserDispatcher extends Dispatcher<Browser, BrowserInitializer> implements BrowserChannel {
constructor(scope: DispatcherScope, browser: BrowserBase, guid?: string) {
constructor(scope: DispatcherScope, browser: Browser, guid?: string) {
super(scope, browser, 'Browser', { version: browser.version() }, true, guid);
browser.on(Events.Browser.Disconnected, () => this._didClose());
}
Expand All @@ -36,7 +35,7 @@ export class BrowserDispatcher extends Dispatcher<Browser, BrowserInitializer> i
}

async newContext(params: BrowserNewContextParams): Promise<{ context: BrowserContextChannel }> {
return { context: new BrowserContextDispatcher(this._scope, await this._object.newContext(params) as BrowserContextBase) };
return { context: new BrowserContextDispatcher(this._scope, await this._object.newContext(params)) };
}

async close(): Promise<void> {
Expand Down
6 changes: 2 additions & 4 deletions src/rpc/server/browserTypeDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* limitations under the License.
*/

import { BrowserBase } from '../../browser';
import { BrowserTypeBase, BrowserType } from '../../server/browserType';
import { BrowserDispatcher } from './browserDispatcher';
import { BrowserChannel, BrowserTypeChannel, BrowserContextChannel, BrowserTypeInitializer, BrowserTypeLaunchParams, BrowserTypeLaunchPersistentContextParams } from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { BrowserContextBase } from '../../browserContext';
import { BrowserContextDispatcher } from './browserContextDispatcher';

export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeInitializer> implements BrowserTypeChannel {
Expand All @@ -32,11 +30,11 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeIn

async launch(params: BrowserTypeLaunchParams): Promise<{ browser: BrowserChannel }> {
const browser = await this._object.launch(params);
return { browser: new BrowserDispatcher(this._scope, browser as BrowserBase) };
return { browser: new BrowserDispatcher(this._scope, browser) };
}

async launchPersistentContext(params: BrowserTypeLaunchPersistentContextParams): Promise<{ context: BrowserContextChannel }> {
const browserContext = await this._object.launchPersistentContext(params.userDataDir, params);
return { context: new BrowserContextDispatcher(this._scope, browserContext as BrowserContextBase) };
return { context: new BrowserContextDispatcher(this._scope, browserContext) };
}
}
3 changes: 1 addition & 2 deletions src/rpc/server/electronDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { Dispatcher, DispatcherScope, lookupDispatcher } from './dispatcher';
import { Electron, ElectronApplication, ElectronEvents, ElectronPage } from '../../server/electron';
import { ElectronApplicationChannel, ElectronApplicationInitializer, PageChannel, JSHandleChannel, ElectronInitializer, ElectronChannel, SerializedArgument, ElectronLaunchParams, SerializedValue } from '../channels';
import { BrowserContextDispatcher } from './browserContextDispatcher';
import { BrowserContextBase } from '../../browserContext';
import { PageDispatcher } from './pageDispatcher';
import { parseArgument, serializeResult } from './jsHandleDispatcher';
import { createHandle } from './elementHandlerDispatcher';
Expand All @@ -37,7 +36,7 @@ export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
super(scope, electronApplication, 'ElectronApplication', {}, true);
this._dispatchEvent('context', { context: new BrowserContextDispatcher(this._scope, electronApplication.context() as BrowserContextBase) });
this._dispatchEvent('context', { context: new BrowserContextDispatcher(this._scope, electronApplication.context()) });
electronApplication.on(ElectronEvents.ElectronApplication.Close, () => {
this._dispatchEvent('close');
this._dispose();
Expand Down
Loading

0 comments on commit 745dc33

Please sign in to comment.