Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 return cache_.get(); | 123 return cache_.get(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 // start id from 1 to distinguish from NULL RequestHandle | 126 // start id from 1 to distinguish from NULL RequestHandle |
| 127 MockHostResolverBase::MockHostResolverBase(bool use_caching) | 127 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
| 128 : synchronous_mode_(false), next_request_id_(1) { | 128 : synchronous_mode_(false), next_request_id_(1) { |
| 129 rules_ = CreateCatchAllHostResolverProc(); | 129 rules_ = CreateCatchAllHostResolverProc(); |
| 130 proc_ = rules_; | 130 proc_ = rules_; |
| 131 | 131 |
| 132 if (use_caching) { | 132 if (use_caching) { |
| 133 cache_.reset(new HostCache( | 133 cache_.reset(new HostCache(100 /* max entries */)); |
|
mmenke
2012/01/19 22:12:43
nit: Suggest you put both constants (this and TTL
| |
| 134 100, // max entries. | |
| 135 base::TimeDelta::FromMinutes(1), | |
| 136 base::TimeDelta::FromSeconds(0))); | |
| 137 } | 134 } |
| 138 } | 135 } |
| 139 | 136 |
| 140 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, | 137 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, |
| 141 AddressList* addresses) { | 138 AddressList* addresses) { |
| 142 IPAddressNumber ip; | 139 IPAddressNumber ip; |
| 143 if (ParseIPLiteralToNumber(info.hostname(), &ip)) { | 140 if (ParseIPLiteralToNumber(info.hostname(), &ip)) { |
| 144 *addresses = AddressList::CreateFromIPAddressWithCname( | 141 *addresses = AddressList::CreateFromIPAddressWithCname( |
| 145 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME); | 142 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME); |
| 146 return OK; | 143 return OK; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 162 | 159 |
| 163 int MockHostResolverBase::ResolveProc(size_t id, | 160 int MockHostResolverBase::ResolveProc(size_t id, |
| 164 const RequestInfo& info, | 161 const RequestInfo& info, |
| 165 AddressList* addresses) { | 162 AddressList* addresses) { |
| 166 AddressList addr; | 163 AddressList addr; |
| 167 int rv = proc_->Resolve(info.hostname(), | 164 int rv = proc_->Resolve(info.hostname(), |
| 168 info.address_family(), | 165 info.address_family(), |
| 169 info.host_resolver_flags(), | 166 info.host_resolver_flags(), |
| 170 &addr, | 167 &addr, |
| 171 NULL); | 168 NULL); |
| 169 if (rv != OK) | |
| 170 return rv; | |
| 171 | |
| 172 if (cache_.get()) { | 172 if (cache_.get()) { |
| 173 HostCache::Key key(info.hostname(), | 173 HostCache::Key key(info.hostname(), |
| 174 info.address_family(), | 174 info.address_family(), |
| 175 info.host_resolver_flags()); | 175 info.host_resolver_flags()); |
| 176 cache_->Set(key, rv, addr, base::TimeTicks::Now()); | 176 cache_->Set(key, rv, addr, |
| 177 base::TimeDelta::FromMinutes(1), | |
| 178 base::TimeTicks::Now()); | |
| 177 } | 179 } |
| 178 if (rv == OK) | 180 *addresses = CreateAddressListUsingPort(addr, info.port()); |
| 179 *addresses = CreateAddressListUsingPort(addr, info.port()); | |
| 180 return rv; | 181 return rv; |
| 181 } | 182 } |
| 182 | 183 |
| 183 void MockHostResolverBase::ResolveNow(size_t id) { | 184 void MockHostResolverBase::ResolveNow(size_t id) { |
| 184 RequestMap::iterator it = requests_.find(id); | 185 RequestMap::iterator it = requests_.find(id); |
| 185 if (it == requests_.end()) | 186 if (it == requests_.end()) |
| 186 return; // was canceled | 187 return; // was canceled |
| 187 | 188 |
| 188 scoped_ptr<Request> req(it->second); | 189 scoped_ptr<Request> req(it->second); |
| 189 requests_.erase(it); | 190 requests_.erase(it); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 CHECK_EQ(old_proc, current_proc_); | 392 CHECK_EQ(old_proc, current_proc_); |
| 392 } | 393 } |
| 393 | 394 |
| 394 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 395 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 395 current_proc_ = proc; | 396 current_proc_ = proc; |
| 396 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 397 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 397 current_proc_->SetLastProc(previous_proc_); | 398 current_proc_->SetLastProc(previous_proc_); |
| 398 } | 399 } |
| 399 | 400 |
| 400 } // namespace net | 401 } // namespace net |
| OLD | NEW |