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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; | 145 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; |
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 |
155 void set_dns_client_for_tests(scoped_ptr<DnsClient> client) { | |
mmenke
2012/03/13 15:56:27
Suggest you make this and the above function priva
szym
2012/03/13 20:06:42
Not a fan of FRIEND_TEST*. It completely opens the
mmenke
2012/03/13 20:31:08
SGTM. I prefer just friending the fixture as well
| |
156 dns_client_ = client.Pass(); | |
157 } | |
158 | |
156 private: | 159 private: |
157 class Job; | 160 class Job; |
158 class ProcTask; | 161 class ProcTask; |
159 class IPv6ProbeJob; | 162 class IPv6ProbeJob; |
160 class DnsTask; | 163 class DnsTask; |
161 class Request; | 164 class Request; |
162 typedef HostCache::Key Key; | 165 typedef HostCache::Key Key; |
163 typedef std::map<Key, Job*> JobMap; | 166 typedef std::map<Key, Job*> JobMap; |
164 typedef std::vector<Request*> RequestsList; | 167 typedef std::vector<Request*> RequestsList; |
165 | 168 |
(...skipping 11 matching lines...) Expand all Loading... | |
177 bool ResolveAsIP(const Key& key, | 180 bool ResolveAsIP(const Key& key, |
178 const RequestInfo& info, | 181 const RequestInfo& info, |
179 int* net_error, | 182 int* net_error, |
180 AddressList* addresses); | 183 AddressList* addresses); |
181 | 184 |
182 // If |key| is not found in cache returns false, otherwise returns | 185 // If |key| is not found in cache returns false, otherwise returns |
183 // true, sets |net_error| to the cached error code and fills |addresses| | 186 // true, sets |net_error| to the cached error code and fills |addresses| |
184 // if it is a positive entry. | 187 // if it is a positive entry. |
185 bool ServeFromCache(const Key& key, | 188 bool ServeFromCache(const Key& key, |
186 const RequestInfo& info, | 189 const RequestInfo& info, |
187 const BoundNetLog& request_net_log, | |
188 int* net_error, | 190 int* net_error, |
189 AddressList* addresses); | 191 AddressList* addresses); |
190 | 192 |
193 // If |key| is not found in the HOSTS file or no HOSTS file known, returns | |
194 // false, otherwise returns true and fills |addresses|. | |
195 bool ServeFromHosts(const Key& key, | |
196 const RequestInfo& info, | |
197 AddressList* addresses); | |
198 | |
191 // Notifies IPv6ProbeJob not to call back, and discard reference to the job. | 199 // Notifies IPv6ProbeJob not to call back, and discard reference to the job. |
192 void DiscardIPv6ProbeJob(); | 200 void DiscardIPv6ProbeJob(); |
193 | 201 |
194 // Callback from IPv6 probe activity. | 202 // Callback from IPv6 probe activity. |
195 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); | 203 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); |
196 | 204 |
197 // Returns the (hostname, address_family) key to use for |info|, choosing an | 205 // Returns the (hostname, address_family) key to use for |info|, choosing an |
198 // "effective" address family by inheriting the resolver's default address | 206 // "effective" address family by inheriting the resolver's default address |
199 // family when the request leaves it unspecified. | 207 // family when the request leaves it unspecified. |
200 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; | 208 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; |
201 | 209 |
202 // Records the result in cache if cache is present. | 210 // Records the result in cache if cache is present. |
203 void CacheResult(const Key& key, | 211 void CacheResult(const Key& key, |
204 int net_error, | 212 int net_error, |
205 const AddressList& addr_list, | 213 const AddressList& addr_list, |
206 base::TimeDelta ttl); | 214 base::TimeDelta ttl); |
207 | 215 |
208 // Removes |job| from |jobs_|, only if it exists. | 216 // Removes |job| from |jobs_|, only if it exists. |
209 void RemoveJob(Job* job); | 217 void RemoveJob(Job* job); |
210 | 218 |
211 // Aborts all in progress jobs and notifies their requests. | 219 // Aborts all in progress jobs and notifies their requests. |
212 // Might start new jobs. | 220 // Might start new jobs. |
213 void AbortAllInProgressJobs(); | 221 void AbortAllInProgressJobs(); |
214 | 222 |
223 // Attempts to serve each Job in |jobs_| from the HOSTS file. | |
224 void TryServingAllJobsFromHosts(); | |
225 | |
215 // NetworkChangeNotifier::IPAddressObserver: | 226 // NetworkChangeNotifier::IPAddressObserver: |
216 virtual void OnIPAddressChanged() OVERRIDE; | 227 virtual void OnIPAddressChanged() OVERRIDE; |
217 | 228 |
218 // NetworkChangeNotifier::DNSObserver: | 229 // NetworkChangeNotifier::DNSObserver: |
219 virtual void OnDNSChanged(unsigned detail) OVERRIDE; | 230 virtual void OnDNSChanged(unsigned detail) OVERRIDE; |
220 | 231 |
221 // DnsConfigService::Observer: | 232 // DnsConfigService::Observer: |
222 virtual void OnConfigChanged(const DnsConfig& dns_config) OVERRIDE; | 233 virtual void OnConfigChanged(const DnsConfig& dns_config) OVERRIDE; |
223 | 234 |
235 // True if have fully configured DNS client. | |
236 bool HaveDnsConfig() const; | |
237 | |
224 // Cache of host resolution results. | 238 // Cache of host resolution results. |
225 scoped_ptr<HostCache> cache_; | 239 scoped_ptr<HostCache> cache_; |
226 | 240 |
227 // Map from HostCache::Key to a Job. | 241 // Map from HostCache::Key to a Job. |
228 JobMap jobs_; | 242 JobMap jobs_; |
229 | 243 |
230 // Starts Jobs according to their priority and the configured limits. | 244 // Starts Jobs according to their priority and the configured limits. |
231 PrioritizedDispatcher dispatcher_; | 245 PrioritizedDispatcher dispatcher_; |
232 | 246 |
233 // Limit on the maximum number of jobs queued in |dispatcher_|. | 247 // Limit on the maximum number of jobs queued in |dispatcher_|. |
234 size_t max_queued_jobs_; | 248 size_t max_queued_jobs_; |
235 | 249 |
236 // Parameters for ProcTask. | 250 // Parameters for ProcTask. |
237 ProcTaskParams proc_params_; | 251 ProcTaskParams proc_params_; |
238 | 252 |
239 scoped_ptr<DnsTransactionFactory> dns_transaction_factory_; | |
240 | |
241 // Address family to use when the request doesn't specify one. | 253 // Address family to use when the request doesn't specify one. |
242 AddressFamily default_address_family_; | 254 AddressFamily default_address_family_; |
243 | 255 |
256 scoped_ptr<DnsClient> dns_client_; | |
244 scoped_ptr<DnsConfigService> dns_config_service_; | 257 scoped_ptr<DnsConfigService> dns_config_service_; |
245 | 258 |
246 // Indicate if probing is done after each network change event to set address | 259 // Indicate if probing is done after each network change event to set address |
247 // family. | 260 // family. |
248 // When false, explicit setting of address family is used. | 261 // When false, explicit setting of address family is used. |
249 bool ipv6_probe_monitoring_; | 262 bool ipv6_probe_monitoring_; |
250 | 263 |
251 // The last un-cancelled IPv6ProbeJob (if any). | 264 // The last un-cancelled IPv6ProbeJob (if any). |
252 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; | 265 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; |
253 | 266 |
254 // Any resolver flags that should be added to a request by default. | 267 // Any resolver flags that should be added to a request by default. |
255 HostResolverFlags additional_resolver_flags_; | 268 HostResolverFlags additional_resolver_flags_; |
256 | 269 |
257 NetLog* net_log_; | 270 NetLog* net_log_; |
258 | 271 |
259 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); | 272 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); |
260 }; | 273 }; |
261 | 274 |
262 } // namespace net | 275 } // namespace net |
263 | 276 |
264 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ | 277 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ |
OLD | NEW |