File: http_server_properties.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (676 lines) | stat: -rw-r--r-- 28,908 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_H_
#define NET_HTTP_HTTP_SERVER_PROPERTIES_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#include "base/containers/lru_cache.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_address.h"
#include "net/base/net_export.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/privacy_mode.h"
#include "net/http/alternative_service.h"
#include "net/http/broken_alternative_services.h"
#include "net/third_party/quiche/src/quiche/http2/core/spdy_framer.h"  // TODO(willchan): Reconsider this.
#include "net/third_party/quiche/src/quiche/http2/core/spdy_protocol.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_bandwidth.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
#include "url/scheme_host_port.h"

namespace base {
class Clock;
class TickClock;
}

namespace net {

class HttpServerPropertiesManager;
class IPAddress;
class NetLog;
struct SSLConfig;

struct NET_EXPORT SupportsQuic {
  SupportsQuic() : used_quic(false) {}
  SupportsQuic(bool used_quic, const std::string& address)
      : used_quic(used_quic), address(address) {}

  bool Equals(const SupportsQuic& other) const {
    return used_quic == other.used_quic && address == other.address;
  }

  bool used_quic;
  std::string address;
};

struct NET_EXPORT ServerNetworkStats {
  ServerNetworkStats() : bandwidth_estimate(quic::QuicBandwidth::Zero()) {}

  friend bool operator==(const ServerNetworkStats&,
                         const ServerNetworkStats&) = default;

  base::TimeDelta srtt;
  quic::QuicBandwidth bandwidth_estimate;
};

typedef std::vector<AlternativeService> AlternativeServiceVector;

// Store at most 200 MRU RecentlyBrokenAlternativeServices in memory and disk.
// This ideally would be with the other constants in HttpServerProperties, but
// has to go here instead of prevent a circular dependency.
const int kMaxRecentlyBrokenAlternativeServiceEntries = 200;

// Store at most 5 MRU QUIC servers by default. This is mainly used by cronet.
const int kDefaultMaxQuicServerEntries = 5;

// The interface for setting/retrieving the HTTP server properties.
// Currently, this class manages servers':
// * HTTP/2 support;
// * Alternative Service support;
// * QUIC data (like ServerNetworkStats and QuicServerInfo).
//
// Optionally retrieves and saves properties from/to disk. This class is not
// threadsafe.
class NET_EXPORT HttpServerProperties
    : public BrokenAlternativeServices::Delegate {
 public:
  // Store at most 500 MRU ServerInfos in memory and disk.
  static const int kMaxServerInfoEntries = 500;

  // Provides an interface to interact with persistent preferences storage
  // implemented by the embedder. The prefs are assumed not to have been loaded
  // before HttpServerPropertiesManager construction.
  class NET_EXPORT PrefDelegate {
   public:
    virtual ~PrefDelegate();

    // Returns the branch of the preferences system for the server properties.
    // Returns nullptr if the pref system has no data for the server properties.
    virtual const base::Value::Dict& GetServerProperties() const = 0;

    // Sets the server properties to the given value. If |callback| is
    // non-empty, flushes data to persistent storage and invokes |callback|
    // asynchronously when complete.
    virtual void SetServerProperties(base::Value::Dict dict,
                                     base::OnceClosure callback) = 0;

    // Starts listening for prefs to be loaded. If prefs are already loaded,
    // |pref_loaded_callback| will be invoked asynchronously. Callback will be
    // invoked even if prefs fail to load. Will only be called once by the
    // HttpServerPropertiesManager.
    virtual void WaitForPrefLoad(base::OnceClosure pref_loaded_callback) = 0;
  };

  // Contains metadata about a particular server. Note that all methods that
  // take a "SchemeHostPort" expect schemes of ws and wss to be mapped to http
  // and https, respectively. See GetNormalizedSchemeHostPort().
  struct NET_EXPORT ServerInfo {
    ServerInfo();
    ServerInfo(const ServerInfo& server_info);
    ServerInfo(ServerInfo&& server_info);
    ~ServerInfo();

    // Returns true if no fields are populated.
    bool empty() const;

    // Used in tests.
    bool operator==(const ServerInfo& other) const;

    // IMPORTANT:  When adding a field here, be sure to update
    // HttpServerProperties::OnServerInfoLoaded() as well as
    // HttpServerPropertiesManager to correctly load/save the from/to the pref
    // store.

    // Whether or not a server is known to support H2/SPDY. False indicates
    // known lack of support, true indicates known support, and not set
    // indicates unknown. The difference between false and not set only matters
    // when loading from disk, when an initialized false value will take
    // priority over a not set value.
    std::optional<bool> supports_spdy;

    // True if the server has previously indicated it required HTTP/1.1. Unlike
    // other fields, not persisted to disk.
    std::optional<bool> requires_http11;

    std::optional<AlternativeServiceInfoVector> alternative_services;
    std::optional<ServerNetworkStats> server_network_stats;
  };

  struct NET_EXPORT ServerInfoMapKey {
    // If |use_network_anonymization_key| is false, an empty
    // NetworkAnonymizationKey is used instead of |network_anonymization_key|.
    // Note that |server| can be passed in via std::move(), since most callsites
    // can pass a recently created SchemeHostPort.
    ServerInfoMapKey(url::SchemeHostPort server,
                     const NetworkAnonymizationKey& network_anonymization_key,
                     bool use_network_anonymization_key);
    ~ServerInfoMapKey();

    bool operator<(const ServerInfoMapKey& other) const;

    // IMPORTANT: The constructor normalizes the scheme so that "ws" is replaced
    // by "http" and "wss" by "https", so this should never be compared directly
    // with values passed into to HttpServerProperties methods.
    url::SchemeHostPort server;

    NetworkAnonymizationKey network_anonymization_key;
  };

  class NET_EXPORT ServerInfoMap
      : public base::LRUCache<ServerInfoMapKey, ServerInfo> {
   public:
    ServerInfoMap();

    ServerInfoMap(const ServerInfoMap&) = delete;
    ServerInfoMap& operator=(const ServerInfoMap&) = delete;

    // If there's an entry corresponding to |key|, brings that entry to the
    // front and returns an iterator to it. Otherwise, inserts an empty
    // ServerInfo using |key|, and returns an iterator to it.
    iterator GetOrPut(const ServerInfoMapKey& key);

    // Erases the ServerInfo identified by |server_info_it| if no fields have
    // data. The iterator must point to an entry in the map. Regardless of
    // whether the entry is removed or not, returns iterator for the next entry.
    iterator EraseIfEmpty(iterator server_info_it);
  };

  struct NET_EXPORT QuicServerInfoMapKey {
    // If |use_network_anonymization_key| is false, an empty
    // NetworkAnonymizationKey is used instead of |network_anonymization_key|.
    QuicServerInfoMapKey(
        const quic::QuicServerId& server_id,
        PrivacyMode privacy_mode,
        const NetworkAnonymizationKey& network_anonymization_key,
        bool use_network_anonymization_key);
    ~QuicServerInfoMapKey();

    bool operator<(const QuicServerInfoMapKey& other) const;

    // Used in tests.
    bool operator==(const QuicServerInfoMapKey& other) const;

    quic::QuicServerId server_id;
    PrivacyMode privacy_mode = PRIVACY_MODE_DISABLED;
    NetworkAnonymizationKey network_anonymization_key;
  };

  // Max number of quic servers to store is not hardcoded and can be set.
  // Because of this, QuicServerInfoMap will not be a subclass of LRUCache.
  // Separate from ServerInfoMap because the key includes privacy mode (Since
  // this is analogous to the SSL session cache, which has separate caches for
  // privacy mode), and each entry can be quite large, so it has its own size
  // limit, which is much smaller than the ServerInfoMap's limit.
  typedef base::LRUCache<QuicServerInfoMapKey, std::string> QuicServerInfoMap;

  // If a |pref_delegate| is specified, it will be used to read/write the
  // properties to a pref file. Writes are rate limited to improve performance.
  //
  // |tick_clock| is used for setting expiration times and scheduling the
  // expiration of broken alternative services. If null, default clock will be
  // used.
  //
  // |clock| is used for converting base::TimeTicks to base::Time for
  // wherever base::Time is preferable.
  explicit HttpServerProperties(
      std::unique_ptr<PrefDelegate> pref_delegate = nullptr,
      NetLog* net_log = nullptr,
      const base::TickClock* tick_clock = nullptr,
      base::Clock* clock = nullptr);

  HttpServerProperties(const HttpServerProperties&) = delete;
  HttpServerProperties& operator=(const HttpServerProperties&) = delete;

  ~HttpServerProperties() override;

  // Deletes all data. If |callback| is non-null, flushes data to disk
  // and invokes the callback asynchronously once changes have been written to
  // disk.
  void Clear(base::OnceClosure callback);

  // Returns true if `server`, in the context of `network_anonymization_key`,
  // has previously supported a network protocol which honors request
  // prioritization. `server` must have either http:// or https:// schemes.
  //
  // Note that this also implies that the server supports request
  // multiplexing, since priorities imply a relationship between
  // multiple requests.
  bool SupportsRequestPriority(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Returns the value set by SetSupportsSpdy(). If not set, returns false.
  // `server` must have either http:// or https:// schemes.
  bool GetSupportsSpdy(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Records whether |server| supports H2 or not. Information is restricted to
  // the context of |network_anonymization_key|, to prevent cross-site
  // information leakage.
  void SetSupportsSpdy(const url::SchemeHostPort& server,
                       const NetworkAnonymizationKey& network_anonymization_key,
                       bool supports_spdy);

  // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code, in
  // the context of |network_anonymization_key|.
  //
  // Any relevant HostMappingRules must already have been applied to `server`.
  bool RequiresHTTP11(const url::SchemeHostPort& server,
                      const NetworkAnonymizationKey& network_anonymization_key);

  // Require HTTP/1.1 on subsequent connections, in the context of
  // |network_anonymization_key|.  Not persisted.
  //
  // Any relevant HostMappingRules must already have been applied to `server`.
  void SetHTTP11Required(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Modify SSLConfig to force HTTP/1.1 if necessary.
  //
  // Any relevant HostMappingRules must already have been applied to `server`.
  void MaybeForceHTTP11(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key,
      SSLConfig* ssl_config);

  // Return all alternative services for |origin|, learned in the context of
  // |network_anonymization_key|, including broken ones. Returned alternative
  // services never have empty hostnames.
  AlternativeServiceInfoVector GetAlternativeServiceInfos(
      const url::SchemeHostPort& origin,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Set a single HTTP/2 alternative service for |origin|.  Previous
  // alternative services for |origin| are discarded.
  // |alternative_service.host| may be empty.
  void SetHttp2AlternativeService(
      const url::SchemeHostPort& origin,
      const NetworkAnonymizationKey& network_anonymization_key,
      const AlternativeService& alternative_service,
      base::Time expiration);

  // Set a single QUIC alternative service for |origin|.  Previous alternative
  // services for |origin| are discarded.
  // |alternative_service.host| may be empty.
  void SetQuicAlternativeService(
      const url::SchemeHostPort& origin,
      const NetworkAnonymizationKey& network_anonymization_key,
      const AlternativeService& alternative_service,
      base::Time expiration,
      const quic::ParsedQuicVersionVector& advertised_versions);

  // Set alternative services for |origin|, learned in the context of
  // |network_anonymization_key|.  Previous alternative services for |origin|
  // are discarded. Hostnames in |alternative_service_info_vector| may be empty.
  // |alternative_service_info_vector| may be empty.
  void SetAlternativeServices(
      const url::SchemeHostPort& origin,
      const NetworkAnonymizationKey& network_anonymization_key,
      const AlternativeServiceInfoVector& alternative_service_info_vector);

  // Marks |alternative_service| as broken in the context of
  // |network_anonymization_key|. |alternative_service.host| must not be empty.
  void MarkAlternativeServiceBroken(
      const AlternativeService& alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Marks |alternative_service| as broken in the context of
  // |network_anonymization_key| until the default network changes.
  // |alternative_service.host| must not be empty.
  void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
      const AlternativeService& alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Marks |alternative_service| as recently broken in the context of
  // |network_anonymization_key|. |alternative_service.host| must not be empty.
  void MarkAlternativeServiceRecentlyBroken(
      const AlternativeService& alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Returns true iff |alternative_service| is currently broken in the context
  // of |network_anonymization_key|. |alternative_service.host| must not be
  // empty.
  bool IsAlternativeServiceBroken(
      const AlternativeService& alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key) const;

  // Returns true iff |alternative_service| was recently broken in the context
  // of |network_anonymization_key|. |alternative_service.host| must not be
  // empty.
  bool WasAlternativeServiceRecentlyBroken(
      const AlternativeService& alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Confirms that |alternative_service| is working in the context of
  // |network_anonymization_key|. |alternative_service.host| must not be empty.
  void ConfirmAlternativeService(
      const AlternativeService& alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Called when the default network changes.
  // Clears all the alternative services that were marked broken until the
  // default network changed.
  void OnDefaultNetworkChanged();

  // Returns all alternative service mappings as human readable strings.
  // Empty alternative service hostnames will be printed as such.
  base::Value GetAlternativeServiceInfoAsValue() const;

  // Tracks the last local address when QUIC was known to work. The address
  // cannot be set to an empty address - use
  // ClearLastLocalAddressWhenQuicWorked() if it needs to be cleared.
  bool WasLastLocalAddressWhenQuicWorked(const IPAddress& local_address) const;
  bool HasLastLocalAddressWhenQuicWorked() const;
  void SetLastLocalAddressWhenQuicWorked(
      IPAddress last_local_address_when_quic_worked);
  void ClearLastLocalAddressWhenQuicWorked();

  // Sets |stats| for |server|.
  void SetServerNetworkStats(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key,
      ServerNetworkStats stats);

  // Clears any stats for |server|.
  void ClearServerNetworkStats(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Returns any stats for |server| or nullptr if there are none.
  const ServerNetworkStats* GetServerNetworkStats(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Save QuicServerInfo (in std::string form) for the given |server_id|, in the
  // context of |network_anonymization_key|.
  void SetQuicServerInfo(
      const quic::QuicServerId& server_id,
      PrivacyMode privacy_mode,
      const NetworkAnonymizationKey& network_anonymization_key,
      const std::string& server_info);

  // Get QuicServerInfo (in std::string form) for the given |server_id|, in the
  // context of |network_anonymization_key|.
  const std::string* GetQuicServerInfo(
      const quic::QuicServerId& server_id,
      PrivacyMode privacy_mode,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Returns all persistent QuicServerInfo objects.
  const QuicServerInfoMap& quic_server_info_map() const;

  // Returns the number of server configs (QuicServerInfo objects) persisted.
  size_t max_server_configs_stored_in_properties() const;

  // Sets the number of server configs (QuicServerInfo objects) to be persisted.
  void SetMaxServerConfigsStoredInProperties(
      size_t max_server_configs_stored_in_properties);

  // If values are present, sets initial_delay and
  // exponential_backoff_on_initial_delay which are used to calculate delay of
  // broken alternative services.
  void SetBrokenAlternativeServicesDelayParams(
      std::optional<base::TimeDelta> initial_delay,
      std::optional<bool> exponential_backoff_on_initial_delay);

  // Returns whether HttpServerProperties is initialized.
  bool IsInitialized() const;

  // BrokenAlternativeServices::Delegate method.
  void OnExpireBrokenAlternativeService(
      const AlternativeService& expired_alternative_service,
      const NetworkAnonymizationKey& network_anonymization_key) override;

  static base::TimeDelta GetUpdatePrefsDelayForTesting();

  // Test-only routines that call the methods used to load the specified
  // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked
  // multiple times.
  void OnServerInfoLoadedForTesting(
      std::unique_ptr<ServerInfoMap> server_info_map) {
    OnServerInfoLoaded(std::move(server_info_map));
  }
  void OnLastLocalAddressWhenQuicWorkedForTesting(
      const IPAddress& last_local_address_when_quic_worked) {
    OnLastLocalAddressWhenQuicWorkedLoaded(last_local_address_when_quic_worked);
  }
  void OnQuicServerInfoMapLoadedForTesting(
      std::unique_ptr<QuicServerInfoMap> quic_server_info_map) {
    OnQuicServerInfoMapLoaded(std::move(quic_server_info_map));
  }
  void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting(
      std::unique_ptr<BrokenAlternativeServiceList>
          broken_alternative_service_list,
      std::unique_ptr<RecentlyBrokenAlternativeServices>
          recently_broken_alternative_services) {
    OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
        std::move(broken_alternative_service_list),
        std::move(recently_broken_alternative_services));
  }

  const std::string* GetCanonicalSuffixForTesting(
      const std::string& host) const {
    return GetCanonicalSuffix(host);
  }

  const ServerInfoMap& server_info_map_for_testing() const {
    return server_info_map_;
  }

  // This will invalidate the start-up properties if called before
  // initialization.
  void FlushWritePropertiesForTesting(base::OnceClosure callback);

  const BrokenAlternativeServices& broken_alternative_services_for_testing()
      const {
    return broken_alternative_services_;
  }

  const QuicServerInfoMap& quic_server_info_map_for_testing() const {
    return quic_server_info_map_;
  }

  // TODO(mmenke): Look into removing this.
  HttpServerPropertiesManager* properties_manager_for_testing() {
    return properties_manager_.get();
  }

 private:
  // TODO (wangyix): modify HttpServerProperties unit tests so this
  // friendness is no longer required.
  friend class HttpServerPropertiesPeer;

  typedef base::flat_map<ServerInfoMapKey, url::SchemeHostPort> CanonicalMap;
  typedef base::flat_map<QuicServerInfoMapKey, quic::QuicServerId>
      QuicCanonicalMap;
  typedef std::vector<std::string> CanonicalSuffixList;

  // Internal implementations of public methods. SchemeHostPort argument must be
  // normalized before calling (ws/wss replaced with http/https). Use wrapped
  // functions instead of putting the normalization in the public functions to
  // reduce chance of regression - normalization in ServerInfoMapKey's
  // constructor would leave |server.scheme| as wrong if not access through the
  // key, and explicit normalization to create |normalized_server| means the one
  // with the incorrect scheme would still be available.
  bool RequiresHTTP11Internal(
      url::SchemeHostPort server,
      const NetworkAnonymizationKey& network_anonymization_key);
  void SetHTTP11RequiredInternal(
      url::SchemeHostPort server,
      const NetworkAnonymizationKey& network_anonymization_key);
  void MaybeForceHTTP11Internal(
      url::SchemeHostPort server,
      const NetworkAnonymizationKey& network_anonymization_key,
      SSLConfig* ssl_config);
  AlternativeServiceInfoVector GetAlternativeServiceInfosInternal(
      const url::SchemeHostPort& origin,
      const NetworkAnonymizationKey& network_anonymization_key);
  void SetAlternativeServicesInternal(
      const url::SchemeHostPort& origin,
      const NetworkAnonymizationKey& network_anonymization_key,
      const AlternativeServiceInfoVector& alternative_service_info_vector);
  void SetServerNetworkStatsInternal(
      url::SchemeHostPort server,
      const NetworkAnonymizationKey& network_anonymization_key,
      ServerNetworkStats stats);
  void ClearServerNetworkStatsInternal(
      url::SchemeHostPort server,
      const NetworkAnonymizationKey& network_anonymization_key);
  const ServerNetworkStats* GetServerNetworkStatsInternal(
      url::SchemeHostPort server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Helper functions to use the passed in parameters and
  // |use_network_anonymization_key_| to create a [Quic]ServerInfoMapKey.
  ServerInfoMapKey CreateServerInfoKey(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key) const;
  QuicServerInfoMapKey CreateQuicServerInfoKey(
      const quic::QuicServerId& server_id,
      PrivacyMode privacy_mode,
      const NetworkAnonymizationKey& network_anonymization_key) const;

  // Return the iterator for |server| in the context of
  // |network_anonymization_key|, or for its canonical host, or end. Skips over
  // ServerInfos without |alternative_service_info| populated.
  ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Return the canonical host for |server|  in the context of
  // |network_anonymization_key|, or end if none exists.
  CanonicalMap::const_iterator GetCanonicalAltSvcHost(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key) const;

  // Return the canonical host with the same canonical suffix as |server|.
  // The returned canonical host can be used to search for server info in
  // |quic_server_info_map_|. Return 'end' the host doesn't exist.
  QuicCanonicalMap::const_iterator GetCanonicalServerInfoHost(
      const QuicServerInfoMapKey& key) const;

  // Remove the canonical alt-svc host for |server| with
  // |network_anonymization_key|.
  void RemoveAltSvcCanonicalHost(
      const url::SchemeHostPort& server,
      const NetworkAnonymizationKey& network_anonymization_key);

  // Update |canonical_server_info_map_| with the new canonical host.
  // The |key| should have the corresponding server info associated with it
  // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't
  // have an entry associated with |key|, the method will add one.
  void UpdateCanonicalServerInfoMap(const QuicServerInfoMapKey& key);

  // Returns the canonical host suffix for |host|, or nullptr if none
  // exists.
  const std::string* GetCanonicalSuffix(const std::string& host) const;

  void OnPrefsLoaded(std::unique_ptr<ServerInfoMap> server_info_map,
                     const IPAddress& last_local_address_when_quic_worked,
                     std::unique_ptr<QuicServerInfoMap> quic_server_info_map,
                     std::unique_ptr<BrokenAlternativeServiceList>
                         broken_alternative_service_list,
                     std::unique_ptr<RecentlyBrokenAlternativeServices>
                         recently_broken_alternative_services);

  // These methods are called by OnPrefsLoaded to handle merging properties
  // loaded from prefs with what has been learned while waiting for prefs to
  // load.
  void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map);
  void OnLastLocalAddressWhenQuicWorkedLoaded(
      const IPAddress& last_local_address_when_quic_worked);
  void OnQuicServerInfoMapLoaded(
      std::unique_ptr<QuicServerInfoMap> quic_server_info_map);
  void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
      std::unique_ptr<BrokenAlternativeServiceList>
          broken_alternative_service_list,
      std::unique_ptr<RecentlyBrokenAlternativeServices>
          recently_broken_alternative_services);

  // Queue a delayed call to WriteProperties(). If |is_initialized_| is false,
  // or |properties_manager_| is nullptr, or there's already a queued call to
  // WriteProperties(), does nothing.
  void MaybeQueueWriteProperties();

  // Writes cached state to |properties_manager_|, which must not be null.
  // Invokes |callback| on completion, if non-null.
  void WriteProperties(base::OnceClosure callback) const;

  raw_ptr<const base::TickClock> tick_clock_;  // Unowned
  raw_ptr<base::Clock> clock_;                 // Unowned

  // Cached value of whether network state partitioning is enabled. Cached to
  // improve performance.
  const bool use_network_anonymization_key_;

  // Set to true once initial properties have been retrieved from disk by
  // |properties_manager_|. Always true if |properties_manager_| is nullptr.
  bool is_initialized_;

  // Queue a write when resources finish loading. Set to true when
  // MaybeQueueWriteProperties() is invoked while still waiting on
  // initialization to complete.
  bool queue_write_on_load_ = false;

  // Used to load/save properties from/to preferences. May be nullptr.
  std::unique_ptr<HttpServerPropertiesManager> properties_manager_;

  ServerInfoMap server_info_map_;

  BrokenAlternativeServices broken_alternative_services_;

  IPAddress last_local_address_when_quic_worked_;
  // Contains a map of servers which could share the same alternate protocol.
  // Map from a Canonical scheme/host/port/NAK (host is some postfix of host
  // names) to an actual origin, which has a plausible alternate protocol
  // mapping.
  CanonicalMap canonical_alt_svc_map_;

  // Contains list of suffixes (for example ".c.youtube.com",
  // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
  const CanonicalSuffixList canonical_suffixes_;

  QuicServerInfoMap quic_server_info_map_;

  // Maps canonical suffixes to host names that have the same canonical suffix
  // and have a corresponding entry in |quic_server_info_map_|. The map can be
  // used to quickly look for server info for hosts that share the same
  // canonical suffix but don't have exact match in |quic_server_info_map_|. The
  // map exists solely to improve the search performance. It only contains
  // derived data that can be recalculated by traversing
  // |quic_server_info_map_|.
  QuicCanonicalMap canonical_server_info_map_;

  size_t max_server_configs_stored_in_properties_;

  // Used to post calls to WriteProperties().
  base::OneShotTimer prefs_update_timer_;

  THREAD_CHECKER(thread_checker_);
};

}  // namespace net

#endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_H_