network_quality.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_NQE_NETWORK_QUALITY_H_
  5. #define NET_NQE_NETWORK_QUALITY_H_
  6. #include <stdint.h>
  7. #include "base/gtest_prod_util.h"
  8. #include "base/sequence_checker.h"
  9. #include "base/time/time.h"
  10. #include "net/base/net_export.h"
  11. namespace net::nqe::internal {
  12. // RTT and throughput values are set to |INVALID_RTT_THROUGHPUT| if a valid
  13. // value is unavailable.
  14. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
  15. enum RttThroughputValues {
  16. // Invalid value.
  17. INVALID_RTT_THROUGHPUT = -1,
  18. };
  19. // Returns the RTT value to be used when the valid RTT is unavailable. Readers
  20. // should discard RTT if it is set to the value returned by |InvalidRTT()|.
  21. // TODO(tbansal): Remove this method, and replace all calls by
  22. // |INVALID_RTT_THROUGHPUT|.
  23. NET_EXPORT_PRIVATE base::TimeDelta InvalidRTT();
  24. // NetworkQuality is used to cache the quality of a network connection.
  25. class NET_EXPORT_PRIVATE NetworkQuality {
  26. public:
  27. NetworkQuality();
  28. // |http_rtt| is the estimate of the round trip time at the HTTP layer.
  29. // |transport_rtt| is the estimate of the round trip time at the transport
  30. // layer. |downstream_throughput_kbps| is the estimate of the downstream
  31. // throughput in kilobits per second.
  32. NetworkQuality(const base::TimeDelta& http_rtt,
  33. const base::TimeDelta& transport_rtt,
  34. int32_t downstream_throughput_kbps);
  35. NetworkQuality(const NetworkQuality& other);
  36. ~NetworkQuality();
  37. NetworkQuality& operator=(const NetworkQuality& other);
  38. bool operator==(const NetworkQuality& other) const;
  39. // Returns true if |this| is at least as fast as |other| for all parameters
  40. // (HTTP RTT, transport RTT etc.)
  41. bool IsFaster(const NetworkQuality& other) const;
  42. // Returns the estimate of the round trip time at the HTTP layer.
  43. const base::TimeDelta& http_rtt() const {
  44. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  45. return http_rtt_;
  46. }
  47. void set_http_rtt(base::TimeDelta http_rtt) {
  48. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  49. http_rtt_ = http_rtt;
  50. DCHECK_LE(INVALID_RTT_THROUGHPUT, http_rtt_.InMilliseconds());
  51. }
  52. // Returns the estimate of the round trip time at the transport layer.
  53. const base::TimeDelta& transport_rtt() const {
  54. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  55. return transport_rtt_;
  56. }
  57. void set_transport_rtt(base::TimeDelta transport_rtt) {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. transport_rtt_ = transport_rtt;
  60. DCHECK_LE(INVALID_RTT_THROUGHPUT, transport_rtt_.InMilliseconds());
  61. }
  62. // Returns the estimate of the downstream throughput in Kbps (Kilobits per
  63. // second).
  64. int32_t downstream_throughput_kbps() const {
  65. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  66. return downstream_throughput_kbps_;
  67. }
  68. void set_downstream_throughput_kbps(int32_t downstream_throughput_kbps) {
  69. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  70. downstream_throughput_kbps_ = downstream_throughput_kbps;
  71. DCHECK_LE(INVALID_RTT_THROUGHPUT, downstream_throughput_kbps_);
  72. }
  73. private:
  74. // Verifies that the value of network quality is within the expected range.
  75. void VerifyValueCorrectness() const;
  76. // Estimated round trip time at the HTTP layer.
  77. base::TimeDelta http_rtt_;
  78. // Estimated round trip time at the transport layer.
  79. base::TimeDelta transport_rtt_;
  80. // Estimated downstream throughput in kilobits per second.
  81. int32_t downstream_throughput_kbps_;
  82. SEQUENCE_CHECKER(sequence_checker_);
  83. };
  84. } // namespace net::nqe::internal
  85. #endif // NET_NQE_NETWORK_QUALITY_H_