2020-07-14 06:46:59 +02: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 { Dispatcher, DispatcherScope, lookupDispatcher } from './dispatcher';
|
2020-07-16 03:48:19 +02:00
|
|
|
import { Electron, ElectronApplication, ElectronEvents, ElectronPage } from '../../server/electron';
|
2020-07-21 21:44:30 +02:00
|
|
|
import { ElectronApplicationChannel, ElectronApplicationInitializer, PageChannel, JSHandleChannel, ElectronInitializer, ElectronChannel, SerializedArgument, ElectronLaunchParams, SerializedValue } from '../channels';
|
2020-07-14 06:46:59 +02:00
|
|
|
import { BrowserContextDispatcher } from './browserContextDispatcher';
|
|
|
|
|
import { BrowserContextBase } from '../../browserContext';
|
|
|
|
|
import { PageDispatcher } from './pageDispatcher';
|
2020-07-20 04:46:19 +02:00
|
|
|
import { parseArgument, serializeResult } from './jsHandleDispatcher';
|
2020-07-14 06:46:59 +02:00
|
|
|
import { createHandle } from './elementHandlerDispatcher';
|
2020-07-21 02:38:06 +02:00
|
|
|
import { envArrayToObject } from '../serializers';
|
2020-07-14 06:46:59 +02:00
|
|
|
|
|
|
|
|
export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer> implements ElectronChannel {
|
|
|
|
|
constructor(scope: DispatcherScope, electron: Electron) {
|
2020-07-22 19:37:21 +02:00
|
|
|
super(scope, electron, 'Electron', {}, true);
|
2020-07-14 06:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-21 02:38:06 +02:00
|
|
|
async launch(params: ElectronLaunchParams): Promise<{ electronApplication: ElectronApplicationChannel }> {
|
|
|
|
|
const options = {
|
|
|
|
|
...params,
|
|
|
|
|
env: params.env ? envArrayToObject(params.env) : undefined,
|
|
|
|
|
};
|
|
|
|
|
const electronApplication = await this._object.launch(params.executablePath, options);
|
2020-07-15 03:26:50 +02:00
|
|
|
return { electronApplication: new ElectronApplicationDispatcher(this._scope, electronApplication) };
|
2020-07-14 06:46:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
|
|
|
|
|
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
|
2020-07-22 19:37:21 +02:00
|
|
|
super(scope, electronApplication, 'ElectronApplication', {
|
2020-07-14 06:46:59 +02:00
|
|
|
context: new BrowserContextDispatcher(scope, electronApplication.context() as BrowserContextBase),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
electronApplication.on(ElectronEvents.ElectronApplication.Close, () => this._dispatchEvent('close'));
|
2020-07-16 03:48:19 +02:00
|
|
|
electronApplication.on(ElectronEvents.ElectronApplication.Window, (page: ElectronPage) => {
|
|
|
|
|
this._dispatchEvent('window', {
|
|
|
|
|
page: lookupDispatcher<PageDispatcher>(page),
|
|
|
|
|
browserWindow: createHandle(this._scope, page.browserWindow),
|
|
|
|
|
});
|
2020-07-15 03:26:50 +02:00
|
|
|
});
|
2020-07-14 06:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
async newBrowserWindow(params: { arg: SerializedArgument }): Promise<{ page: PageChannel }> {
|
2020-07-15 03:26:50 +02:00
|
|
|
const page = await this._object.newBrowserWindow(parseArgument(params.arg));
|
|
|
|
|
return { page: lookupDispatcher<PageChannel>(page) };
|
2020-07-14 06:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
async evaluateExpression(params: { expression: string, isFunction: boolean, arg: SerializedArgument }): Promise<{ value: SerializedValue }> {
|
2020-07-14 06:46:59 +02:00
|
|
|
const handle = this._object._nodeElectronHandle!;
|
2020-07-20 04:46:19 +02:00
|
|
|
return { value: serializeResult(await handle._evaluateExpression(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) };
|
2020-07-14 06:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
async evaluateExpressionHandle(params: { expression: string, isFunction: boolean, arg: SerializedArgument }): Promise<{ handle: JSHandleChannel }> {
|
2020-07-14 06:46:59 +02:00
|
|
|
const handle = this._object._nodeElectronHandle!;
|
|
|
|
|
const result = await handle._evaluateExpression(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg));
|
2020-07-15 03:26:50 +02:00
|
|
|
return { handle: createHandle(this._scope, result) };
|
2020-07-14 06:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
|
|
|
|
await this._object.close();
|
|
|
|
|
}
|
|
|
|
|
}
|