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