blob: 66ad9eba8a692e48f4e9f7ab27b7f98b421d2c4c [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]586b3b02010-08-02 21:26:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_
6#define CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_
7
8#include <windows.h>
9
10#include <string>
11#include <vector>
[email protected]f5d1ea322010-09-28 13:47:1312#include <utility>
13
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/linked_ptr.h"
[email protected]a8d1ebbe2011-01-01 18:26:1615#include "base/win/object_watcher.h"
[email protected]586b3b02010-08-02 21:26:3416
17struct FunctionStub;
18
19// Listens to WinEvents from the WinEventReceiver.
20class WinEventListener {
21 public:
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.
[email protected]39f7f172010-08-17 17:04:2425 virtual void OnEventReceived(DWORD event, HWND hwnd, LONG object_id,
26 LONG child_id) = 0;
[email protected]586b3b02010-08-02 21:26:3427};
28
29// Receives WinEvents and forwards them to its listener. The event types the
30// listener wants to receive can be specified.
31class WinEventReceiver {
32 public:
33 WinEventReceiver();
34 ~WinEventReceiver();
35
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);
40
41 // Same as above, but sets a range of events to listen for.
42 void SetListenerForEvents(WinEventListener* listener, DWORD event_min,
43 DWORD event_max);
44
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();
48
49 private:
50 bool InitializeHook(DWORD event_min, DWORD event_max);
51
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);
55
56 WinEventListener* listener_;
57 HWINEVENTHOOK hook_;
58 FunctionStub* hook_stub_;
59};
60
[email protected]f5d1ea322010-09-28 13:47:1361// Receives notifications when a window is opened or closed.
[email protected]586b3b02010-08-02 21:26:3462class WindowObserver {
63 public:
64 virtual ~WindowObserver() {}
[email protected]f5d1ea322010-09-28 13:47:1365 virtual void OnWindowOpen(HWND hwnd) = 0;
66 virtual void OnWindowClose(HWND hwnd) = 0;
[email protected]586b3b02010-08-02 21:26:3467};
68
[email protected]f5d1ea322010-09-28 13:47:1369// 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
74// the window).
75//
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.
78//
79// Observers are not notified of the closing of windows that were already open
80// when they were registered.
81//
82// Observers may call AddObserver and/or RemoveObserver during notifications.
83//
84// Each instance of this class must only be accessed from a single thread, and
85// that thread must be running a message loop.
[email protected]586b3b02010-08-02 21:26:3486class WindowWatchdog : public WinEventListener {
87 public:
[email protected]f5d1ea322010-09-28 13:47:1388 WindowWatchdog();
89 // Register |observer| to be notified when windows matching |caption_pattern|
[email protected]0b7a012d2010-10-19 23:49:2690 // 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.
[email protected]f5d1ea322010-09-28 13:47:1395 void AddObserver(WindowObserver* observer,
[email protected]0b7a012d2010-10-19 23:49:2696 const std::string& caption_pattern,
97 const std::string& class_name_pattern);
[email protected]586b3b02010-08-02 21:26:3498
[email protected]f5d1ea322010-09-28 13:47:1399 // Remove all registrations of |observer|. The |observer| will not be notified
100 // during or after this call.
[email protected]586b3b02010-08-02 21:26:34101 void RemoveObserver(WindowObserver* observer);
102
103 private:
[email protected]f5d1ea322010-09-28 13:47:13104 class ProcessExitObserver;
105
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.
[email protected]a8d1ebbe2011-01-01 18:26:16109 typedef std::pair<HWND, linked_ptr<base::win::ObjectWatcher::Delegate> >
110 OpenWindowEntry;
[email protected]f5d1ea322010-09-28 13:47:13111 typedef std::vector<OpenWindowEntry> OpenWindowList;
112
113 struct ObserverEntry {
[email protected]586b3b02010-08-02 21:26:34114 WindowObserver* observer;
[email protected]f5d1ea322010-09-28 13:47:13115 std::string caption_pattern;
[email protected]0b7a012d2010-10-19 23:49:26116 std::string class_name_pattern;
[email protected]f5d1ea322010-09-28 13:47:13117 OpenWindowList open_windows;
[email protected]586b3b02010-08-02 21:26:34118 };
119
[email protected]f5d1ea322010-09-28 13:47:13120 typedef std::vector<ObserverEntry> ObserverEntryList;
[email protected]586b3b02010-08-02 21:26:34121
[email protected]f5d1ea322010-09-28 13:47:13122 // WinEventListener implementation.
123 virtual void OnEventReceived(
[email protected]a8d1ebbe2011-01-01 18:26:16124 DWORD event, HWND hwnd, LONG object_id, LONG child_id);
[email protected]586b3b02010-08-02 21:26:34125
[email protected]f5d1ea322010-09-28 13:47:13126 static std::string GetWindowCaption(HWND hwnd);
127
128 void HandleOnOpen(HWND hwnd);
129 void HandleOnClose(HWND hwnd);
130 void OnHwndProcessExited(HWND hwnd);
131
[email protected]0b7a012d2010-10-19 23:49:26132 // 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);
137
[email protected]f5d1ea322010-09-28 13:47:13138 ObserverEntryList observers_;
[email protected]586b3b02010-08-02 21:26:34139 WinEventReceiver win_event_receiver_;
[email protected]f5d1ea322010-09-28 13:47:13140
141 DISALLOW_COPY_AND_ASSIGN(WindowWatchdog);
[email protected]586b3b02010-08-02 21:26:34142};
143
[email protected]f5d1ea322010-09-28 13:47:13144
145
[email protected]39f7f172010-08-17 17:04:24146#endif // CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_