OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/utility/bindings_support_impl.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 #include "mojo/public/bindings/lib/buffer.h" | |
10 #include "mojo/public/utility/run_loop.h" | |
11 #include "mojo/public/utility/run_loop_handler.h" | |
12 #include "mojo/public/utility/thread_local.h" | |
13 | |
14 namespace mojo { | |
15 namespace internal { | |
16 namespace { | |
17 | |
18 ThreadLocalPointer<Buffer>* tls_buffer = NULL; | |
19 | |
20 // RunLoopHandler implementation used for a request to AsyncWait(). There are | |
21 // two ways RunLoopHandlerImpl is deleted: | |
22 // . when the handle is ready (or errored). | |
23 // . when BindingsSupport::CancelWait() is invoked. | |
24 class RunLoopHandlerImpl : public RunLoopHandler { | |
25 public: | |
26 RunLoopHandlerImpl(const Handle& handle, | |
27 BindingsSupport::AsyncWaitCallback* callback) | |
28 : handle_(handle), | |
29 callback_(callback) {} | |
30 virtual ~RunLoopHandlerImpl() { | |
31 RunLoop::current()->RemoveHandler(handle_); | |
32 } | |
33 | |
34 // RunLoopHandler: | |
35 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | |
36 NotifyCallback(MOJO_RESULT_OK); | |
37 } | |
38 | |
39 virtual void OnHandleError(const Handle& handle, | |
40 MojoResult result) MOJO_OVERRIDE { | |
41 NotifyCallback(result); | |
42 } | |
43 | |
44 private: | |
45 void NotifyCallback(MojoResult result) { | |
46 // Delete this to unregister the handle. That way if the callback | |
47 // reregisters everything is ok. | |
48 BindingsSupport::AsyncWaitCallback* callback = callback_; | |
49 delete this; | |
50 | |
51 callback->OnHandleReady(result); | |
52 } | |
53 | |
54 const Handle handle_; | |
55 BindingsSupport::AsyncWaitCallback* callback_; | |
56 | |
57 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl); | |
58 }; | |
59 | |
60 } // namespace | |
61 | |
62 BindingsSupportImpl::BindingsSupportImpl() { | |
63 } | |
64 | |
65 BindingsSupportImpl::~BindingsSupportImpl() { | |
66 } | |
67 | |
68 // static | |
69 void BindingsSupportImpl::SetUp() { | |
70 assert(!tls_buffer); | |
71 tls_buffer = new ThreadLocalPointer<Buffer>; | |
72 } | |
73 | |
74 // static | |
75 void BindingsSupportImpl::TearDown() { | |
76 assert(tls_buffer); | |
77 delete tls_buffer; | |
78 tls_buffer = NULL; | |
79 } | |
80 | |
81 Buffer* BindingsSupportImpl::GetCurrentBuffer() { | |
82 return tls_buffer->Get(); | |
83 } | |
84 | |
85 Buffer* BindingsSupportImpl::SetCurrentBuffer(Buffer* buf) { | |
86 Buffer* old_buf = tls_buffer->Get(); | |
87 tls_buffer->Set(buf); | |
88 return old_buf; | |
89 } | |
90 | |
91 BindingsSupport::AsyncWaitID BindingsSupportImpl::AsyncWait( | |
92 const Handle& handle, | |
93 MojoWaitFlags flags, | |
94 AsyncWaitCallback* callback) { | |
95 RunLoop* run_loop = RunLoop::current(); | |
96 assert(run_loop); | |
97 // |run_loop_handler| is destroyed either when the handle is ready or if | |
98 // CancelWait is invoked. | |
99 RunLoopHandlerImpl* run_loop_handler = | |
100 new RunLoopHandlerImpl(handle, callback); | |
101 run_loop->AddHandler(run_loop_handler, handle, flags, | |
102 MOJO_DEADLINE_INDEFINITE); | |
103 return run_loop_handler; | |
104 } | |
105 | |
106 void BindingsSupportImpl::CancelWait(AsyncWaitID async_wait_id) { | |
107 delete static_cast<RunLoopHandlerImpl*>(async_wait_id); | |
108 } | |
109 | |
110 } // namespace internal | |
111 } // namespace mojo | |
OLD | NEW |