| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "mojo/public/cpp/environment/lib/default_async_waiter.h" | |
| 6 | |
| 7 #include <assert.h> | |
| 8 #include <mojo/environment/async_waiter.h> | |
| 9 | |
| 10 #include "mojo/public/cpp/utility/run_loop.h" | |
| 11 #include "mojo/public/cpp/utility/run_loop_handler.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // RunLoopHandler implementation used for a request to AsyncWait(). There are | |
| 18 // two ways RunLoopHandlerImpl is deleted: | |
| 19 // . when the handle is ready (or errored). | |
| 20 // . when CancelWait() is invoked. | |
| 21 class RunLoopHandlerImpl : public RunLoopHandler { | |
| 22 public: | |
| 23 RunLoopHandlerImpl(MojoAsyncWaitCallback callback, void* closure) | |
| 24 : id_(0u), callback_(callback), closure_(closure) {} | |
| 25 | |
| 26 ~RunLoopHandlerImpl() override { RunLoop::current()->RemoveHandler(id_); } | |
| 27 | |
| 28 void set_id(Id id) { id_ = id; } | |
| 29 | |
| 30 // RunLoopHandler: | |
| 31 void OnHandleReady(Id /*id*/) override { NotifyCallback(MOJO_RESULT_OK); } | |
| 32 | |
| 33 void OnHandleError(Id /*id*/, MojoResult result) override { | |
| 34 NotifyCallback(result); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 void NotifyCallback(MojoResult result) { | |
| 39 // Delete this to unregister the handle. That way if the callback | |
| 40 // reregisters everything is ok. | |
| 41 MojoAsyncWaitCallback callback = callback_; | |
| 42 void* closure = closure_; | |
| 43 delete this; | |
| 44 | |
| 45 callback(closure, result); | |
| 46 } | |
| 47 | |
| 48 Id id_; | |
| 49 const MojoAsyncWaitCallback callback_; | |
| 50 void* const closure_; | |
| 51 | |
| 52 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl); | |
| 53 }; | |
| 54 | |
| 55 MojoAsyncWaitID AsyncWait(MojoHandle handle, | |
| 56 MojoHandleSignals signals, | |
| 57 MojoDeadline deadline, | |
| 58 MojoAsyncWaitCallback callback, | |
| 59 void* closure) { | |
| 60 RunLoop* run_loop = RunLoop::current(); | |
| 61 assert(run_loop); | |
| 62 | |
| 63 // |run_loop_handler| is destroyed either when the handle is ready or if | |
| 64 // CancelWait is invoked. | |
| 65 RunLoopHandlerImpl* run_loop_handler = | |
| 66 new RunLoopHandlerImpl(callback, closure); | |
| 67 run_loop_handler->set_id(run_loop->AddHandler( | |
| 68 run_loop_handler, Handle(handle), signals, deadline)); | |
| 69 return reinterpret_cast<MojoAsyncWaitID>(run_loop_handler); | |
| 70 } | |
| 71 | |
| 72 void CancelWait(MojoAsyncWaitID wait_id) { | |
| 73 delete reinterpret_cast<RunLoopHandlerImpl*>(wait_id); | |
| 74 } | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 namespace internal { | |
| 79 | |
| 80 const MojoAsyncWaiter kDefaultAsyncWaiter = {AsyncWait, CancelWait}; | |
| 81 | |
| 82 } // namespace internal | |
| 83 | |
| 84 } // namespace mojo | |
| OLD | NEW |