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 #include "net/base/mock_host_resolver.h" | 5 #include "net/base/mock_host_resolver.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 MockHostResolverBase::~MockHostResolverBase() { | 64 MockHostResolverBase::~MockHostResolverBase() { |
65 STLDeleteValues(&requests_); | 65 STLDeleteValues(&requests_); |
66 } | 66 } |
67 | 67 |
68 int MockHostResolverBase::Resolve(const RequestInfo& info, | 68 int MockHostResolverBase::Resolve(const RequestInfo& info, |
69 AddressList* addresses, | 69 AddressList* addresses, |
70 const CompletionCallback& callback, | 70 const CompletionCallback& callback, |
71 RequestHandle* handle, | 71 RequestHandle* handle, |
72 const BoundNetLog& net_log) { | 72 const BoundNetLog& net_log) { |
73 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
| 74 num_resolve_++; |
74 size_t id = next_request_id_++; | 75 size_t id = next_request_id_++; |
75 int rv = ResolveFromIPLiteralOrCache(info, addresses); | 76 int rv = ResolveFromIPLiteralOrCache(info, addresses); |
76 if (rv != ERR_DNS_CACHE_MISS) { | 77 if (rv != ERR_DNS_CACHE_MISS) { |
77 return rv; | 78 return rv; |
78 } | 79 } |
79 if (synchronous_mode_) { | 80 if (synchronous_mode_) { |
80 return ResolveProc(id, info, addresses); | 81 return ResolveProc(id, info, addresses); |
81 } | 82 } |
82 // Store the request for asynchronous resolution | 83 // Store the request for asynchronous resolution |
83 Request* req = new Request(info, addresses, callback); | 84 Request* req = new Request(info, addresses, callback); |
84 requests_[id] = req; | 85 requests_[id] = req; |
85 if (handle) | 86 if (handle) |
86 *handle = reinterpret_cast<RequestHandle>(id); | 87 *handle = reinterpret_cast<RequestHandle>(id); |
87 | 88 |
88 if (!ondemand_mode_) { | 89 if (!ondemand_mode_) { |
89 MessageLoop::current()->PostTask( | 90 MessageLoop::current()->PostTask( |
90 FROM_HERE, | 91 FROM_HERE, |
91 base::Bind(&MockHostResolverBase::ResolveNow, AsWeakPtr(), id)); | 92 base::Bind(&MockHostResolverBase::ResolveNow, AsWeakPtr(), id)); |
92 } | 93 } |
93 | 94 |
94 return ERR_IO_PENDING; | 95 return ERR_IO_PENDING; |
95 } | 96 } |
96 | 97 |
97 int MockHostResolverBase::ResolveFromCache(const RequestInfo& info, | 98 int MockHostResolverBase::ResolveFromCache(const RequestInfo& info, |
98 AddressList* addresses, | 99 AddressList* addresses, |
99 const BoundNetLog& net_log) { | 100 const BoundNetLog& net_log) { |
| 101 num_resolve_from_cache_++; |
100 DCHECK(CalledOnValidThread()); | 102 DCHECK(CalledOnValidThread()); |
101 next_request_id_++; | 103 next_request_id_++; |
102 int rv = ResolveFromIPLiteralOrCache(info, addresses); | 104 int rv = ResolveFromIPLiteralOrCache(info, addresses); |
103 return rv; | 105 return rv; |
104 } | 106 } |
105 | 107 |
106 void MockHostResolverBase::CancelRequest(RequestHandle handle) { | 108 void MockHostResolverBase::CancelRequest(RequestHandle handle) { |
107 DCHECK(CalledOnValidThread()); | 109 DCHECK(CalledOnValidThread()); |
108 size_t id = reinterpret_cast<size_t>(handle); | 110 size_t id = reinterpret_cast<size_t>(handle); |
109 RequestMap::iterator it = requests_.find(id); | 111 RequestMap::iterator it = requests_.find(id); |
(...skipping 17 matching lines...) Expand all Loading... |
127 MessageLoop::current()->PostTask( | 129 MessageLoop::current()->PostTask( |
128 FROM_HERE, | 130 FROM_HERE, |
129 base::Bind(&MockHostResolverBase::ResolveNow, AsWeakPtr(), i->first)); | 131 base::Bind(&MockHostResolverBase::ResolveNow, AsWeakPtr(), i->first)); |
130 } | 132 } |
131 } | 133 } |
132 | 134 |
133 // start id from 1 to distinguish from NULL RequestHandle | 135 // start id from 1 to distinguish from NULL RequestHandle |
134 MockHostResolverBase::MockHostResolverBase(bool use_caching) | 136 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
135 : synchronous_mode_(false), | 137 : synchronous_mode_(false), |
136 ondemand_mode_(false), | 138 ondemand_mode_(false), |
137 next_request_id_(1) { | 139 next_request_id_(1), |
| 140 num_resolve_(0), |
| 141 num_resolve_from_cache_(0) { |
138 rules_ = CreateCatchAllHostResolverProc(); | 142 rules_ = CreateCatchAllHostResolverProc(); |
139 | 143 |
140 if (use_caching) { | 144 if (use_caching) { |
141 cache_.reset(new HostCache(kMaxCacheEntries)); | 145 cache_.reset(new HostCache(kMaxCacheEntries)); |
142 } | 146 } |
143 } | 147 } |
144 | 148 |
145 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, | 149 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, |
146 AddressList* addresses) { | 150 AddressList* addresses) { |
147 IPAddressNumber ip; | 151 IPAddressNumber ip; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 302 |
299 void RuleBasedHostResolverProc::AddSimulatedFailure( | 303 void RuleBasedHostResolverProc::AddSimulatedFailure( |
300 const std::string& host_pattern) { | 304 const std::string& host_pattern) { |
301 HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY | | 305 HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY | |
302 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; | 306 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
303 Rule rule(Rule::kResolverTypeFail, host_pattern, ADDRESS_FAMILY_UNSPECIFIED, | 307 Rule rule(Rule::kResolverTypeFail, host_pattern, ADDRESS_FAMILY_UNSPECIFIED, |
304 flags, "", "", 0); | 308 flags, "", "", 0); |
305 rules_.push_back(rule); | 309 rules_.push_back(rule); |
306 } | 310 } |
307 | 311 |
| 312 void RuleBasedHostResolverProc::ClearRules() { |
| 313 rules_.clear(); |
| 314 } |
| 315 |
308 int RuleBasedHostResolverProc::Resolve(const std::string& host, | 316 int RuleBasedHostResolverProc::Resolve(const std::string& host, |
309 AddressFamily address_family, | 317 AddressFamily address_family, |
310 HostResolverFlags host_resolver_flags, | 318 HostResolverFlags host_resolver_flags, |
311 AddressList* addrlist, | 319 AddressList* addrlist, |
312 int* os_error) { | 320 int* os_error) { |
313 RuleList::iterator r; | 321 RuleList::iterator r; |
314 for (r = rules_.begin(); r != rules_.end(); ++r) { | 322 for (r = rules_.begin(); r != rules_.end(); ++r) { |
315 bool matches_address_family = | 323 bool matches_address_family = |
316 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || | 324 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || |
317 r->address_family == address_family; | 325 r->address_family == address_family; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 CHECK_EQ(old_proc, current_proc_); | 408 CHECK_EQ(old_proc, current_proc_); |
401 } | 409 } |
402 | 410 |
403 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 411 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
404 current_proc_ = proc; | 412 current_proc_ = proc; |
405 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 413 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
406 current_proc_->SetLastProc(previous_proc_); | 414 current_proc_->SetLastProc(previous_proc_); |
407 } | 415 } |
408 | 416 |
409 } // namespace net | 417 } // namespace net |
OLD | NEW |