1 // Copyright 2012 The Chromium Authors 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_H_ 6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <memory> 13 #include <set> 14 #include <string> 15 #include <tuple> 16 #include <vector> 17 18 #include "base/containers/lru_cache.h" 19 #include "base/functional/callback.h" 20 #include "base/memory/raw_ptr.h" 21 #include "base/memory/weak_ptr.h" 22 #include "base/threading/thread_checker.h" 23 #include "base/time/time.h" 24 #include "base/timer/timer.h" 25 #include "base/values.h" 26 #include "net/base/host_port_pair.h" 27 #include "net/base/ip_address.h" 28 #include "net/base/net_export.h" 29 #include "net/base/network_anonymization_key.h" 30 #include "net/http/alternative_service.h" 31 #include "net/http/broken_alternative_services.h" 32 #include "net/third_party/quiche/src/quiche/quic/core/quic_bandwidth.h" 33 #include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h" 34 #include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h" 35 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_framer.h" // TODO(willchan): Reconsider this. 36 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h" 37 #include "third_party/abseil-cpp/absl/types/optional.h" 38 #include "url/scheme_host_port.h" 39 40 namespace base { 41 class Clock; 42 class TickClock; 43 } 44 45 namespace net { 46 47 class HttpServerPropertiesManager; 48 class IPAddress; 49 class NetLog; 50 struct SSLConfig; 51 52 struct NET_EXPORT SupportsQuic { SupportsQuicSupportsQuic53 SupportsQuic() : used_quic(false) {} SupportsQuicSupportsQuic54 SupportsQuic(bool used_quic, const std::string& address) 55 : used_quic(used_quic), address(address) {} 56 EqualsSupportsQuic57 bool Equals(const SupportsQuic& other) const { 58 return used_quic == other.used_quic && address == other.address; 59 } 60 61 bool used_quic; 62 std::string address; 63 }; 64 65 struct NET_EXPORT ServerNetworkStats { ServerNetworkStatsServerNetworkStats66 ServerNetworkStats() : bandwidth_estimate(quic::QuicBandwidth::Zero()) {} 67 68 bool operator==(const ServerNetworkStats& other) const { 69 return srtt == other.srtt && bandwidth_estimate == other.bandwidth_estimate; 70 } 71 72 bool operator!=(const ServerNetworkStats& other) const { 73 return !this->operator==(other); 74 } 75 76 base::TimeDelta srtt; 77 quic::QuicBandwidth bandwidth_estimate; 78 }; 79 80 typedef std::vector<AlternativeService> AlternativeServiceVector; 81 82 // Store at most 200 MRU RecentlyBrokenAlternativeServices in memory and disk. 83 // This ideally would be with the other constants in HttpServerProperties, but 84 // has to go here instead of prevent a circular dependency. 85 const int kMaxRecentlyBrokenAlternativeServiceEntries = 200; 86 87 // Store at most 5 MRU QUIC servers by default. This is mainly used by cronet. 88 const int kDefaultMaxQuicServerEntries = 5; 89 90 // The interface for setting/retrieving the HTTP server properties. 91 // Currently, this class manages servers': 92 // * HTTP/2 support; 93 // * Alternative Service support; 94 // * QUIC data (like ServerNetworkStats and QuicServerInfo). 95 // 96 // Optionally retrieves and saves properties from/to disk. This class is not 97 // threadsafe. 98 class NET_EXPORT HttpServerProperties 99 : public BrokenAlternativeServices::Delegate { 100 public: 101 // Store at most 500 MRU ServerInfos in memory and disk. 102 static const int kMaxServerInfoEntries = 500; 103 104 // Provides an interface to interact with persistent preferences storage 105 // implemented by the embedder. The prefs are assumed not to have been loaded 106 // before HttpServerPropertiesManager construction. 107 class NET_EXPORT PrefDelegate { 108 public: 109 virtual ~PrefDelegate(); 110 111 // Returns the branch of the preferences system for the server properties. 112 // Returns nullptr if the pref system has no data for the server properties. 113 virtual const base::Value::Dict& GetServerProperties() const = 0; 114 115 // Sets the server properties to the given value. If |callback| is 116 // non-empty, flushes data to persistent storage and invokes |callback| 117 // asynchronously when complete. 118 virtual void SetServerProperties(base::Value::Dict dict, 119 base::OnceClosure callback) = 0; 120 121 // Starts listening for prefs to be loaded. If prefs are already loaded, 122 // |pref_loaded_callback| will be invoked asynchronously. Callback will be 123 // invoked even if prefs fail to load. Will only be called once by the 124 // HttpServerPropertiesManager. 125 virtual void WaitForPrefLoad(base::OnceClosure pref_loaded_callback) = 0; 126 }; 127 128 // Contains metadata about a particular server. Note that all methods that 129 // take a "SchemeHostPort" expect schemes of ws and wss to be mapped to http 130 // and https, respectively. See GetNormalizedSchemeHostPort(). 131 struct NET_EXPORT ServerInfo { 132 ServerInfo(); 133 ServerInfo(const ServerInfo& server_info); 134 ServerInfo(ServerInfo&& server_info); 135 ~ServerInfo(); 136 137 // Returns true if no fields are populated. 138 bool empty() const; 139 140 // Used in tests. 141 bool operator==(const ServerInfo& other) const; 142 143 // IMPORTANT: When adding a field here, be sure to update 144 // HttpServerProperties::OnServerInfoLoaded() as well as 145 // HttpServerPropertiesManager to correctly load/save the from/to the pref 146 // store. 147 148 // Whether or not a server is known to support H2/SPDY. False indicates 149 // known lack of support, true indicates known support, and not set 150 // indicates unknown. The difference between false and not set only matters 151 // when loading from disk, when an initialized false value will take 152 // priority over a not set value. 153 absl::optional<bool> supports_spdy; 154 155 // True if the server has previously indicated it required HTTP/1.1. Unlike 156 // other fields, not persisted to disk. 157 absl::optional<bool> requires_http11; 158 159 absl::optional<AlternativeServiceInfoVector> alternative_services; 160 absl::optional<ServerNetworkStats> server_network_stats; 161 }; 162 163 struct NET_EXPORT ServerInfoMapKey { 164 // If |use_network_anonymization_key| is false, an empty 165 // NetworkAnonymizationKey is used instead of |network_anonymization_key|. 166 // Note that |server| can be passed in via std::move(), since most callsites 167 // can pass a recently created SchemeHostPort. 168 ServerInfoMapKey(url::SchemeHostPort server, 169 const NetworkAnonymizationKey& network_anonymization_key, 170 bool use_network_anonymization_key); 171 ~ServerInfoMapKey(); 172 173 bool operator<(const ServerInfoMapKey& other) const; 174 175 // IMPORTANT: The constructor normalizes the scheme so that "ws" is replaced 176 // by "http" and "wss" by "https", so this should never be compared directly 177 // with values passed into to HttpServerProperties methods. 178 url::SchemeHostPort server; 179 180 NetworkAnonymizationKey network_anonymization_key; 181 }; 182 183 class NET_EXPORT ServerInfoMap 184 : public base::LRUCache<ServerInfoMapKey, ServerInfo> { 185 public: 186 ServerInfoMap(); 187 188 ServerInfoMap(const ServerInfoMap&) = delete; 189 ServerInfoMap& operator=(const ServerInfoMap&) = delete; 190 191 // If there's an entry corresponding to |key|, brings that entry to the 192 // front and returns an iterator to it. Otherwise, inserts an empty 193 // ServerInfo using |key|, and returns an iterator to it. 194 iterator GetOrPut(const ServerInfoMapKey& key); 195 196 // Erases the ServerInfo identified by |server_info_it| if no fields have 197 // data. The iterator must point to an entry in the map. Regardless of 198 // whether the entry is removed or not, returns iterator for the next entry. 199 iterator EraseIfEmpty(iterator server_info_it); 200 }; 201 202 struct NET_EXPORT QuicServerInfoMapKey { 203 // If |use_network_anonymization_key| is false, an empty 204 // NetworkAnonymizationKey is used instead of |network_anonymization_key|. 205 QuicServerInfoMapKey( 206 const quic::QuicServerId& server_id, 207 const NetworkAnonymizationKey& network_anonymization_key, 208 bool use_network_anonymization_key); 209 ~QuicServerInfoMapKey(); 210 211 bool operator<(const QuicServerInfoMapKey& other) const; 212 213 // Used in tests. 214 bool operator==(const QuicServerInfoMapKey& other) const; 215 216 quic::QuicServerId server_id; 217 NetworkAnonymizationKey network_anonymization_key; 218 }; 219 220 // Max number of quic servers to store is not hardcoded and can be set. 221 // Because of this, QuicServerInfoMap will not be a subclass of LRUCache. 222 // Separate from ServerInfoMap because the key includes privacy mode (Since 223 // this is analogous to the SSL session cache, which has separate caches for 224 // privacy mode), and each entry can be quite large, so it has its own size 225 // limit, which is much smaller than the ServerInfoMap's limit. 226 typedef base::LRUCache<QuicServerInfoMapKey, std::string> QuicServerInfoMap; 227 228 // If a |pref_delegate| is specified, it will be used to read/write the 229 // properties to a pref file. Writes are rate limited to improve performance. 230 // 231 // |tick_clock| is used for setting expiration times and scheduling the 232 // expiration of broken alternative services. If null, default clock will be 233 // used. 234 // 235 // |clock| is used for converting base::TimeTicks to base::Time for 236 // wherever base::Time is preferable. 237 explicit HttpServerProperties( 238 std::unique_ptr<PrefDelegate> pref_delegate = nullptr, 239 NetLog* net_log = nullptr, 240 const base::TickClock* tick_clock = nullptr, 241 base::Clock* clock = nullptr); 242 243 HttpServerProperties(const HttpServerProperties&) = delete; 244 HttpServerProperties& operator=(const HttpServerProperties&) = delete; 245 246 ~HttpServerProperties() override; 247 248 // Deletes all data. If |callback| is non-null, flushes data to disk 249 // and invokes the callback asynchronously once changes have been written to 250 // disk. 251 void Clear(base::OnceClosure callback); 252 253 // Returns true if |server|, in the context of |network_anonymization_key|, 254 // has previously supported a network protocol which honors request 255 // prioritization. 256 // 257 // Note that this also implies that the server supports request 258 // multiplexing, since priorities imply a relationship between 259 // multiple requests. 260 bool SupportsRequestPriority( 261 const url::SchemeHostPort& server, 262 const net::NetworkAnonymizationKey& network_anonymization_key); 263 264 // Returns the value set by SetSupportsSpdy(). If not set, returns false. 265 bool GetSupportsSpdy( 266 const url::SchemeHostPort& server, 267 const net::NetworkAnonymizationKey& network_anonymization_key); 268 269 // Records whether |server| supports H2 or not. Information is restricted to 270 // the context of |network_anonymization_key|, to prevent cross-site 271 // information leakage. 272 void SetSupportsSpdy( 273 const url::SchemeHostPort& server, 274 const net::NetworkAnonymizationKey& network_anonymization_key, 275 bool supports_spdy); 276 277 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code, in 278 // the context of |network_anonymization_key|. 279 bool RequiresHTTP11( 280 const url::SchemeHostPort& server, 281 const net::NetworkAnonymizationKey& network_anonymization_key); 282 283 // Require HTTP/1.1 on subsequent connections, in the context of 284 // |network_anonymization_key|. Not persisted. 285 void SetHTTP11Required( 286 const url::SchemeHostPort& server, 287 const net::NetworkAnonymizationKey& network_anonymization_key); 288 289 // Modify SSLConfig to force HTTP/1.1 if necessary. 290 void MaybeForceHTTP11( 291 const url::SchemeHostPort& server, 292 const net::NetworkAnonymizationKey& network_anonymization_key, 293 SSLConfig* ssl_config); 294 295 // Return all alternative services for |origin|, learned in the context of 296 // |network_anonymization_key|, including broken ones. Returned alternative 297 // services never have empty hostnames. 298 AlternativeServiceInfoVector GetAlternativeServiceInfos( 299 const url::SchemeHostPort& origin, 300 const net::NetworkAnonymizationKey& network_anonymization_key); 301 302 // Set a single HTTP/2 alternative service for |origin|. Previous 303 // alternative services for |origin| are discarded. 304 // |alternative_service.host| may be empty. 305 void SetHttp2AlternativeService( 306 const url::SchemeHostPort& origin, 307 const NetworkAnonymizationKey& network_anonymization_key, 308 const AlternativeService& alternative_service, 309 base::Time expiration); 310 311 // Set a single QUIC alternative service for |origin|. Previous alternative 312 // services for |origin| are discarded. 313 // |alternative_service.host| may be empty. 314 void SetQuicAlternativeService( 315 const url::SchemeHostPort& origin, 316 const NetworkAnonymizationKey& network_anonymization_key, 317 const AlternativeService& alternative_service, 318 base::Time expiration, 319 const quic::ParsedQuicVersionVector& advertised_versions); 320 321 // Set alternative services for |origin|, learned in the context of 322 // |network_anonymization_key|. Previous alternative services for |origin| 323 // are discarded. Hostnames in |alternative_service_info_vector| may be empty. 324 // |alternative_service_info_vector| may be empty. 325 void SetAlternativeServices( 326 const url::SchemeHostPort& origin, 327 const net::NetworkAnonymizationKey& network_anonymization_key, 328 const AlternativeServiceInfoVector& alternative_service_info_vector); 329 330 // Marks |alternative_service| as broken in the context of 331 // |network_anonymization_key|. |alternative_service.host| must not be empty. 332 void MarkAlternativeServiceBroken( 333 const AlternativeService& alternative_service, 334 const net::NetworkAnonymizationKey& network_anonymization_key); 335 336 // Marks |alternative_service| as broken in the context of 337 // |network_anonymization_key| until the default network changes. 338 // |alternative_service.host| must not be empty. 339 void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges( 340 const AlternativeService& alternative_service, 341 const net::NetworkAnonymizationKey& network_anonymization_key); 342 343 // Marks |alternative_service| as recently broken in the context of 344 // |network_anonymization_key|. |alternative_service.host| must not be empty. 345 void MarkAlternativeServiceRecentlyBroken( 346 const AlternativeService& alternative_service, 347 const net::NetworkAnonymizationKey& network_anonymization_key); 348 349 // Returns true iff |alternative_service| is currently broken in the context 350 // of |network_anonymization_key|. |alternative_service.host| must not be 351 // empty. 352 bool IsAlternativeServiceBroken( 353 const AlternativeService& alternative_service, 354 const net::NetworkAnonymizationKey& network_anonymization_key) const; 355 356 // Returns true iff |alternative_service| was recently broken in the context 357 // of |network_anonymization_key|. |alternative_service.host| must not be 358 // empty. 359 bool WasAlternativeServiceRecentlyBroken( 360 const AlternativeService& alternative_service, 361 const net::NetworkAnonymizationKey& network_anonymization_key); 362 363 // Confirms that |alternative_service| is working in the context of 364 // |network_anonymization_key|. |alternative_service.host| must not be empty. 365 void ConfirmAlternativeService( 366 const AlternativeService& alternative_service, 367 const net::NetworkAnonymizationKey& network_anonymization_key); 368 369 // Called when the default network changes. 370 // Clears all the alternative services that were marked broken until the 371 // default network changed. 372 void OnDefaultNetworkChanged(); 373 374 // Returns all alternative service mappings as human readable strings. 375 // Empty alternative service hostnames will be printed as such. 376 base::Value GetAlternativeServiceInfoAsValue() const; 377 378 // Tracks the last local address when QUIC was known to work. The address 379 // cannot be set to an empty address - use 380 // ClearLastLocalAddressWhenQuicWorked() if it needs to be cleared. 381 bool WasLastLocalAddressWhenQuicWorked(const IPAddress& local_address) const; 382 bool HasLastLocalAddressWhenQuicWorked() const; 383 void SetLastLocalAddressWhenQuicWorked( 384 IPAddress last_local_address_when_quic_worked); 385 void ClearLastLocalAddressWhenQuicWorked(); 386 387 // Sets |stats| for |server|. 388 void SetServerNetworkStats( 389 const url::SchemeHostPort& server, 390 const NetworkAnonymizationKey& network_anonymization_key, 391 ServerNetworkStats stats); 392 393 // Clears any stats for |server|. 394 void ClearServerNetworkStats( 395 const url::SchemeHostPort& server, 396 const NetworkAnonymizationKey& network_anonymization_key); 397 398 // Returns any stats for |server| or nullptr if there are none. 399 const ServerNetworkStats* GetServerNetworkStats( 400 const url::SchemeHostPort& server, 401 const NetworkAnonymizationKey& network_anonymization_key); 402 403 // Save QuicServerInfo (in std::string form) for the given |server_id|, in the 404 // context of |network_anonymization_key|. 405 void SetQuicServerInfo( 406 const quic::QuicServerId& server_id, 407 const NetworkAnonymizationKey& network_anonymization_key, 408 const std::string& server_info); 409 410 // Get QuicServerInfo (in std::string form) for the given |server_id|, in the 411 // context of |network_anonymization_key|. 412 const std::string* GetQuicServerInfo( 413 const quic::QuicServerId& server_id, 414 const NetworkAnonymizationKey& network_anonymization_key); 415 416 // Returns all persistent QuicServerInfo objects. 417 const QuicServerInfoMap& quic_server_info_map() const; 418 419 // Returns the number of server configs (QuicServerInfo objects) persisted. 420 size_t max_server_configs_stored_in_properties() const; 421 422 // Sets the number of server configs (QuicServerInfo objects) to be persisted. 423 void SetMaxServerConfigsStoredInProperties( 424 size_t max_server_configs_stored_in_properties); 425 426 // If values are present, sets initial_delay and 427 // exponential_backoff_on_initial_delay which are used to calculate delay of 428 // broken alternative services. 429 void SetBrokenAlternativeServicesDelayParams( 430 absl::optional<base::TimeDelta> initial_delay, 431 absl::optional<bool> exponential_backoff_on_initial_delay); 432 433 // Returns whether HttpServerProperties is initialized. 434 bool IsInitialized() const; 435 436 // BrokenAlternativeServices::Delegate method. 437 void OnExpireBrokenAlternativeService( 438 const AlternativeService& expired_alternative_service, 439 const NetworkAnonymizationKey& network_anonymization_key) override; 440 441 static base::TimeDelta GetUpdatePrefsDelayForTesting(); 442 443 // Test-only routines that call the methods used to load the specified 444 // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked 445 // multiple times. OnServerInfoLoadedForTesting(std::unique_ptr<ServerInfoMap> server_info_map)446 void OnServerInfoLoadedForTesting( 447 std::unique_ptr<ServerInfoMap> server_info_map) { 448 OnServerInfoLoaded(std::move(server_info_map)); 449 } OnLastLocalAddressWhenQuicWorkedForTesting(const IPAddress & last_local_address_when_quic_worked)450 void OnLastLocalAddressWhenQuicWorkedForTesting( 451 const IPAddress& last_local_address_when_quic_worked) { 452 OnLastLocalAddressWhenQuicWorkedLoaded(last_local_address_when_quic_worked); 453 } OnQuicServerInfoMapLoadedForTesting(std::unique_ptr<QuicServerInfoMap> quic_server_info_map)454 void OnQuicServerInfoMapLoadedForTesting( 455 std::unique_ptr<QuicServerInfoMap> quic_server_info_map) { 456 OnQuicServerInfoMapLoaded(std::move(quic_server_info_map)); 457 } OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting(std::unique_ptr<BrokenAlternativeServiceList> broken_alternative_service_list,std::unique_ptr<RecentlyBrokenAlternativeServices> recently_broken_alternative_services)458 void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting( 459 std::unique_ptr<BrokenAlternativeServiceList> 460 broken_alternative_service_list, 461 std::unique_ptr<RecentlyBrokenAlternativeServices> 462 recently_broken_alternative_services) { 463 OnBrokenAndRecentlyBrokenAlternativeServicesLoaded( 464 std::move(broken_alternative_service_list), 465 std::move(recently_broken_alternative_services)); 466 } 467 GetCanonicalSuffixForTesting(const std::string & host)468 const std::string* GetCanonicalSuffixForTesting( 469 const std::string& host) const { 470 return GetCanonicalSuffix(host); 471 } 472 server_info_map_for_testing()473 const ServerInfoMap& server_info_map_for_testing() const { 474 return server_info_map_; 475 } 476 477 // This will invalidate the start-up properties if called before 478 // initialization. 479 void FlushWritePropertiesForTesting(base::OnceClosure callback); 480 broken_alternative_services_for_testing()481 const BrokenAlternativeServices& broken_alternative_services_for_testing() 482 const { 483 return broken_alternative_services_; 484 } 485 quic_server_info_map_for_testing()486 const QuicServerInfoMap& quic_server_info_map_for_testing() const { 487 return quic_server_info_map_; 488 } 489 490 // TODO(mmenke): Look into removing this. properties_manager_for_testing()491 HttpServerPropertiesManager* properties_manager_for_testing() { 492 return properties_manager_.get(); 493 } 494 495 private: 496 // TODO (wangyix): modify HttpServerProperties unit tests so this 497 // friendness is no longer required. 498 friend class HttpServerPropertiesPeer; 499 500 typedef base::flat_map<ServerInfoMapKey, url::SchemeHostPort> CanonicalMap; 501 typedef base::flat_map<QuicServerInfoMapKey, quic::QuicServerId> 502 QuicCanonicalMap; 503 typedef std::vector<std::string> CanonicalSuffixList; 504 505 // Internal implementations of public methods. SchemeHostPort argument must be 506 // normalized before calling (ws/wss replaced with http/https). Use wrapped 507 // functions instead of putting the normalization in the public functions to 508 // reduce chance of regression - normalization in ServerInfoMapKey's 509 // constructor would leave |server.scheme| as wrong if not access through the 510 // key, and explicit normalization to create |normalized_server| means the one 511 // with the incorrect scheme would still be available. 512 bool GetSupportsSpdyInternal( 513 url::SchemeHostPort server, 514 const net::NetworkAnonymizationKey& network_anonymization_key); 515 void SetSupportsSpdyInternal( 516 url::SchemeHostPort server, 517 const net::NetworkAnonymizationKey& network_anonymization_key, 518 bool supports_spdy); 519 bool RequiresHTTP11Internal( 520 url::SchemeHostPort server, 521 const net::NetworkAnonymizationKey& network_anonymization_key); 522 void SetHTTP11RequiredInternal( 523 url::SchemeHostPort server, 524 const net::NetworkAnonymizationKey& network_anonymization_key); 525 void MaybeForceHTTP11Internal( 526 url::SchemeHostPort server, 527 const net::NetworkAnonymizationKey& network_anonymization_key, 528 SSLConfig* ssl_config); 529 AlternativeServiceInfoVector GetAlternativeServiceInfosInternal( 530 const url::SchemeHostPort& origin, 531 const net::NetworkAnonymizationKey& network_anonymization_key); 532 void SetAlternativeServicesInternal( 533 const url::SchemeHostPort& origin, 534 const net::NetworkAnonymizationKey& network_anonymization_key, 535 const AlternativeServiceInfoVector& alternative_service_info_vector); 536 void SetServerNetworkStatsInternal( 537 url::SchemeHostPort server, 538 const NetworkAnonymizationKey& network_anonymization_key, 539 ServerNetworkStats stats); 540 void ClearServerNetworkStatsInternal( 541 url::SchemeHostPort server, 542 const NetworkAnonymizationKey& network_anonymization_key); 543 const ServerNetworkStats* GetServerNetworkStatsInternal( 544 url::SchemeHostPort server, 545 const NetworkAnonymizationKey& network_anonymization_key); 546 547 // Helper functions to use the passed in parameters and 548 // |use_network_anonymization_key_| to create a [Quic]ServerInfoMapKey. 549 ServerInfoMapKey CreateServerInfoKey( 550 const url::SchemeHostPort& server, 551 const NetworkAnonymizationKey& network_anonymization_key) const; 552 QuicServerInfoMapKey CreateQuicServerInfoKey( 553 const quic::QuicServerId& server_id, 554 const NetworkAnonymizationKey& network_anonymization_key) const; 555 556 // Return the iterator for |server| in the context of 557 // |network_anonymization_key|, or for its canonical host, or end. Skips over 558 // ServerInfos without |alternative_service_info| populated. 559 ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo( 560 const url::SchemeHostPort& server, 561 const net::NetworkAnonymizationKey& network_anonymization_key); 562 563 // Return the canonical host for |server| in the context of 564 // |network_anonymization_key|, or end if none exists. 565 CanonicalMap::const_iterator GetCanonicalAltSvcHost( 566 const url::SchemeHostPort& server, 567 const net::NetworkAnonymizationKey& network_anonymization_key) const; 568 569 // Return the canonical host with the same canonical suffix as |server|. 570 // The returned canonical host can be used to search for server info in 571 // |quic_server_info_map_|. Return 'end' the host doesn't exist. 572 QuicCanonicalMap::const_iterator GetCanonicalServerInfoHost( 573 const QuicServerInfoMapKey& key) const; 574 575 // Remove the canonical alt-svc host for |server| with 576 // |network_anonymization_key|. 577 void RemoveAltSvcCanonicalHost( 578 const url::SchemeHostPort& server, 579 const NetworkAnonymizationKey& network_anonymization_key); 580 581 // Update |canonical_server_info_map_| with the new canonical host. 582 // The |key| should have the corresponding server info associated with it 583 // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't 584 // have an entry associated with |key|, the method will add one. 585 void UpdateCanonicalServerInfoMap(const QuicServerInfoMapKey& key); 586 587 // Returns the canonical host suffix for |host|, or nullptr if none 588 // exists. 589 const std::string* GetCanonicalSuffix(const std::string& host) const; 590 591 void OnPrefsLoaded(std::unique_ptr<ServerInfoMap> server_info_map, 592 const IPAddress& last_local_address_when_quic_worked, 593 std::unique_ptr<QuicServerInfoMap> quic_server_info_map, 594 std::unique_ptr<BrokenAlternativeServiceList> 595 broken_alternative_service_list, 596 std::unique_ptr<RecentlyBrokenAlternativeServices> 597 recently_broken_alternative_services); 598 599 // These methods are called by OnPrefsLoaded to handle merging properties 600 // loaded from prefs with what has been learned while waiting for prefs to 601 // load. 602 void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map); 603 void OnLastLocalAddressWhenQuicWorkedLoaded( 604 const IPAddress& last_local_address_when_quic_worked); 605 void OnQuicServerInfoMapLoaded( 606 std::unique_ptr<QuicServerInfoMap> quic_server_info_map); 607 void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded( 608 std::unique_ptr<BrokenAlternativeServiceList> 609 broken_alternative_service_list, 610 std::unique_ptr<RecentlyBrokenAlternativeServices> 611 recently_broken_alternative_services); 612 613 // Queue a delayed call to WriteProperties(). If |is_initialized_| is false, 614 // or |properties_manager_| is nullptr, or there's already a queued call to 615 // WriteProperties(), does nothing. 616 void MaybeQueueWriteProperties(); 617 618 // Writes cached state to |properties_manager_|, which must not be null. 619 // Invokes |callback| on completion, if non-null. 620 void WriteProperties(base::OnceClosure callback) const; 621 622 raw_ptr<const base::TickClock> tick_clock_; // Unowned 623 raw_ptr<base::Clock> clock_; // Unowned 624 625 // Cached value of whether network state partitioning is enabled. Cached to 626 // improve performance. 627 const bool use_network_anonymization_key_; 628 629 // Set to true once initial properties have been retrieved from disk by 630 // |properties_manager_|. Always true if |properties_manager_| is nullptr. 631 bool is_initialized_; 632 633 // Queue a write when resources finish loading. Set to true when 634 // MaybeQueueWriteProperties() is invoked while still waiting on 635 // initialization to complete. 636 bool queue_write_on_load_ = false; 637 638 // Used to load/save properties from/to preferences. May be nullptr. 639 std::unique_ptr<HttpServerPropertiesManager> properties_manager_; 640 641 ServerInfoMap server_info_map_; 642 643 BrokenAlternativeServices broken_alternative_services_; 644 645 IPAddress last_local_address_when_quic_worked_; 646 // Contains a map of servers which could share the same alternate protocol. 647 // Map from a Canonical scheme/host/port/NIK (host is some postfix of host 648 // names) to an actual origin, which has a plausible alternate protocol 649 // mapping. 650 CanonicalMap canonical_alt_svc_map_; 651 652 // Contains list of suffixes (for example ".c.youtube.com", 653 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames. 654 const CanonicalSuffixList canonical_suffixes_; 655 656 QuicServerInfoMap quic_server_info_map_; 657 658 // Maps canonical suffixes to host names that have the same canonical suffix 659 // and have a corresponding entry in |quic_server_info_map_|. The map can be 660 // used to quickly look for server info for hosts that share the same 661 // canonical suffix but don't have exact match in |quic_server_info_map_|. The 662 // map exists solely to improve the search performance. It only contains 663 // derived data that can be recalculated by traversing 664 // |quic_server_info_map_|. 665 QuicCanonicalMap canonical_server_info_map_; 666 667 size_t max_server_configs_stored_in_properties_; 668 669 // Used to post calls to WriteProperties(). 670 base::OneShotTimer prefs_update_timer_; 671 672 THREAD_CHECKER(thread_checker_); 673 }; 674 675 } // namespace net 676 677 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_ 678