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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return; | 313 return; |
314 } | 314 } |
315 // This server's alternate protocol information is coming from a canonical | 315 // This server's alternate protocol information is coming from a canonical |
316 // server. Add an entry in the map for this server explicitly so that | 316 // server. Add an entry in the map for this server explicitly so that |
317 // it can be marked as broken. | 317 // it can be marked as broken. |
318 it = alternate_protocol_map_.Put(server, alternate); | 318 it = alternate_protocol_map_.Put(server, alternate); |
319 } | 319 } |
320 it->second.is_broken = true; | 320 it->second.is_broken = true; |
321 const AlternativeService alternative_service(alternate.protocol, | 321 const AlternativeService alternative_service(alternate.protocol, |
322 server.host(), alternate.port); | 322 server.host(), alternate.port); |
323 int count = ++broken_alternate_protocol_map_[alternative_service]; | 323 int count = ++recently_broken_alternative_services_[alternative_service]; |
324 base::TimeDelta delay = | 324 base::TimeDelta delay = |
325 base::TimeDelta::FromSeconds(kBrokenAlternateProtocolDelaySecs); | 325 base::TimeDelta::FromSeconds(kBrokenAlternateProtocolDelaySecs); |
326 base::TimeTicks when = base::TimeTicks::Now() + delay * (1 << (count - 1)); | 326 base::TimeTicks when = base::TimeTicks::Now() + delay * (1 << (count - 1)); |
327 broken_alternate_protocol_list_.push_back( | 327 auto result = broken_alternative_services_.insert( |
328 BrokenAlternateProtocolEntryWithTime(alternative_service, when)); | 328 std::make_pair(alternative_service, when)); |
| 329 // Return if alternative service is already in expiration queue. |
| 330 if (!result.second) { |
| 331 return; |
| 332 } |
329 | 333 |
330 // Do not leave this host as canonical so that we don't infer the other | 334 // Do not leave this host as canonical so that we don't infer the other |
331 // hosts are also broken without testing them first. | 335 // hosts are also broken without testing them first. |
332 RemoveCanonicalHost(server); | 336 RemoveCanonicalHost(server); |
333 | 337 |
334 // If this is the only entry in the list, schedule an expiration task. | 338 // If this is the only entry in the list, schedule an expiration task. |
335 // Otherwise it will be rescheduled automatically when the pending task runs. | 339 // Otherwise it will be rescheduled automatically when the pending task runs. |
336 if (broken_alternate_protocol_list_.size() == 1) { | 340 if (broken_alternative_services_.size() == 1) { |
337 ScheduleBrokenAlternateProtocolMappingsExpiration(); | 341 ScheduleBrokenAlternateProtocolMappingsExpiration(); |
338 } | 342 } |
339 } | 343 } |
340 | 344 |
341 bool HttpServerPropertiesImpl::WasAlternateProtocolRecentlyBroken( | 345 bool HttpServerPropertiesImpl::WasAlternateProtocolRecentlyBroken( |
342 const HostPortPair& server) { | 346 const HostPortPair& server) { |
343 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); | 347 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); |
344 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) | 348 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) |
345 return false; | 349 return false; |
346 const AlternativeService alternative_service( | 350 const AlternativeService alternative_service( |
347 alternate_protocol.protocol, server.host(), alternate_protocol.port); | 351 alternate_protocol.protocol, server.host(), alternate_protocol.port); |
348 return ContainsKey(broken_alternate_protocol_map_, alternative_service); | 352 return ContainsKey(recently_broken_alternative_services_, |
| 353 alternative_service); |
349 } | 354 } |
350 | 355 |
351 void HttpServerPropertiesImpl::ConfirmAlternateProtocol( | 356 void HttpServerPropertiesImpl::ConfirmAlternateProtocol( |
352 const HostPortPair& server) { | 357 const HostPortPair& server) { |
353 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); | 358 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); |
354 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) | 359 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) |
355 return; | 360 return; |
356 const AlternativeService alternative_service( | 361 const AlternativeService alternative_service( |
357 alternate_protocol.protocol, server.host(), alternate_protocol.port); | 362 alternate_protocol.protocol, server.host(), alternate_protocol.port); |
358 broken_alternate_protocol_map_.erase(alternative_service); | 363 recently_broken_alternative_services_.erase(alternative_service); |
359 } | 364 } |
360 | 365 |
361 void HttpServerPropertiesImpl::ClearAlternateProtocol( | 366 void HttpServerPropertiesImpl::ClearAlternateProtocol( |
362 const HostPortPair& server) { | 367 const HostPortPair& server) { |
363 AlternateProtocolMap::iterator it = alternate_protocol_map_.Peek(server); | 368 AlternateProtocolMap::iterator it = alternate_protocol_map_.Peek(server); |
364 if (it != alternate_protocol_map_.end()) | 369 if (it != alternate_protocol_map_.end()) |
365 alternate_protocol_map_.Erase(it); | 370 alternate_protocol_map_.Erase(it); |
366 | 371 |
367 RemoveCanonicalHost(server); | 372 RemoveCanonicalHost(server); |
368 } | 373 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 return; | 502 return; |
498 | 503 |
499 if (!canonical->second.Equals(server)) | 504 if (!canonical->second.Equals(server)) |
500 return; | 505 return; |
501 | 506 |
502 canonical_host_to_origin_map_.erase(canonical->first); | 507 canonical_host_to_origin_map_.erase(canonical->first); |
503 } | 508 } |
504 | 509 |
505 void HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings() { | 510 void HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings() { |
506 base::TimeTicks now = base::TimeTicks::Now(); | 511 base::TimeTicks now = base::TimeTicks::Now(); |
507 while (!broken_alternate_protocol_list_.empty()) { | 512 while (!broken_alternative_services_.empty()) { |
508 BrokenAlternateProtocolEntryWithTime entry_with_time = | 513 BrokenAlternativeServices::iterator it = |
509 broken_alternate_protocol_list_.front(); | 514 broken_alternative_services_.begin(); |
510 if (now < entry_with_time.when) { | 515 if (now < it->second) { |
511 break; | 516 break; |
512 } | 517 } |
513 | 518 |
514 const AlternativeService& alternative_service = | 519 const AlternativeService& alternative_service = it->first; |
515 entry_with_time.alternative_service; | |
516 ClearAlternateProtocol( | 520 ClearAlternateProtocol( |
517 HostPortPair(alternative_service.host, alternative_service.port)); | 521 HostPortPair(alternative_service.host, alternative_service.port)); |
518 broken_alternate_protocol_list_.pop_front(); | 522 broken_alternative_services_.erase(it); |
519 } | 523 } |
520 ScheduleBrokenAlternateProtocolMappingsExpiration(); | 524 ScheduleBrokenAlternateProtocolMappingsExpiration(); |
521 } | 525 } |
522 | 526 |
523 void | 527 void |
524 HttpServerPropertiesImpl::ScheduleBrokenAlternateProtocolMappingsExpiration() { | 528 HttpServerPropertiesImpl::ScheduleBrokenAlternateProtocolMappingsExpiration() { |
525 if (broken_alternate_protocol_list_.empty()) { | 529 if (broken_alternative_services_.empty()) { |
526 return; | 530 return; |
527 } | 531 } |
528 base::TimeTicks now = base::TimeTicks::Now(); | 532 base::TimeTicks now = base::TimeTicks::Now(); |
529 base::TimeTicks when = broken_alternate_protocol_list_.front().when; | 533 base::TimeTicks when = broken_alternative_services_.front().second; |
530 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); | 534 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); |
531 base::MessageLoop::current()->PostDelayedTask( | 535 base::MessageLoop::current()->PostDelayedTask( |
532 FROM_HERE, | 536 FROM_HERE, |
533 base::Bind( | 537 base::Bind( |
534 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, | 538 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, |
535 weak_ptr_factory_.GetWeakPtr()), | 539 weak_ptr_factory_.GetWeakPtr()), |
536 delay); | 540 delay); |
537 } | 541 } |
538 | 542 |
539 } // namespace net | 543 } // namespace net |
OLD | NEW |