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.
|
|
|
|
*/
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
import {Events} from './Events';
|
|
|
|
import {Page} from './Page';
|
|
|
|
import {Worker as PuppeteerWorker} from './Worker';
|
|
|
|
import {CDPSession} from './Connection';
|
|
|
|
import {TaskQueue} from './TaskQueue';
|
|
|
|
import {Browser, BrowserContext} from './Browser';
|
2020-05-06 13:23:07 +00:00
|
|
|
import type {Viewport} from './PuppeteerViewport';
|
2018-02-26 20:10:06 +00:00
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
export class Target {
|
|
|
|
_targetInfo: Protocol.Target.TargetInfo;
|
|
|
|
_browserContext: BrowserContext;
|
|
|
|
_targetId: string;
|
|
|
|
_sessionFactory: () => Promise<CDPSession>;
|
|
|
|
_ignoreHTTPSErrors: boolean;
|
2020-05-06 13:23:07 +00:00
|
|
|
_defaultViewport?: Viewport;
|
2020-04-29 14:18:09 +00:00
|
|
|
_screenshotTaskQueue: TaskQueue;
|
2020-05-05 12:53:22 +00:00
|
|
|
_pagePromise?: Promise<Page>;
|
2020-04-29 14:18:09 +00:00
|
|
|
_workerPromise?: Promise<PuppeteerWorker>;
|
|
|
|
_initializedPromise: Promise<boolean>;
|
|
|
|
_initializedCallback: (x: boolean) => void;
|
|
|
|
_isClosedPromise: Promise<boolean>;
|
|
|
|
_closedCallback: () => void;
|
|
|
|
_isInitialized: boolean;
|
|
|
|
|
2020-05-06 13:23:07 +00:00
|
|
|
constructor(targetInfo: Protocol.Target.TargetInfo, browserContext: BrowserContext, sessionFactory: () => Promise<CDPSession>, ignoreHTTPSErrors: boolean, defaultViewport: Viewport | null, screenshotTaskQueue: TaskQueue) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetInfo = targetInfo;
|
2018-05-10 20:26:08 +00:00
|
|
|
this._browserContext = browserContext;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetId = targetInfo.targetId;
|
|
|
|
this._sessionFactory = sessionFactory;
|
|
|
|
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
2018-08-01 23:23:03 +00:00
|
|
|
this._defaultViewport = defaultViewport;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._screenshotTaskQueue = screenshotTaskQueue;
|
|
|
|
/** @type {?Promise<!Puppeteer.Page>} */
|
|
|
|
this._pagePromise = null;
|
2020-03-31 13:46:54 +00:00
|
|
|
/** @type {?Promise<!PuppeteerWorker>} */
|
2019-05-10 00:29:18 +00:00
|
|
|
this._workerPromise = null;
|
2020-04-29 14:18:09 +00:00
|
|
|
this._initializedPromise = new Promise<boolean>(fulfill => this._initializedCallback = fulfill).then(async success => {
|
2018-12-13 01:09:42 +00:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
const opener = this.opener();
|
|
|
|
if (!opener || !opener._pagePromise || this.type() !== 'page')
|
|
|
|
return true;
|
|
|
|
const openerPage = await opener._pagePromise;
|
2019-01-15 03:57:05 +00:00
|
|
|
if (!openerPage.listenerCount(Events.Page.Popup))
|
2018-12-13 01:09:42 +00:00
|
|
|
return true;
|
|
|
|
const popupPage = await this.page();
|
2019-01-15 03:57:05 +00:00
|
|
|
openerPage.emit(Events.Page.Popup, popupPage);
|
2018-12-13 01:09:42 +00:00
|
|
|
return true;
|
|
|
|
});
|
2020-04-29 14:18:09 +00:00
|
|
|
this._isClosedPromise = new Promise<boolean>(fulfill => this._closedCallback = fulfill);
|
2018-02-26 20:10:06 +00:00
|
|
|
this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
|
|
|
|
if (this._isInitialized)
|
|
|
|
this._initializedCallback(true);
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
createCDPSession(): Promise<CDPSession> {
|
2018-02-26 20:10:06 +00:00
|
|
|
return this._sessionFactory();
|
|
|
|
}
|
|
|
|
|
2020-05-05 12:53:22 +00:00
|
|
|
async page(): Promise<Page | null> {
|
2018-06-14 20:58:51 +00:00
|
|
|
if ((this._targetInfo.type === 'page' || this._targetInfo.type === 'background_page') && !this._pagePromise) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._pagePromise = this._sessionFactory()
|
2018-08-01 23:23:03 +00:00
|
|
|
.then(client => Page.create(client, this, this._ignoreHTTPSErrors, this._defaultViewport, this._screenshotTaskQueue));
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
async worker(): Promise<PuppeteerWorker | null> {
|
2019-05-10 00:29:18 +00:00
|
|
|
if (this._targetInfo.type !== 'service_worker' && this._targetInfo.type !== 'shared_worker')
|
|
|
|
return null;
|
|
|
|
if (!this._workerPromise) {
|
2019-08-15 04:43:18 +00:00
|
|
|
// TODO(einbinder): Make workers send their console logs.
|
|
|
|
this._workerPromise = this._sessionFactory()
|
2020-03-31 13:46:54 +00:00
|
|
|
.then(client => new PuppeteerWorker(client, this._targetInfo.url, () => {} /* consoleAPICalled */, () => {} /* exceptionThrown */));
|
2019-05-10 00:29:18 +00:00
|
|
|
}
|
|
|
|
return this._workerPromise;
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
url(): string {
|
2018-02-26 20:10:06 +00:00
|
|
|
return this._targetInfo.url;
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
type(): 'page'|'background_page'|'service_worker'|'shared_worker'|'other'|'browser'{
|
2018-02-26 20:10:06 +00:00
|
|
|
const type = this._targetInfo.type;
|
2019-05-10 00:29:18 +00:00
|
|
|
if (type === 'page' || type === 'background_page' || type === 'service_worker' || type === 'shared_worker' || type === 'browser')
|
2018-02-26 20:10:06 +00:00
|
|
|
return type;
|
|
|
|
return 'other';
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
browser(): Browser {
|
2018-05-10 20:26:08 +00:00
|
|
|
return this._browserContext.browser();
|
2018-04-17 17:37:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
browserContext(): BrowserContext {
|
2018-05-10 20:26:08 +00:00
|
|
|
return this._browserContext;
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
opener(): Target | null {
|
2020-04-21 09:40:04 +00:00
|
|
|
const {openerId} = this._targetInfo;
|
2018-06-01 00:06:29 +00:00
|
|
|
if (!openerId)
|
|
|
|
return null;
|
|
|
|
return this.browser()._targets.get(openerId);
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
_targetInfoChanged(targetInfo: Protocol.Target.TargetInfo): void {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetInfo = targetInfo;
|
|
|
|
|
|
|
|
if (!this._isInitialized && (this._targetInfo.type !== 'page' || this._targetInfo.url !== '')) {
|
|
|
|
this._isInitialized = true;
|
|
|
|
this._initializedCallback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|