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_threadsafe.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/external_estimate_provider.h" | 21 #include "net/base/external_estimate_provider.h" |
| 20 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
| 21 #include "net/base/network_change_notifier.h" | 23 #include "net/base/network_change_notifier.h" |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 | 26 |
| 25 class URLRequest; | 27 class URLRequest; |
| 26 | 28 |
| 27 // NetworkQualityEstimator provides network quality estimates (quality of the | 29 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 28 // full paths to all origins that have been connected to). | 30 // full paths to all origins that have been connected to). |
| 29 // The estimates are based on the observed organic traffic. | 31 // The estimates are based on the observed organic traffic. |
| 30 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 32 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 31 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 33 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 32 // A single instance of NQE can be attached to multiple URLRequestContexts, | 34 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 33 // thereby increasing the single NQE instance's accuracy by providing more | 35 // thereby increasing the single NQE instance's accuracy by providing more |
| 34 // observed traffic characteristics. | 36 // observed traffic characteristics. |
| 35 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 37 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 36 : public NetworkChangeNotifier::ConnectionTypeObserver, | 38 : public NetworkChangeNotifier::ConnectionTypeObserver, |
| 37 public ExternalEstimateProvider::UpdatedEstimateDelegate { | 39 public ExternalEstimateProvider::UpdatedEstimateDelegate { |
| 38 public: | 40 public: |
| 41 enum ObservationType { DOWNLINK_THROUGHPUT, RTT }; | |
| 42 | |
| 43 // On Android, a Java counterpart will be generated for this enum. | |
| 44 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | |
| 45 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | |
| 46 // GENERATED_JAVA_PREFIX_TO_STRIP: OBSERVATION_SOURCE_ | |
| 47 enum ObservationSource { | |
| 48 OBSERVATION_SOURCE_URL_REQUEST, | |
| 49 OBSERVATION_SOURCE_TCP, | |
| 50 OBSERVATION_SOURCE_QUIC, | |
| 51 OBSERVATION_SOURCE_CACHED_ESTIMATE, | |
| 52 OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM, | |
| 53 OBSERVATION_SOURCE_EXTERNAL_ESTIMATE | |
| 54 }; | |
| 55 | |
| 56 // Observes measurements of round trip time. | |
| 57 class NET_EXPORT_PRIVATE RTTObserver { | |
| 58 public: | |
| 59 // Will be called when a new RTT observation is available. The round trip | |
| 60 // times is specified in milliseconds. | |
| 61 virtual void OnRTTObservation(int32_t rtt_ms, | |
| 62 const base::TimeTicks& timestamp, | |
| 63 ObservationSource source) = 0; | |
| 64 | |
| 65 protected: | |
| 66 RTTObserver() {} | |
| 67 virtual ~RTTObserver() {} | |
| 68 | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(RTTObserver); | |
| 71 }; | |
| 72 | |
| 73 // Observes measurements of throughput. | |
| 74 class NET_EXPORT_PRIVATE ThroughputObserver { | |
| 75 public: | |
| 76 // Will be called when a new throughput observation is available. | |
| 77 // Throughput is specified in kilobits per second. | |
| 78 virtual void OnThroughputObservation(int32_t throughput_kbps, | |
| 79 const base::TimeTicks& timestamp, | |
| 80 ObservationSource source) = 0; | |
| 81 | |
| 82 protected: | |
| 83 ThroughputObserver() {} | |
| 84 virtual ~ThroughputObserver() {} | |
| 85 | |
| 86 private: | |
| 87 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver); | |
| 88 }; | |
| 89 | |
| 39 // Creates a new NetworkQualityEstimator. | 90 // Creates a new NetworkQualityEstimator. |
| 40 // |variation_params| is the map containing all field trial parameters | 91 // |variation_params| is the map containing all field trial parameters |
| 41 // related to NetworkQualityEstimator field trial. | 92 // related to NetworkQualityEstimator field trial. |
| 42 // |external_estimates_provider| may be NULL. | 93 // |external_estimates_provider| may be NULL. |
| 43 NetworkQualityEstimator( | 94 NetworkQualityEstimator( |
| 44 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, | 95 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 45 const std::map<std::string, std::string>& variation_params); | 96 const std::map<std::string, std::string>& variation_params); |
| 46 | 97 |
| 47 ~NetworkQualityEstimator() override; | 98 ~NetworkQualityEstimator() override; |
| 48 | 99 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 69 virtual bool GetRecentMedianRTT(const base::TimeTicks& begin_timestamp, | 120 virtual bool GetRecentMedianRTT(const base::TimeTicks& begin_timestamp, |
| 70 base::TimeDelta* rtt) const; | 121 base::TimeDelta* rtt) const; |
| 71 | 122 |
| 72 // Returns true if median downstream throughput is available and sets |kbps| | 123 // Returns true if median downstream throughput is available and sets |kbps| |
| 73 // to the median of downstream Kbps observations since |begin_timestamp|. | 124 // to the median of downstream Kbps observations since |begin_timestamp|. |
| 74 // Virtualized for testing. |kbps| should not be null. | 125 // Virtualized for testing. |kbps| should not be null. |
| 75 virtual bool GetRecentMedianDownlinkThroughputKbps( | 126 virtual bool GetRecentMedianDownlinkThroughputKbps( |
| 76 const base::TimeTicks& begin_timestamp, | 127 const base::TimeTicks& begin_timestamp, |
| 77 int32_t* kbps) const; | 128 int32_t* kbps) const; |
| 78 | 129 |
| 130 void AddRTTObserver(RTTObserver* rtt_observer); | |
| 131 void RemoveRTTObserver(RTTObserver* rtt_observer); | |
| 132 void AddThroughputObserver(ThroughputObserver* throughput_observer); | |
| 133 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); | |
| 134 | |
| 135 void Configure(bool allow_local_host_requests, bool allow_smaller_responses); | |
| 136 | |
| 79 protected: | 137 protected: |
| 80 // NetworkID is used to uniquely identify a network. | 138 // NetworkID is used to uniquely identify a network. |
| 81 // For the purpose of network quality estimation and caching, a network is | 139 // For the purpose of network quality estimation and caching, a network is |
| 82 // uniquely identified by a combination of |type| and | 140 // uniquely identified by a combination of |type| and |
| 83 // |id|. This approach is unable to distinguish networks with | 141 // |id|. This approach is unable to distinguish networks with |
| 84 // same name (e.g., different Wi-Fi networks with same SSID). | 142 // same name (e.g., different Wi-Fi networks with same SSID). |
| 85 // This is a protected member to expose it to tests. | 143 // This is a protected member to expose it to tests. |
| 86 struct NET_EXPORT_PRIVATE NetworkID { | 144 struct NET_EXPORT_PRIVATE NetworkID { |
| 87 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 145 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| 88 : type(type), id(id) {} | 146 : type(type), id(id) {} |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 const base::TimeTicks last_update_time_; | 264 const base::TimeTicks last_update_time_; |
| 207 | 265 |
| 208 // Quality of this cached network. | 266 // Quality of this cached network. |
| 209 const NetworkQuality network_quality_; | 267 const NetworkQuality network_quality_; |
| 210 | 268 |
| 211 private: | 269 private: |
| 212 DISALLOW_ASSIGN(CachedNetworkQuality); | 270 DISALLOW_ASSIGN(CachedNetworkQuality); |
| 213 }; | 271 }; |
| 214 | 272 |
| 215 // Records the round trip time or throughput observation, along with the time | 273 // Records the round trip time or throughput observation, along with the time |
| 216 // the observation was made. | 274 // the observation was made. |
|
mef
2015/08/26 17:28:40
add comment about source?
bengr
2015/08/27 16:44:34
Done.
| |
| 217 struct NET_EXPORT_PRIVATE Observation { | 275 struct NET_EXPORT_PRIVATE Observation { |
| 218 Observation(int32_t value, base::TimeTicks timestamp); | 276 Observation(int32_t value, |
| 277 base::TimeTicks timestamp, | |
| 278 ObservationSource source); | |
| 219 ~Observation(); | 279 ~Observation(); |
| 220 | 280 |
| 221 // Value of the observation. | 281 // Value of the observation. |
| 222 const int32_t value; | 282 const int32_t value; |
| 223 | 283 |
| 224 // Time when the observation was taken. | 284 // Time when the observation was taken. |
| 225 const base::TimeTicks timestamp; | 285 const base::TimeTicks timestamp; |
| 286 | |
| 287 // The source of the observation. | |
| 288 ObservationSource source; | |
| 226 }; | 289 }; |
| 227 | 290 |
| 228 // Holds an observation and its weight. | 291 // Holds an observation and its weight. |
| 229 struct NET_EXPORT_PRIVATE WeightedObservation { | 292 struct NET_EXPORT_PRIVATE WeightedObservation { |
| 230 WeightedObservation(int32_t value, double weight) | 293 WeightedObservation(int32_t value, double weight) |
| 231 : value(value), weight(weight) {} | 294 : value(value), weight(weight) {} |
| 232 WeightedObservation(const WeightedObservation& other) | 295 WeightedObservation(const WeightedObservation& other) |
| 233 : WeightedObservation(other.value, other.weight) {} | 296 : WeightedObservation(other.value, other.weight) {} |
| 234 | 297 |
| 235 WeightedObservation& operator=(const WeightedObservation& other) { | 298 WeightedObservation& operator=(const WeightedObservation& other) { |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 const base::TimeTicks& begin_timestamp, | 434 const base::TimeTicks& begin_timestamp, |
| 372 int percentile) const; | 435 int percentile) const; |
| 373 | 436 |
| 374 // Returns the current network ID checking by calling the platform APIs. | 437 // Returns the current network ID checking by calling the platform APIs. |
| 375 // Virtualized for testing. | 438 // Virtualized for testing. |
| 376 virtual NetworkID GetCurrentNetworkID() const; | 439 virtual NetworkID GetCurrentNetworkID() const; |
| 377 | 440 |
| 378 // Writes the estimated quality of the current network to the cache. | 441 // Writes the estimated quality of the current network to the cache. |
| 379 void CacheNetworkQualityEstimate(); | 442 void CacheNetworkQualityEstimate(); |
| 380 | 443 |
| 444 void NotifyObserversOfRTT(const Observation& observation); | |
| 445 | |
| 446 void NotifyObserversOfThroughput(const Observation& observation); | |
| 447 | |
| 381 // Records the UMA related to RTT. | 448 // Records the UMA related to RTT. |
| 382 void RecordRTTUMA(int32_t estimated_value_msec, | 449 void RecordRTTUMA(int32_t estimated_value_msec, |
| 383 int32_t actual_value_msec) const; | 450 int32_t actual_value_msec) const; |
| 384 | 451 |
| 385 // Returns true only if |request| can be used for network quality estimation. | 452 // Returns true only if |request| can be used for network quality estimation. |
| 386 // Only the requests that go over network are considered to provide useful | 453 // Only the requests that go over network are considered to provide useful |
| 387 // observations. | 454 // observations. |
| 388 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 455 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
| 389 | 456 |
| 390 // Determines if the requests to local host can be used in estimating the | 457 // Determines if the requests to local host can be used in estimating the |
| 391 // network quality. Set to true only for tests. | 458 // network quality. Set to true only for tests. |
| 392 const bool allow_localhost_requests_; | 459 bool allow_localhost_requests_; |
| 393 | 460 |
| 394 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 461 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 395 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 462 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 396 // network quality. Set to true only for tests. | 463 // network quality. Set to true only for tests. |
| 397 const bool allow_small_responses_; | 464 bool allow_small_responses_; |
| 398 | 465 |
| 399 // Time when last connection change was observed. | 466 // Time when last connection change was observed. |
| 400 base::TimeTicks last_connection_change_; | 467 base::TimeTicks last_connection_change_; |
| 401 | 468 |
| 402 // ID of the current network. | 469 // ID of the current network. |
| 403 NetworkID current_network_id_; | 470 NetworkID current_network_id_; |
| 404 | 471 |
| 405 // Peak network quality (fastest round-trip-time (RTT) and highest | 472 // Peak network quality (fastest round-trip-time (RTT) and highest |
| 406 // downstream throughput) measured since last connectivity change. RTT is | 473 // downstream throughput) measured since last connectivity change. RTT is |
| 407 // measured from time the request is sent until the first byte received. | 474 // measured from time the request is sent until the first byte received. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 425 NetworkQuality | 492 NetworkQuality |
| 426 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; | 493 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
| 427 | 494 |
| 428 // Estimated network quality. Updated on mainframe requests. | 495 // Estimated network quality. Updated on mainframe requests. |
| 429 NetworkQuality estimated_median_network_quality_; | 496 NetworkQuality estimated_median_network_quality_; |
| 430 | 497 |
| 431 // ExternalEstimateProvider that provides network quality using operating | 498 // ExternalEstimateProvider that provides network quality using operating |
| 432 // system APIs. May be NULL. | 499 // system APIs. May be NULL. |
| 433 const scoped_ptr<ExternalEstimateProvider> external_estimates_provider_; | 500 const scoped_ptr<ExternalEstimateProvider> external_estimates_provider_; |
| 434 | 501 |
| 502 // Observer lists for round trip times and throughput measurements. | |
| 503 scoped_refptr<base::ObserverListThreadSafe<RTTObserver>> rtt_observer_list_; | |
| 504 scoped_refptr<base::ObserverListThreadSafe<ThroughputObserver>> | |
| 505 throughput_observer_list_; | |
| 506 | |
| 435 base::ThreadChecker thread_checker_; | 507 base::ThreadChecker thread_checker_; |
| 436 | 508 |
| 437 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 509 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 438 }; | 510 }; |
| 439 | 511 |
| 440 } // namespace net | 512 } // namespace net |
| 441 | 513 |
| 442 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 514 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |