Skip to content

Commit decf373

Browse files
fix(electron): return a ChromiumBrowserContext for electron (#4913)
1 parent df53cb2 commit decf373

File tree

17 files changed

+42
-21
lines changed

17 files changed

+42
-21
lines changed

src/client/android.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Page } from './page';
2727
import { TimeoutSettings } from '../utils/timeoutSettings';
2828
import { Waiter } from './waiter';
2929
import { EventEmitter } from 'events';
30+
import { ChromiumBrowserContext } from './chromiumBrowserContext';
3031

3132
type Direction = 'down' | 'up' | 'left' | 'right';
3233
type SpeedOptions = { speed?: number };
@@ -233,11 +234,11 @@ export class AndroidDevice extends ChannelOwner<channels.AndroidDeviceChannel, c
233234
});
234235
}
235236

236-
async launchBrowser(options: types.BrowserContextOptions & { pkg?: string } = {}): Promise<BrowserContext> {
237+
async launchBrowser(options: types.BrowserContextOptions & { pkg?: string } = {}): Promise<ChromiumBrowserContext> {
237238
return this._wrapApiCall('androidDevice.launchBrowser', async () => {
238239
const contextOptions = await prepareBrowserContextOptions(options);
239240
const { context } = await this._channel.launchBrowser(contextOptions);
240-
return BrowserContext.from(context);
241+
return BrowserContext.from(context) as ChromiumBrowserContext;
241242
});
242243
}
243244

src/client/browserContext.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
4040
_pages = new Set<Page>();
4141
private _routes: { url: URLMatch, handler: network.RouteHandler }[] = [];
4242
readonly _browser: Browser | null = null;
43-
readonly _browserName: string;
4443
readonly _bindings = new Map<string, (source: structs.BindingSource, ...args: any[]) => any>();
4544
_timeoutSettings = new TimeoutSettings();
4645
_ownerPage: Page | undefined;
@@ -55,11 +54,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
5554
return context ? BrowserContext.from(context) : null;
5655
}
5756

58-
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserContextInitializer, browserName: string) {
57+
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserContextInitializer) {
5958
super(parent, type, guid, initializer);
6059
if (parent instanceof Browser)
6160
this._browser = parent;
62-
this._browserName = browserName;
6361

6462
this._channel.on('bindingCall', ({binding}) => this._onBinding(BindingCall.from(binding)));
6563
this._channel.on('close', () => this._onClose());

src/client/chromiumBrowserContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import * as api from '../../types/types';
2727
export class ChromiumBrowserContext extends BrowserContext implements api.ChromiumBrowserContext {
2828
_backgroundPages = new Set<Page>();
2929
_serviceWorkers = new Set<Worker>();
30+
_isChromium = true;
3031

3132
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserContextInitializer) {
32-
super(parent, type, guid, initializer, 'chromium');
33+
super(parent, type, guid, initializer);
3334
this._channel.on('crBackgroundPage', ({ page }) => {
3435
const backgroundPage = Page.from(page);
3536
this._backgroundPages.add(backgroundPage);

src/client/connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ export class Connection {
173173
break;
174174
}
175175
case 'BrowserContext': {
176-
const browserName = (initializer as channels.BrowserContextInitializer).browserName;
177-
if (browserName === 'chromium')
176+
const {isChromium} = (initializer as channels.BrowserContextInitializer);
177+
if (isChromium)
178178
result = new ChromiumBrowserContext(parent, type, guid, initializer);
179179
else
180-
result = new BrowserContext(parent, type, guid, initializer, browserName);
180+
result = new BrowserContext(parent, type, guid, initializer);
181181
break;
182182
}
183183
case 'BrowserType':

src/client/page.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { evaluationScript, urlMatches } from './clientHelper';
4646
import { isString, isRegExp, isObject, mkdirIfNeeded, headersObjectToArray } from '../utils/utils';
4747
import { isSafeCloseError } from '../utils/errors';
4848
import { Video } from './video';
49+
import type { ChromiumBrowserContext } from './chromiumBrowserContext';
4950

5051
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
5152
const mkdirAsync = util.promisify(fs.mkdir);
@@ -133,7 +134,7 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
133134
this._channel.on('webSocket', ({ webSocket }) => this.emit(Events.Page.WebSocket, WebSocket.from(webSocket)));
134135
this._channel.on('worker', ({ worker }) => this._onWorker(Worker.from(worker)));
135136

136-
if (this._browserContext._browserName === 'chromium') {
137+
if ((this._browserContext as ChromiumBrowserContext)._isChromium) {
137138
this.coverage = new ChromiumCoverage(this._channel);
138139
this.pdf = options => this._pdf(options);
139140
} else {

src/dispatchers/browserContextDispatcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
2727
private _context: BrowserContext;
2828

2929
constructor(scope: DispatcherScope, context: BrowserContext) {
30-
super(scope, context, 'BrowserContext', { browserName: context._browser._options.name }, true);
30+
super(scope, context, 'BrowserContext', { isChromium: context._browser._options.isChromium }, true);
3131
this._context = context;
3232

3333
for (const page of context.pages())
@@ -131,7 +131,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
131131
}
132132

133133
async crNewCDPSession(params: channels.BrowserContextCrNewCDPSessionParams): Promise<channels.BrowserContextCrNewCDPSessionResult> {
134-
if (this._object._browser._options.name !== 'chromium')
134+
if (!this._object._browser._options.isChromium)
135135
throw new Error(`CDP session is only available in Chromium`);
136136
const crBrowserContext = this._object as CRBrowserContext;
137137
return { session: new CDPSessionDispatcher(this._scope, await crBrowserContext.newCDPSession((params.page as PageDispatcher)._object)) };

src/dispatchers/browserDispatcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ export class BrowserDispatcher extends Dispatcher<Browser, channels.BrowserIniti
4545
}
4646

4747
async crNewBrowserCDPSession(): Promise<channels.BrowserCrNewBrowserCDPSessionResult> {
48-
if (this._object._options.name !== 'chromium')
48+
if (!this._object._options.isChromium)
4949
throw new Error(`CDP session is only available in Chromium`);
5050
const crBrowser = this._object as CRBrowser;
5151
return { session: new CDPSessionDispatcher(this._scope, await crBrowser.newBrowserCDPSession()) };
5252
}
5353

5454
async crStartTracing(params: channels.BrowserCrStartTracingParams): Promise<void> {
55-
if (this._object._options.name !== 'chromium')
55+
if (!this._object._options.isChromium)
5656
throw new Error(`Tracing is only available in Chromium`);
5757
const crBrowser = this._object as CRBrowser;
5858
await crBrowser.startTracing(params.page ? (params.page as PageDispatcher)._object : undefined, params);
5959
}
6060

6161
async crStopTracing(): Promise<channels.BrowserCrStopTracingResult> {
62-
if (this._object._options.name !== 'chromium')
62+
if (!this._object._options.isChromium)
6363
throw new Error(`Tracing is only available in Chromium`);
6464
const crBrowser = this._object as CRBrowser;
6565
const buffer = await crBrowser.stopTracing();

src/protocol/channels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ export type BrowserCrStopTracingResult = {
530530

531531
// ----------- BrowserContext -----------
532532
export type BrowserContextInitializer = {
533-
browserName: string,
533+
isChromium: boolean,
534534
};
535535
export interface BrowserContextChannel extends Channel {
536536
on(event: 'bindingCall', callback: (params: BrowserContextBindingCallEvent) => void): this;

src/protocol/protocol.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ BrowserContext:
507507
type: interface
508508

509509
initializer:
510-
browserName: string
510+
isChromium: boolean
511511

512512
commands:
513513

src/server/android/android.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export class AndroidDevice extends EventEmitter {
256256

257257
const browserOptions: BrowserOptions = {
258258
name: 'clank',
259+
isChromium: true,
259260
slowMo: 0,
260261
persistent: { ...options, noDefaultViewport: true },
261262
downloadsPath: undefined,

0 commit comments

Comments
 (0)