OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/net/nqe/ui_network_quality_estimator_service.h" | 5 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 io_thread->globals()->network_quality_estimator.get()); | 73 io_thread->globals()->network_quality_estimator.get()); |
74 } | 74 } |
75 | 75 |
76 } // namespace | 76 } // namespace |
77 | 77 |
78 // A class that sets itself as an observer of the EffectiveconnectionType for | 78 // A class that sets itself as an observer of the EffectiveconnectionType for |
79 // the browser IO thread. It reports any change in EffectiveConnectionType back | 79 // the browser IO thread. It reports any change in EffectiveConnectionType back |
80 // to the UI service. | 80 // to the UI service. |
81 // It is created on the UI thread, but used and deleted on the IO thread. | 81 // It is created on the UI thread, but used and deleted on the IO thread. |
82 class UINetworkQualityEstimatorService::IONetworkQualityObserver | 82 class UINetworkQualityEstimatorService::IONetworkQualityObserver |
83 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | 83 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver, |
| 84 public net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver { |
84 public: | 85 public: |
85 explicit IONetworkQualityObserver( | 86 explicit IONetworkQualityObserver( |
86 base::WeakPtr<UINetworkQualityEstimatorService> service) | 87 base::WeakPtr<UINetworkQualityEstimatorService> service) |
87 : service_(service), network_quality_estimator_(nullptr) { | 88 : service_(service), network_quality_estimator_(nullptr) { |
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
89 } | 90 } |
90 | 91 |
91 ~IONetworkQualityObserver() override { | 92 ~IONetworkQualityObserver() override { |
92 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
93 if (network_quality_estimator_) | 94 if (network_quality_estimator_) { |
94 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); | 95 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); |
| 96 network_quality_estimator_->RemoveRTTAndThroughputEstimatesObserver(this); |
| 97 } |
95 } | 98 } |
96 | 99 |
97 void InitializeOnIOThread(IOThread* io_thread) { | 100 void InitializeOnIOThread(IOThread* io_thread) { |
98 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
99 if (!io_thread->globals()->network_quality_estimator) | 102 if (!io_thread->globals()->network_quality_estimator) |
100 return; | 103 return; |
101 network_quality_estimator_ = | 104 network_quality_estimator_ = |
102 io_thread->globals()->network_quality_estimator.get(); | 105 io_thread->globals()->network_quality_estimator.get(); |
103 if (!network_quality_estimator_) | 106 if (!network_quality_estimator_) |
104 return; | 107 return; |
105 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); | 108 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); |
| 109 network_quality_estimator_->AddRTTAndThroughputEstimatesObserver(this); |
106 } | 110 } |
107 | 111 |
108 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver | 112 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver |
109 // implementation: | 113 // implementation: |
110 void OnEffectiveConnectionTypeChanged( | 114 void OnEffectiveConnectionTypeChanged( |
111 net::EffectiveConnectionType type) override { | 115 net::EffectiveConnectionType type) override { |
112 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 116 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
113 content::BrowserThread::PostTask( | 117 content::BrowserThread::PostTask( |
114 content::BrowserThread::UI, FROM_HERE, | 118 content::BrowserThread::UI, FROM_HERE, |
115 base::Bind( | 119 base::Bind( |
116 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | 120 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
117 service_, type)); | 121 service_, type)); |
118 } | 122 } |
119 | 123 |
| 124 // net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver |
| 125 // implementation: |
| 126 void OnRTTOrThroughputEstimatesComputed( |
| 127 base::TimeDelta http_rtt, |
| 128 base::TimeDelta transport_rtt, |
| 129 int32_t downstream_throughput_kbps) override { |
| 130 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 131 content::BrowserThread::PostTask( |
| 132 content::BrowserThread::UI, FROM_HERE, |
| 133 base::Bind(&UINetworkQualityEstimatorService::RTTOrThroughputComputed, |
| 134 service_, http_rtt, transport_rtt, |
| 135 downstream_throughput_kbps)); |
| 136 } |
| 137 |
120 private: | 138 private: |
121 base::WeakPtr<UINetworkQualityEstimatorService> service_; | 139 base::WeakPtr<UINetworkQualityEstimatorService> service_; |
122 net::NetworkQualityEstimator* network_quality_estimator_; | 140 net::NetworkQualityEstimator* network_quality_estimator_; |
123 | 141 |
124 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); | 142 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); |
125 }; | 143 }; |
126 | 144 |
127 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( | 145 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( |
128 Profile* profile) | 146 Profile* profile) |
129 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), weak_factory_(this) { | 147 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| 148 http_rtt_(base::TimeDelta::FromMilliseconds(-1)), |
| 149 transport_rtt_(base::TimeDelta::FromMilliseconds(-1)), |
| 150 downstream_throughput_kbps_(-1), |
| 151 weak_factory_(this) { |
130 DCHECK(profile); | 152 DCHECK(profile); |
131 // If this is running in a context without an IOThread, don't try to create | 153 // If this is running in a context without an IOThread, don't try to create |
132 // the IO object. | 154 // the IO object. |
133 if (!g_browser_process->io_thread()) | 155 if (!g_browser_process->io_thread()) |
134 return; | 156 return; |
135 io_observer_ = base::WrapUnique( | 157 io_observer_ = base::WrapUnique( |
136 new IONetworkQualityObserver(weak_factory_.GetWeakPtr())); | 158 new IONetworkQualityObserver(weak_factory_.GetWeakPtr())); |
137 std::unique_ptr<PrefDelegateImpl> pref_delegate( | 159 std::unique_ptr<PrefDelegateImpl> pref_delegate( |
138 new PrefDelegateImpl(profile->GetPrefs())); | 160 new PrefDelegateImpl(profile->GetPrefs())); |
139 prefs_manager_ = base::WrapUnique( | 161 prefs_manager_ = base::WrapUnique( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 197 } |
176 | 198 |
177 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( | 199 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( |
178 net::EffectiveConnectionType type) { | 200 net::EffectiveConnectionType type) { |
179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
180 type_ = type; | 202 type_ = type; |
181 for (auto& observer : effective_connection_type_observer_list_) | 203 for (auto& observer : effective_connection_type_observer_list_) |
182 observer.OnEffectiveConnectionTypeChanged(type); | 204 observer.OnEffectiveConnectionTypeChanged(type); |
183 } | 205 } |
184 | 206 |
| 207 void UINetworkQualityEstimatorService::RTTOrThroughputComputed( |
| 208 base::TimeDelta http_rtt, |
| 209 base::TimeDelta transport_rtt, |
| 210 int32_t downstream_throughput_kbps) { |
| 211 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 212 http_rtt_ = http_rtt; |
| 213 transport_rtt_ = transport_rtt; |
| 214 downstream_throughput_kbps_ = downstream_throughput_kbps; |
| 215 |
| 216 for (auto& observer : rtt_throughput_observer_list_) { |
| 217 observer.OnRTTOrThroughputEstimatesComputed(http_rtt, transport_rtt, |
| 218 downstream_throughput_kbps); |
| 219 } |
| 220 } |
| 221 |
185 void UINetworkQualityEstimatorService::AddEffectiveConnectionTypeObserver( | 222 void UINetworkQualityEstimatorService::AddEffectiveConnectionTypeObserver( |
186 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { | 223 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { |
187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 224 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
188 effective_connection_type_observer_list_.AddObserver(observer); | 225 effective_connection_type_observer_list_.AddObserver(observer); |
189 | 226 |
190 // Notify the |observer| on the next message pump since |observer| may not | 227 // Notify the |observer| on the next message pump since |observer| may not |
191 // be completely set up for receiving the callbacks. | 228 // be completely set up for receiving the callbacks. |
192 content::BrowserThread::PostTask( | 229 content::BrowserThread::PostTask( |
193 content::BrowserThread::UI, FROM_HERE, | 230 content::BrowserThread::UI, FROM_HERE, |
194 base::Bind(&UINetworkQualityEstimatorService:: | 231 base::Bind(&UINetworkQualityEstimatorService:: |
195 NotifyEffectiveConnectionTypeObserverIfPresent, | 232 NotifyEffectiveConnectionTypeObserverIfPresent, |
196 weak_factory_.GetWeakPtr(), observer)); | 233 weak_factory_.GetWeakPtr(), observer)); |
197 } | 234 } |
198 | 235 |
199 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver( | 236 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver( |
200 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { | 237 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { |
201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 238 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
202 effective_connection_type_observer_list_.RemoveObserver(observer); | 239 effective_connection_type_observer_list_.RemoveObserver(observer); |
203 } | 240 } |
204 | 241 |
| 242 void UINetworkQualityEstimatorService::AddRTTAndThroughputEstimatesObserver( |
| 243 net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver* observer) { |
| 244 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 245 rtt_throughput_observer_list_.AddObserver(observer); |
| 246 |
| 247 // Notify the |observer| on the next message pump since |observer| may not |
| 248 // be completely set up for receiving the callbacks. |
| 249 content::BrowserThread::PostTask( |
| 250 content::BrowserThread::UI, FROM_HERE, |
| 251 base::Bind(&UINetworkQualityEstimatorService:: |
| 252 NotifyRTTAndThroughputObserverIfPresent, |
| 253 weak_factory_.GetWeakPtr(), observer)); |
| 254 } |
| 255 |
| 256 // Removes |observer| from the list of RTT and throughput estimate observers. |
| 257 // Must be called on the IO thread. |
| 258 void UINetworkQualityEstimatorService::RemoveRTTAndThroughputEstimatesObserver( |
| 259 net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver* observer) { |
| 260 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 261 rtt_throughput_observer_list_.RemoveObserver(observer); |
| 262 } |
| 263 |
205 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( | 264 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( |
206 net::EffectiveConnectionType type) { | 265 net::EffectiveConnectionType type) { |
207 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 266 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
208 EffectiveConnectionTypeChanged(type); | 267 EffectiveConnectionTypeChanged(type); |
209 } | 268 } |
210 | 269 |
211 net::EffectiveConnectionType | 270 net::EffectiveConnectionType |
212 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { | 271 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
213 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
214 return type_; | 273 return type_; |
(...skipping 12 matching lines...) Expand all Loading... |
227 const { | 286 const { |
228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 287 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
229 | 288 |
230 if (!effective_connection_type_observer_list_.HasObserver(observer)) | 289 if (!effective_connection_type_observer_list_.HasObserver(observer)) |
231 return; | 290 return; |
232 if (type_ == net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) | 291 if (type_ == net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) |
233 return; | 292 return; |
234 observer->OnEffectiveConnectionTypeChanged(type_); | 293 observer->OnEffectiveConnectionTypeChanged(type_); |
235 } | 294 } |
236 | 295 |
| 296 void UINetworkQualityEstimatorService::NotifyRTTAndThroughputObserverIfPresent( |
| 297 net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver* observer) |
| 298 const { |
| 299 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 300 |
| 301 if (!rtt_throughput_observer_list_.HasObserver(observer)) |
| 302 return; |
| 303 observer->OnRTTOrThroughputEstimatesComputed(http_rtt_, transport_rtt_, |
| 304 downstream_throughput_kbps_); |
| 305 } |
| 306 |
237 // static | 307 // static |
238 void UINetworkQualityEstimatorService::RegisterProfilePrefs( | 308 void UINetworkQualityEstimatorService::RegisterProfilePrefs( |
239 PrefRegistrySimple* registry) { | 309 PrefRegistrySimple* registry) { |
240 registry->RegisterDictionaryPref(prefs::kNetworkQualities, | 310 registry->RegisterDictionaryPref(prefs::kNetworkQualities, |
241 PrefRegistry::LOSSY_PREF); | 311 PrefRegistry::LOSSY_PREF); |
242 } | 312 } |
243 | 313 |
244 std::map<net::nqe::internal::NetworkID, | 314 std::map<net::nqe::internal::NetworkID, |
245 net::nqe::internal::CachedNetworkQuality> | 315 net::nqe::internal::CachedNetworkQuality> |
246 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { | 316 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { |
247 if (!prefs_manager_) { | 317 if (!prefs_manager_) { |
248 return std::map<net::nqe::internal::NetworkID, | 318 return std::map<net::nqe::internal::NetworkID, |
249 net::nqe::internal::CachedNetworkQuality>(); | 319 net::nqe::internal::CachedNetworkQuality>(); |
250 } | 320 } |
251 return prefs_manager_->ForceReadPrefsForTesting(); | 321 return prefs_manager_->ForceReadPrefsForTesting(); |
252 } | 322 } |
OLD | NEW |