| 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 "remoting/base/scoped_thread_proxy.h" | 5 #include "remoting/base/scoped_thread_proxy.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/single_thread_task_runner.h" |
| 8 | 9 |
| 9 namespace remoting { | 10 namespace remoting { |
| 10 | 11 |
| 11 class ScopedThreadProxy::Core : public base::RefCountedThreadSafe<Core> { | 12 class ScopedThreadProxy::Core : public base::RefCountedThreadSafe<Core> { |
| 12 public: | 13 public: |
| 13 Core(base::MessageLoopProxy* message_loop) | 14 Core(base::SingleThreadTaskRunner* task_runner) |
| 14 : message_loop_(message_loop), | 15 : task_runner_(task_runner), |
| 15 canceled_(false) { | 16 canceled_(false) { |
| 16 } | 17 } |
| 17 | 18 |
| 18 void PostTask(const tracked_objects::Location& from_here, | 19 void PostTask(const tracked_objects::Location& from_here, |
| 19 const base::Closure& closure) { | 20 const base::Closure& closure) { |
| 20 if (!canceled_) | 21 if (!canceled_) |
| 21 message_loop_->PostTask(from_here, Wrap(closure)); | 22 task_runner_->PostTask(from_here, Wrap(closure)); |
| 22 } | 23 } |
| 23 | 24 |
| 24 void PostDelayedTask( | 25 void PostDelayedTask( |
| 25 const tracked_objects::Location& from_here, | 26 const tracked_objects::Location& from_here, |
| 26 const base::Closure& closure, | 27 const base::Closure& closure, |
| 27 int64 delay_ms) { | 28 int64 delay_ms) { |
| 28 if (!canceled_) { | 29 if (!canceled_) { |
| 29 message_loop_->PostDelayedTask(from_here, Wrap(closure), delay_ms); | 30 task_runner_->PostDelayedTask(from_here, Wrap(closure), delay_ms); |
| 30 } | 31 } |
| 31 } | 32 } |
| 32 | 33 |
| 33 void Detach() { | 34 void Detach() { |
| 34 DCHECK(message_loop_->BelongsToCurrentThread()); | 35 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 35 canceled_ = true; | 36 canceled_ = true; |
| 36 } | 37 } |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 friend class base::RefCountedThreadSafe<Core>; | 40 friend class base::RefCountedThreadSafe<Core>; |
| 40 | 41 |
| 41 ~Core() { | 42 ~Core() { |
| 42 DCHECK(canceled_); | 43 DCHECK(canceled_); |
| 43 } | 44 } |
| 44 | 45 |
| 45 base::Closure Wrap(const base::Closure& closure) { | 46 base::Closure Wrap(const base::Closure& closure) { |
| 46 return base::Bind(&Core::CallClosure, this, closure); | 47 return base::Bind(&Core::CallClosure, this, closure); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void CallClosure(const base::Closure& closure) { | 50 void CallClosure(const base::Closure& closure) { |
| 50 DCHECK(message_loop_->BelongsToCurrentThread()); | 51 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 51 | 52 |
| 52 if (!canceled_) | 53 if (!canceled_) |
| 53 closure.Run(); | 54 closure.Run(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 scoped_refptr<base::MessageLoopProxy> message_loop_; | 57 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 57 bool canceled_; | 58 bool canceled_; |
| 58 | 59 |
| 59 DISALLOW_COPY_AND_ASSIGN(Core); | 60 DISALLOW_COPY_AND_ASSIGN(Core); |
| 60 }; | 61 }; |
| 61 | 62 |
| 62 ScopedThreadProxy::ScopedThreadProxy(base::MessageLoopProxy* message_loop) | 63 ScopedThreadProxy::ScopedThreadProxy(base::SingleThreadTaskRunner* task_runner) |
| 63 : core_(new Core(message_loop)) { | 64 : core_(new Core(task_runner)) { |
| 64 } | 65 } |
| 65 | 66 |
| 66 ScopedThreadProxy::~ScopedThreadProxy() { | 67 ScopedThreadProxy::~ScopedThreadProxy() { |
| 67 Detach(); | 68 Detach(); |
| 68 } | 69 } |
| 69 | 70 |
| 70 void ScopedThreadProxy::PostTask(const tracked_objects::Location& from_here, | 71 void ScopedThreadProxy::PostTask(const tracked_objects::Location& from_here, |
| 71 const base::Closure& closure) { | 72 const base::Closure& closure) { |
| 72 core_->PostTask(from_here, closure); | 73 core_->PostTask(from_here, closure); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void ScopedThreadProxy::PostDelayedTask( | 76 void ScopedThreadProxy::PostDelayedTask( |
| 76 const tracked_objects::Location& from_here, | 77 const tracked_objects::Location& from_here, |
| 77 const base::Closure& closure, | 78 const base::Closure& closure, |
| 78 int64 delay_ms) { | 79 int64 delay_ms) { |
| 79 core_->PostDelayedTask(from_here, closure, delay_ms); | 80 core_->PostDelayedTask(from_here, closure, delay_ms); |
| 80 } | 81 } |
| 81 | 82 |
| 82 void ScopedThreadProxy::Detach() { | 83 void ScopedThreadProxy::Detach() { |
| 83 core_->Detach(); | 84 core_->Detach(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 } // namespace remoting | 87 } // namespace remoting |
| OLD | NEW |