| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 BASE_WIN_OBJECT_WATCHER_H_ | |
| 6 #define BASE_WIN_OBJECT_WATCHER_H_ | |
| 7 | |
| 8 #include <windows.h> | |
| 9 | |
| 10 #include "base/base_export.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 | |
| 15 namespace base { | |
| 16 namespace win { | |
| 17 | |
| 18 // A class that provides a means to asynchronously wait for a Windows object to | |
| 19 // become signaled. It is an abstraction around RegisterWaitForSingleObject | |
| 20 // that provides a notification callback, OnObjectSignaled, that runs back on | |
| 21 // the origin thread (i.e., the thread that called StartWatching). | |
| 22 // | |
| 23 // This class acts like a smart pointer such that when it goes out-of-scope, | |
| 24 // UnregisterWaitEx is automatically called, and any in-flight notification is | |
| 25 // suppressed. | |
| 26 // | |
| 27 // Typical usage: | |
| 28 // | |
| 29 // class MyClass : public base::ObjectWatcher::Delegate { | |
| 30 // public: | |
| 31 // void DoStuffWhenSignaled(HANDLE object) { | |
| 32 // watcher_.StartWatching(object, this); | |
| 33 // } | |
| 34 // virtual void OnObjectSignaled(HANDLE object) { | |
| 35 // // OK, time to do stuff! | |
| 36 // } | |
| 37 // private: | |
| 38 // base::ObjectWatcher watcher_; | |
| 39 // }; | |
| 40 // | |
| 41 // In the above example, MyClass wants to "do stuff" when object becomes | |
| 42 // signaled. ObjectWatcher makes this task easy. When MyClass goes out of | |
| 43 // scope, the watcher_ will be destroyed, and there is no need to worry about | |
| 44 // OnObjectSignaled being called on a deleted MyClass pointer. Easy! | |
| 45 // If the object is already signaled before being watched, OnObjectSignaled is | |
| 46 // still called after (but not necessarily immediately after) watch is started. | |
| 47 // | |
| 48 class BASE_EXPORT ObjectWatcher : public MessageLoop::DestructionObserver { | |
| 49 public: | |
| 50 class BASE_EXPORT Delegate { | |
| 51 public: | |
| 52 virtual ~Delegate() {} | |
| 53 // Called from the MessageLoop when a signaled object is detected. To | |
| 54 // continue watching the object, StartWatching must be called again. | |
| 55 virtual void OnObjectSignaled(HANDLE object) = 0; | |
| 56 }; | |
| 57 | |
| 58 ObjectWatcher(); | |
| 59 ~ObjectWatcher() override; | |
| 60 | |
| 61 // When the object is signaled, the given delegate is notified on the thread | |
| 62 // where StartWatching is called. The ObjectWatcher is not responsible for | |
| 63 // deleting the delegate. | |
| 64 // | |
| 65 // Returns true if the watch was started. Otherwise, false is returned. | |
| 66 // | |
| 67 bool StartWatching(HANDLE object, Delegate* delegate); | |
| 68 | |
| 69 // Stops watching. Does nothing if the watch has already completed. If the | |
| 70 // watch is still active, then it is canceled, and the associated delegate is | |
| 71 // not notified. | |
| 72 // | |
| 73 // Returns true if the watch was canceled. Otherwise, false is returned. | |
| 74 // | |
| 75 bool StopWatching(); | |
| 76 | |
| 77 // Returns true if currently watching an object. | |
| 78 bool IsWatching() const; | |
| 79 | |
| 80 // Returns the handle of the object being watched. | |
| 81 HANDLE GetWatchedObject() const; | |
| 82 | |
| 83 private: | |
| 84 // Called on a background thread when done waiting. | |
| 85 static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out); | |
| 86 | |
| 87 void Signal(Delegate* delegate); | |
| 88 | |
| 89 // MessageLoop::DestructionObserver implementation: | |
| 90 void WillDestroyCurrentMessageLoop() override; | |
| 91 | |
| 92 // Internal state. | |
| 93 Closure callback_; | |
| 94 HANDLE object_; // The object being watched | |
| 95 HANDLE wait_object_; // Returned by RegisterWaitForSingleObject | |
| 96 MessageLoop* origin_loop_; // Used to get back to the origin thread | |
| 97 | |
| 98 WeakPtrFactory<ObjectWatcher> weak_factory_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(ObjectWatcher); | |
| 101 }; | |
| 102 | |
| 103 } // namespace win | |
| 104 } // namespace base | |
| 105 | |
| 106 #endif // BASE_WIN_OBJECT_WATCHER_H_ | |
| OLD | NEW |