| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/media/router/discovery/dial/dial_registry.h" | 5 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | 16 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" |
| 17 #include "chrome/browser/media/router/discovery/dial/dial_service.h" | 17 #include "chrome/browser/media/router/discovery/dial/dial_service.h" |
| 18 #include "components/net_log/chrome_net_log.h" | 18 #include "components/net_log/chrome_net_log.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 | 20 |
| 21 using base::Time; | 21 using base::Time; |
| 22 using base::TimeDelta; | 22 using base::TimeDelta; |
| 23 using content::BrowserThread; | 23 using content::BrowserThread; |
| 24 using net::NetworkChangeNotifier; | 24 using net::NetworkChangeNotifier; |
| 25 | 25 |
| 26 namespace { |
| 27 |
| 28 // How often to poll for devices. |
| 29 const int kDialRefreshIntervalSecs = 120; |
| 30 |
| 31 // We prune a device if it does not respond after this time. |
| 32 const int kDialExpirationSecs = 240; |
| 33 |
| 34 // The maximum number of devices retained at once in the registry. |
| 35 const size_t kDialMaxDevices = 256; |
| 36 |
| 37 } // namespace |
| 38 |
| 26 namespace media_router { | 39 namespace media_router { |
| 27 | 40 |
| 28 DialRegistry::DialRegistry(base::TimeDelta refresh_interval, | 41 DialRegistry::DialRegistry() |
| 29 base::TimeDelta expiration, | |
| 30 const size_t max_devices) | |
| 31 : num_listeners_(0), | 42 : num_listeners_(0), |
| 32 registry_generation_(0), | 43 registry_generation_(0), |
| 33 last_event_registry_generation_(0), | 44 last_event_registry_generation_(0), |
| 34 label_count_(0), | 45 label_count_(0), |
| 35 refresh_interval_delta_(refresh_interval), | 46 refresh_interval_delta_( |
| 36 expiration_delta_(expiration), | 47 base::TimeDelta::FromSeconds(kDialRefreshIntervalSecs)), |
| 37 max_devices_(max_devices) { | 48 expiration_delta_(base::TimeDelta::FromSeconds(kDialExpirationSecs)), |
| 49 max_devices_(kDialMaxDevices) { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 39 DCHECK_GT(max_devices_, 0U); | 51 DCHECK_GT(max_devices_, 0U); |
| 52 // This is a leaky singleton, so there's no code to remove |this| as an |
| 53 // observer. |
| 40 NetworkChangeNotifier::AddNetworkChangeObserver(this); | 54 NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 41 } | 55 } |
| 42 | 56 |
| 43 DialRegistry::~DialRegistry() { | 57 // This is a leaky singleton and the dtor won't be called. |
| 58 DialRegistry::~DialRegistry() = default; |
| 59 |
| 60 // static |
| 61 DialRegistry* DialRegistry::GetInstance() { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 62 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 45 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 63 return base::Singleton<DialRegistry, |
| 64 base::LeakySingletonTraits<DialRegistry>>::get(); |
| 46 } | 65 } |
| 47 | 66 |
| 48 std::unique_ptr<DialService> DialRegistry::CreateDialService() { | 67 std::unique_ptr<DialService> DialRegistry::CreateDialService() { |
| 49 DCHECK(g_browser_process->net_log()); | 68 DCHECK(g_browser_process->net_log()); |
| 50 return base::MakeUnique<DialServiceImpl>(g_browser_process->net_log()); | 69 return base::MakeUnique<DialServiceImpl>(g_browser_process->net_log()); |
| 51 } | 70 } |
| 52 | 71 |
| 53 void DialRegistry::ClearDialService() { | 72 void DialRegistry::ClearDialService() { |
| 54 dial_.reset(); | 73 dial_.reset(); |
| 55 } | 74 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 165 } |
| 147 | 166 |
| 148 void DialRegistry::StartPeriodicDiscovery() { | 167 void DialRegistry::StartPeriodicDiscovery() { |
| 149 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 168 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 150 if (!ReadyToDiscover() || dial_) | 169 if (!ReadyToDiscover() || dial_) |
| 151 return; | 170 return; |
| 152 | 171 |
| 153 dial_ = CreateDialService(); | 172 dial_ = CreateDialService(); |
| 154 dial_->AddObserver(this); | 173 dial_->AddObserver(this); |
| 155 DoDiscovery(); | 174 DoDiscovery(); |
| 156 repeating_timer_.Start(FROM_HERE, refresh_interval_delta_, this, | 175 repeating_timer_.reset(new base::RepeatingTimer()); |
| 157 &DialRegistry::DoDiscovery); | 176 repeating_timer_->Start(FROM_HERE, refresh_interval_delta_, this, |
| 177 &DialRegistry::DoDiscovery); |
| 158 } | 178 } |
| 159 | 179 |
| 160 void DialRegistry::DoDiscovery() { | 180 void DialRegistry::DoDiscovery() { |
| 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 162 DCHECK(dial_); | 182 DCHECK(dial_); |
| 163 VLOG(2) << "About to discover!"; | 183 VLOG(2) << "About to discover!"; |
| 164 dial_->Discover(); | 184 dial_->Discover(); |
| 165 } | 185 } |
| 166 | 186 |
| 167 void DialRegistry::StopPeriodicDiscovery() { | 187 void DialRegistry::StopPeriodicDiscovery() { |
| 168 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 188 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 169 if (!dial_) | 189 if (!dial_) |
| 170 return; | 190 return; |
| 171 | 191 |
| 172 repeating_timer_.Stop(); | 192 repeating_timer_->Stop(); |
| 193 repeating_timer_.reset(); |
| 173 dial_->RemoveObserver(this); | 194 dial_->RemoveObserver(this); |
| 174 ClearDialService(); | 195 ClearDialService(); |
| 175 } | 196 } |
| 176 | 197 |
| 177 bool DialRegistry::PruneExpiredDevices() { | 198 bool DialRegistry::PruneExpiredDevices() { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 199 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 bool pruned_device = false; | 200 bool pruned_device = false; |
| 180 DeviceByLabelMap::iterator it = device_by_label_map_.begin(); | 201 DeviceByLabelMap::iterator it = device_by_label_map_.begin(); |
| 181 while (it != device_by_label_map_.end()) { | 202 while (it != device_by_label_map_.end()) { |
| 182 auto* device = it->second; | 203 auto* device = it->second; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 for (auto& observer : observers_) | 382 for (auto& observer : observers_) |
| 362 observer.OnDialDeviceEvent(devices); | 383 observer.OnDialDeviceEvent(devices); |
| 363 } | 384 } |
| 364 | 385 |
| 365 void DialRegistry::OnDialError(DialErrorCode type) { | 386 void DialRegistry::OnDialError(DialErrorCode type) { |
| 366 for (auto& observer : observers_) | 387 for (auto& observer : observers_) |
| 367 observer.OnDialError(type); | 388 observer.OnDialError(type); |
| 368 } | 389 } |
| 369 | 390 |
| 370 } // namespace media_router | 391 } // namespace media_router |
| OLD | NEW |