[email protected] | 586b3b0 | 2010-08-02 21:26:34 | [diff] [blame^] | 1 | // Copyright (c) 2010 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 | |
| 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> |
| 12 | |
| 13 | struct FunctionStub; |
| 14 | |
| 15 | // Listens to WinEvents from the WinEventReceiver. |
| 16 | class WinEventListener { |
| 17 | public: |
| 18 | virtual ~WinEventListener() {} |
| 19 | // Called when an event has been received. |hwnd| is the window that generated |
| 20 | // the event, or null if no window is associated with the event. |
| 21 | virtual void OnEventReceived(DWORD event, HWND hwnd) = 0; |
| 22 | }; |
| 23 | |
| 24 | // Receives WinEvents and forwards them to its listener. The event types the |
| 25 | // listener wants to receive can be specified. |
| 26 | class WinEventReceiver { |
| 27 | public: |
| 28 | WinEventReceiver(); |
| 29 | ~WinEventReceiver(); |
| 30 | |
| 31 | // Sets the sole listener of this receiver. The listener will receive all |
| 32 | // WinEvents of the given event type. Any previous listener will be |
| 33 | // replaced. |listener| should not be NULL. |
| 34 | void SetListenerForEvent(WinEventListener* listener, DWORD event); |
| 35 | |
| 36 | // Same as above, but sets a range of events to listen for. |
| 37 | void SetListenerForEvents(WinEventListener* listener, DWORD event_min, |
| 38 | DWORD event_max); |
| 39 | |
| 40 | // Stops receiving events and forwarding them to the listener. It is |
| 41 | // permitted to call this even if the receiver has already been stopped. |
| 42 | void StopReceivingEvents(); |
| 43 | |
| 44 | private: |
| 45 | bool InitializeHook(DWORD event_min, DWORD event_max); |
| 46 | |
| 47 | static void CALLBACK WinEventHook(WinEventReceiver* me, HWINEVENTHOOK hook, |
| 48 | DWORD event, HWND hwnd, LONG object_id, LONG child_id, |
| 49 | DWORD event_thread_id, DWORD event_time); |
| 50 | |
| 51 | WinEventListener* listener_; |
| 52 | HWINEVENTHOOK hook_; |
| 53 | FunctionStub* hook_stub_; |
| 54 | }; |
| 55 | |
| 56 | // Observes window show events. Used with WindowWatchdog. |
| 57 | class WindowObserver { |
| 58 | public: |
| 59 | virtual ~WindowObserver() {} |
| 60 | // Called when a window has been shown. |
| 61 | virtual void OnWindowDetected(HWND hwnd, const std::string& caption) = 0; |
| 62 | }; |
| 63 | |
| 64 | // Watch a for window to be shown with the given window class name. |
| 65 | // If found, call the observer interested in it. |
| 66 | class WindowWatchdog : public WinEventListener { |
| 67 | public: |
| 68 | // Register for notifications for |window_class|. An observer can register |
| 69 | // for multiple notifications. |
| 70 | void AddObserver(WindowObserver* observer, const std::string& window_class); |
| 71 | |
| 72 | // Remove all entries for |observer|. |
| 73 | void RemoveObserver(WindowObserver* observer); |
| 74 | |
| 75 | private: |
| 76 | struct WindowObserverEntry { |
| 77 | WindowObserver* observer; |
| 78 | std::string window_class; |
| 79 | }; |
| 80 | |
| 81 | typedef std::vector<WindowObserverEntry> ObserverMap; |
| 82 | |
| 83 | // Overriden from WinEventListener. |
| 84 | virtual void OnEventReceived(DWORD event, HWND hwnd); |
| 85 | |
| 86 | ObserverMap observers_; |
| 87 | WinEventReceiver win_event_receiver_; |
| 88 | }; |
| 89 | |
| 90 | #endif // CHROME_FRAME_TEST_WIN_EVENT_RECEIVER_H_ |