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 #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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 10 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 14 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 15 | 18 |
| 16 namespace net { | 19 namespace net { |
| 17 | 20 |
| 18 struct NetworkQuality; | 21 struct NetworkQuality; |
| 19 | 22 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 36 // Returns an estimate of the current network quality. | 39 // Returns an estimate of the current network quality. |
| 37 NetworkQuality GetEstimate() const; | 40 NetworkQuality GetEstimate() const; |
| 38 | 41 |
| 39 // Notifies NetworkQualityEstimator that a response has been received. | 42 // Notifies NetworkQualityEstimator that a response has been received. |
| 40 // |prefilter_bytes_read| is the count of the bytes received prior to | 43 // |prefilter_bytes_read| is the count of the bytes received prior to |
| 41 // applying filters (e.g. decompression, SDCH) from request creation time | 44 // applying filters (e.g. decompression, SDCH) from request creation time |
| 42 // until now. | 45 // until now. |
| 43 void NotifyDataReceived(const URLRequest& request, | 46 void NotifyDataReceived(const URLRequest& request, |
| 44 int64_t prefilter_bytes_read); | 47 int64_t prefilter_bytes_read); |
| 45 | 48 |
| 49 protected: | |
| 50 // Returns true if cached network quality estimate was successfully read. | |
|
bengr
2015/06/01 20:39:35
"if" -> "if the"
tbansal1
2015/06/01 21:56:51
Done.
| |
| 51 bool ReadCachedNetworkQualityEstimate(); | |
|
bengr
2015/06/01 20:39:34
You might want to bite the bullet now and make thi
tbansal1
2015/06/01 21:56:51
This CL is complete in the current form.
I would p
bengr
2015/06/01 23:21:10
Acknowledged.
tbansal1
2015/06/02 18:18:05
Done.
| |
| 52 | |
| 53 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 54 void OnConnectionTypeChanged( | |
| 55 NetworkChangeNotifier::ConnectionType type) override; | |
| 56 | |
| 57 // Set the current network name for testing. | |
| 58 void SetCurrentNetworkNameForTests(const std::string& network_name); | |
| 59 | |
| 60 // Returns the number of entries in the cache. Used only for testing. | |
| 61 uint32_t GetCacheSizeForTests() const; | |
| 62 | |
| 63 // Updates the current network name to: | |
| 64 // WiFi SSID (if the user is connected to a WiFi access point and the SSID | |
| 65 // name is available), or | |
| 66 // The MCC/MNC code of the cellular carrier if the device is connected to a | |
| 67 // cellular network, or | |
| 68 // "ethernet" if the device is connected to an Ethernet network. | |
| 69 // Updates the current network name to an empty string in all other cases or | |
| 70 // if the network name is not exposed by platform APIs. | |
| 71 // Virtualized for testing. | |
| 72 virtual void UpdateCurrentNetworkName(); | |
| 73 | |
| 46 private: | 74 private: |
| 47 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 75 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 48 TestPeakKbpsFastestRTTUpdates); | 76 TestPeakKbpsFastestRTTUpdates); |
| 49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 77 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 50 | 78 |
| 79 // CachedNetworkQuality stores the quality of a previously seen network. | |
| 80 // A network is uniquely identified by combination of |connection_type_| and | |
| 81 // |network_name_|. | |
| 82 class CachedNetworkQuality { | |
| 83 public: | |
| 84 CachedNetworkQuality(NetworkChangeNotifier::ConnectionType connection_type, | |
| 85 const std::string& network_name, | |
| 86 int median_kbps, | |
| 87 const base::TimeDelta& median_rtt); | |
| 88 | |
| 89 virtual ~CachedNetworkQuality(); | |
| 90 | |
| 91 void UpdateNetworkQuality(int median_kbps, | |
| 92 const base::TimeDelta& median_rtt); | |
| 93 | |
| 94 bool MatchesNetwork(NetworkChangeNotifier::ConnectionType connection_type, | |
| 95 const std::string& network_name) const; | |
| 96 | |
| 97 // Median Kbps of this cached network. | |
| 98 int median_kbps_; | |
| 99 | |
| 100 // Median RTT of this cached network. | |
| 101 base::TimeDelta median_rtt_; | |
| 102 | |
| 103 // Time when this cache entry was last updated. | |
| 104 base::TimeTicks last_updated_; | |
| 105 | |
| 106 private: | |
| 107 // Connection type of this cached network. | |
| 108 NetworkChangeNotifier::ConnectionType connection_type_; | |
| 109 | |
| 110 // Name of this cached network. | |
| 111 std::string network_name_; | |
| 112 }; | |
| 113 | |
| 51 // Tiny transfer sizes may give inaccurate throughput results. | 114 // Tiny transfer sizes may give inaccurate throughput results. |
| 52 // Minimum size of the transfer over which the throughput is computed. | 115 // Minimum size of the transfer over which the throughput is computed. |
| 53 static const int kMinTransferSizeInBytes = 10000; | 116 static const int kMinTransferSizeInBytes = 10000; |
| 54 | 117 |
| 55 // Minimum duration (in microseconds) of the transfer over which the | 118 // Minimum duration (in microseconds) of the transfer over which the |
| 56 // throughput is computed. | 119 // throughput is computed. |
| 57 static const int kMinRequestDurationMicroseconds = 1000; | 120 static const int kMinRequestDurationMicroseconds = 1000; |
| 58 | 121 |
| 59 // Construct a NetworkQualityEstimator instance allowing for test | 122 // Construct a NetworkQualityEstimator instance allowing for test |
| 60 // configuration. | 123 // configuration. |
| 61 // Registers for network type change notifications so estimates can be kept | 124 // Registers for network type change notifications so estimates can be kept |
| 62 // network specific. | 125 // network specific. |
| 63 // |allow_local_host_requests_for_tests| should only be true when testing | 126 // |allow_local_host_requests_for_tests| should only be true when testing |
| 64 // against local HTTP server and allows the requests to local host to be | 127 // against local HTTP server and allows the requests to local host to be |
| 65 // used for network quality estimation. | 128 // used for network quality estimation. |
| 66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); | 129 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); |
| 67 | 130 |
| 68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 131 // Write the estimated quality of the current network to the cache. |
| 69 void OnConnectionTypeChanged( | 132 void CacheNetworkQualityEstimate(); |
| 70 NetworkChangeNotifier::ConnectionType type) override; | |
| 71 | 133 |
| 72 // Determines if the requests to local host can be used in estimating the | 134 // Determines if the requests to local host can be used in estimating the |
| 73 // network quality. Set to true only for tests. | 135 // network quality. Set to true only for tests. |
| 74 const bool allow_localhost_requests_; | 136 const bool allow_localhost_requests_; |
| 75 | 137 |
| 76 // Time when last connection change was observed. | 138 // Time when last connection change was observed. |
| 77 base::TimeTicks last_connection_change_; | 139 base::TimeTicks last_connection_change_; |
| 78 | 140 |
| 79 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 141 // Last value passed to |OnConnectionTypeChanged|. This indicates the |
| 80 // current connection type. | 142 // current connection type. |
| 81 NetworkChangeNotifier::ConnectionType current_connection_type_; | 143 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 82 | 144 |
| 83 // Set if any network data has been received since last connectivity change. | 145 // Set if any network data has been received since last connectivity change. |
| 84 bool bytes_read_since_last_connection_change_; | 146 bool bytes_read_since_last_connection_change_; |
| 85 | 147 |
| 86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 148 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 87 // from URLRequest creation until first byte received. | 149 // from URLRequest creation until first byte received. |
| 88 base::TimeDelta fastest_RTT_since_last_connection_change_; | 150 base::TimeDelta fastest_RTT_since_last_connection_change_; |
| 89 | 151 |
| 152 // Cache to store quality of previously seen networks. | |
| 153 std::vector<CachedNetworkQuality> cached_network_quality_; | |
| 154 | |
| 90 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 155 // Rough measurement of downlink peak Kbps witnessed since last connectivity |
| 91 // change. The accuracy is decreased by ignoring these factors: | 156 // change. The accuracy is decreased by ignoring these factors: |
| 92 // 1) Multiple URLRequests can occur concurrently. | 157 // 1) Multiple URLRequests can occur concurrently. |
| 93 // 2) The transfer time includes at least one RTT while no bytes are read. | 158 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 94 uint64_t peak_kbps_since_last_connection_change_; | 159 uint64_t peak_kbps_since_last_connection_change_; |
| 95 | 160 |
| 161 // Name of the current network. | |
| 162 std::string current_network_name_; | |
| 163 | |
| 96 base::ThreadChecker thread_checker_; | 164 base::ThreadChecker thread_checker_; |
| 97 | 165 |
| 98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 166 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 99 }; | 167 }; |
| 100 | 168 |
| 101 } // namespace net | 169 } // namespace net |
| 102 | 170 |
| 103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 171 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |