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