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