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 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 pref_service_(profile->GetPrefs()), | 534 pref_service_(profile->GetPrefs()), |
535 listening_(false) { | 535 listening_(false) { |
536 pref_change_registrar_.Init(pref_service_); | 536 pref_change_registrar_.Init(pref_service_); |
537 base::Closure callback = base::Bind( | 537 base::Closure callback = base::Bind( |
538 &PortForwardingController::OnPrefsChange, base::Unretained(this)); | 538 &PortForwardingController::OnPrefsChange, base::Unretained(this)); |
539 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, callback); | 539 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, callback); |
540 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, callback); | 540 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, callback); |
541 OnPrefsChange(); | 541 OnPrefsChange(); |
542 } | 542 } |
543 | 543 |
544 | |
545 PortForwardingController::~PortForwardingController() {} | 544 PortForwardingController::~PortForwardingController() {} |
546 | 545 |
547 void PortForwardingController::Shutdown() { | 546 PortForwardingController::DevicesStatus |
548 // Existing connection will not be shut down. This might be confusing for | 547 PortForwardingController::DeviceListChanged( |
549 // some users, but the opposite is more confusing. | |
550 StopListening(); | |
551 } | |
552 | |
553 void PortForwardingController::AddListener(Listener* listener) { | |
554 listeners_.push_back(listener); | |
555 } | |
556 | |
557 void PortForwardingController::RemoveListener(Listener* listener) { | |
558 Listeners::iterator it = | |
559 std::find(listeners_.begin(), listeners_.end(), listener); | |
560 DCHECK(it != listeners_.end()); | |
561 listeners_.erase(it); | |
562 } | |
563 | |
564 void PortForwardingController::DeviceListChanged( | |
565 const DevToolsAndroidBridge::RemoteDevices& devices) { | 548 const DevToolsAndroidBridge::RemoteDevices& devices) { |
566 DevicesStatus status; | 549 DevicesStatus status; |
567 | 550 |
568 for (DevToolsAndroidBridge::RemoteDevices::const_iterator it = | 551 for (DevToolsAndroidBridge::RemoteDevices::const_iterator it = |
569 devices.begin(); it != devices.end(); ++it) { | 552 devices.begin(); it != devices.end(); ++it) { |
570 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> device = *it; | 553 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> device = *it; |
571 if (!device->is_connected()) | 554 if (!device->is_connected()) |
572 continue; | 555 continue; |
573 Registry::iterator rit = registry_.find(device->serial()); | 556 Registry::iterator rit = registry_.find(device->serial()); |
574 if (rit == registry_.end()) { | 557 if (rit == registry_.end()) { |
575 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser = | 558 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser = |
576 FindBestBrowserForTethering(device->browsers()); | 559 FindBestBrowserForTethering(device->browsers()); |
577 if (browser) { | 560 if (browser) { |
578 new Connection(®istry_, device, browser, forwarding_map_); | 561 new Connection(®istry_, device, browser, forwarding_map_); |
579 } | 562 } |
580 } else { | 563 } else { |
581 status[device->serial()] = (*rit).second->GetPortStatusMap(); | 564 status[device->serial()] = (*rit).second->GetPortStatusMap(); |
582 } | 565 } |
583 } | 566 } |
584 | 567 |
585 NotifyListeners(status); | 568 return status; |
586 } | 569 } |
587 | 570 |
588 void PortForwardingController::OnPrefsChange() { | 571 void PortForwardingController::OnPrefsChange() { |
589 forwarding_map_.clear(); | 572 forwarding_map_.clear(); |
590 | 573 |
591 if (pref_service_->GetBoolean(prefs::kDevToolsPortForwardingEnabled)) { | 574 if (pref_service_->GetBoolean(prefs::kDevToolsPortForwardingEnabled)) { |
592 const base::DictionaryValue* dict = | 575 const base::DictionaryValue* dict = |
593 pref_service_->GetDictionary(prefs::kDevToolsPortForwardingConfig); | 576 pref_service_->GetDictionary(prefs::kDevToolsPortForwardingConfig); |
594 for (base::DictionaryValue::Iterator it(*dict); | 577 for (base::DictionaryValue::Iterator it(*dict); |
595 !it.IsAtEnd(); it.Advance()) { | 578 !it.IsAtEnd(); it.Advance()) { |
596 int port_num; | 579 int port_num; |
597 std::string location; | 580 std::string location; |
598 if (base::StringToInt(it.key(), &port_num) && | 581 if (base::StringToInt(it.key(), &port_num) && |
599 dict->GetString(it.key(), &location)) | 582 dict->GetString(it.key(), &location)) |
600 forwarding_map_[port_num] = location; | 583 forwarding_map_[port_num] = location; |
601 } | 584 } |
602 } | 585 } |
603 | 586 |
604 if (!forwarding_map_.empty()) { | 587 if (!forwarding_map_.empty()) { |
605 StartListening(); | 588 listening_ = true; |
dgozman
2014/08/27 08:08:34
I don't see where |listening_| is used.
vkuzkokov
2014/09/01 16:13:41
Good point.
| |
606 UpdateConnections(); | 589 UpdateConnections(); |
607 } else { | 590 } else { |
608 StopListening(); | 591 listening_ = false; |
609 ShutdownConnections(); | 592 ShutdownConnections(); |
610 NotifyListeners(DevicesStatus()); | |
611 } | 593 } |
612 } | 594 } |
613 | 595 |
614 void PortForwardingController::StartListening() { | |
615 if (listening_) | |
616 return; | |
617 listening_ = true; | |
618 DevToolsAndroidBridge* android_bridge = | |
619 DevToolsAndroidBridge::Factory::GetForProfile(profile_); | |
620 if (android_bridge) | |
621 android_bridge->AddDeviceListListener(this); | |
622 | |
623 } | |
624 | |
625 void PortForwardingController::StopListening() { | |
626 if (!listening_) | |
627 return; | |
628 listening_ = false; | |
629 DevToolsAndroidBridge* android_bridge = | |
630 DevToolsAndroidBridge::Factory::GetForProfile(profile_); | |
631 if (android_bridge) | |
632 android_bridge->RemoveDeviceListListener(this); | |
633 } | |
634 | |
635 void PortForwardingController::UpdateConnections() { | 596 void PortForwardingController::UpdateConnections() { |
636 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) | 597 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) |
637 it->second->UpdateForwardingMap(forwarding_map_); | 598 it->second->UpdateForwardingMap(forwarding_map_); |
638 } | 599 } |
639 | 600 |
640 void PortForwardingController::ShutdownConnections() { | 601 void PortForwardingController::ShutdownConnections() { |
641 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) | 602 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) |
642 it->second->Shutdown(); | 603 it->second->Shutdown(); |
643 registry_.clear(); | 604 registry_.clear(); |
644 } | 605 } |
645 | |
646 void PortForwardingController::NotifyListeners( | |
647 const DevicesStatus& status) const { | |
648 Listeners copy(listeners_); // Iterate over copy. | |
649 for (Listeners::const_iterator it = copy.begin(); it != copy.end(); ++it) | |
650 (*it)->PortStatusChanged(status); | |
651 } | |
652 | |
653 // static | |
654 PortForwardingController::Factory* | |
655 PortForwardingController::Factory::GetInstance() { | |
656 return Singleton<PortForwardingController::Factory>::get(); | |
657 } | |
658 | |
659 // static | |
660 PortForwardingController* PortForwardingController::Factory::GetForProfile( | |
661 Profile* profile) { | |
662 return static_cast<PortForwardingController*>(GetInstance()-> | |
663 GetServiceForBrowserContext(profile, true)); | |
664 } | |
665 | |
666 PortForwardingController::Factory::Factory() | |
667 : BrowserContextKeyedServiceFactory( | |
668 "PortForwardingController", | |
669 BrowserContextDependencyManager::GetInstance()) {} | |
670 | |
671 PortForwardingController::Factory::~Factory() {} | |
672 | |
673 KeyedService* PortForwardingController::Factory::BuildServiceInstanceFor( | |
674 content::BrowserContext* context) const { | |
675 Profile* profile = Profile::FromBrowserContext(context); | |
676 return new PortForwardingController(profile); | |
677 } | |
OLD | NEW |