1 // Copyright 2014 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.
5 #include "mojo/public/environment/default_async_waiter.h"
9 #include "mojo/public/utility/run_loop.h"
10 #include "mojo/public/utility/run_loop_handler.h"
15 // RunLoopHandler implementation used for a request to AsyncWait(). There are
16 // two ways RunLoopHandlerImpl is deleted:
17 // . when the handle is ready (or errored).
18 // . when CancelWait() is invoked.
19 class RunLoopHandlerImpl : public RunLoopHandler {
21 RunLoopHandlerImpl(const Handle& handle,
22 MojoAsyncWaitCallback callback,
29 virtual ~RunLoopHandlerImpl() {
30 RunLoop::current()->RemoveHandler(handle_);
34 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE {
35 NotifyCallback(MOJO_RESULT_OK);
38 virtual void OnHandleError(const Handle& handle,
39 MojoResult result) MOJO_OVERRIDE {
40 NotifyCallback(result);
44 void NotifyCallback(MojoResult result) {
45 // Delete this to unregister the handle. That way if the callback
46 // reregisters everything is ok.
47 MojoAsyncWaitCallback callback = callback_;
48 void* closure = closure_;
51 callback(closure, result);
55 MojoAsyncWaitCallback callback_;
58 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl);
61 MojoAsyncWaitID AsyncWait(MojoAsyncWaiter* waiter,
64 MojoDeadline deadline,
65 MojoAsyncWaitCallback callback,
67 RunLoop* run_loop = RunLoop::current();
70 // |run_loop_handler| is destroyed either when the handle is ready or if
71 // CancelWait is invoked.
72 RunLoopHandlerImpl* run_loop_handler =
73 new RunLoopHandlerImpl(Handle(handle), callback, closure);
74 run_loop->AddHandler(run_loop_handler, Handle(handle), flags, deadline);
75 return reinterpret_cast<MojoAsyncWaitID>(run_loop_handler);
78 void CancelWait(MojoAsyncWaiter* waiter, MojoAsyncWaitID wait_id) {
79 delete reinterpret_cast<RunLoopHandlerImpl*>(wait_id);
82 MojoAsyncWaiter s_default_async_waiter = {
89 MojoAsyncWaiter* GetDefaultAsyncWaiter() {
90 return &s_default_async_waiter;