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> | |
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 // Name of the network quality estimator field trial. | |
28 const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator"; | |
bengr
2016/10/18 21:47:36
This is only used on line 36, so you might conside
tbansal1
2016/10/19 18:34:46
Done.
| |
29 | |
30 // Returns the variation value for |parameter_name|. If the value is | |
31 // unavailable, |default_value| is returned. | |
32 std::string GetStringValueForVariationParamWithDefaultValue( | |
33 const std::string& parameter_name, | |
34 const std::string& default_value) { | |
35 std::map<std::string, std::string> network_quality_estimator_params; | |
36 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName, | |
37 &network_quality_estimator_params); | |
38 | |
39 const auto it = network_quality_estimator_params.find(parameter_name); | |
40 return it == network_quality_estimator_params.end() ? default_value | |
41 : it->second; | |
42 } | |
43 | |
44 // Returns true if persistent caching has been enabled in the field trial. | |
45 bool persistent_caching_enabled() { | |
46 return GetStringValueForVariationParamWithDefaultValue( | |
47 "persistent_caching_enabled", "false") == "true"; | |
48 } | |
49 | |
50 // PrefDelegateImpl writes the provided dictionary value to the network quality | |
51 // estimator prefs on the disk. | |
52 class PrefDelegateImpl | |
53 : public net::NetworkQualitiesPrefsManager::PrefDelegate { | |
54 public: | |
55 // |pref_service| is used to read and write prefs from/to the disk. | |
56 explicit PrefDelegateImpl(PrefService* pref_service) | |
57 : pref_service_(pref_service), path_(prefs::kNetworkQualities) { | |
58 DCHECK(pref_service_); | |
59 } | |
60 ~PrefDelegateImpl() override {} | |
61 | |
62 void SetDictionaryValue(const base::DictionaryValue& value) override { | |
63 DCHECK(thread_checker_.CalledOnValidThread()); | |
64 if (!persistent_caching_enabled()) | |
65 return; | |
66 | |
67 pref_service_->Set(path_, value); | |
68 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.WriteCount", 1); | |
69 } | |
70 | |
71 const base::DictionaryValue& GetDictionaryValue() override { | |
72 DCHECK(thread_checker_.CalledOnValidThread()); | |
73 UMA_HISTOGRAM_COUNTS_1000("NQE.Prefs.ReadCount", 1); | |
74 return *pref_service_->GetDictionary(path_); | |
75 } | |
76 | |
77 private: | |
78 PrefService* pref_service_; | |
79 | |
80 // |path_| is the location of the network quality estimator prefs. | |
81 const std::string path_; | |
82 | |
83 base::ThreadChecker thread_checker_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(PrefDelegateImpl); | |
86 }; | |
87 | |
88 // Initializes |pref_manager| on |io_thread|. | |
89 void SetNQEOnIOThread(net::NetworkQualitiesPrefsManager* prefs_manager, | |
90 IOThread* io_thread) { | |
91 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
92 | |
93 // Avoid null pointer referencing during browser shutdown. | |
94 if (!io_thread->globals()->network_quality_estimator) | |
95 return; | |
96 | |
97 prefs_manager->InitializeOnNetworkThread( | |
98 io_thread->globals()->network_quality_estimator.get()); | |
99 } | |
100 | |
101 } // namespace | |
11 | 102 |
12 // A class that sets itself as an observer of the EffectiveconnectionType for | 103 // A class that sets itself as an observer of the EffectiveconnectionType for |
13 // the browser IO thread. It reports any change in EffectiveConnectionType back | 104 // the browser IO thread. It reports any change in EffectiveConnectionType back |
14 // to the UI service. | 105 // to the UI service. |
15 // It is created on the UI thread, but used and deleted on the IO thread. | 106 // It is created on the UI thread, but used and deleted on the IO thread. |
16 class UINetworkQualityEstimatorService::IONetworkQualityObserver | 107 class UINetworkQualityEstimatorService::IONetworkQualityObserver |
17 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | 108 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { |
18 public: | 109 public: |
19 IONetworkQualityObserver( | 110 explicit IONetworkQualityObserver( |
20 base::WeakPtr<UINetworkQualityEstimatorService> service) | 111 base::WeakPtr<UINetworkQualityEstimatorService> service) |
21 : service_(service) { | 112 : service_(service) { |
22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
23 } | 114 } |
24 | 115 |
25 ~IONetworkQualityObserver() override { | 116 ~IONetworkQualityObserver() override { |
26 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 117 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
27 if (network_quality_estimator_) | 118 if (network_quality_estimator_) |
28 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); | 119 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); |
29 } | 120 } |
(...skipping 27 matching lines...) Expand all Loading... | |
57 service_, type)); | 148 service_, type)); |
58 } | 149 } |
59 | 150 |
60 private: | 151 private: |
61 base::WeakPtr<UINetworkQualityEstimatorService> service_; | 152 base::WeakPtr<UINetworkQualityEstimatorService> service_; |
62 net::NetworkQualityEstimator* network_quality_estimator_; | 153 net::NetworkQualityEstimator* network_quality_estimator_; |
63 | 154 |
64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); | 155 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); |
65 }; | 156 }; |
66 | 157 |
67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService() | 158 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( |
68 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | 159 Profile* profile) |
69 io_observer_(nullptr), | 160 : type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), weak_factory_(this) { |
70 weak_factory_(this) { | |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
162 DCHECK(profile); | |
72 // If this is running in a context without an IOThread, don't try to create | 163 // If this is running in a context without an IOThread, don't try to create |
73 // the IO object. | 164 // the IO object. |
74 if (!g_browser_process->io_thread()) | 165 if (!g_browser_process->io_thread()) |
75 return; | 166 return; |
76 io_observer_ = new IONetworkQualityObserver(weak_factory_.GetWeakPtr()); | 167 io_observer_ = base::WrapUnique( |
168 new IONetworkQualityObserver(weak_factory_.GetWeakPtr())); | |
169 std::unique_ptr<PrefDelegateImpl> pref_delegate( | |
170 new PrefDelegateImpl(profile->GetPrefs())); | |
171 prefs_manager_ = base::WrapUnique( | |
172 new net::NetworkQualitiesPrefsManager(std::move(pref_delegate))); | |
173 | |
77 content::BrowserThread::PostTask( | 174 content::BrowserThread::PostTask( |
78 content::BrowserThread::IO, FROM_HERE, | 175 content::BrowserThread::IO, FROM_HERE, |
79 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, | 176 base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, |
80 base::Unretained(io_observer_), | 177 base::Unretained(io_observer_.get()), |
178 g_browser_process->io_thread())); | |
179 content::BrowserThread::PostTask( | |
180 content::BrowserThread::IO, FROM_HERE, | |
181 base::Bind(&SetNQEOnIOThread, prefs_manager_.get(), | |
81 g_browser_process->io_thread())); | 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 bool deleted = content::BrowserThread::DeleteSoon( |
194 content::BrowserThread::IO, FROM_HERE, io_observer_.release()); | |
195 DCHECK(deleted); | |
196 // Silence unused variable warning in release builds. | |
197 (void)deleted; | |
198 } | |
199 if (prefs_manager_) { | |
200 prefs_manager_->ShutdownOnPrefThread(); | |
201 bool deleted = content::BrowserThread::DeleteSoon( | |
202 content::BrowserThread::IO, FROM_HERE, prefs_manager_.release()); | |
203 DCHECK(deleted); | |
204 // Silence unused variable warning in release builds. | |
205 (void)deleted; | |
206 } | |
93 } | 207 } |
94 | 208 |
95 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( | 209 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( |
96 net::EffectiveConnectionType type) { | 210 net::EffectiveConnectionType type) { |
97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 211 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
98 type_ = type; | 212 type_ = type; |
99 } | 213 } |
100 | 214 |
101 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( | 215 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( |
102 net::EffectiveConnectionType type) { | 216 net::EffectiveConnectionType type) { |
103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 217 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
104 type_ = type; | 218 type_ = type; |
105 } | 219 } |
106 | 220 |
107 net::EffectiveConnectionType | 221 net::EffectiveConnectionType |
108 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { | 222 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { |
109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 223 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
110 return type_; | 224 return type_; |
111 } | 225 } |
226 | |
227 // static | |
228 void UINetworkQualityEstimatorService::RegisterProfilePrefs( | |
229 PrefRegistrySimple* registry) { | |
230 registry->RegisterDictionaryPref(prefs::kNetworkQualities, | |
231 PrefRegistry::LOSSY_PREF); | |
232 } | |
233 | |
234 std::map<net::nqe::internal::NetworkID, | |
235 net::nqe::internal::CachedNetworkQuality> | |
236 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { | |
237 if (!prefs_manager_) { | |
238 return std::map<net::nqe::internal::NetworkID, | |
239 net::nqe::internal::CachedNetworkQuality>(); | |
240 } | |
241 return prefs_manager_->ForceReadPrefsForTesting(); | |
242 } | |
OLD | NEW |