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/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(scoped_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), | 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 resolver_->RemovePendingJob(this); |
29 resolver_->RemovePendingRequest(this); | |
30 | 45 |
31 callback.Run(rv); | 46 callback.Run(rv); |
32 } | 47 } |
33 | 48 |
34 MockAsyncProxyResolver::Request::~Request() {} | |
35 | |
36 MockAsyncProxyResolver::~MockAsyncProxyResolver() {} | 49 MockAsyncProxyResolver::~MockAsyncProxyResolver() {} |
37 | 50 |
38 int MockAsyncProxyResolver::GetProxyForURL(const GURL& url, | 51 int MockAsyncProxyResolver::GetProxyForURL(const GURL& url, |
39 ProxyInfo* results, | 52 ProxyInfo* results, |
40 const CompletionCallback& callback, | 53 const CompletionCallback& callback, |
41 RequestHandle* request_handle, | 54 scoped_ptr<Request>* request, |
42 const BoundNetLog& /*net_log*/) { | 55 const BoundNetLog& /*net_log*/) { |
43 scoped_refptr<Request> request = new Request(this, url, results, callback); | 56 scoped_ptr<Job> job(new Job(this, url, results, callback)); |
44 pending_requests_.push_back(request); | |
45 | 57 |
46 if (request_handle) | 58 pending_jobs_.push_back(job.get()); |
47 *request_handle = reinterpret_cast<RequestHandle>(request.get()); | 59 request->reset(new RequestImpl(std::move(job))); |
48 | 60 |
49 // Test code completes the request by calling request->CompleteNow(). | 61 // Test code completes the request by calling job->CompleteNow(). |
50 return ERR_IO_PENDING; | 62 return ERR_IO_PENDING; |
51 } | 63 } |
52 | 64 |
53 void MockAsyncProxyResolver::CancelRequest(RequestHandle request_handle) { | 65 void MockAsyncProxyResolver::AddCancelledJob(scoped_ptr<Job> job) { |
54 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle); | 66 std::vector<Job*>::iterator it = |
55 cancelled_requests_.push_back(request); | 67 std::find(pending_jobs_.begin(), pending_jobs_.end(), job.get()); |
56 RemovePendingRequest(request.get()); | 68 // Because this is called always when RequestImpl is destructed, |
| 69 // we need to check if it is still in pending jobs. |
| 70 if (it != pending_jobs_.end()) { |
| 71 cancelled_jobs_.push_back(std::move(job)); |
| 72 pending_jobs_.erase(it); |
| 73 } |
57 } | 74 } |
58 | 75 |
59 LoadState MockAsyncProxyResolver::GetLoadState( | 76 void MockAsyncProxyResolver::RemovePendingJob(Job* job) { |
60 RequestHandle request_handle) const { | 77 DCHECK(job); |
61 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 78 std::vector<Job*>::iterator it = |
62 } | 79 std::find(pending_jobs_.begin(), pending_jobs_.end(), job); |
63 | 80 DCHECK(it != pending_jobs_.end()); |
64 void MockAsyncProxyResolver::RemovePendingRequest(Request* request) { | 81 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 } | 82 } |
70 | 83 |
71 MockAsyncProxyResolver::MockAsyncProxyResolver() { | 84 MockAsyncProxyResolver::MockAsyncProxyResolver() { |
72 } | 85 } |
73 | 86 |
74 MockAsyncProxyResolverFactory::Request::Request( | 87 MockAsyncProxyResolverFactory::Request::Request( |
75 MockAsyncProxyResolverFactory* factory, | 88 MockAsyncProxyResolverFactory* factory, |
76 const scoped_refptr<ProxyResolverScriptData>& script_data, | 89 const scoped_refptr<ProxyResolverScriptData>& script_data, |
77 scoped_ptr<ProxyResolver>* resolver, | 90 scoped_ptr<ProxyResolver>* resolver, |
78 const CompletionCallback& callback) | 91 const CompletionCallback& callback) |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 } | 171 } |
159 } | 172 } |
160 | 173 |
161 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl) | 174 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl) |
162 : impl_(impl) { | 175 : impl_(impl) { |
163 } | 176 } |
164 | 177 |
165 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url, | 178 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url, |
166 ProxyInfo* results, | 179 ProxyInfo* results, |
167 const CompletionCallback& callback, | 180 const CompletionCallback& callback, |
168 RequestHandle* request, | 181 scoped_ptr<Request>* request, |
169 const BoundNetLog& net_log) { | 182 const BoundNetLog& net_log) { |
170 return impl_->GetProxyForURL(query_url, results, callback, request, net_log); | 183 return impl_->GetProxyForURL(query_url, results, callback, request, net_log); |
171 } | 184 } |
172 | 185 |
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 | 186 } // namespace net |
OLD | NEW |