Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: ppapi/shared_impl/thread_aware_callback.cc

Issue 11859015: Pepper: Introduce ThreadAwareCallback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes in response to David's suggestions Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "ppapi/shared_impl/thread_aware_callback.h"
6
7 #include "base/callback.h"
8 #include "ppapi/shared_impl/ppapi_globals.h"
9 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
10
11 namespace ppapi {
12 namespace internal {
13
14 class ThreadAwareCallbackBase::Core : public base::RefCountedThreadSafe<Core> {
15 public:
16 Core() : aborted_(false) {
17 }
18
19 void MarkAsAborted() { aborted_ = true; }
20
21 void RunIfNotAborted(const base::Closure& closure) {
22 // This method is a posted task and run while the proxy lock is not
23 // acquired, so we have to acquire the lock in order to access |aborted_|.
24 ProxyAutoLock auto_lock;
25
26 if (!aborted_)
27 CallWhileUnlocked(closure);
28 }
29
30 private:
31 friend class base::RefCountedThreadSafe<Core>;
32 ~Core() {
33 }
34
35 bool aborted_;
36 };
37
38 ThreadAwareCallbackBase::ThreadAwareCallbackBase()
39 : target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
40 core_(new Core()) {
41 }
42
43 ThreadAwareCallbackBase::~ThreadAwareCallbackBase() {
44 core_->MarkAsAborted();
45 }
46
47 bool ThreadAwareCallbackBase::ShouldPostToTargetLoop() {
48 return target_loop_.get() &&
49 target_loop_ != PpapiGlobals::Get()->GetCurrentMessageLoop();
50 }
51
52 void ThreadAwareCallbackBase::RunIfNotAborted(
53 const base::Closure& closure) {
54 target_loop_->PostClosure(FROM_HERE,
55 base::Bind(&Core::RunIfNotAborted, core_, closure),
56 0);
57 }
58
59 } // namespace internal
60 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698