OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" | 5 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
18 #include "base/string_tokenizer.h" | 18 #include "base/string_tokenizer.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/chromeos/web_socket_proxy.h" | 21 #include "chrome/browser/chromeos/web_socket_proxy.h" |
22 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
25 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
26 #include "content/public/browser/browser_thread.h" | |
27 #include "content/public/browser/notification_observer.h" | 26 #include "content/public/browser/notification_observer.h" |
28 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
29 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
30 #include "content/public/common/url_constants.h" | 29 #include "content/public/common/url_constants.h" |
31 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
32 #include "net/base/network_change_notifier.h" | 31 #include "net/base/network_change_notifier.h" |
33 | 32 |
34 using content::BrowserThread; | |
35 | |
36 namespace { | 33 namespace { |
37 | 34 |
38 const char* kAllowedIds[] = { | 35 const char* kAllowedIds[] = { |
39 "haiffjcadagjlijoggckpgfnoeiflnem", | 36 "haiffjcadagjlijoggckpgfnoeiflnem", |
40 "gnedhmakppccajfpfiihfcdlnpgomkcf", | 37 "gnedhmakppccajfpfiihfcdlnpgomkcf", |
41 "fjcibdnjlbfnbfdjneajpipnlcppleek", | 38 "fjcibdnjlbfnbfdjneajpipnlcppleek", |
42 "okddffdblfhhnmhodogpojmfkjmhinfp" | 39 "okddffdblfhhnmhodogpojmfkjmhinfp" |
43 }; | 40 }; |
44 | 41 |
45 class OriginValidator { | 42 class OriginValidator { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 std::vector<std::string> allowed_ids_; | 94 std::vector<std::string> allowed_ids_; |
98 std::vector<std::string> allowed_origins_; | 95 std::vector<std::string> allowed_origins_; |
99 }; | 96 }; |
100 | 97 |
101 base::LazyInstance<OriginValidator> g_validator = LAZY_INSTANCE_INITIALIZER; | 98 base::LazyInstance<OriginValidator> g_validator = LAZY_INSTANCE_INITIALIZER; |
102 | 99 |
103 class ProxyLifetime | 100 class ProxyLifetime |
104 : public net::NetworkChangeNotifier::OnlineStateObserver, | 101 : public net::NetworkChangeNotifier::OnlineStateObserver, |
105 public content::NotificationObserver { | 102 public content::NotificationObserver { |
106 public: | 103 public: |
107 ProxyLifetime() : delay_ms_(1000), port_(-1), shutdown_requested_(false) { | 104 ProxyLifetime() |
| 105 : delay_ms_(1000), |
| 106 port_(-1), |
| 107 shutdown_requested_(false), |
| 108 web_socket_proxy_thread_("Chrome_WebSocketproxyThread") { |
108 DLOG(INFO) << "WebSocketProxyController initiation"; | 109 DLOG(INFO) << "WebSocketProxyController initiation"; |
109 BrowserThread::PostTask( | 110 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
110 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, | 111 web_socket_proxy_thread_.StartWithOptions(options); |
| 112 web_socket_proxy_thread_.message_loop()->PostTask( |
| 113 FROM_HERE, |
111 base::Bind(&ProxyLifetime::ProxyCallback, base::Unretained(this))); | 114 base::Bind(&ProxyLifetime::ProxyCallback, base::Unretained(this))); |
112 net::NetworkChangeNotifier::AddOnlineStateObserver(this); | 115 net::NetworkChangeNotifier::AddOnlineStateObserver(this); |
113 registrar_.Add( | 116 registrar_.Add( |
114 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, | 117 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, |
115 content::NotificationService::AllSources()); | 118 content::NotificationService::AllSources()); |
116 } | 119 } |
117 | 120 |
118 virtual ~ProxyLifetime() { | 121 virtual ~ProxyLifetime() { |
119 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 122 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
120 } | 123 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 { | 157 { |
155 base::AutoLock alk(lock_); | 158 base::AutoLock alk(lock_); |
156 delete server; | 159 delete server; |
157 server_ = NULL; | 160 server_ = NULL; |
158 if (!shutdown_requested_) { | 161 if (!shutdown_requested_) { |
159 // Proxy terminated unexpectedly or failed to start (it can happen due | 162 // Proxy terminated unexpectedly or failed to start (it can happen due |
160 // to a network problem). Keep trying. | 163 // to a network problem). Keep trying. |
161 if (delay_ms_ < 100 * 1000) | 164 if (delay_ms_ < 100 * 1000) |
162 (delay_ms_ *= 3) /= 2; | 165 (delay_ms_ *= 3) /= 2; |
163 | 166 |
164 BrowserThread::PostDelayedTask( | 167 MessageLoop::current()->PostDelayedTask( |
165 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, | 168 FROM_HERE, |
166 base::Bind(&ProxyLifetime::ProxyCallback, base::Unretained(this)), | 169 base::Bind(&ProxyLifetime::ProxyCallback, base::Unretained(this)), |
167 delay_ms_); | 170 delay_ms_); |
168 } | 171 } |
169 } | 172 } |
170 } | 173 } |
171 | 174 |
172 // Delay between next attempt to run proxy. | 175 // Delay between next attempt to run proxy. |
173 int volatile delay_ms_; | 176 int volatile delay_ms_; |
174 | 177 |
175 // Proxy listens for incoming websocket connections on this port. | 178 // Proxy listens for incoming websocket connections on this port. |
176 int volatile port_; | 179 int volatile port_; |
177 | 180 |
178 chromeos::WebSocketProxy* volatile server_; | 181 chromeos::WebSocketProxy* volatile server_; |
179 volatile bool shutdown_requested_; | 182 volatile bool shutdown_requested_; |
180 base::Lock lock_; | 183 base::Lock lock_; |
181 content::NotificationRegistrar registrar_; | 184 content::NotificationRegistrar registrar_; |
182 friend class chromeos::WebSocketProxyController; | 185 friend class chromeos::WebSocketProxyController; |
| 186 base::Thread web_socket_proxy_thread_; |
183 }; | 187 }; |
184 | 188 |
185 base::LazyInstance<ProxyLifetime> g_proxy_lifetime = LAZY_INSTANCE_INITIALIZER; | 189 base::LazyInstance<ProxyLifetime> g_proxy_lifetime = LAZY_INSTANCE_INITIALIZER; |
186 | 190 |
187 } // namespace | 191 } // namespace |
188 | 192 |
189 namespace chromeos { | 193 namespace chromeos { |
190 | 194 |
191 void FillWithExtensionsIdsWithPrivateAccess(std::vector<std::string>* ids) { | 195 void FillWithExtensionsIdsWithPrivateAccess(std::vector<std::string>* ids) { |
192 ids->clear(); | 196 ids->clear(); |
(...skipping 13 matching lines...) Expand all Loading... |
206 | 210 |
207 // static | 211 // static |
208 int WebSocketProxyController::GetPort() { | 212 int WebSocketProxyController::GetPort() { |
209 int port = g_proxy_lifetime.Get().GetPort(); | 213 int port = g_proxy_lifetime.Get().GetPort(); |
210 DCHECK(IsInitiated()); | 214 DCHECK(IsInitiated()); |
211 return port; | 215 return port; |
212 } | 216 } |
213 | 217 |
214 // static | 218 // static |
215 void WebSocketProxyController::Shutdown() { | 219 void WebSocketProxyController::Shutdown() { |
216 if (IsInitiated()) { | 220 if (!IsInitiated()) |
217 DLOG(INFO) << "WebSocketProxyController shutdown"; | 221 return; |
| 222 |
| 223 DLOG(INFO) << "WebSocketProxyController shutdown"; |
| 224 { |
218 base::AutoLock alk(g_proxy_lifetime.Get().lock_); | 225 base::AutoLock alk(g_proxy_lifetime.Get().lock_); |
219 g_proxy_lifetime.Get().shutdown_requested_ = true; | 226 g_proxy_lifetime.Get().shutdown_requested_ = true; |
220 if (g_proxy_lifetime.Get().server_) | 227 if (g_proxy_lifetime.Get().server_) |
221 g_proxy_lifetime.Get().server_->Shutdown(); | 228 g_proxy_lifetime.Get().server_->Shutdown(); |
222 } | 229 } |
| 230 g_proxy_lifetime.Get().web_socket_proxy_thread_.Stop(); |
223 } | 231 } |
224 | 232 |
225 // static | 233 // static |
226 bool WebSocketProxyController::CheckCredentials( | 234 bool WebSocketProxyController::CheckCredentials( |
227 const std::string& extension_id, | 235 const std::string& extension_id, |
228 const std::string& hostname, | 236 const std::string& hostname, |
229 unsigned short port, | 237 unsigned short port, |
230 ConnectionFlags flags) { | 238 ConnectionFlags flags) { |
231 return g_validator.Get().CheckCredentials( | 239 return g_validator.Get().CheckCredentials( |
232 extension_id, hostname, port, flags); | 240 extension_id, hostname, port, flags); |
233 } | 241 } |
234 | 242 |
235 } // namespace chromeos | 243 } // namespace chromeos |
OLD | NEW |