puppeteer/packages/puppeteer-core/src/common/FrameManager.ts

450 lines
13 KiB
TypeScript
Raw Normal View History

/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Protocol} from 'devtools-protocol';
import {assert} from '../util/assert.js';
import {isErrorLike} from '../util/ErrorLike.js';
import {CDPSession, isTargetClosedError} from './Connection.js';
import {EventEmitter} from './EventEmitter.js';
import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js';
import {Frame} from './Frame.js';
import {FrameTree} from './FrameTree.js';
import {IsolatedWorld} from './IsolatedWorld.js';
import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
import {NetworkManager} from './NetworkManager.js';
import {Page} from '../api/Page.js';
import {Target} from './Target.js';
import {TimeoutSettings} from './TimeoutSettings.js';
import {debugError} from './util.js';
const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
/**
* We use symbols to prevent external parties listening to these events.
* They are internal to Puppeteer.
*
* @internal
*/
export const FrameManagerEmittedEvents = {
FrameAttached: Symbol('FrameManager.FrameAttached'),
FrameNavigated: Symbol('FrameManager.FrameNavigated'),
FrameDetached: Symbol('FrameManager.FrameDetached'),
FrameSwapped: Symbol('FrameManager.FrameSwapped'),
LifecycleEvent: Symbol('FrameManager.LifecycleEvent'),
FrameNavigatedWithinDocument: Symbol(
'FrameManager.FrameNavigatedWithinDocument'
),
ExecutionContextCreated: Symbol('FrameManager.ExecutionContextCreated'),
ExecutionContextDestroyed: Symbol('FrameManager.ExecutionContextDestroyed'),
};
/**
* A frame manager manages the frames for a given {@link Page | page}.
*
* @internal
*/
export class FrameManager extends EventEmitter {
2022-06-13 09:16:25 +00:00
#page: Page;
#networkManager: NetworkManager;
#timeoutSettings: TimeoutSettings;
#contextIdToContext = new Map<string, ExecutionContext>();
#isolatedWorlds = new Set<string>();
#client: CDPSession;
/**
* @internal
*/
_frameTree = new FrameTree();
2022-06-13 09:16:25 +00:00
get timeoutSettings(): TimeoutSettings {
2022-06-13 09:16:25 +00:00
return this.#timeoutSettings;
}
get networkManager(): NetworkManager {
return this.#networkManager;
}
get client(): CDPSession {
2022-06-13 09:16:25 +00:00
return this.#client;
}
2020-05-07 10:54:55 +00:00
constructor(
client: CDPSession,
page: Page,
ignoreHTTPSErrors: boolean,
timeoutSettings: TimeoutSettings
) {
2017-06-21 20:51:06 +00:00
super();
2022-06-13 09:16:25 +00:00
this.#client = client;
this.#page = page;
this.#networkManager = new NetworkManager(client, ignoreHTTPSErrors, this);
this.#timeoutSettings = timeoutSettings;
this.setupEventListeners(this.#client);
}
private setupEventListeners(session: CDPSession) {
session.on('Page.frameAttached', event => {
2022-06-13 09:16:25 +00:00
this.#onFrameAttached(session, event.frameId, event.parentFrameId);
});
session.on('Page.frameNavigated', event => {
2022-06-13 09:16:25 +00:00
this.#onFrameNavigated(event.frame);
});
session.on('Page.navigatedWithinDocument', event => {
2022-06-13 09:16:25 +00:00
this.#onFrameNavigatedWithinDocument(event.frameId, event.url);
});
session.on(
'Page.frameDetached',
(event: Protocol.Page.FrameDetachedEvent) => {
2022-06-13 09:16:25 +00:00
this.#onFrameDetached(
event.frameId,
event.reason as Protocol.Page.FrameDetachedEventReason
);
}
);
session.on('Page.frameStartedLoading', event => {
2022-06-13 09:16:25 +00:00
this.#onFrameStartedLoading(event.frameId);
});
session.on('Page.frameStoppedLoading', event => {
2022-06-13 09:16:25 +00:00
this.#onFrameStoppedLoading(event.frameId);
});
session.on('Runtime.executionContextCreated', event => {
2022-06-13 09:16:25 +00:00
this.#onExecutionContextCreated(event.context, session);
});
session.on('Runtime.executionContextDestroyed', event => {
2022-06-13 09:16:25 +00:00
this.#onExecutionContextDestroyed(event.executionContextId, session);
});
session.on('Runtime.executionContextsCleared', () => {
2022-06-13 09:16:25 +00:00
this.#onExecutionContextsCleared(session);
});
session.on('Page.lifecycleEvent', event => {
2022-06-13 09:16:25 +00:00
this.#onLifecycleEvent(event);
});
}
async initialize(client: CDPSession = this.#client): Promise<void> {
try {
const result = await Promise.all([
client.send('Page.enable'),
client.send('Page.getFrameTree'),
]);
const {frameTree} = result[1];
2022-06-13 09:16:25 +00:00
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.
2022-06-13 09:16:25 +00:00
client === this.#client
? this.#networkManager.initialize()
: Promise.resolve(),
]);
} catch (error) {
// The target might have been closed before the initialization finished.
if (isErrorLike(error) && isTargetClosedError(error)) {
return;
}
throw error;
}
}
executionContextById(
contextId: number,
session: CDPSession = this.#client
): ExecutionContext {
const key = `${session.id()}:${contextId}`;
const context = this.#contextIdToContext.get(key);
assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);
return context;
}
page(): Page {
return this.#page;
}
mainFrame(): Frame {
const mainFrame = this._frameTree.getMainFrame();
assert(mainFrame, 'Requesting main frame too early!');
return mainFrame;
}
frames(): Frame[] {
return Array.from(this._frameTree.frames());
}
frame(frameId: string): Frame | null {
return this._frameTree.getById(frameId) || null;
}
onAttachedToTarget(target: Target): void {
if (target._getTargetInfo().type !== 'iframe') {
return;
}
const frame = this.frame(target._getTargetInfo().targetId);
2022-06-14 11:55:35 +00:00
if (frame) {
frame.updateClient(target._session()!);
2022-06-14 11:55:35 +00:00
}
this.setupEventListeners(target._session()!);
this.initialize(target._session());
}
onDetachedFromTarget(target: Target): void {
const frame = this.frame(target._targetId);
if (frame && frame.isOOPFrame()) {
// When an OOP iframe is removed from the page, it
// will only get a Target.detachedFromTarget event.
2022-06-13 09:16:25 +00:00
this.#removeFramesRecursively(frame);
}
}
2022-06-13 09:16:25 +00:00
#onLifecycleEvent(event: Protocol.Page.LifecycleEventEvent): void {
const frame = this.frame(event.frameId);
2022-06-14 11:55:35 +00:00
if (!frame) {
return;
}
frame._onLifecycleEvent(event.loaderId, event.name);
this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame);
}
2022-06-13 09:16:25 +00:00
#onFrameStartedLoading(frameId: string): void {
const frame = this.frame(frameId);
2022-06-14 11:55:35 +00:00
if (!frame) {
return;
}
frame._onLoadingStarted();
}
2022-06-13 09:16:25 +00:00
#onFrameStoppedLoading(frameId: string): void {
const frame = this.frame(frameId);
2022-06-14 11:55:35 +00:00
if (!frame) {
return;
}
frame._onLoadingStopped();
this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame);
}
2022-06-13 09:16:25 +00:00
#handleFrameTree(
session: CDPSession,
frameTree: Protocol.Page.FrameTree
): void {
if (frameTree.frame.parentId) {
2022-06-13 09:16:25 +00:00
this.#onFrameAttached(
session,
frameTree.frame.id,
frameTree.frame.parentId
);
}
2022-06-13 09:16:25 +00:00
this.#onFrameNavigated(frameTree.frame);
2022-06-14 11:55:35 +00:00
if (!frameTree.childFrames) {
return;
}
for (const child of frameTree.childFrames) {
2022-06-13 09:16:25 +00:00
this.#handleFrameTree(session, child);
}
2017-06-21 20:51:06 +00:00
}
2022-06-13 09:16:25 +00:00
#onFrameAttached(
session: CDPSession,
frameId: string,
parentFrameId: string
): void {
let frame = this.frame(frameId);
if (frame) {
if (session && frame.isOOPFrame()) {
// If an OOP iframes becomes a normal iframe again
// it is first attached to the parent page before
// the target is removed.
frame.updateClient(session);
}
return;
}
frame = new Frame(this, frameId, parentFrameId, session);
this._frameTree.addFrame(frame);
this.emit(FrameManagerEmittedEvents.FrameAttached, frame);
2017-06-21 20:51:06 +00:00
}
async #onFrameNavigated(framePayload: Protocol.Page.Frame): Promise<void> {
const frameId = framePayload.id;
const isMainFrame = !framePayload.parentId;
let frame = this._frameTree.getById(frameId);
// Detach all child frames first.
if (frame) {
for (const child of frame.childFrames()) {
this.#removeFramesRecursively(child);
}
}
// Update or create main frame.
if (isMainFrame) {
if (frame) {
// Update frame id to retain frame identity on cross-process navigation.
this._frameTree.removeFrame(frame);
frame._id = frameId;
} else {
// Initial main frame navigation.
frame = new Frame(this, frameId, undefined, this.#client);
}
this._frameTree.addFrame(frame);
}
frame = await this._frameTree.waitForFrame(frameId);
frame._navigated(framePayload);
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
2017-06-21 20:51:06 +00:00
}
async #createIsolatedWorld(session: CDPSession, name: string): Promise<void> {
const key = `${session.id()}:${name}`;
2022-06-14 11:55:35 +00:00
if (this.#isolatedWorlds.has(key)) {
return;
}
await session.send('Page.addScriptToEvaluateOnNewDocument', {
source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`,
worldName: name,
});
await Promise.all(
this.frames()
.filter(frame => {
return frame._client() === session;
})
.map(frame => {
// Frames might be removed before we send this, so we don't want to
// throw an error.
return session
.send('Page.createIsolatedWorld', {
frameId: frame._id,
worldName: name,
grantUniveralAccess: true,
})
.catch(debugError);
})
);
this.#isolatedWorlds.add(key);
}
2022-06-13 09:16:25 +00:00
#onFrameNavigatedWithinDocument(frameId: string, url: string): void {
const frame = this.frame(frameId);
2022-06-14 11:55:35 +00:00
if (!frame) {
return;
}
frame._navigatedWithinDocument(url);
this.emit(FrameManagerEmittedEvents.FrameNavigatedWithinDocument, frame);
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
}
2022-06-13 09:16:25 +00:00
#onFrameDetached(
frameId: string,
reason: Protocol.Page.FrameDetachedEventReason
): void {
const frame = this.frame(frameId);
if (reason === 'remove') {
// Only remove the frame if the reason for the detached event is
// an actual removement of the frame.
// For frames that become OOP iframes, the reason would be 'swap'.
2022-06-14 11:55:35 +00:00
if (frame) {
this.#removeFramesRecursively(frame);
}
} else if (reason === 'swap') {
this.emit(FrameManagerEmittedEvents.FrameSwapped, frame);
}
2017-06-21 20:51:06 +00:00
}
2022-06-13 09:16:25 +00:00
#onExecutionContextCreated(
contextPayload: Protocol.Runtime.ExecutionContextDescription,
session: CDPSession
2020-05-07 10:54:55 +00:00
): void {
const auxData = contextPayload.auxData as {frameId?: string} | undefined;
2022-05-31 14:34:16 +00:00
const frameId = auxData && auxData.frameId;
const frame = typeof frameId === 'string' ? this.frame(frameId) : undefined;
let world: IsolatedWorld | undefined;
if (frame) {
// Only care about execution contexts created for the current session.
2022-06-14 11:55:35 +00:00
if (frame._client() !== session) {
return;
}
if (contextPayload.auxData && !!contextPayload.auxData['isDefault']) {
world = frame.worlds[MAIN_WORLD];
2020-05-07 10:54:55 +00:00
} else if (
contextPayload.name === UTILITY_WORLD_NAME &&
!frame.worlds[PUPPETEER_WORLD].hasContext()
2020-05-07 10:54:55 +00:00
) {
// In case of multiple sessions to the same target, there's a race between
// connections so we might end up creating multiple isolated worlds.
// We can use either.
world = frame.worlds[PUPPETEER_WORLD];
}
}
const context = new ExecutionContext(
2022-06-13 09:16:25 +00:00
frame?._client() || this.#client,
contextPayload,
world
);
2022-06-14 11:55:35 +00:00
if (world) {
world.setContext(context);
2022-06-14 11:55:35 +00:00
}
const key = `${session.id()}:${contextPayload.id}`;
2022-06-13 09:16:25 +00:00
this.#contextIdToContext.set(key, context);
}
2022-06-13 09:16:25 +00:00
#onExecutionContextDestroyed(
executionContextId: number,
session: CDPSession
): void {
const key = `${session.id()}:${executionContextId}`;
2022-06-13 09:16:25 +00:00
const context = this.#contextIdToContext.get(key);
2022-06-14 11:55:35 +00:00
if (!context) {
return;
}
2022-06-13 09:16:25 +00:00
this.#contextIdToContext.delete(key);
2022-06-14 11:55:35 +00:00
if (context._world) {
context._world.clearContext();
2022-06-14 11:55:35 +00:00
}
2017-06-21 20:51:06 +00:00
}
2022-06-13 09:16:25 +00:00
#onExecutionContextsCleared(session: CDPSession): void {
for (const [key, context] of this.#contextIdToContext.entries()) {
// Make sure to only clear execution contexts that belong
// to the current session.
2022-06-14 11:55:35 +00:00
if (context._client !== session) {
continue;
}
if (context._world) {
context._world.clearContext();
2022-06-14 11:55:35 +00:00
}
2022-06-13 09:16:25 +00:00
this.#contextIdToContext.delete(key);
}
}
2022-06-13 09:16:25 +00:00
#removeFramesRecursively(frame: Frame): void {
2022-06-14 11:55:35 +00:00
for (const child of frame.childFrames()) {
2022-06-13 09:16:25 +00:00
this.#removeFramesRecursively(child);
2022-06-14 11:55:35 +00:00
}
2017-06-21 20:51:06 +00:00
frame._detach();
this._frameTree.removeFrame(frame);
this.emit(FrameManagerEmittedEvents.FrameDetached, frame);
2017-06-21 20:51:06 +00:00
}
}