| 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 #ifndef NET_BASE_HOST_RESOLVER_IMPL_H_ | 5 #ifndef NET_BASE_HOST_RESOLVER_IMPL_H_ |
| 6 #define NET_BASE_HOST_RESOLVER_IMPL_H_ | 6 #define NET_BASE_HOST_RESOLVER_IMPL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "net/base/capturing_net_log.h" | 18 #include "net/base/capturing_net_log.h" |
| 19 #include "net/base/host_cache.h" | 19 #include "net/base/host_cache.h" |
| 20 #include "net/base/host_resolver.h" | 20 #include "net/base/host_resolver.h" |
| 21 #include "net/base/host_resolver_proc.h" | 21 #include "net/base/host_resolver_proc.h" |
| 22 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
| 23 #include "net/base/net_log.h" | 23 #include "net/base/net_log.h" |
| 24 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
| 25 #include "net/base/prioritized_dispatcher.h" | 25 #include "net/base/prioritized_dispatcher.h" |
| 26 #include "net/dns/dns_config_service.h" |
| 26 | 27 |
| 27 namespace net { | 28 namespace net { |
| 28 | 29 |
| 30 class DnsTransactionFactory; |
| 31 |
| 29 // For each hostname that is requested, HostResolver creates a | 32 // For each hostname that is requested, HostResolver creates a |
| 30 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcJob | 33 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask |
| 31 // which runs the given HostResolverProc on a WorkerPool thread. If requests for | 34 // which runs the given HostResolverProc on a WorkerPool thread. If requests for |
| 32 // that same host are made during the job's lifetime, they are attached to the | 35 // that same host are made during the job's lifetime, they are attached to the |
| 33 // existing job rather than creating a new one. This avoids doing parallel | 36 // existing job rather than creating a new one. This avoids doing parallel |
| 34 // resolves for the same host. | 37 // resolves for the same host. |
| 35 // | 38 // |
| 36 // The way these classes fit together is illustrated by: | 39 // The way these classes fit together is illustrated by: |
| 37 // | 40 // |
| 38 // | 41 // |
| 39 // +----------- HostResolverImpl -------------+ | 42 // +----------- HostResolverImpl -------------+ |
| 40 // | | | | 43 // | | | |
| 41 // Job Job Job | 44 // Job Job Job |
| 42 // (for host1, fam1) (for host2, fam2) (for hostx, famx) | 45 // (for host1, fam1) (for host2, fam2) (for hostx, famx) |
| 43 // / | | / | | / | | | 46 // / | | / | | / | | |
| 44 // Request ... Request Request ... Request Request ... Request | 47 // Request ... Request Request ... Request Request ... Request |
| 45 // (port1) (port2) (port3) (port4) (port5) (portX) | 48 // (port1) (port2) (port3) (port4) (port5) (portX) |
| 46 // | 49 // |
| 47 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request | 50 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request |
| 48 // are run on the origin thread. | 51 // are run on the origin thread. |
| 49 // | 52 // |
| 50 // Thread safety: This class is not threadsafe, and must only be called | 53 // Thread safety: This class is not threadsafe, and must only be called |
| 51 // from one thread! | 54 // from one thread! |
| 52 // | 55 // |
| 53 // The HostResolverImpl enforces limits on the maximum number of concurrent | 56 // The HostResolverImpl enforces limits on the maximum number of concurrent |
| 54 // threads using PrioritizedDispatcher::Limits. | 57 // threads using PrioritizedDispatcher::Limits. |
| 55 // | 58 // |
| 56 // Jobs are ordered in the queue based on their priority and order of arrival. | 59 // Jobs are ordered in the queue based on their priority and order of arrival. |
| 57 // | 60 // |
| 61 // TODO(szym): Change DnsConfigService::Observer to Callback. |
| 58 class NET_EXPORT HostResolverImpl | 62 class NET_EXPORT HostResolverImpl |
| 59 : public HostResolver, | 63 : public HostResolver, |
| 60 NON_EXPORTED_BASE(public base::NonThreadSafe), | 64 NON_EXPORTED_BASE(public base::NonThreadSafe), |
| 61 public NetworkChangeNotifier::IPAddressObserver, | 65 public NetworkChangeNotifier::IPAddressObserver, |
| 62 public NetworkChangeNotifier::DNSObserver, | 66 public NetworkChangeNotifier::DNSObserver, |
| 67 NON_EXPORTED_BASE(public DnsConfigService::Observer), |
| 63 public base::SupportsWeakPtr<HostResolverImpl> { | 68 public base::SupportsWeakPtr<HostResolverImpl> { |
| 64 public: | 69 public: |
| 65 // Parameters for ProcTask which resolves hostnames using HostResolveProc. | 70 // Parameters for ProcTask which resolves hostnames using HostResolveProc. |
| 66 // | 71 // |
| 67 // |resolver_proc| is used to perform the actual resolves; it must be | 72 // |resolver_proc| is used to perform the actual resolves; it must be |
| 68 // thread-safe since it is run from multiple worker threads. If | 73 // thread-safe since it is run from multiple worker threads. If |
| 69 // |resolver_proc| is NULL then the default host resolver procedure is | 74 // |resolver_proc| is NULL then the default host resolver procedure is |
| 70 // used (which is SystemHostResolverProc except if overridden). | 75 // used (which is SystemHostResolverProc except if overridden). |
| 71 // | 76 // |
| 72 // For each attempt, we could start another attempt if host is not resolved | 77 // For each attempt, we could start another attempt if host is not resolved |
| (...skipping 25 matching lines...) Expand all Loading... |
| 98 uint32 retry_factor; | 103 uint32 retry_factor; |
| 99 }; | 104 }; |
| 100 | 105 |
| 101 // Creates a HostResolver that first uses the local cache |cache|, and then | 106 // Creates a HostResolver that first uses the local cache |cache|, and then |
| 102 // falls back to |proc_params.resolver_proc|. | 107 // falls back to |proc_params.resolver_proc|. |
| 103 // | 108 // |
| 104 // If |cache| is NULL, then no caching is used. Otherwise we take | 109 // If |cache| is NULL, then no caching is used. Otherwise we take |
| 105 // ownership of the |cache| pointer, and will free it during destruction. | 110 // ownership of the |cache| pointer, and will free it during destruction. |
| 106 // | 111 // |
| 107 // |job_limits| specifies the maximum number of jobs that the resolver will | 112 // |job_limits| specifies the maximum number of jobs that the resolver will |
| 108 // run at once (not counting potential duplicate attempts). | 113 // run at once. This upper-bounds the total number of outstanding |
| 114 // DNS transactions (not counting retransmissions and retries). |
| 115 // |
| 116 // |dns_config_service| will be used to obtain DnsConfig for |
| 117 // DnsTransactionFactory. |
| 109 // | 118 // |
| 110 // |net_log| must remain valid for the life of the HostResolverImpl. | 119 // |net_log| must remain valid for the life of the HostResolverImpl. |
| 111 // TODO(szym): change to scoped_ptr<HostCache>. | 120 // TODO(szym): change to scoped_ptr<HostCache>. |
| 112 HostResolverImpl(HostCache* cache, | 121 HostResolverImpl(HostCache* cache, |
| 113 const PrioritizedDispatcher::Limits& job_limits, | 122 const PrioritizedDispatcher::Limits& job_limits, |
| 114 const ProcTaskParams& proc_params, | 123 const ProcTaskParams& proc_params, |
| 124 scoped_ptr<DnsConfigService> dns_config_service, |
| 115 NetLog* net_log); | 125 NetLog* net_log); |
| 116 | 126 |
| 117 // If any completion callbacks are pending when the resolver is destroyed, | 127 // If any completion callbacks are pending when the resolver is destroyed, |
| 118 // the host resolutions are cancelled, and the completion callbacks will not | 128 // the host resolutions are cancelled, and the completion callbacks will not |
| 119 // be called. | 129 // be called. |
| 120 virtual ~HostResolverImpl(); | 130 virtual ~HostResolverImpl(); |
| 121 | 131 |
| 122 // Configures maximum number of Jobs in the queue. Exposed for testing. | 132 // Configures maximum number of Jobs in the queue. Exposed for testing. |
| 123 // Only allowed when the queue is empty. | 133 // Only allowed when the queue is empty. |
| 124 void SetMaxQueuedJobs(size_t value); | 134 void SetMaxQueuedJobs(size_t value); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 135 virtual void CancelRequest(RequestHandle req) OVERRIDE; | 145 virtual void CancelRequest(RequestHandle req) OVERRIDE; |
| 136 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; | 146 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; |
| 137 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; | 147 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; |
| 138 virtual void ProbeIPv6Support() OVERRIDE; | 148 virtual void ProbeIPv6Support() OVERRIDE; |
| 139 virtual HostCache* GetHostCache() OVERRIDE; | 149 virtual HostCache* GetHostCache() OVERRIDE; |
| 140 | 150 |
| 141 private: | 151 private: |
| 142 class Job; | 152 class Job; |
| 143 class ProcTask; | 153 class ProcTask; |
| 144 class IPv6ProbeJob; | 154 class IPv6ProbeJob; |
| 155 class DnsTask; |
| 145 class Request; | 156 class Request; |
| 146 typedef HostCache::Key Key; | 157 typedef HostCache::Key Key; |
| 147 typedef std::map<Key, Job*> JobMap; | 158 typedef std::map<Key, Job*> JobMap; |
| 148 typedef std::vector<Request*> RequestsList; | 159 typedef std::vector<Request*> RequestsList; |
| 149 | 160 |
| 150 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP | 161 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP |
| 151 // literal and cache lookup, returns OK if successful, | 162 // literal and cache lookup, returns OK if successful, |
| 152 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is | 163 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is |
| 153 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache. | 164 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache. |
| 154 int ResolveHelper(const Key& key, | 165 int ResolveHelper(const Key& key, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 176 void DiscardIPv6ProbeJob(); | 187 void DiscardIPv6ProbeJob(); |
| 177 | 188 |
| 178 // Callback from IPv6 probe activity. | 189 // Callback from IPv6 probe activity. |
| 179 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); | 190 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); |
| 180 | 191 |
| 181 // Returns the (hostname, address_family) key to use for |info|, choosing an | 192 // Returns the (hostname, address_family) key to use for |info|, choosing an |
| 182 // "effective" address family by inheriting the resolver's default address | 193 // "effective" address family by inheriting the resolver's default address |
| 183 // family when the request leaves it unspecified. | 194 // family when the request leaves it unspecified. |
| 184 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; | 195 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; |
| 185 | 196 |
| 186 // Called by |job| when it has finished running. Records the result in cache | 197 // Called by |job| when it has completed. Records the result in cache |
| 187 // if necessary and dispatches another job if possible. | 198 // if necessary and dispatches another job if possible. |
| 188 void OnJobFinished(Job* job, const AddressList& addrlist); | 199 void OnJobFinished(Job* job, |
| 200 int net_error, |
| 201 const AddressList& addr_list, |
| 202 base::TimeDelta ttl); |
| 189 | 203 |
| 190 // Removes |job| from |jobs_|. | 204 // Removes |job| from |jobs_|. |
| 191 void RemoveJob(Job* job); | 205 void RemoveJob(Job* job); |
| 192 | 206 |
| 193 // Aborts all in progress jobs and notifies their requests. | 207 // Aborts all in progress jobs and notifies their requests. |
| 194 // Might start new jobs. | 208 // Might start new jobs. |
| 195 void AbortAllInProgressJobs(); | 209 void AbortAllInProgressJobs(); |
| 196 | 210 |
| 197 // NetworkChangeNotifier::IPAddressObserver methods: | 211 // NetworkChangeNotifier::IPAddressObserver: |
| 198 virtual void OnIPAddressChanged() OVERRIDE; | 212 virtual void OnIPAddressChanged() OVERRIDE; |
| 199 | 213 |
| 200 // NetworkChangeNotifier::OnDNSChanged methods: | 214 // NetworkChangeNotifier::DNSObserver: |
| 201 virtual void OnDNSChanged() OVERRIDE; | 215 virtual void OnDNSChanged() OVERRIDE; |
| 202 | 216 |
| 217 // DnsConfigService::Observer: |
| 218 virtual void OnConfigChanged(const DnsConfig& dns_config) OVERRIDE; |
| 219 |
| 203 // Cache of host resolution results. | 220 // Cache of host resolution results. |
| 204 scoped_ptr<HostCache> cache_; | 221 scoped_ptr<HostCache> cache_; |
| 205 | 222 |
| 206 // Map from HostCache::Key to a Job. | 223 // Map from HostCache::Key to a Job. |
| 207 JobMap jobs_; | 224 JobMap jobs_; |
| 208 | 225 |
| 209 // Starts Jobs according to their priority and the configured limits. | 226 // Starts Jobs according to their priority and the configured limits. |
| 210 PrioritizedDispatcher dispatcher_; | 227 PrioritizedDispatcher dispatcher_; |
| 211 | 228 |
| 212 // Limit on the maximum number of jobs queued in |dispatcher_|. | 229 // Limit on the maximum number of jobs queued in |dispatcher_|. |
| 213 size_t max_queued_jobs_; | 230 size_t max_queued_jobs_; |
| 214 | 231 |
| 215 // Parameters for ProcTask. | 232 // Parameters for ProcTask. |
| 216 ProcTaskParams proc_params_; | 233 ProcTaskParams proc_params_; |
| 217 | 234 |
| 235 scoped_ptr<DnsTransactionFactory> dns_transaction_factory_; |
| 236 |
| 218 // Address family to use when the request doesn't specify one. | 237 // Address family to use when the request doesn't specify one. |
| 219 AddressFamily default_address_family_; | 238 AddressFamily default_address_family_; |
| 220 | 239 |
| 240 scoped_ptr<DnsConfigService> dns_config_service_; |
| 241 |
| 221 // Indicate if probing is done after each network change event to set address | 242 // Indicate if probing is done after each network change event to set address |
| 222 // family. | 243 // family. |
| 223 // When false, explicit setting of address family is used. | 244 // When false, explicit setting of address family is used. |
| 224 bool ipv6_probe_monitoring_; | 245 bool ipv6_probe_monitoring_; |
| 225 | 246 |
| 226 // The last un-cancelled IPv6ProbeJob (if any). | 247 // The last un-cancelled IPv6ProbeJob (if any). |
| 227 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; | 248 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; |
| 228 | 249 |
| 229 // Any resolver flags that should be added to a request by default. | 250 // Any resolver flags that should be added to a request by default. |
| 230 HostResolverFlags additional_resolver_flags_; | 251 HostResolverFlags additional_resolver_flags_; |
| 231 | 252 |
| 232 NetLog* net_log_; | 253 NetLog* net_log_; |
| 233 | 254 |
| 234 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); | 255 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); |
| 235 }; | 256 }; |
| 236 | 257 |
| 237 } // namespace net | 258 } // namespace net |
| 238 | 259 |
| 239 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ | 260 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ |
| OLD | NEW |