Chromium Code Reviews| 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 namespace chrome_browser_net { | |
| 13 | |
| 14 // A class that sets itself as an observer of the EffectiveconnectionType for | |
| 15 // the browser IO thread. It reports any change in EffectiveConnectionType back | |
| 16 // to the UI service. | |
| 17 // It is created on the UI thread, but used and deleted on the IO thread. | |
| 18 class UINetworkQualityEstimatorService::IONetworkQualityObserver | |
| 19 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | |
| 20 public: | |
| 21 IONetworkQualityObserver( | |
| 22 base::WeakPtr<UINetworkQualityEstimatorService> service) | |
| 23 : service_(service) { | |
| 24 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 25 } | |
| 26 | |
| 27 ~IONetworkQualityObserver() override { | |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 29 if (network_quality_estimator_) | |
| 30 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); | |
| 31 } | |
| 32 | |
| 33 void InitializeOnIOThread(IOThread* io_thread) { | |
| 34 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 35 if (!io_thread->globals()->network_quality_estimator) | |
| 36 return; | |
| 37 network_quality_estimator_ = | |
| 38 io_thread->globals()->network_quality_estimator.get(); | |
| 39 if (!network_quality_estimator_) | |
| 40 return; | |
| 41 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); | |
| 42 content::BrowserThread::PostTask( | |
| 43 content::BrowserThread::UI, FROM_HERE, | |
| 44 base::Bind( | |
| 45 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | |
| 46 service_, | |
| 47 network_quality_estimator_->GetEffectiveConnectionType())); | |
| 48 } | |
| 49 | |
| 50 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver | |
| 51 // implementation: | |
| 52 void OnEffectiveConnectionTypeChanged( | |
| 53 net::NetworkQualityEstimator::EffectiveConnectionType type) override { | |
| 54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 55 content::BrowserThread::PostTask( | |
| 56 content::BrowserThread::UI, FROM_HERE, | |
| 57 base::Bind( | |
| 58 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | |
| 59 service_, type)); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 base::WeakPtr<UINetworkQualityEstimatorService> service_; | |
| 64 net::NetworkQualityEstimator* network_quality_estimator_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); | |
| 67 }; | |
| 68 | |
| 69 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( | |
|
mmenke
2016/07/20 17:11:33
Are you thinking this class won't need to support
RyanSturm
2016/07/20 17:37:38
The current plan is to add them later.
| |
| 70 Profile* profile) | |
| 71 : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | |
| 72 io_observer_(nullptr), | |
| 73 weak_factory_(this) { | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
mmenke
2016/07/20 17:11:33
optional: Rather than a thread checks, suggest ju
RyanSturm
2016/07/20 17:37:38
sgtm.
| |
| 75 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); | |
| 76 content::BrowserThread::PostTask( | |
| 77 content::BrowserThread::IO, FROM_HERE, | |
| 78 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, | |
| 79 base::Unretained(io_observer_), | |
| 80 g_browser_process->io_thread())); | |
| 81 } | |
| 82 | |
| 83 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { | |
| 84 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 85 } | |
| 86 | |
| 87 void UINetworkQualityEstimatorService::Shutdown() { | |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 89 weak_factory_.InvalidateWeakPtrs(); | |
| 90 } | |
| 91 | |
| 92 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( | |
| 93 net::NetworkQualityEstimator::EffectiveConnectionType type) { | |
| 94 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 95 type_ = type; | |
| 96 } | |
| 97 | |
| 98 net::NetworkQualityEstimator::EffectiveConnectionType | |
| 99 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { | |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 101 return type_; | |
| 102 } | |
| 103 | |
| 104 } // namespace chrome_browser_net | |
| OLD | NEW |