watcher_set.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef MOJO_CORE_WATCHER_SET_H_
  5. #define MOJO_CORE_WATCHER_SET_H_
  6. #include "base/containers/flat_map.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "mojo/core/handle_signals_state.h"
  10. #include "mojo/core/watcher_dispatcher.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace mojo {
  13. namespace core {
  14. // A WatcherSet maintains a set of references to WatcherDispatchers to be
  15. // notified when a handle changes state.
  16. //
  17. // Dispatchers which may be watched by a watcher should own a WatcherSet and
  18. // notify it of all relevant state changes.
  19. class WatcherSet {
  20. public:
  21. // |owner| is the Dispatcher who owns this WatcherSet.
  22. explicit WatcherSet(Dispatcher* owner);
  23. WatcherSet(const WatcherSet&) = delete;
  24. WatcherSet& operator=(const WatcherSet&) = delete;
  25. ~WatcherSet();
  26. // Notifies all watchers of the handle's current signals state.
  27. void NotifyState(const HandleSignalsState& state);
  28. // Notifies all watchers that this handle has been closed.
  29. void NotifyClosed();
  30. // Adds a new watcher+context.
  31. MojoResult Add(const scoped_refptr<WatcherDispatcher>& watcher,
  32. uintptr_t context,
  33. const HandleSignalsState& current_state);
  34. // Removes a watcher+context.
  35. MojoResult Remove(WatcherDispatcher* watcher, uintptr_t context);
  36. private:
  37. using ContextSet = std::set<uintptr_t>;
  38. struct Entry {
  39. Entry(const scoped_refptr<WatcherDispatcher>& dispatcher);
  40. Entry(const Entry&) = delete;
  41. Entry& operator=(const Entry&) = delete;
  42. Entry(Entry&& other);
  43. ~Entry();
  44. Entry& operator=(Entry&& other);
  45. scoped_refptr<WatcherDispatcher> dispatcher;
  46. ContextSet contexts;
  47. };
  48. const raw_ptr<Dispatcher> owner_;
  49. base::flat_map<WatcherDispatcher*, Entry> watchers_;
  50. absl::optional<HandleSignalsState> last_known_state_;
  51. };
  52. } // namespace core
  53. } // namespace mojo
  54. #endif // MOJO_CORE_WATCHER_SET_H_