| 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/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 15 #include "base/single_thread_task_runner.h" | 14 #include "base/message_loop/message_loop.h" |
| 16 | 15 |
| 17 namespace base { | 16 namespace base { |
| 18 namespace win { | 17 namespace win { |
| 19 | 18 |
| 20 // A class that provides a means to asynchronously wait for a Windows object to | 19 // A class that provides a means to asynchronously wait for a Windows object to |
| 21 // become signaled. It is an abstraction around RegisterWaitForSingleObject | 20 // become signaled. It is an abstraction around RegisterWaitForSingleObject |
| 22 // that provides a notification callback, OnObjectSignaled, that runs back on | 21 // that provides a notification callback, OnObjectSignaled, that runs back on |
| 23 // the origin thread (i.e., the thread that called StartWatching). | 22 // the origin thread (i.e., the thread that called StartWatching). |
| 24 // | 23 // |
| 25 // This class acts like a smart pointer such that when it goes out-of-scope, | 24 // This class acts like a smart pointer such that when it goes out-of-scope, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 40 // base::win::ObjectWatcher watcher_; | 39 // base::win::ObjectWatcher watcher_; |
| 41 // }; | 40 // }; |
| 42 // | 41 // |
| 43 // In the above example, MyClass wants to "do stuff" when object becomes | 42 // In the above example, MyClass wants to "do stuff" when object becomes |
| 44 // signaled. ObjectWatcher makes this task easy. When MyClass goes out of | 43 // signaled. ObjectWatcher makes this task easy. When MyClass goes out of |
| 45 // scope, the watcher_ will be destroyed, and there is no need to worry about | 44 // scope, the watcher_ will be destroyed, and there is no need to worry about |
| 46 // OnObjectSignaled being called on a deleted MyClass pointer. Easy! | 45 // OnObjectSignaled being called on a deleted MyClass pointer. Easy! |
| 47 // If the object is already signaled before being watched, OnObjectSignaled is | 46 // If the object is already signaled before being watched, OnObjectSignaled is |
| 48 // still called after (but not necessarily immediately after) watch is started. | 47 // still called after (but not necessarily immediately after) watch is started. |
| 49 // | 48 // |
| 50 // NOTE: Except for the constructor, all public methods of this class must be | 49 // NOTE: Use of this class requires that there be a current message loop; |
| 51 // called on the same thread. A ThreadTaskRunnerHandle must be set on that | 50 // otherwise, when the object is signaled, there would be no loop to post the |
| 52 // thread. | 51 // callback task to. This means that you cannot use ObjectWatcher in test code |
| 53 class BASE_EXPORT ObjectWatcher { | 52 // that doesn't create a message loop (unless you add such a loop). |
| 53 class BASE_EXPORT ObjectWatcher : public MessageLoop::DestructionObserver { |
| 54 public: | 54 public: |
| 55 class BASE_EXPORT Delegate { | 55 class BASE_EXPORT Delegate { |
| 56 public: | 56 public: |
| 57 virtual ~Delegate() {} | 57 virtual ~Delegate() {} |
| 58 // Called from the thread that started the watch when a signaled object is | 58 // Called from the MessageLoop when a signaled object is detected. To |
| 59 // detected. To continue watching the object, StartWatching must be called | 59 // continue watching the object, StartWatching must be called again. |
| 60 // again. | |
| 61 virtual void OnObjectSignaled(HANDLE object) = 0; | 60 virtual void OnObjectSignaled(HANDLE object) = 0; |
| 62 }; | 61 }; |
| 63 | 62 |
| 64 ObjectWatcher(); | 63 ObjectWatcher(); |
| 65 ~ObjectWatcher(); | 64 ~ObjectWatcher() override; |
| 66 | 65 |
| 67 // When the object is signaled, the given delegate is notified on the thread | 66 // When the object is signaled, the given delegate is notified on the thread |
| 68 // where StartWatchingOnce is called. The ObjectWatcher is not responsible for | 67 // where StartWatchingOnce is called. The ObjectWatcher is not responsible for |
| 69 // deleting the delegate. | 68 // deleting the delegate. |
| 70 // Returns whether watching was successfully initiated. | 69 // Returns whether watching was successfully initiated. |
| 71 bool StartWatchingOnce(HANDLE object, Delegate* delegate); | 70 bool StartWatchingOnce(HANDLE object, Delegate* delegate); |
| 72 | 71 |
| 73 // Notifies the delegate, on the thread where this method is called, each time | 72 // Notifies the delegate, on the thread where this method is called, each time |
| 74 // the object is set. By definition, the handle must be an auto-reset object. | 73 // the object is set. By definition, the handle must be an auto-reset object. |
| 75 // The caller must ensure that it (or any Windows system code) doesn't reset | 74 // The caller must ensure that it (or any Windows system code) doesn't reset |
| (...skipping 17 matching lines...) Expand all Loading... |
| 93 private: | 92 private: |
| 94 // Called on a background thread when done waiting. | 93 // Called on a background thread when done waiting. |
| 95 static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out); | 94 static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out); |
| 96 | 95 |
| 97 // Helper used by StartWatchingOnce and StartWatchingMultipleTimes. | 96 // Helper used by StartWatchingOnce and StartWatchingMultipleTimes. |
| 98 bool StartWatchingInternal(HANDLE object, Delegate* delegate, | 97 bool StartWatchingInternal(HANDLE object, Delegate* delegate, |
| 99 bool execute_only_once); | 98 bool execute_only_once); |
| 100 | 99 |
| 101 void Signal(Delegate* delegate); | 100 void Signal(Delegate* delegate); |
| 102 | 101 |
| 103 void Reset(); | 102 // MessageLoop::DestructionObserver implementation: |
| 103 void WillDestroyCurrentMessageLoop() override; |
| 104 | 104 |
| 105 // A callback pre-bound to Signal() that is posted to the caller's task runner | 105 // Internal state. |
| 106 // when the wait completes. | |
| 107 Closure callback_; | 106 Closure callback_; |
| 108 | 107 HANDLE object_; // The object being watched |
| 109 // The object being watched. | 108 HANDLE wait_object_; // Returned by RegisterWaitForSingleObject |
| 110 HANDLE object_ = nullptr; | 109 MessageLoop* origin_loop_; // Used to get back to the origin thread |
| 111 | 110 bool run_once_; |
| 112 // The wait handle returned by RegisterWaitForSingleObject. | |
| 113 HANDLE wait_object_ = nullptr; | |
| 114 | |
| 115 // The task runner of the thread on which the watch was started. | |
| 116 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
| 117 | |
| 118 bool run_once_ = true; | |
| 119 | |
| 120 WeakPtrFactory<ObjectWatcher> weak_factory_; | 111 WeakPtrFactory<ObjectWatcher> weak_factory_; |
| 121 | 112 |
| 122 DISALLOW_COPY_AND_ASSIGN(ObjectWatcher); | 113 DISALLOW_COPY_AND_ASSIGN(ObjectWatcher); |
| 123 }; | 114 }; |
| 124 | 115 |
| 125 } // namespace win | 116 } // namespace win |
| 126 } // namespace base | 117 } // namespace base |
| 127 | 118 |
| 128 #endif // BASE_WIN_OBJECT_WATCHER_H_ | 119 #endif // BASE_WIN_OBJECT_WATCHER_H_ |
| OLD | NEW |