network_quality.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "net/nqe/network_quality.h"
  5. namespace net::nqe::internal {
  6. base::TimeDelta InvalidRTT() {
  7. return base::Milliseconds(INVALID_RTT_THROUGHPUT);
  8. }
  9. NetworkQuality::NetworkQuality()
  10. : NetworkQuality(InvalidRTT(), InvalidRTT(), INVALID_RTT_THROUGHPUT) {
  11. VerifyValueCorrectness();
  12. DETACH_FROM_SEQUENCE(sequence_checker_);
  13. }
  14. NetworkQuality::NetworkQuality(const base::TimeDelta& http_rtt,
  15. const base::TimeDelta& transport_rtt,
  16. int32_t downstream_throughput_kbps)
  17. : http_rtt_(http_rtt),
  18. transport_rtt_(transport_rtt),
  19. downstream_throughput_kbps_(downstream_throughput_kbps) {
  20. VerifyValueCorrectness();
  21. DETACH_FROM_SEQUENCE(sequence_checker_);
  22. }
  23. NetworkQuality::NetworkQuality(const NetworkQuality& other)
  24. : NetworkQuality(other.http_rtt_,
  25. other.transport_rtt_,
  26. other.downstream_throughput_kbps_) {
  27. VerifyValueCorrectness();
  28. DETACH_FROM_SEQUENCE(sequence_checker_);
  29. }
  30. NetworkQuality::~NetworkQuality() = default;
  31. NetworkQuality& NetworkQuality::operator=(const NetworkQuality& other) {
  32. http_rtt_ = other.http_rtt_;
  33. transport_rtt_ = other.transport_rtt_;
  34. downstream_throughput_kbps_ = other.downstream_throughput_kbps_;
  35. VerifyValueCorrectness();
  36. DETACH_FROM_SEQUENCE(sequence_checker_);
  37. return *this;
  38. }
  39. bool NetworkQuality::operator==(const NetworkQuality& other) const {
  40. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  41. return http_rtt_ == other.http_rtt_ &&
  42. transport_rtt_ == other.transport_rtt_ &&
  43. downstream_throughput_kbps_ == other.downstream_throughput_kbps_;
  44. }
  45. bool NetworkQuality::IsFaster(const NetworkQuality& other) const {
  46. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  47. return (http_rtt() == InvalidRTT() || other.http_rtt() == InvalidRTT() ||
  48. http_rtt() <= other.http_rtt()) &&
  49. (transport_rtt() == InvalidRTT() ||
  50. other.transport_rtt() == InvalidRTT() ||
  51. transport_rtt() <= other.transport_rtt()) &&
  52. (downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT ||
  53. other.downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT ||
  54. downstream_throughput_kbps() >= other.downstream_throughput_kbps());
  55. }
  56. void NetworkQuality::VerifyValueCorrectness() const {
  57. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  58. DCHECK_LE(INVALID_RTT_THROUGHPUT, http_rtt_.InMilliseconds());
  59. DCHECK_LE(INVALID_RTT_THROUGHPUT, transport_rtt_.InMilliseconds());
  60. DCHECK_LE(INVALID_RTT_THROUGHPUT, downstream_throughput_kbps_);
  61. }
  62. } // namespace net::nqe::internal