| 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 "ppapi/shared_impl/thread_aware_callback.h" | 5 #include "ppapi/shared_impl/thread_aware_callback.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "ppapi/shared_impl/ppapi_globals.h" | 9 #include "ppapi/shared_impl/ppapi_globals.h" |
| 10 #include "ppapi/shared_impl/ppb_message_loop_shared.h" | 10 #include "ppapi/shared_impl/ppb_message_loop_shared.h" |
| 11 | 11 |
| 12 namespace ppapi { | 12 namespace ppapi { |
| 13 namespace internal { | 13 namespace internal { |
| 14 | 14 |
| 15 class ThreadAwareCallbackBase::Core : public base::RefCountedThreadSafe<Core> { | 15 class ThreadAwareCallbackBase::Core : public base::RefCountedThreadSafe<Core> { |
| 16 public: | 16 public: |
| 17 Core() : aborted_(false) { | 17 Core() : aborted_(false) {} |
| 18 } | |
| 19 | 18 |
| 20 void MarkAsAborted() { aborted_ = true; } | 19 void MarkAsAborted() { aborted_ = true; } |
| 21 | 20 |
| 22 void RunIfNotAborted(const base::Closure& closure) { | 21 void RunIfNotAborted(const base::Closure& closure) { |
| 23 if (!aborted_) | 22 if (!aborted_) |
| 24 CallWhileUnlocked(closure); | 23 CallWhileUnlocked(closure); |
| 25 } | 24 } |
| 26 | 25 |
| 27 private: | 26 private: |
| 28 friend class base::RefCountedThreadSafe<Core>; | 27 friend class base::RefCountedThreadSafe<Core>; |
| 29 ~Core() { | 28 ~Core() {} |
| 30 } | |
| 31 | 29 |
| 32 bool aborted_; | 30 bool aborted_; |
| 33 }; | 31 }; |
| 34 | 32 |
| 35 ThreadAwareCallbackBase::ThreadAwareCallbackBase() | 33 ThreadAwareCallbackBase::ThreadAwareCallbackBase() |
| 36 : target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()), | 34 : target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()), |
| 37 core_(new Core()) { | 35 core_(new Core()) { |
| 38 DCHECK(target_loop_.get()); | 36 DCHECK(target_loop_.get()); |
| 39 } | 37 } |
| 40 | 38 |
| 41 ThreadAwareCallbackBase::~ThreadAwareCallbackBase() { | 39 ThreadAwareCallbackBase::~ThreadAwareCallbackBase() { core_->MarkAsAborted(); } |
| 42 core_->MarkAsAborted(); | |
| 43 } | |
| 44 | 40 |
| 45 // static | 41 // static |
| 46 bool ThreadAwareCallbackBase::HasTargetLoop() { | 42 bool ThreadAwareCallbackBase::HasTargetLoop() { |
| 47 return !!PpapiGlobals::Get()->GetCurrentMessageLoop(); | 43 return !!PpapiGlobals::Get()->GetCurrentMessageLoop(); |
| 48 } | 44 } |
| 49 | 45 |
| 50 void ThreadAwareCallbackBase::InternalRunOnTargetThread( | 46 void ThreadAwareCallbackBase::InternalRunOnTargetThread( |
| 51 const base::Closure& closure) { | 47 const base::Closure& closure) { |
| 52 if (target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) { | 48 if (target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) { |
| 53 target_loop_->PostClosure( | 49 target_loop_->PostClosure( |
| 54 FROM_HERE, | 50 FROM_HERE, |
| 55 RunWhileLocked(base::Bind(&Core::RunIfNotAborted, core_, closure)), | 51 RunWhileLocked(base::Bind(&Core::RunIfNotAborted, core_, closure)), |
| 56 0); | 52 0); |
| 57 } else { | 53 } else { |
| 58 CallWhileUnlocked(closure); | 54 CallWhileUnlocked(closure); |
| 59 } | 55 } |
| 60 } | 56 } |
| 61 | 57 |
| 62 } // namespace internal | 58 } // namespace internal |
| 63 } // namespace ppapi | 59 } // namespace ppapi |
| OLD | NEW |