Autofill: Add WalletIntegrationAvailable() to components.
[chromium-blink-merge.git] / ppapi / shared_impl / tracked_callback.cc
blobda0da429f73b7dde4f207de8d38971d1d9fc9329
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/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"
21 namespace ppapi {
23 namespace {
25 bool IsMainThread() {
26 return PpapiGlobals::Get()
27 ->GetMainThreadMessageLoop()
28 ->BelongsToCurrentThread();
31 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task,
32 int32_t result) {
33 ProxyLock::AssertAcquired();
34 int32_t task_result = completion_task.Run(result);
35 if (result != PP_ERROR_ABORTED)
36 result = task_result;
37 return result;
40 } // namespace
42 // TrackedCallback -------------------------------------------------------------
44 // Note: don't keep a Resource* since it may go out of scope before us.
45 TrackedCallback::TrackedCallback(Resource* resource,
46 const PP_CompletionCallback& callback)
47 : is_scheduled_(false),
48 resource_id_(resource ? resource->pp_resource() : 0),
49 completed_(false),
50 aborted_(false),
51 callback_(callback),
52 target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
53 result_for_blocked_callback_(PP_OK) {
54 // Note that target_loop_ may be NULL at this point, if the plugin has not
55 // attached a loop to this thread, or if this is an in-process plugin.
56 // The Enter class should handle checking this for us.
58 // TODO(dmichael): Add tracking at the instance level, for callbacks that only
59 // have an instance (e.g. for MouseLock).
60 if (resource) {
61 tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance(
62 resource->pp_instance());
63 tracker_->Add(make_scoped_refptr(this));
66 base::Lock* proxy_lock = ProxyLock::Get();
67 if (proxy_lock) {
68 ProxyLock::AssertAcquired();
69 // If the proxy_lock is valid, we're running out-of-process, and locking
70 // is enabled.
71 if (is_blocking()) {
72 // This is a blocking completion callback, so we will need a condition
73 // variable for blocking & signalling the calling thread.
74 operation_completed_condvar_.reset(
75 new base::ConditionVariable(proxy_lock));
76 } else {
77 // It's a non-blocking callback, so we should have a MessageLoopResource
78 // to dispatch to. Note that we don't error check here, though. Later,
79 // EnterResource::SetResult will check to make sure the callback is valid
80 // and take appropriate action.
85 TrackedCallback::~TrackedCallback() {}
87 void TrackedCallback::Abort() {
88 Run(PP_ERROR_ABORTED);
91 void TrackedCallback::PostAbort() {
92 PostRun(PP_ERROR_ABORTED);
95 void TrackedCallback::Run(int32_t result) {
96 // Retain ourselves, since SignalBlockingCallback and MarkAsCompleted might
97 // otherwise cause |this| to be deleted. Do this before acquiring lock_ so
98 // that |this| is definitely valid at the time we release |lock_|.
99 scoped_refptr<TrackedCallback> thiz(this);
100 base::AutoLock acquire(lock_);
101 // Only allow the callback to be run once. Note that this also covers the case
102 // where the callback was previously Aborted because its associated Resource
103 // went away. The callback may live on for a while because of a reference from
104 // a Closure. But when the Closure runs, Run() quietly does nothing, and the
105 // callback will go away when all referring Closures go away.
106 if (completed_)
107 return;
108 if (result == PP_ERROR_ABORTED)
109 aborted_ = true;
111 // Note that this call of Run() may have been scheduled prior to Abort() or
112 // PostAbort() being called. If we have been told to Abort, that always
113 // trumps a result that was scheduled before, so we should make sure to pass
114 // PP_ERROR_ABORTED.
115 if (aborted_)
116 result = PP_ERROR_ABORTED;
118 if (is_blocking()) {
119 // This is a blocking callback; signal the condvar to wake up the thread.
120 SignalBlockingCallback(result);
121 } else {
122 // If there's a target_loop_, and we're not on the right thread, we need to
123 // post to target_loop_.
124 if (target_loop_ &&
125 target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
126 PostRunWithLock(result);
127 return;
129 // Do this before running the callback in case of reentrancy from running
130 // the completion callback.
131 MarkAsCompletedWithLock();
133 if (!completion_task_.is_null())
134 result = RunCompletionTask(completion_task_, result);
137 base::AutoUnlock release(lock_);
138 // Call the callback without lock_ and without the ProxyLock.
139 CallWhileUnlocked(PP_RunCompletionCallback, &callback_, result);
144 void TrackedCallback::PostRun(int32_t result) {
145 base::AutoLock acquire(lock_);
146 PostRunWithLock(result);
149 void TrackedCallback::set_completion_task(
150 const CompletionTask& completion_task) {
151 base::AutoLock acquire(lock_);
152 DCHECK(completion_task_.is_null());
153 completion_task_ = completion_task;
156 // static
157 bool TrackedCallback::IsPending(
158 const scoped_refptr<TrackedCallback>& callback) {
159 if (!callback)
160 return false;
161 base::AutoLock acquire(callback->lock_);
162 if (callback->aborted_)
163 return false;
164 return !callback->completed_;
167 // static
168 bool TrackedCallback::IsScheduledToRun(
169 const scoped_refptr<TrackedCallback>& callback) {
170 if (!callback)
171 return false;
172 base::AutoLock acquire(callback->lock_);
173 if (callback->aborted_)
174 return false;
175 return !callback->completed_ && callback->is_scheduled_;
178 int32_t TrackedCallback::BlockUntilComplete() {
179 // Note, we are already holding the proxy lock in this method and many others
180 // (see ppapi/thunk/enter.cc for where it gets acquired).
181 ProxyLock::AssertAcquired();
182 base::AutoLock acquire(lock_);
184 // It doesn't make sense to wait on a non-blocking callback. Furthermore,
185 // BlockUntilComplete should never be called for in-process plugins, where
186 // blocking callbacks are not supported.
187 CHECK(is_blocking() && operation_completed_condvar_);
189 // Protect us from being deleted to ensure operation_completed_condvar_ is
190 // available to wait on when we drop our lock.
191 scoped_refptr<TrackedCallback> thiz(this);
192 while (!completed_) {
193 // Unlock our lock temporarily; any thread that tries to signal us will need
194 // the lock.
195 lock_.Release();
196 operation_completed_condvar_->Wait();
197 // Note that the condvar releases the ProxyLock during Wait and re-acquires
198 // the ProxyLock when it's signaled. We reacquire lock_ immediately after,
199 // preserving lock order.
200 ProxyLock::AssertAcquired();
201 lock_.Acquire();
204 if (!completion_task_.is_null()) {
205 result_for_blocked_callback_ =
206 RunCompletionTask(completion_task_, result_for_blocked_callback_);
207 completion_task_.Reset();
209 return result_for_blocked_callback_;
212 void TrackedCallback::MarkAsCompleted() {
213 base::AutoLock acquire(lock_);
214 MarkAsCompletedWithLock();
217 void TrackedCallback::MarkAsCompletedWithLock() {
218 lock_.AssertAcquired();
219 DCHECK(!completed_);
221 // We will be removed; maintain a reference to ensure we won't be deleted
222 // until we're done.
223 scoped_refptr<TrackedCallback> thiz = this;
224 completed_ = true;
225 // We may not have a valid resource, in which case we're not in the tracker.
226 if (resource_id_)
227 tracker_->Remove(thiz);
228 tracker_ = NULL;
229 target_loop_ = NULL;
232 void TrackedCallback::PostRunWithLock(int32_t result) {
233 lock_.AssertAcquired();
234 if (completed_) {
235 NOTREACHED();
236 return;
238 if (result == PP_ERROR_ABORTED)
239 aborted_ = true;
240 // We might abort when there's already a scheduled callback, but callers
241 // should never try to PostRun more than once otherwise.
242 DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_);
244 if (is_blocking()) {
245 // We might not have a MessageLoop to post to, so we must Signal
246 // directly.
247 SignalBlockingCallback(result);
248 } else {
249 base::Closure callback_closure(
250 RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result)));
251 if (target_loop_) {
252 target_loop_->PostClosure(FROM_HERE, callback_closure, 0);
253 } else {
254 // We must be running in-process and on the main thread (the Enter
255 // classes protect against having a null target_loop_ otherwise).
256 DCHECK(IsMainThread());
257 DCHECK(PpapiGlobals::Get()->IsHostGlobals());
258 base::MessageLoop::current()->PostTask(FROM_HERE, callback_closure);
261 is_scheduled_ = true;
264 void TrackedCallback::SignalBlockingCallback(int32_t result) {
265 lock_.AssertAcquired();
266 DCHECK(is_blocking());
267 if (!operation_completed_condvar_) {
268 // If the condition variable is invalid, there are two possibilities. One,
269 // we're running in-process, in which case the call should have come in on
270 // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD
271 // well before this. Otherwise, this callback was not created as a
272 // blocking callback. Either way, there's some internal error.
273 NOTREACHED();
274 return;
276 result_for_blocked_callback_ = result;
277 // Retain ourselves, since MarkAsCompleted will remove us from the
278 // tracker. Then MarkAsCompleted before waking up the blocked thread,
279 // which could potentially re-enter.
280 scoped_refptr<TrackedCallback> thiz(this);
281 MarkAsCompletedWithLock();
282 // Wake up the blocked thread. See BlockUntilComplete for where the thread
283 // Wait()s.
284 operation_completed_condvar_->Signal();
287 } // namespace ppapi