blob: 55822618cf2f8d743ca020b3029697d7ae31a5c7 [file] [log] [blame]
[email protected]9ceda772010-08-30 15:34:111// 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// chrome_frame_helper_main.cc : The .exe that bootstraps the
6// chrome_frame_helper.dll.
7//
8// This is a small exe that loads the hook dll to set the event hooks and then
9// waits in a message loop. It is intended to be shut down by looking for a
10// window with the class and title
11// kChromeFrameHelperWindowClassName and kChromeFrameHelperWindowName and then
12// sending that window a WM_CLOSE message.
13//
14
15#include <crtdbg.h>
16#include <windows.h>
17
[email protected]e4fe427b2010-09-03 19:57:4218#include "chrome_frame/crash_server_init.h"
19
[email protected]9ceda772010-08-30 15:34:1120// Window class and window names.
21const wchar_t kChromeFrameHelperWindowClassName[] =
22 L"ChromeFrameHelperWindowClass";
23const wchar_t kChromeFrameHelperWindowName[] =
24 L"ChromeFrameHelperWindowName";
25
26// Small helper class that assists in loading the DLL that contains code
27// to register our event hook callback. Automatically removes the hook and
28// unloads the DLL on destruction.
29class HookDllLoader {
30 public:
31 HookDllLoader() : dll_(NULL), start_proc_(NULL), stop_proc_(NULL) {}
32 ~HookDllLoader() {
33 if (dll_) {
34 Unload();
35 }
36 }
37
38 bool Load() {
39 dll_ = LoadLibrary(L"chrome_frame_helper.dll");
40 if (dll_) {
41 start_proc_ = GetProcAddress(dll_, "StartUserModeBrowserInjection");
42 stop_proc_ = GetProcAddress(dll_, "StopUserModeBrowserInjection");
43 }
44
45 bool result = true;
46 if (!start_proc_ || !stop_proc_) {
47 _ASSERTE(L"failed to load hook dll.");
48 result = false;
49 } else {
50 if (FAILED(start_proc_())) {
51 _ASSERTE(L"failed to initialize hook dll.");
52 result = false;
53 }
54 }
55 return result;
56 }
57
58 void Unload() {
59 if (stop_proc_) {
60 stop_proc_();
61 }
62 if (dll_) {
63 FreeLibrary(dll_);
64 }
65 }
66
67 private:
68 HMODULE dll_;
69 PROC start_proc_;
70 PROC stop_proc_;
71};
72
73
74LRESULT CALLBACK ChromeFrameHelperWndProc(HWND hwnd,
75 UINT message,
76 WPARAM wparam,
77 LPARAM lparam) {
78 switch (message) {
79 case WM_DESTROY:
80 PostQuitMessage(0);
81 break;
82 default:
83 return ::DefWindowProc(hwnd, message, wparam, lparam);
84 }
85 return 0;
86}
87
88HWND RegisterAndCreateWindow(HINSTANCE hinstance) {
89 WNDCLASSEX wcex = {0};
90 wcex.cbSize = sizeof(WNDCLASSEX);
91 wcex.lpfnWndProc = ChromeFrameHelperWndProc;
92 wcex.hInstance = GetModuleHandle(NULL);
93 wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1);
94 wcex.lpszClassName = kChromeFrameHelperWindowClassName;
95 RegisterClassEx(&wcex);
96
97 HWND hwnd = CreateWindow(kChromeFrameHelperWindowClassName,
98 kChromeFrameHelperWindowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
99 CW_USEDEFAULT, 0, NULL, NULL, hinstance, NULL);
100
101 return hwnd;
102}
103
104int APIENTRY wWinMain(HINSTANCE hinstance, HINSTANCE, wchar_t*, int show_cmd) {
[email protected]e4fe427b2010-09-03 19:57:42105 const wchar_t* cmd_line = ::GetCommandLine();
106 google_breakpad::scoped_ptr<google_breakpad::ExceptionHandler> breakpad(
107 InitializeCrashReporting(cmd_line));
[email protected]9ceda772010-08-30 15:34:11108
109 // Create a window with a known class and title just to listen for WM_CLOSE
110 // messages that will shut us down.
111 HWND hwnd = RegisterAndCreateWindow(hinstance);
112 _ASSERTE(hwnd);
113
114 // Load the hook dll, and set the event hooks.
115 HookDllLoader loader;
116 bool loaded = loader.Load();
117 _ASSERTE(loaded);
118
119 if (loaded) {
120 MSG msg;
121 BOOL ret;
122 // Main message loop:
123 while ((ret = GetMessage(&msg, NULL, 0, 0))) {
124 if (ret == -1) {
125 break;
126 } else {
127 TranslateMessage(&msg);
128 DispatchMessage(&msg);
129 }
130 }
131 }
132
133 return 0;
134}