OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/multi_threaded_proxy_resolver.h" | 5 #include "net/proxy/multi_threaded_proxy_resolver.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 size_t max_num_threads, | 109 size_t max_num_threads, |
110 const scoped_refptr<ProxyResolverScriptData>& script_data, | 110 const scoped_refptr<ProxyResolverScriptData>& script_data, |
111 scoped_refptr<Executor> executor); | 111 scoped_refptr<Executor> executor); |
112 | 112 |
113 ~MultiThreadedProxyResolver() override; | 113 ~MultiThreadedProxyResolver() override; |
114 | 114 |
115 // ProxyResolver implementation: | 115 // ProxyResolver implementation: |
116 int GetProxyForURL(const GURL& url, | 116 int GetProxyForURL(const GURL& url, |
117 ProxyInfo* results, | 117 ProxyInfo* results, |
118 const CompletionCallback& callback, | 118 const CompletionCallback& callback, |
119 RequestHandle* request, | 119 scoped_ptr<Request>* request, |
120 const BoundNetLog& net_log) override; | 120 const BoundNetLog& net_log) override; |
121 void CancelRequest(RequestHandle request) override; | 121 |
122 LoadState GetLoadState(RequestHandle request) const override; | 122 void RemovePendingJob(Job* job) { |
123 PendingJobsQueue::iterator it = | |
124 std::find(pending_jobs_.begin(), pending_jobs_.end(), job); | |
125 DCHECK(it != pending_jobs_.end()); | |
126 pending_jobs_.erase(it); | |
127 } | |
123 | 128 |
124 private: | 129 private: |
125 class GetProxyForURLJob; | 130 class GetProxyForURLJob; |
131 class RequestImpl; | |
126 // FIFO queue of pending jobs waiting to be started. | 132 // FIFO queue of pending jobs waiting to be started. |
127 // TODO(eroman): Make this priority queue. | 133 // TODO(eroman): Make this priority queue. |
128 typedef std::deque<scoped_refptr<Job>> PendingJobsQueue; | 134 typedef std::deque<scoped_refptr<Job>> PendingJobsQueue; |
129 typedef std::vector<scoped_refptr<Executor>> ExecutorList; | 135 typedef std::vector<scoped_refptr<Executor>> ExecutorList; |
130 | 136 |
131 // Returns an idle worker thread which is ready to receive GetProxyForURL() | 137 // Returns an idle worker thread which is ready to receive GetProxyForURL() |
132 // requests. If all threads are occupied, returns NULL. | 138 // requests. If all threads are occupied, returns NULL. |
133 Executor* FindIdleExecutor(); | 139 Executor* FindIdleExecutor(); |
134 | 140 |
135 // Creates a new worker thread, and appends it to |executors_|. | 141 // Creates a new worker thread, and appends it to |executors_|. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 | 229 |
224 virtual ~Job() {} | 230 virtual ~Job() {} |
225 | 231 |
226 private: | 232 private: |
227 const Type type_; | 233 const Type type_; |
228 CompletionCallback callback_; | 234 CompletionCallback callback_; |
229 Executor* executor_; | 235 Executor* executor_; |
230 bool was_cancelled_; | 236 bool was_cancelled_; |
231 }; | 237 }; |
232 | 238 |
239 class MultiThreadedProxyResolver::RequestImpl : public ProxyResolver::Request { | |
240 public: | |
241 RequestImpl(Job* job, MultiThreadedProxyResolver* resolver) | |
242 : job_(job), resolver_(resolver) {} | |
243 | |
244 ~RequestImpl() override { | |
245 DCHECK(resolver_->CalledOnValidThread()); | |
246 | |
247 if (job_->executor()) { | |
eroman
2015/11/24 01:20:59
This doesn't seem correct. If the job was already
| |
248 // If the job was already submitted to the executor, just mark it | |
249 // as cancelled so the user callback isn't run on completion. | |
250 job_->Cancel(); | |
251 } else { | |
252 // Otherwise the job is just sitting in a queue. | |
253 resolver_->RemovePendingJob(job_); | |
254 } | |
255 } | |
256 | |
257 LoadState GetLoadState() override { | |
258 DCHECK(resolver_->CalledOnValidThread()); | |
259 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | |
260 } | |
261 | |
262 private: | |
263 Job* job_; | |
264 MultiThreadedProxyResolver* resolver_; | |
265 }; | |
266 | |
233 // CreateResolverJob ----------------------------------------------------------- | 267 // CreateResolverJob ----------------------------------------------------------- |
234 | 268 |
235 // Runs on the worker thread to call ProxyResolverFactory::CreateProxyResolver. | 269 // Runs on the worker thread to call ProxyResolverFactory::CreateProxyResolver. |
236 class CreateResolverJob : public Job { | 270 class CreateResolverJob : public Job { |
237 public: | 271 public: |
238 CreateResolverJob(const scoped_refptr<ProxyResolverScriptData>& script_data, | 272 CreateResolverJob(const scoped_refptr<ProxyResolverScriptData>& script_data, |
239 ProxyResolverFactory* factory) | 273 ProxyResolverFactory* factory) |
240 : Job(TYPE_CREATE_RESOLVER, CompletionCallback()), | 274 : Job(TYPE_CREATE_RESOLVER, CompletionCallback()), |
241 script_data_(script_data), | 275 script_data_(script_data), |
242 factory_(factory) {} | 276 factory_(factory) {} |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
434 DCHECK(CalledOnValidThread()); | 468 DCHECK(CalledOnValidThread()); |
435 // We will cancel all outstanding requests. | 469 // We will cancel all outstanding requests. |
436 pending_jobs_.clear(); | 470 pending_jobs_.clear(); |
437 | 471 |
438 for (auto& executor : executors_) { | 472 for (auto& executor : executors_) { |
439 executor->Destroy(); | 473 executor->Destroy(); |
440 } | 474 } |
441 } | 475 } |
442 | 476 |
443 int MultiThreadedProxyResolver::GetProxyForURL( | 477 int MultiThreadedProxyResolver::GetProxyForURL( |
444 const GURL& url, ProxyInfo* results, const CompletionCallback& callback, | 478 const GURL& url, |
445 RequestHandle* request, const BoundNetLog& net_log) { | 479 ProxyInfo* results, |
480 const CompletionCallback& callback, | |
481 scoped_ptr<Request>* request, | |
482 const BoundNetLog& net_log) { | |
446 DCHECK(CalledOnValidThread()); | 483 DCHECK(CalledOnValidThread()); |
447 DCHECK(!callback.is_null()); | 484 DCHECK(!callback.is_null()); |
448 | 485 |
449 scoped_refptr<GetProxyForURLJob> job( | 486 scoped_refptr<GetProxyForURLJob> job( |
450 new GetProxyForURLJob(url, results, callback, net_log)); | 487 new GetProxyForURLJob(url, results, callback, net_log)); |
451 | 488 |
452 // Completion will be notified through |callback|, unless the caller cancels | 489 // Completion will be notified through |callback|, unless the caller cancels |
453 // the request using |request|. | 490 // the request using |request|. |
454 if (request) | 491 if (request) |
455 *request = reinterpret_cast<RequestHandle>(job.get()); | 492 request->reset(new RequestImpl(job.get(), this)); |
456 | 493 |
457 // If there is an executor that is ready to run this request, submit it! | 494 // If there is an executor that is ready to run this request, submit it! |
458 Executor* executor = FindIdleExecutor(); | 495 Executor* executor = FindIdleExecutor(); |
459 if (executor) { | 496 if (executor) { |
460 DCHECK_EQ(0u, pending_jobs_.size()); | 497 DCHECK_EQ(0u, pending_jobs_.size()); |
461 executor->StartJob(job.get()); | 498 executor->StartJob(job.get()); |
462 return ERR_IO_PENDING; | 499 return ERR_IO_PENDING; |
463 } | 500 } |
464 | 501 |
465 // Otherwise queue this request. (We will schedule it to a thread once one | 502 // Otherwise queue this request. (We will schedule it to a thread once one |
466 // becomes available). | 503 // becomes available). |
467 job->WaitingForThread(); | 504 job->WaitingForThread(); |
468 pending_jobs_.push_back(job); | 505 pending_jobs_.push_back(job); |
469 | 506 |
470 // If we haven't already reached the thread limit, provision a new thread to | 507 // If we haven't already reached the thread limit, provision a new thread to |
471 // drain the requests more quickly. | 508 // drain the requests more quickly. |
472 if (executors_.size() < max_num_threads_) | 509 if (executors_.size() < max_num_threads_) |
473 AddNewExecutor(); | 510 AddNewExecutor(); |
474 | 511 |
475 return ERR_IO_PENDING; | 512 return ERR_IO_PENDING; |
476 } | 513 } |
477 | 514 |
478 void MultiThreadedProxyResolver::CancelRequest(RequestHandle req) { | |
479 DCHECK(CalledOnValidThread()); | |
480 DCHECK(req); | |
481 | |
482 Job* job = reinterpret_cast<Job*>(req); | |
483 DCHECK_EQ(Job::TYPE_GET_PROXY_FOR_URL, job->type()); | |
484 | |
485 if (job->executor()) { | |
486 // If the job was already submitted to the executor, just mark it | |
487 // as cancelled so the user callback isn't run on completion. | |
488 job->Cancel(); | |
489 } else { | |
490 // Otherwise the job is just sitting in a queue. | |
491 PendingJobsQueue::iterator it = | |
492 std::find(pending_jobs_.begin(), pending_jobs_.end(), job); | |
493 DCHECK(it != pending_jobs_.end()); | |
494 pending_jobs_.erase(it); | |
495 } | |
496 } | |
497 | |
498 LoadState MultiThreadedProxyResolver::GetLoadState(RequestHandle req) const { | |
499 DCHECK(CalledOnValidThread()); | |
500 DCHECK(req); | |
501 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | |
502 } | |
503 | 515 |
504 Executor* MultiThreadedProxyResolver::FindIdleExecutor() { | 516 Executor* MultiThreadedProxyResolver::FindIdleExecutor() { |
505 DCHECK(CalledOnValidThread()); | 517 DCHECK(CalledOnValidThread()); |
506 for (ExecutorList::iterator it = executors_.begin(); | 518 for (ExecutorList::iterator it = executors_.begin(); |
507 it != executors_.end(); ++it) { | 519 it != executors_.end(); ++it) { |
508 Executor* executor = it->get(); | 520 Executor* executor = it->get(); |
509 if (!executor->outstanding_job()) | 521 if (!executor->outstanding_job()) |
510 return executor; | 522 return executor; |
511 } | 523 } |
512 return NULL; | 524 return NULL; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 return ERR_IO_PENDING; | 635 return ERR_IO_PENDING; |
624 } | 636 } |
625 | 637 |
626 void MultiThreadedProxyResolverFactory::RemoveJob( | 638 void MultiThreadedProxyResolverFactory::RemoveJob( |
627 MultiThreadedProxyResolverFactory::Job* job) { | 639 MultiThreadedProxyResolverFactory::Job* job) { |
628 size_t erased = jobs_.erase(job); | 640 size_t erased = jobs_.erase(job); |
629 DCHECK_EQ(1u, erased); | 641 DCHECK_EQ(1u, erased); |
630 } | 642 } |
631 | 643 |
632 } // namespace net | 644 } // namespace net |
OLD | NEW |