Class: Chromium
- Inherits:
-
Object
- Object
- Chromium
- Defined in:
- lib/chromium.rb
Instance Attribute Summary collapse
-
#display ⇒ Object
readonly
Returns the value of attribute display.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#tmp_dir ⇒ Object
readonly
Returns the value of attribute tmp_dir.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Chromium
constructor
A new instance of Chromium.
- #navigate_to(url) ⇒ Object
- #quit ⇒ Object
- #running? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Chromium
Returns a new instance of Chromium.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/chromium.rb', line 6 def initialize( = {}) @tmp_dir = .fetch :tmp_dir, "/tmp/.chromium-#{SecureRandom.hex(6)}" @display = .fetch :display, 0 @settings = .fetch :settings, [] window_size = `DISPLAY=:#{@display} $(which xdotool) getdisplaygeometry`.strip.gsub ' ', ',' window_size = '7000,7000' unless window_size @pid = nil @settings = %W( --start-maximized --incognito --no-first-run --no-sandbox --disable --disable-java --disable-translate --disable-infobars --disable-suggestions-service --disable-save-password-bubble --allow-running-insecure-content --ignore-certificate-errors --ignore-urlfetcher-cert-requests --disable-gpu --new-window --force-device-scale-factor=1 --window-position=0,0 --auto-ssl-client-auth --use-fake-ui-for-media-stream --kiosk --window-size=#{window_size} --disk-cache-dir=#{@tmp_dir}/cache/ --user-data-dir=#{@tmp_dir}/user_data/ ) | @settings dispatch end |
Instance Attribute Details
#display ⇒ Object (readonly)
Returns the value of attribute display.
4 5 6 |
# File 'lib/chromium.rb', line 4 def display @display end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
4 5 6 |
# File 'lib/chromium.rb', line 4 def pid @pid end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
4 5 6 |
# File 'lib/chromium.rb', line 4 def settings @settings end |
#tmp_dir ⇒ Object (readonly)
Returns the value of attribute tmp_dir.
4 5 6 |
# File 'lib/chromium.rb', line 4 def tmp_dir @tmp_dir end |
Instance Method Details
#navigate_to(url) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/chromium.rb', line 44 def navigate_to(url) out_pipe, in_pipe = IO.pipe @pid = Process.spawn "DISPLAY=:#{@display} $(which chromium) #{@settings.join(' ')} #{url}", err: in_pipe raise Headless::Exception.new("Chromium did not launch - something's wrong") unless @pid Process.detach @pid ensure in_pipe.close end |
#quit ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/chromium.rb', line 59 def quit Process.kill 'TERM', @pid Process.wait @pid FileUtils.rm_rf @tmp_dir, force: true rescue # no such process; assume it's already killed end |
#running? ⇒ Boolean
53 54 55 56 57 |
# File 'lib/chromium.rb', line 53 def running? Process.getpgid(@pid) && true rescue Errno::ESRCH false end |