2017-05-11 07:06:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-07-07 17:49:17 +00:00
|
|
|
let {Duplex} = require('stream');
|
2017-06-22 20:38:10 +00:00
|
|
|
let path = require('path');
|
|
|
|
let removeRecursive = require('rimraf').sync;
|
|
|
|
let Page = require('./Page');
|
|
|
|
let childProcess = require('child_process');
|
|
|
|
let Downloader = require('../utils/ChromiumDownloader');
|
|
|
|
let Connection = require('./Connection');
|
2017-07-11 15:00:21 +00:00
|
|
|
let readline = require('readline');
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
let CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile');
|
|
|
|
let browserId = 0;
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
let DEFAULT_ARGS = [
|
2017-06-21 20:51:06 +00:00
|
|
|
'--disable-background-timer-throttling',
|
|
|
|
'--no-first-run',
|
2017-07-11 15:00:21 +00:00
|
|
|
'--remote-debugging-port=0',
|
2017-05-11 07:06:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
class Browser {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {(!Object|undefined)} options
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
constructor(options) {
|
|
|
|
options = options || {};
|
|
|
|
++browserId;
|
|
|
|
this._userDataDir = CHROME_PROFILE_PATH + browserId;
|
2017-07-11 15:00:21 +00:00
|
|
|
this._remoteDebuggingPort = 0;
|
2017-06-21 20:51:06 +00:00
|
|
|
this._chromeArguments = DEFAULT_ARGS.concat([
|
|
|
|
`--user-data-dir=${this._userDataDir}`,
|
|
|
|
]);
|
|
|
|
if (typeof options.headless !== 'boolean' || options.headless) {
|
|
|
|
this._chromeArguments.push(...[
|
|
|
|
`--headless`,
|
|
|
|
`--disable-gpu`,
|
2017-07-11 01:58:29 +00:00
|
|
|
`--hide-scrollbars`,
|
2017-06-21 20:51:06 +00:00
|
|
|
]);
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
if (typeof options.executablePath === 'string') {
|
|
|
|
this._chromeExecutable = options.executablePath;
|
|
|
|
} else {
|
2017-06-22 20:38:10 +00:00
|
|
|
let chromiumRevision = require('../package.json').puppeteer.chromium_revision;
|
|
|
|
let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
|
2017-06-21 20:51:06 +00:00
|
|
|
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
|
|
|
|
this._chromeExecutable = revisionInfo.executablePath;
|
|
|
|
}
|
|
|
|
if (Array.isArray(options.args))
|
|
|
|
this._chromeArguments.push(...options.args);
|
|
|
|
this._terminated = false;
|
|
|
|
this._chromeProcess = null;
|
2017-07-07 01:20:01 +00:00
|
|
|
this._launchPromise = null;
|
2017-07-07 17:49:17 +00:00
|
|
|
|
|
|
|
this.stderr = new ProxyStream();
|
|
|
|
this.stdout = new ProxyStream();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:36:04 +00:00
|
|
|
* @return {!Promise<!Page>}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async newPage() {
|
|
|
|
await this._ensureChromeIsRunning();
|
|
|
|
if (!this._chromeProcess || this._terminated)
|
|
|
|
throw new Error('ERROR: this chrome instance is not alive any more!');
|
2017-06-22 20:38:10 +00:00
|
|
|
let client = await Connection.create(this._remoteDebuggingPort);
|
|
|
|
let page = await Page.create(client);
|
2017-06-21 20:51:06 +00:00
|
|
|
return page;
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:36:04 +00:00
|
|
|
* @param {!Page} page
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async closePage(page) {
|
|
|
|
if (!this._chromeProcess || this._terminated)
|
|
|
|
throw new Error('ERROR: this chrome instance is not running');
|
|
|
|
await page.close();
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:36:04 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async version() {
|
|
|
|
await this._ensureChromeIsRunning();
|
2017-06-22 20:38:10 +00:00
|
|
|
let version = await Connection.version(this._remoteDebuggingPort);
|
2017-06-21 20:51:06 +00:00
|
|
|
return version.Browser;
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
async _ensureChromeIsRunning() {
|
2017-07-07 01:20:01 +00:00
|
|
|
if (!this._launchPromise)
|
|
|
|
this._launchPromise = this._launchChrome();
|
|
|
|
return this._launchPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _launchChrome() {
|
2017-06-21 20:51:06 +00:00
|
|
|
this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {});
|
2017-06-22 20:38:10 +00:00
|
|
|
let stderr = '';
|
2017-06-21 20:51:06 +00:00
|
|
|
this._chromeProcess.stderr.on('data', data => stderr += data.toString('utf8'));
|
|
|
|
// Cleanup as processes exit.
|
2017-07-11 15:11:16 +00:00
|
|
|
const onProcessExit = () => this._chromeProcess.kill();
|
|
|
|
process.on('exit', onProcessExit);
|
2017-06-21 20:51:06 +00:00
|
|
|
this._chromeProcess.on('exit', () => {
|
|
|
|
this._terminated = true;
|
2017-07-11 15:11:16 +00:00
|
|
|
process.removeListener('exit', onProcessExit);
|
2017-06-21 20:51:06 +00:00
|
|
|
removeRecursive(this._userDataDir);
|
|
|
|
});
|
2017-07-07 17:49:17 +00:00
|
|
|
this._chromeProcess.stderr.pipe(this.stderr);
|
|
|
|
this._chromeProcess.stdout.pipe(this.stdout);
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-07-11 15:00:21 +00:00
|
|
|
this._remoteDebuggingPort = await waitForRemoteDebuggingPort(this._chromeProcess);
|
|
|
|
// Failed to connect to browser.
|
|
|
|
if (this._remoteDebuggingPort === -1) {
|
|
|
|
this._chromeProcess.kill();
|
|
|
|
throw new Error('Failed to connect to chrome!');
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
if (this._terminated)
|
|
|
|
throw new Error('Failed to launch chrome! ' + stderr);
|
|
|
|
}
|
2017-06-21 14:45:13 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
close() {
|
|
|
|
if (!this._chromeProcess)
|
|
|
|
return;
|
|
|
|
this._chromeProcess.kill();
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Browser;
|
|
|
|
|
2017-07-11 15:00:21 +00:00
|
|
|
function waitForRemoteDebuggingPort(chromeProcess) {
|
|
|
|
const rl = readline.createInterface({ input: chromeProcess.stderr });
|
2017-06-22 20:38:10 +00:00
|
|
|
let fulfill;
|
|
|
|
let promise = new Promise(x => fulfill = x);
|
2017-07-11 15:00:21 +00:00
|
|
|
rl.on('line', onLine);
|
|
|
|
rl.once('close', () => fulfill(-1));
|
2017-06-21 20:51:06 +00:00
|
|
|
return promise;
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-07-11 15:00:21 +00:00
|
|
|
/**
|
|
|
|
* @param {string} line
|
|
|
|
*/
|
|
|
|
function onLine(line) {
|
|
|
|
const match = line.match(/^DevTools listening on .*:([\d]+)$/);
|
|
|
|
if (!match)
|
|
|
|
return;
|
|
|
|
fulfill(Number.parseInt(match[1], 10));
|
|
|
|
rl.removeListener('line', onLine);
|
|
|
|
rl.close();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
2017-07-07 17:49:17 +00:00
|
|
|
|
|
|
|
class ProxyStream extends Duplex {
|
|
|
|
_read() { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?} chunk
|
|
|
|
* @param {string} encoding
|
|
|
|
* @param {function()} callback
|
|
|
|
*/
|
|
|
|
_write(chunk, encoding, callback) {
|
|
|
|
this.push(chunk, encoding);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|