Chromium Code Reviews| 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/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <Winsock2.h> | 8 #include <Winsock2.h> |
| 9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| (...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1451 | 1451 |
| 1452 // Attempts to serve the job from HOSTS. Returns true if succeeded and | 1452 // Attempts to serve the job from HOSTS. Returns true if succeeded and |
| 1453 // this Job was destroyed. | 1453 // this Job was destroyed. |
| 1454 bool ServeFromHosts() { | 1454 bool ServeFromHosts() { |
| 1455 DCHECK_GT(num_active_requests(), 0u); | 1455 DCHECK_GT(num_active_requests(), 0u); |
| 1456 AddressList addr_list; | 1456 AddressList addr_list; |
| 1457 if (resolver_->ServeFromHosts(key(), | 1457 if (resolver_->ServeFromHosts(key(), |
| 1458 requests_.front()->info(), | 1458 requests_.front()->info(), |
| 1459 &addr_list)) { | 1459 &addr_list)) { |
| 1460 // This will destroy the Job. | 1460 // This will destroy the Job. |
| 1461 CompleteRequests( | 1461 CompleteRequests(MakeCacheEntry(OK, addr_list), base::TimeDelta()); |
| 1462 HostCache::Entry(OK, MakeAddressListForRequest(addr_list)), | |
| 1463 base::TimeDelta()); | |
| 1464 return true; | 1462 return true; |
| 1465 } | 1463 } |
| 1466 return false; | 1464 return false; |
| 1467 } | 1465 } |
| 1468 | 1466 |
| 1469 const Key& key() const { return key_; } | 1467 const Key& key() const { return key_; } |
| 1470 | 1468 |
| 1471 bool is_queued() const { | 1469 bool is_queued() const { |
| 1472 return !handle_.is_null(); | 1470 return !handle_.is_null(); |
| 1473 } | 1471 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 1493 if (is_queued()) { | 1491 if (is_queued()) { |
| 1494 resolver_->dispatcher_->Cancel(handle_); | 1492 resolver_->dispatcher_->Cancel(handle_); |
| 1495 handle_.Reset(); | 1493 handle_.Reset(); |
| 1496 } else if (num_occupied_job_slots_ > 1) { | 1494 } else if (num_occupied_job_slots_ > 1) { |
| 1497 resolver_->dispatcher_->OnJobFinished(); | 1495 resolver_->dispatcher_->OnJobFinished(); |
| 1498 --num_occupied_job_slots_; | 1496 --num_occupied_job_slots_; |
| 1499 } | 1497 } |
| 1500 DCHECK_EQ(1u, num_occupied_job_slots_); | 1498 DCHECK_EQ(1u, num_occupied_job_slots_); |
| 1501 } | 1499 } |
| 1502 | 1500 |
| 1501 // MakeCacheEntry() and MakeCacheEntryWithTTL() are helpers to build a | |
| 1502 // HostCache::Entry(). The address list is omited from the cache entry | |
| 1503 // for errors. | |
| 1504 HostCache::Entry MakeCacheEntry(int net_error, | |
| 1505 const AddressList& addr_list) const { | |
| 1506 return HostCache::Entry( | |
| 1507 net_error, | |
| 1508 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList()); | |
| 1509 } | |
| 1510 | |
| 1511 HostCache::Entry MakeCacheEntryWithTTL(int net_error, | |
| 1512 const AddressList& addr_list, | |
| 1513 base::TimeDelta ttl) const { | |
| 1514 return HostCache::Entry( | |
| 1515 net_error, | |
| 1516 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(), | |
| 1517 ttl); | |
| 1518 } | |
| 1519 | |
| 1503 AddressList MakeAddressListForRequest(const AddressList& list) const { | 1520 AddressList MakeAddressListForRequest(const AddressList& list) const { |
| 1504 if (requests_.empty()) | 1521 if (requests_.empty()) |
| 1505 return list; | 1522 return list; |
| 1506 return AddressList::CopyWithPort(list, requests_.front()->info().port()); | 1523 return AddressList::CopyWithPort(list, requests_.front()->info().port()); |
| 1507 } | 1524 } |
| 1508 | 1525 |
| 1509 void UpdatePriority() { | 1526 void UpdatePriority() { |
| 1510 if (is_queued()) { | 1527 if (is_queued()) { |
| 1511 if (priority() != static_cast<RequestPriority>(handle_.priority())) | 1528 if (priority() != static_cast<RequestPriority>(handle_.priority())) |
| 1512 priority_change_time_ = base::TimeTicks::Now(); | 1529 priority_change_time_ = base::TimeTicks::Now(); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1621 | 1638 |
| 1622 if (ContainsIcannNameCollisionIp(addr_list)) | 1639 if (ContainsIcannNameCollisionIp(addr_list)) |
| 1623 net_error = ERR_ICANN_NAME_COLLISION; | 1640 net_error = ERR_ICANN_NAME_COLLISION; |
| 1624 | 1641 |
| 1625 base::TimeDelta ttl = | 1642 base::TimeDelta ttl = |
| 1626 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds); | 1643 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds); |
| 1627 if (net_error == OK) | 1644 if (net_error == OK) |
| 1628 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); | 1645 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); |
| 1629 | 1646 |
| 1630 // Don't store the |ttl| in cache since it's not obtained from the server. | 1647 // Don't store the |ttl| in cache since it's not obtained from the server. |
| 1631 CompleteRequests( | 1648 CompleteRequests(MakeCacheEntry(net_error, addr_list), ttl); |
| 1632 HostCache::Entry(net_error, MakeAddressListForRequest(addr_list)), | |
| 1633 ttl); | |
| 1634 } | 1649 } |
| 1635 | 1650 |
| 1636 void StartDnsTask() { | 1651 void StartDnsTask() { |
| 1637 DCHECK(resolver_->HaveDnsConfig()); | 1652 DCHECK(resolver_->HaveDnsConfig()); |
| 1638 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this, | 1653 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this, |
| 1639 net_log_)); | 1654 net_log_)); |
| 1640 | 1655 |
| 1641 dns_task_->StartFirstTransaction(); | 1656 dns_task_->StartFirstTransaction(); |
| 1642 // Schedule a second transaction, if needed. | 1657 // Schedule a second transaction, if needed. |
| 1643 if (dns_task_->needs_two_transactions()) | 1658 if (dns_task_->needs_two_transactions()) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1705 } | 1720 } |
| 1706 | 1721 |
| 1707 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS); | 1722 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS); |
| 1708 RecordTTL(ttl); | 1723 RecordTTL(ttl); |
| 1709 | 1724 |
| 1710 resolver_->OnDnsTaskResolve(OK); | 1725 resolver_->OnDnsTaskResolve(OK); |
| 1711 | 1726 |
| 1712 base::TimeDelta bounded_ttl = | 1727 base::TimeDelta bounded_ttl = |
| 1713 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds)); | 1728 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds)); |
| 1714 | 1729 |
| 1715 if (ContainsIcannNameCollisionIp(addr_list)) | 1730 if (ContainsIcannNameCollisionIp(addr_list)) { |
| 1716 net_error = ERR_ICANN_NAME_COLLISION; | 1731 CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION); |
|
eroman
2016/12/14 00:19:52
This has a subtly different behavior for caching (
| |
| 1717 | 1732 } else { |
| 1718 CompleteRequests( | 1733 CompleteRequests(MakeCacheEntryWithTTL(net_error, addr_list, ttl), |
| 1719 HostCache::Entry(net_error, MakeAddressListForRequest(addr_list), ttl), | 1734 bounded_ttl); |
| 1720 bounded_ttl); | 1735 } |
| 1721 } | 1736 } |
| 1722 | 1737 |
| 1723 void OnFirstDnsTransactionComplete() override { | 1738 void OnFirstDnsTransactionComplete() override { |
| 1724 DCHECK(dns_task_->needs_two_transactions()); | 1739 DCHECK(dns_task_->needs_two_transactions()); |
| 1725 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued()); | 1740 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued()); |
| 1726 // No longer need to occupy two dispatcher slots. | 1741 // No longer need to occupy two dispatcher slots. |
| 1727 ReduceToOneJobSlot(); | 1742 ReduceToOneJobSlot(); |
| 1728 | 1743 |
| 1729 // We already have a job slot at the dispatcher, so if the second | 1744 // We already have a job slot at the dispatcher, so if the second |
| 1730 // transaction hasn't started, reuse it now instead of waiting in the queue | 1745 // transaction hasn't started, reuse it now instead of waiting in the queue |
| (...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2616 if (job_) | 2631 if (job_) |
| 2617 job_->CancelRequest(this); | 2632 job_->CancelRequest(this); |
| 2618 } | 2633 } |
| 2619 | 2634 |
| 2620 void HostResolverImpl::RequestImpl::ChangeRequestPriority( | 2635 void HostResolverImpl::RequestImpl::ChangeRequestPriority( |
| 2621 RequestPriority priority) { | 2636 RequestPriority priority) { |
| 2622 job_->ChangeRequestPriority(this, priority); | 2637 job_->ChangeRequestPriority(this, priority); |
| 2623 } | 2638 } |
| 2624 | 2639 |
| 2625 } // namespace net | 2640 } // namespace net |
| OLD | NEW |