Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / http / http_server_properties_impl.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_IMPL_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/containers/mru_cache.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/values.h"
18 #include "net/base/host_port_pair.h"
19 #include "net/base/net_export.h"
20 #include "net/http/http_pipelined_host_capability.h"
21 #include "net/http/http_server_properties.h"
22
23 namespace base {
24 class ListValue;
25 }
26
27 namespace net {
28
29 // The implementation for setting/retrieving the HTTP server properties.
30 class NET_EXPORT HttpServerPropertiesImpl
31     : public HttpServerProperties,
32       NON_EXPORTED_BASE(public base::NonThreadSafe) {
33  public:
34   HttpServerPropertiesImpl();
35   virtual ~HttpServerPropertiesImpl();
36
37   // Initializes |spdy_servers_map_| with the servers (host/port) from
38   // |spdy_servers| that either support SPDY or not.
39   void InitializeSpdyServers(std::vector<std::string>* spdy_servers,
40                              bool support_spdy);
41
42   void InitializeAlternateProtocolServers(
43       AlternateProtocolMap* alternate_protocol_servers);
44
45   void InitializeSpdySettingsServers(SpdySettingsMap* spdy_settings_map);
46
47   // Initializes |pipeline_capability_map_| with the servers (host/port) from
48   // |pipeline_capability_map| that either support HTTP pipelining or not.
49   void InitializePipelineCapabilities(
50       const PipelineCapabilityMap* pipeline_capability_map);
51
52   // Get the list of servers (host/port) that support SPDY. The max_size is the
53   // number of MRU servers that support SPDY that are to be returned.
54   void GetSpdyServerList(base::ListValue* spdy_server_list,
55                          size_t max_size) const;
56
57   // Returns flattened string representation of the |host_port_pair|. Used by
58   // unittests.
59   static std::string GetFlattenedSpdyServer(
60       const net::HostPortPair& host_port_pair);
61
62   // Debugging to simulate presence of an AlternateProtocol.
63   // If we don't have an alternate protocol in the map for any given host/port
64   // pair, force this ProtocolPortPair.
65   static void ForceAlternateProtocol(const PortAlternateProtocolPair& pair);
66   static void DisableForcedAlternateProtocol();
67
68   // Changes the number of host/port pairs we remember pipelining capability
69   // for. A larger number means we're more likely to be able to pipeline
70   // immediately if a host is known good, but uses more memory. This function
71   // can only be called if |pipeline_capability_map_| is empty.
72   void SetNumPipelinedHostsToRemember(int max_size);
73
74   // -----------------------------
75   // HttpServerProperties methods:
76   // -----------------------------
77
78   // Gets a weak pointer for this object.
79   virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE;
80
81   // Deletes all data.
82   virtual void Clear() OVERRIDE;
83
84   // Returns true if |server| supports SPDY.
85   virtual bool SupportsSpdy(const HostPortPair& server) OVERRIDE;
86
87   // Add |server| into the persistent store.
88   virtual void SetSupportsSpdy(const HostPortPair& server,
89                                bool support_spdy) OVERRIDE;
90
91   // Returns true if |server| has an Alternate-Protocol header.
92   virtual bool HasAlternateProtocol(const HostPortPair& server) OVERRIDE;
93
94   // Returns the Alternate-Protocol and port for |server|.
95   // HasAlternateProtocol(server) must be true.
96   virtual PortAlternateProtocolPair GetAlternateProtocol(
97       const HostPortPair& server) OVERRIDE;
98
99   // Sets the Alternate-Protocol for |server|.
100   virtual void SetAlternateProtocol(
101       const HostPortPair& server,
102       uint16 alternate_port,
103       AlternateProtocol alternate_protocol) OVERRIDE;
104
105   // Sets the Alternate-Protocol for |server| to be BROKEN.
106   virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE;
107
108   // Returns true if Alternate-Protocol for |server| was recently BROKEN.
109   virtual bool WasAlternateProtocolRecentlyBroken(
110       const HostPortPair& server) OVERRIDE;
111
112   // Confirms that Alternate-Protocol for |server| is working.
113   virtual void ConfirmAlternateProtocol(const HostPortPair& server) OVERRIDE;
114
115   // Clears the Alternate-Protocol for |server|.
116   virtual void ClearAlternateProtocol(const HostPortPair& server) OVERRIDE;
117
118   // Returns all Alternate-Protocol mappings.
119   virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE;
120
121   // Gets a reference to the SettingsMap stored for a host.
122   // If no settings are stored, returns an empty SettingsMap.
123   virtual const SettingsMap& GetSpdySettings(
124       const HostPortPair& host_port_pair) OVERRIDE;
125
126   // Saves an individual SPDY setting for a host. Returns true if SPDY setting
127   // is to be persisted.
128   virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
129                               SpdySettingsIds id,
130                               SpdySettingsFlags flags,
131                               uint32 value) OVERRIDE;
132
133   // Clears all entries in |spdy_settings_map_| for a host.
134   virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE;
135
136   // Clears all entries in |spdy_settings_map_|.
137   virtual void ClearAllSpdySettings() OVERRIDE;
138
139   // Returns all persistent SPDY settings.
140   virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE;
141
142   virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
143                                      NetworkStats stats) OVERRIDE;
144
145   virtual const NetworkStats* GetServerNetworkStats(
146       const HostPortPair& host_port_pair) const OVERRIDE;
147
148   virtual HttpPipelinedHostCapability GetPipelineCapability(
149       const HostPortPair& origin) OVERRIDE;
150
151   virtual void SetPipelineCapability(
152       const HostPortPair& origin,
153       HttpPipelinedHostCapability capability) OVERRIDE;
154
155   virtual void ClearPipelineCapabilities() OVERRIDE;
156
157   virtual PipelineCapabilityMap GetPipelineCapabilityMap() const OVERRIDE;
158
159  private:
160   typedef base::MRUCache<
161       HostPortPair, HttpPipelinedHostCapability> CachedPipelineCapabilityMap;
162   // |spdy_servers_map_| has flattened representation of servers (host, port)
163   // that either support or not support SPDY protocol.
164   typedef base::MRUCache<std::string, bool> SpdyServerHostPortMap;
165   typedef std::map<HostPortPair, NetworkStats> ServerNetworkStatsMap;
166   typedef std::map<HostPortPair, HostPortPair> CanonicalHostMap;
167   typedef std::vector<std::string> CanonicalSufficList;
168   // List of broken host:ports and the times when they can be expired.
169   struct BrokenAlternateProtocolEntry {
170     HostPortPair server;
171     base::TimeTicks when;
172   };
173   typedef std::list<BrokenAlternateProtocolEntry>
174       BrokenAlternateProtocolList;
175   // Map from host:port to the number of times alternate protocol has
176   // been marked broken.
177   typedef std::map<HostPortPair, int> BrokenAlternateProtocolMap;
178
179   // Return the canonical host for |server|, or end if none exists.
180   CanonicalHostMap::const_iterator GetCanonicalHost(HostPortPair server) const;
181
182   void ExpireBrokenAlternateProtocolMappings();
183   void ScheduleBrokenAlternateProtocolMappingsExpiration();
184
185   SpdyServerHostPortMap spdy_servers_map_;
186
187   AlternateProtocolMap alternate_protocol_map_;
188   BrokenAlternateProtocolList broken_alternate_protocol_list_;
189   BrokenAlternateProtocolMap broken_alternate_protocol_map_;
190
191   SpdySettingsMap spdy_settings_map_;
192   ServerNetworkStatsMap server_network_stats_map_;
193   scoped_ptr<CachedPipelineCapabilityMap> pipeline_capability_map_;
194   // Contains a map of servers which could share the same alternate protocol.
195   // Map from a Canonical host/port (host is some postfix of host names) to an
196   // actual origin, which has a plausible alternate protocol mapping.
197   CanonicalHostMap canonical_host_to_origin_map_;
198   // Contains list of suffixes (for exmaple ".c.youtube.com",
199   // ".googlevideo.com") of canoncial hostnames.
200   CanonicalSufficList canoncial_suffixes_;
201
202   base::WeakPtrFactory<HttpServerPropertiesImpl> weak_ptr_factory_;
203
204   DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl);
205 };
206
207 }  // namespace net
208
209 #endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_