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

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

Issue 2491703003: NQE: Notify observer as soon as it is added (Closed)
Patch Set: ryansturm comments Created 4 years, 1 month 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> 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 119
120 void InitializeOnIOThread(IOThread* io_thread) { 120 void InitializeOnIOThread(IOThread* io_thread) {
121 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 121 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
122 if (!io_thread->globals()->network_quality_estimator) 122 if (!io_thread->globals()->network_quality_estimator)
123 return; 123 return;
124 network_quality_estimator_ = 124 network_quality_estimator_ =
125 io_thread->globals()->network_quality_estimator.get(); 125 io_thread->globals()->network_quality_estimator.get();
126 if (!network_quality_estimator_) 126 if (!network_quality_estimator_)
127 return; 127 return;
128 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); 128 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this);
129 content::BrowserThread::PostTask(
130 content::BrowserThread::UI, FROM_HERE,
131 base::Bind(
132 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged,
133 service_,
134 network_quality_estimator_->GetEffectiveConnectionType()));
135 } 129 }
136 130
137 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver 131 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver
138 // implementation: 132 // implementation:
139 void OnEffectiveConnectionTypeChanged( 133 void OnEffectiveConnectionTypeChanged(
140 net::EffectiveConnectionType type) override { 134 net::EffectiveConnectionType type) override {
141 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 135 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
142 content::BrowserThread::PostTask( 136 content::BrowserThread::PostTask(
143 content::BrowserThread::UI, FROM_HERE, 137 content::BrowserThread::UI, FROM_HERE,
144 base::Bind( 138 base::Bind(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 202 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
209 type_ = type; 203 type_ = type;
210 for (auto& observer : effective_connection_type_observer_list_) 204 for (auto& observer : effective_connection_type_observer_list_)
211 observer.OnEffectiveConnectionTypeChanged(type); 205 observer.OnEffectiveConnectionTypeChanged(type);
212 } 206 }
213 207
214 void UINetworkQualityEstimatorService::AddEffectiveConnectionTypeObserver( 208 void UINetworkQualityEstimatorService::AddEffectiveConnectionTypeObserver(
215 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { 209 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) {
216 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 210 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
217 effective_connection_type_observer_list_.AddObserver(observer); 211 effective_connection_type_observer_list_.AddObserver(observer);
212
213 content::BrowserThread::PostTask(
214 content::BrowserThread::UI, FROM_HERE,
215 base::Bind(&UINetworkQualityEstimatorService::
216 NotifyEffectiveConnectionTypeObserverIfPresent,
217 weak_factory_.GetWeakPtr(), observer));
218 } 218 }
219 219
220 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver( 220 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver(
221 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { 221 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) {
222 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 222 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
223 effective_connection_type_observer_list_.RemoveObserver(observer); 223 effective_connection_type_observer_list_.RemoveObserver(observer);
224 } 224 }
225 225
226 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( 226 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting(
227 net::EffectiveConnectionType type) { 227 net::EffectiveConnectionType type) {
228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
229 EffectiveConnectionTypeChanged(type); 229 EffectiveConnectionTypeChanged(type);
230 } 230 }
231 231
232 net::EffectiveConnectionType 232 net::EffectiveConnectionType
233 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { 233 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const {
234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
235 return type_; 235 return type_;
236 } 236 }
237 237
238 void UINetworkQualityEstimatorService::ClearPrefs() { 238 void UINetworkQualityEstimatorService::ClearPrefs() {
239 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 239 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
240 if (!prefs_manager_) 240 if (!prefs_manager_)
241 return; 241 return;
242 prefs_manager_->ClearPrefs(); 242 prefs_manager_->ClearPrefs();
243 } 243 }
244 244
245 void UINetworkQualityEstimatorService::
246 NotifyEffectiveConnectionTypeObserverIfPresent(
247 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer)
248 const {
249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
250
251 if (!effective_connection_type_observer_list_.HasObserver(observer))
252 return;
253 if (type_ == net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN)
254 return;
255 observer->OnEffectiveConnectionTypeChanged(type_);
256 }
257
245 // static 258 // static
246 void UINetworkQualityEstimatorService::RegisterProfilePrefs( 259 void UINetworkQualityEstimatorService::RegisterProfilePrefs(
247 PrefRegistrySimple* registry) { 260 PrefRegistrySimple* registry) {
248 registry->RegisterDictionaryPref(prefs::kNetworkQualities, 261 registry->RegisterDictionaryPref(prefs::kNetworkQualities,
249 PrefRegistry::LOSSY_PREF); 262 PrefRegistry::LOSSY_PREF);
250 } 263 }
251 264
252 std::map<net::nqe::internal::NetworkID, 265 std::map<net::nqe::internal::NetworkID,
253 net::nqe::internal::CachedNetworkQuality> 266 net::nqe::internal::CachedNetworkQuality>
254 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { 267 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const {
255 if (!prefs_manager_) { 268 if (!prefs_manager_) {
256 return std::map<net::nqe::internal::NetworkID, 269 return std::map<net::nqe::internal::NetworkID,
257 net::nqe::internal::CachedNetworkQuality>(); 270 net::nqe::internal::CachedNetworkQuality>();
258 } 271 }
259 return prefs_manager_->ForceReadPrefsForTesting(); 272 return prefs_manager_->ForceReadPrefsForTesting();
260 } 273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698