Module: Browser

Defined in:
lib/platform_helpers/browser.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.backup_config(name, profile = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/platform_helpers/browser.rb', line 100

def self.backup_config(name, profile = nil)
  case name
  when "firefox" then
    orig = (Constants::CFG[:proxy][:cfg_dir] + "/#{profile}_prefs.js.orig")
    unless File.exists?(orig) then
      FileUtils.cp("#{Constants::BROWSER[name][:profile_dir]}/#{profile}/prefs.js", orig)
    end
  when "ie" then
    unless File.exists?("#{Constants::CFG[:proxy][:cfg_dir]}/ie_cfg.orig") then
      begin
        (reg_type, reg_val = nil
        fd = File.open((Constants::CFG[:proxy][:cfg_dir] + "/ie_cfg.orig"), "w+")
        fd.puts(Win32::Registry::HKEY_CURRENT_USER.open(Constants::BROWSER["ie"][:key], Win32::Registry::KEY_READ).entries.to_yaml)
        fd.close)
      rescue => e
        $logger.error("updating windows registry: #{e.inspect}")
      end
    end
  else
    # do nothing
  end
end

.is_installed?(name) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/platform_helpers/browser.rb', line 62

def self.is_installed?(name)
  if (Constants::PLATFORM == "linux") then
    return false if (name == "ie")
    if Constants::BROWSER.has_key?(name)
      w = `which #{Constants::BROWSER[name][:app_name]}`
      if ((not ("#{w}" == "")) and (not "#{w}" =~ /\/usr\/bin\/which: no/)) then
        return true
      end
    end
  else
    if (Constants::PLATFORM == "osx") then
      if File.exists?(Constants::BROWSER[name][:bin]) then
        $logger.debug("Detected #{name} running under osx")
        return true
      end
    else
      if (Constants::PLATFORM == "windows") then
        if Constants::BROWSER[name][:bin] then
          $logger.debug("Detected #{name} running under windows")
          return true if File.exists?(Constants::BROWSER[name][:bin])
        end
      end
    end
  end
  false
end

.profiles(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/platform_helpers/browser.rb', line 88

def self.profiles(name)
  case name
  when "firefox" then
    p = Dir.entries(Constants::BROWSER[name][:profile_dir])
    p.delete("profiles.ini")
    p.delete(".")
    p.delete("..")
    return p
  else
    # do nothing
  end
end

.restart_chrome(proxy_port = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/platform_helpers/browser.rb', line 25

def self.restart_chrome(proxy_port=nil)
  begin
#      if Constants::PLATFORM == 'osx'
#        ProcessMgt.kill(Constants::BROWSER['chrome'][:app_name])
#      else
      pid = ProcessMgt.pids(:search => Constants::BROWSER['chrome'][:app_name]).strip.split(' ').first
      ProcessMgt.kill(:pid_list => pid)
#      end
  rescue => e
    $logger.error "Error killing chrome: #{e.inspect}"
  end
  Browser.revive('chrome', proxy_port)

end

.restart_firefox(profile, proxy_port = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/platform_helpers/browser.rb', line 10

def self.restart_firefox(profile, proxy_port=nil)
  begin
    ProcessMgt.kill(Constants::BROWSER['firefox'][:app_name])
    if proxy_port
      Browser.backup_config('firefox', profile)
      Browser.update_proxy(:name => 'firefox', :profile => profile, :port => proxy_port)
    else
      Browser.restore_config('firefox', profile)
    end
    Browser.revive('firefox')
  rescue => e
    $logger.error "Error restarting Firefox: #{e.inspect}"
  end

end

.restart_ie(proxy_port = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/platform_helpers/browser.rb', line 40

def self.restart_ie(proxy_port=nil)
  $logger.debug "Updating proxy settings for IE: #{proxy_port}"
  begin
    ProcessMgt.kill(Constants::BROWSER['ie'][:app_name])
    ProcessMgt.kill('ieuser.exe')
  rescue => e
    $logger.error "killing ie -- #{e.inspect}"
  end
  sleep 2
  if proxy_port
    Browser.backup_config('ie')
    Browser.update_proxy(:name => 'ie', :port => proxy_port, :host => '127.0.0.1')
  else
puts "in restore from no proxy"
    Browser.restore_config('ie')
  end
  sleep 2
  Browser.revive('ie')
end

.restore_config(name, profile = nil) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
# File 'lib/platform_helpers/browser.rb', line 122

def self.restore_config(name, profile = nil)
  case name
  when "firefox" then
    if File.exists?("#{Constants::CFG[:proxy][:cfg_dir]}/#{profile}_prefs.js.orig") then
      FileUtils.mv("#{Constants::CFG[:proxy][:cfg_dir]}/#{profile}_prefs.js.orig", "#{Constants::BROWSER[name][:profile_dir]}/#{profile}/prefs.js")
    end
  when "ie" then
    $logger.debug("Updating registry for windows proxy")
    if File.exists?((Constants::CFG[:proxy][:cfg_dir] + "/ie_cfg.orig")) then
      begin
        Win32::Registry::HKEY_CURRENT_USER.open(Constants::BROWSER["ie"][:key], Win32::Registry::KEY_WRITE) do |reg2|
          reg2.write("ProxyEnable", Win32::Registry::REG_DWORD, 0)
        end
      rescue => e
        $logger.error("updating windows registry: #{e.inspect}")
      end
      cfg = YAML.load_file((Constants::CFG[:proxy][:cfg_dir] + "/ie_cfg.orig"))
      cfg.each do |i|
        begin
          Win32::Registry::HKEY_CURRENT_USER.open(Constants::BROWSER["ie"][:key], Win32::Registry::KEY_WRITE) do |reg3|
            reg3.write(i[0], i[1], i[2])
          end
        rescue => e
          $logger.error("updating windows registry: #{e.inspect}")
        end
      end
      begin
        FileUtils.rm_f((Constants::CFG[:proxy][:cfg_dir] + "/ie_cfg.orig"))
      rescue => e
        $logger.error("removing ie_cfg.orig -- #{e.inspect}")
      end
    end
  else
    # do nothing
  end
end

.revive(name, proxy = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/platform_helpers/browser.rb', line 198

def self.revive(name, proxy = nil)
  ops = nil
  ops = "--proxy-server=127.0.0.1:#{proxy}" if ((name == "chrome") and proxy)
  if (Constants::PLATFORM == "windows") then
    begin
      (p = Process.create("app_name" => ("#{Constants::BROWSER[name][:bin]} #{ops}"))
      Process.waitpid(p.process_id))
    rescue => e
      $logger.error("reviving #{name} with ops=#{ops} -- #{e.inspect}")
    end
  else
    browser = "#{Constants::BROWSER[name][:bin].gsub(" ", "\\ ")} #{ops}"
    $logger.debug("Starting browser #{name} with - #{browser}")
    begin
      Thread.new { `#{browser}` }
    rescue => e
      # do nothing
    end
  end
end

.update_proxy(params = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/platform_helpers/browser.rb', line 158

def self.update_proxy(params = {})
  params[:host] ||= "127.0.0.1"
  params[:profile] ||= nil
  case params[:name]
  when "ie" then
    begin
      Browser.backup_config("ie")
    rescue => e
      $logger.error("backing up config from update_proxy -- #{e.inspect}")
    end
    begin
      Win32::Registry::HKEY_CURRENT_USER.open(Constants::BROWSER["ie"][:key], Win32::Registry::KEY_WRITE) do |reg|
        reg.write("ProxyEnable", Win32::Registry::REG_DWORD, 1)
      end
    rescue => e
      puts("error updating proxy: #{e.inspect}")
      $logger.error("updating windows registry: #{e.inspect}")
    end
    begin
      Win32::Registry::HKEY_CURRENT_USER.open(Constants::BROWSER["ie"][:key], Win32::Registry::KEY_WRITE) do |reg|
        reg.write("ProxyServer", Win32::Registry::REG_SZ, "#{params[:host]}:#{params[:port]}")
      end
    rescue => e
      puts("error updating proxy: #{e.inspect}")
      $logger.error("updating windows registry: #{e.inspect}")
    end
  when "firefox" then
    prefs = File.readlines("#{Constants::BROWSER[params[:name]][:profile_dir]}/#{params[:profile]}/prefs.js")
    proxy_entries = ["user_pref(\"network.proxy.ftp\", \"#{params[:host]}\");", "user_pref(\"network.proxy.ftp_port\", #{params[:port]});", "user_pref(\"network.proxy.gopher\", \"#{params[:host]}\");", "user_pref(\"network.proxy.gopher_port\", #{params[:port]});", "user_pref(\"network.proxy.http\", \"#{params[:host]}\");", "user_pref(\"network.proxy.http_port\", #{params[:port]});", "user_pref(\"network.proxy.socks\", \"#{params[:host]}\");", "user_pref(\"network.proxy.socks_port\", #{params[:port]});", "user_pref(\"network.proxy.ssl\", \"#{params[:host]}\");", "user_pref(\"network.proxy.ssl_port\", #{params[:port]});", "user_pref(\"network.proxy.type\", 1);", "user_pref(\"network.proxy.share_proxy_settings\", true);"]
    prefs.each_with_index do |p, i|
      if (p =~ /network\.proxy/ and ((not p =~ /network\.proxy\.backup/) and ((not p =~ /network\.proxy\.no_proxies_on/) and p =~ /_port/))) then
        prefs.delete_at(i)
      end
    end
    prefs = (prefs + proxy_entries)
    fd = File.open("#{Constants::BROWSER[params[:name]][:profile_dir]}/#{params[:profile]}/prefs.js", "w+")
    prefs.each { |line| fd.puts(line) }
    fd.close
  end
end