2023-04-04 13:29:21 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2023 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.
|
|
|
|
*/
|
|
|
|
|
2022-10-21 13:09:21 +00:00
|
|
|
import {mkdtemp} from 'fs/promises';
|
2022-06-22 22:13:39 +00:00
|
|
|
import path from 'path';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-04-04 13:29:21 +00:00
|
|
|
import {
|
|
|
|
computeSystemExecutablePath,
|
|
|
|
Browser as SupportedBrowsers,
|
|
|
|
ChromeReleaseChannel as BrowsersChromeReleaseChannel,
|
|
|
|
} from '@puppeteer/browsers';
|
|
|
|
|
|
|
|
import {debugError} from '../common/util.js';
|
2023-04-21 12:07:10 +00:00
|
|
|
import {Browser} from '../puppeteer-core.js';
|
2022-10-05 12:17:03 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2022-06-22 22:13:39 +00:00
|
|
|
import {
|
|
|
|
BrowserLaunchArgumentOptions,
|
|
|
|
ChromeReleaseChannel,
|
|
|
|
PuppeteerNodeLaunchOptions,
|
|
|
|
} from './LaunchOptions.js';
|
2023-04-04 13:29:21 +00:00
|
|
|
import {ProductLauncher, ResolvedLaunchArgs} from './ProductLauncher.js';
|
2022-10-21 13:09:21 +00:00
|
|
|
import {PuppeteerNode} from './PuppeteerNode.js';
|
2023-04-21 07:45:04 +00:00
|
|
|
import {rm} from './util/fs.js';
|
2022-06-22 22:13:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-10-21 13:09:21 +00:00
|
|
|
export class ChromeLauncher extends ProductLauncher {
|
|
|
|
constructor(puppeteer: PuppeteerNode) {
|
|
|
|
super(puppeteer, 'chrome');
|
2022-06-22 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 12:07:10 +00:00
|
|
|
override launch(options: PuppeteerNodeLaunchOptions = {}): Promise<Browser> {
|
|
|
|
const headless = options.headless ?? true;
|
|
|
|
if (
|
|
|
|
headless === true &&
|
2023-04-25 14:30:46 +00:00
|
|
|
(!this.puppeteer.configuration.logLevel ||
|
|
|
|
this.puppeteer.configuration.logLevel === 'warn') &&
|
|
|
|
!Boolean(process.env['PUPPETEER_DISABLE_HEADLESS_WARNING'])
|
2023-04-21 12:07:10 +00:00
|
|
|
) {
|
|
|
|
console.warn(
|
|
|
|
[
|
|
|
|
'\x1B[1m\x1B[43m\x1B[30m',
|
|
|
|
'Puppeteer old Headless deprecation warning:\x1B[0m\x1B[33m',
|
|
|
|
' In the near feature `headless: true` will default to the new Headless mode',
|
|
|
|
' for Chrome instead of the old Headless implementation. For more',
|
|
|
|
' information, please see https://developer.chrome.com/articles/new-headless/.',
|
|
|
|
' Consider opting in early by passing `headless: "new"` to `puppeteer.launch()`',
|
|
|
|
' If you encounter any bugs, please report them to https://github.com/puppeteer/puppeteer/issues/new/choose.\x1B[0m\n',
|
|
|
|
].join('\n ')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.launch(options);
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:29:21 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
override async computeLaunchArguments(
|
2022-10-21 13:09:21 +00:00
|
|
|
options: PuppeteerNodeLaunchOptions = {}
|
2023-04-04 13:29:21 +00:00
|
|
|
): Promise<ResolvedLaunchArgs> {
|
2022-06-22 22:13:39 +00:00
|
|
|
const {
|
|
|
|
ignoreDefaultArgs = false,
|
|
|
|
args = [],
|
|
|
|
pipe = false,
|
|
|
|
debuggingPort,
|
2023-04-04 13:29:21 +00:00
|
|
|
channel,
|
|
|
|
executablePath,
|
2022-06-22 22:13:39 +00:00
|
|
|
} = options;
|
|
|
|
|
|
|
|
const chromeArguments = [];
|
|
|
|
if (!ignoreDefaultArgs) {
|
|
|
|
chromeArguments.push(...this.defaultArgs(options));
|
|
|
|
} else if (Array.isArray(ignoreDefaultArgs)) {
|
|
|
|
chromeArguments.push(
|
|
|
|
...this.defaultArgs(options).filter(arg => {
|
|
|
|
return !ignoreDefaultArgs.includes(arg);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
chromeArguments.push(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!chromeArguments.some(argument => {
|
|
|
|
return argument.startsWith('--remote-debugging-');
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
if (pipe) {
|
|
|
|
assert(
|
|
|
|
!debuggingPort,
|
|
|
|
'Browser should be launched with either pipe or debugging port - not both.'
|
|
|
|
);
|
|
|
|
chromeArguments.push('--remote-debugging-pipe');
|
|
|
|
} else {
|
|
|
|
chromeArguments.push(`--remote-debugging-port=${debuggingPort || 0}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 05:40:30 +00:00
|
|
|
let isTempUserDataDir = false;
|
2022-06-22 22:13:39 +00:00
|
|
|
|
|
|
|
// Check for the user data dir argument, which will always be set even
|
|
|
|
// with a custom directory specified via the userDataDir option.
|
|
|
|
let userDataDirIndex = chromeArguments.findIndex(arg => {
|
|
|
|
return arg.startsWith('--user-data-dir');
|
|
|
|
});
|
|
|
|
if (userDataDirIndex < 0) {
|
2022-06-27 05:40:30 +00:00
|
|
|
isTempUserDataDir = true;
|
2022-06-22 22:13:39 +00:00
|
|
|
chromeArguments.push(
|
2022-10-21 13:09:21 +00:00
|
|
|
`--user-data-dir=${await mkdtemp(this.getProfilePath())}`
|
2022-06-22 22:13:39 +00:00
|
|
|
);
|
|
|
|
userDataDirIndex = chromeArguments.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const userDataDir = chromeArguments[userDataDirIndex]!.split('=', 2)[1];
|
|
|
|
assert(typeof userDataDir === 'string', '`--user-data-dir` is malformed');
|
|
|
|
|
|
|
|
let chromeExecutable = executablePath;
|
2022-10-21 13:09:21 +00:00
|
|
|
if (!chromeExecutable) {
|
2022-06-22 22:13:39 +00:00
|
|
|
assert(
|
2022-10-21 13:09:21 +00:00
|
|
|
channel || !this.puppeteer._isPuppeteerCore,
|
|
|
|
`An \`executablePath\` or \`channel\` must be specified for \`puppeteer-core\``
|
2022-06-22 22:13:39 +00:00
|
|
|
);
|
2022-10-21 13:09:21 +00:00
|
|
|
chromeExecutable = this.executablePath(channel);
|
2022-06-22 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:29:21 +00:00
|
|
|
return {
|
|
|
|
executablePath: chromeExecutable,
|
|
|
|
args: chromeArguments,
|
|
|
|
isTempUserDataDir,
|
2022-06-22 22:13:39 +00:00
|
|
|
userDataDir,
|
2023-04-04 13:29:21 +00:00
|
|
|
};
|
|
|
|
}
|
2022-06-22 22:13:39 +00:00
|
|
|
|
2023-04-04 13:29:21 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
override async cleanUserDataDir(
|
|
|
|
path: string,
|
|
|
|
opts: {isTemp: boolean}
|
|
|
|
): Promise<void> {
|
|
|
|
if (opts.isTemp) {
|
2022-06-22 22:13:39 +00:00
|
|
|
try {
|
2023-04-04 13:29:21 +00:00
|
|
|
await rm(path);
|
2022-06-22 22:13:39 +00:00
|
|
|
} catch (error) {
|
2023-04-04 13:29:21 +00:00
|
|
|
debugError(error);
|
2022-06-22 22:13:39 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:09:21 +00:00
|
|
|
override defaultArgs(options: BrowserLaunchArgumentOptions = {}): string[] {
|
2022-11-10 10:43:37 +00:00
|
|
|
// See https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
|
2022-06-22 22:13:39 +00:00
|
|
|
const chromeArguments = [
|
|
|
|
'--allow-pre-commit-input',
|
|
|
|
'--disable-background-networking',
|
|
|
|
'--disable-background-timer-throttling',
|
|
|
|
'--disable-backgrounding-occluded-windows',
|
|
|
|
'--disable-breakpad',
|
|
|
|
'--disable-client-side-phishing-detection',
|
|
|
|
'--disable-component-extensions-with-background-pages',
|
2022-11-10 10:43:37 +00:00
|
|
|
'--disable-component-update',
|
2022-06-22 22:13:39 +00:00
|
|
|
'--disable-default-apps',
|
|
|
|
'--disable-dev-shm-usage',
|
|
|
|
'--disable-extensions',
|
2022-07-29 08:01:15 +00:00
|
|
|
// AcceptCHFrame disabled because of crbug.com/1348106.
|
2023-05-02 06:53:40 +00:00
|
|
|
// DIPS is disabled because of crbug.com/1439578. TODO: enable after M115.
|
|
|
|
'--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints,DIPS',
|
2022-06-22 22:13:39 +00:00
|
|
|
'--disable-hang-monitor',
|
|
|
|
'--disable-ipc-flooding-protection',
|
|
|
|
'--disable-popup-blocking',
|
|
|
|
'--disable-prompt-on-repost',
|
|
|
|
'--disable-renderer-backgrounding',
|
|
|
|
'--disable-sync',
|
2022-11-10 10:43:37 +00:00
|
|
|
'--enable-automation',
|
|
|
|
// TODO(sadym): remove '--enable-blink-features=IdleDetection' once
|
|
|
|
// IdleDetection is turned on by default.
|
|
|
|
'--enable-blink-features=IdleDetection',
|
|
|
|
'--enable-features=NetworkServiceInProcess2',
|
|
|
|
'--export-tagged-pdf',
|
2022-06-22 22:13:39 +00:00
|
|
|
'--force-color-profile=srgb',
|
|
|
|
'--metrics-recording-only',
|
|
|
|
'--no-first-run',
|
|
|
|
'--password-store=basic',
|
|
|
|
'--use-mock-keychain',
|
|
|
|
];
|
|
|
|
const {
|
|
|
|
devtools = false,
|
|
|
|
headless = !devtools,
|
|
|
|
args = [],
|
|
|
|
userDataDir,
|
|
|
|
} = options;
|
|
|
|
if (userDataDir) {
|
|
|
|
chromeArguments.push(`--user-data-dir=${path.resolve(userDataDir)}`);
|
|
|
|
}
|
|
|
|
if (devtools) {
|
|
|
|
chromeArguments.push('--auto-open-devtools-for-tabs');
|
|
|
|
}
|
|
|
|
if (headless) {
|
|
|
|
chromeArguments.push(
|
2022-12-07 13:54:00 +00:00
|
|
|
headless === 'new' ? '--headless=new' : '--headless',
|
2022-06-22 22:13:39 +00:00
|
|
|
'--hide-scrollbars',
|
|
|
|
'--mute-audio'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
args.every(arg => {
|
|
|
|
return arg.startsWith('-');
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
chromeArguments.push('about:blank');
|
|
|
|
}
|
|
|
|
chromeArguments.push(...args);
|
|
|
|
return chromeArguments;
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:09:21 +00:00
|
|
|
override executablePath(channel?: ChromeReleaseChannel): string {
|
2022-06-22 22:13:39 +00:00
|
|
|
if (channel) {
|
2023-04-04 13:29:21 +00:00
|
|
|
return computeSystemExecutablePath({
|
|
|
|
browser: SupportedBrowsers.CHROME,
|
|
|
|
channel: convertPuppeteerChannelToBrowsersChannel(channel),
|
|
|
|
});
|
2022-06-22 22:13:39 +00:00
|
|
|
} else {
|
2022-10-21 13:09:21 +00:00
|
|
|
return this.resolveExecutablePath();
|
2022-06-22 22:13:39 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:29:21 +00:00
|
|
|
}
|
2022-06-22 22:13:39 +00:00
|
|
|
|
2023-04-04 13:29:21 +00:00
|
|
|
function convertPuppeteerChannelToBrowsersChannel(
|
|
|
|
channel: ChromeReleaseChannel
|
|
|
|
): BrowsersChromeReleaseChannel {
|
|
|
|
switch (channel) {
|
|
|
|
case 'chrome':
|
|
|
|
return BrowsersChromeReleaseChannel.STABLE;
|
|
|
|
case 'chrome-dev':
|
|
|
|
return BrowsersChromeReleaseChannel.DEV;
|
|
|
|
case 'chrome-beta':
|
|
|
|
return BrowsersChromeReleaseChannel.BETA;
|
|
|
|
case 'chrome-canary':
|
|
|
|
return BrowsersChromeReleaseChannel.CANARY;
|
2022-06-22 22:13:39 +00:00
|
|
|
}
|
|
|
|
}
|