This source file includes following definitions.
- is_main_thread_loop_
- is_main_thread_loop_
- AsPPB_MessageLoop_API
- AttachToCurrentThread
- Run
- PostWork
- PostQuit
- GetCurrent
- DetachFromThread
- IsCurrent
- PostClosure
- GetMessageLoopProxy
- ReleaseMessageLoop
- Create
- GetForMainThread
- GetCurrent
- AttachToCurrentThread
- Run
- PostWork
- PostQuit
- GetInterface
#include "ppapi/proxy/ppb_message_loop_proxy.h"
#include <vector>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_message_loop.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/thunk/enter.h"
using ppapi::thunk::PPB_MessageLoop_API;
namespace ppapi {
namespace proxy {
namespace {
typedef thunk::EnterResource<PPB_MessageLoop_API> EnterMessageLoop;
}
MessageLoopResource::MessageLoopResource(PP_Instance instance)
: MessageLoopShared(instance),
nested_invocations_(0),
destroyed_(false),
should_destroy_(false),
is_main_thread_loop_(false) {
}
MessageLoopResource::MessageLoopResource(ForMainThread for_main_thread)
: MessageLoopShared(for_main_thread),
nested_invocations_(0),
destroyed_(false),
should_destroy_(false),
is_main_thread_loop_(true) {
CHECK(!PluginGlobals::Get()->msg_loop_slot());
base::ThreadLocalStorage::Slot* slot = new base::ThreadLocalStorage::Slot();
PluginGlobals::Get()->set_msg_loop_slot(slot);
slot->Set(this);
loop_proxy_ = base::MessageLoopProxy::current();
}
MessageLoopResource::~MessageLoopResource() {
}
PPB_MessageLoop_API* MessageLoopResource::AsPPB_MessageLoop_API() {
return this;
}
int32_t MessageLoopResource::AttachToCurrentThread() {
if (is_main_thread_loop_)
return PP_ERROR_INPROGRESS;
PluginGlobals* globals = PluginGlobals::Get();
base::ThreadLocalStorage::Slot* slot = globals->msg_loop_slot();
if (!slot) {
slot = new base::ThreadLocalStorage::Slot(&ReleaseMessageLoop);
globals->set_msg_loop_slot(slot);
} else {
if (slot->Get())
return PP_ERROR_INPROGRESS;
}
AddRef();
slot->Set(this);
loop_.reset(new base::MessageLoop);
loop_proxy_ = base::MessageLoopProxy::current();
for (size_t i = 0; i < pending_tasks_.size(); i++) {
const TaskInfo& info = pending_tasks_[i];
PostClosure(info.from_here, info.closure, info.delay_ms);
}
pending_tasks_.clear();
return PP_OK;
}
int32_t MessageLoopResource::Run() {
if (!IsCurrent())
return PP_ERROR_WRONG_THREAD;
if (is_main_thread_loop_)
return PP_ERROR_INPROGRESS;
nested_invocations_++;
CallWhileUnlocked(
base::Bind(&base::MessageLoop::Run, base::Unretained(loop_.get())));
nested_invocations_--;
if (should_destroy_ && nested_invocations_ == 0) {
loop_proxy_ = NULL;
loop_.reset();
destroyed_ = true;
}
return PP_OK;
}
int32_t MessageLoopResource::PostWork(PP_CompletionCallback callback,
int64_t delay_ms) {
if (!callback.func)
return PP_ERROR_BADARGUMENT;
if (destroyed_)
return PP_ERROR_FAILED;
PostClosure(FROM_HERE,
base::Bind(callback.func, callback.user_data,
static_cast<int32_t>(PP_OK)),
delay_ms);
return PP_OK;
}
int32_t MessageLoopResource::PostQuit(PP_Bool should_destroy) {
if (is_main_thread_loop_)
return PP_ERROR_WRONG_THREAD;
if (PP_ToBool(should_destroy))
should_destroy_ = true;
if (IsCurrent() && nested_invocations_ > 0)
loop_->Quit();
else
PostClosure(FROM_HERE, base::MessageLoop::QuitClosure(), 0);
return PP_OK;
}
MessageLoopResource* MessageLoopResource::GetCurrent() {
PluginGlobals* globals = PluginGlobals::Get();
if (!globals->msg_loop_slot())
return NULL;
return reinterpret_cast<MessageLoopResource*>(
globals->msg_loop_slot()->Get());
}
void MessageLoopResource::DetachFromThread() {
loop_proxy_ = NULL;
loop_.reset();
Release();
}
bool MessageLoopResource::IsCurrent() const {
PluginGlobals* globals = PluginGlobals::Get();
if (!globals->msg_loop_slot())
return false;
return static_cast<const void*>(globals->msg_loop_slot()->Get()) ==
static_cast<const void*>(this);
}
void MessageLoopResource::PostClosure(
const tracked_objects::Location& from_here,
const base::Closure& closure,
int64 delay_ms) {
if (loop_proxy_.get()) {
loop_proxy_->PostDelayedTask(
from_here, closure, base::TimeDelta::FromMilliseconds(delay_ms));
} else {
TaskInfo info;
info.from_here = FROM_HERE;
info.closure = closure;
info.delay_ms = delay_ms;
pending_tasks_.push_back(info);
}
}
base::MessageLoopProxy* MessageLoopResource::GetMessageLoopProxy() {
return loop_proxy_.get();
}
void MessageLoopResource::ReleaseMessageLoop(void* value) {
static_cast<MessageLoopResource*>(value)->DetachFromThread();
}
PP_Resource Create(PP_Instance instance) {
ProxyAutoLock lock;
PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
if (!dispatcher)
return 0;
return (new MessageLoopResource(instance))->GetReference();
}
PP_Resource GetForMainThread() {
ProxyAutoLock lock;
return PluginGlobals::Get()->loop_for_main_thread()->GetReference();
}
PP_Resource GetCurrent() {
ProxyAutoLock lock;
Resource* resource = MessageLoopResource::GetCurrent();
if (resource)
return resource->GetReference();
return 0;
}
int32_t AttachToCurrentThread(PP_Resource message_loop) {
EnterMessageLoop enter(message_loop, true);
if (enter.succeeded())
return enter.object()->AttachToCurrentThread();
return PP_ERROR_BADRESOURCE;
}
int32_t Run(PP_Resource message_loop) {
EnterMessageLoop enter(message_loop, true);
if (enter.succeeded())
return enter.object()->Run();
return PP_ERROR_BADRESOURCE;
}
int32_t PostWork(PP_Resource message_loop,
PP_CompletionCallback callback,
int64_t delay_ms) {
EnterMessageLoop enter(message_loop, true);
if (enter.succeeded())
return enter.object()->PostWork(callback, delay_ms);
return PP_ERROR_BADRESOURCE;
}
int32_t PostQuit(PP_Resource message_loop, PP_Bool should_destroy) {
EnterMessageLoop enter(message_loop, true);
if (enter.succeeded())
return enter.object()->PostQuit(should_destroy);
return PP_ERROR_BADRESOURCE;
}
const PPB_MessageLoop_1_0 ppb_message_loop_interface = {
&Create,
&GetForMainThread,
&GetCurrent,
&AttachToCurrentThread,
&Run,
&PostWork,
&PostQuit
};
PPB_MessageLoop_Proxy::PPB_MessageLoop_Proxy(Dispatcher* dispatcher)
: InterfaceProxy(dispatcher) {
}
PPB_MessageLoop_Proxy::~PPB_MessageLoop_Proxy() {
}
const PPB_MessageLoop_1_0* PPB_MessageLoop_Proxy::GetInterface() {
return &ppb_message_loop_interface;
}
}
}