http_server_properties.h 28 KB

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