OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_EDK_SYSTEM_WATCHER_SET_H_ | 5 #ifndef MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
6 #define MOJO_EDK_SYSTEM_WATCHER_SET_H_ | 6 #define MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
7 | 7 |
8 #include <map> | 8 #include <unordered_map> |
9 | 9 |
| 10 #include "base/callback.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/optional.h" | |
13 #include "mojo/edk/system/handle_signals_state.h" | 13 #include "mojo/edk/system/handle_signals_state.h" |
14 #include "mojo/edk/system/watcher_dispatcher.h" | 14 #include "mojo/edk/system/watcher.h" |
| 15 #include "mojo/public/c/system/types.h" |
15 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 namespace edk { | 18 namespace edk { |
18 | 19 |
19 // A WatcherSet maintains a set of references to WatcherDispatchers to be | 20 // A WatcherSet maintains a set of Watchers attached to a single handle and |
20 // notified when a handle changes state. | 21 // keyed on an arbitrary user context. |
21 // | |
22 // Dispatchers which may be watched by a watcher should own a WatcherSet and | |
23 // notify it of all relevant state changes. | |
24 class WatcherSet { | 22 class WatcherSet { |
25 public: | 23 public: |
26 // |owner| is the Dispatcher who owns this WatcherSet. | 24 WatcherSet(); |
27 explicit WatcherSet(Dispatcher* owner); | |
28 ~WatcherSet(); | 25 ~WatcherSet(); |
29 | 26 |
30 // Notifies all watchers of the handle's current signals state. | 27 // Notifies all Watchers of a state change. |
31 void NotifyState(const HandleSignalsState& state); | 28 void NotifyForStateChange(const HandleSignalsState& state); |
32 | 29 |
33 // Notifies all watchers that this handle has been closed. | 30 // Notifies all Watchers that their watched handle has been closed. |
34 void NotifyClosed(); | 31 void NotifyClosed(); |
35 | 32 |
36 // Adds a new watcher+context. | 33 // Adds a new watcher to watch for signals in |signals| to be satisfied or |
37 MojoResult Add(const scoped_refptr<WatcherDispatcher>& watcher, | 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, | 38 uintptr_t context, |
39 const HandleSignalsState& current_state); | 39 const HandleSignalsState& current_state); |
40 | 40 |
41 // Removes a watcher+context. | 41 // Removes a watcher from the set. |
42 MojoResult Remove(WatcherDispatcher* watcher, uintptr_t context); | 42 MojoResult Remove(uintptr_t context); |
43 | 43 |
44 private: | 44 private: |
45 using ContextSet = std::set<uintptr_t>; | 45 // A map of watchers keyed on context value. |
46 | 46 std::unordered_map<uintptr_t, scoped_refptr<Watcher>> watchers_; |
47 struct Entry { | |
48 Entry(const scoped_refptr<WatcherDispatcher>& dispatcher); | |
49 Entry(Entry&& other); | |
50 ~Entry(); | |
51 | |
52 Entry& operator=(Entry&& other); | |
53 | |
54 scoped_refptr<WatcherDispatcher> dispatcher; | |
55 ContextSet contexts; | |
56 | |
57 private: | |
58 DISALLOW_COPY_AND_ASSIGN(Entry); | |
59 }; | |
60 | |
61 Dispatcher* const owner_; | |
62 std::map<WatcherDispatcher*, Entry> watchers_; | |
63 base::Optional<HandleSignalsState> last_known_state_; | |
64 | 47 |
65 DISALLOW_COPY_AND_ASSIGN(WatcherSet); | 48 DISALLOW_COPY_AND_ASSIGN(WatcherSet); |
66 }; | 49 }; |
67 | 50 |
68 } // namespace edk | 51 } // namespace edk |
69 } // namespace mojo | 52 } // namespace mojo |
70 | 53 |
71 #endif // MOJO_EDK_SYSTEM_WATCHER_SET_H_ | 54 #endif // MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
OLD | NEW |