OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_ | 5 #ifndef MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_ |
6 #define MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_ | 6 #define MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
| 10 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "mojo/message_pump/mojo_message_pump_export.h" | 13 #include "mojo/message_pump/mojo_message_pump_export.h" |
13 #include "third_party/mojo/src/mojo/public/cpp/system/core.h" | 14 #include "third_party/mojo/src/mojo/public/cpp/system/core.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class Thread; | 17 class Thread; |
17 } | 18 } |
18 | 19 |
19 namespace mojo { | 20 namespace mojo { |
20 namespace common { | 21 namespace common { |
21 namespace test { | 22 namespace test { |
22 class HandleWatcherTest; | 23 class HandleWatcherTest; |
23 } | 24 } |
24 | 25 |
25 // HandleWatcher is used to asynchronously wait on a handle and notify a Closure | 26 // HandleWatcher is used to asynchronously wait on a handle and notify a Closure |
26 // when the handle is ready, or the deadline has expired. | 27 // when the handle is ready, or the deadline has expired. |
27 class MOJO_MESSAGE_PUMP_EXPORT HandleWatcher { | 28 class MOJO_MESSAGE_PUMP_EXPORT HandleWatcher { |
28 public: | 29 public: |
29 HandleWatcher(); | 30 explicit HandleWatcher(int location); |
30 | 31 |
31 // The destructor implicitly stops listening. See Stop() for details. | 32 // The destructor implicitly stops listening. See Stop() for details. |
32 ~HandleWatcher(); | 33 ~HandleWatcher(); |
33 | 34 |
| 35 int location() const { return location_; } |
| 36 |
34 // Starts listening for |handle|. This implicitly invokes Stop(). In other | 37 // Starts listening for |handle|. This implicitly invokes Stop(). In other |
35 // words, Start() performs one asynchronous watch at a time. It is ok to call | 38 // words, Start() performs one asynchronous watch at a time. It is ok to call |
36 // Start() multiple times, but it cancels any existing watches. |callback| is | 39 // Start() multiple times, but it cancels any existing watches. |callback| is |
37 // notified when the handle is ready, invalid or deadline has passed and is | 40 // notified when the handle is ready, invalid or deadline has passed and is |
38 // notified on the thread Start() was invoked on. If the current thread exits | 41 // notified on the thread Start() was invoked on. If the current thread exits |
39 // before the handle is ready, then |callback| is invoked with a result of | 42 // before the handle is ready, then |callback| is invoked with a result of |
40 // MOJO_RESULT_ABORTED. | 43 // MOJO_RESULT_ABORTED. |
41 void Start(const Handle& handle, | 44 void Start(const Handle& handle, |
42 MojoHandleSignals handle_signals, | 45 MojoHandleSignals handle_signals, |
43 MojoDeadline deadline, | 46 MojoDeadline deadline, |
44 const base::Callback<void(MojoResult)>& callback); | 47 const base::Callback<void(MojoResult)>& callback); |
45 | 48 |
46 // Stops listening. Does nothing if not in the process of listening. Blocks | 49 // Stops listening. Does nothing if not in the process of listening. Blocks |
47 // until no longer listening on the handle. | 50 // until no longer listening on the handle. |
48 void Stop(); | 51 void Stop(); |
49 | 52 |
50 private: | 53 private: |
51 class StateBase; | 54 class StateBase; |
52 class SameThreadWatchingState; | 55 class SameThreadWatchingState; |
53 class SecondaryThreadWatchingState; | 56 class SecondaryThreadWatchingState; |
54 | 57 |
| 58 const int location_; |
| 59 |
55 // If non-NULL Start() has been invoked. | 60 // If non-NULL Start() has been invoked. |
56 scoped_ptr<StateBase> state_; | 61 scoped_ptr<StateBase> state_; |
57 | 62 |
58 DISALLOW_COPY_AND_ASSIGN(HandleWatcher); | 63 DISALLOW_COPY_AND_ASSIGN(HandleWatcher); |
59 }; | 64 }; |
60 | 65 |
61 } // namespace common | 66 } // namespace common |
62 } // namespace mojo | 67 } // namespace mojo |
63 | 68 |
64 #endif // MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_ | 69 #endif // MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_ |
OLD | NEW |