Skip to content

Commit

Permalink
feat: update other clients
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Aug 21, 2023
1 parent 772e89a commit 2ec6fcd
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 32 deletions.
7 changes: 7 additions & 0 deletions packages/puppeteer-core/src/common/Accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export class Accessibility {
this.#client = client;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

/**
* Captures the current state of the accessibility tree.
* The returned object represents the root accessible node of the page.
Expand Down
7 changes: 4 additions & 3 deletions packages/puppeteer-core/src/common/ChromeTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import {Protocol} from 'devtools-protocol';

import {TargetFilterCallback} from '../api/Browser.js';
import {TargetType} from '../api/Target.js';
import {assert} from '../util/assert.js';
import {Deferred} from '../util/Deferred.js';

import {CDPSession, Connection} from './Connection.js';
import {CDPSession, CDPSessionEmittedEvents, Connection} from './Connection.js';
import {EventEmitter} from './EventEmitter.js';
import {InitializationStatus, CDPTarget} from './Target.js';
import {
Expand All @@ -32,7 +33,7 @@ import {
import {debugError} from './util.js';

function isTargetExposed(target: CDPTarget): boolean {
return target.type() !== 'tab' && !target._subtype();
return target.type() !== TargetType.TAB && !target._subtype();
}

function isPageTargetBecomingPrimary(
Expand Down Expand Up @@ -324,7 +325,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
session,
'Target that is being activated is missing a CDPSession.'
);
session.parentSession()?.emit('sessionswapped', session);
session.parentSession()?.emit(CDPSessionEmittedEvents.Swapped, session);
}

target._targetInfoChanged(event.targetInfo);
Expand Down
1 change: 1 addition & 0 deletions packages/puppeteer-core/src/common/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ export interface CDPSessionOnMessageObject {
*/
export const CDPSessionEmittedEvents = {
Disconnected: Symbol('CDPSession.Disconnected'),
Swapped: Symbol('CDPSession.Swapped'),
} as const;

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/puppeteer-core/src/common/Coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ export class Coverage {
this.#cssCoverage = new CSSCoverage(client);
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#jsCoverage.updateClient(client);
this.#cssCoverage.updateClient(client);
}

/**
* @param options - Set of configurable options for coverage defaults to
* `resetOnNavigation : true, reportAnonymousScripts : false,`
Expand Down Expand Up @@ -212,6 +220,13 @@ export class JSCoverage {
this.#client = client;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

async start(
options: {
resetOnNavigation?: boolean;
Expand Down Expand Up @@ -342,6 +357,13 @@ export class CSSCoverage {
this.#client = client;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

async start(options: {resetOnNavigation?: boolean} = {}): Promise<void> {
assert(!this.#enabled, 'CSSCoverage is already enabled');
const {resetOnNavigation = true} = options;
Expand Down
4 changes: 4 additions & 0 deletions packages/puppeteer-core/src/common/EmulationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class EmulationManager {
this.#client = client;
}

updateClient(client: CDPSession): void {
this.#client = client;
}

get javascriptEnabled(): boolean {
return this.#javascriptEnabled;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/puppeteer-core/src/common/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ export class FrameManager extends EventEmitter {
this.#removeFramesRecursively(child);
}
} catch (err) {
if (mainFrame) {
this.#removeFramesRecursively(mainFrame);
}
this.#removeFramesRecursively(mainFrame);
}
}

Expand All @@ -173,6 +171,7 @@ export class FrameManager extends EventEmitter {
this.#onClientDisconnect().catch(debugError);
});
await this.initialize(client);
await this.#networkManager.updateClient(client);
if (frame) {
frame.emit(FrameEmittedEvents.FrameSwappedByActivation);
}
Expand Down
21 changes: 21 additions & 0 deletions packages/puppeteer-core/src/common/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export class CDPKeyboard extends Keyboard {
this.#client = client;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

override async down(
key: KeyInput,
options: Readonly<KeyDownOptions> = {
Expand Down Expand Up @@ -290,6 +297,13 @@ export class CDPMouse extends Mouse {
this.#keyboard = keyboard;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

#_state: Readonly<MouseState> = {
position: {x: 0, y: 0},
buttons: MouseButtonFlag.None,
Expand Down Expand Up @@ -571,6 +585,13 @@ export class CDPTouchscreen extends Touchscreen {
this.#keyboard = keyboard;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

override async tap(x: number, y: number): Promise<void> {
await this.touchStart(x, y);
await this.touchEnd();
Expand Down
54 changes: 30 additions & 24 deletions packages/puppeteer-core/src/common/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {createDebuggableDeferred} from '../util/DebuggableDeferred.js';
import {Deferred} from '../util/Deferred.js';

import {CDPSession} from './Connection.js';
import {EventEmitter} from './EventEmitter.js';
import {EventEmitter, Handler} from './EventEmitter.js';
import {FrameManager} from './FrameManager.js';
import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js';
Expand Down Expand Up @@ -90,6 +90,23 @@ export class NetworkManager extends EventEmitter {
};
#deferredInit?: Deferred<void>;

#handlers = new Map<string, Handler<any>>([
['Fetch.requestPaused', this.#onRequestPaused.bind(this)],
['Fetch.authRequired', this.#onAuthRequired.bind(this)],
['Network.requestWillBeSent', this.#onRequestWillBeSent.bind(this)],
[
'Network.requestServedFromCache',
this.#onRequestServedFromCache.bind(this),
],
['Network.responseReceived', this.#onResponseReceived.bind(this)],
['Network.loadingFinished', this.#onLoadingFinished.bind(this)],
['Network.loadingFailed', this.#onLoadingFailed.bind(this)],
[
'Network.responseReceivedExtraInfo',
this.#onResponseReceivedExtraInfo.bind(this),
],
]);

constructor(
client: CDPSession,
ignoreHTTPSErrors: boolean,
Expand All @@ -100,29 +117,18 @@ export class NetworkManager extends EventEmitter {
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
this.#frameManager = frameManager;

this.#client.on('Fetch.requestPaused', this.#onRequestPaused.bind(this));
this.#client.on('Fetch.authRequired', this.#onAuthRequired.bind(this));
this.#client.on(
'Network.requestWillBeSent',
this.#onRequestWillBeSent.bind(this)
);
this.#client.on(
'Network.requestServedFromCache',
this.#onRequestServedFromCache.bind(this)
);
this.#client.on(
'Network.responseReceived',
this.#onResponseReceived.bind(this)
);
this.#client.on(
'Network.loadingFinished',
this.#onLoadingFinished.bind(this)
);
this.#client.on('Network.loadingFailed', this.#onLoadingFailed.bind(this));
this.#client.on(
'Network.responseReceivedExtraInfo',
this.#onResponseReceivedExtraInfo.bind(this)
);
for (const [event, handler] of this.#handlers) {
this.#client.on(event, handler);
}
}

async updateClient(client: CDPSession): Promise<void> {
this.#client = client;
for (const [event, handler] of this.#handlers) {
this.#client.on(event, handler);
}
this.#deferredInit = undefined;
await this.initialize();
}

/**
Expand Down
10 changes: 8 additions & 2 deletions packages/puppeteer-core/src/common/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,21 @@ export class CDPPage extends Page {

this.#setupEventListeners();

this.#tabSession?.on('sessionswapped', async newSession => {
this.#tabSession?.on(CDPSessionEmittedEvents.Swapped, async newSession => {
this.#client = newSession;
assert(
this.#client instanceof CDPSessionImpl,
'CDPSession is not instance of CDPSessionImpl'
);
this.#target = this.#client._target();
assert(this.#target, 'Missing target on swap');
// TODO: swap the session for other members.
this.#keyboard.updateClient(newSession);
this.#mouse.updateClient(newSession);
this.#touchscreen.updateClient(newSession);
this.#accessibility.updateClient(newSession);
this.#emulationManager.updateClient(newSession);
this.#tracing.updateClient(newSession);
this.#coverage.updateClient(newSession);
await this.#frameManager.swapFrameTree(newSession);
this.#setupEventListeners();
});
Expand Down
7 changes: 7 additions & 0 deletions packages/puppeteer-core/src/common/Tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export class Tracing {
this.#client = client;
}

/**
* @internal
*/
updateClient(client: CDPSession): void {
this.#client = client;
}

/**
* Starts a trace for the current page.
* @remarks
Expand Down

0 comments on commit 2ec6fcd

Please sign in to comment.