Chromium Code Reviews| 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/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/string_tokenizer.h" | 16 #include "base/string_tokenizer.h" |
| 17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/chromeos/web_socket_proxy.h" | 19 #include "chrome/browser/chromeos/web_socket_proxy.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
| 23 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 24 #include "content/browser/browser_thread.h" | 24 #include "content/browser/browser_thread.h" |
| 25 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
| 26 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
| 27 #include "content/public/browser/notification_service.h" | |
| 27 #include "content/public/common/url_constants.h" | 28 #include "content/public/common/url_constants.h" |
| 28 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
| 29 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 const char* kAllowedIds[] = { | 34 const char* kAllowedIds[] = { |
| 34 "haiffjcadagjlijoggckpgfnoeiflnem", | 35 "haiffjcadagjlijoggckpgfnoeiflnem", |
| 35 "gnedhmakppccajfpfiihfcdlnpgomkcf", | 36 "gnedhmakppccajfpfiihfcdlnpgomkcf", |
| 36 "fjcibdnjlbfnbfdjneajpipnlcppleek", | 37 "fjcibdnjlbfnbfdjneajpipnlcppleek", |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 std::vector<std::string> allowed_ids_; | 93 std::vector<std::string> allowed_ids_; |
| 93 std::vector<std::string> allowed_origins_; | 94 std::vector<std::string> allowed_origins_; |
| 94 }; | 95 }; |
| 95 | 96 |
| 96 base::LazyInstance<OriginValidator> g_validator(base::LINKER_INITIALIZED); | 97 base::LazyInstance<OriginValidator> g_validator(base::LINKER_INITIALIZED); |
| 97 | 98 |
| 98 class ProxyTask : public Task { | 99 class ProxyTask : public Task { |
| 99 virtual void Run() OVERRIDE; | 100 virtual void Run() OVERRIDE; |
| 100 }; | 101 }; |
| 101 | 102 |
| 102 class ProxyLifetime : public net::NetworkChangeNotifier::OnlineStateObserver { | 103 class ProxyLifetime |
| 104 : public net::NetworkChangeNotifier::OnlineStateObserver, | |
| 105 public content::NotificationObserver { | |
| 103 public: | 106 public: |
| 104 ProxyLifetime() : delay_ms_(1000), shutdown_requested_(false) { | 107 ProxyLifetime() : delay_ms_(1000), port_(-1), shutdown_requested_(false) { |
| 105 BrowserThread::PostTask( | 108 BrowserThread::PostTask( |
| 106 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); | 109 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); |
| 107 net::NetworkChangeNotifier::AddOnlineStateObserver(this); | 110 net::NetworkChangeNotifier::AddOnlineStateObserver(this); |
| 111 registrar_.Add( | |
| 112 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, | |
| 113 content::NotificationService::AllSources()); | |
| 108 } | 114 } |
| 109 | 115 |
| 110 virtual ~ProxyLifetime() { | 116 virtual ~ProxyLifetime() { |
| 111 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 117 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
| 112 } | 118 } |
| 113 | 119 |
| 120 virtual void Observe(int type, const content::NotificationSource& source, | |
| 121 const content::NotificationDetails& details) OVERRIDE { | |
| 122 base::AutoLock alk(lock_); | |
| 123 port_ = *content::Details<int>(details).ptr(); | |
| 124 } | |
| 125 | |
| 126 int GetPort() { | |
| 127 base::AutoLock alk(lock_); | |
| 128 return port_; | |
|
miket_OOO
2011/10/24 17:50:46
I can't say that synchronizing an int getter/sette
Denis Lagno
2011/10/24 19:47:58
It should be infrequent operation, it is called on
miket_OOO
2011/10/24 19:53:00
It's fine, and the code is correct. I'm just think
| |
| 129 } | |
| 130 | |
| 114 private: | 131 private: |
| 115 // net::NetworkChangeNotifier::OnlineStateObserver overrides. | 132 // net::NetworkChangeNotifier::OnlineStateObserver overrides. |
| 116 virtual void OnOnlineStateChanged(bool online) OVERRIDE { | 133 virtual void OnOnlineStateChanged(bool online) OVERRIDE { |
| 117 DCHECK(chromeos::WebSocketProxyController::IsInitiated()); | 134 DCHECK(chromeos::WebSocketProxyController::IsInitiated()); |
| 118 base::AutoLock alk(lock_); | 135 base::AutoLock alk(lock_); |
| 119 if (server_) | 136 if (server_) |
| 120 server_->OnNetworkChange(); | 137 server_->OnNetworkChange(); |
| 121 } | 138 } |
| 122 | 139 |
| 123 // Delay between next attempt to run proxy. | 140 // Delay between next attempt to run proxy. |
| 124 int delay_ms_; | 141 int volatile delay_ms_; |
| 142 | |
| 143 // Proxy listens incoming websocket connections on this port. | |
|
miket_OOO
2011/10/24 17:50:46
"listens for incoming"
Denis Lagno
2011/10/24 19:47:58
Done.
| |
| 144 int volatile port_; | |
| 125 | 145 |
| 126 chromeos::WebSocketProxy* volatile server_; | 146 chromeos::WebSocketProxy* volatile server_; |
| 127 volatile bool shutdown_requested_; | 147 volatile bool shutdown_requested_; |
| 128 base::Lock lock_; | 148 base::Lock lock_; |
| 129 content::NotificationRegistrar registrar_; | 149 content::NotificationRegistrar registrar_; |
| 130 friend class ProxyTask; | 150 friend class ProxyTask; |
| 131 friend class chromeos::WebSocketProxyController; | 151 friend class chromeos::WebSocketProxyController; |
| 132 }; | 152 }; |
| 133 | 153 |
| 134 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); | 154 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 LOG(INFO) << "WebSocketProxyController initiation"; | 196 LOG(INFO) << "WebSocketProxyController initiation"; |
| 177 g_proxy_lifetime.Get(); | 197 g_proxy_lifetime.Get(); |
| 178 } | 198 } |
| 179 | 199 |
| 180 // static | 200 // static |
| 181 bool WebSocketProxyController::IsInitiated() { | 201 bool WebSocketProxyController::IsInitiated() { |
| 182 return !(g_proxy_lifetime == NULL); | 202 return !(g_proxy_lifetime == NULL); |
| 183 } | 203 } |
| 184 | 204 |
| 185 // static | 205 // static |
| 206 int WebSocketProxyController::GetPort() { | |
| 207 if (IsInitiated()) | |
| 208 return g_proxy_lifetime.Get().GetPort(); | |
| 209 else | |
| 210 return -1; | |
|
miket_OOO
2011/10/24 17:50:46
Is this case a run-time error or a logic error? Ca
Denis Lagno
2011/10/24 19:47:58
I've changed the code.
Generally we do not want t
| |
| 211 } | |
| 212 | |
| 213 // static | |
| 186 void WebSocketProxyController::Shutdown() { | 214 void WebSocketProxyController::Shutdown() { |
| 187 if (IsInitiated()) { | 215 if (IsInitiated()) { |
| 188 LOG(INFO) << "WebSocketProxyController shutdown"; | 216 LOG(INFO) << "WebSocketProxyController shutdown"; |
| 189 base::AutoLock alk(g_proxy_lifetime.Get().lock_); | 217 base::AutoLock alk(g_proxy_lifetime.Get().lock_); |
| 190 g_proxy_lifetime.Get().shutdown_requested_ = true; | 218 g_proxy_lifetime.Get().shutdown_requested_ = true; |
| 191 if (g_proxy_lifetime.Get().server_) | 219 if (g_proxy_lifetime.Get().server_) |
| 192 g_proxy_lifetime.Get().server_->Shutdown(); | 220 g_proxy_lifetime.Get().server_->Shutdown(); |
| 193 } | 221 } |
| 194 } | 222 } |
| 195 | 223 |
| 196 // static | 224 // static |
| 197 bool WebSocketProxyController::CheckCredentials( | 225 bool WebSocketProxyController::CheckCredentials( |
| 198 const std::string& extension_id, | 226 const std::string& extension_id, |
| 199 const std::string& hostname, | 227 const std::string& hostname, |
| 200 unsigned short port, | 228 unsigned short port, |
| 201 ConnectionFlags flags) { | 229 ConnectionFlags flags) { |
| 202 return g_validator.Get().CheckCredentials( | 230 return g_validator.Get().CheckCredentials( |
| 203 extension_id, hostname, port, flags); | 231 extension_id, hostname, port, flags); |
| 204 } | 232 } |
| 205 | 233 |
| 206 } // namespace chromeos | 234 } // namespace chromeos |
| OLD | NEW |