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.
5 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
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"
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
) {
34 HttpServerPropertiesImpl();
35 virtual ~HttpServerPropertiesImpl();
37 // Initializes |spdy_servers_table_| with the servers (host/port) from
38 // |spdy_servers| that either support SPDY or not.
39 void InitializeSpdyServers(std::vector
<std::string
>* spdy_servers
,
42 void InitializeAlternateProtocolServers(
43 AlternateProtocolMap
* alternate_protocol_servers
);
45 void InitializeSpdySettingsServers(SpdySettingsMap
* spdy_settings_map
);
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
);
52 // Get the list of servers (host/port) that support SPDY.
53 void GetSpdyServerList(base::ListValue
* spdy_server_list
) const;
55 // Returns flattened string representation of the |host_port_pair|. Used by
57 static std::string
GetFlattenedSpdyServer(
58 const net::HostPortPair
& host_port_pair
);
60 // Debugging to simulate presence of an AlternateProtocol.
61 // If we don't have an alternate protocol in the map for any given host/port
62 // pair, force this ProtocolPortPair.
63 static void ForceAlternateProtocol(const PortAlternateProtocolPair
& pair
);
64 static void DisableForcedAlternateProtocol();
66 // Changes the number of host/port pairs we remember pipelining capability
67 // for. A larger number means we're more likely to be able to pipeline
68 // immediately if a host is known good, but uses more memory. This function
69 // can only be called if |pipeline_capability_map_| is empty.
70 void SetNumPipelinedHostsToRemember(int max_size
);
72 // -----------------------------
73 // HttpServerProperties methods:
74 // -----------------------------
76 // Gets a weak pointer for this object.
77 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() OVERRIDE
;
80 virtual void Clear() OVERRIDE
;
82 // Returns true if |server| supports SPDY.
83 virtual bool SupportsSpdy(const HostPortPair
& server
) const OVERRIDE
;
85 // Add |server| into the persistent store.
86 virtual void SetSupportsSpdy(const HostPortPair
& server
,
87 bool support_spdy
) OVERRIDE
;
89 // Returns true if |server| has an Alternate-Protocol header.
90 virtual bool HasAlternateProtocol(const HostPortPair
& server
) const OVERRIDE
;
92 // Returns the Alternate-Protocol and port for |server|.
93 // HasAlternateProtocol(server) must be true.
94 virtual PortAlternateProtocolPair
GetAlternateProtocol(
95 const HostPortPair
& server
) const OVERRIDE
;
97 // Sets the Alternate-Protocol for |server|.
98 virtual void SetAlternateProtocol(
99 const HostPortPair
& server
,
100 uint16 alternate_port
,
101 AlternateProtocol alternate_protocol
) OVERRIDE
;
103 // Sets the Alternate-Protocol for |server| to be BROKEN.
104 virtual void SetBrokenAlternateProtocol(const HostPortPair
& server
) OVERRIDE
;
106 // Returns all Alternate-Protocol mappings.
107 virtual const AlternateProtocolMap
& alternate_protocol_map() const OVERRIDE
;
109 // Gets a reference to the SettingsMap stored for a host.
110 // If no settings are stored, returns an empty SettingsMap.
111 virtual const SettingsMap
& GetSpdySettings(
112 const HostPortPair
& host_port_pair
) const OVERRIDE
;
114 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
115 // is to be persisted.
116 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
118 SpdySettingsFlags flags
,
119 uint32 value
) OVERRIDE
;
121 // Clears all entries in |spdy_settings_map_| for a host.
122 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) OVERRIDE
;
124 // Clears all entries in |spdy_settings_map_|.
125 virtual void ClearAllSpdySettings() OVERRIDE
;
127 // Returns all persistent SPDY settings.
128 virtual const SpdySettingsMap
& spdy_settings_map() const OVERRIDE
;
130 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
131 NetworkStats stats
) OVERRIDE
;
133 virtual const NetworkStats
* GetServerNetworkStats(
134 const HostPortPair
& host_port_pair
) const OVERRIDE
;
136 virtual HttpPipelinedHostCapability
GetPipelineCapability(
137 const HostPortPair
& origin
) OVERRIDE
;
139 virtual void SetPipelineCapability(
140 const HostPortPair
& origin
,
141 HttpPipelinedHostCapability capability
) OVERRIDE
;
143 virtual void ClearPipelineCapabilities() OVERRIDE
;
145 virtual PipelineCapabilityMap
GetPipelineCapabilityMap() const OVERRIDE
;
148 typedef base::MRUCache
<
149 HostPortPair
, HttpPipelinedHostCapability
> CachedPipelineCapabilityMap
;
150 // |spdy_servers_table_| has flattened representation of servers (host/port
151 // pair) that either support or not support SPDY protocol.
152 typedef base::hash_map
<std::string
, bool> SpdyServerHostPortTable
;
153 typedef std::map
<HostPortPair
, NetworkStats
> ServerNetworkStatsMap
;
154 typedef std::map
<HostPortPair
, HostPortPair
> CanonicalHostMap
;
155 typedef std::vector
<std::string
> CanonicalSufficList
;
157 // Return the canonical host for |server|, or end if none exists.
158 CanonicalHostMap::const_iterator
GetCanonicalHost(HostPortPair server
) const;
160 SpdyServerHostPortTable spdy_servers_table_
;
162 AlternateProtocolMap alternate_protocol_map_
;
163 SpdySettingsMap spdy_settings_map_
;
164 ServerNetworkStatsMap server_network_stats_map_
;
165 scoped_ptr
<CachedPipelineCapabilityMap
> pipeline_capability_map_
;
166 // Contains a map of servers which could share the same alternate protocol.
167 // Map from a Canonical host/port (host is some postfix of host names) to an
168 // actual origin, which has a plausible alternate protocol mapping.
169 CanonicalHostMap canonical_host_to_origin_map_
;
170 // Contains list of suffixes (for exmaple ".c.youtube.com",
171 // ".googlevideo.com") of canoncial hostnames.
172 CanonicalSufficList canoncial_suffixes_
;
174 base::WeakPtrFactory
<HttpServerPropertiesImpl
> weak_ptr_factory_
;
176 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl
);
181 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_