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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/thread_task_runner_handle.h" |
16 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
18 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
20 #include "net/log/net_log.h" | 21 #include "net/log/net_log.h" |
21 #include "net/proxy/proxy_info.h" | 22 #include "net/proxy/proxy_info.h" |
22 #include "net/proxy/proxy_resolver.h" | 23 #include "net/proxy/proxy_resolver.h" |
23 | 24 |
24 namespace net { | 25 namespace net { |
25 namespace { | 26 namespace { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 195 |
195 // This method is called when the job is inserted into a wait queue | 196 // This method is called when the job is inserted into a wait queue |
196 // because no executors were ready to accept it. | 197 // because no executors were ready to accept it. |
197 virtual void WaitingForThread() {} | 198 virtual void WaitingForThread() {} |
198 | 199 |
199 // This method is called just before the job is posted to the work thread. | 200 // This method is called just before the job is posted to the work thread. |
200 virtual void FinishedWaitingForThread() {} | 201 virtual void FinishedWaitingForThread() {} |
201 | 202 |
202 // This method is called on the worker thread to do the job's work. On | 203 // This method is called on the worker thread to do the job's work. On |
203 // completion, implementors are expected to call OnJobCompleted() on | 204 // completion, implementors are expected to call OnJobCompleted() on |
204 // |origin_loop|. | 205 // |origin_runner|. |
205 virtual void Run(scoped_refptr<base::MessageLoopProxy> origin_loop) = 0; | 206 virtual void Run( |
| 207 scoped_refptr<base::SingleThreadTaskRunner> origin_runner) = 0; |
206 | 208 |
207 protected: | 209 protected: |
208 void OnJobCompleted() { | 210 void OnJobCompleted() { |
209 // |executor_| will be NULL if the executor has already been deleted. | 211 // |executor_| will be NULL if the executor has already been deleted. |
210 if (executor_) | 212 if (executor_) |
211 executor_->OnJobCompleted(this); | 213 executor_->OnJobCompleted(this); |
212 } | 214 } |
213 | 215 |
214 void RunUserCallback(int result) { | 216 void RunUserCallback(int result) { |
215 DCHECK(has_user_callback()); | 217 DCHECK(has_user_callback()); |
(...skipping 19 matching lines...) Expand all Loading... |
235 // Runs on the worker thread to call ProxyResolverFactory::CreateProxyResolver. | 237 // Runs on the worker thread to call ProxyResolverFactory::CreateProxyResolver. |
236 class CreateResolverJob : public Job { | 238 class CreateResolverJob : public Job { |
237 public: | 239 public: |
238 CreateResolverJob(const scoped_refptr<ProxyResolverScriptData>& script_data, | 240 CreateResolverJob(const scoped_refptr<ProxyResolverScriptData>& script_data, |
239 ProxyResolverFactory* factory) | 241 ProxyResolverFactory* factory) |
240 : Job(TYPE_CREATE_RESOLVER, CompletionCallback()), | 242 : Job(TYPE_CREATE_RESOLVER, CompletionCallback()), |
241 script_data_(script_data), | 243 script_data_(script_data), |
242 factory_(factory) {} | 244 factory_(factory) {} |
243 | 245 |
244 // Runs on the worker thread. | 246 // Runs on the worker thread. |
245 void Run(scoped_refptr<base::MessageLoopProxy> origin_loop) override { | 247 void Run(scoped_refptr<base::SingleThreadTaskRunner> origin_runner) override { |
246 scoped_ptr<ProxyResolverFactory::Request> request; | 248 scoped_ptr<ProxyResolverFactory::Request> request; |
247 int rv = factory_->CreateProxyResolver(script_data_, &resolver_, | 249 int rv = factory_->CreateProxyResolver(script_data_, &resolver_, |
248 CompletionCallback(), &request); | 250 CompletionCallback(), &request); |
249 | 251 |
250 DCHECK_NE(rv, ERR_IO_PENDING); | 252 DCHECK_NE(rv, ERR_IO_PENDING); |
251 origin_loop->PostTask( | 253 origin_runner->PostTask( |
252 FROM_HERE, base::Bind(&CreateResolverJob::RequestComplete, this, rv)); | 254 FROM_HERE, base::Bind(&CreateResolverJob::RequestComplete, this, rv)); |
253 } | 255 } |
254 | 256 |
255 protected: | 257 protected: |
256 ~CreateResolverJob() override {} | 258 ~CreateResolverJob() override {} |
257 | 259 |
258 private: | 260 private: |
259 // Runs the completion callback on the origin thread. | 261 // Runs the completion callback on the origin thread. |
260 void RequestComplete(int result_code) { | 262 void RequestComplete(int result_code) { |
261 // The task may have been cancelled after it was started. | 263 // The task may have been cancelled after it was started. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 if (was_waiting_for_thread_) { | 304 if (was_waiting_for_thread_) { |
303 net_log_.EndEvent(NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD); | 305 net_log_.EndEvent(NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD); |
304 } | 306 } |
305 | 307 |
306 net_log_.AddEvent( | 308 net_log_.AddEvent( |
307 NetLog::TYPE_SUBMITTED_TO_RESOLVER_THREAD, | 309 NetLog::TYPE_SUBMITTED_TO_RESOLVER_THREAD, |
308 NetLog::IntegerCallback("thread_number", executor()->thread_number())); | 310 NetLog::IntegerCallback("thread_number", executor()->thread_number())); |
309 } | 311 } |
310 | 312 |
311 // Runs on the worker thread. | 313 // Runs on the worker thread. |
312 void Run(scoped_refptr<base::MessageLoopProxy> origin_loop) override { | 314 void Run(scoped_refptr<base::SingleThreadTaskRunner> origin_runner) override { |
313 ProxyResolver* resolver = executor()->resolver(); | 315 ProxyResolver* resolver = executor()->resolver(); |
314 DCHECK(resolver); | 316 DCHECK(resolver); |
315 int rv = resolver->GetProxyForURL( | 317 int rv = resolver->GetProxyForURL( |
316 url_, &results_buf_, CompletionCallback(), NULL, net_log_); | 318 url_, &results_buf_, CompletionCallback(), NULL, net_log_); |
317 DCHECK_NE(rv, ERR_IO_PENDING); | 319 DCHECK_NE(rv, ERR_IO_PENDING); |
318 | 320 |
319 origin_loop->PostTask( | 321 origin_runner->PostTask( |
320 FROM_HERE, | 322 FROM_HERE, base::Bind(&GetProxyForURLJob::QueryComplete, this, rv)); |
321 base::Bind(&GetProxyForURLJob::QueryComplete, this, rv)); | |
322 } | 323 } |
323 | 324 |
324 protected: | 325 protected: |
325 ~GetProxyForURLJob() override {} | 326 ~GetProxyForURLJob() override {} |
326 | 327 |
327 private: | 328 private: |
328 // Runs the completion callback on the origin thread. | 329 // Runs the completion callback on the origin thread. |
329 void QueryComplete(int result_code) { | 330 void QueryComplete(int result_code) { |
330 // The Job may have been cancelled after it was started. | 331 // The Job may have been cancelled after it was started. |
331 if (!was_cancelled()) { | 332 if (!was_cancelled()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 void Executor::StartJob(Job* job) { | 365 void Executor::StartJob(Job* job) { |
365 DCHECK(!outstanding_job_.get()); | 366 DCHECK(!outstanding_job_.get()); |
366 outstanding_job_ = job; | 367 outstanding_job_ = job; |
367 | 368 |
368 // Run the job. Once it has completed (regardless of whether it was | 369 // Run the job. Once it has completed (regardless of whether it was |
369 // cancelled), it will invoke OnJobCompleted() on this thread. | 370 // cancelled), it will invoke OnJobCompleted() on this thread. |
370 job->set_executor(this); | 371 job->set_executor(this); |
371 job->FinishedWaitingForThread(); | 372 job->FinishedWaitingForThread(); |
372 thread_->message_loop()->PostTask( | 373 thread_->message_loop()->PostTask( |
373 FROM_HERE, | 374 FROM_HERE, |
374 base::Bind(&Job::Run, job, base::MessageLoopProxy::current())); | 375 base::Bind(&Job::Run, job, base::ThreadTaskRunnerHandle::Get())); |
375 } | 376 } |
376 | 377 |
377 void Executor::OnJobCompleted(Job* job) { | 378 void Executor::OnJobCompleted(Job* job) { |
378 DCHECK_EQ(job, outstanding_job_.get()); | 379 DCHECK_EQ(job, outstanding_job_.get()); |
379 outstanding_job_ = NULL; | 380 outstanding_job_ = NULL; |
380 coordinator_->OnExecutorReady(this); | 381 coordinator_->OnExecutorReady(this); |
381 } | 382 } |
382 | 383 |
383 void Executor::Destroy() { | 384 void Executor::Destroy() { |
384 DCHECK(coordinator_); | 385 DCHECK(coordinator_); |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 return ERR_IO_PENDING; | 637 return ERR_IO_PENDING; |
637 } | 638 } |
638 | 639 |
639 void MultiThreadedProxyResolverFactory::RemoveJob( | 640 void MultiThreadedProxyResolverFactory::RemoveJob( |
640 MultiThreadedProxyResolverFactory::Job* job) { | 641 MultiThreadedProxyResolverFactory::Job* job) { |
641 size_t erased = jobs_.erase(job); | 642 size_t erased = jobs_.erase(job); |
642 DCHECK_EQ(1u, erased); | 643 DCHECK_EQ(1u, erased); |
643 } | 644 } |
644 | 645 |
645 } // namespace net | 646 } // namespace net |
OLD | NEW |