| 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" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/io_thread.h" | 15 #include "chrome/browser/io_thread.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "components/prefs/pref_registry.h" | 18 #include "components/prefs/pref_registry.h" |
| 19 #include "components/prefs/pref_registry_simple.h" | 19 #include "components/prefs/pref_registry_simple.h" |
| 20 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 21 #include "components/variations/variations_associated_data.h" | 21 #include "components/variations/variations_associated_data.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "net/nqe/network_qualities_prefs_manager.h" | 23 #include "net/nqe/network_qualities_prefs_manager.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // Returns the variation value for |parameter_name|. If the value is | |
| 28 // unavailable, |default_value| is returned. | |
| 29 std::string GetStringValueForVariationParamWithDefaultValue( | |
| 30 const std::string& parameter_name, | |
| 31 const std::string& default_value) { | |
| 32 std::map<std::string, std::string> network_quality_estimator_params; | |
| 33 // Name of the network quality estimator field trial. | |
| 34 variations::GetVariationParams("NetworkQualityEstimator", | |
| 35 &network_quality_estimator_params); | |
| 36 | |
| 37 const auto it = network_quality_estimator_params.find(parameter_name); | |
| 38 return it == network_quality_estimator_params.end() ? default_value | |
| 39 : it->second; | |
| 40 } | |
| 41 | |
| 42 // Returns true if writing to the persistent cache has been enabled via field | |
| 43 // trial. | |
| 44 bool persistent_cache_writing_enabled() { | |
| 45 return GetStringValueForVariationParamWithDefaultValue( | |
| 46 "persistent_cache_writing_enabled", "true") == "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 if (GetStringValueForVariationParamWithDefaultValue( | |
| 53 "persistent_cache_reading_enabled", "false") != "true") { | |
| 54 return false; | |
| 55 } | |
| 56 // If reading from prefs is enabled, then writing to prefs must be enabled | |
| 57 // too. | |
| 58 DCHECK(persistent_cache_writing_enabled()); | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 // PrefDelegateImpl writes the provided dictionary value to the network quality | 27 // PrefDelegateImpl writes the provided dictionary value to the network quality |
| 63 // estimator prefs on the disk. | 28 // estimator prefs on the disk. |
| 64 class PrefDelegateImpl | 29 class PrefDelegateImpl |
| 65 : public net::NetworkQualitiesPrefsManager::PrefDelegate { | 30 : public net::NetworkQualitiesPrefsManager::PrefDelegate { |
| 66 public: | 31 public: |
| 67 // |pref_service| is used to read and write prefs from/to the disk. | 32 // |pref_service| is used to read and write prefs from/to the disk. |
| 68 explicit PrefDelegateImpl(PrefService* pref_service) | 33 explicit PrefDelegateImpl(PrefService* pref_service) |
| 69 : pref_service_(pref_service), path_(prefs::kNetworkQualities) { | 34 : pref_service_(pref_service), path_(prefs::kNetworkQualities) { |
| 70 DCHECK(pref_service_); | 35 DCHECK(pref_service_); |
| 71 } | 36 } |
| 72 ~PrefDelegateImpl() override {} | 37 ~PrefDelegateImpl() override {} |
| 73 | 38 |
| 74 void SetDictionaryValue(const base::DictionaryValue& value) override { | 39 void SetDictionaryValue(const base::DictionaryValue& value) override { |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 if (!persistent_cache_writing_enabled()) | |
| 77 return; | |
| 78 | 41 |
| 79 pref_service_->Set(path_, value); | 42 pref_service_->Set(path_, value); |
| 80 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.WriteCount", 1, 2); | 43 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.WriteCount", 1, 2); |
| 81 } | 44 } |
| 82 | 45 |
| 83 std::unique_ptr<base::DictionaryValue> GetDictionaryValue() override { | 46 std::unique_ptr<base::DictionaryValue> GetDictionaryValue() override { |
| 84 DCHECK(thread_checker_.CalledOnValidThread()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
| 85 if (!persistent_cache_reading_enabled()) | |
| 86 return base::WrapUnique(new base::DictionaryValue()); | |
| 87 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.ReadCount", 1, 2); | 48 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.ReadCount", 1, 2); |
| 88 return pref_service_->GetDictionary(path_)->CreateDeepCopy(); | 49 return pref_service_->GetDictionary(path_)->CreateDeepCopy(); |
| 89 } | 50 } |
| 90 | 51 |
| 91 private: | 52 private: |
| 92 PrefService* pref_service_; | 53 PrefService* pref_service_; |
| 93 | 54 |
| 94 // |path_| is the location of the network quality estimator prefs. | 55 // |path_| is the location of the network quality estimator prefs. |
| 95 const std::string path_; | 56 const std::string path_; |
| 96 | 57 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 | 243 |
| 283 std::map<net::nqe::internal::NetworkID, | 244 std::map<net::nqe::internal::NetworkID, |
| 284 net::nqe::internal::CachedNetworkQuality> | 245 net::nqe::internal::CachedNetworkQuality> |
| 285 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { | 246 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { |
| 286 if (!prefs_manager_) { | 247 if (!prefs_manager_) { |
| 287 return std::map<net::nqe::internal::NetworkID, | 248 return std::map<net::nqe::internal::NetworkID, |
| 288 net::nqe::internal::CachedNetworkQuality>(); | 249 net::nqe::internal::CachedNetworkQuality>(); |
| 289 } | 250 } |
| 290 return prefs_manager_->ForceReadPrefsForTesting(); | 251 return prefs_manager_->ForceReadPrefsForTesting(); |
| 291 } | 252 } |
| OLD | NEW |