Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / net / dns / host_resolver_impl.h
blob7bcc8007172d24dfeddf416e752a3708761af0dc
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
3 // found in the LICENSE file.
5 #ifndef NET_DNS_HOST_RESOLVER_IMPL_H_
6 #define NET_DNS_HOST_RESOLVER_IMPL_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/time/time.h"
17 #include "net/base/net_export.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/base/prioritized_dispatcher.h"
20 #include "net/dns/host_cache.h"
21 #include "net/dns/host_resolver.h"
22 #include "net/dns/host_resolver_proc.h"
24 namespace net {
26 class BoundNetLog;
27 class DnsClient;
28 class NetLog;
30 // For each hostname that is requested, HostResolver creates a
31 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask
32 // which runs the given HostResolverProc on a WorkerPool thread. If requests for
33 // that same host are made during the job's lifetime, they are attached to the
34 // existing job rather than creating a new one. This avoids doing parallel
35 // resolves for the same host.
37 // The way these classes fit together is illustrated by:
40 // +----------- HostResolverImpl -------------+
41 // | | |
42 // Job Job Job
43 // (for host1, fam1) (for host2, fam2) (for hostx, famx)
44 // / | | / | | / | |
45 // Request ... Request Request ... Request Request ... Request
46 // (port1) (port2) (port3) (port4) (port5) (portX)
48 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request
49 // are run on the origin thread.
51 // Thread safety: This class is not threadsafe, and must only be called
52 // from one thread!
54 // The HostResolverImpl enforces limits on the maximum number of concurrent
55 // threads using PrioritizedDispatcher::Limits.
57 // Jobs are ordered in the queue based on their priority and order of arrival.
58 class NET_EXPORT HostResolverImpl
59 : public HostResolver,
60 NON_EXPORTED_BASE(public base::NonThreadSafe),
61 public NetworkChangeNotifier::IPAddressObserver,
62 public NetworkChangeNotifier::DNSObserver {
63 public:
64 // Parameters for ProcTask which resolves hostnames using HostResolveProc.
66 // |resolver_proc| is used to perform the actual resolves; it must be
67 // thread-safe since it is run from multiple worker threads. If
68 // |resolver_proc| is NULL then the default host resolver procedure is
69 // used (which is SystemHostResolverProc except if overridden).
71 // For each attempt, we could start another attempt if host is not resolved
72 // within |unresponsive_delay| time. We keep attempting to resolve the host
73 // for |max_retry_attempts|. For every retry attempt, we grow the
74 // |unresponsive_delay| by the |retry_factor| amount (that is retry interval
75 // is multiplied by the retry factor each time). Once we have retried
76 // |max_retry_attempts|, we give up on additional attempts.
78 struct NET_EXPORT_PRIVATE ProcTaskParams {
79 // Sets up defaults.
80 ProcTaskParams(HostResolverProc* resolver_proc, size_t max_retry_attempts);
82 ~ProcTaskParams();
84 // The procedure to use for resolving host names. This will be NULL, except
85 // in the case of unit-tests which inject custom host resolving behaviors.
86 scoped_refptr<HostResolverProc> resolver_proc;
88 // Maximum number retry attempts to resolve the hostname.
89 // Pass HostResolver::kDefaultRetryAttempts to choose a default value.
90 size_t max_retry_attempts;
92 // This is the limit after which we make another attempt to resolve the host
93 // if the worker thread has not responded yet.
94 base::TimeDelta unresponsive_delay;
96 // Factor to grow |unresponsive_delay| when we re-re-try.
97 uint32 retry_factor;
100 // Creates a HostResolver that first uses the local cache |cache|, and then
101 // falls back to |proc_params.resolver_proc|.
103 // If |cache| is NULL, then no caching is used. Otherwise we take
104 // ownership of the |cache| pointer, and will free it during destruction.
106 // |job_limits| specifies the maximum number of jobs that the resolver will
107 // run at once. This upper-bounds the total number of outstanding
108 // DNS transactions (not counting retransmissions and retries).
110 // |net_log| must remain valid for the life of the HostResolverImpl.
111 HostResolverImpl(scoped_ptr<HostCache> cache,
112 const PrioritizedDispatcher::Limits& job_limits,
113 const ProcTaskParams& proc_params,
114 NetLog* net_log);
116 // If any completion callbacks are pending when the resolver is destroyed,
117 // the host resolutions are cancelled, and the completion callbacks will not
118 // be called.
119 virtual ~HostResolverImpl();
121 // Configures maximum number of Jobs in the queue. Exposed for testing.
122 // Only allowed when the queue is empty.
123 void SetMaxQueuedJobs(size_t value);
125 // Set the DnsClient to be used for resolution. In case of failure, the
126 // HostResolverProc from ProcTaskParams will be queried. If the DnsClient is
127 // not pre-configured with a valid DnsConfig, a new config is fetched from
128 // NetworkChangeNotifier.
129 void SetDnsClient(scoped_ptr<DnsClient> dns_client);
131 // HostResolver methods:
132 virtual int Resolve(const RequestInfo& info,
133 RequestPriority priority,
134 AddressList* addresses,
135 const CompletionCallback& callback,
136 RequestHandle* out_req,
137 const BoundNetLog& source_net_log) OVERRIDE;
138 virtual int ResolveFromCache(const RequestInfo& info,
139 AddressList* addresses,
140 const BoundNetLog& source_net_log) OVERRIDE;
141 virtual void CancelRequest(RequestHandle req) OVERRIDE;
142 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE;
143 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE;
144 virtual void SetDnsClientEnabled(bool enabled) OVERRIDE;
145 virtual HostCache* GetHostCache() OVERRIDE;
146 virtual base::Value* GetDnsConfigAsValue() const OVERRIDE;
148 private:
149 friend class HostResolverImplTest;
150 class Job;
151 class ProcTask;
152 class LoopbackProbeJob;
153 class DnsTask;
154 class Request;
155 typedef HostCache::Key Key;
156 typedef std::map<Key, Job*> JobMap;
157 typedef ScopedVector<Request> RequestsList;
159 // Number of consecutive failures of DnsTask (with successful fallback to
160 // ProcTask) before the DnsClient is disabled until the next DNS change.
161 static const unsigned kMaximumDnsFailures;
163 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP
164 // literal, cache and HOSTS lookup (if enabled), returns OK if successful,
165 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is
166 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache and HOSTS.
167 int ResolveHelper(const Key& key,
168 const RequestInfo& info,
169 AddressList* addresses,
170 const BoundNetLog& request_net_log);
172 // Tries to resolve |key| as an IP, returns true and sets |net_error| if
173 // succeeds, returns false otherwise.
174 bool ResolveAsIP(const Key& key,
175 const RequestInfo& info,
176 int* net_error,
177 AddressList* addresses);
179 // If |key| is not found in cache returns false, otherwise returns
180 // true, sets |net_error| to the cached error code and fills |addresses|
181 // if it is a positive entry.
182 bool ServeFromCache(const Key& key,
183 const RequestInfo& info,
184 int* net_error,
185 AddressList* addresses);
187 // If we have a DnsClient with a valid DnsConfig, and |key| is found in the
188 // HOSTS file, returns true and fills |addresses|. Otherwise returns false.
189 bool ServeFromHosts(const Key& key,
190 const RequestInfo& info,
191 AddressList* addresses);
193 // Callback from HaveOnlyLoopbackAddresses probe.
194 void SetHaveOnlyLoopbackAddresses(bool result);
196 // Returns the (hostname, address_family) key to use for |info|, choosing an
197 // "effective" address family by inheriting the resolver's default address
198 // family when the request leaves it unspecified.
199 Key GetEffectiveKeyForRequest(const RequestInfo& info,
200 const BoundNetLog& net_log) const;
202 // Records the result in cache if cache is present.
203 void CacheResult(const Key& key,
204 const HostCache::Entry& entry,
205 base::TimeDelta ttl);
207 // Removes |job| from |jobs_|, only if it exists.
208 void RemoveJob(Job* job);
210 // Aborts all in progress jobs with ERR_NETWORK_CHANGED and notifies their
211 // requests. Might start new jobs.
212 void AbortAllInProgressJobs();
214 // Aborts all in progress DnsTasks. In-progress jobs will fall back to
215 // ProcTasks. Might start new jobs, if any jobs were taking up two dispatcher
216 // slots.
217 void AbortDnsTasks();
219 // Attempts to serve each Job in |jobs_| from the HOSTS file if we have
220 // a DnsClient with a valid DnsConfig.
221 void TryServingAllJobsFromHosts();
223 // NetworkChangeNotifier::IPAddressObserver:
224 virtual void OnIPAddressChanged() OVERRIDE;
226 // NetworkChangeNotifier::DNSObserver:
227 virtual void OnDNSChanged() OVERRIDE;
229 // True if have a DnsClient with a valid DnsConfig.
230 bool HaveDnsConfig() const;
232 // Called when a host name is successfully resolved and DnsTask was run on it
233 // and resulted in |net_error|.
234 void OnDnsTaskResolve(int net_error);
236 // Allows the tests to catch slots leaking out of the dispatcher. One
237 // HostResolverImpl::Job could occupy multiple PrioritizedDispatcher job
238 // slots.
239 size_t num_running_dispatcher_jobs_for_tests() const {
240 return dispatcher_.num_running_jobs();
243 // Cache of host resolution results.
244 scoped_ptr<HostCache> cache_;
246 // Map from HostCache::Key to a Job.
247 JobMap jobs_;
249 // Starts Jobs according to their priority and the configured limits.
250 PrioritizedDispatcher dispatcher_;
252 // Limit on the maximum number of jobs queued in |dispatcher_|.
253 size_t max_queued_jobs_;
255 // Parameters for ProcTask.
256 ProcTaskParams proc_params_;
258 NetLog* net_log_;
260 // Address family to use when the request doesn't specify one.
261 AddressFamily default_address_family_;
263 // If present, used by DnsTask and ServeFromHosts to resolve requests.
264 scoped_ptr<DnsClient> dns_client_;
266 // True if received valid config from |dns_config_service_|. Temporary, used
267 // to measure performance of DnsConfigService: http://crbug.com/125599
268 bool received_dns_config_;
270 // Number of consecutive failures of DnsTask, counted when fallback succeeds.
271 unsigned num_dns_failures_;
273 // True if probing is done for each Request to set address family. When false,
274 // explicit setting in |default_address_family_| is used.
275 bool probe_ipv6_support_;
277 // True if DnsConfigService detected that system configuration depends on
278 // local IPv6 connectivity. Disables probing.
279 bool use_local_ipv6_;
281 // True iff ProcTask has successfully resolved a hostname known to have IPv6
282 // addresses using ADDRESS_FAMILY_UNSPECIFIED. Reset on IP address change.
283 bool resolved_known_ipv6_hostname_;
285 // Any resolver flags that should be added to a request by default.
286 HostResolverFlags additional_resolver_flags_;
288 // Allow fallback to ProcTask if DnsTask fails.
289 bool fallback_to_proctask_;
291 base::WeakPtrFactory<HostResolverImpl> weak_ptr_factory_;
293 base::WeakPtrFactory<HostResolverImpl> probe_weak_ptr_factory_;
295 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl);
298 } // namespace net
300 #endif // NET_DNS_HOST_RESOLVER_IMPL_H_