Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / ppapi / shared_impl / tracked_callback.cc
blobc032a503ee8080d7ecc5fd79197543349a471692
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"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/synchronization/lock.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "ppapi/c/pp_completion_callback.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/ppb_message_loop.h"
17 #include "ppapi/shared_impl/callback_tracker.h"
18 #include "ppapi/shared_impl/ppapi_globals.h"
19 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
20 #include "ppapi/shared_impl/proxy_lock.h"
21 #include "ppapi/shared_impl/resource.h"
23 namespace ppapi {
25 namespace {
27 bool IsMainThread() {
28 return PpapiGlobals::Get()
29 ->GetMainThreadMessageLoop()
30 ->BelongsToCurrentThread();
33 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task,
34 int32_t result) {
35 ProxyLock::AssertAcquired();
36 int32_t task_result = completion_task.Run(result);
37 if (result != PP_ERROR_ABORTED)
38 result = task_result;
39 return result;
42 } // namespace
44 // TrackedCallback -------------------------------------------------------------
46 // Note: don't keep a Resource* since it may go out of scope before us.
47 TrackedCallback::TrackedCallback(Resource* resource,
48 const PP_CompletionCallback& callback)
49 : is_scheduled_(false),
50 resource_id_(resource ? resource->pp_resource() : 0),
51 completed_(false),
52 aborted_(false),
53 callback_(callback),
54 target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
55 result_for_blocked_callback_(PP_OK) {
56 // Note that target_loop_ may be NULL at this point, if the plugin has not
57 // attached a loop to this thread, or if this is an in-process plugin.
58 // The Enter class should handle checking this for us.
60 // TODO(dmichael): Add tracking at the instance level, for callbacks that only
61 // have an instance (e.g. for MouseLock).
62 if (resource) {
63 tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance(
64 resource->pp_instance());
65 tracker_->Add(make_scoped_refptr(this));
68 base::Lock* proxy_lock = ProxyLock::Get();
69 if (proxy_lock) {
70 ProxyLock::AssertAcquired();
71 // If the proxy_lock is valid, we're running out-of-process, and locking
72 // is enabled.
73 if (is_blocking()) {
74 // This is a blocking completion callback, so we will need a condition
75 // variable for blocking & signalling the calling thread.
76 operation_completed_condvar_.reset(
77 new base::ConditionVariable(proxy_lock));
78 } else {
79 // It's a non-blocking callback, so we should have a MessageLoopResource
80 // to dispatch to. Note that we don't error check here, though. Later,
81 // EnterResource::SetResult will check to make sure the callback is valid
82 // and take appropriate action.
87 TrackedCallback::~TrackedCallback() {}
89 void TrackedCallback::Abort() {
90 Run(PP_ERROR_ABORTED);
93 void TrackedCallback::PostAbort() {
94 PostRun(PP_ERROR_ABORTED);
97 void TrackedCallback::Run(int32_t result) {
98 // Retain ourselves, since SignalBlockingCallback and MarkAsCompleted might
99 // otherwise cause |this| to be deleted. Do this before acquiring lock_ so
100 // that |this| is definitely valid at the time we release |lock_|.
101 scoped_refptr<TrackedCallback> thiz(this);
102 base::AutoLock acquire(lock_);
103 // Only allow the callback to be run once. Note that this also covers the case
104 // where the callback was previously Aborted because its associated Resource
105 // went away. The callback may live on for a while because of a reference from
106 // a Closure. But when the Closure runs, Run() quietly does nothing, and the
107 // callback will go away when all referring Closures go away.
108 if (completed_)
109 return;
110 if (result == PP_ERROR_ABORTED)
111 aborted_ = true;
113 // Note that this call of Run() may have been scheduled prior to Abort() or
114 // PostAbort() being called. If we have been told to Abort, that always
115 // trumps a result that was scheduled before, so we should make sure to pass
116 // PP_ERROR_ABORTED.
117 if (aborted_)
118 result = PP_ERROR_ABORTED;
120 if (is_blocking()) {
121 // This is a blocking callback; signal the condvar to wake up the thread.
122 SignalBlockingCallback(result);
123 } else {
124 // If there's a target_loop_, and we're not on the right thread, we need to
125 // post to target_loop_.
126 if (target_loop_ &&
127 target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
128 PostRunWithLock(result);
129 return;
131 // Do this before running the callback in case of reentrancy from running
132 // the completion callback.
133 MarkAsCompletedWithLock();
135 if (!completion_task_.is_null())
136 result = RunCompletionTask(completion_task_, result);
139 base::AutoUnlock release(lock_);
140 // Call the callback without lock_ and without the ProxyLock.
141 CallWhileUnlocked(PP_RunCompletionCallback, &callback_, result);
146 void TrackedCallback::PostRun(int32_t result) {
147 base::AutoLock acquire(lock_);
148 PostRunWithLock(result);
151 void TrackedCallback::set_completion_task(
152 const CompletionTask& completion_task) {
153 base::AutoLock acquire(lock_);
154 DCHECK(completion_task_.is_null());
155 completion_task_ = completion_task;
158 // static
159 bool TrackedCallback::IsPending(
160 const scoped_refptr<TrackedCallback>& callback) {
161 if (!callback)
162 return false;
163 base::AutoLock acquire(callback->lock_);
164 if (callback->aborted_)
165 return false;
166 return !callback->completed_;
169 // static
170 bool TrackedCallback::IsScheduledToRun(
171 const scoped_refptr<TrackedCallback>& callback) {
172 if (!callback)
173 return false;
174 base::AutoLock acquire(callback->lock_);
175 if (callback->aborted_)
176 return false;
177 return !callback->completed_ && callback->is_scheduled_;
180 int32_t TrackedCallback::BlockUntilComplete() {
181 // Note, we are already holding the proxy lock in this method and many others
182 // (see ppapi/thunk/enter.cc for where it gets acquired).
183 ProxyLock::AssertAcquired();
184 base::AutoLock acquire(lock_);
186 // It doesn't make sense to wait on a non-blocking callback. Furthermore,
187 // BlockUntilComplete should never be called for in-process plugins, where
188 // blocking callbacks are not supported.
189 CHECK(is_blocking() && operation_completed_condvar_);
191 // Protect us from being deleted to ensure operation_completed_condvar_ is
192 // available to wait on when we drop our lock.
193 scoped_refptr<TrackedCallback> thiz(this);
194 while (!completed_) {
195 // Unlock our lock temporarily; any thread that tries to signal us will need
196 // the lock.
197 lock_.Release();
198 operation_completed_condvar_->Wait();
199 // Note that the condvar releases the ProxyLock during Wait and re-acquires
200 // the ProxyLock when it's signaled. We reacquire lock_ immediately after,
201 // preserving lock order.
202 ProxyLock::AssertAcquired();
203 lock_.Acquire();
206 if (!completion_task_.is_null()) {
207 result_for_blocked_callback_ =
208 RunCompletionTask(completion_task_, result_for_blocked_callback_);
209 completion_task_.Reset();
211 return result_for_blocked_callback_;
214 void TrackedCallback::MarkAsCompleted() {
215 base::AutoLock acquire(lock_);
216 MarkAsCompletedWithLock();
219 void TrackedCallback::MarkAsCompletedWithLock() {
220 lock_.AssertAcquired();
221 DCHECK(!completed_);
223 // We will be removed; maintain a reference to ensure we won't be deleted
224 // until we're done.
225 scoped_refptr<TrackedCallback> thiz = this;
226 completed_ = true;
227 // We may not have a valid resource, in which case we're not in the tracker.
228 if (resource_id_)
229 tracker_->Remove(thiz);
230 tracker_ = NULL;
231 target_loop_ = NULL;
234 void TrackedCallback::PostRunWithLock(int32_t result) {
235 lock_.AssertAcquired();
236 if (completed_) {
237 NOTREACHED();
238 return;
240 if (result == PP_ERROR_ABORTED)
241 aborted_ = true;
242 // We might abort when there's already a scheduled callback, but callers
243 // should never try to PostRun more than once otherwise.
244 DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_);
246 if (is_blocking()) {
247 // We might not have a MessageLoop to post to, so we must Signal
248 // directly.
249 SignalBlockingCallback(result);
250 } else {
251 base::Closure callback_closure(
252 RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result)));
253 if (target_loop_) {
254 target_loop_->PostClosure(FROM_HERE, callback_closure, 0);
255 } else {
256 // We must be running in-process and on the main thread (the Enter
257 // classes protect against having a null target_loop_ otherwise).
258 DCHECK(IsMainThread());
259 DCHECK(PpapiGlobals::Get()->IsHostGlobals());
260 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
261 callback_closure);
264 is_scheduled_ = true;
267 void TrackedCallback::SignalBlockingCallback(int32_t result) {
268 lock_.AssertAcquired();
269 DCHECK(is_blocking());
270 if (!operation_completed_condvar_) {
271 // If the condition variable is invalid, there are two possibilities. One,
272 // we're running in-process, in which case the call should have come in on
273 // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD
274 // well before this. Otherwise, this callback was not created as a
275 // blocking callback. Either way, there's some internal error.
276 NOTREACHED();
277 return;
279 result_for_blocked_callback_ = result;
280 // Retain ourselves, since MarkAsCompleted will remove us from the
281 // tracker. Then MarkAsCompleted before waking up the blocked thread,
282 // which could potentially re-enter.
283 scoped_refptr<TrackedCallback> thiz(this);
284 MarkAsCompletedWithLock();
285 // Wake up the blocked thread. See BlockUntilComplete for where the thread
286 // Wait()s.
287 operation_completed_condvar_->Signal();
290 } // namespace ppapi