2017-06-17 21:27:51 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2022-08-17 15:45:45 +00:00
|
|
|
import {isErrorLike} from '../util/ErrorLike.js';
|
2022-09-14 12:18:12 +00:00
|
|
|
import {CDPSession, isTargetClosedError} from './Connection.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {EventEmitter} from './EventEmitter.js';
|
|
|
|
import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js';
|
2022-08-17 15:45:45 +00:00
|
|
|
import {Frame} from './Frame.js';
|
2022-09-16 05:35:51 +00:00
|
|
|
import {FrameTree} from './FrameTree.js';
|
2022-11-10 16:11:18 +00:00
|
|
|
import {IsolatedWorld} from './IsolatedWorld.js';
|
|
|
|
import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {NetworkManager} from './NetworkManager.js';
|
2022-09-21 06:10:50 +00:00
|
|
|
import {Page} from '../api/Page.js';
|
2022-07-21 18:50:46 +00:00
|
|
|
import {Target} from './Target.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {TimeoutSettings} from './TimeoutSettings.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {debugError} from './util.js';
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2019-01-22 22:55:33 +00:00
|
|
|
const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
|
|
|
|
|
2020-07-08 10:00:11 +00:00
|
|
|
/**
|
|
|
|
* 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'),
|
2022-03-09 11:24:17 +00:00
|
|
|
FrameSwapped: Symbol('FrameManager.FrameSwapped'),
|
2020-07-08 10:00:11 +00:00
|
|
|
LifecycleEvent: Symbol('FrameManager.LifecycleEvent'),
|
|
|
|
FrameNavigatedWithinDocument: Symbol(
|
|
|
|
'FrameManager.FrameNavigatedWithinDocument'
|
|
|
|
),
|
|
|
|
ExecutionContextCreated: Symbol('FrameManager.ExecutionContextCreated'),
|
|
|
|
ExecutionContextDestroyed: Symbol('FrameManager.ExecutionContextDestroyed'),
|
|
|
|
};
|
|
|
|
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* A frame manager manages the frames for a given {@link Page | page}.
|
|
|
|
*
|
2020-07-02 15:13:22 +00:00
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
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;
|
2022-07-25 09:20:38 +00:00
|
|
|
/**
|
2022-09-16 05:35:51 +00:00
|
|
|
* @internal
|
2022-07-25 09:20:38 +00:00
|
|
|
*/
|
2022-09-16 05:35:51 +00:00
|
|
|
_frameTree = new FrameTree();
|
2022-06-13 09:16:25 +00:00
|
|
|
|
2022-12-09 11:36:39 +00:00
|
|
|
/**
|
|
|
|
* Set of frame IDs stored to indicate if a frame has received a
|
|
|
|
* frameNavigated event so that frame tree responses could be ignored as the
|
|
|
|
* frameNavigated event usually contains the latest information.
|
|
|
|
*/
|
|
|
|
#frameNavigatedReceived = new Set<string>();
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
get timeoutSettings(): TimeoutSettings {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#timeoutSettings;
|
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
get networkManager(): NetworkManager {
|
|
|
|
return this.#networkManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
get client(): CDPSession {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#client;
|
|
|
|
}
|
2020-04-29 11:28:16 +00:00
|
|
|
|
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);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private setupEventListeners(session: CDPSession) {
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameAttached', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameAttached(session, event.frameId, event.parentFrameId);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameNavigated', event => {
|
2022-12-09 11:36:39 +00:00
|
|
|
this.#frameNavigatedReceived.add(event.frame.id);
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameNavigated(event.frame);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.navigatedWithinDocument', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameNavigatedWithinDocument(event.frameId, event.url);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
|
|
|
session.on(
|
|
|
|
'Page.frameDetached',
|
|
|
|
(event: Protocol.Page.FrameDetachedEvent) => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameDetached(
|
2021-10-28 09:25:49 +00:00
|
|
|
event.frameId,
|
|
|
|
event.reason as Protocol.Page.FrameDetachedEventReason
|
|
|
|
);
|
|
|
|
}
|
2020-09-21 11:23:36 +00:00
|
|
|
);
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameStartedLoading', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameStartedLoading(event.frameId);
|
2022-05-17 12:15:44 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameStoppedLoading', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameStoppedLoading(event.frameId);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Runtime.executionContextCreated', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onExecutionContextCreated(event.context, session);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Runtime.executionContextDestroyed', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onExecutionContextDestroyed(event.executionContextId, session);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
|
|
|
session.on('Runtime.executionContextsCleared', () => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onExecutionContextsCleared(session);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.lifecycleEvent', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onLifecycleEvent(event);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2019-04-10 04:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
async initialize(client: CDPSession = this.#client): Promise<void> {
|
2021-10-28 09:25:49 +00:00
|
|
|
try {
|
|
|
|
const result = await Promise.all([
|
|
|
|
client.send('Page.enable'),
|
|
|
|
client.send('Page.getFrameTree'),
|
|
|
|
]);
|
2020-03-31 16:42:32 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {frameTree} = result[1];
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#handleFrameTree(client, frameTree);
|
2021-10-28 09:25:49 +00:00
|
|
|
await Promise.all([
|
2022-06-22 13:25:44 +00:00
|
|
|
client.send('Page.setLifecycleEventsEnabled', {enabled: true}),
|
2022-06-15 10:42:21 +00:00
|
|
|
client.send('Runtime.enable').then(() => {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#createIsolatedWorld(client, UTILITY_WORLD_NAME);
|
2022-06-15 10:42:21 +00:00
|
|
|
}),
|
2021-10-28 09:25:49 +00:00
|
|
|
// TODO: Network manager is not aware of OOP iframes yet.
|
2022-06-13 09:16:25 +00:00
|
|
|
client === this.#client
|
|
|
|
? this.#networkManager.initialize()
|
2021-10-28 09:25:49 +00:00
|
|
|
: Promise.resolve(),
|
|
|
|
]);
|
|
|
|
} catch (error) {
|
|
|
|
// The target might have been closed before the initialization finished.
|
2022-09-14 12:18:12 +00:00
|
|
|
if (isErrorLike(error) && isTargetClosedError(error)) {
|
2021-10-28 09:25:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2019-04-10 04:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
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;
|
2017-10-18 02:14:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
page(): Page {
|
|
|
|
return this.#page;
|
|
|
|
}
|
2022-08-10 15:59:58 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
mainFrame(): Frame {
|
2022-09-16 05:35:51 +00:00
|
|
|
const mainFrame = this._frameTree.getMainFrame();
|
|
|
|
assert(mainFrame, 'Requesting main frame too early!');
|
|
|
|
return mainFrame;
|
2022-08-10 21:34:29 +00:00
|
|
|
}
|
2018-09-19 20:12:28 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
frames(): Frame[] {
|
2022-09-16 05:35:51 +00:00
|
|
|
return Array.from(this._frameTree.frames());
|
2018-09-19 20:12:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
frame(frameId: string): Frame | null {
|
2022-09-16 05:35:51 +00:00
|
|
|
return this._frameTree.getById(frameId) || null;
|
2018-09-19 20:12:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
onAttachedToTarget(target: Target): void {
|
2022-07-21 18:50:46 +00:00
|
|
|
if (target._getTargetInfo().type !== 'iframe') {
|
2020-09-21 11:23:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = this.frame(target._getTargetInfo().targetId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame) {
|
2022-08-10 21:34:29 +00:00
|
|
|
frame.updateClient(target._session()!);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-07-21 18:50:46 +00:00
|
|
|
this.setupEventListeners(target._session()!);
|
2022-09-16 05:35:51 +00:00
|
|
|
this.initialize(target._session());
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onLifecycleEvent(event: Protocol.Page.LifecycleEventEvent): void {
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = this.frame(event.frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-10 23:33:14 +00:00
|
|
|
frame._onLifecycleEvent(event.loaderId, event.name);
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame);
|
2017-11-10 23:33:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameStartedLoading(frameId: string): void {
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = this.frame(frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-17 12:15:44 +00:00
|
|
|
frame._onLoadingStarted();
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameStoppedLoading(frameId: string): void {
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = this.frame(frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-10 22:59:41 +00:00
|
|
|
frame._onLoadingStopped();
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame);
|
2018-04-10 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#handleFrameTree(
|
2021-10-28 09:25:49 +00:00
|
|
|
session: CDPSession,
|
|
|
|
frameTree: Protocol.Page.FrameTree
|
|
|
|
): void {
|
|
|
|
if (frameTree.frame.parentId) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameAttached(
|
2021-10-28 09:25:49 +00:00
|
|
|
session,
|
|
|
|
frameTree.frame.id,
|
|
|
|
frameTree.frame.parentId
|
|
|
|
);
|
|
|
|
}
|
2022-12-09 11:36:39 +00:00
|
|
|
if (!this.#frameNavigatedReceived.has(frameTree.frame.id)) {
|
|
|
|
this.#onFrameNavigated(frameTree.frame);
|
|
|
|
} else {
|
|
|
|
this.#frameNavigatedReceived.delete(frameTree.frame.id);
|
|
|
|
}
|
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frameTree.childFrames) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-18 02:14:57 +00:00
|
|
|
|
2021-10-28 09:25:49 +00:00
|
|
|
for (const child of frameTree.childFrames) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#handleFrameTree(session, child);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameAttached(
|
2021-10-28 09:25:49 +00:00
|
|
|
session: CDPSession,
|
|
|
|
frameId: string,
|
2022-07-25 09:20:38 +00:00
|
|
|
parentFrameId: string
|
2021-10-28 09:25:49 +00:00
|
|
|
): void {
|
2022-09-16 05:35:51 +00:00
|
|
|
let frame = this.frame(frameId);
|
|
|
|
if (frame) {
|
2021-10-28 09:25:49 +00:00
|
|
|
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.
|
2022-08-10 21:34:29 +00:00
|
|
|
frame.updateClient(session);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-07-25 09:20:38 +00:00
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
frame = new Frame(this, frameId, parentFrameId, session);
|
|
|
|
this._frameTree.addFrame(frame);
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameAttached, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
async #onFrameNavigated(framePayload: Protocol.Page.Frame): Promise<void> {
|
2022-07-25 09:20:38 +00:00
|
|
|
const frameId = framePayload.id;
|
2017-07-28 23:52:38 +00:00
|
|
|
const isMainFrame = !framePayload.parentId;
|
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
let frame = this._frameTree.getById(frameId);
|
2017-07-28 23:52:38 +00:00
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
// Detach all child frames first.
|
|
|
|
if (frame) {
|
|
|
|
for (const child of frame.childFrames()) {
|
|
|
|
this.#removeFramesRecursively(child);
|
2017-07-28 23:52:38 +00:00
|
|
|
}
|
2022-09-16 05:35:51 +00:00
|
|
|
}
|
2017-07-28 23:52:38 +00:00
|
|
|
|
2022-09-16 05:35:51 +00:00
|
|
|
// 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);
|
2022-07-25 09:20:38 +00:00
|
|
|
}
|
2022-09-16 05:35:51 +00:00
|
|
|
this._frameTree.addFrame(frame);
|
2022-07-25 09:20:38 +00:00
|
|
|
}
|
2022-09-16 05:35:51 +00:00
|
|
|
|
|
|
|
frame = await this._frameTree.waitForFrame(frameId);
|
|
|
|
frame._navigated(framePayload);
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
async #createIsolatedWorld(session: CDPSession, name: string): Promise<void> {
|
2021-10-28 09:25:49 +00:00
|
|
|
const key = `${session.id()}:${name}`;
|
2022-08-10 21:34:29 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#isolatedWorlds.has(key)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
|
|
|
|
await session.send('Page.addScriptToEvaluateOnNewDocument', {
|
2019-01-22 22:55:33 +00:00
|
|
|
source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`,
|
|
|
|
worldName: name,
|
2020-11-26 11:43:42 +00:00
|
|
|
});
|
2022-08-10 21:34:29 +00:00
|
|
|
|
2020-11-26 11:43:42 +00:00
|
|
|
await Promise.all(
|
2021-10-28 09:25:49 +00:00
|
|
|
this.frames()
|
2022-06-22 13:25:44 +00:00
|
|
|
.filter(frame => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return frame._client() === session;
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.map(frame => {
|
2022-08-10 21:34:29 +00:00
|
|
|
// Frames might be removed before we send this, so we don't want to
|
|
|
|
// throw an error.
|
2022-06-15 10:42:21 +00:00
|
|
|
return session
|
2022-03-02 12:03:44 +00:00
|
|
|
.send('Page.createIsolatedWorld', {
|
|
|
|
frameId: frame._id,
|
|
|
|
worldName: name,
|
|
|
|
grantUniveralAccess: true,
|
|
|
|
})
|
2022-06-15 10:42:21 +00:00
|
|
|
.catch(debugError);
|
|
|
|
})
|
2020-11-26 11:43:42 +00:00
|
|
|
);
|
2022-08-10 21:34:29 +00:00
|
|
|
|
|
|
|
this.#isolatedWorlds.add(key);
|
2019-01-22 22:55:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameNavigatedWithinDocument(frameId: string, url: string): void {
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = this.frame(frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-10 06:38:20 +00:00
|
|
|
frame._navigatedWithinDocument(url);
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.FrameNavigatedWithinDocument, frame);
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
|
2018-04-10 06:38:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameDetached(
|
2021-10-28 09:25:49 +00:00
|
|
|
frameId: string,
|
|
|
|
reason: Protocol.Page.FrameDetachedEventReason
|
|
|
|
): void {
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = this.frame(frameId);
|
2021-10-28 09:25:49 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-03-09 11:24:17 +00:00
|
|
|
} else if (reason === 'swap') {
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameSwapped, frame);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onExecutionContextCreated(
|
2021-10-28 09:25:49 +00:00
|
|
|
contextPayload: Protocol.Runtime.ExecutionContextDescription,
|
|
|
|
session: CDPSession
|
2020-05-07 10:54:55 +00:00
|
|
|
): void {
|
2022-06-22 13:25:44 +00:00
|
|
|
const auxData = contextPayload.auxData as {frameId?: string} | undefined;
|
2022-05-31 14:34:16 +00:00
|
|
|
const frameId = auxData && auxData.frameId;
|
2022-09-16 05:35:51 +00:00
|
|
|
const frame = typeof frameId === 'string' ? this.frame(frameId) : undefined;
|
2022-08-09 13:17:42 +00:00
|
|
|
let world: IsolatedWorld | undefined;
|
2019-01-22 22:55:33 +00:00
|
|
|
if (frame) {
|
2021-10-28 09:25:49 +00:00
|
|
|
// Only care about execution contexts created for the current session.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame._client() !== session) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-09 11:36:39 +00:00
|
|
|
if (contextPayload.auxData && contextPayload.auxData['isDefault']) {
|
2022-08-10 21:34:29 +00:00
|
|
|
world = frame.worlds[MAIN_WORLD];
|
2020-05-07 10:54:55 +00:00
|
|
|
} else if (
|
|
|
|
contextPayload.name === UTILITY_WORLD_NAME &&
|
2022-08-10 21:34:29 +00:00
|
|
|
!frame.worlds[PUPPETEER_WORLD].hasContext()
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2019-04-11 20:26:18 +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.
|
2022-08-10 21:34:29 +00:00
|
|
|
world = frame.worlds[PUPPETEER_WORLD];
|
2019-04-11 20:26:18 +00:00
|
|
|
}
|
2019-01-22 22:55:33 +00:00
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
const context = new ExecutionContext(
|
2022-06-13 09:16:25 +00:00
|
|
|
frame?._client() || this.#client,
|
2021-10-28 09:25:49 +00:00
|
|
|
contextPayload,
|
|
|
|
world
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (world) {
|
2022-08-09 11:29:12 +00:00
|
|
|
world.setContext(context);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
const key = `${session.id()}:${contextPayload.id}`;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#contextIdToContext.set(key, context);
|
2017-11-19 00:27:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onExecutionContextDestroyed(
|
2021-10-28 09:25:49 +00:00
|
|
|
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) {
|
2022-08-09 11:29:12 +00:00
|
|
|
context._world.clearContext();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onExecutionContextsCleared(session: CDPSession): void {
|
|
|
|
for (const [key, context] of this.#contextIdToContext.entries()) {
|
2021-10-28 09:25:49 +00:00
|
|
|
// 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) {
|
2022-08-09 11:29:12 +00:00
|
|
|
context._world.clearContext();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#contextIdToContext.delete(key);
|
2018-08-09 21:57:08 +00:00
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2022-09-16 05:35:51 +00:00
|
|
|
this._frameTree.removeFrame(frame);
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.FrameDetached, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|