This source file includes following definitions.
- closure_
- OnHandleReady
- OnHandleError
- NotifyCallback
- CancelWait
- GetDefaultAsyncWaiter
#include "mojo/public/cpp/environment/default_async_waiter.h"
#include <assert.h>
#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/public/cpp/utility/run_loop_handler.h"
namespace mojo {
namespace {
class RunLoopHandlerImpl : public RunLoopHandler {
public:
RunLoopHandlerImpl(const Handle& handle,
MojoAsyncWaitCallback callback,
void* closure)
: handle_(handle),
callback_(callback),
closure_(closure) {
}
virtual ~RunLoopHandlerImpl() {
RunLoop::current()->RemoveHandler(handle_);
}
virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE {
NotifyCallback(MOJO_RESULT_OK);
}
virtual void OnHandleError(const Handle& handle,
MojoResult result) MOJO_OVERRIDE {
NotifyCallback(result);
}
private:
void NotifyCallback(MojoResult result) {
MojoAsyncWaitCallback callback = callback_;
void* closure = closure_;
delete this;
callback(closure, result);
}
const Handle handle_;
MojoAsyncWaitCallback callback_;
void* closure_;
MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl);
};
MojoAsyncWaitID AsyncWait(MojoAsyncWaiter* waiter,
MojoHandle handle,
MojoWaitFlags flags,
MojoDeadline deadline,
MojoAsyncWaitCallback callback,
void* closure) {
RunLoop* run_loop = RunLoop::current();
assert(run_loop);
RunLoopHandlerImpl* run_loop_handler =
new RunLoopHandlerImpl(Handle(handle), callback, closure);
run_loop->AddHandler(run_loop_handler, Handle(handle), flags, deadline);
return reinterpret_cast<MojoAsyncWaitID>(run_loop_handler);
}
void CancelWait(MojoAsyncWaiter* waiter, MojoAsyncWaitID wait_id) {
delete reinterpret_cast<RunLoopHandlerImpl*>(wait_id);
}
MojoAsyncWaiter s_default_async_waiter = {
AsyncWait,
CancelWait
};
}
MojoAsyncWaiter* GetDefaultAsyncWaiter() {
return &s_default_async_waiter;
}
}