2020-07-13 21:46:59 -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.
|
|
|
|
*/
|
|
|
|
|
2021-05-02 22:45:06 -07:00
|
|
|
import type { BrowserWindow } from 'electron';
|
2021-02-01 11:43:26 -08:00
|
|
|
import * as structs from '../../types/structs';
|
|
|
|
import * as api from '../../types/types';
|
2020-08-24 17:05:16 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2021-02-01 11:43:26 -08:00
|
|
|
import { TimeoutSettings } from '../utils/timeoutSettings';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { BrowserContext } from './browserContext';
|
|
|
|
import { ChannelOwner } from './channelOwner';
|
2021-02-01 11:43:26 -08:00
|
|
|
import { envObjectToArray } from './clientHelper';
|
|
|
|
import { Events } from './events';
|
|
|
|
import { JSHandle, parseResult, serializeArgument } from './jsHandle';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { Page } from './page';
|
2021-02-01 11:43:26 -08:00
|
|
|
import { Env, WaitForEventOptions } from './types';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { Waiter } from './waiter';
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
type ElectronOptions = Omit<channels.ElectronLaunchOptions, 'env'> & {
|
2020-07-31 17:00:36 -07:00
|
|
|
env?: Env,
|
|
|
|
};
|
|
|
|
|
2020-12-26 20:25:18 -08:00
|
|
|
type ElectronAppType = typeof import('electron');
|
|
|
|
|
2021-02-01 11:43:26 -08:00
|
|
|
export class Electron extends ChannelOwner<channels.ElectronChannel, channels.ElectronInitializer> implements api.Electron {
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(electron: channels.ElectronChannel): Electron {
|
2020-07-13 21:46:59 -07:00
|
|
|
return (electron as any)._object;
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ElectronInitializer) {
|
2020-07-27 10:21:39 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:43:26 -08:00
|
|
|
async launch(options: ElectronOptions = {}): Promise<ElectronApplication> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('electron.launch', async (channel: channels.ElectronChannel) => {
|
2020-08-24 17:05:16 -07:00
|
|
|
const params: channels.ElectronLaunchParams = {
|
2021-02-11 17:46:54 -08:00
|
|
|
sdkLanguage: 'javascript',
|
2020-07-20 17:38:06 -07:00
|
|
|
...options,
|
2021-02-01 11:43:26 -08:00
|
|
|
env: envObjectToArray(options.env ? options.env : process.env),
|
2020-07-20 17:38:06 -07:00
|
|
|
};
|
2021-02-19 16:21:39 -08:00
|
|
|
return ElectronApplication.from((await channel.launch(params)).electronApplication);
|
2021-02-01 11:43:26 -08:00
|
|
|
});
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:43:26 -08:00
|
|
|
export class ElectronApplication extends ChannelOwner<channels.ElectronApplicationChannel, channels.ElectronApplicationInitializer> implements api.ElectronApplication {
|
2021-05-02 22:45:06 -07:00
|
|
|
private _context: BrowserContext;
|
2020-07-13 21:46:59 -07:00
|
|
|
private _windows = new Set<Page>();
|
|
|
|
private _timeoutSettings = new TimeoutSettings();
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(electronApplication: channels.ElectronApplicationChannel): ElectronApplication {
|
2020-07-13 21:46:59 -07:00
|
|
|
return (electronApplication as any)._object;
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ElectronApplicationInitializer) {
|
2020-07-27 10:21:39 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2021-05-02 22:45:06 -07:00
|
|
|
this._context = BrowserContext.from(initializer.context);
|
2021-05-13 14:10:52 -07:00
|
|
|
for (const page of this._context._pages)
|
|
|
|
this._onPage(page);
|
|
|
|
this._context.on(Events.BrowserContext.Page, page => this._onPage(page));
|
2020-07-29 17:26:59 -07:00
|
|
|
this._channel.on('close', () => this.emit(Events.ElectronApplication.Close));
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
2021-05-13 14:10:52 -07:00
|
|
|
_onPage(page: Page) {
|
|
|
|
this._windows.add(page);
|
|
|
|
this.emit(Events.ElectronApplication.Window, page);
|
|
|
|
page.once(Events.Page.Close, () => this._windows.delete(page));
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:43:26 -08:00
|
|
|
windows(): Page[] {
|
2020-12-26 20:25:18 -08:00
|
|
|
// TODO: add ElectronPage class inherting from Page.
|
2021-02-01 11:43:26 -08:00
|
|
|
return [...this._windows];
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:43:26 -08:00
|
|
|
async firstWindow(): Promise<Page> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('electronApplication.firstWindow', async (channel: channels.ElectronApplicationChannel) => {
|
2021-01-22 06:49:59 -08:00
|
|
|
if (this._windows.size)
|
|
|
|
return this._windows.values().next().value;
|
|
|
|
return this.waitForEvent('window');
|
|
|
|
});
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:47:14 +08:00
|
|
|
context(): BrowserContext {
|
|
|
|
return this._context! as BrowserContext;
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
await this._channel.close();
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
|
2020-07-16 22:38:52 -07:00
|
|
|
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
|
|
|
|
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
|
2021-02-19 18:12:33 -08:00
|
|
|
const waiter = Waiter.createForEvent(this, 'electronApplication', event);
|
2020-07-21 23:48:21 -07:00
|
|
|
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
|
2020-07-29 17:26:59 -07:00
|
|
|
if (event !== Events.ElectronApplication.Close)
|
|
|
|
waiter.rejectOnEvent(this, Events.ElectronApplication.Close, new Error('Electron application closed'));
|
2020-07-13 21:46:59 -07:00
|
|
|
const result = await waiter.waitForEvent(this, event, predicate as any);
|
|
|
|
waiter.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-02 22:45:06 -07:00
|
|
|
async browserWindow(page: Page): Promise<JSHandle<BrowserWindow>> {
|
|
|
|
return this._wrapApiCall('electronApplication.browserWindow', async (channel: channels.ElectronApplicationChannel) => {
|
|
|
|
const result = await channel.browserWindow({ page: page._channel });
|
|
|
|
return JSHandle.from(result.handle);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-26 20:25:18 -08:00
|
|
|
async evaluate<R, Arg>(pageFunction: structs.PageFunctionOn<ElectronAppType, Arg, R>, arg: Arg): Promise<R> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('electronApplication.evaluate', async (channel: channels.ElectronApplicationChannel) => {
|
|
|
|
const result = await channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
2021-01-22 06:49:59 -08:00
|
|
|
return parseResult(result.value);
|
|
|
|
});
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
2020-12-26 20:25:18 -08:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: structs.PageFunctionOn<ElectronAppType, Arg, R>, arg: Arg): Promise<structs.SmartHandle<R>> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('electronApplication.evaluateHandle', async (channel: channels.ElectronApplicationChannel) => {
|
|
|
|
const result = await channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
2021-01-22 06:49:59 -08:00
|
|
|
return JSHandle.from(result.handle) as any as structs.SmartHandle<R>;
|
|
|
|
});
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
}
|