Class: WebkitRemote::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/webkit_remote/process.rb

Overview

Tracks a Webkit process.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Process

Tracker for a yet-unlaunched process.

Parameters:

  • opts (Hash) (defaults to: {})

    tweak the options below

Options Hash (opts):

  • port (Integer)

    the port used by the remote debugging server; the default port is 9292

  • timeout (Number)

    number of seconds to wait for the browser to start; the default timeout is 10 seconds

  • window (Hash<Symbol, Number>)

    set the :left, :top, :width and :height of the browser window; by default, the browser window is 256x256 starting at 0,0.

  • allow_popups (Boolean)

    when true, the popup blocker is disabled; this is sometimes necessary when driving a Web UI via JavaScript

  • headless (Boolean)

    if true, Chrome runs without any dependency on a display server

  • chrome_binary (String)

    path to the Chrome binary to be used; by default, the path is automatically detected



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/webkit_remote/process.rb', line 26

def initialize(opts = {})
  @port = opts[:port] || 9292
  @timeout = opts[:timeout] || 10
  @running = false
  @data_dir = Dir.mktmpdir 'webkit-remote'
  @pid = nil
  if opts[:window]
    @window = opts[:window]
  else
    @window = { }
  end
  @window[:top] ||= 0
  @window[:left] ||= 0
  @window[:width] ||= 256
  @window[:height] ||= 256
  @cli = chrome_cli opts
end

Instance Attribute Details

#portInteger (readonly)

Returns port that the process’ remote debugging server listens to.

Returns:

  • (Integer)

    port that the process’ remote debugging server listens to



108
109
110
# File 'lib/webkit_remote/process.rb', line 108

def port
  @port
end

#runningBoolean (readonly) Also known as: running?

Returns true if the Webkit process is running.

Returns:

  • (Boolean)

    true if the Webkit process is running



81
82
83
# File 'lib/webkit_remote/process.rb', line 81

def running
  @running
end

Class Method Details

.chrome_binaryString

Path to a Google Chrome / Chromium binary.

Returns:

  • (String)

    full-qualified path to a binary that launches Chrome



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/webkit_remote/process.rb', line 180

def self.chrome_binary
  return @chrome_binary unless @chrome_binary == false

  case RUBY_PLATFORM
  when /linux/
    [
      'google-chrome',
      'google-chromium',
    ].each do |binary|
      path = `which #{binary}`
      unless path.empty?
        @chrome_binary = path.strip
        break
      end
    end
  when /darwin/
    [
      '/Applications/Chromium.app/Contents/MacOS/Chromium',
      '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
      '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
    ].each do |path|
      if File.exist? path
        @chrome_binary = path
        break
      end
    end
  else
    raise "Unsupported platform #{RUBY_PLATFORM}"
  end
  @chrome_binary ||= nil
end

Instance Method Details

#chrome_cli(opts) ⇒ Array<String>

Command-line that launches Google Chrome / Chromium

Parameters:

  • opts (Hash)

    options passed to the WebkitRemote::Process constructor

Returns:

  • (Array<String>)

    command line for launching Chrome



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/webkit_remote/process.rb', line 119

def chrome_cli(opts)
  # The Chromium wiki recommends this page for available flags:
  #     http://peter.sh/experiments/chromium-command-line-switches/
  [
    opts[:chrome_binary] || self.class.chrome_binary,
  ] + chrome_cli_flags(opts) + [
    "--remote-debugging-port=#{@port}",  # Webkit remote debugging
    "--user-data-dir=#{@data_dir}",  # really ensure a clean slate
    "--window-position=#{@window[:left]},#{@window[:top]}",
    "--window-size=#{@window[:width]},#{@window[:height]}",

    'about:blank',  # don't load the homepage
    {
      chdir: @data_dir,
      in: '/dev/null',
      out: File.join(@data_dir, '.stdout'),
      err: File.join(@data_dir, '.stderr'),
      close_others: true,
    },
  ]
end

#chrome_cli_flags(opts) ⇒ Array<String>

Flags used on the command-line that launches Google Chrome / Chromium.

Parameters:

  • opts (Hash)

    options passed to the WebkitRemote::Process constructor

Returns:

  • (Array<String>)

    flags used on the command line for launching Chrome



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/webkit_remote/process.rb', line 145

def chrome_cli_flags(opts)
  # TODO - look at --data-path --homedir --profile-directory
  flags = [
    '--bwsi',  # disable extensions, sync, bookmarks
    '--disable-cloud-import',  # no talking with the Google servers
    '--disable-default-apps',  # no bundled apps
    '--disable-extensions',  # no extensions
    '--disable-logging',  # don't trash stdout / stderr
    '--disable-plugins',  # no native content
    '--disable-prompt-on-repost',   # no confirmation dialog on POST refresh
    '--disable-sync',  # no talking with the Google servers
    '--disable-translate',  # no Google Translate calls
    '--incognito',  # don't use old state, don't preserve state
    '--homepage=about:blank',  # don't go to Google in new tabs
    '--keep-alive-for-test',  # don't kill process if the last window dies
    '--lang=en-US',  # set a default language
    '--log-level=3',  # FATAL, because there's no setting for "none"
    '--mute-audio',  # don't let the computer make noise
    '--no-default-browser-check',  # don't hang when Chrome isn't default
    '--no-experiments',  # not sure this is useful
    '--no-first-run',  # don't show the help UI
    '--no-service-autorun',  # don't mess with autorun settings
    '--noerrdialogs',  # don't hang on error dialogs
  ]
  flags << '--disable-popup-blocking' if opts[:allow_popups]
  if opts[:headless]
    flags << '--headless'  # don't create a UI
    flags << '--disable-gpu'  # needed for --headless to work at the moment
  end
  flags
end

#finalizeObject

Remove temporary directory if it’s still there at garbage collection time.



111
112
113
# File 'lib/webkit_remote/process.rb', line 111

def finalize
  PathUtils.rm_rf @data_dir if File.exist?(@data_dir)
end

#startWebkitRemote::Browser

Starts the browser process.

Returns:

  • (WebkitRemote::Browser)

    master session to the started Browser process; the session’s auto_close is set to false so that it can be safely discarded; nil if the launch fails



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
# File 'lib/webkit_remote/process.rb', line 49

def start
  return self if running?

  unless @pid = ::Process.spawn(*@cli)
    # The launch failed.
    stop
    return nil
  end

  (@timeout * 20).times do
    # Check if the browser exited.
    begin
      break if ::Process.wait(@pid, ::Process::WNOHANG)
    rescue SystemCallError  # no children
      break
    end

    # Check if the browser finished starting up.
    begin
      browser = WebkitRemote::Browser.new process: self
      @running = true
      return browser
    rescue SystemCallError  # most likely ECONNREFUSED
      Kernel.sleep 0.05
    end
  end
  # The browser failed, or was too slow to start.
  stop
  nil
end

#stopWebkitRemote::Process

Stops the browser process.

Only call this after you’re done with the process.

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/webkit_remote/process.rb', line 89

def stop
  return self unless running?
  if @pid
    begin
      ::Process.kill 'TERM', @pid
      ::Process.wait @pid
    rescue SystemCallError
      # Process died on its own.
    ensure
      @pid = nil
    end
  end

  FileUtils.rm_rf @data_dir if File.exist?(@data_dir)
  @running = false
  self
end