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