blob: 9cfa07e0c4e93839ad025258cc637a4218a21ed6 [file] [log] [blame]
[email protected]39c48fc2012-03-12 18:42:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]db96a882011-10-09 02:01:542// 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
Matt Menke609160742019-08-02 18:47:268#include <stddef.h>
9#include <stdint.h>
10
[email protected]17291a022011-10-10 07:32:5311#include <map>
Matt Menke609160742019-08-02 18:47:2612#include <memory>
13#include <set>
[email protected]db96a882011-10-09 02:01:5414#include <string>
jsbellcea42a52015-11-30 23:50:2515#include <tuple>
bncd9b132e2015-07-08 05:16:1016#include <vector>
17
Matt Menkedce50562017-12-19 22:12:2018#include "base/callback.h"
[email protected]9801e3702014-03-07 09:33:5519#include "base/containers/mru_cache.h"
Avi Drissman13fc8932015-12-20 04:40:4620#include "base/macros.h"
Matt Menke609160742019-08-02 18:47:2621#include "base/memory/weak_ptr.h"
Matt Menke5e7dcd32019-08-09 22:25:2122#include "base/optional.h"
Matt Menke609160742019-08-02 18:47:2623#include "base/threading/thread_checker.h"
[email protected]3b8cf7f2014-01-27 22:08:5124#include "base/time/time.h"
Matt Menke609160742019-08-02 18:47:2625#include "base/timer/timer.h"
26#include "base/values.h"
27#include "net/base/host_port_pair.h"
28#include "net/base/ip_address.h"
[email protected]db96a882011-10-09 02:01:5429#include "net/base/net_export.h"
Matt Menke1be93d22019-08-20 16:57:5830#include "net/base/network_isolation_key.h"
Matt Menke2890796e2019-08-02 16:55:2331#include "net/http/alternative_service.h"
Matt Menke609160742019-08-02 18:47:2632#include "net/http/broken_alternative_services.h"
33#include "net/http/http_server_properties.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5134#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
35#include "net/third_party/quiche/src/quic/core/quic_server_id.h"
36#include "net/third_party/quiche/src/quic/core/quic_versions.h"
Victor Vasiliev27cc7712019-01-24 11:50:1437#include "net/third_party/quiche/src/spdy/core/spdy_framer.h" // TODO(willchan): Reconsider this.
38#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
zhongyi3d4a55e72016-04-22 20:36:4639#include "url/scheme_host_port.h"
[email protected]db96a882011-10-09 02:01:5440
bnc8ba74a1782015-04-14 17:42:0841namespace base {
Matt Menke609160742019-08-02 18:47:2642class Clock;
43class TickClock;
bnc8ba74a1782015-04-14 17:42:0844class Value;
45}
46
[email protected]db96a882011-10-09 02:01:5447namespace net {
48
Matt Menke2890796e2019-08-02 16:55:2349class HostPortPair;
Matt Menke609160742019-08-02 18:47:2650class HttpServerPropertiesManager;
martijnfe9636e2016-02-06 14:33:3251class IPAddress;
Matt Menke609160742019-08-02 18:47:2652class NetLog;
bncfacdd852015-01-09 19:22:5453struct SSLConfig;
54
rtenneti1c863aa2014-09-25 18:39:3355struct NET_EXPORT SupportsQuic {
56 SupportsQuic() : used_quic(false) {}
57 SupportsQuic(bool used_quic, const std::string& address)
Matt Menke609160742019-08-02 18:47:2658 : used_quic(used_quic), address(address) {}
rtenneti1c863aa2014-09-25 18:39:3359
60 bool Equals(const SupportsQuic& other) const {
61 return used_quic == other.used_quic && address == other.address;
62 }
63
64 bool used_quic;
65 std::string address;
66};
67
rtenneti338cd36a2015-01-06 00:20:0768struct NET_EXPORT ServerNetworkStats {
Ryan Hamilton8d9ee76e2018-05-29 23:52:5269 ServerNetworkStats() : bandwidth_estimate(quic::QuicBandwidth::Zero()) {}
rtenneti338cd36a2015-01-06 00:20:0770
rtenneticce34d52015-06-05 23:36:2971 bool operator==(const ServerNetworkStats& other) const {
72 return srtt == other.srtt && bandwidth_estimate == other.bandwidth_estimate;
73 }
74
75 bool operator!=(const ServerNetworkStats& other) const {
76 return !this->operator==(other);
77 }
78
rtenneti338cd36a2015-01-06 00:20:0779 base::TimeDelta srtt;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5280 quic::QuicBandwidth bandwidth_estimate;
rtenneti338cd36a2015-01-06 00:20:0781};
82
bncd9b132e2015-07-08 05:16:1083typedef std::vector<AlternativeService> AlternativeServiceVector;
84typedef std::vector<AlternativeServiceInfo> AlternativeServiceInfoVector;
Yixin Wang4a227aa22017-11-30 21:33:0185
Yixin Wang4a227aa22017-11-30 21:33:0186// Store at most 200 MRU RecentlyBrokenAlternativeServices in memory and disk.
Matt Menke2890796e2019-08-02 16:55:2387// This ideally would be with the other constants in HttpServerProperties, but
88// has to go here instead of prevent a circular dependency.
Yixin Wang4a227aa22017-11-30 21:33:0189const int kMaxRecentlyBrokenAlternativeServiceEntries = 200;
90
91// Store at most 5 MRU QUIC servers by default. This is mainly used by cronet.
92const int kDefaultMaxQuicServerEntries = 5;
93
Yixin Wang4a227aa22017-11-30 21:33:0194// Max number of quic servers to store is not hardcoded and can be set.
95// Because of this, QuicServerInfoMap will not be a subclass of MRUCache.
Ryan Hamilton8d9ee76e2018-05-29 23:52:5296typedef base::MRUCache<quic::QuicServerId, std::string> QuicServerInfoMap;
[email protected]17291a022011-10-10 07:32:5397
[email protected]db96a882011-10-09 02:01:5498// The interface for setting/retrieving the HTTP server properties.
[email protected]17291a022011-10-10 07:32:5399// Currently, this class manages servers':
Bence Békyffb237f2017-06-29 12:17:39100// * HTTP/2 support;
101// * Alternative Service support;
rtenneticd2aaa15b2015-10-10 20:29:33102// * QUIC data (like ServerNetworkStats and QuicServerInfo).
103//
Matt Menkefe1f1c82019-08-20 17:49:11104// Optionally retrieves and saves properties from/to disk. This class is not
105// threadsafe.
Matt Menke609160742019-08-02 18:47:26106class NET_EXPORT HttpServerProperties
107 : public BrokenAlternativeServices::Delegate {
[email protected]db96a882011-10-09 02:01:54108 public:
Matt Menke5e7dcd32019-08-09 22:25:21109 // Store at most 500 MRU ServerInfos in memory and disk.
110 static const int kMaxServerInfoEntries = 500;
111
Matt Menke609160742019-08-02 18:47:26112 // Provides an interface to interact with persistent preferences storage
113 // implemented by the embedder. The prefs are assumed not to have been loaded
114 // before HttpServerPropertiesManager construction.
115 class NET_EXPORT PrefDelegate {
116 public:
117 virtual ~PrefDelegate();
118
119 // Returns the branch of the preferences system for the server properties.
120 // Returns nullptr if the pref system has no data for the server properties.
121 virtual const base::DictionaryValue* GetServerProperties() const = 0;
122
123 // Sets the server properties to the given value. If |callback| is
124 // non-empty, flushes data to persistent storage and invokes |callback|
125 // asynchronously when complete.
126 virtual void SetServerProperties(const base::DictionaryValue& value,
127 base::OnceClosure callback) = 0;
128
129 // Starts listening for prefs to be loaded. If prefs are already loaded,
130 // |pref_loaded_callback| will be invoked asynchronously. Callback will be
131 // invoked even if prefs fail to load. Will only be called once by the
132 // HttpServerPropertiesManager.
133 virtual void WaitForPrefLoad(base::OnceClosure pref_loaded_callback) = 0;
134 };
135
Matt Menke5e7dcd32019-08-09 22:25:21136 // Contains metadata about a particular server.
137 struct NET_EXPORT ServerInfo {
138 ServerInfo();
139 ServerInfo(const ServerInfo& server_info);
140 ServerInfo(ServerInfo&& server_info);
141 ~ServerInfo();
142
Matt Menkefe9b5962019-08-14 20:56:14143 // Returns true if no fields are populated.
144 bool empty() const;
145
Matt Menke1be93d22019-08-20 16:57:58146 // Used in tests.
147 bool operator==(const ServerInfo& other) const;
148
Matt Menke5e7dcd32019-08-09 22:25:21149 // IMPORTANT: When adding a field here, be sure to update
150 // HttpServerProperties::OnServerInfoLoaded() as well as
151 // HttpServerPropertiesManager to correctly load/save the from/to the pref
152 // store.
153
154 // Whether or not a server is known to support H2/SPDY. False indicates
155 // known lack of support, true indicates known support, and not set
156 // indicates unknown. The difference between false and not set only matters
157 // when loading from disk, when an initialized false value will take
158 // priority over a not set value.
159 base::Optional<bool> supports_spdy;
160
Matt Menkefe9b5962019-08-14 20:56:14161 base::Optional<AlternativeServiceInfoVector> alternative_services;
Matt Menke86878a62019-08-14 21:01:11162 base::Optional<ServerNetworkStats> server_network_stats;
163
164 // TODO(mmenke): Add other per-server data as well
165 // (Http11ServerHostPortSet, QUIC server info).
Matt Menke5e7dcd32019-08-09 22:25:21166 };
167
Matt Menke1be93d22019-08-20 16:57:58168 struct NET_EXPORT ServerInfoMapKey {
169 // If |use_network_isolation_key| is false, an empty NetworkIsolationKey is
170 // used instead of |network_isolation_key|.
171 ServerInfoMapKey(const url::SchemeHostPort& server,
172 const NetworkIsolationKey& network_isolation_key,
173 bool use_network_isolation_key);
174 ~ServerInfoMapKey();
175
176 bool operator<(const ServerInfoMapKey& other) const;
177
178 url::SchemeHostPort server;
179 NetworkIsolationKey network_isolation_key;
180 };
181
Matt Menke5e7dcd32019-08-09 22:25:21182 class NET_EXPORT ServerInfoMap
Matt Menke1be93d22019-08-20 16:57:58183 : public base::MRUCache<ServerInfoMapKey, ServerInfo> {
Matt Menke5e7dcd32019-08-09 22:25:21184 public:
185 ServerInfoMap();
186
187 // If there's an entry corresponding to |key|, brings that entry to the
188 // front and returns an iterator to it. Otherwise, inserts an empty
189 // ServerInfo using |key|, and returns an iterator to it.
Matt Menke1be93d22019-08-20 16:57:58190 iterator GetOrPut(const ServerInfoMapKey& key);
Matt Menke5e7dcd32019-08-09 22:25:21191
Matt Menkefe9b5962019-08-14 20:56:14192 // Erases the ServerInfo identified by |server_info_it| if no fields have
193 // data. The iterator must point to an entry in the map. Regardless of
194 // whether the entry is removed or not, returns iterator for the next entry.
195 iterator EraseIfEmpty(iterator server_info_it);
196
Matt Menke5e7dcd32019-08-09 22:25:21197 private:
198 DISALLOW_COPY_AND_ASSIGN(ServerInfoMap);
199 };
200
Matt Menke609160742019-08-02 18:47:26201 // If a |pref_delegate| is specified, it will be used to read/write the
202 // properties to a pref file. Writes are rate limited to improve performance.
203 //
204 // |tick_clock| is used for setting expiration times and scheduling the
205 // expiration of broken alternative services. If null, default clock will be
206 // used.
207 //
208 // |clock| is used for converting base::TimeTicks to base::Time for
209 // wherever base::Time is preferable.
210 HttpServerProperties(std::unique_ptr<PrefDelegate> pref_delegate = nullptr,
211 NetLog* net_log = nullptr,
212 const base::TickClock* tick_clock = nullptr,
213 base::Clock* clock = nullptr);
214
215 ~HttpServerProperties() override;
[email protected]db96a882011-10-09 02:01:54216
Matt Menkedce50562017-12-19 22:12:20217 // Deletes all data. If |callback| is non-null, flushes data to disk
218 // and invokes the callback asynchronously once changes have been written to
219 // disk.
Matt Menke609160742019-08-02 18:47:26220 void Clear(base::OnceClosure callback);
[email protected]17291a022011-10-10 07:32:53221
Matt Menkefe1f1c82019-08-20 17:49:11222 // Returns true if |server|, in the context of |network_isolation_key|, has
223 // previously supported a network protocol which honors request
224 // prioritization.
225 //
rdsmithc31e0602016-08-30 06:27:23226 // Note that this also implies that the server supports request
227 // multiplexing, since priorities imply a relationship between
228 // multiple requests.
Matt Menkefe1f1c82019-08-20 17:49:11229 bool SupportsRequestPriority(
230 const url::SchemeHostPort& server,
231 const net::NetworkIsolationKey& network_isolation_key);
[email protected]db96a882011-10-09 02:01:54232
rtennetie267d6a2015-06-05 21:55:23233 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
Matt Menkefe1f1c82019-08-20 17:49:11234 bool GetSupportsSpdy(const url::SchemeHostPort& server,
235 const net::NetworkIsolationKey& network_isolation_key);
rtennetie267d6a2015-06-05 21:55:23236
Matt Menkefe1f1c82019-08-20 17:49:11237 // Records whether |server| supports H2 or not. Information is restricted to
238 // the context of |network_isolation_key|, to prevent cross-site information
239 // leakage.
240 void SetSupportsSpdy(const url::SchemeHostPort& server,
241 const net::NetworkIsolationKey& network_isolation_key,
242 bool supports_spdy);
[email protected]db96a882011-10-09 02:01:54243
bncfacdd852015-01-09 19:22:54244 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
Matt Menke609160742019-08-02 18:47:26245 bool RequiresHTTP11(const HostPortPair& server);
bncfacdd852015-01-09 19:22:54246
247 // Require HTTP/1.1 on subsequent connections. Not persisted.
Matt Menke609160742019-08-02 18:47:26248 void SetHTTP11Required(const HostPortPair& server);
bncfacdd852015-01-09 19:22:54249
250 // Modify SSLConfig to force HTTP/1.1.
251 static void ForceHTTP11(SSLConfig* ssl_config);
252
253 // Modify SSLConfig to force HTTP/1.1 if necessary.
Matt Menke609160742019-08-02 18:47:26254 void MaybeForceHTTP11(const HostPortPair& server, SSLConfig* ssl_config);
bncfacdd852015-01-09 19:22:54255
Matt Menke3233d8f22019-08-20 21:01:49256 // Return all alternative services for |origin|, learned in the context of
257 // |network_isolation_key|, including broken ones. Returned alternative
258 // services never have empty hostnames.
Matt Menke609160742019-08-02 18:47:26259 AlternativeServiceInfoVector GetAlternativeServiceInfos(
Matt Menke3233d8f22019-08-20 21:01:49260 const url::SchemeHostPort& origin,
261 const net::NetworkIsolationKey& network_isolation_key);
[email protected]17291a022011-10-10 07:32:53262
zhongyie537a002017-06-27 16:48:21263 // Set a single HTTP/2 alternative service for |origin|. Previous
264 // alternative services for |origin| are discarded.
265 // |alternative_service.host| may be empty.
Matt Menke9aa86262019-08-21 15:52:07266 void SetHttp2AlternativeService(
267 const url::SchemeHostPort& origin,
268 const NetworkIsolationKey& network_isolation_key,
269 const AlternativeService& alternative_service,
270 base::Time expiration);
zhongyie537a002017-06-27 16:48:21271
272 // Set a single QUIC alternative service for |origin|. Previous alternative
bncd9b132e2015-07-08 05:16:10273 // services for |origin| are discarded.
274 // |alternative_service.host| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03275 void SetQuicAlternativeService(
zhongyi3d4a55e72016-04-22 20:36:46276 const url::SchemeHostPort& origin,
Matt Menke9aa86262019-08-21 15:52:07277 const NetworkIsolationKey& network_isolation_key,
bnccacc0992015-03-20 20:22:22278 const AlternativeService& alternative_service,
zhongyie537a002017-06-27 16:48:21279 base::Time expiration,
Matt Menke609160742019-08-02 18:47:26280 const quic::ParsedQuicVersionVector& advertised_versions);
[email protected]17291a022011-10-10 07:32:53281
Matt Menke3233d8f22019-08-20 21:01:49282 // Set alternative services for |origin|, learned in the context of
283 // |network_isolation_key|. Previous alternative services for |origin| are
284 // discarded. Hostnames in |alternative_service_info_vector| may be empty.
bnc4b91d832016-07-27 23:36:12285 // |alternative_service_info_vector| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03286 void SetAlternativeServices(
zhongyi3d4a55e72016-04-22 20:36:46287 const url::SchemeHostPort& origin,
Matt Menke3233d8f22019-08-20 21:01:49288 const net::NetworkIsolationKey& network_isolation_key,
Matt Menke609160742019-08-02 18:47:26289 const AlternativeServiceInfoVector& alternative_service_info_vector);
bncd9b132e2015-07-08 05:16:10290
bnccacc0992015-03-20 20:22:22291 // Marks |alternative_service| as broken.
bncd9b132e2015-07-08 05:16:10292 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26293 void MarkAlternativeServiceBroken(
294 const AlternativeService& alternative_service);
[email protected]17291a022011-10-10 07:32:53295
Zhongyi Shi826b1d22018-08-28 21:45:15296 // Marks |alternative_service| as broken until the default network changes.
297 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26298 void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
299 const AlternativeService& alternative_service);
Zhongyi Shi826b1d22018-08-28 21:45:15300
bncd1e0aa22015-03-13 10:14:31301 // Marks |alternative_service| as recently broken.
bncd9b132e2015-07-08 05:16:10302 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26303 void MarkAlternativeServiceRecentlyBroken(
304 const AlternativeService& alternative_service);
bncd1e0aa22015-03-13 10:14:31305
bnc8445b3002015-03-13 01:57:09306 // Returns true iff |alternative_service| is currently broken.
bncd9b132e2015-07-08 05:16:10307 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26308 bool IsAlternativeServiceBroken(
309 const AlternativeService& alternative_service) const;
bnc8445b3002015-03-13 01:57:09310
bnccacc0992015-03-20 20:22:22311 // Returns true iff |alternative_service| was recently broken.
bncd9b132e2015-07-08 05:16:10312 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26313 bool WasAlternativeServiceRecentlyBroken(
314 const AlternativeService& alternative_service);
[email protected]f5716e32014-04-18 00:44:16315
bnccacc0992015-03-20 20:22:22316 // Confirms that |alternative_service| is working.
bncd9b132e2015-07-08 05:16:10317 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26318 void ConfirmAlternativeService(const AlternativeService& alternative_service);
[email protected]f5716e32014-04-18 00:44:16319
Zhongyi Shi826b1d22018-08-28 21:45:15320 // Called when the default network changes.
321 // Clears all the alternative services that were marked broken until the
322 // default network changed.
Matt Menke2f63ef692019-08-02 22:48:03323 void OnDefaultNetworkChanged();
Zhongyi Shi826b1d22018-08-28 21:45:15324
bnc8ba74a1782015-04-14 17:42:08325 // Returns all alternative service mappings as human readable strings.
bncd9b132e2015-07-08 05:16:10326 // Empty alternative service hostnames will be printed as such.
Matt Menke609160742019-08-02 18:47:26327 std::unique_ptr<base::Value> GetAlternativeServiceInfoAsValue() const;
bnc8ba74a1782015-04-14 17:42:08328
Matt Menke609160742019-08-02 18:47:26329 bool GetSupportsQuic(IPAddress* last_address) const;
rtenneti1c863aa2014-09-25 18:39:33330
Matt Menke609160742019-08-02 18:47:26331 void SetSupportsQuic(bool used_quic, const IPAddress& last_address);
rtenneti1c863aa2014-09-25 18:39:33332
rchac7f35e2017-03-15 20:42:30333 // Sets |stats| for |server|.
Matt Menke609160742019-08-02 18:47:26334 void SetServerNetworkStats(const url::SchemeHostPort& server,
335 ServerNetworkStats stats);
[email protected]3b8cf7f2014-01-27 22:08:51336
rchac7f35e2017-03-15 20:42:30337 // Clears any stats for |server|.
Matt Menke609160742019-08-02 18:47:26338 void ClearServerNetworkStats(const url::SchemeHostPort& server);
rchac7f35e2017-03-15 20:42:30339
340 // Returns any stats for |server| or nullptr if there are none.
Matt Menke609160742019-08-02 18:47:26341 const ServerNetworkStats* GetServerNetworkStats(
342 const url::SchemeHostPort& server);
rtenneti338cd36a2015-01-06 00:20:07343
rtenneti8b673f72015-10-08 23:45:37344 // Save QuicServerInfo (in std::string form) for the given |server_id|.
Matt Menke2f63ef692019-08-02 22:48:03345 void SetQuicServerInfo(const quic::QuicServerId& server_id,
Matt Menke609160742019-08-02 18:47:26346 const std::string& server_info);
rtenneti8b673f72015-10-08 23:45:37347
348 // Get QuicServerInfo (in std::string form) for the given |server_id|.
Matt Menke609160742019-08-02 18:47:26349 const std::string* GetQuicServerInfo(const quic::QuicServerId& server_id);
rtenneti8b673f72015-10-08 23:45:37350
351 // Returns all persistent QuicServerInfo objects.
Matt Menke609160742019-08-02 18:47:26352 const QuicServerInfoMap& quic_server_info_map() const;
rtenneti8b673f72015-10-08 23:45:37353
rtenneti6971c172016-01-15 20:12:10354 // Returns the number of server configs (QuicServerInfo objects) persisted.
Matt Menke609160742019-08-02 18:47:26355 size_t max_server_configs_stored_in_properties() const;
rtenneti6971c172016-01-15 20:12:10356
357 // Sets the number of server configs (QuicServerInfo objects) to be persisted.
Matt Menke609160742019-08-02 18:47:26358 void SetMaxServerConfigsStoredInProperties(
359 size_t max_server_configs_stored_in_properties);
rtenneti6971c172016-01-15 20:12:10360
xunjieli1df4de12017-02-09 17:21:19361 // Returns whether HttpServerProperties is initialized.
Matt Menke609160742019-08-02 18:47:26362 bool IsInitialized() const;
363
364 // BrokenAlternativeServices::Delegate method.
365 void OnExpireBrokenAlternativeService(
366 const AlternativeService& expired_alternative_service) override;
367
368 static base::TimeDelta GetUpdatePrefsDelayForTesting();
369
370 // Test-only routines that call the methods used to load the specified
371 // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked
372 // multiple times.
Matt Menke5e7dcd32019-08-09 22:25:21373 void OnServerInfoLoadedForTesting(
374 std::unique_ptr<ServerInfoMap> server_info_map) {
375 OnServerInfoLoaded(std::move(server_info_map));
Matt Menke609160742019-08-02 18:47:26376 }
Matt Menke609160742019-08-02 18:47:26377 void OnSupportsQuicLoadedForTesting(const IPAddress& last_address) {
378 OnSupportsQuicLoaded(last_address);
379 }
380 void OnQuicServerInfoMapLoadedForTesting(
381 std::unique_ptr<QuicServerInfoMap> quic_server_info_map) {
382 OnQuicServerInfoMapLoaded(std::move(quic_server_info_map));
383 }
384 void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting(
385 std::unique_ptr<BrokenAlternativeServiceList>
386 broken_alternative_service_list,
387 std::unique_ptr<RecentlyBrokenAlternativeServices>
388 recently_broken_alternative_services) {
389 OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
390 std::move(broken_alternative_service_list),
391 std::move(recently_broken_alternative_services));
392 }
393
394 const std::string* GetCanonicalSuffixForTesting(
395 const std::string& host) const {
396 return GetCanonicalSuffix(host);
397 }
398
Matt Menke5e7dcd32019-08-09 22:25:21399 const ServerInfoMap& server_info_map_for_testing() const {
400 return server_info_map_;
Matt Menke609160742019-08-02 18:47:26401 }
402
403 // TODO(mmenke): Look into removing this.
404 HttpServerPropertiesManager* properties_manager_for_testing() {
405 return properties_manager_.get();
406 }
xunjieli1df4de12017-02-09 17:21:19407
[email protected]db96a882011-10-09 02:01:54408 private:
Matt Menke609160742019-08-02 18:47:26409 // TODO (wangyix): modify HttpServerProperties unit tests so this
410 // friendness is no longer required.
411 friend class HttpServerPropertiesPeer;
412
Matt Menke04a5a082019-08-21 15:07:07413 typedef base::flat_map<ServerInfoMapKey, url::SchemeHostPort>
Matt Menke609160742019-08-02 18:47:26414 CanonicalAltSvcMap;
415 typedef base::flat_map<HostPortPair, quic::QuicServerId>
416 CanonicalServerInfoMap;
417 typedef std::vector<std::string> CanonicalSuffixList;
418 typedef std::set<HostPortPair> Http11ServerHostPortSet;
419
Matt Menke1be93d22019-08-20 16:57:58420 // Helper function to use the passed in parameters and
421 // |use_network_isolation_key_| to create a ServerInfoMapKey.
422 ServerInfoMapKey CreateServerInfoKey(
423 const url::SchemeHostPort& server,
Matt Menke04a5a082019-08-21 15:07:07424 const NetworkIsolationKey& network_isolation_key) const;
Matt Menke1be93d22019-08-20 16:57:58425
Matt Menke04a5a082019-08-21 15:07:07426 // Return the iterator for |server| in the context of |network_isolation_key|,
427 // or for its canonical host, or end. Skips over ServerInfos without
428 // |alternative_service_info| populated.
Matt Menkefe9b5962019-08-14 20:56:14429 ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo(
Matt Menke04a5a082019-08-21 15:07:07430 const url::SchemeHostPort& server,
431 const net::NetworkIsolationKey& network_isolation_key);
Matt Menke609160742019-08-02 18:47:26432
Matt Menke04a5a082019-08-21 15:07:07433 // Return the canonical host for |server| in the context of
434 // |network_isolation_key|, or end if none exists.
Matt Menke609160742019-08-02 18:47:26435 CanonicalAltSvcMap::const_iterator GetCanonicalAltSvcHost(
Matt Menke04a5a082019-08-21 15:07:07436 const url::SchemeHostPort& server,
437 const net::NetworkIsolationKey& network_isolation_key) const;
Matt Menke609160742019-08-02 18:47:26438
439 // Return the canonical host with the same canonical suffix as |server|.
440 // The returned canonical host can be used to search for server info in
441 // |quic_server_info_map_|. Return 'end' the host doesn't exist.
442 CanonicalServerInfoMap::const_iterator GetCanonicalServerInfoHost(
443 const quic::QuicServerId& server) const;
444
Matt Menke04a5a082019-08-21 15:07:07445 // Remove the canonical alt-svc host for |server| with
446 // |network_isolation_key|.
447 void RemoveAltSvcCanonicalHost(
448 const url::SchemeHostPort& server,
449 const NetworkIsolationKey& network_isolation_key);
Matt Menke609160742019-08-02 18:47:26450
451 // Update |canonical_server_info_map_| with the new canonical host.
452 // The |server| should have the corresponding server info associated with it
453 // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't
454 // have an entry associated with |server|, the method will add one.
455 void UpdateCanonicalServerInfoMap(const quic::QuicServerId& server);
456
457 // Returns the canonical host suffix for |host|, or nullptr if none
458 // exists.
459 const std::string* GetCanonicalSuffix(const std::string& host) const;
460
461 void OnPrefsLoaded(
Matt Menke5e7dcd32019-08-09 22:25:21462 std::unique_ptr<ServerInfoMap> server_info_map,
Matt Menke609160742019-08-02 18:47:26463 const IPAddress& last_quic_address,
464 std::unique_ptr<QuicServerInfoMap> quic_server_info_map,
465 std::unique_ptr<BrokenAlternativeServiceList>
466 broken_alternative_service_list,
467 std::unique_ptr<RecentlyBrokenAlternativeServices>
Matt Menke7932d812019-08-02 21:26:08468 recently_broken_alternative_services);
Matt Menke609160742019-08-02 18:47:26469
470 // These methods are called by OnPrefsLoaded to handle merging properties
471 // loaded from prefs with what has been learned while waiting for prefs to
472 // load.
Matt Menke5e7dcd32019-08-09 22:25:21473 void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map);
Matt Menke609160742019-08-02 18:47:26474 void OnSupportsQuicLoaded(const IPAddress& last_address);
475 void OnQuicServerInfoMapLoaded(
476 std::unique_ptr<QuicServerInfoMap> quic_server_info_map);
477 void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
478 std::unique_ptr<BrokenAlternativeServiceList>
479 broken_alternative_service_list,
480 std::unique_ptr<RecentlyBrokenAlternativeServices>
481 recently_broken_alternative_services);
482
483 // Queue a delayed call to WriteProperties(). If |is_initialized_| is false,
484 // or |properties_manager_| is nullptr, or there's already a queued call to
485 // WriteProperties(), does nothing.
486 void MaybeQueueWriteProperties();
487
488 // Writes cached state to |properties_manager_|, which must not be null.
489 // Invokes |callback| on completion, if non-null.
490 void WriteProperties(base::OnceClosure callback) const;
491
492 const base::TickClock* tick_clock_; // Unowned
493 base::Clock* clock_; // Unowned
494
Matt Menke1be93d22019-08-20 16:57:58495 // Cached value of kPartitionHttpServerPropertiesByNetworkIsolationKey
496 // feature. Cached to improve performance.
497 const bool use_network_isolation_key_;
498
Matt Menke609160742019-08-02 18:47:26499 // Set to true once initial properties have been retrieved from disk by
500 // |properties_manager_|. Always true if |properties_manager_| is nullptr.
501 bool is_initialized_;
502
Matt Menke723f10292019-08-02 21:13:10503 // Queue a write when resources finish loading. Set to true when
504 // MaybeQueueWriteProperties() is invoked while still waiting on
505 // initialization to complete.
506 bool queue_write_on_load_;
507
Matt Menke609160742019-08-02 18:47:26508 // Used to load/save properties from/to preferences. May be nullptr.
509 std::unique_ptr<HttpServerPropertiesManager> properties_manager_;
510
Matt Menke5e7dcd32019-08-09 22:25:21511 ServerInfoMap server_info_map_;
Matt Menke609160742019-08-02 18:47:26512
Matt Menkefe9b5962019-08-14 20:56:14513 Http11ServerHostPortSet http11_servers_;
Matt Menke609160742019-08-02 18:47:26514
515 BrokenAlternativeServices broken_alternative_services_;
516
517 IPAddress last_quic_address_;
Matt Menke609160742019-08-02 18:47:26518 // Contains a map of servers which could share the same alternate protocol.
519 // Map from a Canonical scheme/host/port (host is some postfix of host names)
520 // to an actual origin, which has a plausible alternate protocol mapping.
521 CanonicalAltSvcMap canonical_alt_svc_map_;
522
523 // Contains list of suffixes (for example ".c.youtube.com",
524 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
525 const CanonicalSuffixList canonical_suffixes_;
526
527 QuicServerInfoMap quic_server_info_map_;
528
529 // Maps canonical suffixes to host names that have the same canonical suffix
530 // and have a corresponding entry in |quic_server_info_map_|. The map can be
531 // used to quickly look for server info for hosts that share the same
532 // canonical suffix but don't have exact match in |quic_server_info_map_|. The
533 // map exists solely to improve the search performance. It only contains
534 // derived data that can be recalculated by traversing
535 // |quic_server_info_map_|.
536 CanonicalServerInfoMap canonical_server_info_map_;
537
538 size_t max_server_configs_stored_in_properties_;
539
540 // Used to post calls to WriteProperties().
541 base::OneShotTimer prefs_update_timer_;
542
543 THREAD_CHECKER(thread_checker_);
544
[email protected]db96a882011-10-09 02:01:54545 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
546};
547
548} // namespace net
549
550#endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_