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 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 5 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <deque> | 11 #include <deque> |
12 #include <map> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 #include <tuple> | 14 #include <tuple> |
15 | 15 |
16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/memory/weak_ptr.h" | |
20 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
21 #include "base/threading/thread_checker.h" | 22 #include "base/threading/thread_checker.h" |
22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
23 #include "net/base/external_estimate_provider.h" | 24 #include "net/base/external_estimate_provider.h" |
24 #include "net/base/net_export.h" | 25 #include "net/base/net_export.h" |
25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
26 #include "net/base/socket_performance_watcher.h" | |
27 #include "net/base/socket_performance_watcher_factory.h" | 27 #include "net/base/socket_performance_watcher_factory.h" |
28 | 28 |
29 namespace base { | |
30 class SingleThreadTaskRunner; | |
31 } // namespace base | |
32 | |
29 namespace net { | 33 namespace net { |
30 | 34 |
31 class URLRequest; | 35 class URLRequest; |
32 | 36 |
33 // NetworkQualityEstimator provides network quality estimates (quality of the | 37 // NetworkQualityEstimator provides network quality estimates (quality of the |
34 // full paths to all origins that have been connected to). | 38 // full paths to all origins that have been connected to). |
35 // The estimates are based on the observed organic traffic. | 39 // The estimates are based on the observed organic traffic. |
36 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 40 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
37 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 41 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
38 // A single instance of NQE can be attached to multiple URLRequestContexts, | 42 // A single instance of NQE can be attached to multiple URLRequestContexts, |
39 // thereby increasing the single NQE instance's accuracy by providing more | 43 // thereby increasing the single NQE instance's accuracy by providing more |
40 // observed traffic characteristics. | 44 // observed traffic characteristics. |
41 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 45 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
42 : public NetworkChangeNotifier::ConnectionTypeObserver, | 46 : public NetworkChangeNotifier::ConnectionTypeObserver, |
43 public ExternalEstimateProvider::UpdatedEstimateDelegate, | 47 public ExternalEstimateProvider::UpdatedEstimateDelegate { |
44 public SocketPerformanceWatcherFactory { | |
45 public: | 48 public: |
46 // On Android, a Java counterpart will be generated for this enum. | 49 // On Android, a Java counterpart will be generated for this enum. |
47 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | 50 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
48 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | 51 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource |
49 // GENERATED_JAVA_PREFIX_TO_STRIP: | 52 // GENERATED_JAVA_PREFIX_TO_STRIP: |
50 enum ObservationSource { | 53 enum ObservationSource { |
51 // The observation was taken at the request layer, e.g., a round trip time | 54 // The observation was taken at the request layer, e.g., a round trip time |
52 // is recorded as the time between the request being sent and the first byte | 55 // is recorded as the time between the request being sent and the first byte |
53 // being received. | 56 // being received. |
54 URL_REQUEST, | 57 URL_REQUEST, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 base::TimeDelta* rtt) const; | 164 base::TimeDelta* rtt) const; |
162 | 165 |
163 // Returns true if median downstream throughput is available and sets |kbps| | 166 // Returns true if median downstream throughput is available and sets |kbps| |
164 // to the median of downstream throughput (in kilobits per second) | 167 // to the median of downstream throughput (in kilobits per second) |
165 // observations since |begin_timestamp|. Virtualized for testing. |kbps| | 168 // observations since |begin_timestamp|. Virtualized for testing. |kbps| |
166 // should not be null. | 169 // should not be null. |
167 virtual bool GetRecentMedianDownlinkThroughputKbps( | 170 virtual bool GetRecentMedianDownlinkThroughputKbps( |
168 const base::TimeTicks& begin_timestamp, | 171 const base::TimeTicks& begin_timestamp, |
169 int32_t* kbps) const; | 172 int32_t* kbps) const; |
170 | 173 |
171 // SocketPerformanceWatcherFactory implementation: | |
172 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( | |
173 const Protocol protocol) override; | |
174 void OnUpdatedRTTAvailable(const Protocol protocol, | |
175 const base::TimeDelta& rtt) override; | |
176 | |
177 // Adds |rtt_observer| to the list of round trip time observers. Must be | 174 // Adds |rtt_observer| to the list of round trip time observers. Must be |
178 // called on the IO thread. | 175 // called on the IO thread. |
179 void AddRTTObserver(RTTObserver* rtt_observer); | 176 void AddRTTObserver(RTTObserver* rtt_observer); |
180 | 177 |
181 // Removes |rtt_observer| from the list of round trip time observers if it | 178 // Removes |rtt_observer| from the list of round trip time observers if it |
182 // is on the list of observers. Must be called on the IO thread. | 179 // is on the list of observers. Must be called on the IO thread. |
183 void RemoveRTTObserver(RTTObserver* rtt_observer); | 180 void RemoveRTTObserver(RTTObserver* rtt_observer); |
184 | 181 |
185 // Adds |throughput_observer| to the list of throughput observers. Must be | 182 // Adds |throughput_observer| to the list of throughput observers. Must be |
186 // called on the IO thread. | 183 // called on the IO thread. |
187 void AddThroughputObserver(ThroughputObserver* throughput_observer); | 184 void AddThroughputObserver(ThroughputObserver* throughput_observer); |
188 | 185 |
189 // Removes |throughput_observer| from the list of throughput observers if it | 186 // Removes |throughput_observer| from the list of throughput observers if it |
190 // is on the list of observers. Must be called on the IO thread. | 187 // is on the list of observers. Must be called on the IO thread. |
191 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); | 188 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
192 | 189 |
190 SocketPerformanceWatcherFactory* GetSocketPerformanceWatcherFactory(); | |
191 | |
193 protected: | 192 protected: |
194 // NetworkID is used to uniquely identify a network. | 193 // NetworkID is used to uniquely identify a network. |
195 // For the purpose of network quality estimation and caching, a network is | 194 // For the purpose of network quality estimation and caching, a network is |
196 // uniquely identified by a combination of |type| and | 195 // uniquely identified by a combination of |type| and |
197 // |id|. This approach is unable to distinguish networks with | 196 // |id|. This approach is unable to distinguish networks with |
198 // same name (e.g., different Wi-Fi networks with same SSID). | 197 // same name (e.g., different Wi-Fi networks with same SSID). |
199 // This is a protected member to expose it to tests. | 198 // This is a protected member to expose it to tests. |
200 struct NET_EXPORT_PRIVATE NetworkID { | 199 struct NET_EXPORT_PRIVATE NetworkID { |
201 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 200 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
202 : type(type), id(id) {} | 201 : type(type), id(id) {} |
(...skipping 29 matching lines...) Expand all Loading... | |
232 bool ReadCachedNetworkQualityEstimate(); | 231 bool ReadCachedNetworkQualityEstimate(); |
233 | 232 |
234 // NetworkChangeNotifier::ConnectionTypeObserver implementation: | 233 // NetworkChangeNotifier::ConnectionTypeObserver implementation: |
235 void OnConnectionTypeChanged( | 234 void OnConnectionTypeChanged( |
236 NetworkChangeNotifier::ConnectionType type) override; | 235 NetworkChangeNotifier::ConnectionType type) override; |
237 | 236 |
238 // ExternalEstimateProvider::UpdatedEstimateObserver implementation. | 237 // ExternalEstimateProvider::UpdatedEstimateObserver implementation. |
239 void OnUpdatedEstimateAvailable() override; | 238 void OnUpdatedEstimateAvailable() override; |
240 | 239 |
241 private: | 240 private: |
241 class SocketWatcher; | |
242 class SocketWatcherDelegate; | |
243 class SocketWatcherFactory; | |
Ryan Sleevi
2016/04/04 17:30:09
If this is because you need to access internal met
tbansal1
2016/04/04 18:35:46
It was for forward declaration. Moved it to after
| |
242 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | 244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates); | 245 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates); |
244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); | 246 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); |
245 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams); | 247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams); |
246 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam); | 248 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam); |
247 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 249 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
248 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 250 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
249 PercentileSameTimestamps); | 251 PercentileSameTimestamps); |
250 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 252 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
251 PercentileDifferentTimestamps); | 253 PercentileDifferentTimestamps); |
252 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); | 254 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); |
253 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); | 255 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); |
254 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 256 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
255 TestLRUCacheMaximumSize); | 257 TestLRUCacheMaximumSize); |
256 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); | 258 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); |
257 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 259 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
258 TestExternalEstimateProvider); | 260 TestExternalEstimateProvider); |
259 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 261 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
260 TestExternalEstimateProviderMergeEstimates); | 262 TestExternalEstimateProviderMergeEstimates); |
261 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers); | |
262 | 263 |
263 // NetworkQuality is used to cache the quality of a network connection. | 264 // NetworkQuality is used to cache the quality of a network connection. |
264 class NET_EXPORT_PRIVATE NetworkQuality { | 265 class NET_EXPORT_PRIVATE NetworkQuality { |
265 public: | 266 public: |
266 NetworkQuality(); | 267 NetworkQuality(); |
267 // |rtt| is the estimate of the round trip time. | 268 // |rtt| is the estimate of the round trip time. |
268 // |downstream_throughput_kbps| is the estimate of the downstream | 269 // |downstream_throughput_kbps| is the estimate of the downstream |
269 // throughput in kilobits per second. | 270 // throughput in kilobits per second. |
270 NetworkQuality(const base::TimeDelta& rtt, | 271 NetworkQuality(const base::TimeDelta& rtt, |
271 int32_t downstream_throughput_kbps); | 272 int32_t downstream_throughput_kbps); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 | 492 |
492 // Time duration (in milliseconds) after which the estimate provided by | 493 // Time duration (in milliseconds) after which the estimate provided by |
493 // external estimate provider is considered stale. | 494 // external estimate provider is considered stale. |
494 static const int kExternalEstimateProviderFreshnessDurationMsec = | 495 static const int kExternalEstimateProviderFreshnessDurationMsec = |
495 5 * 60 * 1000; | 496 5 * 60 * 1000; |
496 | 497 |
497 // Returns the RTT value to be used when the valid RTT is unavailable. Readers | 498 // Returns the RTT value to be used when the valid RTT is unavailable. Readers |
498 // should discard RTT if it is set to the value returned by |InvalidRTT()|. | 499 // should discard RTT if it is set to the value returned by |InvalidRTT()|. |
499 static const base::TimeDelta InvalidRTT(); | 500 static const base::TimeDelta InvalidRTT(); |
500 | 501 |
502 // Notifies |this| of a new transport layer RTT. | |
503 void OnUpdatedRTTAvailable(SocketPerformanceWatcherFactory::Protocol protocol, | |
504 const base::TimeDelta& rtt); | |
505 | |
501 // Queries the external estimate provider for the latest network quality | 506 // Queries the external estimate provider for the latest network quality |
502 // estimates, and adds those estimates to the current observation buffer. | 507 // estimates, and adds those estimates to the current observation buffer. |
503 void QueryExternalEstimateProvider(); | 508 void QueryExternalEstimateProvider(); |
504 | 509 |
505 // Obtains operating parameters from the field trial parameters. | 510 // Obtains operating parameters from the field trial parameters. |
506 void ObtainOperatingParams( | 511 void ObtainOperatingParams( |
507 const std::map<std::string, std::string>& variation_params); | 512 const std::map<std::string, std::string>& variation_params); |
508 | 513 |
509 // Adds the default median RTT and downstream throughput estimate for the | 514 // Adds the default median RTT and downstream throughput estimate for the |
510 // current connection type to the observation buffer. | 515 // current connection type to the observation buffer. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
603 NetworkQuality estimated_median_network_quality_; | 608 NetworkQuality estimated_median_network_quality_; |
604 | 609 |
605 // ExternalEstimateProvider that provides network quality using operating | 610 // ExternalEstimateProvider that provides network quality using operating |
606 // system APIs. May be NULL. | 611 // system APIs. May be NULL. |
607 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; | 612 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
608 | 613 |
609 // Observer lists for round trip times and throughput measurements. | 614 // Observer lists for round trip times and throughput measurements. |
610 base::ObserverList<RTTObserver> rtt_observer_list_; | 615 base::ObserverList<RTTObserver> rtt_observer_list_; |
611 base::ObserverList<ThroughputObserver> throughput_observer_list_; | 616 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
612 | 617 |
618 scoped_ptr<SocketPerformanceWatcherFactory> watcher_factory_; | |
619 | |
620 scoped_ptr<SocketWatcherDelegate> delegate_; | |
621 | |
613 base::ThreadChecker thread_checker_; | 622 base::ThreadChecker thread_checker_; |
614 | 623 |
624 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_; | |
625 | |
615 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 626 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
616 }; | 627 }; |
617 | 628 |
618 } // namespace net | 629 } // namespace net |
619 | 630 |
620 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 631 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |