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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 265 |
266 } // namespace | 266 } // namespace |
267 | 267 |
268 class PortForwardingController::Connection | 268 class PortForwardingController::Connection |
269 : public AdbWebSocket::Delegate, | 269 : public AdbWebSocket::Delegate, |
270 public base::RefCountedThreadSafe< | 270 public base::RefCountedThreadSafe< |
271 Connection, | 271 Connection, |
272 content::BrowserThread::DeleteOnUIThread> { | 272 content::BrowserThread::DeleteOnUIThread> { |
273 public: | 273 public: |
274 Connection(Registry* registry, | 274 Connection(Registry* registry, |
275 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | 275 scoped_refptr<AndroidDevice> device, |
276 const std::string& socket, | 276 const std::string& socket, |
277 scoped_refptr<DevToolsAdbBridge> bridge, | 277 scoped_refptr<RefCountedAdbThread> adb_thread, |
278 PrefService* pref_service); | 278 PrefService* pref_service); |
279 | 279 |
280 const PortStatusMap& GetPortStatusMap(); | 280 const PortStatusMap& GetPortStatusMap(); |
281 | 281 |
282 void Shutdown(); | 282 void Shutdown(); |
283 | 283 |
284 private: | 284 private: |
285 friend struct content::BrowserThread::DeleteOnThread< | 285 friend struct content::BrowserThread::DeleteOnThread< |
286 content::BrowserThread::UI>; | 286 content::BrowserThread::UI>; |
287 friend class base::DeleteHelper<Connection>; | 287 friend class base::DeleteHelper<Connection>; |
(...skipping 22 matching lines...) Expand all Loading... |
310 void UpdatePortStatusMap(); | 310 void UpdatePortStatusMap(); |
311 void UpdatePortStatusMapOnUIThread(const PortStatusMap& status_map); | 311 void UpdatePortStatusMapOnUIThread(const PortStatusMap& status_map); |
312 | 312 |
313 // AdbWebSocket::Delegate implementation: | 313 // AdbWebSocket::Delegate implementation: |
314 virtual void OnSocketOpened() OVERRIDE; | 314 virtual void OnSocketOpened() OVERRIDE; |
315 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 315 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
316 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; | 316 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; |
317 virtual bool ProcessIncomingMessage(const std::string& message) OVERRIDE; | 317 virtual bool ProcessIncomingMessage(const std::string& message) OVERRIDE; |
318 | 318 |
319 PortForwardingController::Registry* registry_; | 319 PortForwardingController::Registry* registry_; |
320 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device_; | 320 scoped_refptr<AndroidDevice> device_; |
321 scoped_refptr<DevToolsAdbBridge> bridge_; | 321 scoped_refptr<RefCountedAdbThread> adb_thread_; |
322 PrefChangeRegistrar pref_change_registrar_; | 322 PrefChangeRegistrar pref_change_registrar_; |
323 scoped_refptr<AdbWebSocket> web_socket_; | 323 scoped_refptr<AdbWebSocket> web_socket_; |
324 int command_id_; | 324 int command_id_; |
325 ForwardingMap forwarding_map_; | 325 ForwardingMap forwarding_map_; |
326 CommandCallbackMap pending_responses_; | 326 CommandCallbackMap pending_responses_; |
327 PortStatusMap port_status_; | 327 PortStatusMap port_status_; |
328 PortStatusMap port_status_on_ui_thread_; | 328 PortStatusMap port_status_on_ui_thread_; |
329 | 329 |
330 DISALLOW_COPY_AND_ASSIGN(Connection); | 330 DISALLOW_COPY_AND_ASSIGN(Connection); |
331 }; | 331 }; |
332 | 332 |
333 PortForwardingController::Connection::Connection( | 333 PortForwardingController::Connection::Connection( |
334 Registry* registry, | 334 Registry* registry, |
335 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | 335 scoped_refptr<AndroidDevice> device, |
336 const std::string& socket, | 336 const std::string& socket, |
337 scoped_refptr<DevToolsAdbBridge> bridge, | 337 scoped_refptr<RefCountedAdbThread> adb_thread, |
338 PrefService* pref_service) | 338 PrefService* pref_service) |
339 : registry_(registry), | 339 : registry_(registry), |
340 device_(device), | 340 device_(device), |
341 bridge_(bridge), | 341 adb_thread_(adb_thread), |
342 command_id_(0) { | 342 command_id_(0) { |
343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
344 pref_change_registrar_.Init(pref_service); | 344 pref_change_registrar_.Init(pref_service); |
345 (*registry_)[device_->serial()] = this; | 345 (*registry_)[device_->serial()] = this; |
346 web_socket_ = new AdbWebSocket( | 346 web_socket_ = new AdbWebSocket( |
347 device, socket, kDevToolsRemoteBrowserTarget, | 347 device, socket, kDevToolsRemoteBrowserTarget, |
348 bridge_->GetAdbMessageLoop(), this); | 348 adb_thread_->message_loop(), this); |
349 AddRef(); // Balanced in OnSocketClosed(); | 349 AddRef(); // Balanced in OnSocketClosed(); |
350 } | 350 } |
351 | 351 |
352 void PortForwardingController::Connection::Shutdown() { | 352 void PortForwardingController::Connection::Shutdown() { |
353 registry_ = NULL; | 353 registry_ = NULL; |
354 // This will have no effect if the socket is not connected yet. | 354 // This will have no effect if the socket is not connected yet. |
355 web_socket_->Disconnect(); | 355 web_socket_->Disconnect(); |
356 } | 356 } |
357 | 357 |
358 PortForwardingController::Connection::~Connection() { | 358 PortForwardingController::Connection::~Connection() { |
(...skipping 16 matching lines...) Expand all Loading... |
375 pref_service->GetDictionary(prefs::kDevToolsPortForwardingConfig); | 375 pref_service->GetDictionary(prefs::kDevToolsPortForwardingConfig); |
376 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 376 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
377 int port_num; | 377 int port_num; |
378 std::string location; | 378 std::string location; |
379 if (base::StringToInt(it.key(), &port_num) && | 379 if (base::StringToInt(it.key(), &port_num) && |
380 dict->GetString(it.key(), &location)) | 380 dict->GetString(it.key(), &location)) |
381 new_forwarding_map[port_num] = location; | 381 new_forwarding_map[port_num] = location; |
382 } | 382 } |
383 } | 383 } |
384 | 384 |
385 bridge_->GetAdbMessageLoop()->PostTask( | 385 adb_thread_->message_loop()->PostTask( |
386 FROM_HERE, | 386 FROM_HERE, |
387 base::Bind(&Connection::ChangeForwardingMap, | 387 base::Bind(&Connection::ChangeForwardingMap, |
388 this, new_forwarding_map)); | 388 this, new_forwarding_map)); |
389 } | 389 } |
390 | 390 |
391 void PortForwardingController::Connection::ChangeForwardingMap( | 391 void PortForwardingController::Connection::ChangeForwardingMap( |
392 ForwardingMap new_forwarding_map) { | 392 ForwardingMap new_forwarding_map) { |
393 DCHECK_EQ(base::MessageLoop::current(), bridge_->GetAdbMessageLoop()); | 393 DCHECK_EQ(base::MessageLoop::current(), adb_thread_->message_loop()); |
394 | 394 |
395 SerializeChanges(kTetheringUnbind, new_forwarding_map, forwarding_map_); | 395 SerializeChanges(kTetheringUnbind, new_forwarding_map, forwarding_map_); |
396 SerializeChanges(kTetheringBind, forwarding_map_, new_forwarding_map); | 396 SerializeChanges(kTetheringBind, forwarding_map_, new_forwarding_map); |
397 forwarding_map_ = new_forwarding_map; | 397 forwarding_map_ = new_forwarding_map; |
398 } | 398 } |
399 | 399 |
400 void PortForwardingController::Connection::SerializeChanges( | 400 void PortForwardingController::Connection::SerializeChanges( |
401 const std::string& method, | 401 const std::string& method, |
402 const ForwardingMap& old_map, | 402 const ForwardingMap& old_map, |
403 const ForwardingMap& new_map) { | 403 const ForwardingMap& new_map) { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 } | 535 } |
536 | 536 |
537 void PortForwardingController::Connection::OnSocketClosed( | 537 void PortForwardingController::Connection::OnSocketClosed( |
538 bool closed_by_device) { | 538 bool closed_by_device) { |
539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
540 Release(); // Balanced in the constructor. | 540 Release(); // Balanced in the constructor. |
541 } | 541 } |
542 | 542 |
543 bool PortForwardingController::Connection::ProcessIncomingMessage( | 543 bool PortForwardingController::Connection::ProcessIncomingMessage( |
544 const std::string& message) { | 544 const std::string& message) { |
545 DCHECK_EQ(base::MessageLoop::current(), bridge_->GetAdbMessageLoop()); | 545 DCHECK_EQ(base::MessageLoop::current(), adb_thread_->message_loop()); |
546 if (ProcessResponse(message)) | 546 if (ProcessResponse(message)) |
547 return true; | 547 return true; |
548 | 548 |
549 scoped_ptr<DevToolsProtocol::Notification> notification( | 549 scoped_ptr<DevToolsProtocol::Notification> notification( |
550 DevToolsProtocol::ParseNotification(message)); | 550 DevToolsProtocol::ParseNotification(message)); |
551 if (!notification) | 551 if (!notification) |
552 return false; | 552 return false; |
553 | 553 |
554 if (notification->method() != kTetheringAccepted) | 554 if (notification->method() != kTetheringAccepted) |
555 return false; | 555 return false; |
(...skipping 15 matching lines...) Expand all Loading... |
571 std::string location = it->second; | 571 std::string location = it->second; |
572 | 572 |
573 SocketTunnel* tunnel = new SocketTunnel(location, | 573 SocketTunnel* tunnel = new SocketTunnel(location, |
574 base::Bind(&Connection::UpdateSocketCount, this, port)); | 574 base::Bind(&Connection::UpdateSocketCount, this, port)); |
575 | 575 |
576 device_->OpenSocket(connection_id.c_str(), | 576 device_->OpenSocket(connection_id.c_str(), |
577 base::Bind(&SocketTunnel::Start, base::Unretained(tunnel))); | 577 base::Bind(&SocketTunnel::Start, base::Unretained(tunnel))); |
578 return true; | 578 return true; |
579 } | 579 } |
580 | 580 |
581 PortForwardingController::PortForwardingController( | 581 PortForwardingController::PortForwardingController(PrefService* pref_service) |
582 scoped_refptr<DevToolsAdbBridge> bridge, | 582 : adb_thread_(RefCountedAdbThread::GetInstance()), |
583 PrefService* pref_service) | |
584 : bridge_(bridge), | |
585 pref_service_(pref_service) { | 583 pref_service_(pref_service) { |
586 } | 584 } |
587 | 585 |
588 PortForwardingController::~PortForwardingController() { | 586 PortForwardingController::~PortForwardingController() { |
589 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) | 587 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) |
590 it->second->Shutdown(); | 588 it->second->Shutdown(); |
591 } | 589 } |
592 | 590 |
593 PortForwardingController::DevicesStatus | 591 PortForwardingController::DevicesStatus |
594 PortForwardingController::UpdateDeviceList( | 592 PortForwardingController::UpdateDeviceList( |
595 const DevToolsAdbBridge::RemoteDevices& devices) { | 593 const DevToolsAdbBridge::RemoteDevices& devices) { |
596 DevicesStatus status; | 594 DevicesStatus status; |
597 for (DevToolsAdbBridge::RemoteDevices::const_iterator it = devices.begin(); | 595 for (DevToolsAdbBridge::RemoteDevices::const_iterator it = devices.begin(); |
598 it != devices.end(); ++it) { | 596 it != devices.end(); ++it) { |
599 scoped_refptr<DevToolsAdbBridge::RemoteDevice> device = *it; | 597 scoped_refptr<DevToolsAdbBridge::RemoteDevice> device = *it; |
600 Registry::iterator rit = registry_.find(device->serial()); | 598 Registry::iterator rit = registry_.find(device->serial()); |
601 if (rit == registry_.end()) { | 599 if (rit == registry_.end()) { |
602 std::string socket = FindBestSocketForTethering(device->browsers()); | 600 std::string socket = FindBestSocketForTethering(device->browsers()); |
603 if (!socket.empty() || device->serial().empty()) { | 601 if (!socket.empty() || device->serial().empty()) { |
604 // Will delete itself when disconnected. | 602 // Will delete itself when disconnected. |
605 new Connection( | 603 new Connection( |
606 ®istry_, device->device(), socket, bridge_, pref_service_); | 604 ®istry_, device->device(), socket, adb_thread_, pref_service_); |
607 } | 605 } |
608 } else { | 606 } else { |
609 status[device->serial()] = (*rit).second->GetPortStatusMap(); | 607 status[device->serial()] = (*rit).second->GetPortStatusMap(); |
610 } | 608 } |
611 } | 609 } |
612 return status; | 610 return status; |
613 } | 611 } |
614 | 612 |
615 // static | 613 // static |
616 PortForwardingController::Factory* | 614 PortForwardingController::Factory* |
(...skipping 12 matching lines...) Expand all Loading... |
629 : BrowserContextKeyedServiceFactory( | 627 : BrowserContextKeyedServiceFactory( |
630 "PortForwardingController", | 628 "PortForwardingController", |
631 BrowserContextDependencyManager::GetInstance()) {} | 629 BrowserContextDependencyManager::GetInstance()) {} |
632 | 630 |
633 PortForwardingController::Factory::~Factory() {} | 631 PortForwardingController::Factory::~Factory() {} |
634 | 632 |
635 BrowserContextKeyedService* | 633 BrowserContextKeyedService* |
636 PortForwardingController::Factory::BuildServiceInstanceFor( | 634 PortForwardingController::Factory::BuildServiceInstanceFor( |
637 content::BrowserContext* context) const { | 635 content::BrowserContext* context) const { |
638 Profile* profile = Profile::FromBrowserContext(context); | 636 Profile* profile = Profile::FromBrowserContext(context); |
639 return new PortForwardingController( | 637 return new PortForwardingController(profile->GetPrefs()); |
640 DevToolsAdbBridge::Factory::GetForProfile(profile), | |
641 profile->GetPrefs()); | |
642 } | 638 } |
OLD | NEW |