Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / http / http_server_properties.h
1 // Copyright (c) 2012 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 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_H_
7
8 #include <map>
9 #include <string>
10 #include "base/basictypes.h"
11 #include "base/containers/mru_cache.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_export.h"
16 #include "net/socket/next_proto.h"
17 #include "net/spdy/spdy_framer.h"  // TODO(willchan): Reconsider this.
18 #include "net/spdy/spdy_protocol.h"
19
20 namespace net {
21
22 enum AlternateProtocolUsage {
23   // Alternate Protocol was used without racing a normal connection.
24   ALTERNATE_PROTOCOL_USAGE_NO_RACE = 0,
25   // Alternate Protocol was used by winning a race with a normal connection.
26   ALTERNATE_PROTOCOL_USAGE_WON_RACE = 1,
27   // Alternate Protocol was not used by losing a race with a normal connection.
28   ALTERNATE_PROTOCOL_USAGE_LOST_RACE = 2,
29   // Alternate Protocol was not used because no Alternate-Protocol information
30   // was available when the request was issued, but an Alternate-Protocol header
31   // was present in the response.
32   ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING = 3,
33   // Alternate Protocol was not used because it was marked broken.
34   ALTERNATE_PROTOCOL_USAGE_BROKEN = 4,
35   // Maximum value for the enum.
36   ALTERNATE_PROTOCOL_USAGE_MAX,
37 };
38
39 // Log a histogram to reflect |usage|.
40 NET_EXPORT void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage);
41
42 enum BrokenAlternateProtocolLocation {
43   BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB = 0,
44   BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY = 1,
45   BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT = 2,
46   BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN = 3,
47   BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX,
48 };
49
50 // Log a histogram to reflect |location|.
51 NET_EXPORT void HistogramBrokenAlternateProtocolLocation(
52     BrokenAlternateProtocolLocation location);
53
54 enum AlternateProtocol {
55   DEPRECATED_NPN_SPDY_2 = 0,
56   ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION = DEPRECATED_NPN_SPDY_2,
57   NPN_SPDY_MINIMUM_VERSION = DEPRECATED_NPN_SPDY_2,
58   NPN_SPDY_3,
59   NPN_SPDY_3_1,
60   NPN_SPDY_4,  // SPDY4 is HTTP/2.
61   NPN_SPDY_MAXIMUM_VERSION = NPN_SPDY_4,
62   QUIC,
63   ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION = QUIC,
64   UNINITIALIZED_ALTERNATE_PROTOCOL,
65 };
66
67 // Simply returns whether |protocol| is between
68 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
69 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
70 NET_EXPORT bool IsAlternateProtocolValid(AlternateProtocol protocol);
71
72 enum AlternateProtocolSize {
73   NUM_VALID_ALTERNATE_PROTOCOLS =
74     ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION -
75     ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION + 1,
76 };
77
78 NET_EXPORT const char* AlternateProtocolToString(AlternateProtocol protocol);
79 NET_EXPORT AlternateProtocol AlternateProtocolFromString(
80     const std::string& str);
81 NET_EXPORT_PRIVATE AlternateProtocol AlternateProtocolFromNextProto(
82     NextProto next_proto);
83
84 struct NET_EXPORT AlternateProtocolInfo {
85   AlternateProtocolInfo(uint16 port,
86                         AlternateProtocol protocol,
87                         double probability)
88       : port(port),
89         protocol(protocol),
90         probability(probability),
91         is_broken(false) {}
92
93   AlternateProtocolInfo(uint16 port,
94                         AlternateProtocol protocol,
95                         double probability,
96                         bool is_broken)
97       : port(port),
98         protocol(protocol),
99         probability(probability),
100         is_broken(is_broken) {}
101
102   bool Equals(const AlternateProtocolInfo& other) const {
103     return port == other.port &&
104         protocol == other.protocol &&
105         probability == other.probability;
106   }
107
108   std::string ToString() const;
109
110   uint16 port;
111   AlternateProtocol protocol;
112   double probability;
113   bool is_broken;
114 };
115
116 struct NET_EXPORT SupportsQuic {
117   SupportsQuic() : used_quic(false) {}
118   SupportsQuic(bool used_quic, const std::string& address)
119       : used_quic(used_quic),
120         address(address) {}
121
122   bool Equals(const SupportsQuic& other) const {
123     return used_quic == other.used_quic && address == other.address;
124   }
125
126   bool used_quic;
127   std::string address;
128 };
129
130 typedef base::MRUCache<
131     HostPortPair, AlternateProtocolInfo> AlternateProtocolMap;
132 typedef base::MRUCache<HostPortPair, SettingsMap> SpdySettingsMap;
133 typedef std::map<HostPortPair, SupportsQuic> SupportsQuicMap;
134
135 extern const char kAlternateProtocolHeader[];
136
137 // The interface for setting/retrieving the HTTP server properties.
138 // Currently, this class manages servers':
139 // * SPDY support (based on NPN results)
140 // * Alternate-Protocol support
141 // * Spdy Settings (like CWND ID field)
142 class NET_EXPORT HttpServerProperties {
143  public:
144   struct NetworkStats {
145     base::TimeDelta srtt;
146     uint64 bandwidth_estimate;
147   };
148
149   HttpServerProperties() {}
150   virtual ~HttpServerProperties() {}
151
152   // Gets a weak pointer for this object.
153   virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() = 0;
154
155   // Deletes all data.
156   virtual void Clear() = 0;
157
158   // Returns true if |server| supports SPDY.
159   virtual bool SupportsSpdy(const HostPortPair& server) = 0;
160
161   // Add |server| into the persistent store. Should only be called from IO
162   // thread.
163   virtual void SetSupportsSpdy(const HostPortPair& server,
164                                bool support_spdy) = 0;
165
166   // Returns true if |server| has an Alternate-Protocol header.
167   virtual bool HasAlternateProtocol(const HostPortPair& server) = 0;
168
169   // Returns the Alternate-Protocol and port for |server|.
170   // HasAlternateProtocol(server) must be true.
171   virtual AlternateProtocolInfo GetAlternateProtocol(
172       const HostPortPair& server) = 0;
173
174   // Sets the Alternate-Protocol for |server|.
175   virtual void SetAlternateProtocol(const HostPortPair& server,
176                                     uint16 alternate_port,
177                                     AlternateProtocol alternate_protocol,
178                                     double probability) = 0;
179
180   // Sets the Alternate-Protocol for |server| to be BROKEN.
181   virtual void SetBrokenAlternateProtocol(const HostPortPair& server) = 0;
182
183   // Returns true if Alternate-Protocol for |server| was recently BROKEN.
184   virtual bool WasAlternateProtocolRecentlyBroken(
185       const HostPortPair& server) = 0;
186
187   // Confirms that Alternate-Protocol for |server| is working.
188   virtual void ConfirmAlternateProtocol(const HostPortPair& server) = 0;
189
190   // Clears the Alternate-Protocol for |server|.
191   virtual void ClearAlternateProtocol(const HostPortPair& server) = 0;
192
193   // Returns all Alternate-Protocol mappings.
194   virtual const AlternateProtocolMap& alternate_protocol_map() const = 0;
195
196   // Sets the threshold to be used when evaluating Alternate-Protocol
197   // advertisments. Only advertisements with a with a probability
198   // greater than |threshold| will be honored. |threshold| must be
199   // between 0 and 1 inclusive. Hence, a threshold of 0 implies that
200   // all advertisements will be honored.
201   virtual void SetAlternateProtocolProbabilityThreshold(
202       double threshold) = 0;
203
204   // Gets a reference to the SettingsMap stored for a host.
205   // If no settings are stored, returns an empty SettingsMap.
206   virtual const SettingsMap& GetSpdySettings(
207       const HostPortPair& host_port_pair) = 0;
208
209   // Saves an individual SPDY setting for a host. Returns true if SPDY setting
210   // is to be persisted.
211   virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
212                               SpdySettingsIds id,
213                               SpdySettingsFlags flags,
214                               uint32 value) = 0;
215
216   // Clears all SPDY settings for a host.
217   virtual void ClearSpdySettings(const HostPortPair& host_port_pair) = 0;
218
219   // Clears all SPDY settings for all hosts.
220   virtual void ClearAllSpdySettings() = 0;
221
222   // Returns all persistent SPDY settings.
223   virtual const SpdySettingsMap& spdy_settings_map() const = 0;
224
225   // TODO(rtenneti): Make SupportsQuic a global (instead of per host_port_pair).
226   virtual SupportsQuic GetSupportsQuic(
227       const HostPortPair& host_port_pair) const = 0;
228
229   virtual void SetSupportsQuic(const HostPortPair& host_port_pair,
230                                bool used_quic,
231                                const std::string& address) = 0;
232
233   virtual const SupportsQuicMap& supports_quic_map() const = 0;
234
235   virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
236                                      NetworkStats stats) = 0;
237
238   virtual const NetworkStats* GetServerNetworkStats(
239       const HostPortPair& host_port_pair) const = 0;
240
241  private:
242   DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
243 };
244
245 }  // namespace net
246
247 #endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_H_