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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as types from '../../types';
|
2020-06-26 11:51:47 -07:00
|
|
|
import { BrowserContextBase, BrowserContext } from '../../browserContext';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { Events } from '../../events';
|
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-07-20 17:38:06 -07:00
|
|
|
import { PageChannel, BrowserContextChannel, BrowserContextInitializer, CDPSessionChannel, BrowserContextSetGeolocationParams, BrowserContextSetHTTPCredentialsParams } from '../channels';
|
2020-06-26 11:51:47 -07:00
|
|
|
import { RouteDispatcher, RequestDispatcher } from './networkDispatchers';
|
2020-07-08 21:36:03 -07:00
|
|
|
import { CRBrowserContext } from '../../chromium/crBrowser';
|
|
|
|
import { CDPSessionDispatcher } from './cdpSessionDispatcher';
|
|
|
|
import { Events as ChromiumEvents } from '../../chromium/events';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-26 17:24:21 -07:00
|
|
|
export class BrowserContextDispatcher extends Dispatcher<BrowserContext, BrowserContextInitializer> implements BrowserContextChannel {
|
2020-06-25 16:05:36 -07:00
|
|
|
private _context: BrowserContextBase;
|
|
|
|
|
|
|
|
constructor(scope: DispatcherScope, context: BrowserContextBase) {
|
2020-07-22 10:37:21 -07:00
|
|
|
super(scope, context, 'BrowserContext', {}, true);
|
2020-06-25 16:05:36 -07:00
|
|
|
this._context = context;
|
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) });
|
|
|
|
context.on(Events.BrowserContext.Page, page => this._dispatchEvent('page', { page: new PageDispatcher(this._scope, page) }));
|
2020-06-25 16:05:36 -07:00
|
|
|
context.on(Events.BrowserContext.Close, () => {
|
|
|
|
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
|
|
|
|
|
|
|
if (context._browserBase._options.name === 'chromium') {
|
|
|
|
for (const page of (context as CRBrowserContext).backgroundPages())
|
2020-07-14 18:26:50 -07:00
|
|
|
this._dispatchEvent('crBackgroundPage', { page: new PageDispatcher(this._scope, page) });
|
2020-07-24 20:40:21 -07:00
|
|
|
context.on(ChromiumEvents.ChromiumBrowserContext.BackgroundPage, page => this._dispatchEvent('crBackgroundPage', { page: new PageDispatcher(this._scope, page) }));
|
2020-07-13 15:26:09 -07:00
|
|
|
for (const serviceWorker of (context as CRBrowserContext).serviceWorkers())
|
|
|
|
this._dispatchEvent('crServiceWorker', new WorkerDispatcher(this._scope, serviceWorker));
|
2020-07-24 20:40:21 -07:00
|
|
|
context.on(ChromiumEvents.ChromiumBrowserContext.ServiceWorker, serviceWorker => this._dispatchEvent('crServiceWorker', { worker: new WorkerDispatcher(this._scope, serviceWorker) }));
|
2020-07-13 15:26:09 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async setDefaultNavigationTimeoutNoReply(params: { timeout: number }) {
|
|
|
|
this._context.setDefaultNavigationTimeout(params.timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setDefaultTimeoutNoReply(params: { timeout: number }) {
|
|
|
|
this._context.setDefaultTimeout(params.timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async exposeBinding(params: { name: string }): Promise<void> {
|
2020-06-26 22:38:21 -07:00
|
|
|
await this._context.exposeBinding(params.name, (source, ...args) => {
|
2020-07-14 18:26:50 -07:00
|
|
|
const binding = new BindingCallDispatcher(this._scope, params.name, source, args);
|
|
|
|
this._dispatchEvent('bindingCall', { binding });
|
|
|
|
return binding.promise();
|
2020-06-26 11:51:47 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-14 18:26:50 -07:00
|
|
|
async newPage(): Promise<{ page: PageChannel }> {
|
|
|
|
return { page: lookupDispatcher<PageDispatcher>(await this._context.newPage()) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-14 18:26:50 -07:00
|
|
|
async cookies(params: { urls: string[] }): Promise<{ cookies: types.NetworkCookie[] }> {
|
|
|
|
return { cookies: await this._context.cookies(params.urls) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async addCookies(params: { cookies: types.SetNetworkCookieParam[] }): Promise<void> {
|
|
|
|
await this._context.addCookies(params.cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearCookies(): Promise<void> {
|
|
|
|
await this._context.clearCookies();
|
|
|
|
}
|
|
|
|
|
|
|
|
async grantPermissions(params: { permissions: string[], options: { origin?: string } }): Promise<void> {
|
|
|
|
await this._context.grantPermissions(params.permissions, params.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions(): Promise<void> {
|
|
|
|
await this._context.clearPermissions();
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
async setGeolocation(params: 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-07-15 13:21:21 -07:00
|
|
|
async setExtraHTTPHeaders(params: { headers: types.HeadersArray }): Promise<void> {
|
2020-08-18 15:38:29 -07:00
|
|
|
await this._context.setExtraHTTPHeaders(params.headers);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async setOffline(params: { offline: boolean }): Promise<void> {
|
|
|
|
await this._context.setOffline(params.offline);
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
async setHTTPCredentials(params: 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
|
|
|
}
|
|
|
|
|
|
|
|
async addInitScript(params: { source: string }): Promise<void> {
|
|
|
|
await this._context._doAddInitScript(params.source);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setNetworkInterceptionEnabled(params: { enabled: boolean }): 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;
|
|
|
|
}
|
2020-08-18 17:34:04 -07:00
|
|
|
this._context._setRequestInterceptor((route, request) => {
|
2020-06-30 21:30:39 -07:00
|
|
|
this._dispatchEvent('route', { route: new RouteDispatcher(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
|
|
|
}
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
2020-06-26 21:22:03 -07:00
|
|
|
await this._context.close();
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-07-08 21:36:03 -07:00
|
|
|
|
2020-07-14 18:26:50 -07:00
|
|
|
async crNewCDPSession(params: { page: PageDispatcher }): Promise<{ session: CDPSessionChannel }> {
|
2020-07-08 21:36:03 -07:00
|
|
|
const crBrowserContext = this._object as CRBrowserContext;
|
2020-07-14 18:26:50 -07:00
|
|
|
return { session: new CDPSessionDispatcher(this._scope, await crBrowserContext.newCDPSession(params.page._object)) };
|
2020-07-08 21:36:03 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|