fix: create _defaultContext only in persistent mode (#1854)

This commit is contained in:
Yury Semikhatsky 2020-04-18 19:58:11 -07:00 committed by GitHub
parent 022bc67c07
commit c359116a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 29 deletions

View File

@ -34,7 +34,7 @@ export class CRBrowser extends BrowserBase {
readonly _connection: CRConnection; readonly _connection: CRConnection;
_session: CRSession; _session: CRSession;
private _clientRootSessionPromise: Promise<CRSession> | null = null; private _clientRootSessionPromise: Promise<CRSession> | null = null;
readonly _defaultContext: CRBrowserContext; readonly _defaultContext: CRBrowserContext | null = null;
readonly _contexts = new Map<string, CRBrowserContext>(); readonly _contexts = new Map<string, CRBrowserContext>();
_crPages = new Map<string, CRPage>(); _crPages = new Map<string, CRPage>();
_backgroundPages = new Map<string, CRPage>(); _backgroundPages = new Map<string, CRPage>();
@ -48,7 +48,7 @@ export class CRBrowser extends BrowserBase {
static async connect(transport: ConnectionTransport, isPersistent: boolean, slowMo?: number): Promise<CRBrowser> { static async connect(transport: ConnectionTransport, isPersistent: boolean, slowMo?: number): Promise<CRBrowser> {
const connection = new CRConnection(SlowMoTransport.wrap(transport, slowMo)); const connection = new CRConnection(SlowMoTransport.wrap(transport, slowMo));
const browser = new CRBrowser(connection); const browser = new CRBrowser(connection, isPersistent);
const session = connection.rootSession; const session = connection.rootSession;
if (!isPersistent) { if (!isPersistent) {
await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }); await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });
@ -83,12 +83,13 @@ export class CRBrowser extends BrowserBase {
return browser; return browser;
} }
constructor(connection: CRConnection) { constructor(connection: CRConnection, isPersistent: boolean) {
super(); super();
this._connection = connection; this._connection = connection;
this._session = this._connection.rootSession; this._session = this._connection.rootSession;
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({})); if (isPersistent)
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({}));
this._connection.on(ConnectionEvents.Disconnected, () => { this._connection.on(ConnectionEvents.Disconnected, () => {
for (const context of this._contexts.values()) for (const context of this._contexts.values())
context._browserClosed(); context._browserClosed();
@ -113,9 +114,26 @@ export class CRBrowser extends BrowserBase {
} }
_onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) { _onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) {
if (targetInfo.type === 'browser')
return;
const session = this._connection.session(sessionId)!; const session = this._connection.session(sessionId)!;
const context = (targetInfo.browserContextId && this._contexts.has(targetInfo.browserContextId)) ? assert(targetInfo.browserContextId, 'targetInfo: ' + JSON.stringify(targetInfo, null, 2));
this._contexts.get(targetInfo.browserContextId)! : this._defaultContext; let context = this._contexts.get(targetInfo.browserContextId) || null;
if (!context) {
// TODO: auto attach only to pages from our contexts.
// assert(this._defaultContext);
context = this._defaultContext;
}
if (targetInfo.type === 'other' || !context) {
if (waitingForDebugger) {
// Ideally, detaching should resume any target, but there is a bug in the backend.
session.send('Runtime.runIfWaitingForDebugger').catch(debugError).then(() => {
this._session.send('Target.detachFromTarget', { sessionId }).catch(debugError);
});
}
return;
}
assert(!this._crPages.has(targetInfo.targetId), 'Duplicate target ' + targetInfo.targetId); assert(!this._crPages.has(targetInfo.targetId), 'Duplicate target ' + targetInfo.targetId);
assert(!this._backgroundPages.has(targetInfo.targetId), 'Duplicate target ' + targetInfo.targetId); assert(!this._backgroundPages.has(targetInfo.targetId), 'Duplicate target ' + targetInfo.targetId);
@ -125,7 +143,7 @@ export class CRBrowser extends BrowserBase {
const backgroundPage = new CRPage(session, targetInfo.targetId, context, null); const backgroundPage = new CRPage(session, targetInfo.targetId, context, null);
this._backgroundPages.set(targetInfo.targetId, backgroundPage); this._backgroundPages.set(targetInfo.targetId, backgroundPage);
backgroundPage.pageOrError().then(() => { backgroundPage.pageOrError().then(() => {
context.emit(Events.CRBrowserContext.BackgroundPage, backgroundPage._page); context!.emit(Events.CRBrowserContext.BackgroundPage, backgroundPage._page);
}); });
return; return;
} }
@ -140,7 +158,7 @@ export class CRBrowser extends BrowserBase {
} }
crPage.pageOrError().then(() => { crPage.pageOrError().then(() => {
this._firstPageCallback(); this._firstPageCallback();
context.emit(CommonEvents.BrowserContext.Page, crPage._page); context!.emit(CommonEvents.BrowserContext.Page, crPage._page);
if (opener) { if (opener) {
opener.pageOrError().then(openerPage => { opener.pageOrError().then(openerPage => {
if (openerPage instanceof Page && !openerPage.isClosed()) if (openerPage instanceof Page && !openerPage.isClosed())
@ -158,13 +176,7 @@ export class CRBrowser extends BrowserBase {
return; return;
} }
assert(targetInfo.type === 'browser' || targetInfo.type === 'other'); assert(false, 'Unknown target type: ' + targetInfo.type);
if (waitingForDebugger) {
// Ideally, detaching should resume any target, but there is a bug in the backend.
session.send('Runtime.runIfWaitingForDebugger').catch(debugError).then(() => {
this._session.send('Target.detachFromTarget', { sessionId }).catch(debugError);
});
}
} }
_onDetachedFromTarget(payload: Protocol.Target.detachFromTargetParameters) { _onDetachedFromTarget(payload: Protocol.Target.detachFromTargetParameters) {

View File

@ -31,7 +31,7 @@ import { Protocol } from './protocol';
export class FFBrowser extends BrowserBase { export class FFBrowser extends BrowserBase {
_connection: FFConnection; _connection: FFConnection;
readonly _ffPages: Map<string, FFPage>; readonly _ffPages: Map<string, FFPage>;
readonly _defaultContext: FFBrowserContext; readonly _defaultContext: FFBrowserContext | null = null;
readonly _contexts: Map<string, FFBrowserContext>; readonly _contexts: Map<string, FFBrowserContext>;
private _eventListeners: RegisteredListener[]; private _eventListeners: RegisteredListener[];
readonly _firstPagePromise: Promise<void>; readonly _firstPagePromise: Promise<void>;
@ -39,17 +39,18 @@ export class FFBrowser extends BrowserBase {
static async connect(transport: ConnectionTransport, attachToDefaultContext: boolean, slowMo?: number): Promise<FFBrowser> { static async connect(transport: ConnectionTransport, attachToDefaultContext: boolean, slowMo?: number): Promise<FFBrowser> {
const connection = new FFConnection(SlowMoTransport.wrap(transport, slowMo)); const connection = new FFConnection(SlowMoTransport.wrap(transport, slowMo));
const browser = new FFBrowser(connection); const browser = new FFBrowser(connection, attachToDefaultContext);
await connection.send('Browser.enable', { attachToDefaultContext }); await connection.send('Browser.enable', { attachToDefaultContext });
return browser; return browser;
} }
constructor(connection: FFConnection) { constructor(connection: FFConnection, isPersistent: boolean) {
super(); super();
this._connection = connection; this._connection = connection;
this._ffPages = new Map(); this._ffPages = new Map();
this._defaultContext = new FFBrowserContext(this, null, validateBrowserContextOptions({})); if (isPersistent)
this._defaultContext = new FFBrowserContext(this, null, validateBrowserContextOptions({}));
this._contexts = new Map(); this._contexts = new Map();
this._connection.on(ConnectionEvents.Disconnected, () => { this._connection.on(ConnectionEvents.Disconnected, () => {
for (const context of this._contexts.values()) for (const context of this._contexts.values())
@ -124,6 +125,7 @@ export class FFBrowser extends BrowserBase {
const {targetId, browserContextId, openerId, type} = payload.targetInfo; const {targetId, browserContextId, openerId, type} = payload.targetInfo;
assert(type === 'page'); assert(type === 'page');
const context = browserContextId ? this._contexts.get(browserContextId)! : this._defaultContext; const context = browserContextId ? this._contexts.get(browserContextId)! : this._defaultContext;
assert(context, `Unknown context id:${browserContextId}, _defaultContext: ${this._defaultContext}`);
const session = this._connection.createSession(payload.sessionId, type); const session = this._connection.createSession(payload.sessionId, type);
const opener = openerId ? this._ffPages.get(openerId)! : null; const opener = openerId ? this._ffPages.get(openerId)! : null;
const ffPage = new FFPage(session, context, opener); const ffPage = new FFPage(session, context, opener);

View File

@ -67,7 +67,7 @@ export class Chromium implements BrowserType<CRBrowser> {
const browser = await CRBrowser.connect(transport!, true, slowMo); const browser = await CRBrowser.connect(transport!, true, slowMo);
browser._ownedServer = browserServer; browser._ownedServer = browserServer;
await helper.waitWithTimeout(browser._firstPagePromise, 'first page', timeout); await helper.waitWithTimeout(browser._firstPagePromise, 'first page', timeout);
return browser._defaultContext; return browser._defaultContext!;
} }
private async _launchServer(options: LaunchServerOptions, launchType: LaunchType, userDataDir?: string): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport, downloadsPath: string }> { private async _launchServer(options: LaunchServerOptions, launchType: LaunchType, userDataDir?: string): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport, downloadsPath: string }> {

View File

@ -74,7 +74,7 @@ export class Firefox implements BrowserType<FFBrowser> {
browser._ownedServer = browserServer; browser._ownedServer = browserServer;
browser._downloadsPath = downloadsPath; browser._downloadsPath = downloadsPath;
await helper.waitWithTimeout(browser._firstPagePromise, 'first page', timeout); await helper.waitWithTimeout(browser._firstPagePromise, 'first page', timeout);
const browserContext = browser._defaultContext; const browserContext = browser._defaultContext!;
return browserContext; return browserContext;
} }

View File

@ -67,7 +67,7 @@ export class WebKit implements BrowserType<WKBrowser> {
const browser = await WKBrowser.connect(transport!, slowMo, true); const browser = await WKBrowser.connect(transport!, slowMo, true);
browser._ownedServer = browserServer; browser._ownedServer = browserServer;
await helper.waitWithTimeout(browser._waitForFirstPageTarget(), 'first page', timeout); await helper.waitWithTimeout(browser._waitForFirstPageTarget(), 'first page', timeout);
return browser._defaultContext; return browser._defaultContext!;
} }
private async _launchServer(options: LaunchServerOptions, launchType: LaunchType, userDataDir?: string): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport, downloadsPath: string }> { private async _launchServer(options: LaunchServerOptions, launchType: LaunchType, userDataDir?: string): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport, downloadsPath: string }> {

View File

@ -31,9 +31,8 @@ const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) Appl
export class WKBrowser extends BrowserBase { export class WKBrowser extends BrowserBase {
private readonly _connection: WKConnection; private readonly _connection: WKConnection;
private readonly _attachToDefaultContext: boolean;
readonly _browserSession: WKSession; readonly _browserSession: WKSession;
readonly _defaultContext: WKBrowserContext; readonly _defaultContext: WKBrowserContext | null = null;
readonly _contexts = new Map<string, WKBrowserContext>(); readonly _contexts = new Map<string, WKBrowserContext>();
readonly _wkPages = new Map<string, WKPage>(); readonly _wkPages = new Map<string, WKPage>();
private readonly _eventListeners: RegisteredListener[]; private readonly _eventListeners: RegisteredListener[];
@ -49,10 +48,10 @@ export class WKBrowser extends BrowserBase {
constructor(transport: ConnectionTransport, attachToDefaultContext: boolean) { constructor(transport: ConnectionTransport, attachToDefaultContext: boolean) {
super(); super();
this._connection = new WKConnection(transport, this._onDisconnect.bind(this)); this._connection = new WKConnection(transport, this._onDisconnect.bind(this));
this._attachToDefaultContext = attachToDefaultContext;
this._browserSession = this._connection.browserSession; this._browserSession = this._connection.browserSession;
this._defaultContext = new WKBrowserContext(this, undefined, validateBrowserContextOptions({})); if (attachToDefaultContext)
this._defaultContext = new WKBrowserContext(this, undefined, validateBrowserContextOptions({}));
this._eventListeners = [ this._eventListeners = [
helper.addEventListener(this._browserSession, 'Playwright.pageProxyCreated', this._onPageProxyCreated.bind(this)), helper.addEventListener(this._browserSession, 'Playwright.pageProxyCreated', this._onPageProxyCreated.bind(this)),
@ -117,10 +116,10 @@ export class WKBrowser extends BrowserBase {
// lifecycle events. // lifecycle events.
context = this._contexts.get(pageProxyInfo.browserContextId) || null; context = this._contexts.get(pageProxyInfo.browserContextId) || null;
} }
if (!context && !this._attachToDefaultContext)
return;
if (!context) if (!context)
context = this._defaultContext; context = this._defaultContext;
if (!context)
return;
const pageProxySession = new WKSession(this._connection, pageProxyId, `The page has been closed.`, (message: any) => { const pageProxySession = new WKSession(this._connection, pageProxyId, `The page has been closed.`, (message: any) => {
this._connection.rawSend({ ...message, pageProxyId }); this._connection.rawSend({ ...message, pageProxyId });
}); });