Class: Ferrum::Browser::Process
- Inherits:
-
Object
- Object
- Ferrum::Browser::Process
- Defined in:
- lib/ferrum/browser/process.rb
Constant Summary collapse
- KILL_TIMEOUT =
2
- WAIT_KILLED =
0.05
- PROCESS_TIMEOUT =
ENV.fetch("FERRUM_PROCESS_TIMEOUT", 2).to_i
- BROWSER_PATH =
ENV["BROWSER_PATH"]
- BROWSER_HOST =
"127.0.0.1"
- BROWSER_PORT =
"0"
- DEFAULT_OPTIONS =
{ "headless" => nil, "disable-gpu" => nil, "hide-scrollbars" => nil, "mute-audio" => nil, "enable-automation" => nil, "disable-web-security" => nil, "disable-session-crashed-bubble" => nil, "disable-breakpad" => nil, "disable-sync" => nil, "no-first-run" => nil, "use-mock-keychain" => nil, "keep-alive-for-test" => nil, "disable-popup-blocking" => nil, "disable-extensions" => nil, "disable-hang-monitor" => nil, "disable-features" => "site-per-process,TranslateUI", "disable-translate" => nil, "disable-background-networking" => nil, "enable-features" => "NetworkService,NetworkServiceInProcess", "disable-background-timer-throttling" => nil, "disable-backgrounding-occluded-windows" => nil, "disable-client-side-phishing-detection" => nil, "disable-default-apps" => nil, "disable-dev-shm-usage" => nil, "disable-ipc-flooding-protection" => nil, "disable-prompt-on-repost" => nil, "disable-renderer-backgrounding" => nil, "force-color-profile" => "srgb", "metrics-recording-only" => nil, "safebrowsing-disable-auto-update" => nil, "password-store" => "basic", # Note: --no-sandbox is not needed if you properly setup a user in the container. # https://github.com/ebidel/lighthouse-ci/blob/master/builder/Dockerfile#L35-L40 # "no-sandbox" => nil, }.freeze
- NOT_FOUND =
"Could not find an executable for chrome. Try to make it " \ "available on the PATH or set environment varible for " \ "example BROWSER_PATH=\"/Applications/Chromium.app/Contents/MacOS/Chromium\""
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ws_url ⇒ Object
readonly
Returns the value of attribute ws_url.
Class Method Summary collapse
- .detect_browser_path ⇒ Object
- .directory_remover(path) ⇒ Object
- .process_killer(pid) ⇒ Object
- .start(*args) ⇒ Object
Instance Method Summary collapse
-
#initialize(options) ⇒ Process
constructor
A new instance of Process.
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options) ⇒ Process
Returns a new instance of Process.
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 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ferrum/browser/process.rb', line 110 def initialize() @options = {} @path = [:browser_path] || BROWSER_PATH || self.class.detect_browser_path if [:url] url = URI.join([:url].to_s, "/json/version") response = JSON.parse(::Net::HTTP.get(url)) set_ws_url(response["webSocketDebuggerUrl"]) return end # Doesn't work on MacOS, so we need to set it by CDP as well @options.merge!("window-size" => [:window_size].join(",")) port = .fetch(:port, BROWSER_PORT) @options.merge!("remote-debugging-port" => port) host = .fetch(:host, BROWSER_HOST) @options.merge!("remote-debugging-address" => host) @temp_user_data_dir = Dir.mktmpdir ObjectSpace.define_finalizer(self, self.class.directory_remover(@temp_user_data_dir)) @options.merge!("user-data-dir" => @temp_user_data_dir) @options = DEFAULT_OPTIONS.merge(@options) unless .fetch(:headless, true) @options.delete("headless") @options.delete("disable-gpu") end @process_timeout = .fetch(:process_timeout, PROCESS_TIMEOUT) @options.merge!(.fetch(:browser_options, {})) @logger = [:logger] end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def cmd @cmd end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def path @path end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def pid @pid end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def port @port end |
#ws_url ⇒ Object (readonly)
Returns the value of attribute ws_url.
60 61 62 |
# File 'lib/ferrum/browser/process.rb', line 60 def ws_url @ws_url end |
Class Method Details
.detect_browser_path ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ferrum/browser/process.rb', line 96 def self.detect_browser_path if RUBY_PLATFORM.include?("darwin") [ "/Applications/Chromium.app/Contents/MacOS/Chromium", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ].find { |path| File.exist?(path) } else %w[chromium google-chrome-unstable google-chrome-beta google-chrome chrome chromium-browser google-chrome-stable].reduce(nil) do |path, exe| path = Cliver.detect(exe) break path if path end end end |
.directory_remover(path) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/ferrum/browser/process.rb', line 87 def self.directory_remover(path) proc do begin FileUtils.remove_entry(path) rescue Errno::ENOENT end end end |
.process_killer(pid) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ferrum/browser/process.rb', line 66 def self.process_killer(pid) proc do begin if Ferrum.windows? ::Process.kill("KILL", pid) else ::Process.kill("USR1", pid) start = Ferrum.monotonic_time while ::Process.wait(pid, ::Process::WNOHANG).nil? sleep(WAIT_KILLED) next unless Ferrum.timeout?(start, KILL_TIMEOUT) ::Process.kill("KILL", pid) ::Process.wait(pid) break end end rescue Errno::ESRCH, Errno::ECHILD end end end |
.start(*args) ⇒ Object
62 63 64 |
# File 'lib/ferrum/browser/process.rb', line 62 def self.start(*args) new(*args).tap(&:start) end |
Instance Method Details
#restart ⇒ Object
177 178 179 180 |
# File 'lib/ferrum/browser/process.rb', line 177 def restart stop start end |
#start ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ferrum/browser/process.rb', line 149 def start # Don't do anything as browser is already running as external process. return if ws_url begin read_io, write_io = IO.pipe = { in: File::NULL } [:pgroup] = true unless Ferrum.windows? [:out] = [:err] = write_io raise Cliver::Dependency::NotFound.new(NOT_FOUND) unless @path @cmd = [@path] + @options.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" } @pid = ::Process.spawn(*@cmd, ) ObjectSpace.define_finalizer(self, self.class.process_killer(@pid)) parse_ws_url(read_io, @process_timeout) ensure close_io(read_io, write_io) end end |
#stop ⇒ Object
171 172 173 174 175 |
# File 'lib/ferrum/browser/process.rb', line 171 def stop kill if @pid remove_temp_user_data_dir if @temp_user_data_dir ObjectSpace.undefine_finalizer(self) end |