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/environment/default_async_waiter.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 #include "mojo/public/cpp/utility/run_loop.h" | |
10 #include "mojo/public/cpp/utility/run_loop_handler.h" | |
11 | |
12 namespace mojo { | |
13 namespace { | |
14 | |
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 { | |
20 public: | |
21 RunLoopHandlerImpl(const Handle& handle, | |
22 MojoAsyncWaitCallback callback, | |
23 void* closure) | |
24 : handle_(handle), | |
25 callback_(callback), | |
26 closure_(closure) { | |
27 } | |
28 | |
29 virtual ~RunLoopHandlerImpl() { | |
30 RunLoop::current()->RemoveHandler(handle_); | |
31 } | |
32 | |
33 // RunLoopHandler: | |
34 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | |
35 NotifyCallback(MOJO_RESULT_OK); | |
36 } | |
37 | |
38 virtual void OnHandleError(const Handle& handle, | |
39 MojoResult result) MOJO_OVERRIDE { | |
40 NotifyCallback(result); | |
41 } | |
42 | |
43 private: | |
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_; | |
49 delete this; | |
50 | |
51 callback(closure, result); | |
52 } | |
53 | |
54 const Handle handle_; | |
55 MojoAsyncWaitCallback callback_; | |
56 void* closure_; | |
57 | |
58 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl); | |
59 }; | |
60 | |
61 MojoAsyncWaitID AsyncWait(MojoAsyncWaiter* waiter, | |
62 MojoHandle handle, | |
63 MojoWaitFlags flags, | |
64 MojoDeadline deadline, | |
65 MojoAsyncWaitCallback callback, | |
66 void* closure) { | |
67 RunLoop* run_loop = RunLoop::current(); | |
68 assert(run_loop); | |
69 | |
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); | |
76 } | |
77 | |
78 void CancelWait(MojoAsyncWaiter* waiter, MojoAsyncWaitID wait_id) { | |
79 delete reinterpret_cast<RunLoopHandlerImpl*>(wait_id); | |
80 } | |
81 | |
82 MojoAsyncWaiter s_default_async_waiter = { | |
83 AsyncWait, | |
84 CancelWait | |
85 }; | |
86 | |
87 } // namespace | |
88 | |
89 MojoAsyncWaiter* GetDefaultAsyncWaiter() { | |
90 return &s_default_async_waiter; | |
91 } | |
92 | |
93 } // namespace mojo | |
OLD | NEW |