| 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" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/io_thread.h" | 15 #include "chrome/browser/io_thread.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "components/prefs/pref_registry.h" | 18 #include "components/prefs/pref_registry.h" |
| 19 #include "components/prefs/pref_registry_simple.h" | 19 #include "components/prefs/pref_registry_simple.h" |
| 20 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 21 #include "components/variations/variations_associated_data.h" | 21 #include "components/variations/variations_associated_data.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "net/nqe/effective_connection_type_observer.h" |
| 23 #include "net/nqe/network_qualities_prefs_manager.h" | 24 #include "net/nqe/network_qualities_prefs_manager.h" |
| 24 #include "net/nqe/network_quality.h" | 25 #include "net/nqe/network_quality.h" |
| 26 #include "net/nqe/rtt_throughput_estimates_observer.h" |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 // PrefDelegateImpl writes the provided dictionary value to the network quality | 30 // PrefDelegateImpl writes the provided dictionary value to the network quality |
| 29 // estimator prefs on the disk. | 31 // estimator prefs on the disk. |
| 30 class PrefDelegateImpl | 32 class PrefDelegateImpl |
| 31 : public net::NetworkQualitiesPrefsManager::PrefDelegate { | 33 : public net::NetworkQualitiesPrefsManager::PrefDelegate { |
| 32 public: | 34 public: |
| 33 // |pref_service| is used to read and write prefs from/to the disk. | 35 // |pref_service| is used to read and write prefs from/to the disk. |
| 34 explicit PrefDelegateImpl(PrefService* pref_service) | 36 explicit PrefDelegateImpl(PrefService* pref_service) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 io_thread->globals()->network_quality_estimator.get()); | 76 io_thread->globals()->network_quality_estimator.get()); |
| 75 } | 77 } |
| 76 | 78 |
| 77 } // namespace | 79 } // namespace |
| 78 | 80 |
| 79 // A class that sets itself as an observer of the EffectiveconnectionType for | 81 // A class that sets itself as an observer of the EffectiveconnectionType for |
| 80 // the browser IO thread. It reports any change in EffectiveConnectionType back | 82 // the browser IO thread. It reports any change in EffectiveConnectionType back |
| 81 // to the UI service. | 83 // to the UI service. |
| 82 // It is created on the UI thread, but used and deleted on the IO thread. | 84 // It is created on the UI thread, but used and deleted on the IO thread. |
| 83 class UINetworkQualityEstimatorService::IONetworkQualityObserver | 85 class UINetworkQualityEstimatorService::IONetworkQualityObserver |
| 84 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver, | 86 : public net::EffectiveConnectionTypeObserver, |
| 85 public net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver { | 87 public net::RTTAndThroughputEstimatesObserver { |
| 86 public: | 88 public: |
| 87 explicit IONetworkQualityObserver( | 89 explicit IONetworkQualityObserver( |
| 88 base::WeakPtr<UINetworkQualityEstimatorService> service) | 90 base::WeakPtr<UINetworkQualityEstimatorService> service) |
| 89 : service_(service), network_quality_estimator_(nullptr) { | 91 : service_(service), network_quality_estimator_(nullptr) { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 } | 93 } |
| 92 | 94 |
| 93 ~IONetworkQualityObserver() override { | 95 ~IONetworkQualityObserver() override { |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 95 if (network_quality_estimator_) { | 97 if (network_quality_estimator_) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 transport_rtt_ = transport_rtt; | 216 transport_rtt_ = transport_rtt; |
| 215 downstream_throughput_kbps_ = downstream_throughput_kbps; | 217 downstream_throughput_kbps_ = downstream_throughput_kbps; |
| 216 | 218 |
| 217 for (auto& observer : rtt_throughput_observer_list_) { | 219 for (auto& observer : rtt_throughput_observer_list_) { |
| 218 observer.OnRTTOrThroughputEstimatesComputed(http_rtt, transport_rtt, | 220 observer.OnRTTOrThroughputEstimatesComputed(http_rtt, transport_rtt, |
| 219 downstream_throughput_kbps); | 221 downstream_throughput_kbps); |
| 220 } | 222 } |
| 221 } | 223 } |
| 222 | 224 |
| 223 void UINetworkQualityEstimatorService::AddEffectiveConnectionTypeObserver( | 225 void UINetworkQualityEstimatorService::AddEffectiveConnectionTypeObserver( |
| 224 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { | 226 net::EffectiveConnectionTypeObserver* observer) { |
| 225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 227 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 226 effective_connection_type_observer_list_.AddObserver(observer); | 228 effective_connection_type_observer_list_.AddObserver(observer); |
| 227 | 229 |
| 228 // Notify the |observer| on the next message pump since |observer| may not | 230 // Notify the |observer| on the next message pump since |observer| may not |
| 229 // be completely set up for receiving the callbacks. | 231 // be completely set up for receiving the callbacks. |
| 230 content::BrowserThread::PostTask( | 232 content::BrowserThread::PostTask( |
| 231 content::BrowserThread::UI, FROM_HERE, | 233 content::BrowserThread::UI, FROM_HERE, |
| 232 base::BindOnce(&UINetworkQualityEstimatorService:: | 234 base::BindOnce(&UINetworkQualityEstimatorService:: |
| 233 NotifyEffectiveConnectionTypeObserverIfPresent, | 235 NotifyEffectiveConnectionTypeObserverIfPresent, |
| 234 weak_factory_.GetWeakPtr(), observer)); | 236 weak_factory_.GetWeakPtr(), observer)); |
| 235 } | 237 } |
| 236 | 238 |
| 237 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver( | 239 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver( |
| 238 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { | 240 net::EffectiveConnectionTypeObserver* observer) { |
| 239 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 241 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 240 effective_connection_type_observer_list_.RemoveObserver(observer); | 242 effective_connection_type_observer_list_.RemoveObserver(observer); |
| 241 } | 243 } |
| 242 | 244 |
| 243 base::Optional<base::TimeDelta> UINetworkQualityEstimatorService::GetHttpRTT() | 245 base::Optional<base::TimeDelta> UINetworkQualityEstimatorService::GetHttpRTT() |
| 244 const { | 246 const { |
| 245 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 247 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 246 | 248 |
| 247 if (http_rtt_ == net::nqe::internal::InvalidRTT()) | 249 if (http_rtt_ == net::nqe::internal::InvalidRTT()) |
| 248 return base::Optional<base::TimeDelta>(); | 250 return base::Optional<base::TimeDelta>(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 261 base::Optional<int32_t> | 263 base::Optional<int32_t> |
| 262 UINetworkQualityEstimatorService::GetDownstreamThroughputKbps() const { | 264 UINetworkQualityEstimatorService::GetDownstreamThroughputKbps() const { |
| 263 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 265 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 264 | 266 |
| 265 if (downstream_throughput_kbps_ == net::nqe::internal::kInvalidThroughput) | 267 if (downstream_throughput_kbps_ == net::nqe::internal::kInvalidThroughput) |
| 266 return base::Optional<int32_t>(); | 268 return base::Optional<int32_t>(); |
| 267 return downstream_throughput_kbps_; | 269 return downstream_throughput_kbps_; |
| 268 } | 270 } |
| 269 | 271 |
| 270 void UINetworkQualityEstimatorService::AddRTTAndThroughputEstimatesObserver( | 272 void UINetworkQualityEstimatorService::AddRTTAndThroughputEstimatesObserver( |
| 271 net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver* observer) { | 273 net::RTTAndThroughputEstimatesObserver* observer) { |
| 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 274 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 273 rtt_throughput_observer_list_.AddObserver(observer); | 275 rtt_throughput_observer_list_.AddObserver(observer); |
| 274 | 276 |
| 275 // Notify the |observer| on the next message pump since |observer| may not | 277 // Notify the |observer| on the next message pump since |observer| may not |
| 276 // be completely set up for receiving the callbacks. | 278 // be completely set up for receiving the callbacks. |
| 277 content::BrowserThread::PostTask( | 279 content::BrowserThread::PostTask( |
| 278 content::BrowserThread::UI, FROM_HERE, | 280 content::BrowserThread::UI, FROM_HERE, |
| 279 base::BindOnce(&UINetworkQualityEstimatorService:: | 281 base::BindOnce(&UINetworkQualityEstimatorService:: |
| 280 NotifyRTTAndThroughputObserverIfPresent, | 282 NotifyRTTAndThroughputObserverIfPresent, |
| 281 weak_factory_.GetWeakPtr(), observer)); | 283 weak_factory_.GetWeakPtr(), observer)); |
| 282 } | 284 } |
| 283 | 285 |
| 284 // Removes |observer| from the list of RTT and throughput estimate observers. | 286 // Removes |observer| from the list of RTT and throughput estimate observers. |
| 285 // Must be called on the IO thread. | 287 // Must be called on the IO thread. |
| 286 void UINetworkQualityEstimatorService::RemoveRTTAndThroughputEstimatesObserver( | 288 void UINetworkQualityEstimatorService::RemoveRTTAndThroughputEstimatesObserver( |
| 287 net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver* observer) { | 289 net::RTTAndThroughputEstimatesObserver* observer) { |
| 288 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 290 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 289 rtt_throughput_observer_list_.RemoveObserver(observer); | 291 rtt_throughput_observer_list_.RemoveObserver(observer); |
| 290 } | 292 } |
| 291 | 293 |
| 292 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( | 294 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( |
| 293 net::EffectiveConnectionType type) { | 295 net::EffectiveConnectionType type) { |
| 294 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 295 EffectiveConnectionTypeChanged(type); | 297 EffectiveConnectionTypeChanged(type); |
| 296 } | 298 } |
| 297 | 299 |
| 298 net::EffectiveConnectionType | 300 net::EffectiveConnectionType |
| 299 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { | 301 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
| 300 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 302 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 301 return type_; | 303 return type_; |
| 302 } | 304 } |
| 303 | 305 |
| 304 void UINetworkQualityEstimatorService::ClearPrefs() { | 306 void UINetworkQualityEstimatorService::ClearPrefs() { |
| 305 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 307 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 306 if (!prefs_manager_) | 308 if (!prefs_manager_) |
| 307 return; | 309 return; |
| 308 prefs_manager_->ClearPrefs(); | 310 prefs_manager_->ClearPrefs(); |
| 309 } | 311 } |
| 310 | 312 |
| 311 void UINetworkQualityEstimatorService:: | 313 void UINetworkQualityEstimatorService:: |
| 312 NotifyEffectiveConnectionTypeObserverIfPresent( | 314 NotifyEffectiveConnectionTypeObserverIfPresent( |
| 313 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) | 315 net::EffectiveConnectionTypeObserver* observer) const { |
| 314 const { | |
| 315 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 316 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 316 | 317 |
| 317 if (!effective_connection_type_observer_list_.HasObserver(observer)) | 318 if (!effective_connection_type_observer_list_.HasObserver(observer)) |
| 318 return; | 319 return; |
| 319 if (type_ == net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) | 320 if (type_ == net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) |
| 320 return; | 321 return; |
| 321 observer->OnEffectiveConnectionTypeChanged(type_); | 322 observer->OnEffectiveConnectionTypeChanged(type_); |
| 322 } | 323 } |
| 323 | 324 |
| 324 void UINetworkQualityEstimatorService::NotifyRTTAndThroughputObserverIfPresent( | 325 void UINetworkQualityEstimatorService::NotifyRTTAndThroughputObserverIfPresent( |
| 325 net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver* observer) | 326 net::RTTAndThroughputEstimatesObserver* observer) const { |
| 326 const { | |
| 327 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 327 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 328 | 328 |
| 329 if (!rtt_throughput_observer_list_.HasObserver(observer)) | 329 if (!rtt_throughput_observer_list_.HasObserver(observer)) |
| 330 return; | 330 return; |
| 331 observer->OnRTTOrThroughputEstimatesComputed(http_rtt_, transport_rtt_, | 331 observer->OnRTTOrThroughputEstimatesComputed(http_rtt_, transport_rtt_, |
| 332 downstream_throughput_kbps_); | 332 downstream_throughput_kbps_); |
| 333 } | 333 } |
| 334 | 334 |
| 335 // static | 335 // static |
| 336 void UINetworkQualityEstimatorService::RegisterProfilePrefs( | 336 void UINetworkQualityEstimatorService::RegisterProfilePrefs( |
| 337 PrefRegistrySimple* registry) { | 337 PrefRegistrySimple* registry) { |
| 338 registry->RegisterDictionaryPref(prefs::kNetworkQualities, | 338 registry->RegisterDictionaryPref(prefs::kNetworkQualities, |
| 339 PrefRegistry::LOSSY_PREF); | 339 PrefRegistry::LOSSY_PREF); |
| 340 } | 340 } |
| 341 | 341 |
| 342 std::map<net::nqe::internal::NetworkID, | 342 std::map<net::nqe::internal::NetworkID, |
| 343 net::nqe::internal::CachedNetworkQuality> | 343 net::nqe::internal::CachedNetworkQuality> |
| 344 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { | 344 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { |
| 345 if (!prefs_manager_) { | 345 if (!prefs_manager_) { |
| 346 return std::map<net::nqe::internal::NetworkID, | 346 return std::map<net::nqe::internal::NetworkID, |
| 347 net::nqe::internal::CachedNetworkQuality>(); | 347 net::nqe::internal::CachedNetworkQuality>(); |
| 348 } | 348 } |
| 349 return prefs_manager_->ForceReadPrefsForTesting(); | 349 return prefs_manager_->ForceReadPrefsForTesting(); |
| 350 } | 350 } |
| OLD | NEW |