| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/devtools/port_forwarding_controller.h" | 5 #include "chrome/browser/devtools/port_forwarding_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 kStatusOK = 0, | 42 kStatusOK = 0, |
| 43 // Positive values are used to count open connections. | 43 // Positive values are used to count open connections. |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 static const char kPortAttribute[] = "port"; | 46 static const char kPortAttribute[] = "port"; |
| 47 static const char kConnectionIdAttribute[] = "connectionId"; | 47 static const char kConnectionIdAttribute[] = "connectionId"; |
| 48 static const char kTetheringAccepted[] = "Tethering.accepted"; | 48 static const char kTetheringAccepted[] = "Tethering.accepted"; |
| 49 static const char kTetheringBind[] = "Tethering.bind"; | 49 static const char kTetheringBind[] = "Tethering.bind"; |
| 50 static const char kTetheringUnbind[] = "Tethering.unbind"; | 50 static const char kTetheringUnbind[] = "Tethering.unbind"; |
| 51 | 51 |
| 52 static const char kChromeProductName[] = "Chrome"; |
| 52 static const char kDevToolsRemoteBrowserTarget[] = "/devtools/browser"; | 53 static const char kDevToolsRemoteBrowserTarget[] = "/devtools/browser"; |
| 53 const int kMinVersionPortForwarding = 28; | 54 const int kMinVersionPortForwarding = 28; |
| 54 | 55 |
| 55 class SocketTunnel { | 56 class SocketTunnel { |
| 56 public: | 57 public: |
| 57 typedef base::Callback<void(int)> CounterCallback; | 58 typedef base::Callback<void(int)> CounterCallback; |
| 58 | 59 |
| 59 SocketTunnel(const std::string& location, const CounterCallback& callback) | 60 SocketTunnel(const std::string& location, const CounterCallback& callback) |
| 60 : location_(location), | 61 : location_(location), |
| 61 pending_writes_(0), | 62 pending_writes_(0), |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 scoped_ptr<net::StreamSocket> remote_socket_; | 214 scoped_ptr<net::StreamSocket> remote_socket_; |
| 214 scoped_ptr<net::StreamSocket> host_socket_; | 215 scoped_ptr<net::StreamSocket> host_socket_; |
| 215 scoped_ptr<net::HostResolver> host_resolver_; | 216 scoped_ptr<net::HostResolver> host_resolver_; |
| 216 net::AddressList address_list_; | 217 net::AddressList address_list_; |
| 217 int pending_writes_; | 218 int pending_writes_; |
| 218 bool pending_destruction_; | 219 bool pending_destruction_; |
| 219 CounterCallback callback_; | 220 CounterCallback callback_; |
| 220 bool about_to_destroy_; | 221 bool about_to_destroy_; |
| 221 }; | 222 }; |
| 222 | 223 |
| 223 typedef DevToolsAdbBridge::RemoteBrowser::ParsedVersion ParsedVersion; | 224 typedef std::vector<int> ParsedVersion; |
| 225 |
| 226 static ParsedVersion ParseVersion(const std::string& version) { |
| 227 ParsedVersion result; |
| 228 std::vector<std::string> parts; |
| 229 Tokenize(version, ".", &parts); |
| 230 for (size_t i = 0; i != parts.size(); ++i) { |
| 231 int value = 0; |
| 232 base::StringToInt(parts[i], &value); |
| 233 result.push_back(value); |
| 234 } |
| 235 return result; |
| 236 } |
| 224 | 237 |
| 225 static bool IsVersionLower(const ParsedVersion& left, | 238 static bool IsVersionLower(const ParsedVersion& left, |
| 226 const ParsedVersion& right) { | 239 const ParsedVersion& right) { |
| 227 return std::lexicographical_compare( | 240 return std::lexicographical_compare( |
| 228 left.begin(), left.end(), right.begin(), right.end()); | 241 left.begin(), left.end(), right.begin(), right.end()); |
| 229 } | 242 } |
| 230 | 243 |
| 231 static bool IsPortForwardingSupported(const ParsedVersion& version) { | 244 static bool IsPortForwardingSupported(const ParsedVersion& version) { |
| 232 return !version.empty() && version[0] >= kMinVersionPortForwarding; | 245 return !version.empty() && version[0] >= kMinVersionPortForwarding; |
| 233 } | 246 } |
| 234 | 247 |
| 235 static std::string FindBestSocketForTethering( | 248 static std::string FindBestSocketForTethering( |
| 236 const DevToolsAdbBridge::RemoteBrowsers browsers) { | 249 const DevToolsAdbBridge::RemoteBrowsers browsers) { |
| 237 std::string socket; | 250 std::string socket; |
| 238 ParsedVersion newest_version; | 251 ParsedVersion newest_version; |
| 239 for (DevToolsAdbBridge::RemoteBrowsers::const_iterator it = browsers.begin(); | 252 for (DevToolsAdbBridge::RemoteBrowsers::const_iterator it = browsers.begin(); |
| 240 it != browsers.end(); ++it) { | 253 it != browsers.end(); ++it) { |
| 241 scoped_refptr<DevToolsAdbBridge::RemoteBrowser> browser = *it; | 254 scoped_refptr<DevToolsAdbBridge::RemoteBrowser> browser = *it; |
| 242 ParsedVersion current_version = browser->GetParsedVersion(); | 255 ParsedVersion current_version = ParseVersion(browser->version()); |
| 243 if (browser->IsChrome() && | 256 if (browser->product() == kChromeProductName && |
| 244 IsPortForwardingSupported(current_version) && | 257 IsPortForwardingSupported(current_version) && |
| 245 IsVersionLower(newest_version, current_version)) { | 258 IsVersionLower(newest_version, current_version)) { |
| 246 socket = browser->socket(); | 259 socket = browser->socket(); |
| 247 newest_version = current_version; | 260 newest_version = current_version; |
| 248 } | 261 } |
| 249 } | 262 } |
| 250 return socket; | 263 return socket; |
| 251 } | 264 } |
| 252 | 265 |
| 253 } // namespace | 266 } // namespace |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 BrowserContextDependencyManager::GetInstance()) {} | 631 BrowserContextDependencyManager::GetInstance()) {} |
| 619 | 632 |
| 620 PortForwardingController::Factory::~Factory() {} | 633 PortForwardingController::Factory::~Factory() {} |
| 621 | 634 |
| 622 BrowserContextKeyedService* | 635 BrowserContextKeyedService* |
| 623 PortForwardingController::Factory::BuildServiceInstanceFor( | 636 PortForwardingController::Factory::BuildServiceInstanceFor( |
| 624 content::BrowserContext* context) const { | 637 content::BrowserContext* context) const { |
| 625 Profile* profile = Profile::FromBrowserContext(context); | 638 Profile* profile = Profile::FromBrowserContext(context); |
| 626 return new PortForwardingController(profile->GetPrefs()); | 639 return new PortForwardingController(profile->GetPrefs()); |
| 627 } | 640 } |
| OLD | NEW |