Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1142)

Side by Side Diff: chrome/browser/devtools/device/port_forwarding_controller.cc

Issue 500373004: DevTools: Make port forwarding part of DevToolsAndroidBridge. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed. Rebased Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 device_->OpenSocket( 524 device_->OpenSocket(
525 connection_id.c_str(), 525 connection_id.c_str(),
526 base::Bind(&SocketTunnel::StartTunnel, 526 base::Bind(&SocketTunnel::StartTunnel,
527 destination_host, 527 destination_host,
528 destination_port, 528 destination_port,
529 callback)); 529 callback));
530 } 530 }
531 531
532 PortForwardingController::PortForwardingController(Profile* profile) 532 PortForwardingController::PortForwardingController(Profile* profile)
533 : profile_(profile), 533 : profile_(profile),
534 pref_service_(profile->GetPrefs()), 534 pref_service_(profile->GetPrefs()) {
535 listening_(false) {
536 pref_change_registrar_.Init(pref_service_); 535 pref_change_registrar_.Init(pref_service_);
537 base::Closure callback = base::Bind( 536 base::Closure callback = base::Bind(
538 &PortForwardingController::OnPrefsChange, base::Unretained(this)); 537 &PortForwardingController::OnPrefsChange, base::Unretained(this));
539 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, callback); 538 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, callback);
540 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, callback); 539 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, callback);
541 OnPrefsChange(); 540 OnPrefsChange();
542 } 541 }
543 542
544
545 PortForwardingController::~PortForwardingController() {} 543 PortForwardingController::~PortForwardingController() {}
546 544
547 void PortForwardingController::Shutdown() { 545 PortForwardingController::DevicesStatus
548 // Existing connection will not be shut down. This might be confusing for 546 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) { 547 const DevToolsAndroidBridge::RemoteDevices& devices) {
566 DevicesStatus status; 548 DevicesStatus status;
549 if (forwarding_map_.empty())
550 return status;
567 551
568 for (DevToolsAndroidBridge::RemoteDevices::const_iterator it = 552 for (DevToolsAndroidBridge::RemoteDevices::const_iterator it =
569 devices.begin(); it != devices.end(); ++it) { 553 devices.begin(); it != devices.end(); ++it) {
570 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> device = *it; 554 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> device = *it;
571 if (!device->is_connected()) 555 if (!device->is_connected())
572 continue; 556 continue;
573 Registry::iterator rit = registry_.find(device->serial()); 557 Registry::iterator rit = registry_.find(device->serial());
574 if (rit == registry_.end()) { 558 if (rit == registry_.end()) {
575 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser = 559 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser =
576 FindBestBrowserForTethering(device->browsers()); 560 FindBestBrowserForTethering(device->browsers());
577 if (browser.get()) { 561 if (browser.get()) {
578 new Connection(&registry_, device, browser, forwarding_map_); 562 new Connection(&registry_, device, browser, forwarding_map_);
579 } 563 }
580 } else { 564 } else {
581 status[device->serial()] = (*rit).second->GetPortStatusMap(); 565 status[device->serial()] = (*rit).second->GetPortStatusMap();
582 } 566 }
583 } 567 }
584 568
585 NotifyListeners(status); 569 return status;
586 } 570 }
587 571
588 void PortForwardingController::OnPrefsChange() { 572 void PortForwardingController::OnPrefsChange() {
589 forwarding_map_.clear(); 573 forwarding_map_.clear();
590 574
591 if (pref_service_->GetBoolean(prefs::kDevToolsPortForwardingEnabled)) { 575 if (pref_service_->GetBoolean(prefs::kDevToolsPortForwardingEnabled)) {
592 const base::DictionaryValue* dict = 576 const base::DictionaryValue* dict =
593 pref_service_->GetDictionary(prefs::kDevToolsPortForwardingConfig); 577 pref_service_->GetDictionary(prefs::kDevToolsPortForwardingConfig);
594 for (base::DictionaryValue::Iterator it(*dict); 578 for (base::DictionaryValue::Iterator it(*dict);
595 !it.IsAtEnd(); it.Advance()) { 579 !it.IsAtEnd(); it.Advance()) {
596 int port_num; 580 int port_num;
597 std::string location; 581 std::string location;
598 if (base::StringToInt(it.key(), &port_num) && 582 if (base::StringToInt(it.key(), &port_num) &&
599 dict->GetString(it.key(), &location)) 583 dict->GetString(it.key(), &location))
600 forwarding_map_[port_num] = location; 584 forwarding_map_[port_num] = location;
601 } 585 }
602 } 586 }
603 587
604 if (!forwarding_map_.empty()) { 588 if (!forwarding_map_.empty()) {
605 StartListening();
606 UpdateConnections(); 589 UpdateConnections();
607 } else { 590 } else {
608 StopListening();
609 ShutdownConnections(); 591 ShutdownConnections();
610 NotifyListeners(DevicesStatus());
611 } 592 }
612 } 593 }
613 594
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() { 595 void PortForwardingController::UpdateConnections() {
636 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) 596 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it)
637 it->second->UpdateForwardingMap(forwarding_map_); 597 it->second->UpdateForwardingMap(forwarding_map_);
638 } 598 }
639 599
640 void PortForwardingController::ShutdownConnections() { 600 void PortForwardingController::ShutdownConnections() {
641 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) 601 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it)
642 it->second->Shutdown(); 602 it->second->Shutdown();
643 registry_.clear(); 603 registry_.clear();
644 } 604 }
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 }
OLDNEW
« no previous file with comments | « chrome/browser/devtools/device/port_forwarding_controller.h ('k') | chrome/browser/devtools/devtools_targets_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698