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