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 = ProxyLock::Get();
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  if (is_blocking()) {
169    // We might not have a MessageLoop to post to, so we must call Run()
170    // directly.
171    Run(result);
172  } else {
173    base::Closure callback_closure(
174        RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result)));
175    if (target_loop_) {
176      target_loop_->PostClosure(FROM_HERE, callback_closure, 0);
177    } else {
178      // We must be running in-process and on the main thread (the Enter
179      // classes protect against having a null target_loop_ otherwise).
180      DCHECK(IsMainThread());
181      DCHECK(PpapiGlobals::Get()->IsHostGlobals());
182      base::MessageLoop::current()->PostTask(FROM_HERE, callback_closure);
183    }
184  }
185  is_scheduled_ = true;
186}
187
188void TrackedCallback::set_completion_task(
189    const CompletionTask& completion_task) {
190  DCHECK(completion_task_.is_null());
191  completion_task_ = completion_task;
192}
193
194// static
195bool TrackedCallback::IsPending(
196    const scoped_refptr<TrackedCallback>& callback) {
197  if (!callback.get())
198    return false;
199  if (callback->aborted())
200    return false;
201  return !callback->completed();
202}
203
204// static
205bool TrackedCallback::IsScheduledToRun(
206    const scoped_refptr<TrackedCallback>& callback) {
207  return IsPending(callback) && callback->is_scheduled_;
208}
209
210int32_t TrackedCallback::BlockUntilComplete() {
211  // Note, we are already holding the proxy lock in all these methods, including
212  // this one (see ppapi/thunk/enter.cc for where it gets acquired).
213
214  // It doesn't make sense to wait on a non-blocking callback. Furthermore,
215  // BlockUntilComplete should never be called for in-process plugins, where
216  // blocking callbacks are not supported.
217  CHECK(operation_completed_condvar_.get());
218  if (!is_blocking() || !operation_completed_condvar_.get()) {
219    NOTREACHED();
220    return PP_ERROR_FAILED;
221  }
222
223  while (!completed())
224    operation_completed_condvar_->Wait();
225
226  if (!completion_task_.is_null()) {
227    result_for_blocked_callback_ =
228        RunCompletionTask(completion_task_, result_for_blocked_callback_);
229    completion_task_.Reset();
230  }
231  return result_for_blocked_callback_;
232}
233
234void TrackedCallback::MarkAsCompleted() {
235  DCHECK(!completed());
236
237  // We will be removed; maintain a reference to ensure we won't be deleted
238  // until we're done.
239  scoped_refptr<TrackedCallback> thiz = this;
240  completed_ = true;
241  // We may not have a valid resource, in which case we're not in the tracker.
242  if (resource_id_)
243    tracker_->Remove(thiz);
244  tracker_ = NULL;
245}
246
247}  // namespace ppapi
248