Chromium Code Reviews| Index: net/base/network_quality_estimator.h |
| diff --git a/net/base/network_quality_estimator.h b/net/base/network_quality_estimator.h |
| index ecfa08af6b8341b3a9bb06897a89baa42651c7b7..d79276e527f3b15e06e0e9c516a820adfd2f4a28 100644 |
| --- a/net/base/network_quality_estimator.h |
| +++ b/net/base/network_quality_estimator.h |
| @@ -7,16 +7,18 @@ |
| #include <stdint.h> |
| +#include <map> |
| +#include <string> |
| + |
| #include "base/gtest_prod_util.h" |
| #include "base/macros.h" |
| #include "base/threading/thread_checker.h" |
| #include "base/time/time.h" |
| #include "net/base/network_change_notifier.h" |
| +#include "net/base/network_quality.h" |
| namespace net { |
| -struct NetworkQuality; |
| - |
| // NetworkQualityEstimator provides network quality estimates (quality of the |
| // full paths to all origins that have been connected to). |
| // The estimates are based on the observed organic traffic. |
| @@ -43,11 +45,82 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| void NotifyDataReceived(const URLRequest& request, |
| int64_t prefilter_bytes_read); |
| + protected: |
| + // Returns true if the cached network quality estimate was successfully read. |
| + bool ReadCachedNetworkQualityEstimate(); |
| + |
| + // NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| + void OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType type) override; |
| + |
| + // Returns the number of entries in the cache. Used only for testing. |
| + size_t GetCacheSizeForTests() const; |
| + |
| private: |
| FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| TestPeakKbpsFastestRTTUpdates); |
| + FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); |
| + FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| + TestLRUCacheMaximumSize); |
| FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| + // NetworkID is used to uniquely identify a network. |
| + // For the purpose of network quality estimation and caching, a network is |
| + // uniquely identified by a combination of |connection_type_| and |
| + // |network_name_|. This approach is unable to distinguish networks with |
| + // same name (e.g., different Wi-Fi networks with same SSID). |
| + struct NetworkID { |
| + NetworkChangeNotifier::ConnectionType type; |
| + std::string id; |
| + |
| + NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| + : type(type), id(id) {} |
| + |
| + ~NetworkID() {} |
| + |
| + NetworkID& operator=(const NetworkID& other) { |
| + type = other.type; |
| + id = other.id; |
| + return *this; |
| + } |
| + |
| + // Overloaded because NetworkID is used as key in a map. |
| + bool operator<(const NetworkID& other) const { |
| + return type < other.type || (type == other.type && id < other.id); |
| + } |
| + }; |
| + |
| + // CachedNetworkQuality stores the quality of a previously seen network. |
| + class CachedNetworkQuality { |
| + public: |
| + explicit CachedNetworkQuality(const NetworkQuality& network_quality); |
| + |
| + ~CachedNetworkQuality(); |
| + |
| + // Returns the network quality associated with this cached entry. |
| + const NetworkQuality GetNetworkQuality() const; |
| + |
| + // Updates the network quality to the specified |median_kbps| and |
| + // |median_rtt|. |
| + void UpdateNetworkQuality(uint64_t median_kbps, |
| + const base::TimeDelta& median_rtt); |
| + |
| + // Returns the time when this cached entry was last updated. |
| + base::TimeTicks last_update_time() const { return last_update_time_; } |
| + |
| + private: |
| + // Time when this cache entry was last updated. |
| + base::TimeTicks last_update_time_; |
| + |
| + // Quality of this cached network. |
| + NetworkQuality network_quality_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality); |
| + }; |
| + |
| + typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>> |
| + CachedNetworkQualities; |
| + |
| // Tiny transfer sizes may give inaccurate throughput results. |
| // Minimum size of the transfer over which the throughput is computed. |
| static const int kMinTransferSizeInBytes = 10000; |
| @@ -56,6 +129,11 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| // throughput is computed. |
| static const int kMinRequestDurationMicroseconds = 1000; |
| + // Maximum size of the cache that holds network quality estimates. |
| + // Smaller size may reduce the cache hit rate due to frequent evictions. |
| + // Larger size may affect performance. |
| + static const uint32_t kMaximumNetworkQualityCacheSize; |
| + |
| // Construct a NetworkQualityEstimator instance allowing for test |
| // configuration. |
| // Registers for network type change notifications so estimates can be kept |
| @@ -65,9 +143,18 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| // used for network quality estimation. |
| explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); |
| - // NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| - void OnConnectionTypeChanged( |
| - NetworkChangeNotifier::ConnectionType type) override; |
| + // Updates the current network name to: |
| + // WiFi SSID (if the user is connected to a WiFi access point and the SSID |
| + // name is available), or |
| + // the MCC/MNC code of the cellular carrier if the device is connected to a |
| + // cellular network. |
| + // Updates the current network name to an empty string in all other cases or |
| + // if the network name is not exposed by platform APIs. |
| + // Virtualized for testing. |
| + virtual std::string GetCurrentNetworkName() const; |
| + |
| + // Writes the estimated quality of the current network to the cache. |
| + void CacheNetworkQualityEstimate(); |
| // Determines if the requests to local host can be used in estimating the |
| // network quality. Set to true only for tests. |
| @@ -76,10 +163,6 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| // Time when last connection change was observed. |
| base::TimeTicks last_connection_change_; |
| - // Last value passed to |OnConnectionTypeChanged|. This indicates the |
| - // current connection type. |
| - NetworkChangeNotifier::ConnectionType current_connection_type_; |
| - |
| // Set if any network data has been received since last connectivity change. |
| bool bytes_read_since_last_connection_change_; |
| @@ -87,12 +170,18 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| // from URLRequest creation until first byte received. |
| base::TimeDelta fastest_RTT_since_last_connection_change_; |
| + // Cache to store quality of previously seen networks. |
| + CachedNetworkQualities cached_network_quality_; |
|
bengr
2015/06/13 00:28:55
cached_network_qualities_
|
| + |
| // Rough measurement of downlink peak Kbps witnessed since last connectivity |
| // change. The accuracy is decreased by ignoring these factors: |
| // 1) Multiple URLRequests can occur concurrently. |
| // 2) The transfer time includes at least one RTT while no bytes are read. |
| uint64_t peak_kbps_since_last_connection_change_; |
| + // ID of the current network. |
| + NetworkID current_network_id_; |
| + |
| base::ThreadChecker thread_checker_; |
| DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |