/** * Copyright 2019 Google Inc. All rights reserved. * Modifications copyright (c) Microsoft Corporation. * * 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 { helper, RegisteredListener } from '../helper'; import { Browser, BrowserContext } from './Browser'; import { Page } from './Page'; import { Protocol } from './protocol'; export class Target { private _browserContext: BrowserContext; _targetId: string; private _type: 'page' | 'service-worker' | 'worker'; _pagePromise: Promise | null = null; private _openerId?: string; private _url: string; _initializedPromise: Promise; _initializedCallback: (value?: unknown) => void; _isClosedPromise: Promise; _closedCallback: (value?: unknown) => void; _isInitialized: boolean; _eventListeners: RegisteredListener[]; constructor(targetInfo: Protocol.Target.TargetInfo, browserContext: BrowserContext, openerId?: string) { const {targetId, url, type} = targetInfo; this._browserContext = browserContext; this._targetId = targetId; this._type = type; /** @type {?Promise} */ this._pagePromise = null; this._url = url; this._openerId = openerId; this._isClosedPromise = new Promise(fulfill => this._closedCallback = fulfill); if (type === 'page') { const session = this._browserContext.browser()._connection.session(this._targetId); this._eventListeners = [ // FIXME: we could use Page.frameStartedLoading if it had url info. helper.addEventListener(session, 'Page.frameNavigated', this._onFrameNavigated.bind(this)), ]; } } async _onFrameNavigated(params: Protocol.Page.frameNavigatedPayload) { // Check if main frame, get url from the event. // Skip child frames. if (params.frame.parentId) return; const url = params.frame.url; if (this._url !== url) { this._url = url; this.browser()._onTargetChanged(this); } } async page(): Promise { if (this._type === 'page' && !this._pagePromise) { const session = this.browser()._connection.session(this._targetId); this._pagePromise = Page.create(session, this, this.browser()._defaultViewport, this.browser()._screenshotTaskQueue); } return this._pagePromise; } url(): string { return this._url; } type(): 'page' | 'service-worker' | 'worker' { return this._type; } browser(): Browser { return this._browserContext.browser(); } browserContext(): BrowserContext { return this._browserContext; } opener(): Target | null { return this._openerId ? this.browser()._targets.get(this._openerId) : null; } }