2020-06-25 16:05:36 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 14:48:03 -07:00
|
|
|
import { BrowserContext } from '../server/browserContext';
|
2020-07-13 16:03:24 -07:00
|
|
|
import { Dispatcher, DispatcherScope, lookupDispatcher } from './dispatcher';
|
2020-07-08 21:36:03 -07:00
|
|
|
import { PageDispatcher, BindingCallDispatcher, WorkerDispatcher } from './pageDispatcher';
|
2020-08-24 14:48:03 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2021-05-13 10:29:14 -07:00
|
|
|
import { RouteDispatcher, RequestDispatcher, ResponseDispatcher } from './networkDispatchers';
|
2020-08-24 14:48:03 -07:00
|
|
|
import { CRBrowserContext } from '../server/chromium/crBrowser';
|
2020-07-08 21:36:03 -07:00
|
|
|
import { CDPSessionDispatcher } from './cdpSessionDispatcher';
|
2021-01-24 19:21:19 -08:00
|
|
|
import { RecorderSupplement } from '../server/supplements/recorderSupplement';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { CallMetadata } from '../server/instrumentation';
|
2021-03-31 10:38:05 -07:00
|
|
|
import { ArtifactDispatcher } from './artifactDispatcher';
|
|
|
|
import { Artifact } from '../server/artifact';
|
2021-05-13 10:29:14 -07:00
|
|
|
import { Request, Response } from '../server/network';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextInitializer> implements channels.BrowserContextChannel {
|
2020-08-19 10:31:59 -07:00
|
|
|
private _context: BrowserContext;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-08-19 10:31:59 -07:00
|
|
|
constructor(scope: DispatcherScope, context: BrowserContext) {
|
2021-01-29 16:00:56 -08:00
|
|
|
super(scope, context, 'BrowserContext', { isChromium: context._browser.options.isChromium }, true);
|
2020-06-25 16:05:36 -07:00
|
|
|
this._context = context;
|
2021-03-31 10:38:05 -07:00
|
|
|
// Note: when launching persistent context, dispatcher is created very late,
|
|
|
|
// so we can already have pages, videos and everything else.
|
|
|
|
|
|
|
|
const onVideo = (artifact: Artifact) => {
|
|
|
|
// Note: Video must outlive Page and BrowserContext, so that client can saveAs it
|
|
|
|
// after closing the context. We use |scope| for it.
|
|
|
|
const artifactDispatcher = new ArtifactDispatcher(scope, artifact);
|
|
|
|
this._dispatchEvent('video', { artifact: artifactDispatcher });
|
|
|
|
};
|
|
|
|
context.on(BrowserContext.Events.VideoStarted, onVideo);
|
|
|
|
for (const video of context._browser._idToVideo.values()) {
|
|
|
|
if (video.context === context)
|
|
|
|
onVideo(video.artifact);
|
|
|
|
}
|
2020-07-13 15:26:09 -07:00
|
|
|
|
|
|
|
for (const page of context.pages())
|
2020-07-14 18:26:50 -07:00
|
|
|
this._dispatchEvent('page', { page: new PageDispatcher(this._scope, page) });
|
2020-08-21 16:26:33 -07:00
|
|
|
context.on(BrowserContext.Events.Page, page => this._dispatchEvent('page', { page: new PageDispatcher(this._scope, page) }));
|
|
|
|
context.on(BrowserContext.Events.Close, () => {
|
2020-06-25 16:05:36 -07:00
|
|
|
this._dispatchEvent('close');
|
2020-07-10 16:24:11 -07:00
|
|
|
this._dispose();
|
2020-06-25 16:05:36 -07:00
|
|
|
});
|
2020-07-13 15:26:09 -07:00
|
|
|
|
2021-01-29 16:00:56 -08:00
|
|
|
if (context._browser.options.name === 'chromium') {
|
2020-07-13 15:26:09 -07:00
|
|
|
for (const page of (context as CRBrowserContext).backgroundPages())
|
2021-04-02 09:47:14 +08:00
|
|
|
this._dispatchEvent('backgroundPage', { page: new PageDispatcher(this._scope, page) });
|
|
|
|
context.on(CRBrowserContext.CREvents.BackgroundPage, page => this._dispatchEvent('backgroundPage', { page: new PageDispatcher(this._scope, page) }));
|
2020-07-13 15:26:09 -07:00
|
|
|
for (const serviceWorker of (context as CRBrowserContext).serviceWorkers())
|
2021-04-02 09:47:14 +08:00
|
|
|
this._dispatchEvent('serviceWorker', { worker: new WorkerDispatcher(this._scope, serviceWorker)});
|
|
|
|
context.on(CRBrowserContext.CREvents.ServiceWorker, serviceWorker => this._dispatchEvent('serviceWorker', { worker: new WorkerDispatcher(this._scope, serviceWorker) }));
|
2020-07-13 15:26:09 -07:00
|
|
|
}
|
2021-05-13 10:29:14 -07:00
|
|
|
context.on(BrowserContext.Events.Request, (request: Request) => {
|
|
|
|
return this._dispatchEvent('request', {
|
|
|
|
request: RequestDispatcher.from(this._scope, request),
|
|
|
|
page: PageDispatcher.fromNullable(this._scope, request.frame()._page.initializedOrUndefined())
|
|
|
|
});
|
|
|
|
});
|
|
|
|
context.on(BrowserContext.Events.Response, (response: Response) => this._dispatchEvent('response', {
|
|
|
|
response: ResponseDispatcher.from(this._scope, response),
|
|
|
|
page: PageDispatcher.fromNullable(this._scope, response.frame()._page.initializedOrUndefined())
|
|
|
|
}));
|
|
|
|
context.on(BrowserContext.Events.RequestFailed, (request: Request) => this._dispatchEvent('requestFailed', {
|
|
|
|
request: RequestDispatcher.from(this._scope, request),
|
|
|
|
failureText: request._failureText,
|
|
|
|
responseEndTiming: request._responseEndTiming,
|
|
|
|
page: PageDispatcher.fromNullable(this._scope, request.frame()._page.initializedOrUndefined())
|
|
|
|
}));
|
|
|
|
context.on(BrowserContext.Events.RequestFinished, (request: Request) => this._dispatchEvent('requestFinished', {
|
|
|
|
request: RequestDispatcher.from(scope, request),
|
|
|
|
responseEndTiming: request._responseEndTiming,
|
|
|
|
page: PageDispatcher.fromNullable(this._scope, request.frame()._page.initializedOrUndefined())
|
|
|
|
}));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setDefaultNavigationTimeoutNoReply(params: channels.BrowserContextSetDefaultNavigationTimeoutNoReplyParams) {
|
2020-06-25 16:05:36 -07:00
|
|
|
this._context.setDefaultNavigationTimeout(params.timeout);
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setDefaultTimeoutNoReply(params: channels.BrowserContextSetDefaultTimeoutNoReplyParams) {
|
2020-06-25 16:05:36 -07:00
|
|
|
this._context.setDefaultTimeout(params.timeout);
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async exposeBinding(params: channels.BrowserContextExposeBindingParams): Promise<void> {
|
2020-10-01 22:47:31 -07:00
|
|
|
await this._context.exposeBinding(params.name, !!params.needsHandle, (source, ...args) => {
|
|
|
|
const binding = new BindingCallDispatcher(this._scope, params.name, !!params.needsHandle, source, args);
|
2020-07-14 18:26:50 -07:00
|
|
|
this._dispatchEvent('bindingCall', { binding });
|
|
|
|
return binding.promise();
|
2021-05-18 16:40:24 +00:00
|
|
|
}, 'main');
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:33:24 -08:00
|
|
|
async newPage(params: channels.BrowserContextNewPageParams, metadata: CallMetadata): Promise<channels.BrowserContextNewPageResult> {
|
|
|
|
return { page: lookupDispatcher<PageDispatcher>(await this._context.newPage(metadata)) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async cookies(params: channels.BrowserContextCookiesParams): Promise<channels.BrowserContextCookiesResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { cookies: await this._context.cookies(params.urls) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async addCookies(params: channels.BrowserContextAddCookiesParams): Promise<void> {
|
2020-06-25 16:05:36 -07:00
|
|
|
await this._context.addCookies(params.cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearCookies(): Promise<void> {
|
|
|
|
await this._context.clearCookies();
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async grantPermissions(params: channels.BrowserContextGrantPermissionsParams): Promise<void> {
|
|
|
|
await this._context.grantPermissions(params.permissions, params.origin);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions(): Promise<void> {
|
|
|
|
await this._context.clearPermissions();
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setGeolocation(params: channels.BrowserContextSetGeolocationParams): Promise<void> {
|
2020-08-17 16:19:21 -07:00
|
|
|
await this._context.setGeolocation(params.geolocation);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setExtraHTTPHeaders(params: channels.BrowserContextSetExtraHTTPHeadersParams): Promise<void> {
|
2020-08-18 15:38:29 -07:00
|
|
|
await this._context.setExtraHTTPHeaders(params.headers);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setOffline(params: channels.BrowserContextSetOfflineParams): Promise<void> {
|
2020-06-25 16:05:36 -07:00
|
|
|
await this._context.setOffline(params.offline);
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setHTTPCredentials(params: channels.BrowserContextSetHTTPCredentialsParams): Promise<void> {
|
2020-08-17 16:19:21 -07:00
|
|
|
await this._context.setHTTPCredentials(params.httpCredentials);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async addInitScript(params: channels.BrowserContextAddInitScriptParams): Promise<void> {
|
2020-06-25 16:05:36 -07:00
|
|
|
await this._context._doAddInitScript(params.source);
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:19:27 -07:00
|
|
|
async setNetworkInterceptionEnabled(params: channels.BrowserContextSetNetworkInterceptionEnabledParams): Promise<void> {
|
2020-06-26 11:51:47 -07:00
|
|
|
if (!params.enabled) {
|
2020-08-18 17:34:04 -07:00
|
|
|
await this._context._setRequestInterceptor(undefined);
|
2020-06-26 11:51:47 -07:00
|
|
|
return;
|
|
|
|
}
|
2021-03-11 21:04:08 -03:00
|
|
|
await this._context._setRequestInterceptor((route, request) => {
|
2021-04-20 23:03:56 -07:00
|
|
|
this._dispatchEvent('route', { route: RouteDispatcher.from(this._scope, route), request: RequestDispatcher.from(this._scope, request) });
|
2020-06-26 11:51:47 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async storageState(params: channels.BrowserContextStorageStateParams, metadata: CallMetadata): Promise<channels.BrowserContextStorageStateResult> {
|
|
|
|
return await this._context.storageState(metadata);
|
2020-11-13 14:24:53 -08:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:33:24 -08:00
|
|
|
async close(params: channels.BrowserContextCloseParams, metadata: CallMetadata): Promise<void> {
|
|
|
|
await this._context.close(metadata);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-07-08 21:36:03 -07:00
|
|
|
|
2021-01-24 19:21:19 -08:00
|
|
|
async recorderSupplementEnable(params: channels.BrowserContextRecorderSupplementEnableParams): Promise<void> {
|
2021-04-21 20:46:45 -07:00
|
|
|
await RecorderSupplement.show(this._context, params);
|
2021-01-25 14:49:26 -08:00
|
|
|
}
|
|
|
|
|
2021-02-12 10:11:30 -08:00
|
|
|
async pause(params: channels.BrowserContextPauseParams, metadata: CallMetadata) {
|
|
|
|
// Inspector controller will take care of this.
|
2020-12-23 14:15:16 -08:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:47:14 +08:00
|
|
|
async newCDPSession(params: channels.BrowserContextNewCDPSessionParams): Promise<channels.BrowserContextNewCDPSessionResult> {
|
2021-01-29 16:00:56 -08:00
|
|
|
if (!this._object._browser.options.isChromium)
|
2020-08-28 10:23:02 -07:00
|
|
|
throw new Error(`CDP session is only available in Chromium`);
|
2020-07-08 21:36:03 -07:00
|
|
|
const crBrowserContext = this._object as CRBrowserContext;
|
2020-08-20 14:19:27 -07:00
|
|
|
return { session: new CDPSessionDispatcher(this._scope, await crBrowserContext.newCDPSession((params.page as PageDispatcher)._object)) };
|
2020-07-08 21:36:03 -07:00
|
|
|
}
|
2021-04-23 20:39:09 -07:00
|
|
|
|
2021-04-24 20:39:48 -07:00
|
|
|
async tracingStart(params: channels.BrowserContextTracingStartParams): Promise<channels.BrowserContextTracingStartResult> {
|
|
|
|
await this._context.tracing.start(params);
|
2021-04-23 20:39:09 -07:00
|
|
|
}
|
|
|
|
|
2021-04-24 20:39:48 -07:00
|
|
|
async tracingStop(params: channels.BrowserContextTracingStopParams): Promise<channels.BrowserContextTracingStopResult> {
|
|
|
|
await this._context.tracing.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
async tracingExport(params: channels.BrowserContextTracingExportParams): Promise<channels.BrowserContextTracingExportResult> {
|
|
|
|
const artifact = await this._context.tracing.export();
|
|
|
|
return { artifact: new ArtifactDispatcher(this._scope, artifact) };
|
2021-04-23 20:39:09 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|