| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/single_threaded_proxy_resolver.h" | 5 #include "net/proxy/single_threaded_proxy_resolver.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/thread.h" | 8 #include "base/thread.h" |
| 9 #include "net/base/capturing_net_log.h" | 9 #include "net/base/capturing_net_log.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 // SingleThreadedProxyResolver::SetPacScriptTask ------------------------------ | 29 // SingleThreadedProxyResolver::SetPacScriptTask ------------------------------ |
| 30 | 30 |
| 31 // Runs on the worker thread to call ProxyResolver::SetPacScript. | 31 // Runs on the worker thread to call ProxyResolver::SetPacScript. |
| 32 class SingleThreadedProxyResolver::SetPacScriptTask | 32 class SingleThreadedProxyResolver::SetPacScriptTask |
| 33 : public base::RefCountedThreadSafe< | 33 : public base::RefCountedThreadSafe< |
| 34 SingleThreadedProxyResolver::SetPacScriptTask> { | 34 SingleThreadedProxyResolver::SetPacScriptTask> { |
| 35 public: | 35 public: |
| 36 SetPacScriptTask(SingleThreadedProxyResolver* coordinator, | 36 SetPacScriptTask(SingleThreadedProxyResolver* coordinator, |
| 37 const GURL& pac_url, | 37 const GURL& pac_url, |
| 38 const std::string& pac_bytes, | 38 const string16& pac_script, |
| 39 CompletionCallback* callback) | 39 CompletionCallback* callback) |
| 40 : coordinator_(coordinator), | 40 : coordinator_(coordinator), |
| 41 callback_(callback), | 41 callback_(callback), |
| 42 pac_bytes_(pac_bytes), | 42 pac_script_(pac_script), |
| 43 pac_url_(pac_url), | 43 pac_url_(pac_url), |
| 44 origin_loop_(MessageLoop::current()) { | 44 origin_loop_(MessageLoop::current()) { |
| 45 DCHECK(callback); | 45 DCHECK(callback); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Start the SetPacScript request on the worker thread. | 48 // Start the SetPacScript request on the worker thread. |
| 49 void Start() { | 49 void Start() { |
| 50 coordinator_->thread()->message_loop()->PostTask( | 50 coordinator_->thread()->message_loop()->PostTask( |
| 51 FROM_HERE, NewRunnableMethod(this, &SetPacScriptTask::DoRequest, | 51 FROM_HERE, NewRunnableMethod(this, &SetPacScriptTask::DoRequest, |
| 52 coordinator_->resolver_.get())); | 52 coordinator_->resolver_.get())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 friend class base::RefCountedThreadSafe< | 66 friend class base::RefCountedThreadSafe< |
| 67 SingleThreadedProxyResolver::SetPacScriptTask>; | 67 SingleThreadedProxyResolver::SetPacScriptTask>; |
| 68 | 68 |
| 69 ~SetPacScriptTask() {} | 69 ~SetPacScriptTask() {} |
| 70 | 70 |
| 71 // Runs on the worker thread. | 71 // Runs on the worker thread. |
| 72 void DoRequest(ProxyResolver* resolver) { | 72 void DoRequest(ProxyResolver* resolver) { |
| 73 int rv = resolver->expects_pac_bytes() ? | 73 int rv = resolver->expects_pac_bytes() ? |
| 74 resolver->SetPacScriptByData(pac_bytes_, NULL) : | 74 resolver->SetPacScriptByData(pac_script_, NULL) : |
| 75 resolver->SetPacScriptByUrl(pac_url_, NULL); | 75 resolver->SetPacScriptByUrl(pac_url_, NULL); |
| 76 | 76 |
| 77 DCHECK_NE(rv, ERR_IO_PENDING); | 77 DCHECK_NE(rv, ERR_IO_PENDING); |
| 78 origin_loop_->PostTask(FROM_HERE, | 78 origin_loop_->PostTask(FROM_HERE, |
| 79 NewRunnableMethod(this, &SetPacScriptTask::RequestComplete, rv)); | 79 NewRunnableMethod(this, &SetPacScriptTask::RequestComplete, rv)); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Runs the completion callback on the origin thread. | 82 // Runs the completion callback on the origin thread. |
| 83 void RequestComplete(int result_code) { | 83 void RequestComplete(int result_code) { |
| 84 // The task may have been cancelled after it was started. | 84 // The task may have been cancelled after it was started. |
| 85 if (!was_cancelled()) { | 85 if (!was_cancelled()) { |
| 86 CompletionCallback* callback = callback_; | 86 CompletionCallback* callback = callback_; |
| 87 coordinator_->RemoveOutstandingSetPacScriptTask(this); | 87 coordinator_->RemoveOutstandingSetPacScriptTask(this); |
| 88 callback->Run(result_code); | 88 callback->Run(result_code); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Must only be used on the "origin" thread. | 92 // Must only be used on the "origin" thread. |
| 93 SingleThreadedProxyResolver* coordinator_; | 93 SingleThreadedProxyResolver* coordinator_; |
| 94 CompletionCallback* callback_; | 94 CompletionCallback* callback_; |
| 95 std::string pac_bytes_; | 95 string16 pac_script_; |
| 96 GURL pac_url_; | 96 GURL pac_url_; |
| 97 | 97 |
| 98 // Usable from within DoQuery on the worker thread. | 98 // Usable from within DoQuery on the worker thread. |
| 99 MessageLoop* origin_loop_; | 99 MessageLoop* origin_loop_; |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 // SingleThreadedProxyResolver::Job ------------------------------------------- | 102 // SingleThreadedProxyResolver::Job ------------------------------------------- |
| 103 | 103 |
| 104 class SingleThreadedProxyResolver::Job | 104 class SingleThreadedProxyResolver::Job |
| 105 : public base::RefCountedThreadSafe<SingleThreadedProxyResolver::Job> { | 105 : public base::RefCountedThreadSafe<SingleThreadedProxyResolver::Job> { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 void SingleThreadedProxyResolver::PurgeMemory() { | 293 void SingleThreadedProxyResolver::PurgeMemory() { |
| 294 if (thread_.get()) { | 294 if (thread_.get()) { |
| 295 scoped_refptr<PurgeMemoryTask> helper(new PurgeMemoryTask(resolver_.get())); | 295 scoped_refptr<PurgeMemoryTask> helper(new PurgeMemoryTask(resolver_.get())); |
| 296 thread_->message_loop()->PostTask(FROM_HERE, | 296 thread_->message_loop()->PostTask(FROM_HERE, |
| 297 NewRunnableMethod(helper.get(), &PurgeMemoryTask::PurgeMemory)); | 297 NewRunnableMethod(helper.get(), &PurgeMemoryTask::PurgeMemory)); |
| 298 } | 298 } |
| 299 } | 299 } |
| 300 | 300 |
| 301 int SingleThreadedProxyResolver::SetPacScript( | 301 int SingleThreadedProxyResolver::SetPacScript( |
| 302 const GURL& pac_url, | 302 const GURL& pac_url, |
| 303 const std::string& pac_bytes, | 303 const string16& pac_script, |
| 304 CompletionCallback* callback) { | 304 CompletionCallback* callback) { |
| 305 EnsureThreadStarted(); | 305 EnsureThreadStarted(); |
| 306 DCHECK(!outstanding_set_pac_script_task_); | 306 DCHECK(!outstanding_set_pac_script_task_); |
| 307 | 307 |
| 308 SetPacScriptTask* task = new SetPacScriptTask( | 308 SetPacScriptTask* task = new SetPacScriptTask( |
| 309 this, pac_url, pac_bytes, callback); | 309 this, pac_url, pac_script, callback); |
| 310 outstanding_set_pac_script_task_ = task; | 310 outstanding_set_pac_script_task_ = task; |
| 311 task->Start(); | 311 task->Start(); |
| 312 return ERR_IO_PENDING; | 312 return ERR_IO_PENDING; |
| 313 } | 313 } |
| 314 | 314 |
| 315 void SingleThreadedProxyResolver::EnsureThreadStarted() { | 315 void SingleThreadedProxyResolver::EnsureThreadStarted() { |
| 316 if (!thread_.get()) { | 316 if (!thread_.get()) { |
| 317 thread_.reset(new base::Thread("pac-thread")); | 317 thread_.reset(new base::Thread("pac-thread")); |
| 318 thread_->Start(); | 318 thread_->Start(); |
| 319 } | 319 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 344 ProcessPendingJobs(); | 344 ProcessPendingJobs(); |
| 345 } | 345 } |
| 346 | 346 |
| 347 void SingleThreadedProxyResolver::RemoveOutstandingSetPacScriptTask( | 347 void SingleThreadedProxyResolver::RemoveOutstandingSetPacScriptTask( |
| 348 SetPacScriptTask* task) { | 348 SetPacScriptTask* task) { |
| 349 DCHECK_EQ(outstanding_set_pac_script_task_.get(), task); | 349 DCHECK_EQ(outstanding_set_pac_script_task_.get(), task); |
| 350 outstanding_set_pac_script_task_ = NULL; | 350 outstanding_set_pac_script_task_ = NULL; |
| 351 } | 351 } |
| 352 | 352 |
| 353 } // namespace net | 353 } // namespace net |
| OLD | NEW |