tracked_callback.cc revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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/tracked_callback.h"
6
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"
20
21namespace ppapi {
22
23namespace {
24
25bool IsMainThread() {
26  return
27      PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread();
28}
29
30int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task,
31                          int32_t result) {
32  int32_t task_result = completion_task.Run(result);
33  if (result != PP_ERROR_ABORTED)
34    result = task_result;
35  return result;
36}
37
38}  // namespace
39
40// TrackedCallback -------------------------------------------------------------
41
42// Note: don't keep a Resource* since it may go out of scope before us.
43TrackedCallback::TrackedCallback(
44    Resource* resource,
45    const PP_CompletionCallback& callback)
46    : is_scheduled_(false),
47      resource_id_(resource ? resource->pp_resource() : 0),
48      completed_(false),
49      aborted_(false),
50      callback_(callback),
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.
56
57  // TODO(dmichael): Add tracking at the instance level, for callbacks that only
58  // have an instance (e.g. for MouseLock).
59  if (resource) {
60    tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance(
61        resource->pp_instance());
62    tracker_->Add(make_scoped_refptr(this));
63  }
64
65  base::Lock* proxy_lock = PpapiGlobals::Get()->GetProxyLock();
66  if (proxy_lock) {
67    // If the proxy_lock is valid, we're running out-of-process, and locking
68    // is enabled.
69    if (is_blocking()) {
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));
74    } else {
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.
79    }
80  }
81}
82
83TrackedCallback::~TrackedCallback() {
84}
85
86void TrackedCallback::Abort() {
87  Run(PP_ERROR_ABORTED);
88}
89
90void TrackedCallback::PostAbort() {
91  PostRun(PP_ERROR_ABORTED);
92}
93
94void TrackedCallback::Run(int32_t result) {
95  // Only allow the callback to be run once. Note that this also covers the case
96  // where the callback was previously Aborted because its associated Resource
97  // went away. The callback may live on for a while because of a reference from
98  // a Closure. But when the Closure runs, Run() quietly does nothing, and the
99  // callback will go away when all referring Closures go away.
100  if (completed())
101    return;
102  if (result == PP_ERROR_ABORTED)
103    aborted_ = true;
104
105  // Note that this call of Run() may have been scheduled prior to Abort() or
106  // PostAbort() being called. If we have been told to Abort, that always
107  // trumps a result that was scheduled before, so we should make sure to pass
108  // PP_ERROR_ABORTED.
109  if (aborted())
110    result = PP_ERROR_ABORTED;
111
112  if (is_blocking()) {
113    // If the condition variable is invalid, there are two possibilities. One,
114    // we're running in-process, in which case the call should have come in on
115    // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD
116    // well before this. Otherwise, this callback was not created as a
117    // blocking callback. Either way, there's some internal error.
118    if (!operation_completed_condvar_.get()) {
119      NOTREACHED();
120      return;
121    }
122    result_for_blocked_callback_ = result;
123    // Retain ourselves, since MarkAsCompleted will remove us from the
124    // tracker. Then MarkAsCompleted before waking up the blocked thread,
125    // which could potentially re-enter.
126    scoped_refptr<TrackedCallback> thiz(this);
127    MarkAsCompleted();
128    // Wake up the blocked thread. See BlockUntilComplete for where the thread
129    // Wait()s.
130    operation_completed_condvar_->Signal();
131  } else {
132    // If there's a target_loop_, and we're not on the right thread, we need to
133    // post to target_loop_.
134    if (target_loop_.get() &&
135        target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
136      PostRun(result);
137      return;
138    }
139
140    // Copy callback fields now, since |MarkAsCompleted()| may delete us.
141    PP_CompletionCallback callback = callback_;
142    CompletionTask completion_task = completion_task_;
143    completion_task_.Reset();
144    // Do this before running the callback in case of reentrancy from running
145    // the completion task.
146    MarkAsCompleted();
147
148    if (!completion_task.is_null())
149      result = RunCompletionTask(completion_task, result);
150
151    // TODO(dmichael): Associate a message loop with the callback; if it's not
152    // the same as the current thread's loop, then post it to the right loop.
153    CallWhileUnlocked(PP_RunCompletionCallback, &callback, result);
154  }
155}
156
157void TrackedCallback::PostRun(int32_t result) {
158  if (completed()) {
159    NOTREACHED();
160    return;
161  }
162  if (result == PP_ERROR_ABORTED)
163    aborted_ = true;
164  // We might abort when there's already a scheduled callback, but callers
165  // should never try to PostRun more than once otherwise.
166  DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_);
167
168  base::Closure callback_closure(
169      RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result)));
170  if (!target_loop_.get()) {
171    // We must be running in-process and on the main thread (the Enter
172    // classes protect against having a null target_loop_ otherwise).
173    DCHECK(IsMainThread());
174    DCHECK(PpapiGlobals::Get()->IsHostGlobals());
175    base::MessageLoop::current()->PostTask(FROM_HERE, callback_closure);
176  } else {
177    target_loop_->PostClosure(FROM_HERE, callback_closure, 0);
178  }
179  is_scheduled_ = true;
180}
181
182void TrackedCallback::set_completion_task(
183    const CompletionTask& completion_task) {
184  DCHECK(completion_task_.is_null());
185  completion_task_ = completion_task;
186}
187
188// static
189bool TrackedCallback::IsPending(
190    const scoped_refptr<TrackedCallback>& callback) {
191  if (!callback.get())
192    return false;
193  if (callback->aborted())
194    return false;
195  return !callback->completed();
196}
197
198// static
199bool TrackedCallback::IsScheduledToRun(
200    const scoped_refptr<TrackedCallback>& callback) {
201  return IsPending(callback) && callback->is_scheduled_;
202}
203
204int32_t TrackedCallback::BlockUntilComplete() {
205  // Note, we are already holding the proxy lock in all these methods, including
206  // this one (see ppapi/thunk/enter.cc for where it gets acquired).
207
208  // It doesn't make sense to wait on a non-blocking callback. Furthermore,
209  // BlockUntilComplete should never be called for in-process plugins, where
210  // blocking callbacks are not supported.
211  CHECK(operation_completed_condvar_.get());
212  if (!is_blocking() || !operation_completed_condvar_.get()) {
213    NOTREACHED();
214    return PP_ERROR_FAILED;
215  }
216
217  while (!completed())
218    operation_completed_condvar_->Wait();
219
220  if (!completion_task_.is_null()) {
221    result_for_blocked_callback_ =
222        RunCompletionTask(completion_task_, result_for_blocked_callback_);
223    completion_task_.Reset();
224  }
225  return result_for_blocked_callback_;
226}
227
228void TrackedCallback::MarkAsCompleted() {
229  DCHECK(!completed());
230
231  // We will be removed; maintain a reference to ensure we won't be deleted
232  // until we're done.
233  scoped_refptr<TrackedCallback> thiz = this;
234  completed_ = true;
235  // We may not have a valid resource, in which case we're not in the tracker.
236  if (resource_id_)
237    tracker_->Remove(thiz);
238  tracker_ = NULL;
239}
240
241}  // namespace ppapi
242