import{mkdtemp}from"fs/promises";import path from"path";import{computeSystemExecutablePath,Browser as SupportedBrowsers,ChromeReleaseChannel as BrowsersChromeReleaseChannel}from"@puppeteer/browsers";import{debugError}from"../common/util.js";import{assert}from"../util/assert.js";import{ProductLauncher}from"./ProductLauncher.js";import{rm}from"./util/fs.js";export class ChromeLauncher extends ProductLauncher{constructor(e){super(e,"chrome")}launch(e={}){return!0!==(e.headless??!0)||"warn"!==this.puppeteer.configuration.logLevel||Boolean(process.env.PUPPETEER_DISABLE_HEADLESS_WARNING)||console.warn(["[1m[43m[30m","Puppeteer old Headless deprecation warning:[0m[33m","  In the near future `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.[0m\n"].join("\n  ")),super.launch(e)}async computeLaunchArguments(e={}){const{ignoreDefaultArgs:r=!1,args:s=[],pipe:t=!1,debuggingPort:a,channel:o,executablePath:n}=e,i=[];r?Array.isArray(r)?i.push(...this.defaultArgs(e).filter((e=>!r.includes(e)))):i.push(...s):i.push(...this.defaultArgs(e)),i.some((e=>e.startsWith("--remote-debugging-")))||(t?(assert(!a,"Browser should be launched with either pipe or debugging port - not both."),i.push("--remote-debugging-pipe")):i.push(`--remote-debugging-port=${a||0}`));let l=!1,u=i.findIndex((e=>e.startsWith("--user-data-dir")));u<0&&(l=!0,i.push(`--user-data-dir=${await mkdtemp(this.getProfilePath())}`),u=i.length-1);const p=i[u].split("=",2)[1];assert("string"==typeof p,"`--user-data-dir` is malformed");let h=n;return h||(assert(o||!this.puppeteer._isPuppeteerCore,"An `executablePath` or `channel` must be specified for `puppeteer-core`"),h=this.executablePath(o)),{executablePath:h,args:i,isTempUserDataDir:l,userDataDir:p}}async cleanUserDataDir(e,r){if(r.isTemp)try{await rm(e)}catch(e){throw debugError(e),e}}defaultArgs(e={}){const r=["--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","--disable-component-update","--disable-default-apps","--disable-dev-shm-usage","--disable-extensions","--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints","--disable-hang-monitor","--disable-ipc-flooding-protection","--disable-popup-blocking","--disable-prompt-on-repost","--disable-renderer-backgrounding","--disable-sync","--enable-automation","--enable-blink-features=IdleDetection","--enable-features=NetworkServiceInProcess2","--export-tagged-pdf","--force-color-profile=srgb","--metrics-recording-only","--no-first-run","--password-store=basic","--use-mock-keychain"],{devtools:s=!1,headless:t=!s,args:a=[],userDataDir:o}=e;return o&&r.push(`--user-data-dir=${path.resolve(o)}`),s&&r.push("--auto-open-devtools-for-tabs"),t&&r.push("new"===t?"--headless=new":"--headless","--hide-scrollbars","--mute-audio"),a.every((e=>e.startsWith("-")))&&r.push("about:blank"),r.push(...a),r}executablePath(e){return e?computeSystemExecutablePath({browser:SupportedBrowsers.CHROME,channel:convertPuppeteerChannelToBrowsersChannel(e)}):this.resolveExecutablePath()}}function convertPuppeteerChannelToBrowsersChannel(e){switch(e){case"chrome":return BrowsersChromeReleaseChannel.STABLE;case"chrome-dev":return BrowsersChromeReleaseChannel.DEV;case"chrome-beta":return BrowsersChromeReleaseChannel.BETA;case"chrome-canary":return BrowsersChromeReleaseChannel.CANARY}}