object_watcher.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_OBJECT_WATCHER_H_
  5. #define BASE_WIN_OBJECT_WATCHER_H_
  6. #include "base/win/windows_types.h"
  7. #include "base/base_export.h"
  8. #include "base/callback.h"
  9. #include "base/macros.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/sequenced_task_runner.h"
  13. namespace base {
  14. namespace win {
  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 sequence (i.e., the sequence 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. // The waiting handle MUST NOT be closed while watching is in progress. If this
  25. // handle is closed while the wait is still pending, the behavior is undefined
  26. // (see MSDN:RegisterWaitForSingleObject).
  27. //
  28. // Typical usage:
  29. //
  30. // class MyClass : public base::win::ObjectWatcher::Delegate {
  31. // public:
  32. // void DoStuffWhenSignaled(HANDLE object) {
  33. // watcher_.StartWatchingOnce(object, this);
  34. // }
  35. // void OnObjectSignaled(HANDLE object) override {
  36. // // OK, time to do stuff!
  37. // }
  38. // private:
  39. // base::win::ObjectWatcher watcher_;
  40. // };
  41. //
  42. // 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. // 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. // If the object is already signaled before being watched, OnObjectSignaled is
  47. // still called after (but not necessarily immediately after) watch is started.
  48. //
  49. // NOTE: Except for the constructor, all public methods of this class must be
  50. // called in sequence, in a scope where SequencedTaskRunnerHandle::IsSet().
  51. class BASE_EXPORT ObjectWatcher {
  52. public:
  53. class BASE_EXPORT Delegate {
  54. public:
  55. virtual ~Delegate() = default;
  56. // Called from the sequence that started the watch when a signaled object is
  57. // detected. To continue watching the object, StartWatching must be called
  58. // again.
  59. virtual void OnObjectSignaled(HANDLE object) = 0;
  60. };
  61. ObjectWatcher();
  62. ~ObjectWatcher();
  63. // When the object is signaled, the given delegate is notified on the sequence
  64. // where StartWatchingOnce is called. The ObjectWatcher is not responsible for
  65. // deleting the delegate.
  66. // Returns whether watching was successfully initiated.
  67. bool StartWatchingOnce(HANDLE object,
  68. Delegate* delegate,
  69. const Location& from_here = Location::Current());
  70. // Notifies the delegate, on the sequence where this method is called, each
  71. // time the object is set. By definition, the handle must be an auto-reset
  72. // object. The caller must ensure that it (or any Windows system code) doesn't
  73. // reset the event or else the delegate won't be called.
  74. // Returns whether watching was successfully initiated.
  75. bool StartWatchingMultipleTimes(
  76. HANDLE object,
  77. Delegate* delegate,
  78. const Location& from_here = Location::Current());
  79. // Stops watching. Does nothing if the watch has already completed. If the
  80. // watch is still active, then it is canceled, and the associated delegate is
  81. // not notified.
  82. //
  83. // Returns true if the watch was canceled. Otherwise, false is returned.
  84. bool StopWatching();
  85. // Returns true if currently watching an object.
  86. bool IsWatching() const;
  87. // Returns the handle of the object being watched.
  88. HANDLE GetWatchedObject() const;
  89. private:
  90. // Called on a background thread when done waiting.
  91. static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out);
  92. // Helper used by StartWatchingOnce and StartWatchingMultipleTimes.
  93. bool StartWatchingInternal(HANDLE object,
  94. Delegate* delegate,
  95. bool execute_only_once,
  96. const Location& from_here);
  97. void Signal(Delegate* delegate);
  98. void Reset();
  99. Location location_;
  100. // A callback pre-bound to Signal() that is posted to the caller's task runner
  101. // when the wait completes.
  102. RepeatingClosure callback_;
  103. // The object being watched.
  104. HANDLE object_ = nullptr;
  105. // The wait handle returned by RegisterWaitForSingleObject.
  106. HANDLE wait_object_ = nullptr;
  107. // The task runner of the sequence on which the watch was started.
  108. scoped_refptr<SequencedTaskRunner> task_runner_;
  109. bool run_once_ = true;
  110. WeakPtrFactory<ObjectWatcher> weak_factory_{this};
  111. DISALLOW_COPY_AND_ASSIGN(ObjectWatcher);
  112. };
  113. } // namespace win
  114. } // namespace base
  115. #endif // BASE_WIN_OBJECT_WATCHER_H_