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(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=)


10
11
12
13
14
15
16
17
18
# File 'lib/puppeteer/browser_runner.rb', line 10

def initialize(executable_path, process_arguments, user_data_dir, using_temp_user_data_dir)
  @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.



20
21
22
# File 'lib/puppeteer/browser_runner.rb', line 20

def connection
  @connection
end

#procObject (readonly)

Returns the value of attribute proc.



20
21
22
# File 'lib/puppeteer/browser_runner.rb', line 20

def proc
  @proc
end

Instance Method Details

#closePromise

Returns:

  • (Promise)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/puppeteer/browser_runner.rb', line 125

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)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppeteer/browser_runner.rb', line 142

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:, protocol_timeout: nil) ⇒ !Promise<!Connection>

Parameters:

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

Returns:



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

def setup_connection(use_pipe:, timeout:, slow_mo:, preferred_revision:, protocol_timeout: nil)
  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,
      protocol_timeout: protocol_timeout,
    )
  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:



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

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