• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NET_NQE_NETWORK_QUALITY_H_
6 #define NET_NQE_NETWORK_QUALITY_H_
7 
8 #include <stdint.h>
9 
10 #include "base/gtest_prod_util.h"
11 #include "base/sequence_checker.h"
12 #include "base/time/time.h"
13 #include "net/base/net_export.h"
14 
15 namespace net::nqe::internal {
16 
17 // RTT and throughput values are set to |INVALID_RTT_THROUGHPUT| if a valid
18 // value is unavailable.
19 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
20 enum RttThroughputValues {
21   // Invalid value.
22   INVALID_RTT_THROUGHPUT = -1,
23 };
24 
25 // Returns the RTT value to be used when the valid RTT is unavailable. Readers
26 // should discard RTT if it is set to the value returned by |InvalidRTT()|.
27 // TODO(tbansal): Remove this method, and replace all calls by
28 // |INVALID_RTT_THROUGHPUT|.
29 NET_EXPORT_PRIVATE base::TimeDelta InvalidRTT();
30 
31 // NetworkQuality is used to cache the quality of a network connection.
32 class NET_EXPORT_PRIVATE NetworkQuality {
33  public:
34   NetworkQuality();
35   // |http_rtt| is the estimate of the round trip time at the HTTP layer.
36   // |transport_rtt| is the estimate of the round trip time at the transport
37   // layer. |downstream_throughput_kbps| is the estimate of the downstream
38   // throughput in kilobits per second.
39   NetworkQuality(const base::TimeDelta& http_rtt,
40                  const base::TimeDelta& transport_rtt,
41                  int32_t downstream_throughput_kbps);
42   NetworkQuality(const NetworkQuality& other);
43   ~NetworkQuality();
44 
45   NetworkQuality& operator=(const NetworkQuality& other);
46 
47   bool operator==(const NetworkQuality& other) const;
48 
49   // Returns true if |this| is at least as fast as |other| for all parameters
50   // (HTTP RTT, transport RTT etc.)
51   bool IsFaster(const NetworkQuality& other) const;
52 
53   // Returns the estimate of the round trip time at the HTTP layer.
http_rtt()54   const base::TimeDelta& http_rtt() const {
55     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
56     return http_rtt_;
57   }
58 
set_http_rtt(base::TimeDelta http_rtt)59   void set_http_rtt(base::TimeDelta http_rtt) {
60     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
61     http_rtt_ = http_rtt;
62     DCHECK_LE(INVALID_RTT_THROUGHPUT, http_rtt_.InMilliseconds());
63   }
64 
65   // Returns the estimate of the round trip time at the transport layer.
transport_rtt()66   const base::TimeDelta& transport_rtt() const {
67     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
68     return transport_rtt_;
69   }
70 
set_transport_rtt(base::TimeDelta transport_rtt)71   void set_transport_rtt(base::TimeDelta transport_rtt) {
72     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
73     transport_rtt_ = transport_rtt;
74     DCHECK_LE(INVALID_RTT_THROUGHPUT, transport_rtt_.InMilliseconds());
75   }
76 
77   // Returns the estimate of the downstream throughput in Kbps (Kilobits per
78   // second).
downstream_throughput_kbps()79   int32_t downstream_throughput_kbps() const {
80     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
81     return downstream_throughput_kbps_;
82   }
83 
set_downstream_throughput_kbps(int32_t downstream_throughput_kbps)84   void set_downstream_throughput_kbps(int32_t downstream_throughput_kbps) {
85     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
86     downstream_throughput_kbps_ = downstream_throughput_kbps;
87     DCHECK_LE(INVALID_RTT_THROUGHPUT, downstream_throughput_kbps_);
88   }
89 
90  private:
91   // Verifies that the value of network quality is within the expected range.
92   void VerifyValueCorrectness() const;
93 
94   // Estimated round trip time at the HTTP layer.
95   base::TimeDelta http_rtt_;
96 
97   // Estimated round trip time at the transport layer.
98   base::TimeDelta transport_rtt_;
99 
100   // Estimated downstream throughput in kilobits per second.
101   int32_t downstream_throughput_kbps_;
102 
103   SEQUENCE_CHECKER(sequence_checker_);
104 };
105 
106 }  // namespace net::nqe::internal
107 
108 #endif  // NET_NQE_NETWORK_QUALITY_H_
109