summaryrefslogtreecommitdiffstats
path: root/chrome_frame/event_hooker.cc
blob: 839b3adcd20f4005aa1f6b8c5c5bb27f248cc706 (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
// 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/event_hooker.h"

#include <crtdbg.h>
#include "chrome_frame/bho_loader.h"

EXTERN_C IMAGE_DOS_HEADER __ImageBase;

EventHooker::EventHooker()
: window_creation_hook_(NULL) {}

EventHooker::~EventHooker() {
  StopHook();
}

bool EventHooker::StartHook() {
  if ((NULL != window_creation_hook_)) {
    return false;
  }

  window_creation_hook_ = SetWinEventHook(EVENT_OBJECT_CREATE,
                                          EVENT_OBJECT_CREATE,
                                          reinterpret_cast<HMODULE>(
                                              &__ImageBase),
                                          WindowCreationHookProc,
                                          0,
                                          0,
                                          WINEVENT_INCONTEXT);
  if (NULL == window_creation_hook_) {
    return false;
  }
  return true;
}

void EventHooker::StopHook() {
  if (NULL != window_creation_hook_) {
    UnhookWinEvent(window_creation_hook_);
    window_creation_hook_ = NULL;
  }
}

VOID CALLBACK EventHooker::WindowCreationHookProc(HWINEVENTHOOK hook,
                                                  DWORD event,
                                                  HWND window,
                                                  LONG object_id,
                                                  LONG child_id,
                                                  DWORD event_tid,
                                                  DWORD event_time) {
  _ASSERTE((EVENT_OBJECT_CREATE == event) ||
           (EVENT_OBJECT_PARENTCHANGE == event));
  if (OBJID_WINDOW == object_id) {
    BHOLoader::GetInstance()->OnHookEvent(event, window);
  }
}