http_server_properties.cc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/http/http_server_properties.h"
  5. #include "base/bind.h"
  6. #include "base/check_op.h"
  7. #include "base/containers/adapters.h"
  8. #include "base/containers/contains.h"
  9. #include "base/feature_list.h"
  10. #include "base/location.h"
  11. #include "base/metrics/histogram_macros.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/strings/stringprintf.h"
  14. #include "base/task/single_thread_task_runner.h"
  15. #include "base/threading/thread_task_runner_handle.h"
  16. #include "base/time/default_clock.h"
  17. #include "base/time/default_tick_clock.h"
  18. #include "base/values.h"
  19. #include "net/base/features.h"
  20. #include "net/base/url_util.h"
  21. #include "net/http/http_network_session.h"
  22. #include "net/http/http_server_properties_manager.h"
  23. #include "net/socket/ssl_client_socket.h"
  24. #include "net/ssl/ssl_config.h"
  25. namespace net {
  26. namespace {
  27. // Time to wait before starting an update the preferences from the
  28. // http_server_properties_impl_ cache. Scheduling another update during this
  29. // period will be a no-op.
  30. constexpr base::TimeDelta kUpdatePrefsDelay = base::Seconds(60);
  31. url::SchemeHostPort NormalizeSchemeHostPort(
  32. const url::SchemeHostPort& scheme_host_port) {
  33. if (scheme_host_port.scheme() == url::kWssScheme) {
  34. return url::SchemeHostPort(url::kHttpsScheme, scheme_host_port.host(),
  35. scheme_host_port.port());
  36. }
  37. if (scheme_host_port.scheme() == url::kWsScheme) {
  38. return url::SchemeHostPort(url::kHttpScheme, scheme_host_port.host(),
  39. scheme_host_port.port());
  40. }
  41. return scheme_host_port;
  42. }
  43. } // namespace
  44. HttpServerProperties::PrefDelegate::~PrefDelegate() = default;
  45. HttpServerProperties::ServerInfo::ServerInfo() = default;
  46. HttpServerProperties::ServerInfo::ServerInfo(const ServerInfo& server_info) =
  47. default;
  48. HttpServerProperties::ServerInfo::ServerInfo(ServerInfo&& server_info) =
  49. default;
  50. HttpServerProperties::ServerInfo::~ServerInfo() = default;
  51. bool HttpServerProperties::ServerInfo::empty() const {
  52. return !supports_spdy.has_value() && !alternative_services.has_value() &&
  53. !server_network_stats.has_value();
  54. }
  55. bool HttpServerProperties::ServerInfo::operator==(
  56. const ServerInfo& other) const {
  57. return supports_spdy == other.supports_spdy &&
  58. alternative_services == other.alternative_services &&
  59. server_network_stats == other.server_network_stats;
  60. }
  61. HttpServerProperties::ServerInfoMapKey::ServerInfoMapKey(
  62. url::SchemeHostPort server,
  63. const NetworkIsolationKey& network_isolation_key,
  64. bool use_network_isolation_key)
  65. : server(std::move(server)),
  66. network_isolation_key(use_network_isolation_key ? network_isolation_key
  67. : NetworkIsolationKey()) {
  68. // Scheme should have been normalized before this method was called.
  69. DCHECK_NE(this->server.scheme(), url::kWsScheme);
  70. DCHECK_NE(this->server.scheme(), url::kWssScheme);
  71. }
  72. HttpServerProperties::ServerInfoMapKey::~ServerInfoMapKey() = default;
  73. bool HttpServerProperties::ServerInfoMapKey::operator<(
  74. const ServerInfoMapKey& other) const {
  75. return std::tie(server, network_isolation_key) <
  76. std::tie(other.server, other.network_isolation_key);
  77. }
  78. HttpServerProperties::QuicServerInfoMapKey::QuicServerInfoMapKey(
  79. const quic::QuicServerId& server_id,
  80. const NetworkIsolationKey& network_isolation_key,
  81. bool use_network_isolation_key)
  82. : server_id(server_id),
  83. network_isolation_key(use_network_isolation_key ? network_isolation_key
  84. : NetworkIsolationKey()) {
  85. }
  86. HttpServerProperties::QuicServerInfoMapKey::~QuicServerInfoMapKey() = default;
  87. bool HttpServerProperties::QuicServerInfoMapKey::operator<(
  88. const QuicServerInfoMapKey& other) const {
  89. return std::tie(server_id, network_isolation_key) <
  90. std::tie(other.server_id, other.network_isolation_key);
  91. }
  92. // Used in tests.
  93. bool HttpServerProperties::QuicServerInfoMapKey::operator==(
  94. const QuicServerInfoMapKey& other) const {
  95. return std::tie(server_id, network_isolation_key) ==
  96. std::tie(other.server_id, other.network_isolation_key);
  97. }
  98. HttpServerProperties::ServerInfoMap::ServerInfoMap()
  99. : base::LRUCache<ServerInfoMapKey, ServerInfo>(kMaxServerInfoEntries) {}
  100. HttpServerProperties::ServerInfoMap::iterator
  101. HttpServerProperties::ServerInfoMap::GetOrPut(const ServerInfoMapKey& key) {
  102. auto it = Get(key);
  103. if (it != end())
  104. return it;
  105. return Put(key, ServerInfo());
  106. }
  107. HttpServerProperties::ServerInfoMap::iterator
  108. HttpServerProperties::ServerInfoMap::EraseIfEmpty(iterator server_info_it) {
  109. if (server_info_it->second.empty())
  110. return Erase(server_info_it);
  111. return ++server_info_it;
  112. }
  113. HttpServerProperties::HttpServerProperties(
  114. std::unique_ptr<PrefDelegate> pref_delegate,
  115. NetLog* net_log,
  116. const base::TickClock* tick_clock,
  117. base::Clock* clock)
  118. : tick_clock_(tick_clock ? tick_clock
  119. : base::DefaultTickClock::GetInstance()),
  120. clock_(clock ? clock : base::DefaultClock::GetInstance()),
  121. use_network_isolation_key_(base::FeatureList::IsEnabled(
  122. features::kPartitionHttpServerPropertiesByNetworkIsolationKey)),
  123. is_initialized_(pref_delegate.get() == nullptr),
  124. properties_manager_(
  125. pref_delegate
  126. ? std::make_unique<HttpServerPropertiesManager>(
  127. std::move(pref_delegate),
  128. base::BindOnce(&HttpServerProperties::OnPrefsLoaded,
  129. base::Unretained(this)),
  130. kDefaultMaxQuicServerEntries,
  131. net_log,
  132. tick_clock_)
  133. : nullptr),
  134. broken_alternative_services_(kMaxRecentlyBrokenAlternativeServiceEntries,
  135. this,
  136. tick_clock_),
  137. canonical_suffixes_({".ggpht.com", ".c.youtube.com", ".googlevideo.com",
  138. ".googleusercontent.com", ".gvt1.com"}),
  139. quic_server_info_map_(kDefaultMaxQuicServerEntries),
  140. max_server_configs_stored_in_properties_(kDefaultMaxQuicServerEntries) {}
  141. HttpServerProperties::~HttpServerProperties() {
  142. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  143. if (properties_manager_) {
  144. // Stop waiting for initial settings.
  145. is_initialized_ = true;
  146. // Stop the timer if it's running, since this will write to the properties
  147. // file immediately.
  148. prefs_update_timer_.Stop();
  149. WriteProperties(base::OnceClosure());
  150. }
  151. }
  152. void HttpServerProperties::Clear(base::OnceClosure callback) {
  153. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  154. server_info_map_.Clear();
  155. broken_alternative_services_.Clear();
  156. canonical_alt_svc_map_.clear();
  157. last_local_address_when_quic_worked_ = IPAddress();
  158. quic_server_info_map_.Clear();
  159. canonical_server_info_map_.clear();
  160. if (properties_manager_) {
  161. // Stop waiting for initial settings.
  162. is_initialized_ = true;
  163. // Leaving this as-is doesn't actually have any effect, if it's true, but
  164. // seems best to be safe.
  165. queue_write_on_load_ = false;
  166. // Stop the timer if it's running, since this will write to the properties
  167. // file immediately.
  168. prefs_update_timer_.Stop();
  169. WriteProperties(std::move(callback));
  170. } else if (callback) {
  171. base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  172. std::move(callback));
  173. }
  174. }
  175. bool HttpServerProperties::SupportsRequestPriority(
  176. const url::SchemeHostPort& server,
  177. const net::NetworkIsolationKey& network_isolation_key) {
  178. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  179. if (server.host().empty())
  180. return false;
  181. if (GetSupportsSpdy(server, network_isolation_key))
  182. return true;
  183. const AlternativeServiceInfoVector alternative_service_info_vector =
  184. GetAlternativeServiceInfos(server, network_isolation_key);
  185. for (const AlternativeServiceInfo& alternative_service_info :
  186. alternative_service_info_vector) {
  187. if (alternative_service_info.alternative_service().protocol == kProtoQUIC) {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. bool HttpServerProperties::GetSupportsSpdy(
  194. const url::SchemeHostPort& server,
  195. const net::NetworkIsolationKey& network_isolation_key) {
  196. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  197. return GetSupportsSpdyInternal(NormalizeSchemeHostPort(server),
  198. network_isolation_key);
  199. }
  200. void HttpServerProperties::SetSupportsSpdy(
  201. const url::SchemeHostPort& server,
  202. const net::NetworkIsolationKey& network_isolation_key,
  203. bool supports_spdy) {
  204. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  205. SetSupportsSpdyInternal(NormalizeSchemeHostPort(server),
  206. network_isolation_key, supports_spdy);
  207. }
  208. bool HttpServerProperties::RequiresHTTP11(
  209. const url::SchemeHostPort& server,
  210. const net::NetworkIsolationKey& network_isolation_key) {
  211. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  212. return RequiresHTTP11Internal(NormalizeSchemeHostPort(server),
  213. network_isolation_key);
  214. }
  215. void HttpServerProperties::SetHTTP11Required(
  216. const url::SchemeHostPort& server,
  217. const net::NetworkIsolationKey& network_isolation_key) {
  218. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  219. SetHTTP11RequiredInternal(NormalizeSchemeHostPort(server),
  220. network_isolation_key);
  221. }
  222. void HttpServerProperties::MaybeForceHTTP11(
  223. const url::SchemeHostPort& server,
  224. const net::NetworkIsolationKey& network_isolation_key,
  225. SSLConfig* ssl_config) {
  226. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  227. MaybeForceHTTP11Internal(NormalizeSchemeHostPort(server),
  228. network_isolation_key, ssl_config);
  229. }
  230. AlternativeServiceInfoVector HttpServerProperties::GetAlternativeServiceInfos(
  231. const url::SchemeHostPort& origin,
  232. const net::NetworkIsolationKey& network_isolation_key) {
  233. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  234. return GetAlternativeServiceInfosInternal(NormalizeSchemeHostPort(origin),
  235. network_isolation_key);
  236. }
  237. void HttpServerProperties::SetHttp2AlternativeService(
  238. const url::SchemeHostPort& origin,
  239. const NetworkIsolationKey& network_isolation_key,
  240. const AlternativeService& alternative_service,
  241. base::Time expiration) {
  242. DCHECK_EQ(alternative_service.protocol, kProtoHTTP2);
  243. SetAlternativeServices(
  244. origin, network_isolation_key,
  245. AlternativeServiceInfoVector(
  246. /*size=*/1, AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  247. alternative_service, expiration)));
  248. }
  249. void HttpServerProperties::SetQuicAlternativeService(
  250. const url::SchemeHostPort& origin,
  251. const NetworkIsolationKey& network_isolation_key,
  252. const AlternativeService& alternative_service,
  253. base::Time expiration,
  254. const quic::ParsedQuicVersionVector& advertised_versions) {
  255. DCHECK(alternative_service.protocol == kProtoQUIC);
  256. SetAlternativeServices(
  257. origin, network_isolation_key,
  258. AlternativeServiceInfoVector(
  259. /*size=*/1,
  260. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  261. alternative_service, expiration, advertised_versions)));
  262. }
  263. void HttpServerProperties::SetAlternativeServices(
  264. const url::SchemeHostPort& origin,
  265. const net::NetworkIsolationKey& network_isolation_key,
  266. const AlternativeServiceInfoVector& alternative_service_info_vector) {
  267. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  268. SetAlternativeServicesInternal(NormalizeSchemeHostPort(origin),
  269. network_isolation_key,
  270. alternative_service_info_vector);
  271. }
  272. void HttpServerProperties::MarkAlternativeServiceBroken(
  273. const AlternativeService& alternative_service,
  274. const net::NetworkIsolationKey& network_isolation_key) {
  275. broken_alternative_services_.MarkBroken(BrokenAlternativeService(
  276. alternative_service, network_isolation_key, use_network_isolation_key_));
  277. MaybeQueueWriteProperties();
  278. }
  279. void HttpServerProperties::
  280. MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
  281. const AlternativeService& alternative_service,
  282. const net::NetworkIsolationKey& network_isolation_key) {
  283. broken_alternative_services_.MarkBrokenUntilDefaultNetworkChanges(
  284. BrokenAlternativeService(alternative_service, network_isolation_key,
  285. use_network_isolation_key_));
  286. MaybeQueueWriteProperties();
  287. }
  288. void HttpServerProperties::MarkAlternativeServiceRecentlyBroken(
  289. const AlternativeService& alternative_service,
  290. const net::NetworkIsolationKey& network_isolation_key) {
  291. broken_alternative_services_.MarkRecentlyBroken(BrokenAlternativeService(
  292. alternative_service, network_isolation_key, use_network_isolation_key_));
  293. MaybeQueueWriteProperties();
  294. }
  295. bool HttpServerProperties::IsAlternativeServiceBroken(
  296. const AlternativeService& alternative_service,
  297. const net::NetworkIsolationKey& network_isolation_key) const {
  298. return broken_alternative_services_.IsBroken(BrokenAlternativeService(
  299. alternative_service, network_isolation_key, use_network_isolation_key_));
  300. }
  301. bool HttpServerProperties::WasAlternativeServiceRecentlyBroken(
  302. const AlternativeService& alternative_service,
  303. const net::NetworkIsolationKey& network_isolation_key) {
  304. return broken_alternative_services_.WasRecentlyBroken(
  305. BrokenAlternativeService(alternative_service, network_isolation_key,
  306. use_network_isolation_key_));
  307. }
  308. void HttpServerProperties::ConfirmAlternativeService(
  309. const AlternativeService& alternative_service,
  310. const net::NetworkIsolationKey& network_isolation_key) {
  311. bool old_value =
  312. IsAlternativeServiceBroken(alternative_service, network_isolation_key);
  313. broken_alternative_services_.Confirm(BrokenAlternativeService(
  314. alternative_service, network_isolation_key, use_network_isolation_key_));
  315. bool new_value =
  316. IsAlternativeServiceBroken(alternative_service, network_isolation_key);
  317. // For persisting, we only care about the value returned by
  318. // IsAlternativeServiceBroken. If that value changes, then call persist.
  319. if (old_value != new_value)
  320. MaybeQueueWriteProperties();
  321. }
  322. void HttpServerProperties::OnDefaultNetworkChanged() {
  323. bool changed = broken_alternative_services_.OnDefaultNetworkChanged();
  324. if (changed)
  325. MaybeQueueWriteProperties();
  326. }
  327. base::Value HttpServerProperties::GetAlternativeServiceInfoAsValue() const {
  328. const base::Time now = clock_->Now();
  329. const base::TimeTicks now_ticks = tick_clock_->NowTicks();
  330. base::Value::List dict_list;
  331. for (const auto& server_info : server_info_map_) {
  332. if (!server_info.second.alternative_services.has_value())
  333. continue;
  334. base::Value::List alternative_service_list;
  335. const ServerInfoMapKey& key = server_info.first;
  336. for (const AlternativeServiceInfo& alternative_service_info :
  337. server_info.second.alternative_services.value()) {
  338. std::string alternative_service_string(
  339. alternative_service_info.ToString());
  340. AlternativeService alternative_service(
  341. alternative_service_info.alternative_service());
  342. if (alternative_service.host.empty()) {
  343. alternative_service.host = key.server.host();
  344. }
  345. base::TimeTicks brokenness_expiration_ticks;
  346. if (broken_alternative_services_.IsBroken(
  347. BrokenAlternativeService(alternative_service,
  348. server_info.first.network_isolation_key,
  349. use_network_isolation_key_),
  350. &brokenness_expiration_ticks)) {
  351. // Convert |brokenness_expiration| from TimeTicks to Time
  352. base::Time brokenness_expiration =
  353. now + (brokenness_expiration_ticks - now_ticks);
  354. base::Time::Exploded exploded;
  355. brokenness_expiration.LocalExplode(&exploded);
  356. std::string broken_info_string =
  357. " (broken until " +
  358. base::StringPrintf("%04d-%02d-%02d %0d:%0d:%0d", exploded.year,
  359. exploded.month, exploded.day_of_month,
  360. exploded.hour, exploded.minute,
  361. exploded.second) +
  362. ")";
  363. alternative_service_string.append(broken_info_string);
  364. }
  365. alternative_service_list.Append(std::move(alternative_service_string));
  366. }
  367. if (alternative_service_list.empty())
  368. continue;
  369. base::Value::Dict dict;
  370. dict.Set("server", key.server.Serialize());
  371. dict.Set("network_isolation_key",
  372. key.network_isolation_key.ToDebugString());
  373. dict.Set("alternative_service", std::move(alternative_service_list));
  374. dict_list.Append(std::move(dict));
  375. }
  376. return base::Value(std::move(dict_list));
  377. }
  378. bool HttpServerProperties::WasLastLocalAddressWhenQuicWorked(
  379. const IPAddress& local_address) const {
  380. return !last_local_address_when_quic_worked_.empty() &&
  381. last_local_address_when_quic_worked_ == local_address;
  382. }
  383. bool HttpServerProperties::HasLastLocalAddressWhenQuicWorked() const {
  384. return !last_local_address_when_quic_worked_.empty();
  385. }
  386. void HttpServerProperties::SetLastLocalAddressWhenQuicWorked(
  387. IPAddress last_local_address_when_quic_worked) {
  388. DCHECK(!last_local_address_when_quic_worked.empty());
  389. if (last_local_address_when_quic_worked_ ==
  390. last_local_address_when_quic_worked) {
  391. return;
  392. }
  393. last_local_address_when_quic_worked_ = last_local_address_when_quic_worked;
  394. MaybeQueueWriteProperties();
  395. }
  396. void HttpServerProperties::ClearLastLocalAddressWhenQuicWorked() {
  397. if (last_local_address_when_quic_worked_.empty())
  398. return;
  399. last_local_address_when_quic_worked_ = IPAddress();
  400. MaybeQueueWriteProperties();
  401. }
  402. void HttpServerProperties::SetServerNetworkStats(
  403. const url::SchemeHostPort& server,
  404. const NetworkIsolationKey& network_isolation_key,
  405. ServerNetworkStats stats) {
  406. SetServerNetworkStatsInternal(NormalizeSchemeHostPort(server),
  407. network_isolation_key, std::move(stats));
  408. }
  409. void HttpServerProperties::ClearServerNetworkStats(
  410. const url::SchemeHostPort& server,
  411. const NetworkIsolationKey& network_isolation_key) {
  412. ClearServerNetworkStatsInternal(NormalizeSchemeHostPort(server),
  413. network_isolation_key);
  414. }
  415. const ServerNetworkStats* HttpServerProperties::GetServerNetworkStats(
  416. const url::SchemeHostPort& server,
  417. const NetworkIsolationKey& network_isolation_key) {
  418. return GetServerNetworkStatsInternal(NormalizeSchemeHostPort(server),
  419. network_isolation_key);
  420. }
  421. void HttpServerProperties::SetQuicServerInfo(
  422. const quic::QuicServerId& server_id,
  423. const NetworkIsolationKey& network_isolation_key,
  424. const std::string& server_info) {
  425. QuicServerInfoMapKey key =
  426. CreateQuicServerInfoKey(server_id, network_isolation_key);
  427. auto it = quic_server_info_map_.Peek(key);
  428. bool changed =
  429. (it == quic_server_info_map_.end() || it->second != server_info);
  430. quic_server_info_map_.Put(key, server_info);
  431. UpdateCanonicalServerInfoMap(key);
  432. if (changed)
  433. MaybeQueueWriteProperties();
  434. }
  435. const std::string* HttpServerProperties::GetQuicServerInfo(
  436. const quic::QuicServerId& server_id,
  437. const NetworkIsolationKey& network_isolation_key) {
  438. QuicServerInfoMapKey key =
  439. CreateQuicServerInfoKey(server_id, network_isolation_key);
  440. auto it = quic_server_info_map_.Get(key);
  441. if (it != quic_server_info_map_.end()) {
  442. // Since |canonical_server_info_map_| should always map to the most
  443. // recent host, update it with the one that became MRU in
  444. // |quic_server_info_map_|.
  445. UpdateCanonicalServerInfoMap(key);
  446. return &it->second;
  447. }
  448. // If the exact match for |server_id| wasn't found, check
  449. // |canonical_server_info_map_| whether there is server info for a host with
  450. // the same canonical host suffix.
  451. auto canonical_itr = GetCanonicalServerInfoHost(key);
  452. if (canonical_itr == canonical_server_info_map_.end())
  453. return nullptr;
  454. // When search in |quic_server_info_map_|, do not change the MRU order.
  455. it = quic_server_info_map_.Peek(
  456. CreateQuicServerInfoKey(canonical_itr->second, network_isolation_key));
  457. if (it != quic_server_info_map_.end())
  458. return &it->second;
  459. return nullptr;
  460. }
  461. const HttpServerProperties::QuicServerInfoMap&
  462. HttpServerProperties::quic_server_info_map() const {
  463. return quic_server_info_map_;
  464. }
  465. size_t HttpServerProperties::max_server_configs_stored_in_properties() const {
  466. return max_server_configs_stored_in_properties_;
  467. }
  468. void HttpServerProperties::SetMaxServerConfigsStoredInProperties(
  469. size_t max_server_configs_stored_in_properties) {
  470. // Do nothing if the new size is the same as the old one.
  471. if (max_server_configs_stored_in_properties_ ==
  472. max_server_configs_stored_in_properties) {
  473. return;
  474. }
  475. max_server_configs_stored_in_properties_ =
  476. max_server_configs_stored_in_properties;
  477. // LRUCache doesn't allow the capacity of the cache to be changed. Thus create
  478. // a new map with the new size and add current elements and swap the new map.
  479. quic_server_info_map_.ShrinkToSize(max_server_configs_stored_in_properties_);
  480. QuicServerInfoMap temp_map(max_server_configs_stored_in_properties_);
  481. // Update the |canonical_server_info_map_| as well, so it stays in sync with
  482. // |quic_server_info_map_|.
  483. canonical_server_info_map_ = QuicCanonicalMap();
  484. for (const auto& [key, server_info] : base::Reversed(quic_server_info_map_)) {
  485. temp_map.Put(key, server_info);
  486. UpdateCanonicalServerInfoMap(key);
  487. }
  488. quic_server_info_map_.Swap(temp_map);
  489. if (properties_manager_) {
  490. properties_manager_->set_max_server_configs_stored_in_properties(
  491. max_server_configs_stored_in_properties);
  492. }
  493. }
  494. void HttpServerProperties::SetBrokenAlternativeServicesDelayParams(
  495. absl::optional<base::TimeDelta> initial_delay,
  496. absl::optional<bool> exponential_backoff_on_initial_delay) {
  497. broken_alternative_services_.SetDelayParams(
  498. initial_delay, exponential_backoff_on_initial_delay);
  499. }
  500. bool HttpServerProperties::IsInitialized() const {
  501. return is_initialized_;
  502. }
  503. void HttpServerProperties::OnExpireBrokenAlternativeService(
  504. const AlternativeService& expired_alternative_service,
  505. const NetworkIsolationKey& network_isolation_key) {
  506. // Remove every occurrence of |expired_alternative_service| from
  507. // |alternative_service_map_|.
  508. for (auto map_it = server_info_map_.begin();
  509. map_it != server_info_map_.end();) {
  510. if (!map_it->second.alternative_services.has_value() ||
  511. map_it->first.network_isolation_key != network_isolation_key) {
  512. ++map_it;
  513. continue;
  514. }
  515. AlternativeServiceInfoVector* service_info =
  516. &map_it->second.alternative_services.value();
  517. for (auto it = service_info->begin(); it != service_info->end();) {
  518. AlternativeService alternative_service(it->alternative_service());
  519. // Empty hostname in map means hostname of key: substitute before
  520. // comparing to |expired_alternative_service|.
  521. if (alternative_service.host.empty()) {
  522. alternative_service.host = map_it->first.server.host();
  523. }
  524. if (alternative_service == expired_alternative_service) {
  525. it = service_info->erase(it);
  526. continue;
  527. }
  528. ++it;
  529. }
  530. // If an origin has an empty list of alternative services, then remove it
  531. // from both |canonical_alt_svc_map_| and
  532. // |alternative_service_map_|.
  533. if (service_info->empty()) {
  534. RemoveAltSvcCanonicalHost(map_it->first.server, network_isolation_key);
  535. map_it->second.alternative_services.reset();
  536. map_it = server_info_map_.EraseIfEmpty(map_it);
  537. continue;
  538. }
  539. ++map_it;
  540. }
  541. }
  542. base::TimeDelta HttpServerProperties::GetUpdatePrefsDelayForTesting() {
  543. return kUpdatePrefsDelay;
  544. }
  545. bool HttpServerProperties::GetSupportsSpdyInternal(
  546. url::SchemeHostPort server,
  547. const net::NetworkIsolationKey& network_isolation_key) {
  548. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  549. DCHECK_NE(server.scheme(), url::kWsScheme);
  550. DCHECK_NE(server.scheme(), url::kWssScheme);
  551. if (server.host().empty())
  552. return false;
  553. auto server_info = server_info_map_.Get(
  554. CreateServerInfoKey(std::move(server), network_isolation_key));
  555. return server_info != server_info_map_.end() &&
  556. server_info->second.supports_spdy.value_or(false);
  557. }
  558. void HttpServerProperties::SetSupportsSpdyInternal(
  559. url::SchemeHostPort server,
  560. const net::NetworkIsolationKey& network_isolation_key,
  561. bool supports_spdy) {
  562. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  563. DCHECK_NE(server.scheme(), url::kWsScheme);
  564. DCHECK_NE(server.scheme(), url::kWssScheme);
  565. if (server.host().empty())
  566. return;
  567. auto server_info = server_info_map_.GetOrPut(
  568. CreateServerInfoKey(std::move(server), network_isolation_key));
  569. // If value is already the same as |supports_spdy|, or value is unset and
  570. // |supports_spdy| is false, don't queue a write.
  571. bool queue_write =
  572. server_info->second.supports_spdy.value_or(false) != supports_spdy;
  573. server_info->second.supports_spdy = supports_spdy;
  574. if (queue_write)
  575. MaybeQueueWriteProperties();
  576. }
  577. bool HttpServerProperties::RequiresHTTP11Internal(
  578. url::SchemeHostPort server,
  579. const net::NetworkIsolationKey& network_isolation_key) {
  580. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  581. DCHECK_NE(server.scheme(), url::kWsScheme);
  582. DCHECK_NE(server.scheme(), url::kWssScheme);
  583. if (server.host().empty())
  584. return false;
  585. auto spdy_info = server_info_map_.Get(
  586. CreateServerInfoKey(std::move(server), network_isolation_key));
  587. return spdy_info != server_info_map_.end() &&
  588. spdy_info->second.requires_http11.value_or(false);
  589. }
  590. void HttpServerProperties::SetHTTP11RequiredInternal(
  591. url::SchemeHostPort server,
  592. const net::NetworkIsolationKey& network_isolation_key) {
  593. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  594. DCHECK_NE(server.scheme(), url::kWsScheme);
  595. DCHECK_NE(server.scheme(), url::kWssScheme);
  596. if (server.host().empty())
  597. return;
  598. server_info_map_
  599. .GetOrPut(CreateServerInfoKey(std::move(server), network_isolation_key))
  600. ->second.requires_http11 = true;
  601. // No need to call MaybeQueueWriteProperties(), as this information is not
  602. // persisted to preferences.
  603. }
  604. void HttpServerProperties::MaybeForceHTTP11Internal(
  605. url::SchemeHostPort server,
  606. const net::NetworkIsolationKey& network_isolation_key,
  607. SSLConfig* ssl_config) {
  608. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  609. DCHECK_NE(server.scheme(), url::kWsScheme);
  610. DCHECK_NE(server.scheme(), url::kWssScheme);
  611. if (RequiresHTTP11(std::move(server), network_isolation_key)) {
  612. ssl_config->alpn_protos.clear();
  613. ssl_config->alpn_protos.push_back(kProtoHTTP11);
  614. }
  615. }
  616. AlternativeServiceInfoVector
  617. HttpServerProperties::GetAlternativeServiceInfosInternal(
  618. const url::SchemeHostPort& origin,
  619. const net::NetworkIsolationKey& network_isolation_key) {
  620. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  621. DCHECK_NE(origin.scheme(), url::kWsScheme);
  622. DCHECK_NE(origin.scheme(), url::kWssScheme);
  623. // Copy valid alternative service infos into
  624. // |valid_alternative_service_infos|.
  625. AlternativeServiceInfoVector valid_alternative_service_infos;
  626. const base::Time now = clock_->Now();
  627. auto map_it =
  628. server_info_map_.Get(CreateServerInfoKey(origin, network_isolation_key));
  629. if (map_it != server_info_map_.end() &&
  630. map_it->second.alternative_services.has_value()) {
  631. AlternativeServiceInfoVector* service_info =
  632. &map_it->second.alternative_services.value();
  633. HostPortPair host_port_pair(origin.host(), origin.port());
  634. for (auto it = service_info->begin(); it != service_info->end();) {
  635. if (it->expiration() < now) {
  636. it = service_info->erase(it);
  637. continue;
  638. }
  639. AlternativeService alternative_service(it->alternative_service());
  640. if (alternative_service.host.empty()) {
  641. alternative_service.host = origin.host();
  642. }
  643. // If the alternative service is equivalent to the origin (same host, same
  644. // port, and both TCP), skip it.
  645. if (host_port_pair.Equals(alternative_service.host_port_pair()) &&
  646. alternative_service.protocol == kProtoHTTP2) {
  647. ++it;
  648. continue;
  649. }
  650. if (alternative_service.protocol == kProtoQUIC) {
  651. valid_alternative_service_infos.push_back(
  652. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  653. alternative_service, it->expiration(),
  654. it->advertised_versions()));
  655. } else {
  656. valid_alternative_service_infos.push_back(
  657. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  658. alternative_service, it->expiration()));
  659. }
  660. ++it;
  661. }
  662. if (service_info->empty()) {
  663. map_it->second.alternative_services.reset();
  664. server_info_map_.EraseIfEmpty(map_it);
  665. }
  666. return valid_alternative_service_infos;
  667. }
  668. auto canonical = GetCanonicalAltSvcHost(origin, network_isolation_key);
  669. if (canonical == canonical_alt_svc_map_.end()) {
  670. return AlternativeServiceInfoVector();
  671. }
  672. map_it = server_info_map_.Get(
  673. CreateServerInfoKey(canonical->second, network_isolation_key));
  674. if (map_it == server_info_map_.end() ||
  675. !map_it->second.alternative_services.has_value()) {
  676. return AlternativeServiceInfoVector();
  677. }
  678. AlternativeServiceInfoVector* service_info =
  679. &map_it->second.alternative_services.value();
  680. for (auto it = service_info->begin(); it != service_info->end();) {
  681. if (it->expiration() < now) {
  682. it = service_info->erase(it);
  683. continue;
  684. }
  685. AlternativeService alternative_service(it->alternative_service());
  686. if (alternative_service.host.empty()) {
  687. alternative_service.host = canonical->second.host();
  688. if (IsAlternativeServiceBroken(alternative_service,
  689. network_isolation_key)) {
  690. ++it;
  691. continue;
  692. }
  693. alternative_service.host = origin.host();
  694. } else if (IsAlternativeServiceBroken(alternative_service,
  695. network_isolation_key)) {
  696. ++it;
  697. continue;
  698. }
  699. if (alternative_service.protocol == kProtoQUIC) {
  700. valid_alternative_service_infos.push_back(
  701. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  702. alternative_service, it->expiration(),
  703. it->advertised_versions()));
  704. } else {
  705. valid_alternative_service_infos.push_back(
  706. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  707. alternative_service, it->expiration()));
  708. }
  709. ++it;
  710. }
  711. if (service_info->empty())
  712. server_info_map_.EraseIfEmpty(map_it);
  713. return valid_alternative_service_infos;
  714. }
  715. void HttpServerProperties::SetAlternativeServicesInternal(
  716. const url::SchemeHostPort& origin,
  717. const net::NetworkIsolationKey& network_isolation_key,
  718. const AlternativeServiceInfoVector& alternative_service_info_vector) {
  719. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  720. DCHECK_NE(origin.scheme(), url::kWsScheme);
  721. DCHECK_NE(origin.scheme(), url::kWssScheme);
  722. if (alternative_service_info_vector.empty()) {
  723. RemoveAltSvcCanonicalHost(origin, network_isolation_key);
  724. // Don't bother moving to front when erasing information.
  725. auto it = server_info_map_.Peek(
  726. CreateServerInfoKey(origin, network_isolation_key));
  727. if (it == server_info_map_.end() ||
  728. !it->second.alternative_services.has_value()) {
  729. return;
  730. }
  731. it->second.alternative_services.reset();
  732. server_info_map_.EraseIfEmpty(it);
  733. MaybeQueueWriteProperties();
  734. return;
  735. }
  736. auto it = server_info_map_.GetOrPut(
  737. CreateServerInfoKey(origin, network_isolation_key));
  738. bool need_update_pref = true;
  739. if (it->second.alternative_services.has_value()) {
  740. DCHECK(!it->second.empty());
  741. if (it->second.alternative_services->size() ==
  742. alternative_service_info_vector.size()) {
  743. const base::Time now = clock_->Now();
  744. need_update_pref = false;
  745. auto new_it = alternative_service_info_vector.begin();
  746. for (const auto& old : *it->second.alternative_services) {
  747. // Persist to disk immediately if new entry has different scheme, host,
  748. // or port.
  749. if (old.alternative_service() != new_it->alternative_service()) {
  750. need_update_pref = true;
  751. break;
  752. }
  753. // Also persist to disk if new expiration it more that twice as far or
  754. // less than half as far in the future.
  755. base::Time old_time = old.expiration();
  756. base::Time new_time = new_it->expiration();
  757. if (new_time - now > 2 * (old_time - now) ||
  758. 2 * (new_time - now) < (old_time - now)) {
  759. need_update_pref = true;
  760. break;
  761. }
  762. // Also persist to disk if new entry has a different list of advertised
  763. // versions.
  764. if (old.advertised_versions() != new_it->advertised_versions()) {
  765. need_update_pref = true;
  766. break;
  767. }
  768. ++new_it;
  769. }
  770. }
  771. }
  772. const bool previously_no_alternative_services =
  773. (GetIteratorWithAlternativeServiceInfo(origin, network_isolation_key) ==
  774. server_info_map_.end());
  775. it->second.alternative_services = alternative_service_info_vector;
  776. if (previously_no_alternative_services &&
  777. !GetAlternativeServiceInfos(origin, network_isolation_key).empty()) {
  778. // TODO(rch): Consider the case where multiple requests are started
  779. // before the first completes. In this case, only one of the jobs
  780. // would reach this code, whereas all of them should should have.
  781. HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING,
  782. IsGoogleHost(origin.host()));
  783. }
  784. // If this host ends with a canonical suffix, then set it as the
  785. // canonical host.
  786. const char* kCanonicalScheme = "https";
  787. if (origin.scheme() == kCanonicalScheme) {
  788. const std::string* canonical_suffix = GetCanonicalSuffix(origin.host());
  789. if (canonical_suffix != nullptr) {
  790. url::SchemeHostPort canonical_server(kCanonicalScheme, *canonical_suffix,
  791. origin.port());
  792. canonical_alt_svc_map_[CreateServerInfoKey(
  793. canonical_server, network_isolation_key)] = origin;
  794. }
  795. }
  796. if (need_update_pref)
  797. MaybeQueueWriteProperties();
  798. }
  799. void HttpServerProperties::SetServerNetworkStatsInternal(
  800. url::SchemeHostPort server,
  801. const NetworkIsolationKey& network_isolation_key,
  802. ServerNetworkStats stats) {
  803. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  804. DCHECK_NE(server.scheme(), url::kWsScheme);
  805. DCHECK_NE(server.scheme(), url::kWssScheme);
  806. auto server_info = server_info_map_.GetOrPut(
  807. CreateServerInfoKey(std::move(server), network_isolation_key));
  808. bool changed = !server_info->second.server_network_stats.has_value() ||
  809. server_info->second.server_network_stats.value() != stats;
  810. if (changed) {
  811. server_info->second.server_network_stats = stats;
  812. MaybeQueueWriteProperties();
  813. }
  814. }
  815. void HttpServerProperties::ClearServerNetworkStatsInternal(
  816. url::SchemeHostPort server,
  817. const NetworkIsolationKey& network_isolation_key) {
  818. auto server_info = server_info_map_.Peek(
  819. CreateServerInfoKey(std::move(server), network_isolation_key));
  820. // If stats are empty, nothing to do.
  821. if (server_info == server_info_map_.end() ||
  822. !server_info->second.server_network_stats.has_value()) {
  823. return;
  824. }
  825. // Otherwise, clear and delete if needed. No need to bring to front of MRU
  826. // cache when clearing data.
  827. server_info->second.server_network_stats.reset();
  828. if (server_info->second.empty())
  829. server_info_map_.EraseIfEmpty(server_info);
  830. MaybeQueueWriteProperties();
  831. }
  832. const ServerNetworkStats* HttpServerProperties::GetServerNetworkStatsInternal(
  833. url::SchemeHostPort server,
  834. const NetworkIsolationKey& network_isolation_key) {
  835. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  836. DCHECK_NE(server.scheme(), url::kWsScheme);
  837. DCHECK_NE(server.scheme(), url::kWssScheme);
  838. auto server_info = server_info_map_.Get(
  839. CreateServerInfoKey(std::move(server), network_isolation_key));
  840. if (server_info == server_info_map_.end() ||
  841. !server_info->second.server_network_stats.has_value()) {
  842. return nullptr;
  843. }
  844. return &server_info->second.server_network_stats.value();
  845. }
  846. HttpServerProperties::QuicServerInfoMapKey
  847. HttpServerProperties::CreateQuicServerInfoKey(
  848. const quic::QuicServerId& server_id,
  849. const NetworkIsolationKey& network_isolation_key) const {
  850. return QuicServerInfoMapKey(server_id, network_isolation_key,
  851. use_network_isolation_key_);
  852. }
  853. HttpServerProperties::ServerInfoMapKey
  854. HttpServerProperties::CreateServerInfoKey(
  855. const url::SchemeHostPort& server,
  856. const NetworkIsolationKey& network_isolation_key) const {
  857. return ServerInfoMapKey(server, network_isolation_key,
  858. use_network_isolation_key_);
  859. }
  860. HttpServerProperties::ServerInfoMap::const_iterator
  861. HttpServerProperties::GetIteratorWithAlternativeServiceInfo(
  862. const url::SchemeHostPort& server,
  863. const net::NetworkIsolationKey& network_isolation_key) {
  864. ServerInfoMap::const_iterator it =
  865. server_info_map_.Get(CreateServerInfoKey(server, network_isolation_key));
  866. if (it != server_info_map_.end() && it->second.alternative_services)
  867. return it;
  868. auto canonical = GetCanonicalAltSvcHost(server, network_isolation_key);
  869. if (canonical == canonical_alt_svc_map_.end()) {
  870. return server_info_map_.end();
  871. }
  872. const url::SchemeHostPort canonical_server = canonical->second;
  873. it = server_info_map_.Get(
  874. CreateServerInfoKey(canonical_server, network_isolation_key));
  875. if (it == server_info_map_.end() || !it->second.alternative_services)
  876. return server_info_map_.end();
  877. for (const AlternativeServiceInfo& alternative_service_info :
  878. it->second.alternative_services.value()) {
  879. AlternativeService alternative_service(
  880. alternative_service_info.alternative_service());
  881. if (alternative_service.host.empty()) {
  882. alternative_service.host = canonical_server.host();
  883. }
  884. if (!IsAlternativeServiceBroken(alternative_service,
  885. network_isolation_key)) {
  886. return it;
  887. }
  888. }
  889. RemoveAltSvcCanonicalHost(canonical_server, network_isolation_key);
  890. return server_info_map_.end();
  891. }
  892. HttpServerProperties::CanonicalMap::const_iterator
  893. HttpServerProperties::GetCanonicalAltSvcHost(
  894. const url::SchemeHostPort& server,
  895. const net::NetworkIsolationKey& network_isolation_key) const {
  896. const char* kCanonicalScheme = "https";
  897. if (server.scheme() != kCanonicalScheme)
  898. return canonical_alt_svc_map_.end();
  899. const std::string* canonical_suffix = GetCanonicalSuffix(server.host());
  900. if (canonical_suffix == nullptr)
  901. return canonical_alt_svc_map_.end();
  902. url::SchemeHostPort canonical_server(kCanonicalScheme, *canonical_suffix,
  903. server.port());
  904. return canonical_alt_svc_map_.find(
  905. CreateServerInfoKey(canonical_server, network_isolation_key));
  906. }
  907. HttpServerProperties::QuicCanonicalMap::const_iterator
  908. HttpServerProperties::GetCanonicalServerInfoHost(
  909. const QuicServerInfoMapKey& key) const {
  910. const std::string* canonical_suffix =
  911. GetCanonicalSuffix(key.server_id.host());
  912. if (canonical_suffix == nullptr)
  913. return canonical_server_info_map_.end();
  914. quic::QuicServerId canonical_server_id(*canonical_suffix,
  915. key.server_id.privacy_mode_enabled(),
  916. key.server_id.port());
  917. return canonical_server_info_map_.find(
  918. CreateQuicServerInfoKey(canonical_server_id, key.network_isolation_key));
  919. }
  920. void HttpServerProperties::RemoveAltSvcCanonicalHost(
  921. const url::SchemeHostPort& server,
  922. const NetworkIsolationKey& network_isolation_key) {
  923. auto canonical = GetCanonicalAltSvcHost(server, network_isolation_key);
  924. if (canonical == canonical_alt_svc_map_.end())
  925. return;
  926. canonical_alt_svc_map_.erase(canonical->first);
  927. }
  928. void HttpServerProperties::UpdateCanonicalServerInfoMap(
  929. const QuicServerInfoMapKey& key) {
  930. const std::string* suffix = GetCanonicalSuffix(key.server_id.host());
  931. if (!suffix)
  932. return;
  933. quic::QuicServerId canonical_server(
  934. *suffix, key.server_id.privacy_mode_enabled(), key.server_id.port());
  935. canonical_server_info_map_[CreateQuicServerInfoKey(
  936. canonical_server, key.network_isolation_key)] = key.server_id;
  937. }
  938. const std::string* HttpServerProperties::GetCanonicalSuffix(
  939. const std::string& host) const {
  940. // If this host ends with a canonical suffix, then return the canonical
  941. // suffix.
  942. for (const std::string& canonical_suffix : canonical_suffixes_) {
  943. if (base::EndsWith(host, canonical_suffix,
  944. base::CompareCase::INSENSITIVE_ASCII)) {
  945. return &canonical_suffix;
  946. }
  947. }
  948. return nullptr;
  949. }
  950. void HttpServerProperties::OnPrefsLoaded(
  951. std::unique_ptr<ServerInfoMap> server_info_map,
  952. const IPAddress& last_local_address_when_quic_worked,
  953. std::unique_ptr<QuicServerInfoMap> quic_server_info_map,
  954. std::unique_ptr<BrokenAlternativeServiceList>
  955. broken_alternative_service_list,
  956. std::unique_ptr<RecentlyBrokenAlternativeServices>
  957. recently_broken_alternative_services) {
  958. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  959. DCHECK(!is_initialized_);
  960. // Either all of these are nullptr, or none of them are (except the broken alt
  961. // service fields).
  962. if (server_info_map) {
  963. OnServerInfoLoaded(std::move(server_info_map));
  964. OnLastLocalAddressWhenQuicWorkedLoaded(last_local_address_when_quic_worked);
  965. OnQuicServerInfoMapLoaded(std::move(quic_server_info_map));
  966. if (recently_broken_alternative_services) {
  967. DCHECK(broken_alternative_service_list);
  968. OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
  969. std::move(broken_alternative_service_list),
  970. std::move(recently_broken_alternative_services));
  971. }
  972. }
  973. is_initialized_ = true;
  974. if (queue_write_on_load_) {
  975. // Leaving this as true doesn't actually have any effect, but seems best to
  976. // be safe.
  977. queue_write_on_load_ = false;
  978. MaybeQueueWriteProperties();
  979. }
  980. }
  981. void HttpServerProperties::OnServerInfoLoaded(
  982. std::unique_ptr<ServerInfoMap> server_info_map) {
  983. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  984. // Perform a simple sanity check on loaded data, when DCHECKs are enabled.
  985. #if DCHECK_IS_ON()
  986. if (!use_network_isolation_key_) {
  987. for (auto server_info = server_info_map->begin();
  988. server_info != server_info_map->end(); ++server_info) {
  989. DCHECK(server_info->first.network_isolation_key.IsEmpty());
  990. }
  991. }
  992. #endif // DCHECK_IS_ON()
  993. // Swap in the entries from persisted data. This allows the MRU cache to be
  994. // sorted based on the order of the entries in the newer in-memory cache.
  995. server_info_map_.Swap(*server_info_map);
  996. // Add the entries from the memory cache.
  997. for (auto& [key, server_info] : base::Reversed(*server_info_map)) {
  998. // If there's no corresponding old entry, add the new entry directly.
  999. auto old_entry = server_info_map_.Get(key);
  1000. if (old_entry == server_info_map_.end()) {
  1001. server_info_map_.Put(key, std::move(server_info));
  1002. continue;
  1003. }
  1004. // Otherwise, merge the old and new entries. Prefer values from older
  1005. // entries.
  1006. if (!old_entry->second.supports_spdy.has_value())
  1007. old_entry->second.supports_spdy = server_info.supports_spdy;
  1008. if (!old_entry->second.alternative_services.has_value())
  1009. old_entry->second.alternative_services = server_info.alternative_services;
  1010. if (!old_entry->second.server_network_stats.has_value())
  1011. old_entry->second.server_network_stats = server_info.server_network_stats;
  1012. // |requires_http11| isn't saved to prefs, so the loaded entry should not
  1013. // have it set. Unconditionally copy it from the new entry.
  1014. DCHECK(!old_entry->second.requires_http11.has_value());
  1015. old_entry->second.requires_http11 = server_info.requires_http11;
  1016. }
  1017. // Attempt to find canonical servers. Canonical suffix only apply to HTTPS.
  1018. const uint16_t kCanonicalPort = 443;
  1019. const char* kCanonicalScheme = "https";
  1020. for (const auto& it : server_info_map_) {
  1021. if (!it.second.alternative_services ||
  1022. it.first.server.scheme() != kCanonicalScheme) {
  1023. continue;
  1024. }
  1025. const std::string* canonical_suffix =
  1026. GetCanonicalSuffix(it.first.server.host());
  1027. if (!canonical_suffix)
  1028. continue;
  1029. ServerInfoMapKey key = CreateServerInfoKey(
  1030. url::SchemeHostPort(kCanonicalScheme, *canonical_suffix,
  1031. kCanonicalPort),
  1032. it.first.network_isolation_key);
  1033. // If we already have a valid canonical server, we're done.
  1034. if (base::Contains(canonical_alt_svc_map_, key)) {
  1035. auto key_it = server_info_map_.Peek(key);
  1036. if (key_it != server_info_map_.end() &&
  1037. key_it->second.alternative_services.has_value()) {
  1038. continue;
  1039. }
  1040. }
  1041. canonical_alt_svc_map_[key] = it.first.server;
  1042. }
  1043. }
  1044. void HttpServerProperties::OnLastLocalAddressWhenQuicWorkedLoaded(
  1045. const IPAddress& last_local_address_when_quic_worked) {
  1046. last_local_address_when_quic_worked_ = last_local_address_when_quic_worked;
  1047. }
  1048. void HttpServerProperties::OnQuicServerInfoMapLoaded(
  1049. std::unique_ptr<QuicServerInfoMap> quic_server_info_map) {
  1050. DCHECK_EQ(quic_server_info_map->max_size(), quic_server_info_map_.max_size());
  1051. // Add the entries from persisted data.
  1052. quic_server_info_map_.Swap(*quic_server_info_map);
  1053. // Add the entries from the memory cache.
  1054. for (const auto& [key, server_info] : base::Reversed(*quic_server_info_map)) {
  1055. if (quic_server_info_map_.Get(key) == quic_server_info_map_.end()) {
  1056. quic_server_info_map_.Put(key, server_info);
  1057. }
  1058. }
  1059. // Repopulate |canonical_server_info_map_| to stay in sync with
  1060. // |quic_server_info_map_|.
  1061. canonical_server_info_map_.clear();
  1062. for (const auto& [key, server_info] : base::Reversed(quic_server_info_map_)) {
  1063. UpdateCanonicalServerInfoMap(key);
  1064. }
  1065. }
  1066. void HttpServerProperties::OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
  1067. std::unique_ptr<BrokenAlternativeServiceList>
  1068. broken_alternative_service_list,
  1069. std::unique_ptr<RecentlyBrokenAlternativeServices>
  1070. recently_broken_alternative_services) {
  1071. broken_alternative_services_.SetBrokenAndRecentlyBrokenAlternativeServices(
  1072. std::move(broken_alternative_service_list),
  1073. std::move(recently_broken_alternative_services));
  1074. }
  1075. void HttpServerProperties::MaybeQueueWriteProperties() {
  1076. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  1077. if (prefs_update_timer_.IsRunning() || !properties_manager_)
  1078. return;
  1079. if (!is_initialized_) {
  1080. queue_write_on_load_ = true;
  1081. return;
  1082. }
  1083. prefs_update_timer_.Start(
  1084. FROM_HERE, kUpdatePrefsDelay,
  1085. base::BindOnce(&HttpServerProperties::WriteProperties,
  1086. base::Unretained(this), base::OnceClosure()));
  1087. }
  1088. void HttpServerProperties::WriteProperties(base::OnceClosure callback) const {
  1089. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  1090. DCHECK(properties_manager_);
  1091. // |this| shouldn't be waiting to load properties cached to disk when this
  1092. // method is invoked, since this method will overwrite any cached properties.
  1093. DCHECK(is_initialized_);
  1094. // There shouldn't be a queued update when this is run, since this method
  1095. // removes the need for any update to be queued.
  1096. DCHECK(!prefs_update_timer_.IsRunning());
  1097. properties_manager_->WriteToPrefs(
  1098. server_info_map_,
  1099. base::BindRepeating(&HttpServerProperties::GetCanonicalSuffix,
  1100. base::Unretained(this)),
  1101. last_local_address_when_quic_worked_, quic_server_info_map_,
  1102. broken_alternative_services_.broken_alternative_service_list(),
  1103. broken_alternative_services_.recently_broken_alternative_services(),
  1104. std::move(callback));
  1105. }
  1106. } // namespace net