| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/device/port_forwarding_controller.h" | 5 #include "chrome/browser/devtools/device/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 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 return best_browser; | 249 return best_browser; |
| 250 } | 250 } |
| 251 | 251 |
| 252 } // namespace | 252 } // namespace |
| 253 | 253 |
| 254 class PortForwardingController::Connection | 254 class PortForwardingController::Connection |
| 255 : public AndroidDeviceManager::AndroidWebSocket::Delegate { | 255 : public AndroidDeviceManager::AndroidWebSocket::Delegate { |
| 256 public: | 256 public: |
| 257 Connection(Registry* registry, | 257 Connection(PortForwardingController* controller, |
| 258 scoped_refptr<AndroidDeviceManager::Device> device, | |
| 259 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 258 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
| 260 const ForwardingMap& forwarding_map); | 259 const ForwardingMap& forwarding_map); |
| 261 ~Connection() override; | 260 ~Connection() override; |
| 262 | 261 |
| 263 const PortStatusMap& GetPortStatusMap(); | 262 const PortStatusMap& GetPortStatusMap(); |
| 264 | 263 |
| 265 void UpdateForwardingMap(const ForwardingMap& new_forwarding_map); | 264 void UpdateForwardingMap(const ForwardingMap& new_forwarding_map); |
| 266 | 265 |
| 267 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser() { | 266 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser() { |
| 268 return browser_; | 267 return browser_; |
| 269 } | 268 } |
| 270 | 269 |
| 271 private: | 270 private: |
| 272 friend struct content::BrowserThread::DeleteOnThread< | 271 friend struct content::BrowserThread::DeleteOnThread< |
| 273 content::BrowserThread::UI>; | 272 content::BrowserThread::UI>; |
| 274 friend class base::DeleteHelper<Connection>; | 273 friend class base::DeleteHelper<Connection>; |
| 275 | 274 |
| 276 | |
| 277 typedef std::map<int, std::string> ForwardingMap; | 275 typedef std::map<int, std::string> ForwardingMap; |
| 278 | |
| 279 typedef base::Callback<void(PortStatus)> CommandCallback; | 276 typedef base::Callback<void(PortStatus)> CommandCallback; |
| 280 typedef std::map<int, CommandCallback> CommandCallbackMap; | 277 typedef std::map<int, CommandCallback> CommandCallbackMap; |
| 281 | 278 |
| 282 void SerializeChanges(const std::string& method, | 279 void SerializeChanges(const std::string& method, |
| 283 const ForwardingMap& old_map, | 280 const ForwardingMap& old_map, |
| 284 const ForwardingMap& new_map); | 281 const ForwardingMap& new_map); |
| 285 | 282 |
| 286 void SendCommand(const std::string& method, int port); | 283 void SendCommand(const std::string& method, int port); |
| 287 bool ProcessResponse(const std::string& json); | 284 bool ProcessResponse(const std::string& json); |
| 288 | 285 |
| 289 void ProcessBindResponse(int port, PortStatus status); | 286 void ProcessBindResponse(int port, PortStatus status); |
| 290 void ProcessUnbindResponse(int port, PortStatus status); | 287 void ProcessUnbindResponse(int port, PortStatus status); |
| 291 | 288 |
| 292 static void UpdateSocketCountOnHandlerThread( | 289 static void UpdateSocketCountOnHandlerThread( |
| 293 base::WeakPtr<Connection> weak_connection, int port, int increment); | 290 base::WeakPtr<Connection> weak_connection, int port, int increment); |
| 294 void UpdateSocketCount(int port, int increment); | 291 void UpdateSocketCount(int port, int increment); |
| 295 | 292 |
| 296 // DevToolsAndroidBridge::AndroidWebSocket::Delegate implementation: | 293 // DevToolsAndroidBridge::AndroidWebSocket::Delegate implementation: |
| 297 void OnSocketOpened() override; | 294 void OnSocketOpened() override; |
| 298 void OnFrameRead(const std::string& message) override; | 295 void OnFrameRead(const std::string& message) override; |
| 299 void OnSocketClosed() override; | 296 void OnSocketClosed() override; |
| 300 | 297 |
| 301 PortForwardingController::Registry* registry_; | 298 PortForwardingController* controller_; |
| 302 scoped_refptr<AndroidDeviceManager::Device> device_; | |
| 303 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_; | 299 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_; |
| 304 scoped_ptr<AndroidDeviceManager::AndroidWebSocket> web_socket_; | 300 scoped_ptr<AndroidDeviceManager::AndroidWebSocket> web_socket_; |
| 305 int command_id_; | 301 int command_id_; |
| 306 bool connected_; | 302 bool connected_; |
| 307 ForwardingMap forwarding_map_; | 303 ForwardingMap forwarding_map_; |
| 308 CommandCallbackMap pending_responses_; | 304 CommandCallbackMap pending_responses_; |
| 309 PortStatusMap port_status_; | 305 PortStatusMap port_status_; |
| 310 base::WeakPtrFactory<Connection> weak_factory_; | 306 base::WeakPtrFactory<Connection> weak_factory_; |
| 311 | 307 |
| 312 DISALLOW_COPY_AND_ASSIGN(Connection); | 308 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 313 }; | 309 }; |
| 314 | 310 |
| 315 PortForwardingController::Connection::Connection( | 311 PortForwardingController::Connection::Connection( |
| 316 Registry* registry, | 312 PortForwardingController* controller, |
| 317 scoped_refptr<AndroidDeviceManager::Device> device, | |
| 318 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 313 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
| 319 const ForwardingMap& forwarding_map) | 314 const ForwardingMap& forwarding_map) |
| 320 : registry_(registry), | 315 : controller_(controller), |
| 321 device_(device), | |
| 322 browser_(browser), | 316 browser_(browser), |
| 323 command_id_(0), | 317 command_id_(0), |
| 324 connected_(false), | 318 connected_(false), |
| 325 forwarding_map_(forwarding_map), | 319 forwarding_map_(forwarding_map), |
| 326 weak_factory_(this) { | 320 weak_factory_(this) { |
| 327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 328 (*registry_)[device_->serial()] = this; | 322 controller_->registry_[browser->serial()] = this; |
| 323 scoped_refptr<AndroidDeviceManager::Device> device( |
| 324 controller_->bridge_->FindDevice(browser->serial())); |
| 325 DCHECK(device.get()); |
| 329 web_socket_.reset( | 326 web_socket_.reset( |
| 330 device_->CreateWebSocket(browser->socket(), | 327 device->CreateWebSocket(browser->socket(), |
| 331 kDevToolsRemoteBrowserTarget, this)); | 328 kDevToolsRemoteBrowserTarget, this)); |
| 332 } | 329 } |
| 333 | 330 |
| 334 PortForwardingController::Connection::~Connection() { | 331 PortForwardingController::Connection::~Connection() { |
| 335 | |
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 337 DCHECK(registry_->find(device_->serial()) != registry_->end()); | 333 DCHECK(controller_->registry_.find(browser_->serial()) != |
| 338 registry_->erase(device_->serial()); | 334 controller_->registry_.end()); |
| 335 controller_->registry_.erase(browser_->serial()); |
| 339 } | 336 } |
| 340 | 337 |
| 341 void PortForwardingController::Connection::UpdateForwardingMap( | 338 void PortForwardingController::Connection::UpdateForwardingMap( |
| 342 const ForwardingMap& new_forwarding_map) { | 339 const ForwardingMap& new_forwarding_map) { |
| 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 344 if (connected_) { | 341 if (connected_) { |
| 345 SerializeChanges(tethering::unbind::kName, | 342 SerializeChanges(tethering::unbind::kName, |
| 346 new_forwarding_map, forwarding_map_); | 343 new_forwarding_map, forwarding_map_); |
| 347 SerializeChanges(tethering::bind::kName, | 344 SerializeChanges(tethering::bind::kName, |
| 348 forwarding_map_, new_forwarding_map); | 345 forwarding_map_, new_forwarding_map); |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 Tokenize(location, ":", &tokens); | 505 Tokenize(location, ":", &tokens); |
| 509 int destination_port = 0; | 506 int destination_port = 0; |
| 510 if (tokens.size() != 2 || !base::StringToInt(tokens[1], &destination_port)) | 507 if (tokens.size() != 2 || !base::StringToInt(tokens[1], &destination_port)) |
| 511 return; | 508 return; |
| 512 std::string destination_host = tokens[0]; | 509 std::string destination_host = tokens[0]; |
| 513 | 510 |
| 514 SocketTunnel::CounterCallback callback = | 511 SocketTunnel::CounterCallback callback = |
| 515 base::Bind(&Connection::UpdateSocketCountOnHandlerThread, | 512 base::Bind(&Connection::UpdateSocketCountOnHandlerThread, |
| 516 weak_factory_.GetWeakPtr(), port); | 513 weak_factory_.GetWeakPtr(), port); |
| 517 | 514 |
| 518 device_->OpenSocket( | 515 scoped_refptr<AndroidDeviceManager::Device> device( |
| 516 controller_->bridge_->FindDevice(browser_->serial())); |
| 517 DCHECK(device.get()); |
| 518 device->OpenSocket( |
| 519 connection_id.c_str(), | 519 connection_id.c_str(), |
| 520 base::Bind(&SocketTunnel::StartTunnel, | 520 base::Bind(&SocketTunnel::StartTunnel, |
| 521 destination_host, | 521 destination_host, |
| 522 destination_port, | 522 destination_port, |
| 523 callback)); | 523 callback)); |
| 524 } | 524 } |
| 525 | 525 |
| 526 PortForwardingController::PortForwardingController(Profile* profile) | 526 PortForwardingController::PortForwardingController( |
| 527 Profile* profile, |
| 528 DevToolsAndroidBridge* bridge) |
| 527 : profile_(profile), | 529 : profile_(profile), |
| 530 bridge_(bridge), |
| 528 pref_service_(profile->GetPrefs()) { | 531 pref_service_(profile->GetPrefs()) { |
| 529 pref_change_registrar_.Init(pref_service_); | 532 pref_change_registrar_.Init(pref_service_); |
| 530 base::Closure callback = base::Bind( | 533 base::Closure callback = base::Bind( |
| 531 &PortForwardingController::OnPrefsChange, base::Unretained(this)); | 534 &PortForwardingController::OnPrefsChange, base::Unretained(this)); |
| 532 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, callback); | 535 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, callback); |
| 533 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, callback); | 536 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, callback); |
| 534 OnPrefsChange(); | 537 OnPrefsChange(); |
| 535 } | 538 } |
| 536 | 539 |
| 537 PortForwardingController::~PortForwardingController() {} | 540 PortForwardingController::~PortForwardingController() {} |
| 538 | 541 |
| 539 PortForwardingController::ForwardingStatus | 542 PortForwardingController::ForwardingStatus |
| 540 PortForwardingController::DeviceListChanged( | 543 PortForwardingController::DeviceListChanged( |
| 541 const DevToolsAndroidBridge::CompleteDevices& complete_devices) { | 544 const DevToolsAndroidBridge::RemoteDevices& devices) { |
| 542 ForwardingStatus status; | 545 ForwardingStatus status; |
| 543 if (forwarding_map_.empty()) | 546 if (forwarding_map_.empty()) |
| 544 return status; | 547 return status; |
| 545 | 548 |
| 546 for (const auto& pair : complete_devices) { | 549 for (const auto& device : devices) { |
| 547 scoped_refptr<AndroidDeviceManager::Device> device(pair.first); | 550 if (!device->is_connected()) |
| 548 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> remote_device( | |
| 549 pair.second); | |
| 550 if (!remote_device->is_connected()) | |
| 551 continue; | 551 continue; |
| 552 Registry::iterator rit = registry_.find(remote_device->serial()); | 552 Registry::iterator rit = registry_.find(device->serial()); |
| 553 if (rit == registry_.end()) { | 553 if (rit == registry_.end()) { |
| 554 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser( | 554 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser( |
| 555 FindBestBrowserForTethering(remote_device->browsers())); | 555 FindBestBrowserForTethering(device->browsers())); |
| 556 if (browser.get()) | 556 if (browser.get()) |
| 557 new Connection(®istry_, device, browser, forwarding_map_); | 557 new Connection(this, browser, forwarding_map_); |
| 558 } else { | 558 } else { |
| 559 status.push_back(std::make_pair(rit->second->browser(), | 559 status.push_back(std::make_pair(rit->second->browser(), |
| 560 rit->second->GetPortStatusMap())); | 560 rit->second->GetPortStatusMap())); |
| 561 } | 561 } |
| 562 } | 562 } |
| 563 return status; | 563 return status; |
| 564 } | 564 } |
| 565 | 565 |
| 566 void PortForwardingController::OnPrefsChange() { | 566 void PortForwardingController::OnPrefsChange() { |
| 567 forwarding_map_.clear(); | 567 forwarding_map_.clear(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 588 registry_copy.push_back(it->second); | 588 registry_copy.push_back(it->second); |
| 589 } | 589 } |
| 590 STLDeleteElements(®istry_copy); | 590 STLDeleteElements(®istry_copy); |
| 591 } | 591 } |
| 592 } | 592 } |
| 593 | 593 |
| 594 void PortForwardingController::UpdateConnections() { | 594 void PortForwardingController::UpdateConnections() { |
| 595 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) | 595 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) |
| 596 it->second->UpdateForwardingMap(forwarding_map_); | 596 it->second->UpdateForwardingMap(forwarding_map_); |
| 597 } | 597 } |
| OLD | NEW |