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