| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return rv; | 77 return rv; |
| 78 } | 78 } |
| 79 if (synchronous_mode_) { | 79 if (synchronous_mode_) { |
| 80 return ResolveProc(id, info, addresses); | 80 return ResolveProc(id, info, addresses); |
| 81 } | 81 } |
| 82 // Store the request for asynchronous resolution | 82 // Store the request for asynchronous resolution |
| 83 Request* req = new Request(info, addresses, callback); | 83 Request* req = new Request(info, addresses, callback); |
| 84 requests_[id] = req; | 84 requests_[id] = req; |
| 85 if (handle) | 85 if (handle) |
| 86 *handle = reinterpret_cast<RequestHandle>(id); | 86 *handle = reinterpret_cast<RequestHandle>(id); |
| 87 MessageLoop::current()->PostTask(FROM_HERE, | 87 |
| 88 base::Bind(&MockHostResolverBase::ResolveNow, | 88 if (resolve_automatically_) { |
| 89 AsWeakPtr(), | 89 MessageLoop::current()->PostTask( |
| 90 id)); | 90 FROM_HERE, |
| 91 base::Bind(&MockHostResolverBase::ResolveNow, AsWeakPtr(), id)); |
| 92 } |
| 93 |
| 91 return ERR_IO_PENDING; | 94 return ERR_IO_PENDING; |
| 92 } | 95 } |
| 93 | 96 |
| 94 int MockHostResolverBase::ResolveFromCache(const RequestInfo& info, | 97 int MockHostResolverBase::ResolveFromCache(const RequestInfo& info, |
| 95 AddressList* addresses, | 98 AddressList* addresses, |
| 96 const BoundNetLog& net_log) { | 99 const BoundNetLog& net_log) { |
| 97 DCHECK(CalledOnValidThread()); | 100 DCHECK(CalledOnValidThread()); |
| 98 next_request_id_++; | 101 next_request_id_++; |
| 99 int rv = ResolveFromIPLiteralOrCache(info, addresses); | 102 int rv = ResolveFromIPLiteralOrCache(info, addresses); |
| 100 return rv; | 103 return rv; |
| 101 } | 104 } |
| 102 | 105 |
| 103 void MockHostResolverBase::CancelRequest(RequestHandle handle) { | 106 void MockHostResolverBase::CancelRequest(RequestHandle handle) { |
| 104 DCHECK(CalledOnValidThread()); | 107 DCHECK(CalledOnValidThread()); |
| 105 size_t id = reinterpret_cast<size_t>(handle); | 108 size_t id = reinterpret_cast<size_t>(handle); |
| 106 RequestMap::iterator it = requests_.find(id); | 109 RequestMap::iterator it = requests_.find(id); |
| 107 if (it != requests_.end()) { | 110 if (it != requests_.end()) { |
| 108 scoped_ptr<Request> req(it->second); | 111 scoped_ptr<Request> req(it->second); |
| 109 requests_.erase(it); | 112 requests_.erase(it); |
| 110 } else { | 113 } else { |
| 111 NOTREACHED() << "CancelRequest must NOT be called after request is " | 114 NOTREACHED() << "CancelRequest must NOT be called after request is " |
| 112 "complete or canceled."; | 115 "complete or canceled."; |
| 113 } | 116 } |
| 114 } | 117 } |
| 115 | 118 |
| 116 HostCache* MockHostResolverBase::GetHostCache() { | 119 HostCache* MockHostResolverBase::GetHostCache() { |
| 117 return cache_.get(); | 120 return cache_.get(); |
| 118 } | 121 } |
| 119 | 122 |
| 123 void MockHostResolverBase::ResolveAllPending() { |
| 124 DCHECK(CalledOnValidThread()); |
| 125 DCHECK(!resolve_automatically_); |
| 126 for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
| 127 MessageLoop::current()->PostTask( |
| 128 FROM_HERE, |
| 129 base::Bind(&MockHostResolverBase::ResolveNow, AsWeakPtr(), i->first)); |
| 130 } |
| 131 } |
| 132 |
| 120 // start id from 1 to distinguish from NULL RequestHandle | 133 // start id from 1 to distinguish from NULL RequestHandle |
| 121 MockHostResolverBase::MockHostResolverBase(bool use_caching) | 134 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
| 122 : synchronous_mode_(false), next_request_id_(1) { | 135 : synchronous_mode_(false), |
| 136 resolve_automatically_(true), |
| 137 next_request_id_(1) { |
| 123 rules_ = CreateCatchAllHostResolverProc(); | 138 rules_ = CreateCatchAllHostResolverProc(); |
| 124 | 139 |
| 125 if (use_caching) { | 140 if (use_caching) { |
| 126 cache_.reset(new HostCache(kMaxCacheEntries)); | 141 cache_.reset(new HostCache(kMaxCacheEntries)); |
| 127 } | 142 } |
| 128 } | 143 } |
| 129 | 144 |
| 130 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, | 145 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, |
| 131 AddressList* addresses) { | 146 AddressList* addresses) { |
| 132 IPAddressNumber ip; | 147 IPAddressNumber ip; |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 CHECK_EQ(old_proc, current_proc_); | 400 CHECK_EQ(old_proc, current_proc_); |
| 386 } | 401 } |
| 387 | 402 |
| 388 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 403 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 389 current_proc_ = proc; | 404 current_proc_ = proc; |
| 390 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 405 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 391 current_proc_->SetLastProc(previous_proc_); | 406 current_proc_->SetLastProc(previous_proc_); |
| 392 } | 407 } |
| 393 | 408 |
| 394 } // namespace net | 409 } // namespace net |
| OLD | NEW |