tracked_callback.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.h"
11#include "base/synchronization/lock.h"
12#include "ppapi/c/dev/ppb_message_loop_dev.h"
13#include "ppapi/c/pp_completion_callback.h"
14#include "ppapi/c/pp_errors.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
30}  // namespace
31
32// TrackedCallback -------------------------------------------------------------
33
34// Note: don't keep a Resource* since it may go out of scope before us.
35TrackedCallback::TrackedCallback(
36    Resource* resource,
37    const PP_CompletionCallback& callback)
38    : is_scheduled_(false),
39      resource_id_(resource ? resource->pp_resource() : 0),
40      completed_(false),
41      aborted_(false),
42      callback_(callback),
43      target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
44      result_for_blocked_callback_(PP_OK) {
45  // Note that target_loop_ may be NULL at this point, if the plugin has not
46  // attached a loop to this thread, or if this is an in-process plugin.
47  // The Enter class should handle checking this for us.
48
49  // TODO(dmichael): Add tracking at the instance level, for callbacks that only
50  // have an instance (e.g. for MouseLock).
51  if (resource) {
52    tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance(
53        resource->pp_instance());
54    tracker_->Add(make_scoped_refptr(this));
55  }
56
57  base::Lock* proxy_lock = PpapiGlobals::Get()->GetProxyLock();
58  if (proxy_lock) {
59    // If the proxy_lock is valid, we're running out-of-process, and locking
60    // is enabled.
61    if (is_blocking()) {
62      // This is a blocking completion callback, so we will need a condition
63      // variable for blocking & signalling the calling thread.
64      operation_completed_condvar_.reset(
65          new base::ConditionVariable(proxy_lock));
66    } else {
67      // It's a non-blocking callback, so we should have a MessageLoopResource
68      // to dispatch to. Note that we don't error check here, though. Later,
69      // EnterResource::SetResult will check to make sure the callback is valid
70      // and take appropriate action.
71    }
72  }
73}
74
75TrackedCallback::~TrackedCallback() {
76}
77
78void TrackedCallback::Abort() {
79  Run(PP_ERROR_ABORTED);
80}
81
82void TrackedCallback::PostAbort() {
83  PostRun(PP_ERROR_ABORTED);
84}
85
86void TrackedCallback::Run(int32_t result) {
87  // Only allow the callback to be run once. Note that this also covers the case
88  // where the callback was previously Aborted because its associated Resource
89  // went away. The callback may live on for a while because of a reference from
90  // a Closure. But when the Closure runs, Run() quietly does nothing, and the
91  // callback will go away when all referring Closures go away.
92  if (completed())
93    return;
94  if (result == PP_ERROR_ABORTED)
95    aborted_ = true;
96
97  // Note that this call of Run() may have been scheduled prior to Abort() or
98  // PostAbort() being called. If we have been told to Abort, that always
99  // trumps a result that was scheduled before, so we should make sure to pass
100  // PP_ERROR_ABORTED.
101  if (aborted())
102    result = PP_ERROR_ABORTED;
103
104  if (is_blocking()) {
105    // If the condition variable is invalid, there are two possibilities. One,
106    // we're running in-process, in which case the call should have come in on
107    // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD
108    // well before this. Otherwise, this callback was not created as a
109    // blocking callback. Either way, there's some internal error.
110    if (!operation_completed_condvar_.get()) {
111      NOTREACHED();
112      return;
113    }
114    result_for_blocked_callback_ = result;
115    // Retain ourselves, since MarkAsCompleted will remove us from the
116    // tracker. Then MarkAsCompleted before waking up the blocked thread,
117    // which could potentially re-enter.
118    scoped_refptr<TrackedCallback> thiz(this);
119    MarkAsCompleted();
120    // Wake up the blocked thread. See BlockUntilComplete for where the thread
121    // Wait()s.
122    operation_completed_condvar_->Signal();
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_ != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
128      PostRun(result);
129      return;
130    }
131    // Copy |callback_| now, since |MarkAsCompleted()| may delete us.
132    PP_CompletionCallback callback = callback_;
133    // Do this before running the callback in case of reentrancy (which
134    // shouldn't happen, but avoid strange failures).
135    MarkAsCompleted();
136    // TODO(dmichael): Associate a message loop with the callback; if it's not
137    // the same as the current thread's loop, then post it to the right loop.
138    CallWhileUnlocked(PP_RunCompletionCallback, &callback, result);
139  }
140}
141
142void TrackedCallback::PostRun(int32_t result) {
143  if (completed()) {
144    NOTREACHED();
145    return;
146  }
147  if (result == PP_ERROR_ABORTED)
148    aborted_ = true;
149  // We might abort when there's already a scheduled callback, but callers
150  // should never try to PostRun more than once otherwise.
151  DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_);
152
153  base::Closure callback_closure(
154      RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result)));
155  if (!target_loop_) {
156    // We must be running in-process and on the main thread (the Enter
157    // classes protect against having a null target_loop_ otherwise).
158    DCHECK(IsMainThread());
159    DCHECK(PpapiGlobals::Get()->IsHostGlobals());
160    MessageLoop::current()->PostTask(FROM_HERE, callback_closure);
161  } else {
162    target_loop_->PostClosure(FROM_HERE, callback_closure, 0);
163  }
164  is_scheduled_ = true;
165}
166
167// static
168bool TrackedCallback::IsPending(
169    const scoped_refptr<TrackedCallback>& callback) {
170  if (!callback.get())
171    return false;
172  return !callback->completed();
173}
174
175int32_t TrackedCallback::BlockUntilComplete() {
176  // Note, we are already holding the proxy lock in all these methods, including
177  // this one (see ppapi/thunk/enter.cc for where it gets acquired).
178
179  // It doesn't make sense to wait on a non-blocking callback. Furthermore,
180  // BlockUntilComplete should never be called for in-process plugins, where
181  // blocking callbacks are not supported.
182  CHECK(operation_completed_condvar_.get());
183  if (!is_blocking() || !operation_completed_condvar_.get()) {
184    NOTREACHED();
185    return PP_ERROR_FAILED;
186  }
187
188  while (!completed())
189    operation_completed_condvar_->Wait();
190  return result_for_blocked_callback_;
191}
192
193void TrackedCallback::MarkAsCompleted() {
194  DCHECK(!completed());
195
196  // We will be removed; maintain a reference to ensure we won't be deleted
197  // until we're done.
198  scoped_refptr<TrackedCallback> thiz = this;
199  completed_ = true;
200  // We may not have a valid resource, in which case we're not in the tracker.
201  if (resource_id_)
202    tracker_->Remove(thiz);
203  tracker_ = NULL;
204}
205
206}  // namespace ppapi
207