1 // Copyright 2016 The Chromium Authors
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::nqe::internal {
8
InvalidRTT()9 base::TimeDelta InvalidRTT() {
10 return base::Milliseconds(INVALID_RTT_THROUGHPUT);
11 }
12
NetworkQuality()13 NetworkQuality::NetworkQuality()
14 : NetworkQuality(InvalidRTT(), InvalidRTT(), INVALID_RTT_THROUGHPUT) {
15 VerifyValueCorrectness();
16 DETACH_FROM_SEQUENCE(sequence_checker_);
17 }
18
NetworkQuality(const base::TimeDelta & http_rtt,const base::TimeDelta & transport_rtt,int32_t downstream_throughput_kbps)19 NetworkQuality::NetworkQuality(const base::TimeDelta& http_rtt,
20 const base::TimeDelta& transport_rtt,
21 int32_t downstream_throughput_kbps)
22 : http_rtt_(http_rtt),
23 transport_rtt_(transport_rtt),
24 downstream_throughput_kbps_(downstream_throughput_kbps) {
25 VerifyValueCorrectness();
26 DETACH_FROM_SEQUENCE(sequence_checker_);
27 }
28
NetworkQuality(const NetworkQuality & other)29 NetworkQuality::NetworkQuality(const NetworkQuality& other)
30 : NetworkQuality(other.http_rtt_,
31 other.transport_rtt_,
32 other.downstream_throughput_kbps_) {
33 VerifyValueCorrectness();
34 DETACH_FROM_SEQUENCE(sequence_checker_);
35 }
36
37 NetworkQuality::~NetworkQuality() = default;
38
operator =(const NetworkQuality & other)39 NetworkQuality& NetworkQuality::operator=(const NetworkQuality& other) {
40 http_rtt_ = other.http_rtt_;
41 transport_rtt_ = other.transport_rtt_;
42 downstream_throughput_kbps_ = other.downstream_throughput_kbps_;
43 VerifyValueCorrectness();
44 DETACH_FROM_SEQUENCE(sequence_checker_);
45 return *this;
46 }
47
operator ==(const NetworkQuality & other) const48 bool NetworkQuality::operator==(const NetworkQuality& other) const {
49 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
50 return http_rtt_ == other.http_rtt_ &&
51 transport_rtt_ == other.transport_rtt_ &&
52 downstream_throughput_kbps_ == other.downstream_throughput_kbps_;
53 }
54
IsFaster(const NetworkQuality & other) const55 bool NetworkQuality::IsFaster(const NetworkQuality& other) const {
56 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
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()) &&
62 (downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT ||
63 other.downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT ||
64 downstream_throughput_kbps() >= other.downstream_throughput_kbps());
65 }
66
VerifyValueCorrectness() const67 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
74 } // namespace net::nqe::internal
75