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

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

Issue 2369673004: Wire NQE Prefs to Profile (Closed)
Patch Set: More fixups 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/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/values.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 // 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 persistent caching has been enabled in the field trial.
43 bool persistent_caching_enabled() {
44 return GetStringValueForVariationParamWithDefaultValue(
45 "persistent_caching_enabled", "false") == "true";
46 }
47
48 // PrefDelegateImpl writes the provided dictionary value to the network quality
49 // estimator prefs on the disk.
50 class PrefDelegateImpl
51 : public net::NetworkQualitiesPrefsManager::PrefDelegate {
52 public:
53 // |pref_service| is used to read and write prefs from/to the disk.
54 explicit PrefDelegateImpl(PrefService* pref_service)
55 : pref_service_(pref_service), path_(prefs::kNetworkQualities) {
56 DCHECK(pref_service_);
57 }
58 ~PrefDelegateImpl() override {}
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);
66 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.WriteCount", 1);
67 }
68
69 const base::DictionaryValue& GetDictionaryValue() override {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.ReadCount", 1);
72 return *pref_service_->GetDictionary(path_);
73 }
74
75 private:
76 PrefService* pref_service_;
77
78 // |path_| is the location of the network quality estimator prefs.
79 const std::string path_;
80
81 base::ThreadChecker thread_checker_;
82
83 DISALLOW_COPY_AND_ASSIGN(PrefDelegateImpl);
84 };
85
86 // Initializes |pref_manager| on |io_thread|.
87 void SetNQEOnIOThread(net::NetworkQualitiesPrefsManager* prefs_manager,
88 IOThread* io_thread) {
89 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
90
91 // Avoid null pointer referencing during browser shutdown.
92 if (!io_thread->globals()->network_quality_estimator)
93 return;
94
95 prefs_manager->InitializeOnNetworkThread(
96 io_thread->globals()->network_quality_estimator.get());
97 }
98
99 } // namespace
11 100
12 // A class that sets itself as an observer of the EffectiveconnectionType for 101 // A class that sets itself as an observer of the EffectiveconnectionType for
13 // the browser IO thread. It reports any change in EffectiveConnectionType back 102 // the browser IO thread. It reports any change in EffectiveConnectionType back
14 // to the UI service. 103 // to the UI service.
15 // It is created on the UI thread, but used and deleted on the IO thread. 104 // It is created on the UI thread, but used and deleted on the IO thread.
16 class UINetworkQualityEstimatorService::IONetworkQualityObserver 105 class UINetworkQualityEstimatorService::IONetworkQualityObserver
17 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { 106 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver {
18 public: 107 public:
19 IONetworkQualityObserver( 108 explicit IONetworkQualityObserver(
20 base::WeakPtr<UINetworkQualityEstimatorService> service) 109 base::WeakPtr<UINetworkQualityEstimatorService> service)
21 : service_(service) { 110 : service_(service), network_quality_estimator_(nullptr) {
22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
23 } 112 }
24 113
25 ~IONetworkQualityObserver() override { 114 ~IONetworkQualityObserver() override {
26 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 115 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
27 if (network_quality_estimator_) 116 if (network_quality_estimator_)
28 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); 117 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this);
29 } 118 }
30 119
31 void InitializeOnIOThread(IOThread* io_thread) { 120 void InitializeOnIOThread(IOThread* io_thread) {
(...skipping 25 matching lines...) Expand all
57 service_, type)); 146 service_, type));
58 } 147 }
59 148
60 private: 149 private:
61 base::WeakPtr<UINetworkQualityEstimatorService> service_; 150 base::WeakPtr<UINetworkQualityEstimatorService> service_;
62 net::NetworkQualityEstimator* network_quality_estimator_; 151 net::NetworkQualityEstimator* network_quality_estimator_;
63 152
64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); 153 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver);
65 }; 154 };
66 155
67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService() 156 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService(
68 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), 157 Profile* profile)
69 io_observer_(nullptr), 158 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), weak_factory_(this) {
70 weak_factory_(this) { 159 DCHECK(profile);
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
72 // If this is running in a context without an IOThread, don't try to create 160 // If this is running in a context without an IOThread, don't try to create
73 // the IO object. 161 // the IO object.
74 if (!g_browser_process->io_thread()) 162 if (!g_browser_process->io_thread())
75 return; 163 return;
76 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); 164 io_observer_ = base::WrapUnique(
165 new IONetworkQualityObserver(weak_factory_.GetWeakPtr()));
166 std::unique_ptr<PrefDelegateImpl> pref_delegate(
167 new PrefDelegateImpl(profile->GetPrefs()));
168 prefs_manager_ = base::WrapUnique(
169 new net::NetworkQualitiesPrefsManager(std::move(pref_delegate)));
170
77 content::BrowserThread::PostTask( 171 content::BrowserThread::PostTask(
78 content::BrowserThread::IO, FROM_HERE, 172 content::BrowserThread::IO, FROM_HERE,
79 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, 173 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread,
80 base::Unretained(io_observer_), 174 base::Unretained(io_observer_.get()),
175 g_browser_process->io_thread()));
176 content::BrowserThread::PostTask(
177 content::BrowserThread::IO, FROM_HERE,
178 base::Bind(&SetNQEOnIOThread, prefs_manager_.get(),
81 g_browser_process->io_thread())); 179 g_browser_process->io_thread()));
82 } 180 }
83 181
84 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { 182 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() {
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 183 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
86 } 184 }
87 185
88 void UINetworkQualityEstimatorService::Shutdown() { 186 void UINetworkQualityEstimatorService::Shutdown() {
89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
90 weak_factory_.InvalidateWeakPtrs(); 188 weak_factory_.InvalidateWeakPtrs();
91 DCHECK(content::BrowserThread::DeleteSoon(content::BrowserThread::IO, 189 if (io_observer_) {
92 FROM_HERE, io_observer_)); 190 bool deleted = content::BrowserThread::DeleteSoon(
191 content::BrowserThread::IO, FROM_HERE, io_observer_.release());
192 DCHECK(deleted);
193 // Silence unused variable warning in release builds.
194 (void)deleted;
195 }
196 if (prefs_manager_) {
197 prefs_manager_->ShutdownOnPrefThread();
198 bool deleted = content::BrowserThread::DeleteSoon(
199 content::BrowserThread::IO, FROM_HERE, prefs_manager_.release());
200 DCHECK(deleted);
201 // Silence unused variable warning in release builds.
202 (void)deleted;
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698