Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "net/nqe/network_quality_estimator.h" | 5 #include "net/nqe/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/metrics/histogram_base.h" | 16 #include "base/metrics/histogram_base.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
|
bengr
2016/06/01 17:41:04
Is this needed anymore?
tbansal1
2016/06/01 18:24:36
Done.
| |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/default_tick_clock.h" | 20 #include "base/time/default_tick_clock.h" |
| 21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/base/load_timing_info.h" | 24 #include "net/base/load_timing_info.h" |
| 25 #include "net/base/network_interfaces.h" | 25 #include "net/base/network_interfaces.h" |
| 26 #include "net/base/url_util.h" | 26 #include "net/base/url_util.h" |
| 27 #include "net/nqe/socket_watcher_factory.h" | |
| 27 #include "net/nqe/throughput_analyzer.h" | 28 #include "net/nqe/throughput_analyzer.h" |
| 28 #include "net/socket/socket_performance_watcher.h" | |
| 29 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 30 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 31 | 31 |
| 32 #if defined(OS_ANDROID) | 32 #if defined(OS_ANDROID) |
| 33 #include "net/android/network_library.h" | 33 #include "net/android/network_library.h" |
| 34 #endif // OS_ANDROID | 34 #endif // OS_ANDROID |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // Default value of the half life (in seconds) for computing time weighted | 38 // Default value of the half life (in seconds) for computing time weighted |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 | 154 |
| 155 // Returns true if the scheme of the |request| is either HTTP or HTTPS. | 155 // Returns true if the scheme of the |request| is either HTTP or HTTPS. |
| 156 bool RequestSchemeIsHTTPOrHTTPS(const net::URLRequest& request) { | 156 bool RequestSchemeIsHTTPOrHTTPS(const net::URLRequest& request) { |
| 157 return request.url().is_valid() && request.url().SchemeIsHTTPOrHTTPS(); | 157 return request.url().is_valid() && request.url().SchemeIsHTTPOrHTTPS(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 } // namespace | 160 } // namespace |
| 161 | 161 |
| 162 namespace net { | 162 namespace net { |
| 163 | 163 |
| 164 // SocketWatcher implements SocketPerformanceWatcher, and notifies | |
| 165 // NetworkQualityEstimator of various socket performance events. SocketWatcher | |
| 166 // is not thread-safe. | |
| 167 class NetworkQualityEstimator::SocketWatcher : public SocketPerformanceWatcher { | |
| 168 public: | |
| 169 SocketWatcher( | |
| 170 SocketPerformanceWatcherFactory::Protocol protocol, | |
| 171 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 172 const base::WeakPtr<NetworkQualityEstimator>& network_quality_estimator) | |
| 173 : protocol_(protocol), | |
| 174 task_runner_(std::move(task_runner)), | |
| 175 network_quality_estimator_(network_quality_estimator) {} | |
| 176 | |
| 177 ~SocketWatcher() override {} | |
| 178 | |
| 179 // SocketPerformanceWatcher implementation: | |
| 180 bool ShouldNotifyUpdatedRTT() const override { | |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 182 | |
| 183 return true; | |
| 184 } | |
| 185 | |
| 186 void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override { | |
| 187 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 188 | |
| 189 task_runner_->PostTask( | |
| 190 FROM_HERE, base::Bind(&NetworkQualityEstimator::OnUpdatedRTTAvailable, | |
| 191 network_quality_estimator_, protocol_, rtt)); | |
| 192 } | |
| 193 | |
| 194 void OnConnectionChanged() override { | |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 196 } | |
| 197 | |
| 198 private: | |
| 199 // Transport layer protocol used by the socket that |this| is watching. | |
| 200 const SocketPerformanceWatcherFactory::Protocol protocol_; | |
| 201 | |
| 202 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 203 | |
| 204 base::WeakPtr<NetworkQualityEstimator> network_quality_estimator_; | |
| 205 | |
| 206 base::ThreadChecker thread_checker_; | |
| 207 | |
| 208 DISALLOW_COPY_AND_ASSIGN(SocketWatcher); | |
| 209 }; | |
| 210 | |
| 211 // SocketWatcherFactory implements SocketPerformanceWatcherFactory, and is | |
| 212 // owned by NetworkQualityEstimator. SocketWatcherFactory is thread safe. | |
| 213 class NetworkQualityEstimator::SocketWatcherFactory | |
| 214 : public SocketPerformanceWatcherFactory { | |
| 215 public: | |
| 216 SocketWatcherFactory( | |
| 217 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 218 const base::WeakPtr<NetworkQualityEstimator>& network_quality_estimator) | |
| 219 : task_runner_(std::move(task_runner)), | |
| 220 network_quality_estimator_(network_quality_estimator) {} | |
| 221 | |
| 222 ~SocketWatcherFactory() override {} | |
| 223 | |
| 224 // SocketPerformanceWatcherFactory implementation: | |
| 225 std::unique_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( | |
| 226 const Protocol protocol) override { | |
| 227 return std::unique_ptr<SocketPerformanceWatcher>( | |
| 228 new SocketWatcher(protocol, task_runner_, network_quality_estimator_)); | |
| 229 } | |
| 230 | |
| 231 private: | |
| 232 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 233 | |
| 234 base::WeakPtr<NetworkQualityEstimator> network_quality_estimator_; | |
| 235 | |
| 236 DISALLOW_COPY_AND_ASSIGN(SocketWatcherFactory); | |
| 237 }; | |
| 238 | |
| 239 NetworkQualityEstimator::NetworkQualityEstimator( | 164 NetworkQualityEstimator::NetworkQualityEstimator( |
| 240 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 165 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 241 const std::map<std::string, std::string>& variation_params) | 166 const std::map<std::string, std::string>& variation_params) |
| 242 : NetworkQualityEstimator(std::move(external_estimates_provider), | 167 : NetworkQualityEstimator(std::move(external_estimates_provider), |
| 243 variation_params, | 168 variation_params, |
| 244 false, | 169 false, |
| 245 false) {} | 170 false) {} |
| 246 | 171 |
| 247 NetworkQualityEstimator::NetworkQualityEstimator( | 172 NetworkQualityEstimator::NetworkQualityEstimator( |
| 248 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 173 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 } | 212 } |
| 288 current_network_id_ = GetCurrentNetworkID(); | 213 current_network_id_ = GetCurrentNetworkID(); |
| 289 AddDefaultEstimates(); | 214 AddDefaultEstimates(); |
| 290 | 215 |
| 291 throughput_analyzer_.reset(new nqe::internal::ThroughputAnalyzer( | 216 throughput_analyzer_.reset(new nqe::internal::ThroughputAnalyzer( |
| 292 base::ThreadTaskRunnerHandle::Get(), | 217 base::ThreadTaskRunnerHandle::Get(), |
| 293 base::Bind(&NetworkQualityEstimator::OnNewThroughputObservationAvailable, | 218 base::Bind(&NetworkQualityEstimator::OnNewThroughputObservationAvailable, |
| 294 base::Unretained(this)), | 219 base::Unretained(this)), |
| 295 use_localhost_requests_, use_smaller_responses_for_tests)); | 220 use_localhost_requests_, use_smaller_responses_for_tests)); |
| 296 | 221 |
| 297 watcher_factory_.reset(new SocketWatcherFactory( | 222 watcher_factory_.reset(new nqe::internal::SocketWatcherFactory( |
| 298 base::ThreadTaskRunnerHandle::Get(), weak_ptr_factory_.GetWeakPtr())); | 223 base::ThreadTaskRunnerHandle::Get(), |
| 224 base::Bind(&NetworkQualityEstimator::OnUpdatedRTTAvailable, | |
| 225 base::Unretained(this)))); | |
| 299 } | 226 } |
| 300 | 227 |
| 301 void NetworkQualityEstimator::ObtainOperatingParams( | 228 void NetworkQualityEstimator::ObtainOperatingParams( |
| 302 const std::map<std::string, std::string>& variation_params) { | 229 const std::map<std::string, std::string>& variation_params) { |
| 303 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
| 304 | 231 |
| 305 for (size_t i = 0; i <= NetworkChangeNotifier::CONNECTION_LAST; ++i) { | 232 for (size_t i = 0; i <= NetworkChangeNotifier::CONNECTION_LAST; ++i) { |
| 306 NetworkChangeNotifier::ConnectionType type = | 233 NetworkChangeNotifier::ConnectionType type = |
| 307 static_cast<NetworkChangeNotifier::ConnectionType>(i); | 234 static_cast<NetworkChangeNotifier::ConnectionType>(i); |
| 308 DCHECK_EQ(nqe::internal::InvalidRTT(), default_observations_[i].http_rtt()); | 235 DCHECK_EQ(nqe::internal::InvalidRTT(), default_observations_[i].http_rtt()); |
| (...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1190 NotifyObserversOfEffectiveConnectionTypeChanged() { | 1117 NotifyObserversOfEffectiveConnectionTypeChanged() { |
| 1191 DCHECK(thread_checker_.CalledOnValidThread()); | 1118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1192 | 1119 |
| 1193 // TODO(tbansal): Add hysteresis in the notification. | 1120 // TODO(tbansal): Add hysteresis in the notification. |
| 1194 FOR_EACH_OBSERVER( | 1121 FOR_EACH_OBSERVER( |
| 1195 EffectiveConnectionTypeObserver, effective_connection_type_observer_list_, | 1122 EffectiveConnectionTypeObserver, effective_connection_type_observer_list_, |
| 1196 OnEffectiveConnectionTypeChanged(effective_connection_type_)); | 1123 OnEffectiveConnectionTypeChanged(effective_connection_type_)); |
| 1197 } | 1124 } |
| 1198 | 1125 |
| 1199 } // namespace net | 1126 } // namespace net |
| OLD | NEW |