Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/net/nqe/ui_network_quality_estimator_service.cc

Issue 2369673004: Wire NQE Prefs to Profile (Closed)
Patch Set: Addressed ryansturm comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/values.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 // |pref_service| is used to read and write prefs from/to the disk.
55 explicit PrefDelegateImpl(PrefService* pref_service)
56 : pref_service_(pref_service), path_(prefs::kNetworkQualities) {
57 DCHECK(pref_service_);
58 }
59 ~PrefDelegateImpl() override {}
60
61 void SetDictionaryValue(const base::DictionaryValue& value) override {
62 DCHECK(thread_checker_.CalledOnValidThread());
63 if (!persistent_caching_enabled())
64 return;
65
66 pref_service_->Set(path_, value);
67 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.WriteCount", 1);
68 }
69
70 const base::DictionaryValue& GetDictionaryValue() override {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.ReadCount", 1);
73 return *pref_service_->GetDictionary(path_);
74 }
75
76 private:
77 PrefService* pref_service_;
78
79 // |path_| is the location of the network quality estimator prefs.
80 const std::string path_;
81
82 base::ThreadChecker thread_checker_;
83
84 DISALLOW_COPY_AND_ASSIGN(PrefDelegateImpl);
85 };
86
87 // Initializes |pref_manager| on |io_thread|.
88 void SetNQEOnIOThread(net::NetworkQualitiesPrefsManager* prefs_manager,
89 IOThread* io_thread) {
90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
91
92 // Avoid null pointer referencing during browser shutdown.
93 if (!io_thread->globals()->network_quality_estimator)
94 return;
95
96 prefs_manager->InitializeOnNetworkThread(
97 io_thread->globals()->network_quality_estimator.get());
98 }
99
100 } // namespace
11 101
12 // A class that sets itself as an observer of the EffectiveconnectionType for 102 // A class that sets itself as an observer of the EffectiveconnectionType for
13 // the browser IO thread. It reports any change in EffectiveConnectionType back 103 // the browser IO thread. It reports any change in EffectiveConnectionType back
14 // to the UI service. 104 // to the UI service.
15 // It is created on the UI thread, but used and deleted on the IO thread. 105 // It is created on the UI thread, but used and deleted on the IO thread.
16 class UINetworkQualityEstimatorService::IONetworkQualityObserver 106 class UINetworkQualityEstimatorService::IONetworkQualityObserver
17 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { 107 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver {
18 public: 108 public:
19 IONetworkQualityObserver( 109 explicit IONetworkQualityObserver(
20 base::WeakPtr<UINetworkQualityEstimatorService> service) 110 base::WeakPtr<UINetworkQualityEstimatorService> service)
21 : service_(service) { 111 : service_(service) {
22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
23 } 113 }
24 114
25 ~IONetworkQualityObserver() override { 115 ~IONetworkQualityObserver() override {
26 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 116 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
27 if (network_quality_estimator_) 117 if (network_quality_estimator_)
28 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); 118 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this);
29 } 119 }
(...skipping 27 matching lines...) Expand all
57 service_, type)); 147 service_, type));
58 } 148 }
59 149
60 private: 150 private:
61 base::WeakPtr<UINetworkQualityEstimatorService> service_; 151 base::WeakPtr<UINetworkQualityEstimatorService> service_;
62 net::NetworkQualityEstimator* network_quality_estimator_; 152 net::NetworkQualityEstimator* network_quality_estimator_;
63 153
64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); 154 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver);
65 }; 155 };
66 156
67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService() 157 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService(
158 Profile* profile)
68 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), 159 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
69 io_observer_(nullptr), 160 io_observer_(nullptr),
161 prefs_manager_(nullptr),
70 weak_factory_(this) { 162 weak_factory_(this) {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
164 DCHECK(profile);
72 // If this is running in a context without an IOThread, don't try to create 165 // If this is running in a context without an IOThread, don't try to create
73 // the IO object. 166 // the IO object.
74 if (!g_browser_process->io_thread()) 167 if (!g_browser_process->io_thread())
75 return; 168 return;
76 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); 169 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr());
170 std::unique_ptr<PrefDelegateImpl> pref_delegate(
171 new PrefDelegateImpl(profile->GetPrefs()));
172 prefs_manager_ =
173 new net::NetworkQualitiesPrefsManager(std::move(pref_delegate));
174
77 content::BrowserThread::PostTask( 175 content::BrowserThread::PostTask(
78 content::BrowserThread::IO, FROM_HERE, 176 content::BrowserThread::IO, FROM_HERE,
79 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, 177 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread,
80 base::Unretained(io_observer_), 178 base::Unretained(io_observer_),
81 g_browser_process->io_thread())); 179 g_browser_process->io_thread()));
180 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
181 base::Bind(&SetNQEOnIOThread, prefs_manager_,
182 g_browser_process->io_thread()));
82 } 183 }
83 184
84 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { 185 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() {
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 186 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
86 } 187 }
87 188
88 void UINetworkQualityEstimatorService::Shutdown() { 189 void UINetworkQualityEstimatorService::Shutdown() {
89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 190 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
90 weak_factory_.InvalidateWeakPtrs(); 191 weak_factory_.InvalidateWeakPtrs();
91 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO, 192 if (io_observer_) {
92 FROM_HERE, io_observer_)); 193 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO,
bengr 2016/10/14 22:37:12 Why is this in a DCHECK? Do you not want this to h
RyanSturm 2016/10/14 22:52:03 Should we put io_observer_ and prefs_manager_ in s
tbansal1 2016/10/14 23:25:53 Done.
tbansal1 2016/10/14 23:25:53 Done.
194 FROM_HERE, io_observer_));
195 io_observer_ = nullptr;
196 }
197 if (prefs_manager_) {
198 prefs_manager_->ShutdownOnPrefThread();
199 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO,
bengr 2016/10/14 22:37:13 Why is this in a DCHECK? Do you not want this to h
tbansal1 2016/10/14 23:25:53 Done.
200 FROM_HERE, prefs_manager_));
201 prefs_manager_ = nullptr;
202 }
93 } 203 }
94 204
95 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( 205 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged(
96 net::EffectiveConnectionType type) { 206 net::EffectiveConnectionType type) {
97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 207 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
98 type_ = type; 208 type_ = type;
99 } 209 }
100 210
101 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( 211 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting(
102 net::EffectiveConnectionType type) { 212 net::EffectiveConnectionType type) {
103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 213 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
104 type_ = type; 214 type_ = type;
105 } 215 }
106 216
107 net::EffectiveConnectionType 217 net::EffectiveConnectionType
108 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { 218 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const {
109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
110 return type_; 220 return type_;
111 } 221 }
222
223 // static
224 void UINetworkQualityEstimatorService::RegisterProfilePrefs(
bengr 2016/10/14 22:37:12 I don't like the name of this class. UI... ick.
tbansal1 2016/10/14 23:25:53 May be we can change it in a separate CL.
225 PrefRegistrySimple* registry) {
226 registry->RegisterDictionaryPref(prefs::kNetworkQualities,
227 PrefRegistry::LOSSY_PREF);
228 }
229
230 std::map<net::nqe::internal::NetworkID,
231 net::nqe::internal::CachedNetworkQuality>
232 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const {
233 if (!prefs_manager_) {
234 return std::map<net::nqe::internal::NetworkID,
235 net::nqe::internal::CachedNetworkQuality>();
236 }
237 return prefs_manager_->ForceReadPrefsForTesting();
238 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698