• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_
6 #define MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_
7 
8 #include <memory>
9 
10 #include "base/callback_forward.h"
11 #include "base/macros.h"
12 #include "base/run_loop.h"
13 #include "mojo/message_pump/mojo_message_pump_export.h"
14 #include "mojo/public/cpp/system/core.h"
15 
16 namespace base {
17 class Thread;
18 }
19 
20 namespace mojo {
21 namespace common {
22 namespace test {
23 class HandleWatcherTest;
24 }
25 
26 // HandleWatcher is used to asynchronously wait on a handle and notify a Closure
27 // when the handle is ready, or the deadline has expired.
28 class MOJO_MESSAGE_PUMP_EXPORT HandleWatcher {
29  public:
30   HandleWatcher();
31 
32   ~HandleWatcher();
33 
34   // 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
36   // 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
38   // 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
40   // MOJO_RESULT_ABORTED.
41   void Start(const Handle& handle,
42              MojoHandleSignals handle_signals,
43              MojoDeadline deadline,
44              const base::Callback<void(MojoResult)>& callback);
45 
46   // Stops listening. Does nothing if not in the process of listening.
47   void Stop();
48 
is_watching()49   bool is_watching() const { return !!state_; }
50 
51  private:
52   class StateBase;
53   class SameThreadWatchingState;
54   class SecondaryThreadWatchingState;
55 
56   // If non-NULL Start() has been invoked.
57   std::unique_ptr<StateBase> state_;
58 
59   DISALLOW_COPY_AND_ASSIGN(HandleWatcher);
60 };
61 
62 }  // namespace common
63 }  // namespace mojo
64 
65 #endif  // MOJO_MESSAGE_PUMP_HANDLE_WATCHER_H_
66