Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / net / http / http_server_properties_impl.h
blob7b6fa0ac995e23b5a493970008f22f5f4fdeb0bc
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_
8 #include <deque>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "base/values.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/linked_hash_map.h"
21 #include "net/base/net_export.h"
22 #include "net/http/http_server_properties.h"
24 namespace base {
25 class ListValue;
28 namespace BASE_HASH_NAMESPACE {
30 template <>
31 struct hash<net::AlternativeService> {
32 size_t operator()(const net::AlternativeService& entry) const {
33 return entry.protocol ^ hash<std::string>()(entry.host) ^ entry.port;
37 } // namespace BASE_HASH_NAMESPACE
39 namespace net {
41 // The implementation for setting/retrieving the HTTP server properties.
42 class NET_EXPORT HttpServerPropertiesImpl
43 : public HttpServerProperties,
44 NON_EXPORTED_BASE(public base::NonThreadSafe) {
45 public:
46 HttpServerPropertiesImpl();
47 ~HttpServerPropertiesImpl() override;
49 // Initializes |spdy_servers_map_| with the servers (host/port) from
50 // |spdy_servers| that either support SPDY or not.
51 void InitializeSpdyServers(std::vector<std::string>* spdy_servers,
52 bool support_spdy);
54 void InitializeAlternativeServiceServers(
55 AlternativeServiceMap* alternate_protocol_servers);
57 void InitializeSpdySettingsServers(SpdySettingsMap* spdy_settings_map);
59 void InitializeSupportsQuic(IPAddressNumber* last_address);
61 void InitializeServerNetworkStats(
62 ServerNetworkStatsMap* server_network_stats_map);
64 // Get the list of servers (host/port) that support SPDY. The max_size is the
65 // number of MRU servers that support SPDY that are to be returned.
66 void GetSpdyServerList(base::ListValue* spdy_server_list,
67 size_t max_size) const;
69 // Returns flattened string representation of the |host_port_pair|. Used by
70 // unittests.
71 static std::string GetFlattenedSpdyServer(const HostPortPair& host_port_pair);
73 // Returns the canonical host suffix for |host|, or std::string() if none
74 // exists.
75 std::string GetCanonicalSuffix(const std::string& host);
77 // -----------------------------
78 // HttpServerProperties methods:
79 // -----------------------------
81 base::WeakPtr<HttpServerProperties> GetWeakPtr() override;
82 void Clear() override;
83 bool SupportsRequestPriority(const HostPortPair& server) override;
84 bool GetSupportsSpdy(const HostPortPair& server) override;
85 void SetSupportsSpdy(const HostPortPair& server, bool support_spdy) override;
86 bool RequiresHTTP11(const HostPortPair& server) override;
87 void SetHTTP11Required(const HostPortPair& server) override;
88 void MaybeForceHTTP11(const HostPortPair& server,
89 SSLConfig* ssl_config) override;
90 AlternativeServiceVector GetAlternativeServices(
91 const HostPortPair& origin) override;
92 bool SetAlternativeService(const HostPortPair& origin,
93 const AlternativeService& alternative_service,
94 double alternative_probability,
95 base::Time expiration) override;
96 bool SetAlternativeServices(const HostPortPair& origin,
97 const AlternativeServiceInfoVector&
98 alternative_service_info_vector) override;
99 void MarkAlternativeServiceBroken(
100 const AlternativeService& alternative_service) override;
101 void MarkAlternativeServiceRecentlyBroken(
102 const AlternativeService& alternative_service) override;
103 bool IsAlternativeServiceBroken(
104 const AlternativeService& alternative_service) const override;
105 bool WasAlternativeServiceRecentlyBroken(
106 const AlternativeService& alternative_service) override;
107 void ConfirmAlternativeService(
108 const AlternativeService& alternative_service) override;
109 void ClearAlternativeServices(const HostPortPair& origin) override;
110 const AlternativeServiceMap& alternative_service_map() const override;
111 scoped_ptr<base::Value> GetAlternativeServiceInfoAsValue() const override;
112 void SetAlternativeServiceProbabilityThreshold(double threshold) override;
113 const SettingsMap& GetSpdySettings(
114 const HostPortPair& host_port_pair) override;
115 bool SetSpdySetting(const HostPortPair& host_port_pair,
116 SpdySettingsIds id,
117 SpdySettingsFlags flags,
118 uint32 value) override;
119 void ClearSpdySettings(const HostPortPair& host_port_pair) override;
120 void ClearAllSpdySettings() override;
121 const SpdySettingsMap& spdy_settings_map() const override;
122 bool GetSupportsQuic(IPAddressNumber* last_address) const override;
123 void SetSupportsQuic(bool used_quic, const IPAddressNumber& address) override;
124 void SetServerNetworkStats(const HostPortPair& host_port_pair,
125 ServerNetworkStats stats) override;
126 const ServerNetworkStats* GetServerNetworkStats(
127 const HostPortPair& host_port_pair) override;
128 const ServerNetworkStatsMap& server_network_stats_map() const override;
130 private:
131 friend class HttpServerPropertiesImplPeer;
133 // |spdy_servers_map_| has flattened representation of servers (host, port)
134 // that either support or not support SPDY protocol.
135 typedef base::MRUCache<std::string, bool> SpdyServerHostPortMap;
136 typedef std::map<HostPortPair, HostPortPair> CanonicalHostMap;
137 typedef std::vector<std::string> CanonicalSufficList;
138 typedef std::set<HostPortPair> Http11ServerHostPortSet;
140 // Linked hash map from AlternativeService to expiration time. This container
141 // is a queue with O(1) enqueue and dequeue, and a hash_map with O(1) lookup
142 // at the same time.
143 typedef linked_hash_map<AlternativeService, base::TimeTicks>
144 BrokenAlternativeServices;
145 // Map to the number of times each alternative service has been marked broken.
146 typedef std::map<AlternativeService, int> RecentlyBrokenAlternativeServices;
148 // Return the iterator for |server|, or for its canonical host, or end.
149 AlternativeServiceMap::const_iterator GetAlternateProtocolIterator(
150 const HostPortPair& server);
152 // Return the canonical host for |server|, or end if none exists.
153 CanonicalHostMap::const_iterator GetCanonicalHost(HostPortPair server) const;
155 void RemoveCanonicalHost(const HostPortPair& server);
156 void ExpireBrokenAlternateProtocolMappings();
157 void ScheduleBrokenAlternateProtocolMappingsExpiration();
159 SpdyServerHostPortMap spdy_servers_map_;
160 Http11ServerHostPortSet http11_servers_;
162 AlternativeServiceMap alternative_service_map_;
163 BrokenAlternativeServices broken_alternative_services_;
164 // Class invariant: Every alternative service in broken_alternative_services_
165 // must also be in recently_broken_alternative_services_.
166 RecentlyBrokenAlternativeServices recently_broken_alternative_services_;
168 IPAddressNumber last_quic_address_;
169 SpdySettingsMap spdy_settings_map_;
170 ServerNetworkStatsMap server_network_stats_map_;
171 // Contains a map of servers which could share the same alternate protocol.
172 // Map from a Canonical host/port (host is some postfix of host names) to an
173 // actual origin, which has a plausible alternate protocol mapping.
174 CanonicalHostMap canonical_host_to_origin_map_;
175 // Contains list of suffixes (for exmaple ".c.youtube.com",
176 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
177 CanonicalSufficList canonical_suffixes_;
179 double alternative_service_probability_threshold_;
181 base::WeakPtrFactory<HttpServerPropertiesImpl> weak_ptr_factory_;
183 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl);
186 } // namespace net
188 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_