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.
5 #ifndef CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_
6 #define CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_
14 #include "base/memory/linked_ptr.h"
15 #include "base/win/object_watcher.h"
19 // Listens to WinEvents from the WinEventReceiver.
20 class WinEventListener
{
22 virtual ~WinEventListener() {}
23 // Called when an event has been received. |hwnd| is the window that generated
24 // the event, or null if no window is associated with the event.
25 virtual void OnEventReceived(DWORD event
, HWND hwnd
, LONG object_id
,
29 // Receives WinEvents and forwards them to its listener. The event types the
30 // listener wants to receive can be specified.
31 class WinEventReceiver
{
36 // Sets the sole listener of this receiver. The listener will receive all
37 // WinEvents of the given event type. Any previous listener will be
38 // replaced. |listener| should not be NULL.
39 void SetListenerForEvent(WinEventListener
* listener
, DWORD event
);
41 // Same as above, but sets a range of events to listen for.
42 void SetListenerForEvents(WinEventListener
* listener
, DWORD event_min
,
45 // Stops receiving events and forwarding them to the listener. It is
46 // permitted to call this even if the receiver has already been stopped.
47 void StopReceivingEvents();
50 bool InitializeHook(DWORD event_min
, DWORD event_max
);
52 static void CALLBACK
WinEventHook(WinEventReceiver
* me
, HWINEVENTHOOK hook
,
53 DWORD event
, HWND hwnd
, LONG object_id
, LONG child_id
,
54 DWORD event_thread_id
, DWORD event_time
);
56 WinEventListener
* listener_
;
58 FunctionStub
* hook_stub_
;
61 // Receives notifications when a window is opened or closed.
62 class WindowObserver
{
64 virtual ~WindowObserver() {}
65 virtual void OnWindowOpen(HWND hwnd
) = 0;
66 virtual void OnWindowClose(HWND hwnd
) = 0;
69 // Notifies observers when windows whose captions match specified patterns
70 // open or close. When a window opens, its caption is compared to the patterns
71 // associated with each observer. Observers registered with matching patterns
72 // are notified of the window's opening and will be notified when the same
73 // window is closed (including if the owning process terminates without closing
76 // Changes to a window's caption while it is open do not affect the set of
77 // observers to be notified when it closes.
79 // Observers are not notified of the closing of windows that were already open
80 // when they were registered.
82 // Observers may call AddObserver and/or RemoveObserver during notifications.
84 // Each instance of this class must only be accessed from a single thread, and
85 // that thread must be running a message loop.
86 class WindowWatchdog
: public WinEventListener
{
89 // Register |observer| to be notified when windows matching |caption_pattern|
90 // and/or |class_name_pattern| are opened or closed. A single observer may be
91 // registered multiple times.
92 // If a single window caption and/or class name matches multiple
93 // registrations of a single observer, the observer will be notified once per
94 // matching registration.
95 void AddObserver(WindowObserver
* observer
,
96 const std::string
& caption_pattern
,
97 const std::string
& class_name_pattern
);
99 // Remove all registrations of |observer|. The |observer| will not be notified
100 // during or after this call.
101 void RemoveObserver(WindowObserver
* observer
);
104 class ProcessExitObserver
;
106 // The Delegate object is actually a ProcessExitObserver, but declaring
107 // it as such would require fully declaring the ProcessExitObserver class
108 // here in order for linked_ptr to access its destructor.
109 typedef std::pair
<HWND
, linked_ptr
<base::win::ObjectWatcher::Delegate
> >
111 typedef std::vector
<OpenWindowEntry
> OpenWindowList
;
113 struct ObserverEntry
{
114 WindowObserver
* observer
;
115 std::string caption_pattern
;
116 std::string class_name_pattern
;
117 OpenWindowList open_windows
;
120 typedef std::vector
<ObserverEntry
> ObserverEntryList
;
122 // WinEventListener implementation.
123 virtual void OnEventReceived(
124 DWORD event
, HWND hwnd
, LONG object_id
, LONG child_id
);
126 static std::string
GetWindowCaption(HWND hwnd
);
128 void HandleOnOpen(HWND hwnd
);
129 void HandleOnClose(HWND hwnd
);
130 void OnHwndProcessExited(HWND hwnd
);
132 // Returns true if the caption pattern and/or the class name pattern in the
133 // observer entry structure matches the caption and/or class name passed in.
134 bool MatchingWindow(const ObserverEntry
& entry
,
135 const std::string
& caption
,
136 const std::string
& class_name
);
138 ObserverEntryList observers_
;
139 WinEventReceiver win_event_receiver_
;
141 DISALLOW_COPY_AND_ASSIGN(WindowWatchdog
);
146 #endif // CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_