Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(829)

Side by Side Diff: net/base/mock_host_resolver.cc

Issue 8533011: Remove unused HostResolver::Observer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove id from AsyncHostResolver; add double-cancelation check; comments' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/mock_host_resolver.h ('k') | net/base/single_request_host_resolver_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
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)); 116 } else {
117 NOTREACHED() << "CancelRequest must NOT be called after request is "
118 "complete or canceled.";
123 } 119 }
124 } 120 }
125 121
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() { 122 HostCache* MockHostResolverBase::GetHostCache() {
137 return cache_.get(); 123 return cache_.get();
138 } 124 }
139 125
140 // start id from 1 to distinguish from NULL RequestHandle 126 // start id from 1 to distinguish from NULL RequestHandle
141 MockHostResolverBase::MockHostResolverBase(bool use_caching) 127 MockHostResolverBase::MockHostResolverBase(bool use_caching)
142 : synchronous_mode_(false), next_request_id_(1) { 128 : synchronous_mode_(false), next_request_id_(1) {
143 rules_ = CreateCatchAllHostResolverProc(); 129 rules_ = CreateCatchAllHostResolverProc();
144 proc_ = rules_; 130 proc_ = rules_;
145 131
146 if (use_caching) { 132 if (use_caching) {
147 cache_.reset(new HostCache( 133 cache_.reset(new HostCache(
148 100, // max entries. 134 100, // max entries.
149 base::TimeDelta::FromMinutes(1), 135 base::TimeDelta::FromMinutes(1),
150 base::TimeDelta::FromSeconds(0))); 136 base::TimeDelta::FromSeconds(0)));
151 } 137 }
152 STLDeleteValues(&requests_);
153 } 138 }
154 139
155 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, 140 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info,
156 AddressList* addresses) { 141 AddressList* addresses) {
157 IPAddressNumber ip; 142 IPAddressNumber ip;
158 if (ParseIPLiteralToNumber(info.hostname(), &ip)) { 143 if (ParseIPLiteralToNumber(info.hostname(), &ip)) {
159 *addresses = AddressList::CreateFromIPAddressWithCname( 144 *addresses = AddressList::CreateFromIPAddressWithCname(
160 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME); 145 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME);
161 return OK; 146 return OK;
162 } 147 }
(...skipping 22 matching lines...) Expand all
185 &addr, 170 &addr,
186 NULL); 171 NULL);
187 if (cache_.get()) { 172 if (cache_.get()) {
188 HostCache::Key key(info.hostname(), 173 HostCache::Key key(info.hostname(),
189 info.address_family(), 174 info.address_family(),
190 info.host_resolver_flags()); 175 info.host_resolver_flags());
191 cache_->Set(key, rv, addr, base::TimeTicks::Now()); 176 cache_->Set(key, rv, addr, base::TimeTicks::Now());
192 } 177 }
193 if (rv == OK) 178 if (rv == OK)
194 *addresses = CreateAddressListUsingPort(addr, info.port()); 179 *addresses = CreateAddressListUsingPort(addr, info.port());
195 FOR_EACH_OBSERVER(Observer, observers_,
196 OnFinishResolutionWithStatus(id, rv == OK, info));
197 return rv; 180 return rv;
198 } 181 }
199 182
200 void MockHostResolverBase::ResolveNow(size_t id) { 183 void MockHostResolverBase::ResolveNow(size_t id) {
201 RequestMap::iterator it = requests_.find(id); 184 RequestMap::iterator it = requests_.find(id);
202 if (it == requests_.end()) 185 if (it == requests_.end())
203 return; // was canceled 186 return; // was canceled
204 187
205 scoped_ptr<Request> req(it->second); 188 scoped_ptr<Request> req(it->second);
206 requests_.erase(it); 189 requests_.erase(it);
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 CHECK_EQ(old_proc, current_proc_); 391 CHECK_EQ(old_proc, current_proc_);
409 } 392 }
410 393
411 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { 394 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) {
412 current_proc_ = proc; 395 current_proc_ = proc;
413 previous_proc_ = HostResolverProc::SetDefault(current_proc_); 396 previous_proc_ = HostResolverProc::SetDefault(current_proc_);
414 current_proc_->SetLastProc(previous_proc_); 397 current_proc_->SetLastProc(previous_proc_);
415 } 398 }
416 399
417 } // namespace net 400 } // namespace net
OLDNEW
« no previous file with comments | « net/base/mock_host_resolver.h ('k') | net/base/single_request_host_resolver_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698