module DEBUGGER__::UI_CDP

def run_new_chrome

def run_new_chrome
  path = CONFIG[:chrome_path]
  data = nil
  port = nil
  wait_thr = nil
  # The process to check OS is based on `selenium` project.
  case RbConfig::CONFIG['host_os']
  when /mswin|msys|mingw|cygwin|emc/
    if path.nil?
      candidates = ['C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe']
      path = get_chrome_path candidates
    end
    # The path is based on https://github.com/sindresorhus/open/blob/v8.4.0/index.js#L128.
    stdin, stdout, stderr, wait_thr = *Open3.popen3("#{ENV['SystemRoot']}\\System32\\WindowsPowerShell\\v1.0\\powershell")
    tf = Tempfile.create(['debug-', '.txt'])
    stdin.puts("Start-process '#{path}' -Argumentlist '--remote-debugging-port=0', '--no-first-run', '--no-default-browser-check', '--user-data-dir=C:\\temp' -Wait -RedirectStandardError #{tf.path}")
    stdin.close
    stdout.close
    stderr.close
    port, path = get_devtools_endpoint(tf.path)
    at_exit{
      DEBUGGER__.skip_all
      stdin, stdout, stderr, wait_thr = *Open3.popen3("#{ENV['SystemRoot']}\\System32\\WindowsPowerShell\\v1.0\\powershell")
      stdin.puts("Stop-process -Name chrome")
      stdin.close
      stdout.close
      stderr.close
      tf.close
      begin
        File.unlink(tf)
      rescue Errno::EACCES
      end
    }
  when /darwin|mac os/
    path = path || '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
    dir = Dir.mktmpdir
    # The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting.
    stdin, stdout, stderr, wait_thr = *Open3.popen3("#{path} --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{dir}")
    stdin.close
    stdout.close
    data = stderr.readpartial 4096
    stderr.close
    if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
      port = $1
      path = $2
    end
    at_exit{
      DEBUGGER__.skip_all
      FileUtils.rm_rf dir
    }
  when /linux/
    path = path || 'google-chrome'
    dir = Dir.mktmpdir
    # The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting.
    stdin, stdout, stderr, wait_thr = *Open3.popen3("#{path} --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{dir}")
    stdin.close
    stdout.close
    data = ''
    begin
      Timeout.timeout(TIMEOUT_SEC) do
        until data.match?(/DevTools listening on ws:\/\/127.0.0.1:\d+.*/)
          data = stderr.readpartial 4096
        end
      end
    rescue Exception
      raise NotFoundChromeEndpointError
    end
    stderr.close
    if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
      port = $1
      path = $2
    end
    at_exit{
      DEBUGGER__.skip_all
      FileUtils.rm_rf dir
    }
  else
    raise UnsupportedError
  end
  [port, path, wait_thr.pid]
end