chore(webkit): pass session to Target constructor (#187)

This commit is contained in:
Yury Semikhatsky 2019-12-09 15:41:20 -07:00 committed by Dmitry Gozman
parent cb92cd0d5c
commit 37540179de
5 changed files with 26 additions and 28 deletions

View File

@ -23,7 +23,7 @@ export const debugError = debug(`playwright:error`);
export type RegisteredListener = {
emitter: NodeJS.EventEmitter;
eventName: (string | symbol);
handler: (_: any) => void;
handler: (...args: any[]) => void;
};
class Helper {
@ -63,7 +63,7 @@ class Helper {
static addEventListener(
emitter: NodeJS.EventEmitter,
eventName: (string | symbol),
handler: (_: any) => void): RegisteredListener {
handler: (...args: any[]) => void): RegisteredListener {
emitter.on(eventName, handler);
return { emitter, eventName, handler };
}
@ -71,7 +71,7 @@ class Helper {
static removeEventListeners(listeners: Array<{
emitter: NodeJS.EventEmitter;
eventName: (string | symbol);
handler: (_: any) => void;
handler: (...args: any[]) => void;
}>) {
for (const listener of listeners)
listener.emitter.removeListener(listener.eventName, listener.handler);

View File

@ -19,7 +19,7 @@ import * as childProcess from 'child_process';
import { EventEmitter } from 'events';
import { assert, helper, RegisteredListener, debugError } from '../helper';
import { filterCookies, NetworkCookie, rewriteCookies, SetNetworkCookieParam } from '../network';
import { Connection } from './Connection';
import { Connection, ConnectionEvents, TargetSession } from './Connection';
import { Page } from './Page';
import { Target } from './Target';
import { Protocol } from './protocol';
@ -55,7 +55,7 @@ export class Browser extends EventEmitter {
this._contexts = new Map();
this._eventListeners = [
helper.addEventListener(this._connection, 'Target.targetCreated', this._onTargetCreated.bind(this)),
helper.addEventListener(this._connection, ConnectionEvents.TargetCreated, this._onTargetCreated.bind(this)),
helper.addEventListener(this._connection, 'Target.targetDestroyed', this._onTargetDestroyed.bind(this)),
helper.addEventListener(this._connection, 'Target.didCommitProvisionalTarget', this._onProvisionalTargetCommitted.bind(this)),
];
@ -142,7 +142,7 @@ export class Browser extends EventEmitter {
return contextPages.reduce((acc, x) => acc.concat(x), []);
}
async _onTargetCreated({targetInfo}) {
async _onTargetCreated(session: TargetSession, targetInfo: Protocol.Target.TargetInfo) {
let context = null;
if (targetInfo.browserContextId) {
// FIXME: we don't know about the default context id, so assume that all targets from
@ -155,7 +155,7 @@ export class Browser extends EventEmitter {
}
if (!context)
context = this._defaultContext;
const target = new Target(targetInfo, context);
const target = new Target(session, targetInfo, context);
this._targets.set(targetInfo.targetId, target);
this._privateEvents.emit(BrowserEvents.TargetCreated, target);
}

View File

@ -25,7 +25,8 @@ const debugProtocol = debug('playwright:protocol');
const debugWrappedMessage = require('debug')('wrapped');
export const ConnectionEvents = {
Disconnected: Symbol('ConnectionEvents.Disconnected')
Disconnected: Symbol('ConnectionEvents.Disconnected'),
TargetCreated: Symbol('ConnectionEvents.TargetCreated')
};
export class Connection extends EventEmitter {
@ -101,6 +102,7 @@ export class Connection extends EventEmitter {
const {targetId, type} = object.params.targetInfo;
const session = new TargetSession(this, type, targetId);
this._sessions.set(targetId, session);
this.emit(ConnectionEvents.TargetCreated, session, object.params.targetInfo);
} else if (object.method === 'Target.targetDestroyed') {
const session = this._sessions.get(object.params.targetId);
if (session) {
@ -143,10 +145,6 @@ export class Connection extends EventEmitter {
this._onClose();
this._transport.close();
}
session(targetId: string) : TargetSession {
return this._sessions.get(targetId);
}
}
export const TargetSessionEvents = {

View File

@ -31,7 +31,7 @@ export const NetworkManagerEvents = {
};
export class NetworkManager extends EventEmitter {
private _sesssion: TargetSession;
private _session: TargetSession;
private _frameManager: FrameManager;
private _requestIdToRequest = new Map<string, InterceptableRequest>();
private _extraHTTPHeaders: network.Headers = {};
@ -46,15 +46,15 @@ export class NetworkManager extends EventEmitter {
async initialize(session: TargetSession) {
helper.removeEventListeners(this._sessionListeners);
this._sesssion = session;
this._session = session;
this._sessionListeners = [
helper.addEventListener(this._sesssion, 'Network.requestWillBeSent', this._onRequestWillBeSent.bind(this)),
helper.addEventListener(this._sesssion, 'Network.responseReceived', this._onResponseReceived.bind(this)),
helper.addEventListener(this._sesssion, 'Network.loadingFinished', this._onLoadingFinished.bind(this)),
helper.addEventListener(this._sesssion, 'Network.loadingFailed', this._onLoadingFailed.bind(this)),
helper.addEventListener(this._session, 'Network.requestWillBeSent', this._onRequestWillBeSent.bind(this)),
helper.addEventListener(this._session, 'Network.responseReceived', this._onResponseReceived.bind(this)),
helper.addEventListener(this._session, 'Network.loadingFinished', this._onLoadingFinished.bind(this)),
helper.addEventListener(this._session, 'Network.loadingFailed', this._onLoadingFailed.bind(this)),
];
await this._sesssion.send('Network.enable');
await this._sesssion.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
await this._session.send('Network.enable');
await this._session.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
}
async setExtraHTTPHeaders(extraHTTPHeaders: { [s: string]: string; }) {
@ -64,7 +64,7 @@ export class NetworkManager extends EventEmitter {
assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
this._extraHTTPHeaders[key.toLowerCase()] = value;
}
await this._sesssion.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
await this._session.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
}
extraHTTPHeaders(): { [s: string]: string; } {
@ -77,7 +77,7 @@ export class NetworkManager extends EventEmitter {
}
async _updateProtocolCacheDisabled() {
await this._sesssion.send('Network.setResourceCachingDisabled', {
await this._session.send('Network.setResourceCachingDisabled', {
disabled: this._userCacheDisabled
});
}
@ -101,7 +101,7 @@ export class NetworkManager extends EventEmitter {
_createResponse(request: InterceptableRequest, responsePayload: Protocol.Network.Response): network.Response {
const remoteAddress: network.RemoteAddress = { ip: '', port: 0 };
const getResponseBody = async () => {
const response = await this._sesssion.send('Network.getResponseBody', { requestId: request._requestId });
const response = await this._session.send('Network.getResponseBody', { requestId: request._requestId });
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
};
return new network.Response(request.request, responsePayload.status, responsePayload.statusText, headersObject(responsePayload.headers), remoteAddress, getResponseBody);

View File

@ -26,6 +26,7 @@ export class Target {
readonly _browserContext: BrowserContext;
readonly _targetId: string;
readonly _type: 'page' | 'service-worker' | 'worker';
private readonly _session: TargetSession;
private _pagePromise: Promise<Page> | null = null;
private _page: Page | null = null;
@ -33,8 +34,9 @@ export class Target {
return (page as any)[targetSymbol];
}
constructor(targetInfo: Protocol.Target.TargetInfo, browserContext: BrowserContext) {
constructor(session: TargetSession, targetInfo: Protocol.Target.TargetInfo, browserContext: BrowserContext) {
const {targetId, type} = targetInfo;
this._session = session;
this._browserContext = browserContext;
this._targetId = targetId;
this._type = type;
@ -61,14 +63,12 @@ export class Target {
private async _adoptPage() {
(this._page as any)[targetSymbol] = this;
const browser = this._browserContext.browser();
const session = browser._connection.session(this._targetId);
session.once(TargetSessionEvents.Disconnected, () => {
this._session.once(TargetSessionEvents.Disconnected, () => {
// Once swapped out, we reset _page and won't call _didDisconnect for old session.
if (this._page)
this._page._didDisconnect();
});
await this._page._initialize(session).catch(e => {
await this._page._initialize(this._session).catch(e => {
// Swallow initialization errors due to newer target swap in,
// since we will reinitialize again.
if (!isSwappedOutError(e))