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 <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/threading/non_thread_safe.h" | |
19 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_checker.h" |
20 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/log/net_log.h" | 23 #include "net/log/net_log.h" |
24 #include "net/log/net_log_event_type.h" | 24 #include "net/log/net_log_event_type.h" |
25 #include "net/log/net_log_with_source.h" | 25 #include "net/log/net_log_with_source.h" |
26 #include "net/proxy/proxy_info.h" | 26 #include "net/proxy/proxy_info.h" |
27 #include "net/proxy/proxy_resolver.h" | 27 #include "net/proxy/proxy_resolver.h" |
28 | 28 |
29 namespace net { | 29 namespace net { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 std::unique_ptr<ProxyResolver> resolver_; | 91 std::unique_ptr<ProxyResolver> resolver_; |
92 | 92 |
93 // The thread where |resolver_| is run on. | 93 // The thread where |resolver_| is run on. |
94 // Note that declaration ordering is important here. |thread_| needs to be | 94 // Note that declaration ordering is important here. |thread_| needs to be |
95 // destroyed *before* |resolver_|, in case |resolver_| is currently | 95 // destroyed *before* |resolver_|, in case |resolver_| is currently |
96 // executing on |thread_|. | 96 // executing on |thread_|. |
97 std::unique_ptr<base::Thread> thread_; | 97 std::unique_ptr<base::Thread> thread_; |
98 }; | 98 }; |
99 | 99 |
100 class MultiThreadedProxyResolver : public ProxyResolver, | 100 class MultiThreadedProxyResolver : public ProxyResolver, |
101 public Executor::Coordinator, | 101 public Executor::Coordinator { |
102 public base::NonThreadSafe { | |
103 public: | 102 public: |
104 // Creates an asynchronous ProxyResolver that runs requests on up to | 103 // Creates an asynchronous ProxyResolver that runs requests on up to |
105 // |max_num_threads|. | 104 // |max_num_threads|. |
106 // | 105 // |
107 // For each thread that is created, an accompanying synchronous ProxyResolver | 106 // For each thread that is created, an accompanying synchronous ProxyResolver |
108 // will be provisioned using |resolver_factory|. All methods on these | 107 // will be provisioned using |resolver_factory|. All methods on these |
109 // ProxyResolvers will be called on the one thread. | 108 // ProxyResolvers will be called on the one thread. |
110 MultiThreadedProxyResolver( | 109 MultiThreadedProxyResolver( |
111 std::unique_ptr<ProxyResolverFactory> resolver_factory, | 110 std::unique_ptr<ProxyResolverFactory> resolver_factory, |
112 size_t max_num_threads, | 111 size_t max_num_threads, |
(...skipping 25 matching lines...) Expand all Loading... |
138 void AddNewExecutor(); | 137 void AddNewExecutor(); |
139 | 138 |
140 // Starts the next job from |pending_jobs_| if possible. | 139 // Starts the next job from |pending_jobs_| if possible. |
141 void OnExecutorReady(Executor* executor) override; | 140 void OnExecutorReady(Executor* executor) override; |
142 | 141 |
143 const std::unique_ptr<ProxyResolverFactory> resolver_factory_; | 142 const std::unique_ptr<ProxyResolverFactory> resolver_factory_; |
144 const size_t max_num_threads_; | 143 const size_t max_num_threads_; |
145 PendingJobsQueue pending_jobs_; | 144 PendingJobsQueue pending_jobs_; |
146 ExecutorList executors_; | 145 ExecutorList executors_; |
147 scoped_refptr<ProxyResolverScriptData> script_data_; | 146 scoped_refptr<ProxyResolverScriptData> script_data_; |
| 147 |
| 148 THREAD_CHECKER(thread_checker_); |
148 }; | 149 }; |
149 | 150 |
150 // Job --------------------------------------------- | 151 // Job --------------------------------------------- |
151 | 152 |
152 class Job : public base::RefCountedThreadSafe<Job> { | 153 class Job : public base::RefCountedThreadSafe<Job> { |
153 public: | 154 public: |
154 // Identifies the subclass of Job (only being used for debugging purposes). | 155 // Identifies the subclass of Job (only being used for debugging purposes). |
155 enum Type { | 156 enum Type { |
156 TYPE_GET_PROXY_FOR_URL, | 157 TYPE_GET_PROXY_FOR_URL, |
157 TYPE_CREATE_RESOLVER, | 158 TYPE_CREATE_RESOLVER, |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 scoped_refptr<Executor> executor) | 441 scoped_refptr<Executor> executor) |
441 : resolver_factory_(std::move(resolver_factory)), | 442 : resolver_factory_(std::move(resolver_factory)), |
442 max_num_threads_(max_num_threads), | 443 max_num_threads_(max_num_threads), |
443 script_data_(script_data) { | 444 script_data_(script_data) { |
444 DCHECK(script_data_); | 445 DCHECK(script_data_); |
445 executor->set_coordinator(this); | 446 executor->set_coordinator(this); |
446 executors_.push_back(executor); | 447 executors_.push_back(executor); |
447 } | 448 } |
448 | 449 |
449 MultiThreadedProxyResolver::~MultiThreadedProxyResolver() { | 450 MultiThreadedProxyResolver::~MultiThreadedProxyResolver() { |
450 DCHECK(CalledOnValidThread()); | 451 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
451 // We will cancel all outstanding requests. | 452 // We will cancel all outstanding requests. |
452 pending_jobs_.clear(); | 453 pending_jobs_.clear(); |
453 | 454 |
454 for (auto& executor : executors_) { | 455 for (auto& executor : executors_) { |
455 executor->Destroy(); | 456 executor->Destroy(); |
456 } | 457 } |
457 } | 458 } |
458 | 459 |
459 int MultiThreadedProxyResolver::GetProxyForURL( | 460 int MultiThreadedProxyResolver::GetProxyForURL( |
460 const GURL& url, | 461 const GURL& url, |
461 ProxyInfo* results, | 462 ProxyInfo* results, |
462 const CompletionCallback& callback, | 463 const CompletionCallback& callback, |
463 std::unique_ptr<Request>* request, | 464 std::unique_ptr<Request>* request, |
464 const NetLogWithSource& net_log) { | 465 const NetLogWithSource& net_log) { |
465 DCHECK(CalledOnValidThread()); | 466 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
466 DCHECK(!callback.is_null()); | 467 DCHECK(!callback.is_null()); |
467 | 468 |
468 scoped_refptr<GetProxyForURLJob> job( | 469 scoped_refptr<GetProxyForURLJob> job( |
469 new GetProxyForURLJob(url, results, callback, net_log)); | 470 new GetProxyForURLJob(url, results, callback, net_log)); |
470 | 471 |
471 // Completion will be notified through |callback|, unless the caller cancels | 472 // Completion will be notified through |callback|, unless the caller cancels |
472 // the request using |request|. | 473 // the request using |request|. |
473 if (request) | 474 if (request) |
474 request->reset(new RequestImpl(job)); | 475 request->reset(new RequestImpl(job)); |
475 | 476 |
(...skipping 12 matching lines...) Expand all Loading... |
488 | 489 |
489 // If we haven't already reached the thread limit, provision a new thread to | 490 // If we haven't already reached the thread limit, provision a new thread to |
490 // drain the requests more quickly. | 491 // drain the requests more quickly. |
491 if (executors_.size() < max_num_threads_) | 492 if (executors_.size() < max_num_threads_) |
492 AddNewExecutor(); | 493 AddNewExecutor(); |
493 | 494 |
494 return ERR_IO_PENDING; | 495 return ERR_IO_PENDING; |
495 } | 496 } |
496 | 497 |
497 Executor* MultiThreadedProxyResolver::FindIdleExecutor() { | 498 Executor* MultiThreadedProxyResolver::FindIdleExecutor() { |
498 DCHECK(CalledOnValidThread()); | 499 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
499 for (ExecutorList::iterator it = executors_.begin(); | 500 for (ExecutorList::iterator it = executors_.begin(); |
500 it != executors_.end(); ++it) { | 501 it != executors_.end(); ++it) { |
501 Executor* executor = it->get(); | 502 Executor* executor = it->get(); |
502 if (!executor->outstanding_job()) | 503 if (!executor->outstanding_job()) |
503 return executor; | 504 return executor; |
504 } | 505 } |
505 return NULL; | 506 return NULL; |
506 } | 507 } |
507 | 508 |
508 void MultiThreadedProxyResolver::AddNewExecutor() { | 509 void MultiThreadedProxyResolver::AddNewExecutor() { |
509 DCHECK(CalledOnValidThread()); | 510 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
510 DCHECK_LT(executors_.size(), max_num_threads_); | 511 DCHECK_LT(executors_.size(), max_num_threads_); |
511 // The "thread number" is used to give the thread a unique name. | 512 // The "thread number" is used to give the thread a unique name. |
512 int thread_number = executors_.size(); | 513 int thread_number = executors_.size(); |
513 Executor* executor = new Executor(this, thread_number); | 514 Executor* executor = new Executor(this, thread_number); |
514 executor->StartJob( | 515 executor->StartJob( |
515 new CreateResolverJob(script_data_, resolver_factory_.get())); | 516 new CreateResolverJob(script_data_, resolver_factory_.get())); |
516 executors_.push_back(make_scoped_refptr(executor)); | 517 executors_.push_back(make_scoped_refptr(executor)); |
517 } | 518 } |
518 | 519 |
519 void MultiThreadedProxyResolver::OnExecutorReady(Executor* executor) { | 520 void MultiThreadedProxyResolver::OnExecutorReady(Executor* executor) { |
520 DCHECK(CalledOnValidThread()); | 521 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
521 while (!pending_jobs_.empty()) { | 522 while (!pending_jobs_.empty()) { |
522 scoped_refptr<Job> job = pending_jobs_.front(); | 523 scoped_refptr<Job> job = pending_jobs_.front(); |
523 pending_jobs_.pop_front(); | 524 pending_jobs_.pop_front(); |
524 if (!job->was_cancelled()) { | 525 if (!job->was_cancelled()) { |
525 executor->StartJob(job.get()); | 526 executor->StartJob(job.get()); |
526 return; | 527 return; |
527 } | 528 } |
528 } | 529 } |
529 } | 530 } |
530 | 531 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 return ERR_IO_PENDING; | 617 return ERR_IO_PENDING; |
617 } | 618 } |
618 | 619 |
619 void MultiThreadedProxyResolverFactory::RemoveJob( | 620 void MultiThreadedProxyResolverFactory::RemoveJob( |
620 MultiThreadedProxyResolverFactory::Job* job) { | 621 MultiThreadedProxyResolverFactory::Job* job) { |
621 size_t erased = jobs_.erase(job); | 622 size_t erased = jobs_.erase(job); |
622 DCHECK_EQ(1u, erased); | 623 DCHECK_EQ(1u, erased); |
623 } | 624 } |
624 | 625 |
625 } // namespace net | 626 } // namespace net |
OLD | NEW |