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) {}
19 void MarkAsAborted() { aborted_
= true; }
21 void RunIfNotAborted(const base::Closure
& closure
) {
23 CallWhileUnlocked(closure
);
27 friend class base::RefCountedThreadSafe
<Core
>;
33 ThreadAwareCallbackBase::ThreadAwareCallbackBase()
34 : target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
36 DCHECK(target_loop_
.get());
39 ThreadAwareCallbackBase::~ThreadAwareCallbackBase() { core_
->MarkAsAborted(); }
42 bool ThreadAwareCallbackBase::HasTargetLoop() {
43 return !!PpapiGlobals::Get()->GetCurrentMessageLoop();
46 void ThreadAwareCallbackBase::InternalRunOnTargetThread(
47 const base::Closure
& closure
) {
48 if (target_loop_
.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
49 target_loop_
->PostClosure(
51 RunWhileLocked(base::Bind(&Core::RunIfNotAborted
, core_
, closure
)),
54 CallWhileUnlocked(closure
);
58 } // namespace internal