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
3 // found in the LICENSE file.
5 #include "ppapi/shared_impl/thread_aware_callback.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "ppapi/shared_impl/ppapi_globals.h"
10 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
15 class ThreadAwareCallbackBase::Core
: public base::RefCountedThreadSafe
<Core
> {
17 Core() : aborted_(false) {
20 void MarkAsAborted() { aborted_
= true; }
22 void RunIfNotAborted(const base::Closure
& closure
) {
24 CallWhileUnlocked(closure
);
28 friend class base::RefCountedThreadSafe
<Core
>;
35 ThreadAwareCallbackBase::ThreadAwareCallbackBase()
36 : target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
38 DCHECK(target_loop_
.get());
41 ThreadAwareCallbackBase::~ThreadAwareCallbackBase() {
42 core_
->MarkAsAborted();
46 bool ThreadAwareCallbackBase::HasTargetLoop() {
47 return !!PpapiGlobals::Get()->GetCurrentMessageLoop();
50 void ThreadAwareCallbackBase::InternalRunOnTargetThread(
51 const base::Closure
& closure
) {
52 if (target_loop_
.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
53 target_loop_
->PostClosure(
55 RunWhileLocked(base::Bind(&Core::RunIfNotAborted
, core_
, closure
)),
58 CallWhileUnlocked(closure
);
62 } // namespace internal