Index: mojo/public/environment/standalone/async_waiter.cc |
diff --git a/mojo/public/environment/standalone/async_waiter.cc b/mojo/public/environment/standalone/async_waiter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4335c20ad745b0c23f7d70f2d1cacf6ca6261691 |
--- /dev/null |
+++ b/mojo/public/environment/standalone/async_waiter.cc |
@@ -0,0 +1,94 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "mojo/public/environment/async_waiter.h" |
+ |
+#include <assert.h> |
+ |
+#include "mojo/public/utility/run_loop.h" |
+#include "mojo/public/utility/run_loop_handler.h" |
+ |
+namespace mojo { |
+namespace internal { |
+namespace { |
+ |
+// RunLoopHandler implementation used for a request to AsyncWait(). There are |
+// two ways RunLoopHandlerImpl is deleted: |
+// . when the handle is ready (or errored). |
+// . when CancelWait() is invoked. |
+class RunLoopHandlerImpl : public RunLoopHandler { |
+ public: |
+ RunLoopHandlerImpl(const Handle& handle, |
+ MojoAsyncWaitCallback callback, |
+ uintptr_t callback_data) |
+ : handle_(handle), |
+ callback_(callback), |
+ callback_data_(callback_data) { |
+ } |
+ |
+ virtual ~RunLoopHandlerImpl() { |
+ RunLoop::current()->RemoveHandler(handle_); |
+ } |
+ |
+ // RunLoopHandler: |
+ 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) { |
+ // Delete this to unregister the handle. That way if the callback |
+ // reregisters everything is ok. |
+ MojoAsyncWaitCallback callback = callback_; |
+ uintptr_t callback_data = callback_data_; |
+ delete this; |
+ |
+ callback(callback_data, result); |
+ } |
+ |
+ const Handle handle_; |
+ MojoAsyncWaitCallback callback_; |
+ uintptr_t callback_data_; |
+ |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl); |
+}; |
+ |
+MojoAsyncWaitID AsyncWait(MojoAsyncWaiter* waiter, |
+ MojoHandle handle, |
+ MojoWaitFlags flags, |
+ MojoDeadline deadline, |
+ MojoAsyncWaitCallback callback, |
+ uintptr_t user_data) { |
+ RunLoop* run_loop = RunLoop::current(); |
+ assert(run_loop); |
+ |
+ // |run_loop_handler| is destroyed either when the handle is ready or if |
+ // CancelWait is invoked. |
+ RunLoopHandlerImpl* run_loop_handler = |
+ new RunLoopHandlerImpl(Handle(handle), callback, user_data); |
+ 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 |
+}; |
+ |
+} // namespace |
+} // namespace internal |
+} // namespace mojo |
+ |
+extern "C" MojoAsyncWaiter* MojoGetDefaultAsyncWaiter() { |
+ return &mojo::internal::s_default_async_waiter; |
+} |