Chromium Code Reviews| Index: chrome/browser/net/nqe/ui_network_quality_estimator_service.cc |
| diff --git a/chrome/browser/net/nqe/ui_network_quality_estimator_service.cc b/chrome/browser/net/nqe/ui_network_quality_estimator_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..26e2a89b5a46fe8a9b679b39306f3505eb8bb827 |
| --- /dev/null |
| +++ b/chrome/browser/net/nqe/ui_network_quality_estimator_service.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/io_thread.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace chrome_browser_net { |
| + |
| +// A class that sets itself as an observer of the EffectiveconnectionType for |
| +// the browser IO thread. It reports any change in EffectiveConnectionType back |
| +// to the UI service. |
| +// It is created on the UI thread, but used and deleted on the IO thread. |
| +class UINetworkQualityEstimatorService::IONetworkQualityObserver |
| + : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { |
| + public: |
| + IONetworkQualityObserver( |
| + base::WeakPtr<UINetworkQualityEstimatorService> service) |
| + : service_(service) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + } |
| + |
| + ~IONetworkQualityObserver() override { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + if (network_quality_estimator_) |
| + network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); |
| + } |
| + |
| + void InitializeOnIOThread(IOThread* io_thread) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + if (!io_thread->globals()->network_quality_estimator) |
| + return; |
| + network_quality_estimator_ = |
| + io_thread->globals()->network_quality_estimator.get(); |
| + if (!network_quality_estimator_) |
| + return; |
| + network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind( |
| + &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
| + service_, |
| + network_quality_estimator_->GetEffectiveConnectionType())); |
| + } |
| + |
| + // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver |
| + // implementation: |
| + void OnEffectiveConnectionTypeChanged( |
| + net::NetworkQualityEstimator::EffectiveConnectionType type) override { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind( |
| + &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, |
| + service_, type)); |
| + } |
| + |
| + private: |
| + base::WeakPtr<UINetworkQualityEstimatorService> service_; |
| + net::NetworkQualityEstimator* network_quality_estimator_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); |
| +}; |
| + |
| +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.
|
| + Profile* profile) |
| + : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| + io_observer_(nullptr), |
| + weak_factory_(this) { |
| + 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.
|
| + io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, |
| + base::Unretained(io_observer_), |
| + g_browser_process->io_thread())); |
| +} |
| + |
| +UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| +} |
| + |
| +void UINetworkQualityEstimatorService::Shutdown() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + weak_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( |
| + net::NetworkQualityEstimator::EffectiveConnectionType type) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + type_ = type; |
| +} |
| + |
| +net::NetworkQualityEstimator::EffectiveConnectionType |
| +UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + return type_; |
| +} |
| + |
| +} // namespace chrome_browser_net |