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/default_clock.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | 17 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" |
17 #include "chrome/browser/media/router/discovery/dial/dial_service.h" | 18 #include "chrome/browser/media/router/discovery/dial/dial_service.h" |
18 #include "components/net_log/chrome_net_log.h" | 19 #include "components/net_log/chrome_net_log.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 | 21 |
21 using base::Time; | 22 using base::Time; |
22 using base::TimeDelta; | 23 using base::TimeDelta; |
(...skipping 13 matching lines...) Expand all Loading... |
36 | 37 |
37 } // namespace | 38 } // namespace |
38 | 39 |
39 namespace media_router { | 40 namespace media_router { |
40 | 41 |
41 DialRegistry::DialRegistry() | 42 DialRegistry::DialRegistry() |
42 : num_listeners_(0), | 43 : num_listeners_(0), |
43 registry_generation_(0), | 44 registry_generation_(0), |
44 last_event_registry_generation_(0), | 45 last_event_registry_generation_(0), |
45 label_count_(0), | 46 label_count_(0), |
46 refresh_interval_delta_( | 47 refresh_interval_delta_(TimeDelta::FromSeconds(kDialRefreshIntervalSecs)), |
47 base::TimeDelta::FromSeconds(kDialRefreshIntervalSecs)), | 48 expiration_delta_(TimeDelta::FromSeconds(kDialExpirationSecs)), |
48 expiration_delta_(base::TimeDelta::FromSeconds(kDialExpirationSecs)), | 49 max_devices_(kDialMaxDevices), |
49 max_devices_(kDialMaxDevices) { | 50 clock_(new base::DefaultClock()) { |
50 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
51 DCHECK_GT(max_devices_, 0U); | 52 DCHECK_GT(max_devices_, 0U); |
52 // This is a leaky singleton, so there's no code to remove |this| as an | 53 // This is a leaky singleton, so there's no code to remove |this| as an |
53 // observer. | 54 // observer. |
54 NetworkChangeNotifier::AddNetworkChangeObserver(this); | 55 NetworkChangeNotifier::AddNetworkChangeObserver(this); |
55 } | 56 } |
56 | 57 |
57 // This is a leaky singleton and the dtor won't be called. | 58 // This is a leaky singleton and the dtor won't be called. |
58 DialRegistry::~DialRegistry() = default; | 59 DialRegistry::~DialRegistry() = default; |
59 | 60 |
60 // static | 61 // static |
61 DialRegistry* DialRegistry::GetInstance() { | 62 DialRegistry* DialRegistry::GetInstance() { |
62 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 63 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
63 return base::Singleton<DialRegistry, | 64 return base::Singleton<DialRegistry, |
64 base::LeakySingletonTraits<DialRegistry>>::get(); | 65 base::LeakySingletonTraits<DialRegistry>>::get(); |
65 } | 66 } |
66 | 67 |
67 std::unique_ptr<DialService> DialRegistry::CreateDialService() { | 68 std::unique_ptr<DialService> DialRegistry::CreateDialService() { |
68 DCHECK(g_browser_process->net_log()); | 69 DCHECK(g_browser_process->net_log()); |
69 return base::MakeUnique<DialServiceImpl>(g_browser_process->net_log()); | 70 return base::MakeUnique<DialServiceImpl>(g_browser_process->net_log()); |
70 } | 71 } |
71 | 72 |
72 void DialRegistry::ClearDialService() { | 73 void DialRegistry::ClearDialService() { |
73 dial_.reset(); | 74 dial_.reset(); |
74 } | 75 } |
75 | 76 |
76 base::Time DialRegistry::Now() const { | |
77 return Time::Now(); | |
78 } | |
79 | |
80 void DialRegistry::OnListenerAdded() { | 77 void DialRegistry::OnListenerAdded() { |
81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
82 if (++num_listeners_ == 1) { | 79 if (++num_listeners_ == 1) { |
83 VLOG(2) << "Listener added; starting periodic discovery."; | 80 VLOG(2) << "Listener added; starting periodic discovery."; |
84 StartPeriodicDiscovery(); | 81 StartPeriodicDiscovery(); |
85 } | 82 } |
86 // Event listeners with the current device list. | 83 // Event listeners with the current device list. |
87 // TODO(crbug.com/576817): Rework the DIAL API so we don't need to have extra | 84 // TODO(crbug.com/576817): Rework the DIAL API so we don't need to have extra |
88 // behaviors when adding listeners. | 85 // behaviors when adding listeners. |
89 SendEvent(); | 86 SendEvent(); |
(...skipping 28 matching lines...) Expand all Loading... |
118 | 115 |
119 void DialRegistry::AddDeviceForTest(const DialDeviceData& device_data) { | 116 void DialRegistry::AddDeviceForTest(const DialDeviceData& device_data) { |
120 std::unique_ptr<DialDeviceData> test_data = | 117 std::unique_ptr<DialDeviceData> test_data = |
121 base::MakeUnique<DialDeviceData>(device_data); | 118 base::MakeUnique<DialDeviceData>(device_data); |
122 device_by_label_map_.insert( | 119 device_by_label_map_.insert( |
123 std::make_pair(device_data.label(), test_data.get())); | 120 std::make_pair(device_data.label(), test_data.get())); |
124 device_by_id_map_.insert( | 121 device_by_id_map_.insert( |
125 std::make_pair(device_data.device_id(), std::move(test_data))); | 122 std::make_pair(device_data.device_id(), std::move(test_data))); |
126 } | 123 } |
127 | 124 |
| 125 void DialRegistry::SetClockForTest(std::unique_ptr<base::Clock> clock) { |
| 126 clock_ = std::move(clock); |
| 127 } |
| 128 |
128 bool DialRegistry::ReadyToDiscover() { | 129 bool DialRegistry::ReadyToDiscover() { |
129 if (num_listeners_ == 0) { | 130 if (num_listeners_ == 0) { |
130 OnDialError(DIAL_NO_LISTENERS); | 131 OnDialError(DIAL_NO_LISTENERS); |
131 return false; | 132 return false; |
132 } | 133 } |
133 if (NetworkChangeNotifier::IsOffline()) { | 134 if (NetworkChangeNotifier::IsOffline()) { |
134 OnDialError(DIAL_NETWORK_DISCONNECTED); | 135 OnDialError(DIAL_NETWORK_DISCONNECTED); |
135 return false; | 136 return false; |
136 } | 137 } |
137 if (NetworkChangeNotifier::IsConnectionCellular( | 138 if (NetworkChangeNotifier::IsConnectionCellular( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 device_by_label_map_.erase(it++); | 213 device_by_label_map_.erase(it++); |
213 pruned_device = true; | 214 pruned_device = true; |
214 } else { | 215 } else { |
215 ++it; | 216 ++it; |
216 } | 217 } |
217 } | 218 } |
218 return pruned_device; | 219 return pruned_device; |
219 } | 220 } |
220 | 221 |
221 bool DialRegistry::IsDeviceExpired(const DialDeviceData& device) const { | 222 bool DialRegistry::IsDeviceExpired(const DialDeviceData& device) const { |
222 Time now = Now(); | 223 Time now = clock_->Now(); |
223 | 224 |
224 // Check against our default expiration timeout. | 225 // Check against our default expiration timeout. |
225 Time default_expiration_time = device.response_time() + expiration_delta_; | 226 Time default_expiration_time = device.response_time() + expiration_delta_; |
226 if (now > default_expiration_time) | 227 if (now > default_expiration_time) |
227 return true; | 228 return true; |
228 | 229 |
229 // Check against the device's cache-control header, if set. | 230 // Check against the device's cache-control header, if set. |
230 if (device.has_max_age()) { | 231 if (device.has_max_age()) { |
231 Time max_age_expiration_time = | 232 Time max_age_expiration_time = |
232 device.response_time() + TimeDelta::FromSeconds(device.max_age()); | 233 device.response_time() + TimeDelta::FromSeconds(device.max_age()); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 for (auto& observer : observers_) | 383 for (auto& observer : observers_) |
383 observer.OnDialDeviceEvent(devices); | 384 observer.OnDialDeviceEvent(devices); |
384 } | 385 } |
385 | 386 |
386 void DialRegistry::OnDialError(DialErrorCode type) { | 387 void DialRegistry::OnDialError(DialErrorCode type) { |
387 for (auto& observer : observers_) | 388 for (auto& observer : observers_) |
388 observer.OnDialError(type); | 389 observer.OnDialError(type); |
389 } | 390 } |
390 | 391 |
391 } // namespace media_router | 392 } // namespace media_router |
OLD | NEW |