@@ -13,26 +13,18 @@ def setup_chrome
1313 require 'open3'
1414 require 'tmpdir'
1515
16- set_chrome_path
17- @dir = Dir . mktmpdir
18- # The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
19- stdin , stdout , stderr , @wait_thr = *Open3 . popen3 ( "#{ CONFIG [ :chrome_path ] } --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{ @dir } " )
20- stdin . close
21- stdout . close
22-
23- data = stderr . readpartial 4096
24- if data . match /DevTools listening on ws:\/ \/ 127.0.0.1:(\d +)(.*)/
25- port = $1
26- path = $2
16+ if File . exist? p = history_file
17+ port , path = File . read ( p ) . split ( "\n " )
2718 else
28- if File . exist? "#{ @dir } /DevToolsActivePort"
29- port , path = File . read ( "#{ @dir } /DevToolsActivePort" ) . split ( "\n " )
30- else
31- raise "Can't open Chrome browser"
32- end
19+ port , path = run_new_chrome
20+ end
21+ begin
22+ s = Socket . tcp "127.0.0.1" , port
23+ rescue Errno ::ECONNREFUSED
24+ port , path = run_new_chrome
25+ retry
3326 end
3427
35- s = Socket . tcp "127.0.0.1" , port
3628 ws_client = WebSocketClient . new ( s )
3729 ws_client . handshake port , path
3830 ws_client . send id : 1 , method : 'Target.getTargets'
@@ -59,6 +51,10 @@ def setup_chrome
5951 end
6052 end
6153
54+ def history_file
55+ CONFIG [ :chrome_history_file ] || File . expand_path ( '~/.rdbg_chrome_history' )
56+ end
57+
6258 def set_chrome_path
6359 # The process to check OS is based on `selenium` project.
6460 case RbConfig ::CONFIG [ 'host_os' ]
@@ -73,6 +69,29 @@ def set_chrome_path
7369 end
7470 end
7571
72+ def run_new_chrome
73+ set_chrome_path
74+ @dir = Dir . mktmpdir
75+ # The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
76+ stdin , stdout , stderr , @wait_thr = *Open3 . popen3 ( "#{ CONFIG [ :chrome_path ] } --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{ @dir } " )
77+ stdin . close
78+ stdout . close
79+
80+ data = stderr . readpartial 4096
81+ if data . match /DevTools listening on ws:\/ \/ 127.0.0.1:(\d +)(.*)/
82+ port = $1
83+ path = $2
84+ else
85+ if File . exist? "#{ @dir } /DevToolsActivePort"
86+ port , path = File . read ( "#{ @dir } /DevToolsActivePort" ) . split ( "\n " )
87+ else
88+ raise "Can't open Chrome browser"
89+ end
90+ end
91+ f = File . open ( history_file , "w" ) { |f | f . print "#{ port } \n #{ path } " }
92+ [ port , path ]
93+ end
94+
7695 class WebSocketClient
7796 def initialize s
7897 @sock = s
0 commit comments