Skip to content

Commit

Permalink
fix: make network manager multi session (#10793)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Aug 29, 2023
1 parent 6f2e3db commit 085936b
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 139 deletions.
19 changes: 10 additions & 9 deletions packages/puppeteer-core/src/common/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class FrameManager extends EventEmitter {
super();
this.#client = client;
this.#page = page;
this.#networkManager = new NetworkManager(client, ignoreHTTPSErrors, this);
this.#networkManager = new NetworkManager(ignoreHTTPSErrors, this);
this.#timeoutSettings = timeoutSettings;
this.setupEventListeners(this.#client);
client.once(CDPSessionEmittedEvents.Disconnected, () => {
Expand Down Expand Up @@ -176,12 +176,16 @@ export class FrameManager extends EventEmitter {
this.#onClientDisconnect().catch(debugError);
});
await this.initialize(client);
await this.#networkManager.updateClient(client);
await this.#networkManager.addClient(client);
if (frame) {
frame.emit(FrameEmittedEvents.FrameSwappedByActivation);
}
}

async registerSecondaryPage(client: CDPSessionImpl): Promise<void> {
await this.#networkManager.addClient(client);
}

private setupEventListeners(session: CDPSession) {
session.on('Page.frameAttached', event => {
this.#onFrameAttached(session, event.frameId, event.parentFrameId);
Expand Down Expand Up @@ -222,24 +226,21 @@ export class FrameManager extends EventEmitter {
});
}

async initialize(client: CDPSession = this.#client): Promise<void> {
async initialize(client: CDPSession): Promise<void> {
try {
const networkInit = this.#networkManager.addClient(client);
const result = await Promise.all([
client.send('Page.enable'),
client.send('Page.getFrameTree'),
]);

const {frameTree} = result[1];
this.#handleFrameTree(client, frameTree);
await Promise.all([
client.send('Page.setLifecycleEventsEnabled', {enabled: true}),
client.send('Runtime.enable').then(() => {
return this.#createIsolatedWorld(client, UTILITY_WORLD_NAME);
}),
// TODO: Network manager is not aware of OOP iframes yet.
client === this.#client
? this.#networkManager.initialize()
: Promise.resolve(),
networkInit,
]);
} catch (error) {
// The target might have been closed before the initialization finished.
Expand Down Expand Up @@ -295,7 +296,7 @@ export class FrameManager extends EventEmitter {
frame.updateClient(target._session()!);
}
this.setupEventListeners(target._session()!);
void this.initialize(target._session());
void this.initialize(target._session()!);
}

/**
Expand Down
21 changes: 14 additions & 7 deletions packages/puppeteer-core/src/common/NetworkManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ class MockCDPSession extends EventEmitter {
describe('NetworkManager', () => {
it('should process extra info on multiple redirects', async () => {
const mockCDPSession = new MockCDPSession();
new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);
mockCDPSession.emit('Network.requestWillBeSent', {
requestId: '7760711DEFCFA23132D98ABA6B4E175C',
loaderId: '7760711DEFCFA23132D98ABA6B4E175C',
Expand Down Expand Up @@ -476,11 +477,12 @@ describe('NetworkManager', () => {
});
it(`should handle "double pause" (crbug.com/1196004) Fetch.requestPaused events for the same Network.requestWillBeSent event`, async () => {
const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);
await manager.setRequestInterception(true);

const requests: HTTPRequest[] = [];
Expand Down Expand Up @@ -562,11 +564,12 @@ describe('NetworkManager', () => {
});
it(`should handle Network.responseReceivedExtraInfo event after Network.responseReceived event (github.com/puppeteer/puppeteer/issues/8234)`, async () => {
const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);

const requests: HTTPRequest[] = [];
manager.on(
Expand Down Expand Up @@ -680,11 +683,12 @@ describe('NetworkManager', () => {

it(`should resolve the response once the late responseReceivedExtraInfo event arrives`, async () => {
const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);

const finishedRequests: HTTPRequest[] = [];
const pendingRequests: HTTPRequest[] = [];
Expand Down Expand Up @@ -832,11 +836,12 @@ describe('NetworkManager', () => {

it(`should send responses for iframe that don't receive loadingFinished event`, async () => {
const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);

const responses: HTTPResponse[] = [];
const requests: HTTPRequest[] = [];
Expand Down Expand Up @@ -995,11 +1000,12 @@ describe('NetworkManager', () => {

it(`should send responses for iframe that don't receive loadingFinished event`, async () => {
const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);

const responses: HTTPResponse[] = [];
const requests: HTTPRequest[] = [];
Expand Down Expand Up @@ -1141,11 +1147,12 @@ describe('NetworkManager', () => {

it(`should handle cached redirects`, async () => {
const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(mockCDPSession, true, {
const manager = new NetworkManager(true, {
frame(): Frame | null {
return null;
},
});
await manager.addClient(mockCDPSession);

const responses: HTTPResponse[] = [];
const requests: HTTPRequest[] = [];
Expand Down
Loading

0 comments on commit 085936b

Please sign in to comment.