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

Side by Side Diff: net/proxy/mock_proxy_resolver.cc

Issue 1439053002: Change ProxyResolver::GetProxyForURL() to take a scoped_ptr<Request>* rather than a RequestHandle* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ToT Created 4 years, 11 months 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
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/proxy/mock_proxy_resolver.h" 5 #include "net/proxy/mock_proxy_resolver.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 11
12 namespace net { 12 namespace net {
13 13
14 MockAsyncProxyResolver::Request::Request(MockAsyncProxyResolver* resolver, 14 MockAsyncProxyResolver::RequestImpl::RequestImpl(base::WeakPtr<Job> job)
15 const GURL& url, 15 : job_(job) {
16 ProxyInfo* results, 16 DCHECK(job_);
17 const CompletionCallback& callback) 17 }
18
19 MockAsyncProxyResolver::RequestImpl::~RequestImpl() {
20 if (job_) {
21 job_->Resolver()->AddCancelledJob(job_);
22 }
23 }
24
25 LoadState MockAsyncProxyResolver::RequestImpl::GetLoadState() {
26 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
27 }
28
29 MockAsyncProxyResolver::Job::Job(MockAsyncProxyResolver* resolver,
30 const GURL& url,
31 ProxyInfo* results,
32 const CompletionCallback& callback)
18 : resolver_(resolver), 33 : resolver_(resolver),
19 url_(url), 34 url_(url),
20 results_(results), 35 results_(results),
21 callback_(callback), 36 callback_(callback),
22 origin_loop_(base::MessageLoop::current()) { 37 origin_loop_(base::MessageLoop::current()) {}
23 }
24 38
25 void MockAsyncProxyResolver::Request::CompleteNow(int rv) { 39 MockAsyncProxyResolver::Job::~Job() {}
40
41 void MockAsyncProxyResolver::Job::CompleteNow(int rv) {
26 CompletionCallback callback = callback_; 42 CompletionCallback callback = callback_;
27 43
28 // May delete |this|. 44 // May delete |this|.
29 resolver_->RemovePendingRequest(this); 45 resolver_->RemovePendingJob(AsWeakPtr());
30 46
31 callback.Run(rv); 47 callback.Run(rv);
32 } 48 }
33 49
34 MockAsyncProxyResolver::Request::~Request() {}
35
36 MockAsyncProxyResolver::~MockAsyncProxyResolver() {} 50 MockAsyncProxyResolver::~MockAsyncProxyResolver() {}
37 51
38 int MockAsyncProxyResolver::GetProxyForURL(const GURL& url, 52 int MockAsyncProxyResolver::GetProxyForURL(const GURL& url,
39 ProxyInfo* results, 53 ProxyInfo* results,
40 const CompletionCallback& callback, 54 const CompletionCallback& callback,
41 RequestHandle* request_handle, 55 scoped_ptr<Request>* request,
42 const BoundNetLog& /*net_log*/) { 56 const BoundNetLog& /*net_log*/) {
43 scoped_refptr<Request> request = new Request(this, url, results, callback); 57 scoped_refptr<Job> job = new Job(this, url, results, callback);
44 pending_requests_.push_back(request);
45 58
46 if (request_handle) 59 if (request)
47 *request_handle = reinterpret_cast<RequestHandle>(request.get()); 60 request->reset(new RequestImpl(job->AsWeakPtr()));
61
62 pending_jobs_.push_back(job);
48 63
49 // Test code completes the request by calling request->CompleteNow(). 64 // Test code completes the request by calling request->CompleteNow().
50 return ERR_IO_PENDING; 65 return ERR_IO_PENDING;
51 } 66 }
52 67
53 void MockAsyncProxyResolver::CancelRequest(RequestHandle request_handle) { 68 void MockAsyncProxyResolver::AddCancelledJob(const base::WeakPtr<Job> job) {
54 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle); 69 DCHECK(job);
55 cancelled_requests_.push_back(request); 70 JobsList::iterator it =
56 RemovePendingRequest(request.get()); 71 std::find(pending_jobs_.begin(), pending_jobs_.end(), job.get());
72 DCHECK(it != pending_jobs_.end());
73 cancelled_jobs_.push_back(*it);
74 pending_jobs_.erase(it);
57 } 75 }
58 76
59 LoadState MockAsyncProxyResolver::GetLoadState( 77 void MockAsyncProxyResolver::RemovePendingJob(const base::WeakPtr<Job> job) {
60 RequestHandle request_handle) const { 78 DCHECK(job);
61 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; 79 JobsList::iterator it =
62 } 80 std::find(pending_jobs_.begin(), pending_jobs_.end(), job.get());
63 81 DCHECK(it != pending_jobs_.end());
64 void MockAsyncProxyResolver::RemovePendingRequest(Request* request) { 82 pending_jobs_.erase(it);
65 RequestsList::iterator it = std::find(
66 pending_requests_.begin(), pending_requests_.end(), request);
67 DCHECK(it != pending_requests_.end());
68 pending_requests_.erase(it);
69 } 83 }
70 84
71 MockAsyncProxyResolver::MockAsyncProxyResolver() { 85 MockAsyncProxyResolver::MockAsyncProxyResolver() {
72 } 86 }
73 87
74 MockAsyncProxyResolverFactory::Request::Request( 88 MockAsyncProxyResolverFactory::Request::Request(
75 MockAsyncProxyResolverFactory* factory, 89 MockAsyncProxyResolverFactory* factory,
76 const scoped_refptr<ProxyResolverScriptData>& script_data, 90 const scoped_refptr<ProxyResolverScriptData>& script_data,
77 scoped_ptr<ProxyResolver>* resolver, 91 scoped_ptr<ProxyResolver>* resolver,
78 const CompletionCallback& callback) 92 const CompletionCallback& callback)
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 172 }
159 } 173 }
160 174
161 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl) 175 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl)
162 : impl_(impl) { 176 : impl_(impl) {
163 } 177 }
164 178
165 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url, 179 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url,
166 ProxyInfo* results, 180 ProxyInfo* results,
167 const CompletionCallback& callback, 181 const CompletionCallback& callback,
168 RequestHandle* request, 182 scoped_ptr<Request>* request,
169 const BoundNetLog& net_log) { 183 const BoundNetLog& net_log) {
170 return impl_->GetProxyForURL(query_url, results, callback, request, net_log); 184 return impl_->GetProxyForURL(query_url, results, callback, request, net_log);
171 } 185 }
172 186
173 void ForwardingProxyResolver::CancelRequest(RequestHandle request) {
174 impl_->CancelRequest(request);
175 }
176
177 LoadState ForwardingProxyResolver::GetLoadState(RequestHandle request) const {
178 return impl_->GetLoadState(request);
179 }
180
181 } // namespace net 187 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698