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_H_ 6 #define MOJO_EDK_SYSTEM_WATCHER_H_ 7 8 #include "base/callback.h" 9 #include "base/macros.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/synchronization/lock.h" 12 #include "mojo/public/c/system/functions.h" 13 #include "mojo/public/c/system/types.h" 14 15 namespace mojo { 16 namespace edk { 17 18 struct HandleSignalsState; 19 20 // This object corresponds to a watch added by a single call to |MojoWatch()|. 21 // 22 // An event may occur at any time which should trigger a Watcher to run its 23 // callback, but the callback needs to be deferred until all EDK locks are 24 // released. At the same time, a watch may be cancelled at any time by 25 // |MojoCancelWatch()| and it is not OK for the callback to be invoked after 26 // that happens. 27 // 28 // Therefore a Watcher needs to have some associated thread-safe state to track 29 // its cancellation, which is why it's ref-counted. 30 class Watcher : public base::RefCountedThreadSafe<Watcher> { 31 public: 32 using WatchCallback = base::Callback<void(MojoResult, 33 const HandleSignalsState&, 34 MojoWatchNotificationFlags)>; 35 36 // Constructs a new Watcher which watches for |signals| to be satisfied on a 37 // handle and which invokes |callback| either when one such signal is 38 // satisfied, or all such signals become unsatisfiable. 39 Watcher(MojoHandleSignals signals, const WatchCallback& callback); 40 41 // Runs the Watcher's callback with the given arguments if it hasn't been 42 // cancelled yet. 43 void MaybeInvokeCallback(MojoResult result, 44 const HandleSignalsState& state, 45 MojoWatchNotificationFlags flags); 46 47 // Notifies the Watcher of a state change. This may result in the Watcher 48 // adding a finalizer to the current RequestContext to invoke its callback, 49 // cancellation notwithstanding. 50 void NotifyForStateChange(const HandleSignalsState& signals_state); 51 52 // Notifies the Watcher of handle closure. This always results in the Watcher 53 // adding a finalizer to the current RequestContext to invoke its callback, 54 // cancellation notwithstanding. 55 void NotifyClosed(); 56 57 // Explicitly cancels the watch, guaranteeing that its callback will never be 58 // be invoked again. 59 void Cancel(); 60 61 private: 62 friend class base::RefCountedThreadSafe<Watcher>; 63 64 ~Watcher(); 65 66 // The set of signals which are watched by this Watcher. 67 const MojoHandleSignals signals_; 68 69 // The callback to invoke with a result and signal state any time signals in 70 // |signals_| are satisfied or become permanently unsatisfiable. 71 const WatchCallback callback_; 72 73 // Guards |is_cancelled_|. 74 base::Lock lock_; 75 76 // Indicates whether the watch has been cancelled. A |Watcher| may exist for a 77 // brief period of time after being cancelled if it's been attached as a 78 // RequestContext finalizer. In such cases the callback must not be invoked, 79 // hence this flag. 80 bool is_cancelled_ = false; 81 82 DISALLOW_COPY_AND_ASSIGN(Watcher); 83 }; 84 85 } // namespace edk 86 } // namespace mojo 87 88 #endif // MOJO_EDK_SYSTEM_WATCHER_H_ 89