@@ -77,6 +77,10 @@ def activate session, on_fork: false
77
77
@q_msg = nil
78
78
@q_ans . close
79
79
@q_ans = nil
80
+ if CONFIG [ :open_frontend ] == 'chrome'
81
+ Process . kill ( :KILL , @wait_thr . pid )
82
+ FileUtils . remove_entry_secure @dir
83
+ end
80
84
end # accept
81
85
82
86
rescue Terminate
@@ -108,8 +112,6 @@ def greeting
108
112
@repl = false
109
113
dap_setup @sock . read ( $1. to_i )
110
114
when /^GET \/ HTTP\/ 1.1/
111
- require_relative 'server_cdp'
112
-
113
115
self . extend ( UI_CDP )
114
116
@repl = false
115
117
@ws_server = UI_CDP ::WebSocketServer . new ( @sock )
@@ -271,6 +273,66 @@ def initialize host: nil, port: nil
271
273
super ( )
272
274
end
273
275
276
+ def chrome_setup
277
+ require 'open3'
278
+ require 'socket'
279
+ require 'tmpdir'
280
+
281
+ require_relative 'server_cdp'
282
+
283
+ # The process to check OS is based on `selenium` project.
284
+ case RbConfig ::CONFIG [ 'host_os' ]
285
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
286
+ chrome_path = 'chrome.exe'
287
+ when /darwin|mac os/
288
+ chrome_path = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
289
+ when /linux/
290
+ chrome_path = 'google-chrome'
291
+ else
292
+ raise "Unsupported OS"
293
+ end
294
+
295
+ @dir = Dir . mktmpdir
296
+ # The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
297
+ stdin , stdout , stderr , @wait_thr = *Open3 . popen3 ( "#{ chrome_path } --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{ @dir } " )
298
+ stdin . close
299
+ stdout . close
300
+
301
+ data = stderr . readpartial 4096
302
+ if data . match /DevTools listening on ws:\/ \/ 127.0.0.1:(\d +)\/ (.*)/
303
+ port = $1
304
+ path = $2
305
+ else
306
+ raise "Can't open Chrome browser: #{ data } "
307
+ end
308
+
309
+ s = Socket . tcp "127.0.0.1" , port
310
+ ws_client = UI_CDP ::WebSocketClient . new ( s )
311
+ ws_client . handshake port , path
312
+ ws_client . send id : 1 , method : 'Target.getTargets'
313
+
314
+ 3 . times do
315
+ res = ws_client . extract_data
316
+ case
317
+ when res [ 'id' ] == 1 && target_info = res . dig ( 'result' , 'targetInfos' )
318
+ p = target_info . find { |t | t [ 'type' ] == 'page' }
319
+ ws_client . send id : 2 , method : 'Target.attachToTarget' ,
320
+ params : {
321
+ targetId : p [ 'targetId' ] ,
322
+ flatten : true
323
+ }
324
+ when res [ 'id' ] == 2
325
+ sleep 0.1
326
+ s_id = res . dig ( 'result' , 'sessionId' )
327
+ ws_client . send sessionId : s_id , id : 3 ,
328
+ method : 'Page.navigate' ,
329
+ params : {
330
+ url : "devtools://devtools/bundled/inspector.html?ws=#{ @addr } "
331
+ }
332
+ end
333
+ end
334
+ end
335
+
274
336
def accept
275
337
retry_cnt = 0
276
338
super # for fork
@@ -288,12 +350,7 @@ def accept
288
350
#
289
351
EOS
290
352
291
- DEBUGGER__ . warn <<~EOS if CONFIG [ :open_frontend ] == 'chrome'
292
- With Chrome browser, type the following URL in the address-bar:
293
-
294
- devtools://devtools/bundled/inspector.html?ws=#{ @addr }
295
-
296
- EOS
353
+ chrome_setup if CONFIG [ :open_frontend ] == 'chrome'
297
354
298
355
Socket . accept_loop ( socks ) do |sock , client |
299
356
@client_addr = client
0 commit comments