summaryrefslogtreecommitdiffstats
path: root/chrome_frame/test/win_event_receiver.cc
blob: a187556214c0edf49864f87b93fdeb9c87b946f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome_frame/test/win_event_receiver.h"

#include "base/logging.h"
#include "base/message_loop.h"
#include "base/win/object_watcher.h"
#include "base/string_util.h"

#include "chrome_frame/function_stub.h"

// WinEventReceiver methods
WinEventReceiver::WinEventReceiver()
    : listener_(NULL),
      hook_(NULL),
      hook_stub_(NULL) {
}

WinEventReceiver::~WinEventReceiver() {
  StopReceivingEvents();
}

void WinEventReceiver::SetListenerForEvent(WinEventListener* listener,
                                           DWORD event) {
  SetListenerForEvents(listener, event, event);
}

void WinEventReceiver::SetListenerForEvents(WinEventListener* listener,
                                            DWORD event_min, DWORD event_max) {
  DCHECK(listener != NULL);
  StopReceivingEvents();

  listener_ = listener;

  InitializeHook(event_min, event_max);
}

void WinEventReceiver::StopReceivingEvents() {
  if (hook_) {
    ::UnhookWinEvent(hook_);
    hook_ = NULL;
    FunctionStub::Destroy(hook_stub_);
    hook_stub_ = NULL;
  }
}

bool WinEventReceiver::InitializeHook(DWORD event_min, DWORD event_max) {
  DCHECK(hook_ == NULL);
  DCHECK(hook_stub_ == NULL);
  hook_stub_ = FunctionStub::Create(reinterpret_cast<uintptr_t>(this),
                                    WinEventHook);
  // Don't use WINEVENT_SKIPOWNPROCESS here because we fake generate an event
  // in the mock IE event sink (IA2_EVENT_DOCUMENT_LOAD_COMPLETE) that we want
  // to catch.
  hook_ = SetWinEventHook(event_min, event_max, NULL,
                          reinterpret_cast<WINEVENTPROC>(hook_stub_->code()), 0,
                          0, WINEVENT_OUTOFCONTEXT);
  DLOG_IF(ERROR, hook_ == NULL) << "Unable to SetWinEvent hook";
  return hook_ != NULL;
}

// static
void WinEventReceiver::WinEventHook(WinEventReceiver* me, HWINEVENTHOOK hook,
                                    DWORD event, HWND hwnd, LONG object_id,
                                    LONG child_id, DWORD event_thread_id,
                                    DWORD event_time) {
  DCHECK(me->listener_ != NULL);
  me->listener_->OnEventReceived(event, hwnd, object_id, child_id);
}

// Notifies WindowWatchdog when the process owning a given window exits.
//
// If the process terminates before its handle may be obtained, this class will
// still properly notifyy the WindowWatchdog.
//
// Notification is always delivered via a message loop task in the message loop
// that is active when the instance is constructed.
class WindowWatchdog::ProcessExitObserver
    : public base::win::ObjectWatcher::Delegate {
 public:
  // Initiates the process watch. Will always return without notifying the
  // watchdog.
  ProcessExitObserver(WindowWatchdog* window_watchdog, HWND hwnd);
  virtual ~ProcessExitObserver();

  // base::ObjectWatcher::Delegate implementation
  virtual void OnObjectSignaled(HANDLE process_handle);

 private:
  WindowWatchdog* window_watchdog_;
  HANDLE process_handle_;
  HWND hwnd_;

  ScopedRunnableMethodFactory<ProcessExitObserver> method_task_factory_;
  base::win::ObjectWatcher object_watcher_;

  DISALLOW_COPY_AND_ASSIGN(ProcessExitObserver);
};

WindowWatchdog::ProcessExitObserver::ProcessExitObserver(
    WindowWatchdog* window_watchdog, HWND hwnd)
    : window_watchdog_(window_watchdog),
      process_handle_(NULL),
      hwnd_(hwnd),
      ALLOW_THIS_IN_INITIALIZER_LIST(method_task_factory_(this)) {
  DWORD pid = 0;
  ::GetWindowThreadProcessId(hwnd, &pid);
  if (pid != 0) {
    process_handle_ = ::OpenProcess(SYNCHRONIZE, FALSE, pid);
  }

  if (process_handle_ != NULL) {
    object_watcher_.StartWatching(process_handle_, this);
  } else {
    // Process is gone, so the window must be gone too. Notify our observer!
    MessageLoop::current()->PostTask(
        FROM_HERE,
        method_task_factory_.NewRunnableMethod(
            &ProcessExitObserver::OnObjectSignaled, HANDLE(NULL)));
  }
}

WindowWatchdog::ProcessExitObserver::~ProcessExitObserver() {
  if (process_handle_ != NULL) {
    ::CloseHandle(process_handle_);
  }
}

void WindowWatchdog::ProcessExitObserver::OnObjectSignaled(
    HANDLE process_handle) {
  window_watchdog_->OnHwndProcessExited(hwnd_);
}

WindowWatchdog::WindowWatchdog() {}

void WindowWatchdog::AddObserver(WindowObserver* observer,
                                 const std::string& caption_pattern,
                                 const std::string& class_name_pattern) {
  if (observers_.empty()) {
    // SetListenerForEvents takes an event_min and event_max.
    // EVENT_OBJECT_DESTROY, EVENT_OBJECT_SHOW, and EVENT_OBJECT_HIDE are
    // consecutive, in that order; hence we supply only DESTROY and HIDE to
    // denote exactly the required set.
    win_event_receiver_.SetListenerForEvents(
        this, EVENT_OBJECT_DESTROY, EVENT_OBJECT_HIDE);
  }

  ObserverEntry new_entry = {
      observer,
      caption_pattern,
      class_name_pattern,
      OpenWindowList() };

  observers_.push_back(new_entry);
}

void WindowWatchdog::RemoveObserver(WindowObserver* observer) {
  for (ObserverEntryList::iterator i = observers_.begin();
       i != observers_.end(); ) {
    i = (observer == i->observer) ? observers_.erase(i) : ++i;
  }

  if (observers_.empty())
    win_event_receiver_.StopReceivingEvents();
}

std::string WindowWatchdog::GetWindowCaption(HWND hwnd) {
  std::string caption;
  int len = ::GetWindowTextLength(hwnd) + 1;
  ::GetWindowTextA(hwnd, WriteInto(&caption, len), len);

  return caption;
}

bool WindowWatchdog::MatchingWindow(const ObserverEntry& entry,
                                    const std::string& caption,
                                    const std::string& class_name) {
  bool should_match_caption = !entry.caption_pattern.empty();
  bool should_match_class = !entry.class_name_pattern.empty();

  if (should_match_caption &&
      MatchPattern(caption, entry.caption_pattern) &&
      !should_match_class) {
    return true;
  }
  if (should_match_class &&
      MatchPattern(class_name, entry.class_name_pattern)) {
    return true;
  }
  return false;
}

void WindowWatchdog::HandleOnOpen(HWND hwnd) {
  std::string caption = GetWindowCaption(hwnd);
  char class_name[MAX_PATH] = {0};
  GetClassNameA(hwnd, class_name, arraysize(class_name));

  // Instantiated only if there is at least one interested observer. Each
  // interested observer will maintain a reference to this object, such that it
  // is deleted when the last observer disappears.
  linked_ptr<ProcessExitObserver> process_exit_observer;

  // Identify the interested observers and mark them as watching this HWND for
  // close.
  ObserverEntryList interested_observers;
  for (ObserverEntryList::iterator entry_iter = observers_.begin();
       entry_iter != observers_.end(); ++entry_iter) {
    if (MatchingWindow(*entry_iter, caption, class_name)) {
      if (process_exit_observer == NULL) {
        process_exit_observer.reset(new ProcessExitObserver(this, hwnd));
      }

      entry_iter->open_windows.push_back(
          OpenWindowEntry(hwnd, process_exit_observer));

      interested_observers.push_back(*entry_iter);
    }
  }

  // Notify the interested observers in a separate pass in case AddObserver or
  // RemoveObserver is called as a side-effect of the notification.
  for (ObserverEntryList::iterator entry_iter = interested_observers.begin();
       entry_iter != interested_observers.end(); ++entry_iter) {
    entry_iter->observer->OnWindowOpen(hwnd);
  }
}

void WindowWatchdog::HandleOnClose(HWND hwnd) {
  // Identify the interested observers, reaping OpenWindow entries as
  // appropriate
  ObserverEntryList interested_observers;
  for (ObserverEntryList::iterator entry_iter = observers_.begin();
       entry_iter != observers_.end(); ++entry_iter) {
    size_t num_open_windows = entry_iter->open_windows.size();

    OpenWindowList::iterator window_iter = entry_iter->open_windows.begin();
    while (window_iter != entry_iter->open_windows.end()) {
      if (hwnd == window_iter->first) {
        window_iter = entry_iter->open_windows.erase(window_iter);
      } else {
        ++window_iter;
      }
    }

    if (num_open_windows != entry_iter->open_windows.size()) {
      interested_observers.push_back(*entry_iter);
    }
  }

  // Notify the interested observers in a separate pass in case AddObserver or
  // RemoveObserver is called as a side-effect of the notification.
  for (ObserverEntryList::iterator entry_iter = interested_observers.begin();
    entry_iter != interested_observers.end(); ++entry_iter) {
    entry_iter->observer->OnWindowClose(hwnd);
  }
}

void WindowWatchdog::OnEventReceived(
    DWORD event, HWND hwnd, LONG object_id, LONG child_id) {
  // We need to look for top level windows and a natural check is for
  // WS_CHILD. Instead, checking for WS_CAPTION allows us to filter
  // out other stray popups
  if (event == EVENT_OBJECT_SHOW) {
    HandleOnOpen(hwnd);
  } else {
    DCHECK(event == EVENT_OBJECT_DESTROY || event == EVENT_OBJECT_HIDE);
    HandleOnClose(hwnd);
  }
}

void WindowWatchdog::OnHwndProcessExited(HWND hwnd) {
  HandleOnClose(hwnd);
}