Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: mojo/public/environment/standalone/async_waiter.cc

Issue 134253004: Mojo: AsyncWaiter and mojo/public/environment (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: improve README.md Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/async_waiter.h"
6
7 #include <assert.h>
8
9 #include "mojo/public/utility/run_loop.h"
10 #include "mojo/public/utility/run_loop_handler.h"
11
12 namespace mojo {
13 namespace internal {
14 namespace {
15
16 // RunLoopHandler implementation used for a request to AsyncWait(). There are
17 // two ways RunLoopHandlerImpl is deleted:
18 // . when the handle is ready (or errored).
19 // . when CancelWait() is invoked.
20 class RunLoopHandlerImpl : public RunLoopHandler {
21 public:
22 RunLoopHandlerImpl(const Handle& handle,
23 MojoAsyncWaitCallback callback,
24 uintptr_t callback_data)
25 : handle_(handle),
26 callback_(callback),
27 callback_data_(callback_data) {
28 }
29
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 MojoAsyncWaitCallback callback = callback_;
49 uintptr_t callback_data = callback_data_;
50 delete this;
51
52 callback(callback_data, result);
53 }
54
55 const Handle handle_;
56 MojoAsyncWaitCallback callback_;
57 uintptr_t callback_data_;
58
59 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl);
60 };
61
62 MojoAsyncWaitID AsyncWait(MojoAsyncWaiter* waiter,
63 MojoHandle handle,
64 MojoWaitFlags flags,
65 MojoDeadline deadline,
66 MojoAsyncWaitCallback callback,
67 uintptr_t user_data) {
68 RunLoop* run_loop = RunLoop::current();
69 assert(run_loop);
70
71 // |run_loop_handler| is destroyed either when the handle is ready or if
72 // CancelWait is invoked.
73 RunLoopHandlerImpl* run_loop_handler =
74 new RunLoopHandlerImpl(Handle(handle), callback, user_data);
75 run_loop->AddHandler(run_loop_handler, Handle(handle), flags, deadline);
76 return reinterpret_cast<MojoAsyncWaitID>(run_loop_handler);
77 }
78
79 void CancelWait(MojoAsyncWaiter* waiter, MojoAsyncWaitID wait_id) {
80 delete reinterpret_cast<RunLoopHandlerImpl*>(wait_id);
81 }
82
83 MojoAsyncWaiter s_default_async_waiter = {
84 AsyncWait,
85 CancelWait
86 };
87
88 } // namespace
89 } // namespace internal
90 } // namespace mojo
91
92 extern "C" MojoAsyncWaiter* MojoGetDefaultAsyncWaiter() {
93 return &mojo::internal::s_default_async_waiter;
94 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698