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 <deque> | 10 #include <deque> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/observer_list.h" | |
| 17 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 18 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 19 #include "net/base/net_export.h" | 21 #include "net/base/net_export.h" |
| 20 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" |
| 21 #include "net/base/network_quality.h" | 23 #include "net/base/network_quality.h" |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 | 26 |
| 25 // NetworkQualityEstimator provides network quality estimates (quality of the | 27 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 26 // full paths to all origins that have been connected to). | 28 // full paths to all origins that have been connected to). |
| 27 // The estimates are based on the observed organic traffic. | 29 // The estimates are based on the observed organic traffic. |
| 28 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 30 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 29 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 31 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 30 // A single instance of NQE can be attached to multiple URLRequestContexts, | 32 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 31 // thereby increasing the single NQE instance's accuracy by providing more | 33 // thereby increasing the single NQE instance's accuracy by providing more |
| 32 // observed traffic characteristics. | 34 // observed traffic characteristics. |
| 33 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 35 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 34 : public NetworkChangeNotifier::ConnectionTypeObserver { | 36 : public NetworkChangeNotifier::ConnectionTypeObserver { |
| 35 public: | 37 public: |
| 38 enum ObservationType { DOWNLINK_BANDWIDTH, RTT }; | |
| 39 | |
| 40 // On Android, a Java counterpart will be generated for this enum. | |
| 41 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | |
| 42 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | |
| 43 // GENERATED_JAVA_PREFIX_TO_STRIP: OBSERVATION_SOURCE_ | |
| 44 enum ObservationSource { | |
| 45 OBSERVATION_SOURCE_URL_REQUEST, | |
| 46 OBSERVATION_SOURCE_TCP, | |
| 47 OBSERVATION_SOURCE_QUIC, | |
| 48 OBSERVATION_SOURCE_CACHED_ESTIMATE, | |
| 49 OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM, | |
| 50 OBSERVATION_SOURCE_EXTERNAL_ESTIMATE | |
| 51 }; | |
| 52 | |
| 53 class NET_EXPORT RTTObserver { | |
|
mef
2015/08/11 17:16:27
Why is this class NET_EXPORT but parent is NET_EXP
bengr
2015/08/25 23:43:34
Done.
| |
| 54 public: | |
| 55 // Will be called when a new RTT observation is available. | |
| 56 virtual void OnRTTObservation(int32_t value, | |
|
mef
2015/08/11 17:16:27
Need to specify units of |value|. Also below.
bengr
2015/08/25 23:43:34
Done.
| |
| 57 const base::TimeTicks& timestamp, | |
| 58 ObservationSource source) = 0; | |
| 59 | |
| 60 protected: | |
| 61 RTTObserver() {} | |
| 62 virtual ~RTTObserver() {} | |
| 63 | |
| 64 private: | |
| 65 DISALLOW_COPY_AND_ASSIGN(RTTObserver); | |
| 66 }; | |
| 67 | |
| 68 class NET_EXPORT BandwidthObserver { | |
|
mef
2015/08/11 17:16:27
add class comment?
bengr
2015/08/25 23:43:34
Done.
| |
| 69 public: | |
| 70 // Will be called when a new Bandwidth observation is available. | |
| 71 virtual void OnBandwidthObservation(int32_t value, | |
| 72 const base::TimeTicks& timestamp, | |
| 73 ObservationSource source) = 0; | |
| 74 | |
| 75 protected: | |
| 76 BandwidthObserver() {} | |
| 77 virtual ~BandwidthObserver() {} | |
| 78 | |
| 79 private: | |
| 80 DISALLOW_COPY_AND_ASSIGN(BandwidthObserver); | |
| 81 }; | |
| 82 | |
| 36 // Creates a new NetworkQualityEstimator. | 83 // Creates a new NetworkQualityEstimator. |
| 37 // |variation_params| is the map containing all field trial parameters | 84 // |variation_params| is the map containing all field trial parameters |
| 38 // related to NetworkQualityEstimator field trial. | 85 // related to NetworkQualityEstimator field trial. |
| 39 explicit NetworkQualityEstimator( | 86 explicit NetworkQualityEstimator( |
| 40 const std::map<std::string, std::string>& variation_params); | 87 const std::map<std::string, std::string>& variation_params); |
| 41 | 88 |
| 42 ~NetworkQualityEstimator() override; | 89 ~NetworkQualityEstimator() override; |
| 43 | 90 |
| 44 // Returns the peak estimates (fastest RTT and peak throughput) of the | 91 // Returns the peak estimates (fastest RTT and peak throughput) of the |
| 45 // current network. | 92 // current network. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 58 // Notifies NetworkQualityEstimator that a response has been received. | 105 // Notifies NetworkQualityEstimator that a response has been received. |
| 59 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior | 106 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior |
| 60 // to applying filters (e.g. decompression, SDCH) from request creation time | 107 // to applying filters (e.g. decompression, SDCH) from request creation time |
| 61 // until now. | 108 // until now. |
| 62 // |prefiltered_bytes_read| is the count of the bytes received prior | 109 // |prefiltered_bytes_read| is the count of the bytes received prior |
| 63 // to applying filters in the most recent read. | 110 // to applying filters in the most recent read. |
| 64 void NotifyDataReceived(const URLRequest& request, | 111 void NotifyDataReceived(const URLRequest& request, |
| 65 int64_t cumulative_prefilter_bytes_read, | 112 int64_t cumulative_prefilter_bytes_read, |
| 66 int64_t prefiltered_bytes_read); | 113 int64_t prefiltered_bytes_read); |
| 67 | 114 |
| 115 void AddRTTObserver(RTTObserver* rtt_observer); | |
| 116 void RemoveRTTObserver(RTTObserver* rtt_observer); | |
| 117 void AddBandwidthObserver(BandwidthObserver* bandwidth_observer); | |
| 118 void RemoveBandwidthObserver(BandwidthObserver* bandwidth_observer); | |
| 119 | |
| 120 void Configure(bool allow_local_host_requests, bool allow_smaller_responses); | |
| 121 | |
| 68 protected: | 122 protected: |
| 69 // NetworkID is used to uniquely identify a network. | 123 // NetworkID is used to uniquely identify a network. |
| 70 // For the purpose of network quality estimation and caching, a network is | 124 // For the purpose of network quality estimation and caching, a network is |
| 71 // uniquely identified by a combination of |type| and | 125 // uniquely identified by a combination of |type| and |
| 72 // |id|. This approach is unable to distinguish networks with | 126 // |id|. This approach is unable to distinguish networks with |
| 73 // same name (e.g., different Wi-Fi networks with same SSID). | 127 // same name (e.g., different Wi-Fi networks with same SSID). |
| 74 // This is a protected member to expose it to tests. | 128 // This is a protected member to expose it to tests. |
| 75 struct NET_EXPORT_PRIVATE NetworkID { | 129 struct NET_EXPORT_PRIVATE NetworkID { |
| 76 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 130 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| 77 : type(type), id(id) {} | 131 : type(type), id(id) {} |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 // Quality of this cached network. | 217 // Quality of this cached network. |
| 164 const NetworkQuality network_quality_; | 218 const NetworkQuality network_quality_; |
| 165 | 219 |
| 166 private: | 220 private: |
| 167 DISALLOW_ASSIGN(CachedNetworkQuality); | 221 DISALLOW_ASSIGN(CachedNetworkQuality); |
| 168 }; | 222 }; |
| 169 | 223 |
| 170 // Records the round trip time or throughput observation, along with the time | 224 // Records the round trip time or throughput observation, along with the time |
| 171 // the observation was made. | 225 // the observation was made. |
| 172 struct NET_EXPORT_PRIVATE Observation { | 226 struct NET_EXPORT_PRIVATE Observation { |
| 173 Observation(int32_t value, base::TimeTicks timestamp); | 227 Observation(int32_t value, |
| 228 base::TimeTicks timestamp, | |
| 229 ObservationSource source); | |
| 174 ~Observation(); | 230 ~Observation(); |
| 175 | 231 |
| 176 // Value of the observation. | 232 // Value of the observation. |
| 177 const int32_t value; | 233 const int32_t value; |
| 178 | 234 |
| 179 // Time when the observation was taken. | 235 // Time when the observation was taken. |
| 180 const base::TimeTicks timestamp; | 236 const base::TimeTicks timestamp; |
| 237 | |
| 238 // The source of the observation. | |
| 239 ObservationSource source; | |
| 181 }; | 240 }; |
| 182 | 241 |
| 183 // Holds an observation and its weight. | 242 // Holds an observation and its weight. |
| 184 struct NET_EXPORT_PRIVATE WeightedObservation { | 243 struct NET_EXPORT_PRIVATE WeightedObservation { |
| 185 WeightedObservation(int32_t value, double weight) | 244 WeightedObservation(int32_t value, double weight) |
| 186 : value(value), weight(weight) {} | 245 : value(value), weight(weight) {} |
| 187 WeightedObservation(const WeightedObservation& other) | 246 WeightedObservation(const WeightedObservation& other) |
| 188 : WeightedObservation(other.value, other.weight) {} | 247 : WeightedObservation(other.value, other.weight) {} |
| 189 | 248 |
| 190 WeightedObservation& operator=(const WeightedObservation& other) { | 249 WeightedObservation& operator=(const WeightedObservation& other) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 // be slower than the returned estimate with 0.1 probability. | 357 // be slower than the returned estimate with 0.1 probability. |
| 299 NetworkQuality GetEstimate(int percentile) const; | 358 NetworkQuality GetEstimate(int percentile) const; |
| 300 | 359 |
| 301 // Returns the current network ID checking by calling the platform APIs. | 360 // Returns the current network ID checking by calling the platform APIs. |
| 302 // Virtualized for testing. | 361 // Virtualized for testing. |
| 303 virtual NetworkID GetCurrentNetworkID() const; | 362 virtual NetworkID GetCurrentNetworkID() const; |
| 304 | 363 |
| 305 // Writes the estimated quality of the current network to the cache. | 364 // Writes the estimated quality of the current network to the cache. |
| 306 void CacheNetworkQualityEstimate(); | 365 void CacheNetworkQualityEstimate(); |
| 307 | 366 |
| 367 void NotifyObserversOfRTT(const Observation& observation); | |
| 368 | |
| 369 void NotifyObserversOfBandwidth(const Observation& observation); | |
| 370 | |
| 308 // Records the UMA related to RTT. | 371 // Records the UMA related to RTT. |
| 309 void RecordRTTUMA(int32_t estimated_value_msec, | 372 void RecordRTTUMA(int32_t estimated_value_msec, |
| 310 int32_t actual_value_msec) const; | 373 int32_t actual_value_msec) const; |
| 311 | 374 |
| 312 // Determines if the requests to local host can be used in estimating the | 375 // Determines if the requests to local host can be used in estimating the |
| 313 // network quality. Set to true only for tests. | 376 // network quality. Set to true only for tests. |
| 314 const bool allow_localhost_requests_; | 377 bool allow_localhost_requests_; |
| 315 | 378 |
| 316 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 379 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 317 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 380 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 318 // network quality. Set to true only for tests. | 381 // network quality. Set to true only for tests. |
| 319 const bool allow_small_responses_; | 382 bool allow_small_responses_; |
| 320 | 383 |
| 321 // Time when last connection change was observed. | 384 // Time when last connection change was observed. |
| 322 base::TimeTicks last_connection_change_; | 385 base::TimeTicks last_connection_change_; |
| 323 | 386 |
| 324 // ID of the current network. | 387 // ID of the current network. |
| 325 NetworkID current_network_id_; | 388 NetworkID current_network_id_; |
| 326 | 389 |
| 327 // Peak network quality (fastest round-trip-time (RTT) and highest | 390 // Peak network quality (fastest round-trip-time (RTT) and highest |
| 328 // downstream throughput) measured since last connectivity change. RTT is | 391 // downstream throughput) measured since last connectivity change. RTT is |
| 329 // measured from time the request is sent until the first byte received. | 392 // measured from time the request is sent until the first byte received. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 343 | 406 |
| 344 // Default network quality observations obtained from the network quality | 407 // Default network quality observations obtained from the network quality |
| 345 // estimator field trial parameters. The observations are indexed by | 408 // estimator field trial parameters. The observations are indexed by |
| 346 // ConnectionType. | 409 // ConnectionType. |
| 347 NetworkQuality | 410 NetworkQuality |
| 348 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; | 411 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
| 349 | 412 |
| 350 // Estimated network quality. Updated on mainframe requests. | 413 // Estimated network quality. Updated on mainframe requests. |
| 351 NetworkQuality estimated_median_network_quality_; | 414 NetworkQuality estimated_median_network_quality_; |
| 352 | 415 |
| 416 base::ObserverList<RTTObserver> rtt_observer_list_; | |
| 417 base::ObserverList<BandwidthObserver> bandwidth_observer_list_; | |
| 418 | |
| 353 base::ThreadChecker thread_checker_; | 419 base::ThreadChecker thread_checker_; |
| 354 | 420 |
| 355 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 421 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 356 }; | 422 }; |
| 357 | 423 |
| 358 } // namespace net | 424 } // namespace net |
| 359 | 425 |
| 360 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 426 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |