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/tracked_callback.h"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/synchronization/lock.h"
12 #include "ppapi/c/pp_completion_callback.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/c/ppb_message_loop.h"
15 #include "ppapi/shared_impl/callback_tracker.h"
16 #include "ppapi/shared_impl/ppapi_globals.h"
17 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
18 #include "ppapi/shared_impl/proxy_lock.h"
19 #include "ppapi/shared_impl/resource.h"
26 return PpapiGlobals::Get()
27 ->GetMainThreadMessageLoop()
28 ->BelongsToCurrentThread();
31 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task
,
33 int32_t task_result
= completion_task
.Run(result
);
34 if (result
!= PP_ERROR_ABORTED
)
41 // TrackedCallback -------------------------------------------------------------
43 // Note: don't keep a Resource* since it may go out of scope before us.
44 TrackedCallback::TrackedCallback(Resource
* resource
,
45 const PP_CompletionCallback
& callback
)
46 : is_scheduled_(false),
47 resource_id_(resource
? resource
->pp_resource() : 0),
51 target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
52 result_for_blocked_callback_(PP_OK
) {
53 // Note that target_loop_ may be NULL at this point, if the plugin has not
54 // attached a loop to this thread, or if this is an in-process plugin.
55 // The Enter class should handle checking this for us.
57 // TODO(dmichael): Add tracking at the instance level, for callbacks that only
58 // have an instance (e.g. for MouseLock).
60 tracker_
= PpapiGlobals::Get()->GetCallbackTrackerForInstance(
61 resource
->pp_instance());
62 tracker_
->Add(make_scoped_refptr(this));
65 base::Lock
* proxy_lock
= ProxyLock::Get();
67 // If the proxy_lock is valid, we're running out-of-process, and locking
70 // This is a blocking completion callback, so we will need a condition
71 // variable for blocking & signalling the calling thread.
72 operation_completed_condvar_
.reset(
73 new base::ConditionVariable(proxy_lock
));
75 // It's a non-blocking callback, so we should have a MessageLoopResource
76 // to dispatch to. Note that we don't error check here, though. Later,
77 // EnterResource::SetResult will check to make sure the callback is valid
78 // and take appropriate action.
83 TrackedCallback::~TrackedCallback() {}
85 void TrackedCallback::Abort() { Run(PP_ERROR_ABORTED
); }
87 void TrackedCallback::PostAbort() { PostRun(PP_ERROR_ABORTED
); }
89 void TrackedCallback::Run(int32_t result
) {
90 // Only allow the callback to be run once. Note that this also covers the case
91 // where the callback was previously Aborted because its associated Resource
92 // went away. The callback may live on for a while because of a reference from
93 // a Closure. But when the Closure runs, Run() quietly does nothing, and the
94 // callback will go away when all referring Closures go away.
97 if (result
== PP_ERROR_ABORTED
)
100 // Note that this call of Run() may have been scheduled prior to Abort() or
101 // PostAbort() being called. If we have been told to Abort, that always
102 // trumps a result that was scheduled before, so we should make sure to pass
105 result
= PP_ERROR_ABORTED
;
108 // If the condition variable is invalid, there are two possibilities. One,
109 // we're running in-process, in which case the call should have come in on
110 // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD
111 // well before this. Otherwise, this callback was not created as a
112 // blocking callback. Either way, there's some internal error.
113 if (!operation_completed_condvar_
.get()) {
117 result_for_blocked_callback_
= result
;
118 // Retain ourselves, since MarkAsCompleted will remove us from the
119 // tracker. Then MarkAsCompleted before waking up the blocked thread,
120 // which could potentially re-enter.
121 scoped_refptr
<TrackedCallback
> thiz(this);
123 // Wake up the blocked thread. See BlockUntilComplete for where the thread
125 operation_completed_condvar_
->Signal();
127 // If there's a target_loop_, and we're not on the right thread, we need to
128 // post to target_loop_.
129 if (target_loop_
.get() &&
130 target_loop_
.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
135 // Copy callback fields now, since |MarkAsCompleted()| may delete us.
136 PP_CompletionCallback callback
= callback_
;
137 CompletionTask completion_task
= completion_task_
;
138 completion_task_
.Reset();
139 // Do this before running the callback in case of reentrancy from running
140 // the completion task.
143 if (!completion_task
.is_null())
144 result
= RunCompletionTask(completion_task
, result
);
146 // TODO(dmichael): Associate a message loop with the callback; if it's not
147 // the same as the current thread's loop, then post it to the right loop.
148 CallWhileUnlocked(PP_RunCompletionCallback
, &callback
, result
);
152 void TrackedCallback::PostRun(int32_t result
) {
157 if (result
== PP_ERROR_ABORTED
)
159 // We might abort when there's already a scheduled callback, but callers
160 // should never try to PostRun more than once otherwise.
161 DCHECK(result
== PP_ERROR_ABORTED
|| !is_scheduled_
);
164 // We might not have a MessageLoop to post to, so we must call Run()
168 base::Closure
callback_closure(
169 RunWhileLocked(base::Bind(&TrackedCallback::Run
, this, result
)));
171 target_loop_
->PostClosure(FROM_HERE
, callback_closure
, 0);
173 // We must be running in-process and on the main thread (the Enter
174 // classes protect against having a null target_loop_ otherwise).
175 DCHECK(IsMainThread());
176 DCHECK(PpapiGlobals::Get()->IsHostGlobals());
177 base::MessageLoop::current()->PostTask(FROM_HERE
, callback_closure
);
180 is_scheduled_
= true;
183 void TrackedCallback::set_completion_task(
184 const CompletionTask
& completion_task
) {
185 DCHECK(completion_task_
.is_null());
186 completion_task_
= completion_task
;
190 bool TrackedCallback::IsPending(
191 const scoped_refptr
<TrackedCallback
>& callback
) {
194 if (callback
->aborted())
196 return !callback
->completed();
200 bool TrackedCallback::IsScheduledToRun(
201 const scoped_refptr
<TrackedCallback
>& callback
) {
202 return IsPending(callback
) && callback
->is_scheduled_
;
205 int32_t TrackedCallback::BlockUntilComplete() {
206 // Note, we are already holding the proxy lock in all these methods, including
207 // this one (see ppapi/thunk/enter.cc for where it gets acquired).
209 // It doesn't make sense to wait on a non-blocking callback. Furthermore,
210 // BlockUntilComplete should never be called for in-process plugins, where
211 // blocking callbacks are not supported.
212 CHECK(operation_completed_condvar_
.get());
213 if (!is_blocking() || !operation_completed_condvar_
.get()) {
215 return PP_ERROR_FAILED
;
219 operation_completed_condvar_
->Wait();
221 if (!completion_task_
.is_null()) {
222 result_for_blocked_callback_
=
223 RunCompletionTask(completion_task_
, result_for_blocked_callback_
);
224 completion_task_
.Reset();
226 return result_for_blocked_callback_
;
229 void TrackedCallback::MarkAsCompleted() {
230 DCHECK(!completed());
232 // We will be removed; maintain a reference to ensure we won't be deleted
234 scoped_refptr
<TrackedCallback
> thiz
= this;
236 // We may not have a valid resource, in which case we're not in the tracker.
238 tracker_
->Remove(thiz
);