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