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/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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 MockAsyncProxyResolver::Request::Request(MockAsyncProxyResolver* resolver, | 14 MockAsyncProxyResolver::RequestImpl::RequestImpl(std::unique_ptr<Job> job) |
15 const GURL& url, | 15 : job_(std::move(job)) { |
16 ProxyInfo* results, | 16 DCHECK(job_); |
17 const CompletionCallback& callback) | 17 } |
| 18 |
| 19 MockAsyncProxyResolver::RequestImpl::~RequestImpl() { |
| 20 MockAsyncProxyResolver* resolver = job_->Resolver(); |
| 21 // AddCancelledJob will check if request is already cancelled |
| 22 resolver->AddCancelledJob(std::move(job_)); |
| 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), url_(url), results_(results), callback_(callback) {} | 33 : resolver_(resolver), url_(url), results_(results), callback_(callback) {} |
19 | 34 |
20 void MockAsyncProxyResolver::Request::CompleteNow(int rv) { | 35 MockAsyncProxyResolver::Job::~Job() {} |
| 36 |
| 37 void MockAsyncProxyResolver::Job::CompleteNow(int rv) { |
21 CompletionCallback callback = callback_; | 38 CompletionCallback callback = callback_; |
22 | 39 |
23 // May delete |this|. | 40 resolver_->RemovePendingJob(this); |
24 resolver_->RemovePendingRequest(this); | |
25 | 41 |
26 callback.Run(rv); | 42 callback.Run(rv); |
27 } | 43 } |
28 | 44 |
29 MockAsyncProxyResolver::Request::~Request() {} | |
30 | |
31 MockAsyncProxyResolver::~MockAsyncProxyResolver() {} | 45 MockAsyncProxyResolver::~MockAsyncProxyResolver() {} |
32 | 46 |
33 int MockAsyncProxyResolver::GetProxyForURL( | 47 int MockAsyncProxyResolver::GetProxyForURL( |
34 const GURL& url, | 48 const GURL& url, |
35 ProxyInfo* results, | 49 ProxyInfo* results, |
36 const CompletionCallback& callback, | 50 const CompletionCallback& callback, |
37 RequestHandle* request_handle, | 51 std::unique_ptr<Request>* request, |
38 const NetLogWithSource& /*net_log*/) { | 52 const NetLogWithSource& /*net_log*/) { |
39 scoped_refptr<Request> request = new Request(this, url, results, callback); | 53 std::unique_ptr<Job> job(new Job(this, url, results, callback)); |
40 pending_requests_.push_back(request); | |
41 | 54 |
42 if (request_handle) | 55 pending_jobs_.push_back(job.get()); |
43 *request_handle = reinterpret_cast<RequestHandle>(request.get()); | 56 request->reset(new RequestImpl(std::move(job))); |
44 | 57 |
45 // Test code completes the request by calling request->CompleteNow(). | 58 // Test code completes the request by calling job->CompleteNow(). |
46 return ERR_IO_PENDING; | 59 return ERR_IO_PENDING; |
47 } | 60 } |
48 | 61 |
49 void MockAsyncProxyResolver::CancelRequest(RequestHandle request_handle) { | 62 void MockAsyncProxyResolver::AddCancelledJob(std::unique_ptr<Job> job) { |
50 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle); | 63 std::vector<Job*>::iterator it = |
51 cancelled_requests_.push_back(request); | 64 std::find(pending_jobs_.begin(), pending_jobs_.end(), job.get()); |
52 RemovePendingRequest(request.get()); | 65 // Because this is called always when RequestImpl is destructed, |
| 66 // we need to check if it is still in pending jobs. |
| 67 if (it != pending_jobs_.end()) { |
| 68 cancelled_jobs_.push_back(std::move(job)); |
| 69 pending_jobs_.erase(it); |
| 70 } |
53 } | 71 } |
54 | 72 |
55 LoadState MockAsyncProxyResolver::GetLoadState( | 73 void MockAsyncProxyResolver::RemovePendingJob(Job* job) { |
56 RequestHandle request_handle) const { | 74 DCHECK(job); |
57 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 75 std::vector<Job*>::iterator it = |
58 } | 76 std::find(pending_jobs_.begin(), pending_jobs_.end(), job); |
59 | 77 DCHECK(it != pending_jobs_.end()); |
60 void MockAsyncProxyResolver::RemovePendingRequest(Request* request) { | 78 pending_jobs_.erase(it); |
61 RequestsList::iterator it = std::find( | |
62 pending_requests_.begin(), pending_requests_.end(), request); | |
63 DCHECK(it != pending_requests_.end()); | |
64 pending_requests_.erase(it); | |
65 } | 79 } |
66 | 80 |
67 MockAsyncProxyResolver::MockAsyncProxyResolver() { | 81 MockAsyncProxyResolver::MockAsyncProxyResolver() { |
68 } | 82 } |
69 | 83 |
70 MockAsyncProxyResolverFactory::Request::Request( | 84 MockAsyncProxyResolverFactory::Request::Request( |
71 MockAsyncProxyResolverFactory* factory, | 85 MockAsyncProxyResolverFactory* factory, |
72 const scoped_refptr<ProxyResolverScriptData>& script_data, | 86 const scoped_refptr<ProxyResolverScriptData>& script_data, |
73 std::unique_ptr<ProxyResolver>* resolver, | 87 std::unique_ptr<ProxyResolver>* resolver, |
74 const CompletionCallback& callback) | 88 const CompletionCallback& callback) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } | 167 } |
154 } | 168 } |
155 | 169 |
156 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl) | 170 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl) |
157 : impl_(impl) { | 171 : impl_(impl) { |
158 } | 172 } |
159 | 173 |
160 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url, | 174 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url, |
161 ProxyInfo* results, | 175 ProxyInfo* results, |
162 const CompletionCallback& callback, | 176 const CompletionCallback& callback, |
163 RequestHandle* request, | 177 std::unique_ptr<Request>* request, |
164 const NetLogWithSource& net_log) { | 178 const NetLogWithSource& net_log) { |
165 return impl_->GetProxyForURL(query_url, results, callback, request, net_log); | 179 return impl_->GetProxyForURL(query_url, results, callback, request, net_log); |
166 } | 180 } |
167 | 181 |
168 void ForwardingProxyResolver::CancelRequest(RequestHandle request) { | |
169 impl_->CancelRequest(request); | |
170 } | |
171 | |
172 LoadState ForwardingProxyResolver::GetLoadState(RequestHandle request) const { | |
173 return impl_->GetLoadState(request); | |
174 } | |
175 | |
176 } // namespace net | 182 } // namespace net |
OLD | NEW |