1 // Copyright 2017 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_PUBLIC_CPP_SYSTEM_HANDLE_SIGNAL_TRACKER_H_ 6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_SIGNAL_TRACKER_H_ 7 8 #include "base/callback.h" 9 #include "base/macros.h" 10 #include "mojo/public/c/system/types.h" 11 #include "mojo/public/cpp/system/handle.h" 12 #include "mojo/public/cpp/system/simple_watcher.h" 13 #include "mojo/public/cpp/system/system_export.h" 14 15 namespace mojo { 16 17 // This class helps track the state of specific signal on a handle so that 18 // the user doesn't have to manually query the signal state every time they 19 // want to know the handle's state. 20 // 21 // Usage of this class is specifically targeting cases where the signal state 22 // changes infrequently but must be queried frequently. If either condition does 23 // not hold, consider using Handle::QuerySignalsState (or 24 // MojoQueryHandleSignalsState) directly instead. 25 class MOJO_CPP_SYSTEM_EXPORT HandleSignalTracker { 26 public: 27 using NotificationCallback = 28 base::Callback<void(const HandleSignalsState& signals_state)>; 29 30 // Constructs a tracker which tracks |signals| on |handle|. |signals| may 31 // be any single signal flag or any combination of signal flags. 32 HandleSignalTracker(Handle handle, MojoHandleSignals signals); 33 ~HandleSignalTracker(); 34 last_known_state()35 const HandleSignalsState& last_known_state() const { 36 return last_known_state_; 37 } 38 39 // Sets an optional callback to be invoked any time the tracker is notified of 40 // a relevant state change. set_notification_callback(const NotificationCallback & callback)41 void set_notification_callback(const NotificationCallback& callback) { 42 notification_callback_ = callback; 43 } 44 45 private: 46 class State; 47 48 void Arm(); 49 void OnNotify(MojoResult result, const HandleSignalsState& state); 50 51 NotificationCallback notification_callback_; 52 53 // The last known signaliing state of the handle. 54 HandleSignalsState last_known_state_ = {0, 0}; 55 56 // Watches for the signal(s) to be signaled. May only be armed when 57 // |low_watcher_| is not. 58 SimpleWatcher high_watcher_; 59 60 // Watches for the signal(s) to be cleared. May only be armed when 61 // |high_watcher_| is not. 62 SimpleWatcher low_watcher_; 63 64 DISALLOW_COPY_AND_ASSIGN(HandleSignalTracker); 65 }; 66 67 } // namespace mojo 68 69 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_SIGNAL_TRACKER_H_ 70