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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 STLDeleteValues(&requests_); | 71 STLDeleteValues(&requests_); |
| 72 } | 72 } |
| 73 | 73 |
| 74 int MockHostResolverBase::Resolve(const RequestInfo& info, | 74 int MockHostResolverBase::Resolve(const RequestInfo& info, |
| 75 AddressList* addresses, | 75 AddressList* addresses, |
| 76 OldCompletionCallback* callback, | 76 OldCompletionCallback* callback, |
| 77 RequestHandle* handle, | 77 RequestHandle* handle, |
| 78 const BoundNetLog& net_log) { | 78 const BoundNetLog& net_log) { |
| 79 DCHECK(CalledOnValidThread()); | 79 DCHECK(CalledOnValidThread()); |
| 80 size_t id = next_request_id_++; | 80 size_t id = next_request_id_++; |
| 81 FOR_EACH_OBSERVER(Observer, observers_, OnStartResolution(id, info)); | |
| 82 int rv = ResolveFromIPLiteralOrCache(info, addresses); | 81 int rv = ResolveFromIPLiteralOrCache(info, addresses); |
| 83 if (rv != ERR_DNS_CACHE_MISS) { | 82 if (rv != ERR_DNS_CACHE_MISS) { |
| 84 FOR_EACH_OBSERVER(Observer, observers_, | |
| 85 OnFinishResolutionWithStatus(id, rv == OK, info)); | |
| 86 return rv; | 83 return rv; |
| 87 } | 84 } |
| 88 if (synchronous_mode_) { | 85 if (synchronous_mode_) { |
| 89 return ResolveProc(id, info, addresses); | 86 return ResolveProc(id, info, addresses); |
| 90 } | 87 } |
| 91 // Store the request for asynchronous resolution | 88 // Store the request for asynchronous resolution |
| 92 Request* req = new Request(info, addresses, callback); | 89 Request* req = new Request(info, addresses, callback); |
| 93 requests_[id] = req; | 90 requests_[id] = req; |
| 94 if (handle) | 91 if (handle) |
| 95 *handle = reinterpret_cast<RequestHandle>(id); | 92 *handle = reinterpret_cast<RequestHandle>(id); |
|
cbentzel
2011/11/15 10:49:01
Can you remove next_request_id_ from the mock by h
| |
| 96 MessageLoop::current()->PostTask(FROM_HERE, | 93 MessageLoop::current()->PostTask(FROM_HERE, |
| 97 base::Bind(&MockHostResolverBase::ResolveNow, | 94 base::Bind(&MockHostResolverBase::ResolveNow, |
| 98 AsWeakPtr(), | 95 AsWeakPtr(), |
| 99 id)); | 96 id)); |
| 100 return ERR_IO_PENDING; | 97 return ERR_IO_PENDING; |
| 101 } | 98 } |
| 102 | 99 |
| 103 int MockHostResolverBase::ResolveFromCache(const RequestInfo& info, | 100 int MockHostResolverBase::ResolveFromCache(const RequestInfo& info, |
| 104 AddressList* addresses, | 101 AddressList* addresses, |
| 105 const BoundNetLog& net_log) { | 102 const BoundNetLog& net_log) { |
| 106 DCHECK(CalledOnValidThread()); | 103 DCHECK(CalledOnValidThread()); |
| 107 size_t id = next_request_id_++; | 104 next_request_id_++; |
| 108 FOR_EACH_OBSERVER(Observer, observers_, OnStartResolution(id, info)); | |
| 109 int rv = ResolveFromIPLiteralOrCache(info, addresses); | 105 int rv = ResolveFromIPLiteralOrCache(info, addresses); |
| 110 FOR_EACH_OBSERVER(Observer, observers_, | |
| 111 OnFinishResolutionWithStatus(id, rv == OK, info)); | |
| 112 return rv; | 106 return rv; |
| 113 } | 107 } |
| 114 | 108 |
| 115 void MockHostResolverBase::CancelRequest(RequestHandle handle) { | 109 void MockHostResolverBase::CancelRequest(RequestHandle handle) { |
| 116 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
| 117 size_t id = reinterpret_cast<size_t>(handle); | 111 size_t id = reinterpret_cast<size_t>(handle); |
| 118 RequestMap::iterator it = requests_.find(id); | 112 RequestMap::iterator it = requests_.find(id); |
| 119 if (it != requests_.end()) { | 113 if (it != requests_.end()) { |
| 120 scoped_ptr<Request> req(it->second); | 114 scoped_ptr<Request> req(it->second); |
| 121 requests_.erase(it); | 115 requests_.erase(it); |
| 122 FOR_EACH_OBSERVER(Observer, observers_, OnCancelResolution(id, req->info)); | |
| 123 } | 116 } |
| 124 } | 117 } |
| 125 | 118 |
| 126 void MockHostResolverBase::AddObserver(Observer* observer) { | |
| 127 DCHECK(CalledOnValidThread()); | |
| 128 observers_.AddObserver(observer); | |
| 129 } | |
| 130 | |
| 131 void MockHostResolverBase::RemoveObserver(Observer* observer) { | |
| 132 DCHECK(CalledOnValidThread()); | |
| 133 observers_.RemoveObserver(observer); | |
| 134 } | |
| 135 | |
| 136 HostCache* MockHostResolverBase::GetHostCache() { | 119 HostCache* MockHostResolverBase::GetHostCache() { |
| 137 return cache_.get(); | 120 return cache_.get(); |
| 138 } | 121 } |
| 139 | 122 |
| 140 // start id from 1 to distinguish from NULL RequestHandle | 123 // start id from 1 to distinguish from NULL RequestHandle |
| 141 MockHostResolverBase::MockHostResolverBase(bool use_caching) | 124 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
| 142 : synchronous_mode_(false), next_request_id_(1) { | 125 : synchronous_mode_(false), next_request_id_(1) { |
| 143 rules_ = CreateCatchAllHostResolverProc(); | 126 rules_ = CreateCatchAllHostResolverProc(); |
| 144 proc_ = rules_; | 127 proc_ = rules_; |
| 145 | 128 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 &addr, | 168 &addr, |
| 186 NULL); | 169 NULL); |
| 187 if (cache_.get()) { | 170 if (cache_.get()) { |
| 188 HostCache::Key key(info.hostname(), | 171 HostCache::Key key(info.hostname(), |
| 189 info.address_family(), | 172 info.address_family(), |
| 190 info.host_resolver_flags()); | 173 info.host_resolver_flags()); |
| 191 cache_->Set(key, rv, addr, base::TimeTicks::Now()); | 174 cache_->Set(key, rv, addr, base::TimeTicks::Now()); |
| 192 } | 175 } |
| 193 if (rv == OK) | 176 if (rv == OK) |
| 194 *addresses = CreateAddressListUsingPort(addr, info.port()); | 177 *addresses = CreateAddressListUsingPort(addr, info.port()); |
| 195 FOR_EACH_OBSERVER(Observer, observers_, | |
| 196 OnFinishResolutionWithStatus(id, rv == OK, info)); | |
| 197 return rv; | 178 return rv; |
| 198 } | 179 } |
| 199 | 180 |
| 200 void MockHostResolverBase::ResolveNow(size_t id) { | 181 void MockHostResolverBase::ResolveNow(size_t id) { |
| 201 RequestMap::iterator it = requests_.find(id); | 182 RequestMap::iterator it = requests_.find(id); |
| 202 if (it == requests_.end()) | 183 if (it == requests_.end()) |
| 203 return; // was canceled | 184 return; // was canceled |
| 204 | 185 |
| 205 scoped_ptr<Request> req(it->second); | 186 scoped_ptr<Request> req(it->second); |
| 206 requests_.erase(it); | 187 requests_.erase(it); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 CHECK_EQ(old_proc, current_proc_); | 389 CHECK_EQ(old_proc, current_proc_); |
| 409 } | 390 } |
| 410 | 391 |
| 411 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 392 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 412 current_proc_ = proc; | 393 current_proc_ = proc; |
| 413 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 394 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 414 current_proc_->SetLastProc(previous_proc_); | 395 current_proc_->SetLastProc(previous_proc_); |
| 415 } | 396 } |
| 416 | 397 |
| 417 } // namespace net | 398 } // namespace net |
| OLD | NEW |