1 // Copyright 2011 The Chromium Authors 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 BASE_WIN_OBJECT_WATCHER_H_ 6 #define BASE_WIN_OBJECT_WATCHER_H_ 7 8 #include "base/win/windows_types.h" 9 10 #include "base/base_export.h" 11 #include "base/functional/callback.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/task/sequenced_task_runner.h" 15 16 namespace base { 17 namespace win { 18 19 // A class that provides a means to asynchronously wait for a Windows object to 20 // become signaled. It is an abstraction around RegisterWaitForSingleObject 21 // that provides a notification callback, OnObjectSignaled, that runs back on 22 // the origin sequence (i.e., the sequence that called StartWatching). 23 // 24 // This class acts like a smart pointer such that when it goes out-of-scope, 25 // UnregisterWaitEx is automatically called, and any in-flight notification is 26 // suppressed. 27 // 28 // The waiting handle MUST NOT be closed while watching is in progress. If this 29 // handle is closed while the wait is still pending, the behavior is undefined 30 // (see MSDN:RegisterWaitForSingleObject). 31 // 32 // Typical usage: 33 // 34 // class MyClass : public base::win::ObjectWatcher::Delegate { 35 // public: 36 // void DoStuffWhenSignaled(HANDLE object) { 37 // watcher_.StartWatchingOnce(object, this); 38 // } 39 // void OnObjectSignaled(HANDLE object) override { 40 // // OK, time to do stuff! 41 // } 42 // private: 43 // base::win::ObjectWatcher watcher_; 44 // }; 45 // 46 // In the above example, MyClass wants to "do stuff" when object becomes 47 // signaled. ObjectWatcher makes this task easy. When MyClass goes out of 48 // scope, the watcher_ will be destroyed, and there is no need to worry about 49 // OnObjectSignaled being called on a deleted MyClass pointer. Easy! 50 // If the object is already signaled before being watched, OnObjectSignaled is 51 // still called after (but not necessarily immediately after) watch is started. 52 // 53 // NOTE: Except for the constructor, all public methods of this class must be 54 // called in sequence, in a scope where 55 // SequencedTaskRunner::HasCurrentDefault(). 56 class BASE_EXPORT ObjectWatcher { 57 public: 58 class BASE_EXPORT Delegate { 59 public: 60 virtual ~Delegate() = default; 61 // Called from the sequence that started the watch when a signaled object is 62 // detected. To continue watching the object, StartWatching must be called 63 // again. 64 virtual void OnObjectSignaled(HANDLE object) = 0; 65 }; 66 67 ObjectWatcher(); 68 69 ObjectWatcher(const ObjectWatcher&) = delete; 70 ObjectWatcher& operator=(const ObjectWatcher&) = delete; 71 72 ~ObjectWatcher(); 73 74 // When the object is signaled, the given delegate is notified on the sequence 75 // where StartWatchingOnce is called. The ObjectWatcher is not responsible for 76 // deleting the delegate. 77 // Returns whether watching was successfully initiated. 78 bool StartWatchingOnce(HANDLE object, 79 Delegate* delegate, 80 const Location& from_here = Location::Current()); 81 82 // Notifies the delegate, on the sequence where this method is called, each 83 // time the object is set. By definition, the handle must be an auto-reset 84 // object. The caller must ensure that it (or any Windows system code) doesn't 85 // reset the event or else the delegate won't be called. 86 // Returns whether watching was successfully initiated. 87 bool StartWatchingMultipleTimes( 88 HANDLE object, 89 Delegate* delegate, 90 const Location& from_here = Location::Current()); 91 92 // Stops watching. Does nothing if the watch has already completed. If the 93 // watch is still active, then it is canceled, and the associated delegate is 94 // not notified. 95 // 96 // Returns true if the watch was canceled. Otherwise, false is returned. 97 bool StopWatching(); 98 99 // Returns true if currently watching an object. 100 bool IsWatching() const; 101 102 // Returns the handle of the object being watched. 103 HANDLE GetWatchedObject() const; 104 105 private: 106 // Called on a background thread when done waiting. 107 static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out); 108 109 // Helper used by StartWatchingOnce and StartWatchingMultipleTimes. 110 bool StartWatchingInternal(HANDLE object, 111 Delegate* delegate, 112 bool execute_only_once, 113 const Location& from_here); 114 115 void Signal(Delegate* delegate); 116 117 void Reset(); 118 119 Location location_; 120 121 // A callback pre-bound to Signal() that is posted to the caller's task runner 122 // when the wait completes. 123 RepeatingClosure callback_; 124 125 // The object being watched. 126 HANDLE object_ = nullptr; 127 128 // The wait handle returned by RegisterWaitForSingleObject. 129 HANDLE wait_object_ = nullptr; 130 131 // The task runner of the sequence on which the watch was started. 132 scoped_refptr<SequencedTaskRunner> task_runner_; 133 134 bool run_once_ = true; 135 136 WeakPtrFactory<ObjectWatcher> weak_factory_{this}; 137 }; 138 139 } // namespace win 140 } // namespace base 141 142 #endif // BASE_WIN_OBJECT_WATCHER_H_ 143