blob: d0da63f25ab954f401278d9fce1bc7706537a0ce [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
rchdc7b9052016-03-17 20:51:50256 // Return all alternative services for |origin|, including broken ones.
bncd9b132e2015-07-08 05:16:10257 // Returned alternative services never have empty hostnames.
Matt Menke609160742019-08-02 18:47:26258 AlternativeServiceInfoVector GetAlternativeServiceInfos(
259 const url::SchemeHostPort& origin);
[email protected]17291a022011-10-10 07:32:53260
zhongyie537a002017-06-27 16:48:21261 // Set a single HTTP/2 alternative service for |origin|. Previous
262 // alternative services for |origin| are discarded.
263 // |alternative_service.host| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03264 void SetHttp2AlternativeService(const url::SchemeHostPort& origin,
Matt Menke609160742019-08-02 18:47:26265 const AlternativeService& alternative_service,
266 base::Time expiration);
zhongyie537a002017-06-27 16:48:21267
268 // Set a single QUIC alternative service for |origin|. Previous alternative
bncd9b132e2015-07-08 05:16:10269 // services for |origin| are discarded.
270 // |alternative_service.host| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03271 void SetQuicAlternativeService(
zhongyi3d4a55e72016-04-22 20:36:46272 const url::SchemeHostPort& origin,
bnccacc0992015-03-20 20:22:22273 const AlternativeService& alternative_service,
zhongyie537a002017-06-27 16:48:21274 base::Time expiration,
Matt Menke609160742019-08-02 18:47:26275 const quic::ParsedQuicVersionVector& advertised_versions);
[email protected]17291a022011-10-10 07:32:53276
bncd9b132e2015-07-08 05:16:10277 // Set alternative services for |origin|. Previous alternative services for
278 // |origin| are discarded.
279 // Hostnames in |alternative_service_info_vector| may be empty.
bnc4b91d832016-07-27 23:36:12280 // |alternative_service_info_vector| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03281 void SetAlternativeServices(
zhongyi3d4a55e72016-04-22 20:36:46282 const url::SchemeHostPort& origin,
Matt Menke609160742019-08-02 18:47:26283 const AlternativeServiceInfoVector& alternative_service_info_vector);
bncd9b132e2015-07-08 05:16:10284
bnccacc0992015-03-20 20:22:22285 // Marks |alternative_service| as broken.
bncd9b132e2015-07-08 05:16:10286 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26287 void MarkAlternativeServiceBroken(
288 const AlternativeService& alternative_service);
[email protected]17291a022011-10-10 07:32:53289
Zhongyi Shi826b1d22018-08-28 21:45:15290 // Marks |alternative_service| as broken until the default network changes.
291 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26292 void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
293 const AlternativeService& alternative_service);
Zhongyi Shi826b1d22018-08-28 21:45:15294
bncd1e0aa22015-03-13 10:14:31295 // Marks |alternative_service| as recently broken.
bncd9b132e2015-07-08 05:16:10296 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26297 void MarkAlternativeServiceRecentlyBroken(
298 const AlternativeService& alternative_service);
bncd1e0aa22015-03-13 10:14:31299
bnc8445b3002015-03-13 01:57:09300 // Returns true iff |alternative_service| is currently broken.
bncd9b132e2015-07-08 05:16:10301 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26302 bool IsAlternativeServiceBroken(
303 const AlternativeService& alternative_service) const;
bnc8445b3002015-03-13 01:57:09304
bnccacc0992015-03-20 20:22:22305 // Returns true iff |alternative_service| was recently broken.
bncd9b132e2015-07-08 05:16:10306 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26307 bool WasAlternativeServiceRecentlyBroken(
308 const AlternativeService& alternative_service);
[email protected]f5716e32014-04-18 00:44:16309
bnccacc0992015-03-20 20:22:22310 // Confirms that |alternative_service| is working.
bncd9b132e2015-07-08 05:16:10311 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26312 void ConfirmAlternativeService(const AlternativeService& alternative_service);
[email protected]f5716e32014-04-18 00:44:16313
Zhongyi Shi826b1d22018-08-28 21:45:15314 // Called when the default network changes.
315 // Clears all the alternative services that were marked broken until the
316 // default network changed.
Matt Menke2f63ef692019-08-02 22:48:03317 void OnDefaultNetworkChanged();
Zhongyi Shi826b1d22018-08-28 21:45:15318
bnc8ba74a1782015-04-14 17:42:08319 // Returns all alternative service mappings as human readable strings.
bncd9b132e2015-07-08 05:16:10320 // Empty alternative service hostnames will be printed as such.
Matt Menke609160742019-08-02 18:47:26321 std::unique_ptr<base::Value> GetAlternativeServiceInfoAsValue() const;
bnc8ba74a1782015-04-14 17:42:08322
Matt Menke609160742019-08-02 18:47:26323 bool GetSupportsQuic(IPAddress* last_address) const;
rtenneti1c863aa2014-09-25 18:39:33324
Matt Menke609160742019-08-02 18:47:26325 void SetSupportsQuic(bool used_quic, const IPAddress& last_address);
rtenneti1c863aa2014-09-25 18:39:33326
rchac7f35e2017-03-15 20:42:30327 // Sets |stats| for |server|.
Matt Menke609160742019-08-02 18:47:26328 void SetServerNetworkStats(const url::SchemeHostPort& server,
329 ServerNetworkStats stats);
[email protected]3b8cf7f2014-01-27 22:08:51330
rchac7f35e2017-03-15 20:42:30331 // Clears any stats for |server|.
Matt Menke609160742019-08-02 18:47:26332 void ClearServerNetworkStats(const url::SchemeHostPort& server);
rchac7f35e2017-03-15 20:42:30333
334 // Returns any stats for |server| or nullptr if there are none.
Matt Menke609160742019-08-02 18:47:26335 const ServerNetworkStats* GetServerNetworkStats(
336 const url::SchemeHostPort& server);
rtenneti338cd36a2015-01-06 00:20:07337
rtenneti8b673f72015-10-08 23:45:37338 // Save QuicServerInfo (in std::string form) for the given |server_id|.
Matt Menke2f63ef692019-08-02 22:48:03339 void SetQuicServerInfo(const quic::QuicServerId& server_id,
Matt Menke609160742019-08-02 18:47:26340 const std::string& server_info);
rtenneti8b673f72015-10-08 23:45:37341
342 // Get QuicServerInfo (in std::string form) for the given |server_id|.
Matt Menke609160742019-08-02 18:47:26343 const std::string* GetQuicServerInfo(const quic::QuicServerId& server_id);
rtenneti8b673f72015-10-08 23:45:37344
345 // Returns all persistent QuicServerInfo objects.
Matt Menke609160742019-08-02 18:47:26346 const QuicServerInfoMap& quic_server_info_map() const;
rtenneti8b673f72015-10-08 23:45:37347
rtenneti6971c172016-01-15 20:12:10348 // Returns the number of server configs (QuicServerInfo objects) persisted.
Matt Menke609160742019-08-02 18:47:26349 size_t max_server_configs_stored_in_properties() const;
rtenneti6971c172016-01-15 20:12:10350
351 // Sets the number of server configs (QuicServerInfo objects) to be persisted.
Matt Menke609160742019-08-02 18:47:26352 void SetMaxServerConfigsStoredInProperties(
353 size_t max_server_configs_stored_in_properties);
rtenneti6971c172016-01-15 20:12:10354
xunjieli1df4de12017-02-09 17:21:19355 // Returns whether HttpServerProperties is initialized.
Matt Menke609160742019-08-02 18:47:26356 bool IsInitialized() const;
357
358 // BrokenAlternativeServices::Delegate method.
359 void OnExpireBrokenAlternativeService(
360 const AlternativeService& expired_alternative_service) override;
361
362 static base::TimeDelta GetUpdatePrefsDelayForTesting();
363
364 // Test-only routines that call the methods used to load the specified
365 // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked
366 // multiple times.
Matt Menke5e7dcd32019-08-09 22:25:21367 void OnServerInfoLoadedForTesting(
368 std::unique_ptr<ServerInfoMap> server_info_map) {
369 OnServerInfoLoaded(std::move(server_info_map));
Matt Menke609160742019-08-02 18:47:26370 }
Matt Menke609160742019-08-02 18:47:26371 void OnSupportsQuicLoadedForTesting(const IPAddress& last_address) {
372 OnSupportsQuicLoaded(last_address);
373 }
374 void OnQuicServerInfoMapLoadedForTesting(
375 std::unique_ptr<QuicServerInfoMap> quic_server_info_map) {
376 OnQuicServerInfoMapLoaded(std::move(quic_server_info_map));
377 }
378 void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting(
379 std::unique_ptr<BrokenAlternativeServiceList>
380 broken_alternative_service_list,
381 std::unique_ptr<RecentlyBrokenAlternativeServices>
382 recently_broken_alternative_services) {
383 OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
384 std::move(broken_alternative_service_list),
385 std::move(recently_broken_alternative_services));
386 }
387
388 const std::string* GetCanonicalSuffixForTesting(
389 const std::string& host) const {
390 return GetCanonicalSuffix(host);
391 }
392
Matt Menke5e7dcd32019-08-09 22:25:21393 const ServerInfoMap& server_info_map_for_testing() const {
394 return server_info_map_;
Matt Menke609160742019-08-02 18:47:26395 }
396
397 // TODO(mmenke): Look into removing this.
398 HttpServerPropertiesManager* properties_manager_for_testing() {
399 return properties_manager_.get();
400 }
xunjieli1df4de12017-02-09 17:21:19401
[email protected]db96a882011-10-09 02:01:54402 private:
Matt Menke609160742019-08-02 18:47:26403 // TODO (wangyix): modify HttpServerProperties unit tests so this
404 // friendness is no longer required.
405 friend class HttpServerPropertiesPeer;
406
407 typedef base::flat_map<url::SchemeHostPort, url::SchemeHostPort>
408 CanonicalAltSvcMap;
409 typedef base::flat_map<HostPortPair, quic::QuicServerId>
410 CanonicalServerInfoMap;
411 typedef std::vector<std::string> CanonicalSuffixList;
412 typedef std::set<HostPortPair> Http11ServerHostPortSet;
413
Matt Menke1be93d22019-08-20 16:57:58414 // Helper function to use the passed in parameters and
415 // |use_network_isolation_key_| to create a ServerInfoMapKey.
416 ServerInfoMapKey CreateServerInfoKey(
417 const url::SchemeHostPort& server,
418 const NetworkIsolationKey& network_isolation_key);
419
Matt Menkefe9b5962019-08-14 20:56:14420 // Return the iterator for |server|, or for its canonical host, or end. Skips
421 // over ServerInfos without |alternative_service_info| populated.
422 ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo(
Matt Menke609160742019-08-02 18:47:26423 const url::SchemeHostPort& server);
424
425 // Return the canonical host for |server|, or end if none exists.
426 CanonicalAltSvcMap::const_iterator GetCanonicalAltSvcHost(
427 const url::SchemeHostPort& server) const;
428
429 // Return the canonical host with the same canonical suffix as |server|.
430 // The returned canonical host can be used to search for server info in
431 // |quic_server_info_map_|. Return 'end' the host doesn't exist.
432 CanonicalServerInfoMap::const_iterator GetCanonicalServerInfoHost(
433 const quic::QuicServerId& server) const;
434
435 // Remove the canonical alt-svc host for |server|.
436 void RemoveAltSvcCanonicalHost(const url::SchemeHostPort& server);
437
438 // Update |canonical_server_info_map_| with the new canonical host.
439 // The |server| should have the corresponding server info associated with it
440 // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't
441 // have an entry associated with |server|, the method will add one.
442 void UpdateCanonicalServerInfoMap(const quic::QuicServerId& server);
443
444 // Returns the canonical host suffix for |host|, or nullptr if none
445 // exists.
446 const std::string* GetCanonicalSuffix(const std::string& host) const;
447
448 void OnPrefsLoaded(
Matt Menke5e7dcd32019-08-09 22:25:21449 std::unique_ptr<ServerInfoMap> server_info_map,
Matt Menke609160742019-08-02 18:47:26450 const IPAddress& last_quic_address,
451 std::unique_ptr<QuicServerInfoMap> quic_server_info_map,
452 std::unique_ptr<BrokenAlternativeServiceList>
453 broken_alternative_service_list,
454 std::unique_ptr<RecentlyBrokenAlternativeServices>
Matt Menke7932d812019-08-02 21:26:08455 recently_broken_alternative_services);
Matt Menke609160742019-08-02 18:47:26456
457 // These methods are called by OnPrefsLoaded to handle merging properties
458 // loaded from prefs with what has been learned while waiting for prefs to
459 // load.
Matt Menke5e7dcd32019-08-09 22:25:21460 void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map);
Matt Menke609160742019-08-02 18:47:26461 void OnSupportsQuicLoaded(const IPAddress& last_address);
462 void OnQuicServerInfoMapLoaded(
463 std::unique_ptr<QuicServerInfoMap> quic_server_info_map);
464 void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
465 std::unique_ptr<BrokenAlternativeServiceList>
466 broken_alternative_service_list,
467 std::unique_ptr<RecentlyBrokenAlternativeServices>
468 recently_broken_alternative_services);
469
470 // Queue a delayed call to WriteProperties(). If |is_initialized_| is false,
471 // or |properties_manager_| is nullptr, or there's already a queued call to
472 // WriteProperties(), does nothing.
473 void MaybeQueueWriteProperties();
474
475 // Writes cached state to |properties_manager_|, which must not be null.
476 // Invokes |callback| on completion, if non-null.
477 void WriteProperties(base::OnceClosure callback) const;
478
479 const base::TickClock* tick_clock_; // Unowned
480 base::Clock* clock_; // Unowned
481
Matt Menke1be93d22019-08-20 16:57:58482 // Cached value of kPartitionHttpServerPropertiesByNetworkIsolationKey
483 // feature. Cached to improve performance.
484 const bool use_network_isolation_key_;
485
Matt Menke609160742019-08-02 18:47:26486 // Set to true once initial properties have been retrieved from disk by
487 // |properties_manager_|. Always true if |properties_manager_| is nullptr.
488 bool is_initialized_;
489
Matt Menke723f10292019-08-02 21:13:10490 // Queue a write when resources finish loading. Set to true when
491 // MaybeQueueWriteProperties() is invoked while still waiting on
492 // initialization to complete.
493 bool queue_write_on_load_;
494
Matt Menke609160742019-08-02 18:47:26495 // Used to load/save properties from/to preferences. May be nullptr.
496 std::unique_ptr<HttpServerPropertiesManager> properties_manager_;
497
Matt Menke5e7dcd32019-08-09 22:25:21498 ServerInfoMap server_info_map_;
Matt Menke609160742019-08-02 18:47:26499
Matt Menkefe9b5962019-08-14 20:56:14500 Http11ServerHostPortSet http11_servers_;
Matt Menke609160742019-08-02 18:47:26501
502 BrokenAlternativeServices broken_alternative_services_;
503
504 IPAddress last_quic_address_;
Matt Menke609160742019-08-02 18:47:26505 // Contains a map of servers which could share the same alternate protocol.
506 // Map from a Canonical scheme/host/port (host is some postfix of host names)
507 // to an actual origin, which has a plausible alternate protocol mapping.
508 CanonicalAltSvcMap canonical_alt_svc_map_;
509
510 // Contains list of suffixes (for example ".c.youtube.com",
511 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
512 const CanonicalSuffixList canonical_suffixes_;
513
514 QuicServerInfoMap quic_server_info_map_;
515
516 // Maps canonical suffixes to host names that have the same canonical suffix
517 // and have a corresponding entry in |quic_server_info_map_|. The map can be
518 // used to quickly look for server info for hosts that share the same
519 // canonical suffix but don't have exact match in |quic_server_info_map_|. The
520 // map exists solely to improve the search performance. It only contains
521 // derived data that can be recalculated by traversing
522 // |quic_server_info_map_|.
523 CanonicalServerInfoMap canonical_server_info_map_;
524
525 size_t max_server_configs_stored_in_properties_;
526
527 // Used to post calls to WriteProperties().
528 base::OneShotTimer prefs_update_timer_;
529
530 THREAD_CHECKER(thread_checker_);
531
[email protected]db96a882011-10-09 02:01:54532 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
533};
534
535} // namespace net
536
537#endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_