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

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: Addressed bengr 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 // Notify the |observer| on the next message pump since |observer| may not
214 // be completely set up for receiving the callbacks.
215 content::BrowserThread::PostTask(
216 content::BrowserThread::UI, FROM_HERE,
217 base::Bind(&UINetworkQualityEstimatorService::
218 NotifyEffectiveConnectionTypeObserverIfPresent,
219 weak_factory_.GetWeakPtr(), observer));
218 } 220 }
219 221
220 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver( 222 void UINetworkQualityEstimatorService::RemoveEffectiveConnectionTypeObserver(
221 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) { 223 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) {
222 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 224 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
223 effective_connection_type_observer_list_.RemoveObserver(observer); 225 effective_connection_type_observer_list_.RemoveObserver(observer);
224 } 226 }
225 227
226 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting( 228 void UINetworkQualityEstimatorService::SetEffectiveConnectionTypeForTesting(
227 net::EffectiveConnectionType type) { 229 net::EffectiveConnectionType type) {
228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 230 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
229 EffectiveConnectionTypeChanged(type); 231 EffectiveConnectionTypeChanged(type);
230 } 232 }
231 233
232 net::EffectiveConnectionType 234 net::EffectiveConnectionType
233 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const { 235 UINetworkQualityEstimatorService::GetEffectiveConnectionType() const {
234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 236 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
235 return type_; 237 return type_;
236 } 238 }
237 239
238 void UINetworkQualityEstimatorService::ClearPrefs() { 240 void UINetworkQualityEstimatorService::ClearPrefs() {
239 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 241 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
240 if (!prefs_manager_) 242 if (!prefs_manager_)
241 return; 243 return;
242 prefs_manager_->ClearPrefs(); 244 prefs_manager_->ClearPrefs();
243 } 245 }
244 246
247 void UINetworkQualityEstimatorService::
248 NotifyEffectiveConnectionTypeObserverIfPresent(
249 net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer)
250 const {
251 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
252
253 if (!effective_connection_type_observer_list_.HasObserver(observer))
254 return;
255 if (type_ == net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN)
256 return;
257 observer->OnEffectiveConnectionTypeChanged(type_);
258 }
259
245 // static 260 // static
246 void UINetworkQualityEstimatorService::RegisterProfilePrefs( 261 void UINetworkQualityEstimatorService::RegisterProfilePrefs(
247 PrefRegistrySimple* registry) { 262 PrefRegistrySimple* registry) {
248 registry->RegisterDictionaryPref(prefs::kNetworkQualities, 263 registry->RegisterDictionaryPref(prefs::kNetworkQualities,
249 PrefRegistry::LOSSY_PREF); 264 PrefRegistry::LOSSY_PREF);
250 } 265 }
251 266
252 std::map<net::nqe::internal::NetworkID, 267 std::map<net::nqe::internal::NetworkID,
253 net::nqe::internal::CachedNetworkQuality> 268 net::nqe::internal::CachedNetworkQuality>
254 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const { 269 UINetworkQualityEstimatorService::ForceReadPrefsForTesting() const {
255 if (!prefs_manager_) { 270 if (!prefs_manager_) {
256 return std::map<net::nqe::internal::NetworkID, 271 return std::map<net::nqe::internal::NetworkID,
257 net::nqe::internal::CachedNetworkQuality>(); 272 net::nqe::internal::CachedNetworkQuality>();
258 } 273 }
259 return prefs_manager_->ForceReadPrefsForTesting(); 274 return prefs_manager_->ForceReadPrefsForTesting();
260 } 275 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698