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 .
* /
2020-01-07 13:58:23 -08:00
import * as fs from 'fs' ;
import * as os from 'os' ;
import * as path from 'path' ;
import * as util from 'util' ;
2020-01-09 14:49:22 -08:00
import { BrowserFetcher , BrowserFetcherOptions } from '../server/browserFetcher' ;
2020-01-07 12:53:06 -08:00
import { DeviceDescriptors } from '../deviceDescriptors' ;
import * as types from '../types' ;
2019-12-19 16:53:24 -08:00
import { assert } from '../helper' ;
2020-01-23 14:40:37 -08:00
import { CRBrowser } from '../chromium/crBrowser' ;
2020-01-07 13:58:23 -08:00
import * as platform from '../platform' ;
import { TimeoutError } from '../errors' ;
2020-01-07 15:27:45 -08:00
import { launchProcess , waitForLine } from '../server/processLauncher' ;
2020-01-23 17:45:31 -08:00
import { kBrowserCloseMessageId } from '../chromium/crConnection' ;
2020-01-07 15:27:45 -08:00
import { PipeTransport } from './pipeTransport' ;
2020-01-24 14:49:47 -08:00
import { LaunchOptions , BrowserArgOptions , BrowserType } from './browserType' ;
2020-02-04 19:41:38 -08:00
import { ConnectOptions } from '../browser' ;
2020-01-23 14:40:37 -08:00
import { BrowserApp } from './browserApp' ;
2020-01-24 15:58:04 -08:00
import { Events } from '../events' ;
2020-02-04 19:41:38 -08:00
import { ConnectionTransport } from '../transport' ;
2020-01-07 13:58:23 -08:00
2020-01-24 14:49:47 -08:00
export class Chromium implements BrowserType {
2019-12-19 16:53:24 -08:00
private _projectRoot : string ;
readonly _revision : string ;
constructor ( projectRoot : string , preferredRevision : string ) {
this . _projectRoot = projectRoot ;
this . _revision = preferredRevision ;
}
2020-01-28 18:09:07 -08:00
name() {
return 'chromium' ;
}
2020-02-04 19:41:38 -08:00
async launch ( options? : LaunchOptions & { slowMo? : number } ) : Promise < CRBrowser > {
const { browserApp , transport } = await this . _launchBrowserApp ( options , false ) ;
const browser = await CRBrowser . connect ( transport ! , options && options . slowMo ) ;
2020-01-23 08:51:43 -08:00
// Hack: for typical launch scenario, ensure that close waits for actual process termination.
2020-02-04 19:41:38 -08:00
browser . close = ( ) = > browserApp . close ( ) ;
( browser as any ) [ '__app__' ] = browserApp ;
2020-01-23 08:51:43 -08:00
return browser ;
2019-12-19 16:53:24 -08:00
}
2020-02-04 19:41:38 -08:00
async launchBrowserApp ( options? : LaunchOptions ) : Promise < BrowserApp > {
return ( await this . _launchBrowserApp ( options , true ) ) . browserApp ;
}
async _launchBrowserApp ( options : LaunchOptions = { } , isServer : boolean ) : Promise < { browserApp : BrowserApp , transport? : ConnectionTransport } > {
2020-01-07 13:58:23 -08:00
const {
ignoreDefaultArgs = false ,
args = [ ] ,
dumpio = false ,
executablePath = null ,
env = process . env ,
handleSIGINT = true ,
handleSIGTERM = true ,
handleSIGHUP = true ,
timeout = 30000
} = options ;
const chromeArguments = [ ] ;
if ( ! ignoreDefaultArgs )
chromeArguments . push ( . . . this . defaultArgs ( options ) ) ;
else if ( Array . isArray ( ignoreDefaultArgs ) )
chromeArguments . push ( . . . this . defaultArgs ( options ) . filter ( arg = > ignoreDefaultArgs . indexOf ( arg ) === - 1 ) ) ;
else
chromeArguments . push ( . . . args ) ;
let temporaryUserDataDir : string | null = null ;
if ( ! chromeArguments . some ( argument = > argument . startsWith ( '--remote-debugging-' ) ) )
2020-02-04 19:41:38 -08:00
chromeArguments . push ( isServer ? '--remote-debugging-port=0' : '--remote-debugging-pipe' ) ;
2020-01-07 13:58:23 -08:00
if ( ! chromeArguments . some ( arg = > arg . startsWith ( '--user-data-dir' ) ) ) {
2020-01-13 10:13:28 -08:00
temporaryUserDataDir = await mkdtempAsync ( CHROMIUM_PROFILE_PATH ) ;
2020-01-07 13:58:23 -08:00
chromeArguments . push ( ` --user-data-dir= ${ temporaryUserDataDir } ` ) ;
}
let chromeExecutable = executablePath ;
if ( ! executablePath ) {
const { missingText , executablePath } = this . _resolveExecutablePath ( ) ;
if ( missingText )
throw new Error ( missingText ) ;
chromeExecutable = executablePath ;
}
const usePipe = chromeArguments . includes ( '--remote-debugging-pipe' ) ;
2020-02-04 19:41:38 -08:00
if ( usePipe && isServer )
throw new Error ( ` Argument "--remote-debugging-pipe" is not compatible with the launchBrowserApp. ` ) ;
2020-01-07 13:58:23 -08:00
2020-01-24 15:58:04 -08:00
let browserApp : BrowserApp | undefined = undefined ;
2020-01-08 13:55:38 -08:00
const { launchedProcess , gracefullyClose } = await launchProcess ( {
2020-01-13 13:33:25 -08:00
executablePath : chromeExecutable ! ,
2020-01-07 13:58:23 -08:00
args : chromeArguments ,
env ,
handleSIGINT ,
handleSIGTERM ,
handleSIGHUP ,
dumpio ,
pipe : usePipe ,
2020-01-13 13:33:25 -08:00
tempDir : temporaryUserDataDir || undefined ,
2020-01-08 13:55:38 -08:00
attemptToGracefullyClose : async ( ) = > {
2020-01-24 15:58:04 -08:00
if ( ! browserApp )
2020-01-08 13:55:38 -08:00
return Promise . reject ( ) ;
// We try to gracefully close to prevent crash reporting and core dumps.
// Note that it's fine to reuse the pipe transport, since
2020-01-23 17:45:31 -08:00
// our connection ignores kBrowserCloseMessageId.
2020-02-04 19:41:38 -08:00
const t = transport || await platform . createWebSocketTransport ( browserWSEndpoint ! ) ;
2020-01-23 17:45:31 -08:00
const message = { method : 'Browser.close' , id : kBrowserCloseMessageId } ;
2020-02-04 19:41:38 -08:00
t . send ( JSON . stringify ( message ) ) ;
2020-01-08 13:55:38 -08:00
} ,
2020-01-28 13:07:53 -08:00
onkill : ( exitCode , signal ) = > {
2020-01-24 15:58:04 -08:00
if ( browserApp )
2020-01-28 13:07:53 -08:00
browserApp . emit ( Events . BrowserApp . Close , exitCode , signal ) ;
2020-01-24 15:58:04 -08:00
} ,
2020-01-07 13:58:23 -08:00
} ) ;
2020-02-04 19:41:38 -08:00
let transport : ConnectionTransport | undefined ;
let browserWSEndpoint : string | null ;
if ( isServer ) {
2020-01-13 10:13:28 -08:00
const timeoutError = new TimeoutError ( ` Timed out after ${ timeout } ms while trying to connect to Chromium! The only Chromium revision guaranteed to work is r ${ this . _revision } ` ) ;
2020-01-08 13:55:38 -08:00
const match = await waitForLine ( launchedProcess , launchedProcess . stderr , /^DevTools listening on (ws:\/\/.*)$/ , timeout , timeoutError ) ;
2020-02-04 19:41:38 -08:00
browserWSEndpoint = match [ 1 ] ;
2020-01-08 13:55:38 -08:00
} else {
2020-02-04 19:41:38 -08:00
transport = new PipeTransport ( launchedProcess . stdio [ 3 ] as NodeJS . WritableStream , launchedProcess . stdio [ 4 ] as NodeJS . ReadableStream ) ;
browserWSEndpoint = null ;
2020-01-07 13:58:23 -08:00
}
2020-02-04 19:41:38 -08:00
browserApp = new BrowserApp ( launchedProcess , gracefullyClose , browserWSEndpoint ) ;
return { browserApp , transport } ;
2019-12-19 16:53:24 -08:00
}
2020-02-04 19:41:38 -08:00
async connect ( options : ConnectOptions ) : Promise < CRBrowser > {
const transport = await platform . createWebSocketTransport ( options . wsEndpoint ) ;
return CRBrowser . connect ( transport , options . slowMo ) ;
2019-12-19 16:53:24 -08:00
}
executablePath ( ) : string {
2020-01-07 13:58:23 -08:00
return this . _resolveExecutablePath ( ) . executablePath ;
2019-12-19 16:53:24 -08:00
}
2020-01-07 12:53:06 -08:00
get devices ( ) : types . Devices {
return DeviceDescriptors ;
2019-12-19 16:53:24 -08:00
}
2020-01-09 14:49:22 -08:00
get errors ( ) : { TimeoutError : typeof TimeoutError } {
return { TimeoutError } ;
2019-12-19 16:53:24 -08:00
}
2020-01-24 14:49:47 -08:00
defaultArgs ( options : BrowserArgOptions = { } ) : string [ ] {
2020-01-07 13:58:23 -08:00
const {
devtools = false ,
headless = ! devtools ,
args = [ ] ,
userDataDir = null
} = options ;
const chromeArguments = [ . . . DEFAULT_ARGS ] ;
if ( userDataDir )
chromeArguments . push ( ` --user-data-dir= ${ userDataDir } ` ) ;
if ( devtools )
chromeArguments . push ( '--auto-open-devtools-for-tabs' ) ;
if ( headless ) {
chromeArguments . push (
'--headless' ,
'--hide-scrollbars' ,
'--mute-audio'
) ;
}
if ( args . every ( arg = > arg . startsWith ( '-' ) ) )
chromeArguments . push ( 'about:blank' ) ;
chromeArguments . push ( . . . args ) ;
return chromeArguments ;
2019-12-19 16:53:24 -08:00
}
2020-01-14 10:07:26 -08:00
_createBrowserFetcher ( options : BrowserFetcherOptions = { } ) : BrowserFetcher {
2020-01-07 13:58:23 -08:00
const downloadURLs = {
linux : '%s/chromium-browser-snapshots/Linux_x64/%d/%s.zip' ,
mac : '%s/chromium-browser-snapshots/Mac/%d/%s.zip' ,
win32 : '%s/chromium-browser-snapshots/Win/%d/%s.zip' ,
win64 : '%s/chromium-browser-snapshots/Win_x64/%d/%s.zip' ,
} ;
const defaultOptions = {
path : path.join ( this . _projectRoot , '.local-chromium' ) ,
host : 'https://storage.googleapis.com' ,
platform : ( ( ) = > {
const platform = os . platform ( ) ;
if ( platform === 'darwin' )
return 'mac' ;
if ( platform === 'linux' )
return 'linux' ;
if ( platform === 'win32' )
return os . arch ( ) === 'x64' ? 'win64' : 'win32' ;
return platform ;
} ) ( )
} ;
options = {
. . . defaultOptions ,
. . . options ,
} ;
2020-01-13 13:33:25 -08:00
assert ( ! ! ( downloadURLs as any ) [ options . platform ! ] , 'Unsupported platform: ' + options . platform ) ;
2020-01-07 13:58:23 -08:00
2020-01-13 13:33:25 -08:00
return new BrowserFetcher ( options . path ! , options . platform ! , this . _revision , ( platform : string , revision : string ) = > {
2020-01-07 13:58:23 -08:00
let archiveName = '' ;
let executablePath = '' ;
if ( platform === 'linux' ) {
archiveName = 'chrome-linux' ;
executablePath = path . join ( archiveName , 'chrome' ) ;
} else if ( platform === 'mac' ) {
archiveName = 'chrome-mac' ;
executablePath = path . join ( archiveName , 'Chromium.app' , 'Contents' , 'MacOS' , 'Chromium' ) ;
} else if ( platform === 'win32' || platform === 'win64' ) {
// Windows archive name changed at r591479.
archiveName = parseInt ( revision , 10 ) > 591479 ? 'chrome-win' : 'chrome-win32' ;
executablePath = path . join ( archiveName , 'chrome.exe' ) ;
}
return {
downloadUrl : util.format ( ( downloadURLs as any ) [ platform ] , options . host , revision , archiveName ) ,
executablePath
} ;
} ) ;
}
_resolveExecutablePath ( ) : { executablePath : string ; missingText : string | null ; } {
2020-01-14 10:07:26 -08:00
const browserFetcher = this . _createBrowserFetcher ( ) ;
2020-01-09 14:49:22 -08:00
const revisionInfo = browserFetcher . revisionInfo ( ) ;
2020-01-31 17:23:39 -08:00
const missingText = ! revisionInfo . local ? ` Chromium revision is not downloaded. Run "npm install" ` : null ;
2020-01-07 13:58:23 -08:00
return { executablePath : revisionInfo.executablePath , missingText } ;
2019-12-19 16:53:24 -08:00
}
}
2020-01-07 13:58:23 -08:00
const mkdtempAsync = platform . promisify ( fs . mkdtemp ) ;
2020-01-13 10:13:28 -08:00
const CHROMIUM_PROFILE_PATH = path . join ( os . tmpdir ( ) , 'playwright_dev_profile-' ) ;
2020-01-07 13:58:23 -08:00
const DEFAULT_ARGS = [
'--disable-background-networking' ,
'--enable-features=NetworkService,NetworkServiceInProcess' ,
'--disable-background-timer-throttling' ,
'--disable-backgrounding-occluded-windows' ,
'--disable-breakpad' ,
'--disable-client-side-phishing-detection' ,
'--disable-component-extensions-with-background-pages' ,
'--disable-default-apps' ,
'--disable-dev-shm-usage' ,
'--disable-extensions' ,
// BlinkGenPropertyTrees disabled due to crbug.com/937609
'--disable-features=TranslateUI,BlinkGenPropertyTrees' ,
'--disable-hang-monitor' ,
'--disable-ipc-flooding-protection' ,
'--disable-popup-blocking' ,
'--disable-prompt-on-repost' ,
'--disable-renderer-backgrounding' ,
'--disable-sync' ,
'--force-color-profile=srgb' ,
'--metrics-recording-only' ,
'--no-first-run' ,
'--enable-automation' ,
'--password-store=basic' ,
'--use-mock-keychain' ,
] ;