| 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) { |
| 108 DLOG(INFO) << "WebSocketProxyController initiation"; |
| 105 BrowserThread::PostTask( | 109 BrowserThread::PostTask( |
| 106 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); | 110 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); |
| 107 net::NetworkChangeNotifier::AddOnlineStateObserver(this); | 111 net::NetworkChangeNotifier::AddOnlineStateObserver(this); |
| 112 registrar_.Add( |
| 113 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, |
| 114 content::NotificationService::AllSources()); |
| 108 } | 115 } |
| 109 | 116 |
| 110 virtual ~ProxyLifetime() { | 117 virtual ~ProxyLifetime() { |
| 111 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 118 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
| 112 } | 119 } |
| 113 | 120 |
| 121 virtual void Observe(int type, const content::NotificationSource& source, |
| 122 const content::NotificationDetails& details) OVERRIDE { |
| 123 base::AutoLock alk(lock_); |
| 124 port_ = *content::Details<int>(details).ptr(); |
| 125 } |
| 126 |
| 127 int GetPort() { |
| 128 base::AutoLock alk(lock_); |
| 129 return port_; |
| 130 } |
| 131 |
| 114 private: | 132 private: |
| 115 // net::NetworkChangeNotifier::OnlineStateObserver overrides. | 133 // net::NetworkChangeNotifier::OnlineStateObserver overrides. |
| 116 virtual void OnOnlineStateChanged(bool online) OVERRIDE { | 134 virtual void OnOnlineStateChanged(bool online) OVERRIDE { |
| 117 DCHECK(chromeos::WebSocketProxyController::IsInitiated()); | 135 DCHECK(chromeos::WebSocketProxyController::IsInitiated()); |
| 118 base::AutoLock alk(lock_); | 136 base::AutoLock alk(lock_); |
| 119 if (server_) | 137 if (server_) |
| 120 server_->OnNetworkChange(); | 138 server_->OnNetworkChange(); |
| 121 } | 139 } |
| 122 | 140 |
| 123 // Delay between next attempt to run proxy. | 141 // Delay between next attempt to run proxy. |
| 124 int delay_ms_; | 142 int volatile delay_ms_; |
| 143 |
| 144 // Proxy listens for incoming websocket connections on this port. |
| 145 int volatile port_; |
| 125 | 146 |
| 126 chromeos::WebSocketProxy* volatile server_; | 147 chromeos::WebSocketProxy* volatile server_; |
| 127 volatile bool shutdown_requested_; | 148 volatile bool shutdown_requested_; |
| 128 base::Lock lock_; | 149 base::Lock lock_; |
| 129 content::NotificationRegistrar registrar_; | 150 content::NotificationRegistrar registrar_; |
| 130 friend class ProxyTask; | 151 friend class ProxyTask; |
| 131 friend class chromeos::WebSocketProxyController; | 152 friend class chromeos::WebSocketProxyController; |
| 132 }; | 153 }; |
| 133 | 154 |
| 134 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); | 155 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 namespace chromeos { | 187 namespace chromeos { |
| 167 | 188 |
| 168 void FillWithExtensionsIdsWithPrivateAccess(std::vector<std::string>* ids) { | 189 void FillWithExtensionsIdsWithPrivateAccess(std::vector<std::string>* ids) { |
| 169 ids->clear(); | 190 ids->clear(); |
| 170 for (size_t i = 0; i < arraysize(kAllowedIds); ++i) | 191 for (size_t i = 0; i < arraysize(kAllowedIds); ++i) |
| 171 ids->push_back(kAllowedIds[i]); | 192 ids->push_back(kAllowedIds[i]); |
| 172 } | 193 } |
| 173 | 194 |
| 174 // static | 195 // static |
| 175 void WebSocketProxyController::Initiate() { | 196 void WebSocketProxyController::Initiate() { |
| 176 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 int port = g_proxy_lifetime.Get().GetPort(); |
| 208 DCHECK(IsInitiated()); |
| 209 return port; |
| 210 } |
| 211 |
| 212 // static |
| 186 void WebSocketProxyController::Shutdown() { | 213 void WebSocketProxyController::Shutdown() { |
| 187 if (IsInitiated()) { | 214 if (IsInitiated()) { |
| 188 LOG(INFO) << "WebSocketProxyController shutdown"; | 215 DLOG(INFO) << "WebSocketProxyController shutdown"; |
| 189 base::AutoLock alk(g_proxy_lifetime.Get().lock_); | 216 base::AutoLock alk(g_proxy_lifetime.Get().lock_); |
| 190 g_proxy_lifetime.Get().shutdown_requested_ = true; | 217 g_proxy_lifetime.Get().shutdown_requested_ = true; |
| 191 if (g_proxy_lifetime.Get().server_) | 218 if (g_proxy_lifetime.Get().server_) |
| 192 g_proxy_lifetime.Get().server_->Shutdown(); | 219 g_proxy_lifetime.Get().server_->Shutdown(); |
| 193 } | 220 } |
| 194 } | 221 } |
| 195 | 222 |
| 196 // static | 223 // static |
| 197 bool WebSocketProxyController::CheckCredentials( | 224 bool WebSocketProxyController::CheckCredentials( |
| 198 const std::string& extension_id, | 225 const std::string& extension_id, |
| 199 const std::string& hostname, | 226 const std::string& hostname, |
| 200 unsigned short port, | 227 unsigned short port, |
| 201 ConnectionFlags flags) { | 228 ConnectionFlags flags) { |
| 202 return g_validator.Get().CheckCredentials( | 229 return g_validator.Get().CheckCredentials( |
| 203 extension_id, hostname, port, flags); | 230 extension_id, hostname, port, flags); |
| 204 } | 231 } |
| 205 | 232 |
| 206 } // namespace chromeos | 233 } // namespace chromeos |
| OLD | NEW |