Chromium Code Reviews| 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 <map> | |
| 8 #include <string> | |
| 9 | |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" | |
|
RyanSturm
2016/10/14 18:36:20
is this used?
tbansal1
2016/10/14 21:15:10
Done.
| |
| 12 #include "base/metrics/histogram_macros.h" | |
| 13 #include "base/threading/thread_checker.h" | |
| 8 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/io_thread.h" | 15 #include "chrome/browser/io_thread.h" |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "components/prefs/pref_registry.h" | |
| 19 #include "components/prefs/pref_registry_simple.h" | |
| 20 #include "components/prefs/pref_service.h" | |
| 21 #include "components/variations/variations_associated_data.h" | |
| 10 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "net/nqe/network_qualities_prefs_manager.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Name of the network quality estimator field trial. | |
| 28 const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator"; | |
| 29 | |
| 30 // Returns the variation value for |parameter_name|. If the value is | |
| 31 // unavailable, |default_value| is returned. | |
| 32 std::string GetStringValueForVariationParamWithDefaultValue( | |
| 33 const std::string& parameter_name, | |
| 34 const std::string& default_value) { | |
| 35 std::map<std::string, std::string> network_quality_estimator_params; | |
| 36 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName, | |
| 37 &network_quality_estimator_params); | |
| 38 | |
| 39 const auto it = network_quality_estimator_params.find(parameter_name); | |
| 40 return it == network_quality_estimator_params.end() ? default_value | |
| 41 : it->second; | |
| 42 } | |
| 43 | |
| 44 // Returns true if persistent caching has been enabled in the field trial. | |
| 45 bool persistent_caching_enabled() { | |
| 46 return GetStringValueForVariationParamWithDefaultValue( | |
| 47 "persistent_caching_enabled", "false") == "true"; | |
| 48 } | |
| 49 | |
| 50 // PrefDelegateImpl writes the provided dictionary value to the network quality | |
| 51 // estimator prefs on the disk. | |
| 52 class PrefDelegateImpl | |
| 53 : public net::NetworkQualitiesPrefsManager::PrefDelegate { | |
| 54 public: | |
| 55 // |pref_service| is used to read and write prefs from/to the disk. | |
| 56 explicit PrefDelegateImpl(PrefService* pref_service) | |
| 57 : pref_service_(pref_service), path_(prefs::kNetworkQualities) { | |
| 58 DCHECK(pref_service_); | |
| 59 } | |
| 60 ~PrefDelegateImpl() override {} | |
| 61 | |
| 62 void SetDictionaryValue(const base::DictionaryValue& value) override { | |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 64 if (!persistent_caching_enabled()) | |
| 65 return; | |
| 66 | |
| 67 pref_service_->Set(path_, value); | |
| 68 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.WriteCount", 1); | |
| 69 } | |
| 70 | |
| 71 const base::DictionaryValue& GetDictionaryValue() override { | |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 73 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.ReadCount", 1); | |
| 74 return *pref_service_->GetDictionary(path_); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 PrefService* pref_service_; | |
| 79 | |
| 80 // |path_| is the location of the network quality estimator prefs. | |
| 81 const std::string path_; | |
| 82 | |
| 83 base::ThreadChecker thread_checker_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(PrefDelegateImpl); | |
| 86 }; | |
| 87 | |
| 88 // Initializes |pref_manager| on |io_thread|. | |
| 89 void SetNQEOnIOThread(net::NetworkQualitiesPrefsManager* prefs_manager, | |
| 90 IOThread* io_thread) { | |
| 91 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 92 | |
| 93 // Avoid null pointer referencing during browser shutdown. | |
| 94 if (!io_thread->globals()->network_quality_estimator) | |
| 95 return; | |
| 96 | |
| 97 prefs_manager->InitializeOnNetworkThread( | |
| 98 io_thread->globals()->network_quality_estimator.get()); | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| 11 | 102 |
| 12 // A class that sets itself as an observer of the EffectiveconnectionType for | 103 // A class that sets itself as an observer of the EffectiveconnectionType for |
| 13 // the browser IO thread. It reports any change in EffectiveConnectionType back | 104 // the browser IO thread. It reports any change in EffectiveConnectionType back |
| 14 // to the UI service. | 105 // to the UI service. |
| 15 // It is created on the UI thread, but used and deleted on the IO thread. | 106 // It is created on the UI thread, but used and deleted on the IO thread. |
| 16 class UINetworkQualityEstimatorService::IONetworkQualityObserver | 107 class UINetworkQualityEstimatorService::IONetworkQualityObserver |
| 17 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | 108 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { |
| 18 public: | 109 public: |
| 19 IONetworkQualityObserver( | 110 explicit IONetworkQualityObserver( |
| 20 base::WeakPtr<UINetworkQualityEstimatorService> service) | 111 base::WeakPtr<UINetworkQualityEstimatorService> service) |
| 21 : service_(service) { | 112 : service_(service) { |
| 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 23 } | 114 } |
| 24 | 115 |
| 25 ~IONetworkQualityObserver() override { | 116 ~IONetworkQualityObserver() override { |
| 26 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 117 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 27 if (network_quality_estimator_) | 118 if (network_quality_estimator_) |
| 28 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); | 119 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); |
| 29 } | 120 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 57 service_, type)); | 148 service_, type)); |
| 58 } | 149 } |
| 59 | 150 |
| 60 private: | 151 private: |
| 61 base::WeakPtr<UINetworkQualityEstimatorService> service_; | 152 base::WeakPtr<UINetworkQualityEstimatorService> service_; |
| 62 net::NetworkQualityEstimator* network_quality_estimator_; | 153 net::NetworkQualityEstimator* network_quality_estimator_; |
| 63 | 154 |
| 64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); | 155 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); |
| 65 }; | 156 }; |
| 66 | 157 |
| 67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService() | 158 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( |
| 159 Profile* profile) | |
| 68 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | 160 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| 69 io_observer_(nullptr), | 161 io_observer_(nullptr), |
| 162 prefs_manager_(nullptr), | |
| 70 weak_factory_(this) { | 163 weak_factory_(this) { |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 165 DCHECK(profile); | |
| 72 // If this is running in a context without an IOThread, don't try to create | 166 // If this is running in a context without an IOThread, don't try to create |
| 73 // the IO object. | 167 // the IO object. |
| 74 if (!g_browser_process->io_thread()) | 168 if (!g_browser_process->io_thread()) |
| 75 return; | 169 return; |
| 76 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); | 170 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); |
| 171 std::unique_ptr<PrefDelegateImpl> pref_delegate( | |
| 172 new PrefDelegateImpl(profile->GetPrefs())); | |
| 173 prefs_manager_ = | |
| 174 new net::NetworkQualitiesPrefsManager(std::move(pref_delegate)); | |
| 175 | |
| 77 content::BrowserThread::PostTask( | 176 content::BrowserThread::PostTask( |
| 78 content::BrowserThread::IO, FROM_HERE, | 177 content::BrowserThread::IO, FROM_HERE, |
| 79 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, | 178 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, |
| 80 base::Unretained(io_observer_), | 179 base::Unretained(io_observer_), |
| 81 g_browser_process->io_thread())); | 180 g_browser_process->io_thread())); |
| 181 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
| 182 base::Bind(&SetNQEOnIOThread, prefs_manager_, | |
| 183 g_browser_process->io_thread())); | |
| 82 } | 184 } |
| 83 | 185 |
| 84 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { | 186 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { |
| 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 86 } | 188 } |
| 87 | 189 |
| 88 void UINetworkQualityEstimatorService::Shutdown() { | 190 void UINetworkQualityEstimatorService::Shutdown() { |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 191 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 90 weak_factory_.InvalidateWeakPtrs(); | 192 weak_factory_.InvalidateWeakPtrs(); |
| 91 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO, | 193 if (io_observer_) { |
| 92 FROM_HERE, io_observer_)); | 194 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO, |
| 195 FROM_HERE, io_observer_)); | |
| 196 io_observer_ = nullptr; | |
| 197 } | |
| 198 if (prefs_manager_) { | |
| 199 prefs_manager_->ShutdownOnPrefThread(); | |
| 200 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO, | |
| 201 FROM_HERE, prefs_manager_)); | |
| 202 prefs_manager_ = nullptr; | |
| 203 } | |
| 93 } | 204 } |
| 94 | 205 |
| 95 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( | 206 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( |
| 96 net::EffectiveConnectionType type) { | 207 net::EffectiveConnectionType type) { |
| 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 208 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 98 type_ = type; | 209 type_ = type; |
| 99 } | 210 } |
| 100 | 211 |
| 101 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( | 212 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( |
| 102 net::EffectiveConnectionType type) { | 213 net::EffectiveConnectionType type) { |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 214 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 104 type_ = type; | 215 type_ = type; |
| 105 } | 216 } |
| 106 | 217 |
| 107 net::EffectiveConnectionType | 218 net::EffectiveConnectionType |
| 108 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { | 219 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 220 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 110 return type_; | 221 return type_; |
| 111 } | 222 } |
| 223 | |
| 224 // static | |
| 225 void UINetworkQualityEstimatorService::RegisterProfilePrefs( | |
| 226 PrefRegistrySimple* registry) { | |
| 227 registry->RegisterDictionaryPref(prefs::kNetworkQualities, | |
| 228 PrefRegistry::LOSSY_PREF); | |
| 229 } | |
| 230 | |
| 231 std::map<net::nqe::internal::NetworkID, | |
| 232 net::nqe::internal::CachedNetworkQuality> | |
| 233 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { | |
| 234 if (!prefs_manager_) { | |
| 235 return std::map<net::nqe::internal::NetworkID, | |
| 236 net::nqe::internal::CachedNetworkQuality>(); | |
| 237 } | |
| 238 return prefs_manager_->ForceReadPrefsForTesting(); | |
| 239 } | |
| OLD | NEW |