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. | |
| 51 bool ReadCachedNetworkQualityEstimate(); | |
| 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(std::string network_name); | |
|
bengr
2015/05/29 17:38:10
const std::string&
tbansal1
2015/05/29 19:13:36
Done.
| |
| 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 MCCMNC code of the cellular carrier if the device is connected to a | |
|
bengr
2015/05/29 17:38:10
nit: MCC/MNC
tbansal1
2015/05/29 19:13:36
Done.
| |
| 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 std::string network_name, | |
|
bengr
2015/05/29 17:38:10
const std::string&
tbansal1
2015/05/29 19:13:36
Done.
| |
| 86 int median_kbps, | |
| 87 int median_rtt_milliseconds) | |
| 88 : median_kbps_(median_kbps), | |
| 89 median_rtt_milliseconds_(median_rtt_milliseconds), | |
| 90 last_updated_(base::TimeTicks::Now()), | |
| 91 connection_type_(connection_type), | |
| 92 network_name_(network_name) { | |
| 93 DCHECK_NE(network_name_, std::string()); | |
|
bengr
2015/05/29 17:38:10
After reading the comment for UpdateCurrentNetwork
tbansal1
2015/05/29 19:13:37
The current network name may be empty but in that
| |
| 94 } | |
| 95 | |
| 96 virtual ~CachedNetworkQuality() {} | |
| 97 | |
| 98 void UpdateNetworkQuality(int updated_median_kbps, | |
| 99 int updated_median_rtt_milliseconds) { | |
| 100 median_kbps_ = updated_median_kbps; | |
| 101 median_rtt_milliseconds_ = updated_median_rtt_milliseconds; | |
|
bengr
2015/05/29 17:38:10
Why are these not TimeDeltas?
tbansal1
2015/05/29 19:13:36
Initially, I did not make it TimeDelta because if
| |
| 102 last_updated_ = base::TimeTicks::Now(); | |
| 103 } | |
| 104 | |
| 105 bool MatchesNetwork(NetworkChangeNotifier::ConnectionType connection_type, | |
| 106 std::string network_name) const { | |
|
bengr
2015/05/29 17:38:10
const std::string&
tbansal1
2015/05/29 19:13:36
Done.
| |
| 107 return connection_type == connection_type_ && | |
| 108 network_name == network_name_; | |
| 109 } | |
| 110 | |
| 111 // Median Kbps of this cached network. | |
| 112 int median_kbps_; | |
| 113 | |
| 114 // Median RTT (in milliseconds) of this cached network. | |
| 115 int median_rtt_milliseconds_; | |
|
bengr
2015/05/29 17:38:10
TimeDelta?
tbansal1
2015/05/29 19:13:36
Done.
| |
| 116 | |
| 117 // Time when this cache entry was last updated. | |
| 118 base::TimeTicks last_updated_; | |
| 119 | |
| 120 private: | |
| 121 // Connection type of this cached network. | |
| 122 NetworkChangeNotifier::ConnectionType connection_type_; | |
| 123 | |
| 124 // Name of this cached network. | |
| 125 std::string network_name_; | |
| 126 }; | |
| 127 | |
| 51 // Tiny transfer sizes may give inaccurate throughput results. | 128 // Tiny transfer sizes may give inaccurate throughput results. |
| 52 // Minimum size of the transfer over which the throughput is computed. | 129 // Minimum size of the transfer over which the throughput is computed. |
| 53 static const int kMinTransferSizeInBytes = 10000; | 130 static const int kMinTransferSizeInBytes = 10000; |
| 54 | 131 |
| 55 // Minimum duration (in microseconds) of the transfer over which the | 132 // Minimum duration (in microseconds) of the transfer over which the |
| 56 // throughput is computed. | 133 // throughput is computed. |
| 57 static const int kMinRequestDurationMicroseconds = 1000; | 134 static const int kMinRequestDurationMicroseconds = 1000; |
| 58 | 135 |
| 59 // Construct a NetworkQualityEstimator instance allowing for test | 136 // Construct a NetworkQualityEstimator instance allowing for test |
| 60 // configuration. | 137 // configuration. |
| 61 // Registers for network type change notifications so estimates can be kept | 138 // Registers for network type change notifications so estimates can be kept |
| 62 // network specific. | 139 // network specific. |
| 63 // |allow_local_host_requests_for_tests| should only be true when testing | 140 // |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 | 141 // against local HTTP server and allows the requests to local host to be |
| 65 // used for network quality estimation. | 142 // used for network quality estimation. |
| 66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); | 143 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); |
| 67 | 144 |
| 68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 145 // Write the estimated quality of the current network to the cache. |
| 69 void OnConnectionTypeChanged( | 146 void CacheNetworkQualityEstimate(); |
| 70 NetworkChangeNotifier::ConnectionType type) override; | |
| 71 | 147 |
| 72 // Determines if the requests to local host can be used in estimating the | 148 // Determines if the requests to local host can be used in estimating the |
| 73 // network quality. Set to true only for tests. | 149 // network quality. Set to true only for tests. |
| 74 const bool allow_localhost_requests_; | 150 const bool allow_localhost_requests_; |
| 75 | 151 |
| 76 // Time when last connection change was observed. | 152 // Time when last connection change was observed. |
| 77 base::TimeTicks last_connection_change_; | 153 base::TimeTicks last_connection_change_; |
| 78 | 154 |
| 79 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 155 // Last value passed to |OnConnectionTypeChanged|. This indicates the |
| 80 // current connection type. | 156 // current connection type. |
| 81 NetworkChangeNotifier::ConnectionType current_connection_type_; | 157 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 82 | 158 |
| 83 // Set if any network data has been received since last connectivity change. | 159 // Set if any network data has been received since last connectivity change. |
| 84 bool bytes_read_since_last_connection_change_; | 160 bool bytes_read_since_last_connection_change_; |
| 85 | 161 |
| 86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 162 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 87 // from URLRequest creation until first byte received. | 163 // from URLRequest creation until first byte received. |
| 88 base::TimeDelta fastest_RTT_since_last_connection_change_; | 164 base::TimeDelta fastest_RTT_since_last_connection_change_; |
| 89 | 165 |
| 166 // Cache to store quality of previously seen networks. | |
| 167 std::vector<CachedNetworkQuality> cached_network_quality_; | |
| 168 | |
| 90 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 169 // Rough measurement of downlink peak Kbps witnessed since last connectivity |
| 91 // change. The accuracy is decreased by ignoring these factors: | 170 // change. The accuracy is decreased by ignoring these factors: |
| 92 // 1) Multiple URLRequests can occur concurrently. | 171 // 1) Multiple URLRequests can occur concurrently. |
| 93 // 2) The transfer time includes at least one RTT while no bytes are read. | 172 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 94 uint64_t peak_kbps_since_last_connection_change_; | 173 uint64_t peak_kbps_since_last_connection_change_; |
| 95 | 174 |
| 175 // Name of the current network. | |
| 176 std::string current_network_name_; | |
| 177 | |
| 96 base::ThreadChecker thread_checker_; | 178 base::ThreadChecker thread_checker_; |
| 97 | 179 |
| 98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 180 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 99 }; | 181 }; |
| 100 | 182 |
| 101 } // namespace net | 183 } // namespace net |
| 102 | 184 |
| 103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 185 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |