2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications 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 os from 'os';
|
2020-05-31 09:28:57 -07:00
|
|
|
import * as fs from 'fs';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as path from 'path';
|
2020-03-28 10:14:59 -07:00
|
|
|
import * as ws from 'ws';
|
2020-03-02 13:51:32 -08:00
|
|
|
import { FFBrowser } from '../firefox/ffBrowser';
|
|
|
|
import { kBrowserCloseMessageId } from '../firefox/ffConnection';
|
2020-06-25 08:30:56 -07:00
|
|
|
import { BrowserTypeBase, LaunchNonPersistentOptions } from './browserType';
|
2020-05-22 07:03:42 -07:00
|
|
|
import { Env } from './processLauncher';
|
2020-06-10 16:33:27 -07:00
|
|
|
import { ConnectionTransport, ProtocolResponse, ProtocolRequest } from '../transport';
|
2020-06-16 17:11:19 -07:00
|
|
|
import { Logger } from '../logger';
|
2020-05-21 19:16:13 -07:00
|
|
|
import { BrowserOptions } from '../browser';
|
2020-05-22 07:03:42 -07:00
|
|
|
import { BrowserDescriptor } from '../install/browserPaths';
|
2020-06-10 16:33:27 -07:00
|
|
|
import { WebSocketServer } from './webSocketServer';
|
2020-02-11 19:10:02 -08:00
|
|
|
|
2020-05-20 16:30:04 -07:00
|
|
|
export class Firefox extends BrowserTypeBase {
|
2020-05-22 07:03:42 -07:00
|
|
|
constructor(packagePath: string, browser: BrowserDescriptor) {
|
2020-06-04 16:40:07 -07:00
|
|
|
const webSocketRegex = /^Juggler listening on (ws:\/\/.*)$/;
|
|
|
|
super(packagePath, browser, { webSocketRegex, stream: 'stdout' });
|
2020-05-22 07:03:42 -07:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:30:04 -07:00
|
|
|
_connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<FFBrowser> {
|
|
|
|
return FFBrowser.connect(transport, options);
|
2020-02-05 12:41:55 -08:00
|
|
|
}
|
|
|
|
|
2020-05-22 07:03:42 -07:00
|
|
|
_amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env {
|
|
|
|
return os.platform() === 'linux' ? {
|
|
|
|
...env,
|
|
|
|
// On linux Juggler ships the libstdc++ it was linked against.
|
|
|
|
LD_LIBRARY_PATH: `${path.dirname(executable)}:${process.env.LD_LIBRARY_PATH}`,
|
|
|
|
} : env;
|
|
|
|
}
|
2020-03-28 10:14:59 -07:00
|
|
|
|
2020-05-22 07:03:42 -07:00
|
|
|
_attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void {
|
|
|
|
const message = { method: 'Browser.close', params: {}, id: kBrowserCloseMessageId };
|
|
|
|
transport.send(message);
|
|
|
|
}
|
2020-05-21 09:43:10 -07:00
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
_startWebSocketServer(transport: ConnectionTransport, logger: Logger, port: number): WebSocketServer {
|
2020-06-10 16:33:27 -07:00
|
|
|
return startWebSocketServer(transport, logger, port);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-06-25 08:30:56 -07:00
|
|
|
_defaultArgs(options: LaunchNonPersistentOptions, isPersistent: boolean, userDataDir: string): string[] {
|
2020-06-10 20:48:54 -07:00
|
|
|
const { args = [], proxy, devtools, headless } = options;
|
2020-01-24 14:49:47 -08:00
|
|
|
if (devtools)
|
2020-03-24 22:42:20 +01:00
|
|
|
console.warn('devtools parameter is not supported as a launch argument in Firefox. You can launch the devtools window manually.');
|
2020-02-05 16:36:36 -08:00
|
|
|
const userDataDirArg = args.find(arg => arg.startsWith('-profile') || arg.startsWith('--profile'));
|
|
|
|
if (userDataDirArg)
|
|
|
|
throw new Error('Pass userDataDir parameter instead of specifying -profile argument');
|
|
|
|
if (args.find(arg => arg.startsWith('-juggler')))
|
|
|
|
throw new Error('Use the port parameter instead of -juggler argument');
|
2020-06-05 13:50:15 -07:00
|
|
|
if (proxy) {
|
|
|
|
options.firefoxUserPrefs = options.firefoxUserPrefs || {};
|
|
|
|
options.firefoxUserPrefs['network.proxy.type'] = 1;
|
|
|
|
const proxyServer = new URL(proxy.server);
|
|
|
|
const isSocks = proxyServer.protocol === 'socks5:';
|
|
|
|
if (isSocks) {
|
|
|
|
options.firefoxUserPrefs['network.proxy.socks'] = proxyServer.hostname;
|
|
|
|
options.firefoxUserPrefs['network.proxy.socks_port'] = parseInt(proxyServer.port, 10);
|
|
|
|
} else {
|
|
|
|
options.firefoxUserPrefs['network.proxy.http'] = proxyServer.hostname;
|
|
|
|
options.firefoxUserPrefs['network.proxy.http_port'] = parseInt(proxyServer.port, 10);
|
|
|
|
options.firefoxUserPrefs['network.proxy.ssl'] = proxyServer.hostname;
|
|
|
|
options.firefoxUserPrefs['network.proxy.ssl_port'] = parseInt(proxyServer.port, 10);
|
|
|
|
}
|
|
|
|
if (proxy.bypass)
|
|
|
|
options.firefoxUserPrefs['network.proxy.no_proxies_on'] = proxy.bypass;
|
|
|
|
}
|
2020-05-31 09:28:57 -07:00
|
|
|
if (options.firefoxUserPrefs) {
|
|
|
|
const lines: string[] = [];
|
|
|
|
for (const [name, value] of Object.entries(options.firefoxUserPrefs))
|
|
|
|
lines.push(`user_pref(${JSON.stringify(name)}, ${JSON.stringify(value)});`);
|
|
|
|
fs.writeFileSync(path.join(userDataDir, 'user.js'), lines.join('\n'));
|
|
|
|
}
|
2020-02-05 16:36:36 -08:00
|
|
|
const firefoxArguments = ['-no-remote'];
|
2020-02-10 20:35:58 -08:00
|
|
|
if (headless) {
|
2020-01-07 16:13:49 -08:00
|
|
|
firefoxArguments.push('-headless');
|
2020-02-10 20:35:58 -08:00
|
|
|
} else {
|
2020-01-21 17:22:48 -08:00
|
|
|
firefoxArguments.push('-wait-for-browser');
|
2020-02-10 20:35:58 -08:00
|
|
|
firefoxArguments.push('-foreground');
|
|
|
|
}
|
2020-02-05 16:36:36 -08:00
|
|
|
firefoxArguments.push(`-profile`, userDataDir);
|
2020-05-22 07:03:42 -07:00
|
|
|
firefoxArguments.push('-juggler', '0');
|
2020-01-07 16:13:49 -08:00
|
|
|
firefoxArguments.push(...args);
|
2020-05-22 16:06:00 -07:00
|
|
|
if (isPersistent)
|
2020-05-10 15:23:53 -07:00
|
|
|
firefoxArguments.push('about:blank');
|
|
|
|
else
|
2020-04-10 14:12:30 -07:00
|
|
|
firefoxArguments.push('-silent');
|
2020-01-07 16:13:49 -08:00
|
|
|
return firefoxArguments;
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:33:27 -07:00
|
|
|
type SessionData = {
|
|
|
|
socket: ws,
|
|
|
|
};
|
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
function startWebSocketServer(transport: ConnectionTransport, logger: Logger, port: number): WebSocketServer {
|
2020-03-28 10:14:59 -07:00
|
|
|
const pendingBrowserContextCreations = new Set<number>();
|
|
|
|
const pendingBrowserContextDeletions = new Map<number, string>();
|
|
|
|
const browserContextIds = new Map<string, ws>();
|
2020-06-10 16:33:27 -07:00
|
|
|
const sessionToData = new Map<string, SessionData>();
|
|
|
|
|
|
|
|
function removeSession(sessionId: string): SessionData | undefined {
|
|
|
|
const data = sessionToData.get(sessionId);
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
sessionToData.delete(sessionId);
|
|
|
|
return data;
|
|
|
|
}
|
2020-03-28 10:14:59 -07:00
|
|
|
|
2020-06-10 16:33:27 -07:00
|
|
|
const server = new WebSocketServer(transport, logger, port, {
|
|
|
|
onBrowserResponse(seqNum: number, source: ws, message: ProtocolResponse) {
|
2020-03-28 10:14:59 -07:00
|
|
|
// Process command response.
|
2020-06-10 16:33:27 -07:00
|
|
|
if (source.readyState === ws.CLOSING || source.readyState === ws.CLOSED) {
|
|
|
|
if (pendingBrowserContextCreations.has(seqNum))
|
|
|
|
server.sendMessageToBrowserOneWay('Browser.removeBrowserContext', { browserContextId: message.result.browserContextId });
|
2020-03-28 10:14:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pendingBrowserContextCreations.has(seqNum)) {
|
|
|
|
// Browser.createBrowserContext response -> establish context attribution.
|
2020-06-10 16:33:27 -07:00
|
|
|
browserContextIds.set(message.result.browserContextId, source);
|
2020-03-28 10:14:59 -07:00
|
|
|
pendingBrowserContextCreations.delete(seqNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletedContextId = pendingBrowserContextDeletions.get(seqNum);
|
|
|
|
if (deletedContextId) {
|
|
|
|
// Browser.removeBrowserContext response -> remove context attribution.
|
|
|
|
browserContextIds.delete(deletedContextId);
|
|
|
|
pendingBrowserContextDeletions.delete(seqNum);
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:33:27 -07:00
|
|
|
source.send(JSON.stringify(message));
|
2020-03-28 10:14:59 -07:00
|
|
|
return;
|
2020-06-10 16:33:27 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
onBrowserNotification(message: ProtocolResponse) {
|
|
|
|
// Process notification response.
|
|
|
|
const { method, params, sessionId } = message;
|
|
|
|
if (sessionId) {
|
|
|
|
const data = sessionToData.get(sessionId);
|
|
|
|
if (!data || data.socket.readyState === ws.CLOSING) {
|
|
|
|
// Drop unattributed messages on the floor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.socket.send(JSON.stringify(message));
|
2020-03-28 10:14:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-06-10 16:33:27 -07:00
|
|
|
if (method === 'Browser.attachedToTarget') {
|
|
|
|
const socket = browserContextIds.get(params.targetInfo.browserContextId);
|
|
|
|
if (!socket || socket.readyState === ws.CLOSING) {
|
|
|
|
// Drop unattributed messages on the floor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sessionToData.set(params.sessionId, { socket });
|
|
|
|
socket.send(JSON.stringify(message));
|
2020-03-28 10:14:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-06-10 16:33:27 -07:00
|
|
|
if (method === 'Browser.detachedFromTarget') {
|
|
|
|
const data = removeSession(params.sessionId);
|
|
|
|
if (data && data.socket.readyState !== ws.CLOSING)
|
|
|
|
data.socket.send(JSON.stringify(message));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2020-03-28 10:14:59 -07:00
|
|
|
|
2020-06-10 16:33:27 -07:00
|
|
|
onClientAttached() {},
|
2020-03-28 10:14:59 -07:00
|
|
|
|
2020-06-10 16:33:27 -07:00
|
|
|
onClientRequest(socket: ws, message: ProtocolRequest) {
|
|
|
|
const { method, params } = message;
|
|
|
|
const seqNum = server.sendMessageToBrowser(message, socket);
|
2020-03-28 10:14:59 -07:00
|
|
|
if (method === 'Browser.createBrowserContext')
|
|
|
|
pendingBrowserContextCreations.add(seqNum);
|
|
|
|
if (method === 'Browser.removeBrowserContext')
|
|
|
|
pendingBrowserContextDeletions.set(seqNum, params.browserContextId);
|
2020-06-10 16:33:27 -07:00
|
|
|
},
|
2020-03-28 10:14:59 -07:00
|
|
|
|
2020-06-10 16:33:27 -07:00
|
|
|
onClientDetached(socket: ws) {
|
2020-03-28 10:14:59 -07:00
|
|
|
for (const [browserContextId, s] of browserContextIds) {
|
|
|
|
if (s === socket) {
|
2020-06-10 16:33:27 -07:00
|
|
|
server.sendMessageToBrowserOneWay('Browser.removeBrowserContext', { browserContextId });
|
2020-03-28 10:14:59 -07:00
|
|
|
browserContextIds.delete(browserContextId);
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 16:33:27 -07:00
|
|
|
}
|
2020-03-28 10:14:59 -07:00
|
|
|
});
|
2020-06-10 16:33:27 -07:00
|
|
|
return server;
|
2020-03-28 10:14:59 -07:00
|
|
|
}
|