2019-01-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2019 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-15 23:09:31 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
|
|
|
|
2023-05-12 09:51:28 +00:00
|
|
|
import type {Browser} from '../api/Browser.js';
|
2022-10-19 07:06:31 +00:00
|
|
|
import type {BrowserContext} from '../api/BrowserContext.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {Page, PageEmittedEvents} from '../api/Page.js';
|
2023-05-16 15:18:22 +00:00
|
|
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
|
|
|
import {CDPSession} from './Connection.js';
|
|
|
|
import {CDPPage} from './Page.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Viewport} from './PuppeteerViewport.js';
|
2022-07-21 18:50:46 +00:00
|
|
|
import {TargetManager} from './TargetManager.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {TaskQueue} from './TaskQueue.js';
|
2023-05-12 09:51:28 +00:00
|
|
|
import {debugError} from './util.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {WebWorker} from './WebWorker.js';
|
2018-02-26 20:10:06 +00:00
|
|
|
|
2023-05-16 15:18:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export enum InitializationStatus {
|
|
|
|
SUCCESS = 'success',
|
|
|
|
ABORTED = 'aborted',
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:15:39 +00:00
|
|
|
/**
|
2022-12-09 12:57:39 +00:00
|
|
|
* Target represents a
|
|
|
|
* {@link https://chromedevtools.github.io/devtools-protocol/tot/Target/ | CDP target}.
|
|
|
|
* In CDP a target is something that can be debugged such a frame, a page or a
|
|
|
|
* worker.
|
|
|
|
*
|
2020-07-02 11:15:39 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
export class Target {
|
2022-06-13 09:16:25 +00:00
|
|
|
#browserContext: BrowserContext;
|
2022-07-21 18:50:46 +00:00
|
|
|
#session?: CDPSession;
|
2022-06-13 09:16:25 +00:00
|
|
|
#targetInfo: Protocol.Target.TargetInfo;
|
2023-05-16 15:18:22 +00:00
|
|
|
#targetManager: TargetManager;
|
2022-08-16 10:56:13 +00:00
|
|
|
#sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>;
|
2020-06-22 12:57:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-05-16 15:18:22 +00:00
|
|
|
_initializedPromise = createDeferredPromise<InitializationStatus>();
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-05-16 15:18:22 +00:00
|
|
|
_isClosedPromise = createDeferredPromise<void>();
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_targetId: string;
|
2020-04-29 14:18:09 +00:00
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
targetInfo: Protocol.Target.TargetInfo,
|
2022-07-21 18:50:46 +00:00
|
|
|
session: CDPSession | undefined,
|
2020-05-07 10:54:55 +00:00
|
|
|
browserContext: BrowserContext,
|
2022-07-21 18:50:46 +00:00
|
|
|
targetManager: TargetManager,
|
2023-05-12 09:51:28 +00:00
|
|
|
sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2022-07-21 18:50:46 +00:00
|
|
|
this.#session = session;
|
|
|
|
this.#targetManager = targetManager;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#targetInfo = targetInfo;
|
|
|
|
this.#browserContext = browserContext;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetId = targetInfo.targetId;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionFactory = sessionFactory;
|
2023-05-12 09:51:28 +00:00
|
|
|
this._initialize();
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_session(): CDPSession | undefined {
|
|
|
|
return this.#session;
|
|
|
|
}
|
|
|
|
|
2023-05-12 09:51:28 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
protected _sessionFactory(): (
|
|
|
|
isAutoAttachEmulated: boolean
|
|
|
|
) => Promise<CDPSession> {
|
|
|
|
return this.#sessionFactory;
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Creates a Chrome Devtools Protocol session attached to the target.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
createCDPSession(): Promise<CDPSession> {
|
2022-08-16 10:56:13 +00:00
|
|
|
return this.#sessionFactory(false);
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_targetManager(): TargetManager {
|
|
|
|
return this.#targetManager;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_getTargetInfo(): Protocol.Target.TargetInfo {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#targetInfo;
|
2022-06-02 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`.
|
|
|
|
*/
|
2020-05-29 11:57:54 +00:00
|
|
|
async worker(): Promise<WebWorker | null> {
|
2023-05-15 11:12:23 +00:00
|
|
|
return null;
|
2019-05-10 00:29:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
url(): string {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#targetInfo.url;
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Identifies what kind of target this is.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
*
|
|
|
|
* See {@link https://developer.chrome.com/extensions/background_pages | docs} for more info about background pages.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
type():
|
|
|
|
| 'page'
|
|
|
|
| 'background_page'
|
|
|
|
| 'service_worker'
|
|
|
|
| 'shared_worker'
|
|
|
|
| 'other'
|
2020-05-29 08:46:44 +00:00
|
|
|
| 'browser'
|
|
|
|
| 'webview' {
|
2022-06-13 09:16:25 +00:00
|
|
|
const type = this.#targetInfo.type;
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
type === 'page' ||
|
|
|
|
type === 'background_page' ||
|
|
|
|
type === 'service_worker' ||
|
|
|
|
type === 'shared_worker' ||
|
2020-05-29 08:46:44 +00:00
|
|
|
type === 'browser' ||
|
|
|
|
type === 'webview'
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2018-02-26 20:10:06 +00:00
|
|
|
return type;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-02-26 20:10:06 +00:00
|
|
|
return 'other';
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Get the browser the target belongs to.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
browser(): Browser {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#browserContext.browser();
|
2018-04-17 17:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 06:47:25 +00:00
|
|
|
/**
|
|
|
|
* Get the browser context the target belongs to.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
browserContext(): BrowserContext {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#browserContext;
|
2018-05-10 20:26:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Get the target that opened this target. Top-level targets return `null`.
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
opener(): Target | undefined {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {openerId} = this.#targetInfo;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!openerId) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 00:06:29 +00:00
|
|
|
return this.browser()._targets.get(openerId);
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_targetInfoChanged(targetInfo: Protocol.Target.TargetInfo): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#targetInfo = targetInfo;
|
2023-05-12 09:51:28 +00:00
|
|
|
this._checkIfInitialized();
|
|
|
|
}
|
2018-02-26 20:10:06 +00:00
|
|
|
|
2023-05-12 09:51:28 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
protected _initialize(): void {
|
2023-05-16 15:18:22 +00:00
|
|
|
this._initializedPromise.resolve(InitializationStatus.SUCCESS);
|
2023-05-12 09:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
protected _checkIfInitialized(): void {
|
2023-05-16 15:18:22 +00:00
|
|
|
if (!this._initializedPromise.resolved()) {
|
|
|
|
this._initializedPromise.resolve(InitializationStatus.SUCCESS);
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-12 09:51:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the target is not of type `"page"`, `"webview"` or `"background_page"`,
|
|
|
|
* returns `null`.
|
|
|
|
*/
|
|
|
|
async page(): Promise<Page | null> {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class PageTarget extends Target {
|
|
|
|
#defaultViewport?: Viewport;
|
|
|
|
protected pagePromise?: Promise<Page>;
|
|
|
|
#screenshotTaskQueue: TaskQueue;
|
|
|
|
#ignoreHTTPSErrors: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
targetInfo: Protocol.Target.TargetInfo,
|
|
|
|
session: CDPSession | undefined,
|
|
|
|
browserContext: BrowserContext,
|
|
|
|
targetManager: TargetManager,
|
|
|
|
sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>,
|
|
|
|
ignoreHTTPSErrors: boolean,
|
|
|
|
defaultViewport: Viewport | null,
|
|
|
|
screenshotTaskQueue: TaskQueue
|
|
|
|
) {
|
|
|
|
super(targetInfo, session, browserContext, targetManager, sessionFactory);
|
|
|
|
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
|
|
|
|
this.#defaultViewport = defaultViewport ?? undefined;
|
|
|
|
this.#screenshotTaskQueue = screenshotTaskQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override _initialize(): void {
|
|
|
|
this._initializedPromise
|
|
|
|
.then(async success => {
|
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const opener = this.opener();
|
|
|
|
if (!(opener instanceof PageTarget)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!opener || !opener.pagePromise || this.type() !== 'page') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const openerPage = await opener.pagePromise;
|
|
|
|
if (!openerPage.listenerCount(PageEmittedEvents.Popup)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const popupPage = await this.page();
|
|
|
|
openerPage.emit(PageEmittedEvents.Popup, popupPage);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch(debugError);
|
|
|
|
this._checkIfInitialized();
|
|
|
|
}
|
|
|
|
|
|
|
|
override async page(): Promise<Page | null> {
|
|
|
|
if (!this.pagePromise) {
|
|
|
|
const session = this._session();
|
|
|
|
this.pagePromise = (
|
2023-05-15 11:12:23 +00:00
|
|
|
session
|
|
|
|
? Promise.resolve(session)
|
|
|
|
: this._sessionFactory()(/* isAutoAttachEmulated=*/ false)
|
2023-05-12 09:51:28 +00:00
|
|
|
).then(client => {
|
|
|
|
return CDPPage._create(
|
|
|
|
client,
|
|
|
|
this,
|
|
|
|
this.#ignoreHTTPSErrors,
|
|
|
|
this.#defaultViewport ?? null,
|
|
|
|
this.#screenshotTaskQueue
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return (await this.pagePromise) ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
override _checkIfInitialized(): void {
|
2023-05-16 15:18:22 +00:00
|
|
|
if (this._initializedPromise.resolved()) {
|
2023-05-12 09:51:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-16 15:18:22 +00:00
|
|
|
if (this._getTargetInfo().url !== '') {
|
|
|
|
this._initializedPromise.resolve(InitializationStatus.SUCCESS);
|
2023-05-12 09:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
2023-05-15 11:12:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class WorkerTarget extends Target {
|
|
|
|
#workerPromise?: Promise<WebWorker>;
|
|
|
|
|
|
|
|
override async worker(): Promise<WebWorker | null> {
|
|
|
|
if (!this.#workerPromise) {
|
|
|
|
const session = this._session();
|
|
|
|
// TODO(einbinder): Make workers send their console logs.
|
|
|
|
this.#workerPromise = (
|
|
|
|
session
|
|
|
|
? Promise.resolve(session)
|
|
|
|
: this._sessionFactory()(/* isAutoAttachEmulated=*/ false)
|
|
|
|
).then(client => {
|
|
|
|
return new WebWorker(
|
|
|
|
client,
|
|
|
|
this._getTargetInfo().url,
|
|
|
|
() => {} /* consoleAPICalled */,
|
|
|
|
() => {} /* exceptionThrown */
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.#workerPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class OtherTarget extends Target {}
|