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.h" |
27 #include "net/base/socket_performance_watcher_factory.h" | 28 #include "net/base/socket_performance_watcher_factory.h" |
28 | 29 |
29 namespace net { | 30 namespace net { |
30 | 31 |
32 class NetworkQualityEstimatorWatcherFactory; | |
31 class URLRequest; | 33 class URLRequest; |
32 | 34 |
33 // NetworkQualityEstimator provides network quality estimates (quality of the | 35 // NetworkQualityEstimator provides network quality estimates (quality of the |
34 // full paths to all origins that have been connected to). | 36 // full paths to all origins that have been connected to). |
35 // The estimates are based on the observed organic traffic. | 37 // The estimates are based on the observed organic traffic. |
36 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 38 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
37 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 39 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
38 // A single instance of NQE can be attached to multiple URLRequestContexts, | 40 // A single instance of NQE can be attached to multiple URLRequestContexts, |
39 // thereby increasing the single NQE instance's accuracy by providing more | 41 // thereby increasing the single NQE instance's accuracy by providing more |
40 // observed traffic characteristics. | 42 // observed traffic characteristics. |
41 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 43 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
42 : public NetworkChangeNotifier::ConnectionTypeObserver, | 44 : public NetworkChangeNotifier::ConnectionTypeObserver, |
43 public ExternalEstimateProvider::UpdatedEstimateDelegate, | 45 public ExternalEstimateProvider::UpdatedEstimateDelegate { |
44 public SocketPerformanceWatcherFactory { | |
45 public: | 46 public: |
46 // On Android, a Java counterpart will be generated for this enum. | 47 // On Android, a Java counterpart will be generated for this enum. |
47 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | 48 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
48 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | 49 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource |
49 // GENERATED_JAVA_PREFIX_TO_STRIP: | 50 // GENERATED_JAVA_PREFIX_TO_STRIP: |
50 enum ObservationSource { | 51 enum ObservationSource { |
51 // The observation was taken at the request layer, e.g., a round trip time | 52 // 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 | 53 // is recorded as the time between the request being sent and the first byte |
53 // being received. | 54 // being received. |
54 URL_REQUEST, | 55 URL_REQUEST, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 base::TimeDelta* rtt) const; | 155 base::TimeDelta* rtt) const; |
155 | 156 |
156 // Returns true if median downstream throughput is available and sets |kbps| | 157 // Returns true if median downstream throughput is available and sets |kbps| |
157 // to the median of downstream throughput (in kilobits per second) | 158 // to the median of downstream throughput (in kilobits per second) |
158 // observations since |begin_timestamp|. Virtualized for testing. |kbps| | 159 // observations since |begin_timestamp|. Virtualized for testing. |kbps| |
159 // should not be null. | 160 // should not be null. |
160 virtual bool GetRecentMedianDownlinkThroughputKbps( | 161 virtual bool GetRecentMedianDownlinkThroughputKbps( |
161 const base::TimeTicks& begin_timestamp, | 162 const base::TimeTicks& begin_timestamp, |
162 int32_t* kbps) const; | 163 int32_t* kbps) const; |
163 | 164 |
164 // SocketPerformanceWatcherFactory implementation: | |
165 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( | |
166 const Protocol protocol) override; | |
167 void OnUpdatedRTTAvailable(const Protocol protocol, | |
168 const base::TimeDelta& rtt) override; | |
169 | |
170 // Adds |rtt_observer| to the list of round trip time observers. Must be | 165 // Adds |rtt_observer| to the list of round trip time observers. Must be |
171 // called on the IO thread. | 166 // called on the IO thread. |
172 void AddRTTObserver(RTTObserver* rtt_observer); | 167 void AddRTTObserver(RTTObserver* rtt_observer); |
173 | 168 |
174 // Removes |rtt_observer| from the list of round trip time observers if it | 169 // Removes |rtt_observer| from the list of round trip time observers if it |
175 // is on the list of observers. Must be called on the IO thread. | 170 // is on the list of observers. Must be called on the IO thread. |
176 void RemoveRTTObserver(RTTObserver* rtt_observer); | 171 void RemoveRTTObserver(RTTObserver* rtt_observer); |
177 | 172 |
178 // Adds |throughput_observer| to the list of throughput observers. Must be | 173 // Adds |throughput_observer| to the list of throughput observers. Must be |
179 // called on the IO thread. | 174 // called on the IO thread. |
180 void AddThroughputObserver(ThroughputObserver* throughput_observer); | 175 void AddThroughputObserver(ThroughputObserver* throughput_observer); |
181 | 176 |
182 // Removes |throughput_observer| from the list of throughput observers if it | 177 // Removes |throughput_observer| from the list of throughput observers if it |
183 // is on the list of observers. Must be called on the IO thread. | 178 // is on the list of observers. Must be called on the IO thread. |
184 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); | 179 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
185 | 180 |
181 // Virtualized for testing. | |
182 virtual SocketPerformanceWatcherFactory* GetSocketPerformanceWatcherFactory(); | |
183 | |
186 protected: | 184 protected: |
187 // NetworkID is used to uniquely identify a network. | 185 // NetworkID is used to uniquely identify a network. |
188 // For the purpose of network quality estimation and caching, a network is | 186 // For the purpose of network quality estimation and caching, a network is |
189 // uniquely identified by a combination of |type| and | 187 // uniquely identified by a combination of |type| and |
190 // |id|. This approach is unable to distinguish networks with | 188 // |id|. This approach is unable to distinguish networks with |
191 // same name (e.g., different Wi-Fi networks with same SSID). | 189 // same name (e.g., different Wi-Fi networks with same SSID). |
192 // This is a protected member to expose it to tests. | 190 // This is a protected member to expose it to tests. |
193 struct NET_EXPORT_PRIVATE NetworkID { | 191 struct NET_EXPORT_PRIVATE NetworkID { |
194 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 192 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
195 : type(type), id(id) {} | 193 : type(type), id(id) {} |
(...skipping 28 matching lines...) Expand all Loading... | |
224 // Returns true if the cached network quality estimate was successfully read. | 222 // Returns true if the cached network quality estimate was successfully read. |
225 bool ReadCachedNetworkQualityEstimate(); | 223 bool ReadCachedNetworkQualityEstimate(); |
226 | 224 |
227 // NetworkChangeNotifier::ConnectionTypeObserver implementation: | 225 // NetworkChangeNotifier::ConnectionTypeObserver implementation: |
228 void OnConnectionTypeChanged( | 226 void OnConnectionTypeChanged( |
229 NetworkChangeNotifier::ConnectionType type) override; | 227 NetworkChangeNotifier::ConnectionType type) override; |
230 | 228 |
231 // ExternalEstimateProvider::UpdatedEstimateObserver implementation. | 229 // ExternalEstimateProvider::UpdatedEstimateObserver implementation. |
232 void OnUpdatedEstimateAvailable() override; | 230 void OnUpdatedEstimateAvailable() override; |
233 | 231 |
232 // Called by |socket_performance_watcher_factory_| every time it receives a | |
233 // notification from a socket performance watcher. | |
Ryan Sleevi
2016/03/25 01:45:34
This comment is leaking the abstraction details in
| |
234 void OnUpdatedRTTAvailable(SocketPerformanceWatcherFactory::Protocol protocol, | |
235 const base::TimeDelta& rtt); | |
236 void OnWatcherReset(); | |
237 | |
234 private: | 238 private: |
239 // Allows |NetworkQualityEstimatorWatcherFactory| to call protected functions. | |
Ryan Sleevi
2016/03/25 01:45:34
You can drop this comment. Per https://google.gith
| |
240 friend class NetworkQualityEstimatorWatcherFactory; | |
235 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | 241 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
236 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates); | 242 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates); |
237 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); | 243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); |
238 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams); | 244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams); |
239 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam); | 245 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam); |
240 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 246 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
241 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
242 PercentileSameTimestamps); | 248 PercentileSameTimestamps); |
243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 249 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
244 PercentileDifferentTimestamps); | 250 PercentileDifferentTimestamps); |
245 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); | 251 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); |
246 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); | 252 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); |
247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 253 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
248 TestLRUCacheMaximumSize); | 254 TestLRUCacheMaximumSize); |
249 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); | 255 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); |
250 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 256 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
251 TestExternalEstimateProvider); | 257 TestExternalEstimateProvider); |
252 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 258 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
253 TestExternalEstimateProviderMergeEstimates); | 259 TestExternalEstimateProviderMergeEstimates); |
254 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers); | 260 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers); |
261 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestTcpSocketRtt); | |
255 | 262 |
256 // NetworkQuality is used to cache the quality of a network connection. | 263 // NetworkQuality is used to cache the quality of a network connection. |
257 class NET_EXPORT_PRIVATE NetworkQuality { | 264 class NET_EXPORT_PRIVATE NetworkQuality { |
258 public: | 265 public: |
259 NetworkQuality(); | 266 NetworkQuality(); |
260 // |rtt| is the estimate of the round trip time. | 267 // |rtt| is the estimate of the round trip time. |
261 // |downstream_throughput_kbps| is the estimate of the downstream | 268 // |downstream_throughput_kbps| is the estimate of the downstream |
262 // throughput in kilobits per second. | 269 // throughput in kilobits per second. |
263 NetworkQuality(const base::TimeDelta& rtt, | 270 NetworkQuality(const base::TimeDelta& rtt, |
264 int32_t downstream_throughput_kbps); | 271 int32_t downstream_throughput_kbps); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 // Virtualized for testing. | 520 // Virtualized for testing. |
514 virtual NetworkID GetCurrentNetworkID() const; | 521 virtual NetworkID GetCurrentNetworkID() const; |
515 | 522 |
516 // Writes the estimated quality of the current network to the cache. | 523 // Writes the estimated quality of the current network to the cache. |
517 void CacheNetworkQualityEstimate(); | 524 void CacheNetworkQualityEstimate(); |
518 | 525 |
519 void NotifyObserversOfRTT(const RttObservation& observation); | 526 void NotifyObserversOfRTT(const RttObservation& observation); |
520 | 527 |
521 void NotifyObserversOfThroughput(const ThroughputObservation& observation); | 528 void NotifyObserversOfThroughput(const ThroughputObservation& observation); |
522 | 529 |
530 // For use by SocketPerformanceWatcher. | |
531 base::WeakPtr<NetworkQualityEstimator> GetWeakPtr(); | |
532 | |
523 // Records the UMA related to RTT. | 533 // Records the UMA related to RTT. |
524 void RecordRTTUMA(int32_t estimated_value_msec, | 534 void RecordRTTUMA(int32_t estimated_value_msec, |
525 int32_t actual_value_msec) const; | 535 int32_t actual_value_msec) const; |
526 | 536 |
527 // Returns true only if |request| can be used for network quality estimation. | 537 // Returns true only if |request| can be used for network quality estimation. |
528 // Only the requests that go over network are considered to provide useful | 538 // Only the requests that go over network are considered to provide useful |
529 // observations. | 539 // observations. |
530 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 540 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
531 | 541 |
532 // Values of external estimate provider status. This enum must remain | 542 // Values of external estimate provider status. This enum must remain |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
588 NetworkQuality estimated_median_network_quality_; | 598 NetworkQuality estimated_median_network_quality_; |
589 | 599 |
590 // ExternalEstimateProvider that provides network quality using operating | 600 // ExternalEstimateProvider that provides network quality using operating |
591 // system APIs. May be NULL. | 601 // system APIs. May be NULL. |
592 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; | 602 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
593 | 603 |
594 // Observer lists for round trip times and throughput measurements. | 604 // Observer lists for round trip times and throughput measurements. |
595 base::ObserverList<RTTObserver> rtt_observer_list_; | 605 base::ObserverList<RTTObserver> rtt_observer_list_; |
596 base::ObserverList<ThroughputObserver> throughput_observer_list_; | 606 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
597 | 607 |
608 scoped_ptr<SocketPerformanceWatcherFactory> | |
609 socket_performance_watcher_factory_; | |
610 | |
598 base::ThreadChecker thread_checker_; | 611 base::ThreadChecker thread_checker_; |
599 | 612 |
613 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_; | |
614 | |
600 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 615 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
601 }; | 616 }; |
602 | 617 |
603 } // namespace net | 618 } // namespace net |
604 | 619 |
605 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 620 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |