OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_server_properties_impl.h" | 5 #include "net/http/http_server_properties_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 const uint64_t kBrokenAlternativeProtocolDelaySecs = 300; | 26 const uint64_t kBrokenAlternativeProtocolDelaySecs = 300; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 HttpServerPropertiesImpl::HttpServerPropertiesImpl() | 30 HttpServerPropertiesImpl::HttpServerPropertiesImpl() |
31 : spdy_servers_map_(SpdyServerHostPortMap::NO_AUTO_EVICT), | 31 : spdy_servers_map_(SpdyServerHostPortMap::NO_AUTO_EVICT), |
32 alternative_service_map_(AlternativeServiceMap::NO_AUTO_EVICT), | 32 alternative_service_map_(AlternativeServiceMap::NO_AUTO_EVICT), |
33 spdy_settings_map_(SpdySettingsMap::NO_AUTO_EVICT), | 33 spdy_settings_map_(SpdySettingsMap::NO_AUTO_EVICT), |
34 server_network_stats_map_(ServerNetworkStatsMap::NO_AUTO_EVICT), | 34 server_network_stats_map_(ServerNetworkStatsMap::NO_AUTO_EVICT), |
35 alternative_service_probability_threshold_(1.0), | |
36 quic_server_info_map_(QuicServerInfoMap::NO_AUTO_EVICT), | 35 quic_server_info_map_(QuicServerInfoMap::NO_AUTO_EVICT), |
37 max_server_configs_stored_in_properties_(kMaxQuicServersToPersist), | 36 max_server_configs_stored_in_properties_(kMaxQuicServersToPersist), |
38 weak_ptr_factory_(this) { | 37 weak_ptr_factory_(this) { |
39 canonical_suffixes_.push_back(".c.youtube.com"); | 38 canonical_suffixes_.push_back(".c.youtube.com"); |
40 canonical_suffixes_.push_back(".googlevideo.com"); | 39 canonical_suffixes_.push_back(".googlevideo.com"); |
41 canonical_suffixes_.push_back(".googleusercontent.com"); | 40 canonical_suffixes_.push_back(".googleusercontent.com"); |
42 } | 41 } |
43 | 42 |
44 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { | 43 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { |
45 } | 44 } |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 if (base::EndsWith(host, canonical_suffixes_[i], | 315 if (base::EndsWith(host, canonical_suffixes_[i], |
317 base::CompareCase::INSENSITIVE_ASCII)) { | 316 base::CompareCase::INSENSITIVE_ASCII)) { |
318 return canonical_suffix; | 317 return canonical_suffix; |
319 } | 318 } |
320 } | 319 } |
321 return std::string(); | 320 return std::string(); |
322 } | 321 } |
323 | 322 |
324 AlternativeServiceVector HttpServerPropertiesImpl::GetAlternativeServices( | 323 AlternativeServiceVector HttpServerPropertiesImpl::GetAlternativeServices( |
325 const HostPortPair& origin) { | 324 const HostPortPair& origin) { |
326 // Copy alternative services with probability greater than or equal to the | 325 // Copy valid alternative services into |valid_alternative_services|. |
327 // threshold into |alternative_services_above_threshold|. | 326 AlternativeServiceVector valid_alternative_services; |
328 AlternativeServiceVector alternative_services_above_threshold; | |
329 const base::Time now = base::Time::Now(); | 327 const base::Time now = base::Time::Now(); |
330 AlternativeServiceMap::iterator map_it = alternative_service_map_.Get(origin); | 328 AlternativeServiceMap::iterator map_it = alternative_service_map_.Get(origin); |
331 if (map_it != alternative_service_map_.end()) { | 329 if (map_it != alternative_service_map_.end()) { |
332 for (AlternativeServiceInfoVector::iterator it = map_it->second.begin(); | 330 for (AlternativeServiceInfoVector::iterator it = map_it->second.begin(); |
333 it != map_it->second.end();) { | 331 it != map_it->second.end();) { |
334 if (it->expiration < now) { | 332 if (it->expiration < now) { |
335 it = map_it->second.erase(it); | 333 it = map_it->second.erase(it); |
336 continue; | 334 continue; |
337 } | 335 } |
338 if (it->probability == 0 || | |
339 it->probability < alternative_service_probability_threshold_) { | |
340 ++it; | |
341 continue; | |
342 } | |
343 AlternativeService alternative_service(it->alternative_service); | 336 AlternativeService alternative_service(it->alternative_service); |
344 if (alternative_service.host.empty()) { | 337 if (alternative_service.host.empty()) { |
345 alternative_service.host = origin.host(); | 338 alternative_service.host = origin.host(); |
346 } | 339 } |
347 // If the alternative service is equivalent to the origin (same host, same | 340 // If the alternative service is equivalent to the origin (same host, same |
348 // port, and both TCP), then there is already a Job for it, so do not | 341 // port, and both TCP), then there is already a Job for it, so do not |
349 // return it here. | 342 // return it here. |
350 if (origin.Equals(alternative_service.host_port_pair()) && | 343 if (origin.Equals(alternative_service.host_port_pair()) && |
351 NPN_SPDY_MINIMUM_VERSION <= alternative_service.protocol && | 344 NPN_SPDY_MINIMUM_VERSION <= alternative_service.protocol && |
352 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) { | 345 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) { |
353 ++it; | 346 ++it; |
354 continue; | 347 continue; |
355 } | 348 } |
356 alternative_services_above_threshold.push_back(alternative_service); | 349 valid_alternative_services.push_back(alternative_service); |
357 ++it; | 350 ++it; |
358 } | 351 } |
359 if (map_it->second.empty()) { | 352 if (map_it->second.empty()) { |
360 alternative_service_map_.Erase(map_it); | 353 alternative_service_map_.Erase(map_it); |
361 } | 354 } |
362 return alternative_services_above_threshold; | 355 return valid_alternative_services; |
363 } | 356 } |
364 | 357 |
365 CanonicalHostMap::const_iterator canonical = GetCanonicalHost(origin); | 358 CanonicalHostMap::const_iterator canonical = GetCanonicalHost(origin); |
366 if (canonical == canonical_host_to_origin_map_.end()) { | 359 if (canonical == canonical_host_to_origin_map_.end()) { |
367 return AlternativeServiceVector(); | 360 return AlternativeServiceVector(); |
368 } | 361 } |
369 map_it = alternative_service_map_.Get(canonical->second); | 362 map_it = alternative_service_map_.Get(canonical->second); |
370 if (map_it == alternative_service_map_.end()) { | 363 if (map_it == alternative_service_map_.end()) { |
371 return AlternativeServiceVector(); | 364 return AlternativeServiceVector(); |
372 } | 365 } |
373 for (AlternativeServiceInfoVector::iterator it = map_it->second.begin(); | 366 for (AlternativeServiceInfoVector::iterator it = map_it->second.begin(); |
374 it != map_it->second.end();) { | 367 it != map_it->second.end();) { |
375 if (it->expiration < now) { | 368 if (it->expiration < now) { |
376 it = map_it->second.erase(it); | 369 it = map_it->second.erase(it); |
377 continue; | 370 continue; |
378 } | 371 } |
379 if (it->probability < alternative_service_probability_threshold_) { | |
380 ++it; | |
381 continue; | |
382 } | |
383 AlternativeService alternative_service(it->alternative_service); | 372 AlternativeService alternative_service(it->alternative_service); |
384 if (alternative_service.host.empty()) { | 373 if (alternative_service.host.empty()) { |
385 alternative_service.host = canonical->second.host(); | 374 alternative_service.host = canonical->second.host(); |
386 if (IsAlternativeServiceBroken(alternative_service)) { | 375 if (IsAlternativeServiceBroken(alternative_service)) { |
387 ++it; | 376 ++it; |
388 continue; | 377 continue; |
389 } | 378 } |
390 alternative_service.host = origin.host(); | 379 alternative_service.host = origin.host(); |
391 } else if (IsAlternativeServiceBroken(alternative_service)) { | 380 } else if (IsAlternativeServiceBroken(alternative_service)) { |
392 ++it; | 381 ++it; |
393 continue; | 382 continue; |
394 } | 383 } |
395 alternative_services_above_threshold.push_back(alternative_service); | 384 valid_alternative_services.push_back(alternative_service); |
396 ++it; | 385 ++it; |
397 } | 386 } |
398 if (map_it->second.empty()) { | 387 if (map_it->second.empty()) { |
399 alternative_service_map_.Erase(map_it); | 388 alternative_service_map_.Erase(map_it); |
400 } | 389 } |
401 return alternative_services_above_threshold; | 390 return valid_alternative_services; |
402 } | 391 } |
403 | 392 |
404 bool HttpServerPropertiesImpl::SetAlternativeService( | 393 bool HttpServerPropertiesImpl::SetAlternativeService( |
405 const HostPortPair& origin, | 394 const HostPortPair& origin, |
406 const AlternativeService& alternative_service, | 395 const AlternativeService& alternative_service, |
407 double alternative_probability, | |
408 base::Time expiration) { | 396 base::Time expiration) { |
409 return SetAlternativeServices( | 397 return SetAlternativeServices( |
410 origin, AlternativeServiceInfoVector( | 398 origin, |
411 /*size=*/1, | 399 AlternativeServiceInfoVector( |
412 AlternativeServiceInfo(alternative_service, | 400 /*size=*/1, AlternativeServiceInfo(alternative_service, expiration))); |
413 alternative_probability, expiration))); | |
414 } | 401 } |
415 | 402 |
416 bool HttpServerPropertiesImpl::SetAlternativeServices( | 403 bool HttpServerPropertiesImpl::SetAlternativeServices( |
417 const HostPortPair& origin, | 404 const HostPortPair& origin, |
418 const AlternativeServiceInfoVector& alternative_service_info_vector) { | 405 const AlternativeServiceInfoVector& alternative_service_info_vector) { |
419 AlternativeServiceMap::iterator it = alternative_service_map_.Peek(origin); | 406 AlternativeServiceMap::iterator it = alternative_service_map_.Peek(origin); |
420 | 407 |
421 if (alternative_service_info_vector.empty()) { | 408 if (alternative_service_info_vector.empty()) { |
422 if (it == alternative_service_map_.end()) { | 409 if (it == alternative_service_map_.end()) { |
423 return false; | 410 return false; |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 quic_server_info_map_.ShrinkToSize(max_server_configs_stored_in_properties_); | 677 quic_server_info_map_.ShrinkToSize(max_server_configs_stored_in_properties_); |
691 QuicServerInfoMap temp_map(max_server_configs_stored_in_properties_); | 678 QuicServerInfoMap temp_map(max_server_configs_stored_in_properties_); |
692 for (QuicServerInfoMap::reverse_iterator it = quic_server_info_map_.rbegin(); | 679 for (QuicServerInfoMap::reverse_iterator it = quic_server_info_map_.rbegin(); |
693 it != quic_server_info_map_.rend(); ++it) { | 680 it != quic_server_info_map_.rend(); ++it) { |
694 temp_map.Put(it->first, it->second); | 681 temp_map.Put(it->first, it->second); |
695 } | 682 } |
696 | 683 |
697 quic_server_info_map_.Swap(temp_map); | 684 quic_server_info_map_.Swap(temp_map); |
698 } | 685 } |
699 | 686 |
700 void HttpServerPropertiesImpl::SetAlternativeServiceProbabilityThreshold( | |
701 double threshold) { | |
702 alternative_service_probability_threshold_ = threshold; | |
703 } | |
704 | |
705 AlternativeServiceMap::const_iterator | 687 AlternativeServiceMap::const_iterator |
706 HttpServerPropertiesImpl::GetAlternateProtocolIterator( | 688 HttpServerPropertiesImpl::GetAlternateProtocolIterator( |
707 const HostPortPair& server) { | 689 const HostPortPair& server) { |
708 AlternativeServiceMap::const_iterator it = | 690 AlternativeServiceMap::const_iterator it = |
709 alternative_service_map_.Get(server); | 691 alternative_service_map_.Get(server); |
710 if (it != alternative_service_map_.end()) | 692 if (it != alternative_service_map_.end()) |
711 return it; | 693 return it; |
712 | 694 |
713 CanonicalHostMap::const_iterator canonical = GetCanonicalHost(server); | 695 CanonicalHostMap::const_iterator canonical = GetCanonicalHost(server); |
714 if (canonical == canonical_host_to_origin_map_.end()) { | 696 if (canonical == canonical_host_to_origin_map_.end()) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
817 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); | 799 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); |
818 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 800 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
819 FROM_HERE, | 801 FROM_HERE, |
820 base::Bind( | 802 base::Bind( |
821 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, | 803 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, |
822 weak_ptr_factory_.GetWeakPtr()), | 804 weak_ptr_factory_.GetWeakPtr()), |
823 delay); | 805 delay); |
824 } | 806 } |
825 | 807 |
826 } // namespace net | 808 } // namespace net |
OLD | NEW |