Web MIDI: make platform dependent initialization asynchronous
[chromium-blink-merge.git] / net / quic / quic_config.h
blob88ad8b6a91c602d7e5a4e6058d74269a2c4bb5c9
1 // Copyright (c) 2013 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.
5 #ifndef NET_QUIC_QUIC_CONFIG_H_
6 #define NET_QUIC_QUIC_CONFIG_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "net/quic/quic_protocol.h"
12 #include "net/quic/quic_time.h"
14 namespace net {
16 namespace test {
17 class QuicConfigPeer;
18 } // namespace test
20 class CryptoHandshakeMessage;
22 // Describes whether or not a given QuicTag is required or optional in the
23 // handshake message.
24 enum QuicConfigPresence {
25 // This negotiable value can be absent from the handshake message. Default
26 // value is selected as the negotiated value in such a case.
27 PRESENCE_OPTIONAL,
28 // This negotiable value is required in the handshake message otherwise the
29 // Process*Hello function returns an error.
30 PRESENCE_REQUIRED,
33 // Whether the CryptoHandshakeMessage is from the client or server.
34 enum HelloType {
35 CLIENT,
36 SERVER,
39 // An abstract base class that stores a value that can be sent in CHLO/SHLO
40 // message. These values can be OPTIONAL or REQUIRED, depending on |presence_|.
41 class NET_EXPORT_PRIVATE QuicConfigValue {
42 public:
43 QuicConfigValue(QuicTag tag, QuicConfigPresence presence);
44 virtual ~QuicConfigValue();
46 // Serialises tag name and value(s) to |out|.
47 virtual void ToHandshakeMessage(CryptoHandshakeMessage* out) const = 0;
49 // Selects a mutually acceptable value from those offered in |peer_hello|
50 // and those defined in the subclass.
51 virtual QuicErrorCode ProcessPeerHello(
52 const CryptoHandshakeMessage& peer_hello,
53 HelloType hello_type,
54 std::string* error_details) = 0;
56 protected:
57 const QuicTag tag_;
58 const QuicConfigPresence presence_;
61 class NET_EXPORT_PRIVATE QuicNegotiableValue : public QuicConfigValue {
62 public:
63 QuicNegotiableValue(QuicTag tag, QuicConfigPresence presence);
64 virtual ~QuicNegotiableValue();
66 bool negotiated() const {
67 return negotiated_;
70 protected:
71 bool negotiated_;
74 class NET_EXPORT_PRIVATE QuicNegotiableUint32 : public QuicNegotiableValue {
75 public:
76 // Default and max values default to 0.
77 QuicNegotiableUint32(QuicTag name, QuicConfigPresence presence);
78 virtual ~QuicNegotiableUint32();
80 // Sets the maximum possible value that can be achieved after negotiation and
81 // also the default values to be assumed if PRESENCE_OPTIONAL and the *HLO msg
82 // doesn't contain a value corresponding to |name_|. |max| is serialised via
83 // ToHandshakeMessage call if |negotiated_| is false.
84 void set(uint32 max, uint32 default_value);
86 // Returns the value negotiated if |negotiated_| is true, otherwise returns
87 // default_value_ (used to set default values before negotiation finishes).
88 uint32 GetUint32() const;
90 // Serialises |name_| and value to |out|. If |negotiated_| is true then
91 // |negotiated_value_| is serialised, otherwise |max_value_| is serialised.
92 virtual void ToHandshakeMessage(CryptoHandshakeMessage* out) const OVERRIDE;
94 // Sets |negotiated_value_| to the minimum of |max_value_| and the
95 // corresponding value from |peer_hello|. If the corresponding value is
96 // missing and PRESENCE_OPTIONAL then |negotiated_value_| is set to
97 // |default_value_|.
98 virtual QuicErrorCode ProcessPeerHello(
99 const CryptoHandshakeMessage& peer_hello,
100 HelloType hello_type,
101 std::string* error_details) OVERRIDE;
103 private:
104 uint32 max_value_;
105 uint32 default_value_;
106 uint32 negotiated_value_;
109 class NET_EXPORT_PRIVATE QuicNegotiableTag : public QuicNegotiableValue {
110 public:
111 QuicNegotiableTag(QuicTag name, QuicConfigPresence presence);
112 virtual ~QuicNegotiableTag();
114 // Sets the possible values that |negotiated_tag_| can take after negotiation
115 // and the default value that |negotiated_tag_| takes if OPTIONAL and *HLO
116 // msg doesn't contain tag |name_|.
117 void set(const QuicTagVector& possible_values, QuicTag default_value);
119 // Returns the negotiated tag if |negotiated_| is true, otherwise returns
120 // |default_value_| (used to set default values before negotiation finishes).
121 QuicTag GetTag() const;
123 // Serialises |name_| and vector (either possible or negotiated) to |out|. If
124 // |negotiated_| is true then |negotiated_tag_| is serialised, otherwise
125 // |possible_values_| is serialised.
126 virtual void ToHandshakeMessage(CryptoHandshakeMessage* out) const OVERRIDE;
128 // Selects the tag common to both tags in |client_hello| for |name_| and
129 // |possible_values_| with preference to tag in |possible_values_|. The
130 // selected tag is set as |negotiated_tag_|.
131 virtual QuicErrorCode ProcessPeerHello(
132 const CryptoHandshakeMessage& peer_hello,
133 HelloType hello_type,
134 std::string* error_details) OVERRIDE;
136 private:
137 // Reads the vector corresponding to |name_| from |msg| into |out|. If the
138 // |name_| is absent in |msg| and |presence_| is set to OPTIONAL |out| is set
139 // to |possible_values_|.
140 QuicErrorCode ReadVector(const CryptoHandshakeMessage& msg,
141 const QuicTag** out,
142 size_t* out_length,
143 std::string* error_details) const;
145 QuicTag negotiated_tag_;
146 QuicTagVector possible_values_;
147 QuicTag default_value_;
150 // Stores uint32 from CHLO or SHLO messages that are not negotiated.
151 class NET_EXPORT_PRIVATE QuicFixedUint32 : public QuicConfigValue {
152 public:
153 QuicFixedUint32(QuicTag name, QuicConfigPresence presence);
154 virtual ~QuicFixedUint32();
156 bool HasSendValue() const;
158 uint32 GetSendValue() const;
160 void SetSendValue(uint32 value);
162 bool HasReceivedValue() const;
164 uint32 GetReceivedValue() const;
166 void SetReceivedValue(uint32 value);
168 // If has_send_value is true, serialises |tag_| and |send_value_| to |out|.
169 virtual void ToHandshakeMessage(CryptoHandshakeMessage* out) const OVERRIDE;
171 // Sets |value_| to the corresponding value from |peer_hello_| if it exists.
172 virtual QuicErrorCode ProcessPeerHello(
173 const CryptoHandshakeMessage& peer_hello,
174 HelloType hello_type,
175 std::string* error_details) OVERRIDE;
177 private:
178 uint32 send_value_;
179 bool has_send_value_;
180 uint32 receive_value_;
181 bool has_receive_value_;
184 // Stores tag from CHLO or SHLO messages that are not negotiated.
185 class NET_EXPORT_PRIVATE QuicFixedTag : public QuicConfigValue {
186 public:
187 QuicFixedTag(QuicTag name, QuicConfigPresence presence);
188 virtual ~QuicFixedTag();
190 bool HasSendValue() const;
192 QuicTag GetSendValue() const;
194 void SetSendValue(QuicTag value);
196 bool HasReceivedValue() const;
198 QuicTag GetReceivedValue() const;
200 void SetReceivedValue(QuicTag value);
202 // If has_send_value is true, serialises |tag_| and |send_value_| to |out|.
203 virtual void ToHandshakeMessage(CryptoHandshakeMessage* out) const OVERRIDE;
205 // Sets |value_| to the corresponding value from |client_hello_| if it exists.
206 virtual QuicErrorCode ProcessPeerHello(
207 const CryptoHandshakeMessage& peer_hello,
208 HelloType hello_type,
209 std::string* error_details) OVERRIDE;
211 private:
212 QuicTag send_value_;
213 bool has_send_value_;
214 QuicTag receive_value_;
215 bool has_receive_value_;
218 // QuicConfig contains non-crypto configuration options that are negotiated in
219 // the crypto handshake.
220 class NET_EXPORT_PRIVATE QuicConfig {
221 public:
222 QuicConfig();
223 ~QuicConfig();
225 void set_congestion_control(const QuicTagVector& congestion_control,
226 QuicTag default_congestion_control);
228 QuicTag congestion_control() const;
230 void SetLossDetectionToSend(QuicTag loss_detection);
232 bool HasReceivedLossDetection() const;
234 QuicTag ReceivedLossDetection() const;
236 void set_idle_connection_state_lifetime(
237 QuicTime::Delta max_idle_connection_state_lifetime,
238 QuicTime::Delta default_idle_conection_state_lifetime);
240 QuicTime::Delta idle_connection_state_lifetime() const;
242 QuicTime::Delta keepalive_timeout() const;
244 void set_max_streams_per_connection(size_t max_streams,
245 size_t default_streams);
247 uint32 max_streams_per_connection() const;
249 void set_max_time_before_crypto_handshake(
250 QuicTime::Delta max_time_before_crypto_handshake);
252 QuicTime::Delta max_time_before_crypto_handshake() const;
254 // Sets the peer's default initial congestion window in packets.
255 void SetInitialCongestionWindowToSend(size_t initial_window);
257 bool HasReceivedInitialCongestionWindow() const;
259 uint32 ReceivedInitialCongestionWindow() const;
261 // Sets an estimated initial round trip time in us.
262 void SetInitialRoundTripTimeUsToSend(size_t rtt_us);
264 bool HasReceivedInitialRoundTripTimeUs() const;
266 uint32 ReceivedInitialRoundTripTimeUs() const;
268 // Sets an initial flow control window size to transmit to the peer.
269 void SetInitialFlowControlWindowToSend(uint32 window_bytes);
271 bool HasReceivedInitialFlowControlWindowBytes() const;
273 uint32 ReceivedInitialFlowControlWindowBytes() const;
275 bool negotiated();
277 // SetDefaults sets the members to sensible, default values.
278 void SetDefaults();
280 // Enabled pacing.
281 void EnablePacing(bool enable_pacing);
283 // ToHandshakeMessage serialises the settings in this object as a series of
284 // tags /value pairs and adds them to |out|.
285 void ToHandshakeMessage(CryptoHandshakeMessage* out) const;
287 // Calls ProcessPeerHello on each negotiable parameter. On failure returns
288 // the corresponding QuicErrorCode and sets detailed error in |error_details|.
289 QuicErrorCode ProcessPeerHello(const CryptoHandshakeMessage& peer_hello,
290 HelloType hello_type,
291 std::string* error_details);
293 private:
294 friend class test::QuicConfigPeer;
296 // Congestion control feedback type.
297 QuicNegotiableTag congestion_control_;
298 // Loss detection feedback type.
299 QuicFixedTag loss_detection_;
300 // Idle connection state lifetime
301 QuicNegotiableUint32 idle_connection_state_lifetime_seconds_;
302 // Keepalive timeout, or 0 to turn off keepalive probes
303 QuicNegotiableUint32 keepalive_timeout_seconds_;
304 // Maximum number of streams that the connection can support.
305 QuicNegotiableUint32 max_streams_per_connection_;
306 // Maximum time till the session can be alive before crypto handshake is
307 // finished. (Not negotiated).
308 QuicTime::Delta max_time_before_crypto_handshake_;
309 // Initial congestion window in packets.
310 QuicFixedUint32 initial_congestion_window_;
311 // Initial round trip time estimate in microseconds.
312 QuicFixedUint32 initial_round_trip_time_us_;
313 // Initial flow control receive window in bytes.
314 QuicFixedUint32 initial_flow_control_window_bytes_;
317 } // namespace net
319 #endif // NET_QUIC_QUIC_CONFIG_H_