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_client.h" |
26 #include "net/dns/dns_config_service.h" | 27 #include "net/dns/dns_config_service.h" |
27 | 28 |
28 namespace net { | 29 namespace net { |
29 | 30 |
30 class DnsTransactionFactory; | |
31 | |
32 // For each hostname that is requested, HostResolver creates a | 31 // For each hostname that is requested, HostResolver creates a |
33 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask | 32 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask |
34 // which runs the given HostResolverProc on a WorkerPool thread. If requests for | 33 // which runs the given HostResolverProc on a WorkerPool thread. If requests for |
35 // that same host are made during the job's lifetime, they are attached to the | 34 // that same host are made during the job's lifetime, they are attached to the |
36 // existing job rather than creating a new one. This avoids doing parallel | 35 // existing job rather than creating a new one. This avoids doing parallel |
37 // resolves for the same host. | 36 // resolves for the same host. |
38 // | 37 // |
39 // The way these classes fit together is illustrated by: | 38 // The way these classes fit together is illustrated by: |
40 // | 39 // |
41 // | 40 // |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; | 146 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; |
148 virtual void ProbeIPv6Support() OVERRIDE; | 147 virtual void ProbeIPv6Support() OVERRIDE; |
149 virtual HostCache* GetHostCache() OVERRIDE; | 148 virtual HostCache* GetHostCache() OVERRIDE; |
150 | 149 |
151 // Allows the tests to catch slots leaking out of the dispatcher. | 150 // Allows the tests to catch slots leaking out of the dispatcher. |
152 size_t num_running_jobs_for_tests() const { | 151 size_t num_running_jobs_for_tests() const { |
153 return dispatcher_.num_running_jobs(); | 152 return dispatcher_.num_running_jobs(); |
154 } | 153 } |
155 | 154 |
156 private: | 155 private: |
| 156 FRIEND_TEST_ALL_PREFIXES(HostResolverImplTest, DnsTask); |
| 157 FRIEND_TEST_ALL_PREFIXES(HostResolverImplTest, ServeFromHosts); |
157 class Job; | 158 class Job; |
158 class ProcTask; | 159 class ProcTask; |
159 class IPv6ProbeJob; | 160 class IPv6ProbeJob; |
160 class DnsTask; | 161 class DnsTask; |
161 class Request; | 162 class Request; |
162 typedef HostCache::Key Key; | 163 typedef HostCache::Key Key; |
163 typedef std::map<Key, Job*> JobMap; | 164 typedef std::map<Key, Job*> JobMap; |
164 typedef std::vector<Request*> RequestsList; | 165 typedef std::vector<Request*> RequestsList; |
165 | 166 |
| 167 void set_dns_client_for_tests(scoped_ptr<DnsClient> client) { |
| 168 dns_client_ = client.Pass(); |
| 169 } |
| 170 |
166 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP | 171 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP |
167 // literal and cache lookup, returns OK if successful, | 172 // literal, cache and HOSTS lookup (if enabled), returns OK if successful, |
168 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is | 173 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is |
169 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache. | 174 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache and HOSTS. |
170 int ResolveHelper(const Key& key, | 175 int ResolveHelper(const Key& key, |
171 const RequestInfo& info, | 176 const RequestInfo& info, |
172 AddressList* addresses, | 177 AddressList* addresses, |
173 const BoundNetLog& request_net_log); | 178 const BoundNetLog& request_net_log); |
174 | 179 |
175 // Tries to resolve |key| as an IP, returns true and sets |net_error| if | 180 // Tries to resolve |key| as an IP, returns true and sets |net_error| if |
176 // succeeds, returns false otherwise. | 181 // succeeds, returns false otherwise. |
177 bool ResolveAsIP(const Key& key, | 182 bool ResolveAsIP(const Key& key, |
178 const RequestInfo& info, | 183 const RequestInfo& info, |
179 int* net_error, | 184 int* net_error, |
180 AddressList* addresses); | 185 AddressList* addresses); |
181 | 186 |
182 // If |key| is not found in cache returns false, otherwise returns | 187 // If |key| is not found in cache returns false, otherwise returns |
183 // true, sets |net_error| to the cached error code and fills |addresses| | 188 // true, sets |net_error| to the cached error code and fills |addresses| |
184 // if it is a positive entry. | 189 // if it is a positive entry. |
185 bool ServeFromCache(const Key& key, | 190 bool ServeFromCache(const Key& key, |
186 const RequestInfo& info, | 191 const RequestInfo& info, |
187 const BoundNetLog& request_net_log, | |
188 int* net_error, | 192 int* net_error, |
189 AddressList* addresses); | 193 AddressList* addresses); |
190 | 194 |
| 195 // If |key| is not found in the HOSTS file or no HOSTS file known, returns |
| 196 // false, otherwise returns true and fills |addresses|. |
| 197 bool ServeFromHosts(const Key& key, |
| 198 const RequestInfo& info, |
| 199 AddressList* addresses); |
| 200 |
191 // Notifies IPv6ProbeJob not to call back, and discard reference to the job. | 201 // Notifies IPv6ProbeJob not to call back, and discard reference to the job. |
192 void DiscardIPv6ProbeJob(); | 202 void DiscardIPv6ProbeJob(); |
193 | 203 |
194 // Callback from IPv6 probe activity. | 204 // Callback from IPv6 probe activity. |
195 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); | 205 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); |
196 | 206 |
197 // Returns the (hostname, address_family) key to use for |info|, choosing an | 207 // Returns the (hostname, address_family) key to use for |info|, choosing an |
198 // "effective" address family by inheriting the resolver's default address | 208 // "effective" address family by inheriting the resolver's default address |
199 // family when the request leaves it unspecified. | 209 // family when the request leaves it unspecified. |
200 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; | 210 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; |
201 | 211 |
202 // Records the result in cache if cache is present. | 212 // Records the result in cache if cache is present. |
203 void CacheResult(const Key& key, | 213 void CacheResult(const Key& key, |
204 int net_error, | 214 int net_error, |
205 const AddressList& addr_list, | 215 const AddressList& addr_list, |
206 base::TimeDelta ttl); | 216 base::TimeDelta ttl); |
207 | 217 |
208 // Removes |job| from |jobs_|, only if it exists. | 218 // Removes |job| from |jobs_|, only if it exists. |
209 void RemoveJob(Job* job); | 219 void RemoveJob(Job* job); |
210 | 220 |
211 // Aborts all in progress jobs and notifies their requests. | 221 // Aborts all in progress jobs and notifies their requests. |
212 // Might start new jobs. | 222 // Might start new jobs. |
213 void AbortAllInProgressJobs(); | 223 void AbortAllInProgressJobs(); |
214 | 224 |
| 225 // Attempts to serve each Job in |jobs_| from the HOSTS file. |
| 226 void TryServingAllJobsFromHosts(); |
| 227 |
215 // NetworkChangeNotifier::IPAddressObserver: | 228 // NetworkChangeNotifier::IPAddressObserver: |
216 virtual void OnIPAddressChanged() OVERRIDE; | 229 virtual void OnIPAddressChanged() OVERRIDE; |
217 | 230 |
218 // NetworkChangeNotifier::DNSObserver: | 231 // NetworkChangeNotifier::DNSObserver: |
219 virtual void OnDNSChanged(unsigned detail) OVERRIDE; | 232 virtual void OnDNSChanged(unsigned detail) OVERRIDE; |
220 | 233 |
221 // DnsConfigService::Observer: | 234 // DnsConfigService::Observer: |
222 virtual void OnConfigChanged(const DnsConfig& dns_config) OVERRIDE; | 235 virtual void OnConfigChanged(const DnsConfig& dns_config) OVERRIDE; |
223 | 236 |
| 237 // True if have fully configured DNS client. |
| 238 bool HaveDnsConfig() const; |
| 239 |
224 // Cache of host resolution results. | 240 // Cache of host resolution results. |
225 scoped_ptr<HostCache> cache_; | 241 scoped_ptr<HostCache> cache_; |
226 | 242 |
227 // Map from HostCache::Key to a Job. | 243 // Map from HostCache::Key to a Job. |
228 JobMap jobs_; | 244 JobMap jobs_; |
229 | 245 |
230 // Starts Jobs according to their priority and the configured limits. | 246 // Starts Jobs according to their priority and the configured limits. |
231 PrioritizedDispatcher dispatcher_; | 247 PrioritizedDispatcher dispatcher_; |
232 | 248 |
233 // Limit on the maximum number of jobs queued in |dispatcher_|. | 249 // Limit on the maximum number of jobs queued in |dispatcher_|. |
234 size_t max_queued_jobs_; | 250 size_t max_queued_jobs_; |
235 | 251 |
236 // Parameters for ProcTask. | 252 // Parameters for ProcTask. |
237 ProcTaskParams proc_params_; | 253 ProcTaskParams proc_params_; |
238 | 254 |
239 scoped_ptr<DnsTransactionFactory> dns_transaction_factory_; | |
240 | |
241 // Address family to use when the request doesn't specify one. | 255 // Address family to use when the request doesn't specify one. |
242 AddressFamily default_address_family_; | 256 AddressFamily default_address_family_; |
243 | 257 |
| 258 scoped_ptr<DnsClient> dns_client_; |
244 scoped_ptr<DnsConfigService> dns_config_service_; | 259 scoped_ptr<DnsConfigService> dns_config_service_; |
245 | 260 |
246 // Indicate if probing is done after each network change event to set address | 261 // Indicate if probing is done after each network change event to set address |
247 // family. | 262 // family. |
248 // When false, explicit setting of address family is used. | 263 // When false, explicit setting of address family is used. |
249 bool ipv6_probe_monitoring_; | 264 bool ipv6_probe_monitoring_; |
250 | 265 |
251 // The last un-cancelled IPv6ProbeJob (if any). | 266 // The last un-cancelled IPv6ProbeJob (if any). |
252 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; | 267 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; |
253 | 268 |
254 // Any resolver flags that should be added to a request by default. | 269 // Any resolver flags that should be added to a request by default. |
255 HostResolverFlags additional_resolver_flags_; | 270 HostResolverFlags additional_resolver_flags_; |
256 | 271 |
257 NetLog* net_log_; | 272 NetLog* net_log_; |
258 | 273 |
259 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); | 274 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); |
260 }; | 275 }; |
261 | 276 |
262 } // namespace net | 277 } // namespace net |
263 | 278 |
264 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ | 279 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ |
OLD | NEW |