Class: Puppeteer::Launcher::Chrome

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/launcher/chrome.rb,
sig/_supplementary.rbs

Defined Under Namespace

Classes: DefaultArgs

Constant Summary collapse

CHROMIUM_CHANNELS =
{
  windows: {
    'chrome' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome\\Application\\chrome.exe",
    'chrome-beta' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome Beta\\Application\\chrome.exe",
    'chrome-canary' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome SxS\\Application\\chrome.exe",
    'chrome-dev' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome Dev\\Application\\chrome.exe",
    'msedge' => "#{ENV['PROGRAMFILES(X86)']}\\Microsoft\\Edge\\Application\\msedge.exe",
  },
  darwin: {
    'chrome' => '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    'chrome-beta' => '/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta',
    'chrome-canary' => '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
    'chrome-dev' => '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev',
    'msedge' => '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
  },
  linux: {
    'chrome' => -> {
      Puppeteer::ExecutablePathFinder.new(
        'google-chrome-stable',
        'google-chrome',
        'chrome',
        'chromium-freeworld',
        'chromium-browser',
        'chromium',
      ).find_first
    },
    'chrome-beta' => '/opt/google/chrome-beta/chrome',
    'chrome-dev' => '/opt/google/chrome-unstable/chrome',
  },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root:, preferred_revision:, is_puppeteer_core:) ⇒ Chrome

Returns a new instance of Chrome.

Parameters:

  • project_root: (Object)
  • preferred_revision: (Object)
  • is_puppeteer_core: (Object)


24
25
26
27
28
# File 'lib/puppeteer/launcher/chrome.rb', line 24

def initialize(project_root:, preferred_revision:, is_puppeteer_core:)
  @project_root = project_root
  @preferred_revision = preferred_revision
  @is_puppeteer_core = is_puppeteer_core
end

Class Method Details

.get_features(flag, options = []) ⇒ Array[String]

Parameters:

  • flag (String)
  • options (Array[String]) (defaults to: [])

Returns:

  • (Array[String])


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/puppeteer/launcher/chrome.rb', line 6

def self.get_features(flag, options = [])
  prefix = flag.end_with?('=') ? flag : "#{flag}="
  options
    .select { |option| option.start_with?(prefix) }
    .flat_map do |option|
      option[(option.index('=') + 1)..]
        .strip
        .split(',')
        .map(&:strip)
    end
    .reject(&:empty?)
end

.remove_matching_flags(array, flag) ⇒ Array[String]

Parameters:

  • array (Array[String])
  • flag (String)

Returns:

  • (Array[String])


19
20
21
22
# File 'lib/puppeteer/launcher/chrome.rb', line 19

def self.remove_matching_flags(array, flag)
  prefix = flag.end_with?('=') ? flag : "#{flag}="
  array.delete_if { |option| option.start_with?(prefix) }
end

Instance Method Details

#default_args(options = nil) ⇒ DefaultArgs

Parameters:

  • options (Hash[Symbol, untyped], nil) (defaults to: nil)

Returns:



256
257
258
# File 'lib/puppeteer/launcher/chrome.rb', line 256

def default_args(options = nil)
  DefaultArgs.new(ChromeArgOptions.new(options || {}))
end

#executable_path(channel: nil) ⇒ string

Parameters:

  • channel: (String, Symbol, nil) (defaults to: nil)

Returns:

  • (string)


261
262
263
264
265
266
267
# File 'lib/puppeteer/launcher/chrome.rb', line 261

def executable_path(channel: nil)
  if channel
    executable_path_for_channel(channel.to_s)
  else
    fallback_executable_path
  end
end

#executable_path_for_channel(channel) ⇒ String

Parameters:

  • channel (String)

Returns:

  • (String)


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/puppeteer/launcher/chrome.rb', line 305

private def executable_path_for_channel(channel)
  chrome_path_map =
    if Puppeteer.env.windows?
      CHROMIUM_CHANNELS[:windows]
    elsif Puppeteer.env.darwin?
      CHROMIUM_CHANNELS[:darwin]
    else
      CHROMIUM_CHANNELS[:linux]
    end

  chrome_path = chrome_path_map[channel]
  unless chrome_path
    raise ArgumentError.new("Invalid channel: '#{channel}'. Allowed channel is #{chrome_path_map.keys}")
  end

  if chrome_path.is_a?(Proc)
    chrome_path = chrome_path.call
  end

  if !chrome_path || !File.exist?(chrome_path)
    raise "#{channel} is not installed on this system.\nExpected path: #{chrome_path}"
  end

  chrome_path
end

#fallback_executable_pathString

Returns:

  • (String)


269
270
271
# File 'lib/puppeteer/launcher/chrome.rb', line 269

private def fallback_executable_path
  executable_path_for_channel('chrome')
end

#launch(options = {}) ⇒ !Promise<!Browser>

Parameters:

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppeteer/launcher/chrome.rb', line 32

def launch(options = {})
  @chrome_arg_options = ChromeArgOptions.new(options)
  @launch_options = LaunchOptions.new(options)
  @browser_options = BrowserOptions.new(options)

  chrome_arguments =
    if !@launch_options.ignore_default_args
      default_args(options).to_a
    elsif @launch_options.ignore_default_args.is_a?(Enumerable)
      default_args(options).reject do |arg|
        @launch_options.ignore_default_args.include?(arg)
      end.to_a
    else
      @chrome_arg_options.args.dup
    end

  if chrome_arguments.none? { |arg| arg.start_with?('--remote-debugging-') }
    if @launch_options.pipe?
      chrome_arguments << '--remote-debugging-pipe'
    else
      chrome_arguments << "--remote-debugging-port=#{@chrome_arg_options.debugging_port}"
    end
  end

  user_data_dir = chrome_arguments.find { |arg| arg.start_with?('--user-data-dir') }
  if user_data_dir
    user_data_dir = user_data_dir.split('=').last
    unless File.exist?(user_data_dir)
      raise ArgumentError.new("Chrome user data dir not found at '#{user_data_dir}'")
    end
    using_temp_user_data_dir = false
  else
    user_data_dir = Dir.mktmpdir('puppeteer_dev_chrome_profile-', ENV['PUPPETEER_TMP_DIR'])
    chrome_arguments << "--user-data-dir=#{user_data_dir}"
    using_temp_user_data_dir = true
  end

  chrome_executable =
    if @launch_options.channel
      executable_path_for_channel(@launch_options.channel.to_s)
    else
      @launch_options.executable_path || fallback_executable_path
    end
  use_pipe = chrome_arguments.include?('--remote-debugging-pipe')
  runner = Puppeteer::BrowserRunner.new(
    chrome_executable,
    chrome_arguments,
    user_data_dir,
    using_temp_user_data_dir,
  )
  runner.start(
    handle_SIGHUP: @launch_options.handle_SIGHUP?,
    handle_SIGTERM: @launch_options.handle_SIGTERM?,
    handle_SIGINT: @launch_options.handle_SIGINT?,
    dumpio: @launch_options.dumpio?,
    env: @launch_options.env,
    pipe: use_pipe,
  )

  browser =
    begin
      connection = runner.setup_connection(
        use_pipe: use_pipe,
        timeout: @launch_options.timeout,
        slow_mo: @browser_options.slow_mo,
        preferred_revision: @preferred_revision,
        protocol_timeout: @browser_options.protocol_timeout,
      )

      Puppeteer::Browser.create(
        product: product,
        connection: connection,
        context_ids: [],
        ignore_https_errors: @browser_options.ignore_https_errors?,
        default_viewport: @browser_options.default_viewport,
        network_enabled: @browser_options.network_enabled,
        issues_enabled: @browser_options.issues_enabled,
        block_list: @browser_options.block_list,
        allow_list: @browser_options.allow_list,
        process: runner.proc,
        close_callback: -> { runner.close },
        target_filter_callback: nil,
        is_page_target_callback: nil,
      )
    rescue
      runner.kill
      raise
    end

  begin
    if @launch_options.wait_for_initial_page?
      browser.wait_for_target(
        predicate: ->(target) { target.type == 'page' },
        timeout: @launch_options.timeout,
      )
    end
  rescue
    browser.close
    raise
  end

  browser
end

#productString

Returns:

  • (String)


331
332
333
# File 'lib/puppeteer/launcher/chrome.rb', line 331

def product
  'chrome'
end