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