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