tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 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 | |
| 5 | #include "net/nqe/network_quality.h" |
| 6 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 7 | namespace net::nqe::internal { |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 8 | |
| 9 | base::TimeDelta InvalidRTT() { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 10 | return base::Milliseconds(INVALID_RTT_THROUGHPUT); |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | NetworkQuality::NetworkQuality() |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 14 | : NetworkQuality(InvalidRTT(), InvalidRTT(), INVALID_RTT_THROUGHPUT) { |
| 15 | VerifyValueCorrectness(); |
| 16 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 17 | } |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 18 | |
tbansal | 805892c | 2016-05-26 16:46:59 | [diff] [blame] | 19 | NetworkQuality::NetworkQuality(const base::TimeDelta& http_rtt, |
| 20 | const base::TimeDelta& transport_rtt, |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 21 | int32_t downstream_throughput_kbps) |
tbansal | 805892c | 2016-05-26 16:46:59 | [diff] [blame] | 22 | : http_rtt_(http_rtt), |
| 23 | transport_rtt_(transport_rtt), |
| 24 | downstream_throughput_kbps_(downstream_throughput_kbps) { |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 25 | VerifyValueCorrectness(); |
| 26 | DETACH_FROM_SEQUENCE(sequence_checker_); |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | NetworkQuality::NetworkQuality(const NetworkQuality& other) |
tbansal | 805892c | 2016-05-26 16:46:59 | [diff] [blame] | 30 | : NetworkQuality(other.http_rtt_, |
| 31 | other.transport_rtt_, |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 32 | other.downstream_throughput_kbps_) { |
| 33 | VerifyValueCorrectness(); |
| 34 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 35 | } |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 36 | |
Chris Watkins | f3c65a6 | 2017-12-01 02:06:41 | [diff] [blame] | 37 | NetworkQuality::~NetworkQuality() = default; |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 38 | |
| 39 | NetworkQuality& NetworkQuality::operator=(const NetworkQuality& other) { |
tbansal | 805892c | 2016-05-26 16:46:59 | [diff] [blame] | 40 | http_rtt_ = other.http_rtt_; |
| 41 | transport_rtt_ = other.transport_rtt_; |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 42 | downstream_throughput_kbps_ = other.downstream_throughput_kbps_; |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 43 | VerifyValueCorrectness(); |
| 44 | DETACH_FROM_SEQUENCE(sequence_checker_); |
tbansal | b67539d | 2016-05-16 17:54:13 | [diff] [blame] | 45 | return *this; |
| 46 | } |
| 47 | |
tbansal | bff2aec | 2016-07-25 18:28:54 | [diff] [blame] | 48 | bool NetworkQuality::operator==(const NetworkQuality& other) const { |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 49 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
tbansal | bff2aec | 2016-07-25 18:28:54 | [diff] [blame] | 50 | return http_rtt_ == other.http_rtt_ && |
| 51 | transport_rtt_ == other.transport_rtt_ && |
| 52 | downstream_throughput_kbps_ == other.downstream_throughput_kbps_; |
| 53 | } |
| 54 | |
tbansal | 2739868c | 2016-07-27 00:44:20 | [diff] [blame] | 55 | bool NetworkQuality::IsFaster(const NetworkQuality& other) const { |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 56 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
tbansal | 2739868c | 2016-07-27 00:44:20 | [diff] [blame] | 57 | return (http_rtt() == InvalidRTT() || other.http_rtt() == InvalidRTT() || |
| 58 | http_rtt() <= other.http_rtt()) && |
| 59 | (transport_rtt() == InvalidRTT() || |
| 60 | other.transport_rtt() == InvalidRTT() || |
| 61 | transport_rtt() <= other.transport_rtt()) && |
zhuoyu.qian | d80590e5 | 2017-10-30 10:23:51 | [diff] [blame] | 62 | (downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT || |
| 63 | other.downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT || |
tbansal | 2739868c | 2016-07-27 00:44:20 | [diff] [blame] | 64 | downstream_throughput_kbps() >= other.downstream_throughput_kbps()); |
| 65 | } |
| 66 | |
Tarun Bansal | df224ab | 2018-01-03 17:27:37 | [diff] [blame] | 67 | void NetworkQuality::VerifyValueCorrectness() const { |
| 68 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 69 | DCHECK_LE(INVALID_RTT_THROUGHPUT, http_rtt_.InMilliseconds()); |
| 70 | DCHECK_LE(INVALID_RTT_THROUGHPUT, transport_rtt_.InMilliseconds()); |
| 71 | DCHECK_LE(INVALID_RTT_THROUGHPUT, downstream_throughput_kbps_); |
| 72 | } |
| 73 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 74 | } // namespace net::nqe::internal |