This source file includes following definitions.
- IsMainThread
- RunCompletionTask
- result_for_blocked_callback_
- Abort
- PostAbort
- Run
- PostRun
- set_completion_task
- IsPending
- IsScheduledToRun
- BlockUntilComplete
- MarkAsCompleted
#include "ppapi/shared_impl/tracked_callback.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/lock.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_message_loop.h"
#include "ppapi/shared_impl/callback_tracker.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/ppb_message_loop_shared.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/resource.h"
namespace ppapi {
namespace {
bool IsMainThread() {
return PpapiGlobals::Get()
->GetMainThreadMessageLoop()
->BelongsToCurrentThread();
}
int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task,
int32_t result) {
int32_t task_result = completion_task.Run(result);
if (result != PP_ERROR_ABORTED)
result = task_result;
return result;
}
}
TrackedCallback::TrackedCallback(Resource* resource,
const PP_CompletionCallback& callback)
: is_scheduled_(false),
resource_id_(resource ? resource->pp_resource() : 0),
completed_(false),
aborted_(false),
callback_(callback),
target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()),
result_for_blocked_callback_(PP_OK) {
if (resource) {
tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance(
resource->pp_instance());
tracker_->Add(make_scoped_refptr(this));
}
base::Lock* proxy_lock = ProxyLock::Get();
if (proxy_lock) {
if (is_blocking()) {
operation_completed_condvar_.reset(
new base::ConditionVariable(proxy_lock));
} else {
}
}
}
TrackedCallback::~TrackedCallback() {}
void TrackedCallback::Abort() { Run(PP_ERROR_ABORTED); }
void TrackedCallback::PostAbort() { PostRun(PP_ERROR_ABORTED); }
void TrackedCallback::Run(int32_t result) {
if (completed())
return;
if (result == PP_ERROR_ABORTED)
aborted_ = true;
if (aborted())
result = PP_ERROR_ABORTED;
if (is_blocking()) {
if (!operation_completed_condvar_.get()) {
NOTREACHED();
return;
}
result_for_blocked_callback_ = result;
scoped_refptr<TrackedCallback> thiz(this);
MarkAsCompleted();
operation_completed_condvar_->Signal();
} else {
if (target_loop_.get() &&
target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
PostRun(result);
return;
}
PP_CompletionCallback callback = callback_;
CompletionTask completion_task = completion_task_;
completion_task_.Reset();
MarkAsCompleted();
if (!completion_task.is_null())
result = RunCompletionTask(completion_task, result);
CallWhileUnlocked(PP_RunCompletionCallback, &callback, result);
}
}
void TrackedCallback::PostRun(int32_t result) {
if (completed()) {
NOTREACHED();
return;
}
if (result == PP_ERROR_ABORTED)
aborted_ = true;
DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_);
if (is_blocking()) {
Run(result);
} else {
base::Closure callback_closure(
RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result)));
if (target_loop_) {
target_loop_->PostClosure(FROM_HERE, callback_closure, 0);
} else {
DCHECK(IsMainThread());
DCHECK(PpapiGlobals::Get()->IsHostGlobals());
base::MessageLoop::current()->PostTask(FROM_HERE, callback_closure);
}
}
is_scheduled_ = true;
}
void TrackedCallback::set_completion_task(
const CompletionTask& completion_task) {
DCHECK(completion_task_.is_null());
completion_task_ = completion_task;
}
bool TrackedCallback::IsPending(
const scoped_refptr<TrackedCallback>& callback) {
if (!callback.get())
return false;
if (callback->aborted())
return false;
return !callback->completed();
}
bool TrackedCallback::IsScheduledToRun(
const scoped_refptr<TrackedCallback>& callback) {
return IsPending(callback) && callback->is_scheduled_;
}
int32_t TrackedCallback::BlockUntilComplete() {
CHECK(operation_completed_condvar_.get());
if (!is_blocking() || !operation_completed_condvar_.get()) {
NOTREACHED();
return PP_ERROR_FAILED;
}
while (!completed())
operation_completed_condvar_->Wait();
if (!completion_task_.is_null()) {
result_for_blocked_callback_ =
RunCompletionTask(completion_task_, result_for_blocked_callback_);
completion_task_.Reset();
}
return result_for_blocked_callback_;
}
void TrackedCallback::MarkAsCompleted() {
DCHECK(!completed());
scoped_refptr<TrackedCallback> thiz = this;
completed_ = true;
if (resource_id_)
tracker_->Remove(thiz);
tracker_ = NULL;
}
}