Class: Minibidi::FirefoxLauncher

Inherits:
Object
  • Object
show all
Defined in:
lib/minibidi/firefox_launcher.rb

Instance Method Summary collapse

Instance Method Details

#launch(&block) ⇒ Object

Raises:

  • (ArgumentError)


31
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
# File 'lib/minibidi/firefox_launcher.rb', line 31

def launch(&block)
  raise ArgumentError, "block is required" unless block

  tmp = Dir.mktmpdir('minibidi')
  (tmp)
  args = [
    "--remote-debugging-port=0",
    "--profile",
    tmp,
    "--no-remote",
  ]
  if RUBY_PLATFORM =~ /darwin/
    args << "--foreground"
  end
  if %w[true 1].include?(ENV['HEADLESS'])
    args << "--headless"
  end

  proc = BrowserProcess.new(
    Firefox.discover_binary,
    *args,
    "about:blank",
  )
  at_exit do
    proc.kill
    FileUtils.remove_entry(tmp)
  end
  trap(:INT) { proc.kill ; exit 130 }
  trap(:TERM) { proc.kill ; proc.dispose }
  trap(:HUP) { proc.kill ; proc.dispose }

  endpoint = wait_for_ws_endpoint(proc)

  Async::Reactor.run do
    Async::WebSocket::Client.connect(Async::HTTP::Endpoint.parse("#{endpoint}/session")) do |async_websocket_connection|
      browser = Browser.new(async_websocket_connection)
      begin
        block.call(browser)
      ensure
        browser.close
      end
    end
  end
end