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