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" | |
18 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
20 #include "base/time/default_tick_clock.h" | 19 #include "base/time/default_tick_clock.h" |
21 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
22 #include "build/build_config.h" | 21 #include "build/build_config.h" |
23 #include "net/base/load_flags.h" | 22 #include "net/base/load_flags.h" |
24 #include "net/base/load_timing_info.h" | 23 #include "net/base/load_timing_info.h" |
25 #include "net/base/network_interfaces.h" | 24 #include "net/base/network_interfaces.h" |
26 #include "net/base/url_util.h" | 25 #include "net/base/url_util.h" |
| 26 #include "net/nqe/socket_watcher_factory.h" |
27 #include "net/nqe/throughput_analyzer.h" | 27 #include "net/nqe/throughput_analyzer.h" |
28 #include "net/socket/socket_performance_watcher.h" | |
29 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
30 #include "url/gurl.h" | 29 #include "url/gurl.h" |
31 | 30 |
32 #if defined(OS_ANDROID) | 31 #if defined(OS_ANDROID) |
33 #include "net/android/network_library.h" | 32 #include "net/android/network_library.h" |
34 #endif // OS_ANDROID | 33 #endif // OS_ANDROID |
35 | 34 |
36 namespace { | 35 namespace { |
37 | 36 |
38 // Default value of the half life (in seconds) for computing time weighted | 37 // 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 | 153 |
155 // Returns true if the scheme of the |request| is either HTTP or HTTPS. | 154 // Returns true if the scheme of the |request| is either HTTP or HTTPS. |
156 bool RequestSchemeIsHTTPOrHTTPS(const net::URLRequest& request) { | 155 bool RequestSchemeIsHTTPOrHTTPS(const net::URLRequest& request) { |
157 return request.url().is_valid() && request.url().SchemeIsHTTPOrHTTPS(); | 156 return request.url().is_valid() && request.url().SchemeIsHTTPOrHTTPS(); |
158 } | 157 } |
159 | 158 |
160 } // namespace | 159 } // namespace |
161 | 160 |
162 namespace net { | 161 namespace net { |
163 | 162 |
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( | 163 NetworkQualityEstimator::NetworkQualityEstimator( |
240 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 164 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
241 const std::map<std::string, std::string>& variation_params) | 165 const std::map<std::string, std::string>& variation_params) |
242 : NetworkQualityEstimator(std::move(external_estimates_provider), | 166 : NetworkQualityEstimator(std::move(external_estimates_provider), |
243 variation_params, | 167 variation_params, |
244 false, | 168 false, |
245 false) {} | 169 false) {} |
246 | 170 |
247 NetworkQualityEstimator::NetworkQualityEstimator( | 171 NetworkQualityEstimator::NetworkQualityEstimator( |
248 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 172 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 } | 211 } |
288 current_network_id_ = GetCurrentNetworkID(); | 212 current_network_id_ = GetCurrentNetworkID(); |
289 AddDefaultEstimates(); | 213 AddDefaultEstimates(); |
290 | 214 |
291 throughput_analyzer_.reset(new nqe::internal::ThroughputAnalyzer( | 215 throughput_analyzer_.reset(new nqe::internal::ThroughputAnalyzer( |
292 base::ThreadTaskRunnerHandle::Get(), | 216 base::ThreadTaskRunnerHandle::Get(), |
293 base::Bind(&NetworkQualityEstimator::OnNewThroughputObservationAvailable, | 217 base::Bind(&NetworkQualityEstimator::OnNewThroughputObservationAvailable, |
294 base::Unretained(this)), | 218 base::Unretained(this)), |
295 use_localhost_requests_, use_smaller_responses_for_tests)); | 219 use_localhost_requests_, use_smaller_responses_for_tests)); |
296 | 220 |
297 watcher_factory_.reset(new SocketWatcherFactory( | 221 watcher_factory_.reset(new nqe::internal::SocketWatcherFactory( |
298 base::ThreadTaskRunnerHandle::Get(), weak_ptr_factory_.GetWeakPtr())); | 222 base::ThreadTaskRunnerHandle::Get(), |
| 223 base::Bind(&NetworkQualityEstimator::OnUpdatedRTTAvailable, |
| 224 base::Unretained(this)))); |
299 } | 225 } |
300 | 226 |
301 void NetworkQualityEstimator::ObtainOperatingParams( | 227 void NetworkQualityEstimator::ObtainOperatingParams( |
302 const std::map<std::string, std::string>& variation_params) { | 228 const std::map<std::string, std::string>& variation_params) { |
303 DCHECK(thread_checker_.CalledOnValidThread()); | 229 DCHECK(thread_checker_.CalledOnValidThread()); |
304 | 230 |
305 for (size_t i = 0; i <= NetworkChangeNotifier::CONNECTION_LAST; ++i) { | 231 for (size_t i = 0; i <= NetworkChangeNotifier::CONNECTION_LAST; ++i) { |
306 NetworkChangeNotifier::ConnectionType type = | 232 NetworkChangeNotifier::ConnectionType type = |
307 static_cast<NetworkChangeNotifier::ConnectionType>(i); | 233 static_cast<NetworkChangeNotifier::ConnectionType>(i); |
308 DCHECK_EQ(nqe::internal::InvalidRTT(), default_observations_[i].http_rtt()); | 234 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() { | 1116 NotifyObserversOfEffectiveConnectionTypeChanged() { |
1191 DCHECK(thread_checker_.CalledOnValidThread()); | 1117 DCHECK(thread_checker_.CalledOnValidThread()); |
1192 | 1118 |
1193 // TODO(tbansal): Add hysteresis in the notification. | 1119 // TODO(tbansal): Add hysteresis in the notification. |
1194 FOR_EACH_OBSERVER( | 1120 FOR_EACH_OBSERVER( |
1195 EffectiveConnectionTypeObserver, effective_connection_type_observer_list_, | 1121 EffectiveConnectionTypeObserver, effective_connection_type_observer_list_, |
1196 OnEffectiveConnectionTypeChanged(effective_connection_type_)); | 1122 OnEffectiveConnectionTypeChanged(effective_connection_type_)); |
1197 } | 1123 } |
1198 | 1124 |
1199 } // namespace net | 1125 } // namespace net |
OLD | NEW |