Skip to content

Commit

Permalink
refactor: add parentSession() to CDPSession
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Aug 8, 2023
1 parent 2dec12f commit 364d304
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
27 changes: 24 additions & 3 deletions packages/puppeteer-core/src/common/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@ export class Connection extends EventEmitter {
const object = JSON.parse(message);
if (object.method === 'Target.attachedToTarget') {
const sessionId = object.params.sessionId;
const parentSession = this.#sessions.get(object.sessionId);
const session = new CDPSessionImpl(
this,
object.params.targetInfo.type,
sessionId
sessionId,
parentSession
);
this.#sessions.set(sessionId, session);
this.emit('sessionattached', session);
const parentSession = this.#sessions.get(object.sessionId);
if (parentSession) {
parentSession.emit('sessionattached', session);
}
Expand Down Expand Up @@ -470,6 +471,15 @@ export class CDPSession extends EventEmitter {
throw new Error('Not implemented');
}

/**
* Parent session in terms of CDP's auto-attach mechanism.
*
* @internal
*/
parentSession(): CDPSession | undefined {
return undefined;
}

send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
Expand Down Expand Up @@ -504,21 +514,32 @@ export class CDPSessionImpl extends CDPSession {
#targetType: string;
#callbacks = new CallbackRegistry();
#connection?: Connection;
#parentSession?: CDPSessionImpl;

/**
* @internal
*/
constructor(connection: Connection, targetType: string, sessionId: string) {
constructor(
connection: Connection,
targetType: string,
sessionId: string,
parentSession: CDPSessionImpl | undefined
) {
super();
this.#connection = connection;
this.#targetType = targetType;
this.#sessionId = sessionId;
this.#parentSession = parentSession;
}

override connection(): Connection | undefined {
return this.#connection;
}

override parentSession(): CDPSessionImpl | undefined {
return this.#parentSession;
}

override send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class MockCDPSession extends EventEmitter {
id() {
return '1';
}
parentSession() {
return undefined;
}
}

describe('DeviceRequestPrompt', function () {
Expand Down
3 changes: 3 additions & 0 deletions packages/puppeteer-core/src/common/NetworkManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class MockCDPSession extends EventEmitter {
id() {
return '1';
}
parentSession() {
return undefined;
}
}

describe('NetworkManager', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/src/CDPSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ describe('Target.createCDPSession', function () {
expect(foo).toBe('bar');
});

it('should return parent session', async () => {
const {page} = await getTestState();
const client = await page.createCDPSession();
expect(client.parentSession).toBeDefined();
});

it('should not report created targets for custom CDP sessions', async () => {
const {browser} = await getTestState();
let called = 0;
Expand Down

0 comments on commit 364d304

Please sign in to comment.