OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/io_thread.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 |
| 12 // 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 // to the UI service. |
| 15 // It is created on the UI thread, but used and deleted on the IO thread. |
| 16 class UINetworkQualityEstimatorService::IONetworkQualityObserver |
| 17 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { |
| 18 public: |
| 19 IONetworkQualityObserver( |
| 20 base::WeakPtr<UINetworkQualityEstimatorService> service) |
| 21 : service_(service) { |
| 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 23 } |
| 24 |
| 25 ~IONetworkQualityObserver() override { |
| 26 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 27 if (network_quality_estimator_) |
| 28 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); |
| 29 } |
| 30 |
| 31 void InitializeOnIOThread(IOThread* io_thread) { |
| 32 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 33 if (!io_thread->globals()->network_quality_estimator) |
| 34 return; |
| 35 network_quality_estimator_ = |
| 36 io_thread->globals()->network_quality_estimator.get(); |
| 37 if (!network_quality_estimator_) |
| 38 return; |
| 39 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); |
| 40 content::BrowserThread::PostTask( |
| 41 content::BrowserThread::UI, FROM_HERE, |
| 42 base::Bind( |
| 43 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
| 44 service_, |
| 45 network_quality_estimator_->GetEffectiveConnectionType())); |
| 46 } |
| 47 |
| 48 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver |
| 49 // implementation: |
| 50 void OnEffectiveConnectionTypeChanged( |
| 51 net::NetworkQualityEstimator::EffectiveConnectionType type) override { |
| 52 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 53 content::BrowserThread::PostTask( |
| 54 content::BrowserThread::UI, FROM_HERE, |
| 55 base::Bind( |
| 56 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
| 57 service_, type)); |
| 58 } |
| 59 |
| 60 private: |
| 61 base::WeakPtr<UINetworkQualityEstimatorService> service_; |
| 62 net::NetworkQualityEstimator* network_quality_estimator_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); |
| 65 }; |
| 66 |
| 67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( |
| 68 Profile* profile) |
| 69 : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| 70 io_observer_(nullptr), |
| 71 weak_factory_(this) { |
| 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 73 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); |
| 74 content::BrowserThread::PostTask( |
| 75 content::BrowserThread::IO, FROM_HERE, |
| 76 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, |
| 77 base::Unretained(io_observer_), |
| 78 g_browser_process->io_thread())); |
| 79 } |
| 80 |
| 81 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 } |
| 84 |
| 85 void UINetworkQualityEstimatorService::Shutdown() { |
| 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 87 weak_factory_.InvalidateWeakPtrs(); |
| 88 } |
| 89 |
| 90 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( |
| 91 net::NetworkQualityEstimator::EffectiveConnectionType type) { |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 93 type_ = type; |
| 94 } |
| 95 |
| 96 net::NetworkQualityEstimator::EffectiveConnectionType |
| 97 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 99 return type_; |
| 100 } |
OLD | NEW |