1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/object_watcher.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_OBJECT_WATCHER_H_ 1.9 +#define BASE_OBJECT_WATCHER_H_ 1.10 + 1.11 +#include <windows.h> 1.12 +#ifdef GetClassName 1.13 +#undef GetClassName 1.14 +#endif 1.15 + 1.16 +#include "base/message_loop.h" 1.17 + 1.18 +namespace base { 1.19 + 1.20 +// A class that provides a means to asynchronously wait for a Windows object to 1.21 +// become signaled. It is an abstraction around RegisterWaitForSingleObject 1.22 +// that provides a notification callback, OnObjectSignaled, that runs back on 1.23 +// the origin thread (i.e., the thread that called StartWatching). 1.24 +// 1.25 +// This class acts like a smart pointer such that when it goes out-of-scope, 1.26 +// UnregisterWaitEx is automatically called, and any in-flight notification is 1.27 +// suppressed. 1.28 +// 1.29 +// Typical usage: 1.30 +// 1.31 +// class MyClass : public base::ObjectWatcher::Delegate { 1.32 +// public: 1.33 +// void DoStuffWhenSignaled(HANDLE object) { 1.34 +// watcher_.StartWatching(object, this); 1.35 +// } 1.36 +// virtual void OnObjectSignaled(HANDLE object) { 1.37 +// // OK, time to do stuff! 1.38 +// } 1.39 +// private: 1.40 +// base::ObjectWatcher watcher_; 1.41 +// }; 1.42 +// 1.43 +// In the above example, MyClass wants to "do stuff" when object becomes 1.44 +// signaled. ObjectWatcher makes this task easy. When MyClass goes out of 1.45 +// scope, the watcher_ will be destroyed, and there is no need to worry about 1.46 +// OnObjectSignaled being called on a deleted MyClass pointer. Easy! 1.47 +// 1.48 +class ObjectWatcher : public MessageLoop::DestructionObserver { 1.49 + public: 1.50 + class Delegate { 1.51 + public: 1.52 + virtual ~Delegate() {} 1.53 + // Called from the MessageLoop when a signaled object is detected. To 1.54 + // continue watching the object, AddWatch must be called again. 1.55 + virtual void OnObjectSignaled(HANDLE object) = 0; 1.56 + }; 1.57 + 1.58 + ObjectWatcher(); 1.59 + ~ObjectWatcher(); 1.60 + 1.61 + // When the object is signaled, the given delegate is notified on the thread 1.62 + // where StartWatching is called. The ObjectWatcher is not responsible for 1.63 + // deleting the delegate. 1.64 + // 1.65 + // Returns true if the watch was started. Otherwise, false is returned. 1.66 + // 1.67 + bool StartWatching(HANDLE object, Delegate* delegate); 1.68 + 1.69 + // Stops watching. Does nothing if the watch has already completed. If the 1.70 + // watch is still active, then it is canceled, and the associated delegate is 1.71 + // not notified. 1.72 + // 1.73 + // Returns true if the watch was canceled. Otherwise, false is returned. 1.74 + // 1.75 + bool StopWatching(); 1.76 + 1.77 + // Returns the handle of the object being watched, or NULL if the object 1.78 + // watcher is stopped. 1.79 + HANDLE GetWatchedObject(); 1.80 + 1.81 + private: 1.82 + // Called on a background thread when done waiting. 1.83 + static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out); 1.84 + 1.85 + // MessageLoop::DestructionObserver implementation: 1.86 + virtual void WillDestroyCurrentMessageLoop(); 1.87 + 1.88 + // Internal state. 1.89 + struct Watch; 1.90 + Watch* watch_; 1.91 + 1.92 + DISALLOW_COPY_AND_ASSIGN(ObjectWatcher); 1.93 +}; 1.94 + 1.95 +} // namespace base 1.96 + 1.97 +#endif // BASE_OBJECT_WATCHER_H_