| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/io_thread.h" | 9 #include "chrome/browser/io_thread.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "net/nqe/network_quality_estimator.h" |
| 11 | 12 |
| 12 // A class that sets itself as an observer of the EffectiveconnectionType for | 13 // A class that sets itself as an observer of the EffectiveconnectionType for |
| 13 // the browser IO thread. It reports any change in EffectiveConnectionType back | 14 // the browser IO thread. It reports any change in EffectiveConnectionType back |
| 14 // to the UI service. | 15 // to the UI service. |
| 15 // It is created on the UI thread, but used and deleted on the IO thread. | 16 // It is created on the UI thread, but used and deleted on the IO thread. |
| 16 class UINetworkQualityEstimatorService::IONetworkQualityObserver | 17 class UINetworkQualityEstimatorService::IONetworkQualityObserver |
| 17 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | 18 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { |
| 18 public: | 19 public: |
| 19 IONetworkQualityObserver( | 20 IONetworkQualityObserver( |
| 20 base::WeakPtr<UINetworkQualityEstimatorService> service) | 21 base::WeakPtr<UINetworkQualityEstimatorService> service) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 content::BrowserThread::UI, FROM_HERE, | 42 content::BrowserThread::UI, FROM_HERE, |
| 42 base::Bind( | 43 base::Bind( |
| 43 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | 44 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
| 44 service_, | 45 service_, |
| 45 network_quality_estimator_->GetEffectiveConnectionType())); | 46 network_quality_estimator_->GetEffectiveConnectionType())); |
| 46 } | 47 } |
| 47 | 48 |
| 48 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver | 49 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver |
| 49 // implementation: | 50 // implementation: |
| 50 void OnEffectiveConnectionTypeChanged( | 51 void OnEffectiveConnectionTypeChanged( |
| 51 net::NetworkQualityEstimator::EffectiveConnectionType type) override { | 52 net::EffectiveConnectionType type) override { |
| 52 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 53 content::BrowserThread::PostTask( | 54 content::BrowserThread::PostTask( |
| 54 content::BrowserThread::UI, FROM_HERE, | 55 content::BrowserThread::UI, FROM_HERE, |
| 55 base::Bind( | 56 base::Bind( |
| 56 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | 57 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
| 57 service_, type)); | 58 service_, type)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 private: | 61 private: |
| 61 base::WeakPtr<UINetworkQualityEstimatorService> service_; | 62 base::WeakPtr<UINetworkQualityEstimatorService> service_; |
| 62 net::NetworkQualityEstimator* network_quality_estimator_; | 63 net::NetworkQualityEstimator* network_quality_estimator_; |
| 63 | 64 |
| 64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); | 65 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService() | 68 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService() |
| 68 : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | 69 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| 69 io_observer_(nullptr), | 70 io_observer_(nullptr), |
| 70 weak_factory_(this) { | 71 weak_factory_(this) { |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 72 // If this is running in a context without an IOThread, don't try to create | 73 // If this is running in a context without an IOThread, don't try to create |
| 73 // the IO object. | 74 // the IO object. |
| 74 if (!g_browser_process->io_thread()) | 75 if (!g_browser_process->io_thread()) |
| 75 return; | 76 return; |
| 76 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); | 77 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); |
| 77 content::BrowserThread::PostTask( | 78 content::BrowserThread::PostTask( |
| 78 content::BrowserThread::IO, FROM_HERE, | 79 content::BrowserThread::IO, FROM_HERE, |
| 79 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, | 80 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, |
| 80 base::Unretained(io_observer_), | 81 base::Unretained(io_observer_), |
| 81 g_browser_process->io_thread())); | 82 g_browser_process->io_thread())); |
| 82 } | 83 } |
| 83 | 84 |
| 84 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { | 85 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { |
| 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void UINetworkQualityEstimatorService::Shutdown() { | 89 void UINetworkQualityEstimatorService::Shutdown() { |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 90 weak_factory_.InvalidateWeakPtrs(); | 91 weak_factory_.InvalidateWeakPtrs(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( | 94 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( |
| 94 net::NetworkQualityEstimator::EffectiveConnectionType type) { | 95 net::EffectiveConnectionType type) { |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 96 type_ = type; | 97 type_ = type; |
| 97 } | 98 } |
| 98 | 99 |
| 99 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( | 100 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( |
| 100 net::NetworkQualityEstimator::EffectiveConnectionType type) { | 101 net::EffectiveConnectionType type) { |
| 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 102 type_ = type; | 103 type_ = type; |
| 103 } | 104 } |
| 104 | 105 |
| 105 net::NetworkQualityEstimator::EffectiveConnectionType | 106 net::EffectiveConnectionType |
| 106 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { | 107 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
| 107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 108 return type_; | 109 return type_; |
| 109 } | 110 } |
| OLD | NEW |