| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 std::map<std::string, std::string> network_quality_estimator_params; | 32 std::map<std::string, std::string> network_quality_estimator_params; |
| 33 // Name of the network quality estimator field trial. | 33 // Name of the network quality estimator field trial. |
| 34 variations::GetVariationParams("NetworkQualityEstimator", | 34 variations::GetVariationParams("NetworkQualityEstimator", |
| 35 &network_quality_estimator_params); | 35 &network_quality_estimator_params); |
| 36 | 36 |
| 37 const auto it = network_quality_estimator_params.find(parameter_name); | 37 const auto it = network_quality_estimator_params.find(parameter_name); |
| 38 return it == network_quality_estimator_params.end() ? default_value | 38 return it == network_quality_estimator_params.end() ? default_value |
| 39 : it->second; | 39 : it->second; |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Returns true if persistent caching has been enabled in the field trial. | 42 // Returns true if writing to the persistent cache has been enabled via field |
| 43 bool persistent_caching_enabled() { | 43 // trial. |
| 44 bool persistent_cache_writing_enabled() { |
| 44 return GetStringValueForVariationParamWithDefaultValue( | 45 return GetStringValueForVariationParamWithDefaultValue( |
| 45 "persistent_caching_enabled", "false") == "true"; | 46 "persistent_cache_writing_enabled", "false") == "true"; |
| 47 } |
| 48 |
| 49 // Returns true if reading from the persistent cache has been enabled via field |
| 50 // trial. |
| 51 bool persistent_cache_reading_enabled() { |
| 52 return GetStringValueForVariationParamWithDefaultValue( |
| 53 "persistent_cache_reading_enabled", "false") == "true"; |
| 46 } | 54 } |
| 47 | 55 |
| 48 // PrefDelegateImpl writes the provided dictionary value to the network quality | 56 // PrefDelegateImpl writes the provided dictionary value to the network quality |
| 49 // estimator prefs on the disk. | 57 // estimator prefs on the disk. |
| 50 class PrefDelegateImpl | 58 class PrefDelegateImpl |
| 51 : public net::NetworkQualitiesPrefsManager::PrefDelegate { | 59 : public net::NetworkQualitiesPrefsManager::PrefDelegate { |
| 52 public: | 60 public: |
| 53 // |pref_service| is used to read and write prefs from/to the disk. | 61 // |pref_service| is used to read and write prefs from/to the disk. |
| 54 explicit PrefDelegateImpl(PrefService* pref_service) | 62 explicit PrefDelegateImpl(PrefService* pref_service) |
| 55 : pref_service_(pref_service), path_(prefs::kNetworkQualities) { | 63 : pref_service_(pref_service), path_(prefs::kNetworkQualities) { |
| 56 DCHECK(pref_service_); | 64 DCHECK(pref_service_); |
| 57 } | 65 } |
| 58 ~PrefDelegateImpl() override {} | 66 ~PrefDelegateImpl() override {} |
| 59 | 67 |
| 60 void SetDictionaryValue(const base::DictionaryValue& value) override { | 68 void SetDictionaryValue(const base::DictionaryValue& value) override { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 if (!persistent_caching_enabled()) | 70 if (!persistent_cache_writing_enabled()) |
| 63 return; | 71 return; |
| 64 | 72 |
| 65 pref_service_->Set(path_, value); | 73 pref_service_->Set(path_, value); |
| 66 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.WriteCount", 1); | 74 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.WriteCount", 1); |
| 67 } | 75 } |
| 68 | 76 |
| 69 const base::DictionaryValue& GetDictionaryValue() override { | 77 std::unique_ptr<base::DictionaryValue> GetDictionaryValue() override { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 if (!persistent_cache_reading_enabled()) |
| 80 return base::WrapUnique(new base::DictionaryValue()); |
| 71 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.ReadCount", 1); | 81 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.ReadCount", 1); |
| 72 return *pref_service_->GetDictionary(path_); | 82 return pref_service_->GetDictionary(path_)->CreateDeepCopy(); |
| 73 } | 83 } |
| 74 | 84 |
| 75 private: | 85 private: |
| 76 PrefService* pref_service_; | 86 PrefService* pref_service_; |
| 77 | 87 |
| 78 // |path_| is the location of the network quality estimator prefs. | 88 // |path_| is the location of the network quality estimator prefs. |
| 79 const std::string path_; | 89 const std::string path_; |
| 80 | 90 |
| 81 base::ThreadChecker thread_checker_; | 91 base::ThreadChecker thread_checker_; |
| 82 | 92 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 276 |
| 267 std::map<net::nqe::internal::NetworkID, | 277 std::map<net::nqe::internal::NetworkID, |
| 268 net::nqe::internal::CachedNetworkQuality> | 278 net::nqe::internal::CachedNetworkQuality> |
| 269 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { | 279 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { |
| 270 if (!prefs_manager_) { | 280 if (!prefs_manager_) { |
| 271 return std::map<net::nqe::internal::NetworkID, | 281 return std::map<net::nqe::internal::NetworkID, |
| 272 net::nqe::internal::CachedNetworkQuality>(); | 282 net::nqe::internal::CachedNetworkQuality>(); |
| 273 } | 283 } |
| 274 return prefs_manager_->ForceReadPrefsForTesting(); | 284 return prefs_manager_->ForceReadPrefsForTesting(); |
| 275 } | 285 } |
| OLD | NEW |