Class: Puppeteer::BrowserRunner

Inherits:
Object
  • Object
show all
Includes:
DebugPrint
Defined in:
lib/puppeteer/browser_runner.rb

Overview

Defined Under Namespace

Classes: BrowserProcess, LaunchError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(for_firefox, executable_path, process_arguments, user_data_dir, using_temp_user_data_dir) ⇒ BrowserRunner

Returns a new instance of BrowserRunner.

Parameters:

  • executablePath (string)
  • processArguments (!Array<string>)
  • tempDirectory (string=)


12
13
14
15
16
17
18
19
20
21
# File 'lib/puppeteer/browser_runner.rb', line 12

def initialize(for_firefox, executable_path, process_arguments, user_data_dir, using_temp_user_data_dir)
  @for_firefox = for_firefox
  @executable_path = executable_path
  @process_arguments = process_arguments
  @user_data_dir = user_data_dir
  @using_temp_user_data_dir = using_temp_user_data_dir
  @proc = nil
  @connection = nil
  @closed = true
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



23
24
25
# File 'lib/puppeteer/browser_runner.rb', line 23

def connection
  @connection
end

#procObject (readonly)

Returns the value of attribute proc.



23
24
25
# File 'lib/puppeteer/browser_runner.rb', line 23

def proc
  @proc
end

Instance Method Details

#closePromise

Returns:

  • (Promise)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppeteer/browser_runner.rb', line 128

def close
  return if @closed

  if @using_temp_user_data_dir
    kill
  elsif @connection
    begin
      @connection.send_message('Browser.close')
    rescue
      kill
    end
  end

  @process_closing.call
end

#killPromise

Returns:

  • (Promise)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppeteer/browser_runner.rb', line 145

def kill
  # If the process failed to launch (for example if the browser executable path
  # is invalid), then the process does not get a pid assigned. A call to
  # `proc.kill` would error, as the `pid` to-be-killed can not be found.
  @proc&.kill

  # Attempt to remove temporary profile directory to avoid littering.
  begin
    if @using_temp_user_data_dir
      FileUtils.rm_rf(@user_data_dir)
    end
  rescue => err
    debug_puts(err)
  end
end

#setup_connection(use_pipe:, timeout:, slow_mo:, preferred_revision:) ⇒ !Promise<!Connection>

Parameters:

  • options (!({usePipe?: boolean, timeout: number, slowMo: number, preferredRevision: string}))

Returns:



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppeteer/browser_runner.rb', line 164

def setup_connection(use_pipe:, timeout:, slow_mo:, preferred_revision:)
  if !use_pipe
    browser_ws_endpoint = wait_for_ws_endpoint(@proc, timeout, preferred_revision)
    transport = Puppeteer::WebSocketTransport.create(browser_ws_endpoint)
    @connection = Puppeteer::Connection.new(browser_ws_endpoint, transport, slow_mo)
  else
    raise NotImplementedError.new('PipeTransport is not yet implemented')
  end

  @connection
end

#start(executable_path: nil, ignore_default_args: nil, handle_SIGINT: nil, handle_SIGTERM: nil, handle_SIGHUP: nil, timeout: nil, dumpio: nil, env: nil, pipe: nil) ⇒ Object

Parameters:



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
# File 'lib/puppeteer/browser_runner.rb', line 64

def start(
  executable_path: nil,
  ignore_default_args: nil,
  handle_SIGINT: nil,
  handle_SIGTERM: nil,
  handle_SIGHUP: nil,
  timeout: nil,
  dumpio: nil,
  env: nil,
  pipe: nil
)
  @launch_options = Puppeteer::Launcher::LaunchOptions.new({
    executable_path: executable_path,
    ignore_default_args: ignore_default_args,
    handle_SIGINT: handle_SIGINT,
    handle_SIGTERM: handle_SIGTERM,
    handle_SIGHUP: handle_SIGHUP,
    timeout: timeout,
    dumpio: dumpio,
    env: env,
    pipe: pipe,
  }.compact)
  @proc = BrowserProcess.new(
    @launch_options.env,
    @executable_path,
    @process_arguments,
  )
  # if (dumpio) {
  #   this.proc.stderr.pipe(process.stderr);
  #   this.proc.stdout.pipe(process.stdout);
  # }
  @closed = false
  @process_closing = -> {
    @proc.dispose
    @closed = true
    if @using_temp_user_data_dir
      FileUtils.rm_rf(@user_data_dir)
    end
  }
  at_exit do
    kill
  end

  if @launch_options.handle_SIGINT?
    trap(:INT) do
      kill
      exit 130
    end
  end

  if @launch_options.handle_SIGTERM?
    trap(:TERM) do
      close
    end
  end

  if @launch_options.handle_SIGHUP? && !Puppeteer.env.windows?
    trap(:HUP) do
      close
    end
  end
end