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