| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/base/host_resolver_impl.h" | 5 #include "net/base/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 11 matching lines...) Expand all Loading... |
| 22 #include "base/stl_util-inl.h" | 22 #include "base/stl_util-inl.h" |
| 23 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 24 #include "base/time.h" | 24 #include "base/time.h" |
| 25 #include "base/values.h" | 25 #include "base/values.h" |
| 26 #include "base/worker_pool.h" | 26 #include "base/worker_pool.h" |
| 27 #include "net/base/address_list.h" | 27 #include "net/base/address_list.h" |
| 28 #include "net/base/host_resolver_proc.h" | 28 #include "net/base/host_resolver_proc.h" |
| 29 #include "net/base/net_log.h" | 29 #include "net/base/net_log.h" |
| 30 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 31 #include "net/base/net_util.h" | 31 #include "net/base/net_util.h" |
| 32 #include "net/base/network_change_notifier.h" | |
| 33 | 32 |
| 34 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
| 35 #include "net/base/winsock_init.h" | 34 #include "net/base/winsock_init.h" |
| 36 #endif | 35 #endif |
| 37 | 36 |
| 38 namespace net { | 37 namespace net { |
| 39 | 38 |
| 40 namespace { | 39 namespace { |
| 41 | 40 |
| 42 HostCache* CreateDefaultCache() { | 41 HostCache* CreateDefaultCache() { |
| 43 static const size_t kMaxHostCacheEntries = 100; | 42 static const size_t kMaxHostCacheEntries = 100; |
| 44 | 43 |
| 45 HostCache* cache = new HostCache( | 44 HostCache* cache = new HostCache( |
| 46 kMaxHostCacheEntries, | 45 kMaxHostCacheEntries, |
| 47 base::TimeDelta::FromMinutes(1), | 46 base::TimeDelta::FromMinutes(1), |
| 48 base::TimeDelta::FromSeconds(0)); // Disable caching of failed DNS. | 47 base::TimeDelta::FromSeconds(0)); // Disable caching of failed DNS. |
| 49 | 48 |
| 50 return cache; | 49 return cache; |
| 51 } | 50 } |
| 52 | 51 |
| 53 } // anonymous namespace | 52 } // anonymous namespace |
| 54 | 53 |
| 55 HostResolver* CreateSystemHostResolver( | 54 HostResolver* CreateSystemHostResolver() { |
| 56 NetworkChangeNotifier* network_change_notifier) { | |
| 57 // Maximum of 50 concurrent threads. | 55 // Maximum of 50 concurrent threads. |
| 58 // TODO(eroman): Adjust this, do some A/B experiments. | 56 // TODO(eroman): Adjust this, do some A/B experiments. |
| 59 static const size_t kMaxJobs = 50u; | 57 static const size_t kMaxJobs = 50u; |
| 60 | 58 |
| 61 HostResolverImpl* resolver = new HostResolverImpl( | 59 HostResolverImpl* resolver = |
| 62 NULL, CreateDefaultCache(), network_change_notifier, kMaxJobs); | 60 new HostResolverImpl(NULL, CreateDefaultCache(), kMaxJobs); |
| 63 | 61 |
| 64 return resolver; | 62 return resolver; |
| 65 } | 63 } |
| 66 | 64 |
| 67 static int ResolveAddrInfo(HostResolverProc* resolver_proc, | 65 static int ResolveAddrInfo(HostResolverProc* resolver_proc, |
| 68 const std::string& host, | 66 const std::string& host, |
| 69 AddressFamily address_family, | 67 AddressFamily address_family, |
| 70 HostResolverFlags host_resolver_flags, | 68 HostResolverFlags host_resolver_flags, |
| 71 AddressList* out, | 69 AddressList* out, |
| 72 int* os_error) { | 70 int* os_error) { |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 | 695 |
| 698 // The requests which are waiting to be started for this pool. | 696 // The requests which are waiting to be started for this pool. |
| 699 PendingRequestsQueue pending_requests_[NUM_PRIORITIES]; | 697 PendingRequestsQueue pending_requests_[NUM_PRIORITIES]; |
| 700 }; | 698 }; |
| 701 | 699 |
| 702 //----------------------------------------------------------------------------- | 700 //----------------------------------------------------------------------------- |
| 703 | 701 |
| 704 HostResolverImpl::HostResolverImpl( | 702 HostResolverImpl::HostResolverImpl( |
| 705 HostResolverProc* resolver_proc, | 703 HostResolverProc* resolver_proc, |
| 706 HostCache* cache, | 704 HostCache* cache, |
| 707 NetworkChangeNotifier* network_change_notifier, | |
| 708 size_t max_jobs) | 705 size_t max_jobs) |
| 709 : cache_(cache), | 706 : cache_(cache), |
| 710 max_jobs_(max_jobs), | 707 max_jobs_(max_jobs), |
| 711 next_request_id_(0), | 708 next_request_id_(0), |
| 712 next_job_id_(0), | 709 next_job_id_(0), |
| 713 resolver_proc_(resolver_proc), | 710 resolver_proc_(resolver_proc), |
| 714 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), | 711 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
| 715 shutdown_(false), | 712 shutdown_(false), |
| 716 network_change_notifier_(network_change_notifier), | |
| 717 ipv6_probe_monitoring_(false) { | 713 ipv6_probe_monitoring_(false) { |
| 718 DCHECK_GT(max_jobs, 0u); | 714 DCHECK_GT(max_jobs, 0u); |
| 719 | 715 |
| 720 // It is cumbersome to expose all of the constraints in the constructor, | 716 // It is cumbersome to expose all of the constraints in the constructor, |
| 721 // so we choose some defaults, which users can override later. | 717 // so we choose some defaults, which users can override later. |
| 722 job_pools_[POOL_NORMAL] = new JobPool(max_jobs, 100u * max_jobs); | 718 job_pools_[POOL_NORMAL] = new JobPool(max_jobs, 100u * max_jobs); |
| 723 | 719 |
| 724 #if defined(OS_WIN) | 720 #if defined(OS_WIN) |
| 725 EnsureWinsockInit(); | 721 EnsureWinsockInit(); |
| 726 #endif | 722 #endif |
| 727 if (network_change_notifier_) | 723 NetworkChangeNotifier::AddObserver(this); |
| 728 network_change_notifier_->AddObserver(this); | |
| 729 } | 724 } |
| 730 | 725 |
| 731 HostResolverImpl::~HostResolverImpl() { | 726 HostResolverImpl::~HostResolverImpl() { |
| 732 // Cancel the outstanding jobs. Those jobs may contain several attached | 727 // Cancel the outstanding jobs. Those jobs may contain several attached |
| 733 // requests, which will also be cancelled. | 728 // requests, which will also be cancelled. |
| 734 DiscardIPv6ProbeJob(); | 729 DiscardIPv6ProbeJob(); |
| 735 | 730 |
| 736 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) | 731 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) |
| 737 it->second->Cancel(); | 732 it->second->Cancel(); |
| 738 | 733 |
| 739 // In case we are being deleted during the processing of a callback. | 734 // In case we are being deleted during the processing of a callback. |
| 740 if (cur_completing_job_) | 735 if (cur_completing_job_) |
| 741 cur_completing_job_->Cancel(); | 736 cur_completing_job_->Cancel(); |
| 742 | 737 |
| 743 if (network_change_notifier_) | 738 NetworkChangeNotifier::RemoveObserver(this); |
| 744 network_change_notifier_->RemoveObserver(this); | |
| 745 | 739 |
| 746 // Delete the job pools. | 740 // Delete the job pools. |
| 747 for (size_t i = 0u; i < arraysize(job_pools_); ++i) | 741 for (size_t i = 0u; i < arraysize(job_pools_); ++i) |
| 748 delete job_pools_[i]; | 742 delete job_pools_[i]; |
| 749 } | 743 } |
| 750 | 744 |
| 751 // TODO(eroman): Don't create cache entries for hostnames which are simply IP | 745 // TODO(eroman): Don't create cache entries for hostnames which are simply IP |
| 752 // address literals. | 746 // address literals. |
| 753 int HostResolverImpl::Resolve(const RequestInfo& info, | 747 int HostResolverImpl::Resolve(const RequestInfo& info, |
| 754 AddressList* addresses, | 748 AddressList* addresses, |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 if (r == req) | 1148 if (r == req) |
| 1155 return error; | 1149 return error; |
| 1156 | 1150 |
| 1157 r->OnComplete(error, AddressList()); | 1151 r->OnComplete(error, AddressList()); |
| 1158 } | 1152 } |
| 1159 | 1153 |
| 1160 return ERR_IO_PENDING; | 1154 return ERR_IO_PENDING; |
| 1161 } | 1155 } |
| 1162 | 1156 |
| 1163 } // namespace net | 1157 } // namespace net |
| OLD | NEW |