• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_EDK_SYSTEM_WATCHER_SET_H_
6 #define MOJO_EDK_SYSTEM_WATCHER_SET_H_
7 
8 #include <unordered_map>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "mojo/edk/system/handle_signals_state.h"
14 #include "mojo/edk/system/watcher.h"
15 #include "mojo/public/c/system/types.h"
16 
17 namespace mojo {
18 namespace edk {
19 
20 // A WatcherSet maintains a set of Watchers attached to a single handle and
21 // keyed on an arbitrary user context.
22 class WatcherSet {
23  public:
24   WatcherSet();
25   ~WatcherSet();
26 
27   // Notifies all Watchers of a state change.
28   void NotifyForStateChange(const HandleSignalsState& state);
29 
30   // Notifies all Watchers that their watched handle has been closed.
31   void NotifyClosed();
32 
33   // Adds a new watcher to watch for signals in |signals| to be satisfied or
34   // unsatisfiable. |current_state| is the current signals state of the
35   // handle being watched.
36   MojoResult Add(MojoHandleSignals signals,
37                  const Watcher::WatchCallback& callback,
38                  uintptr_t context,
39                  const HandleSignalsState& current_state);
40 
41   // Removes a watcher from the set.
42   MojoResult Remove(uintptr_t context);
43 
44  private:
45   // A map of watchers keyed on context value.
46   std::unordered_map<uintptr_t, scoped_refptr<Watcher>> watchers_;
47 
48   DISALLOW_COPY_AND_ASSIGN(WatcherSet);
49 };
50 
51 }  // namespace edk
52 }  // namespace mojo
53 
54 #endif  // MOJO_EDK_SYSTEM_WATCHER_SET_H_
55