Chromium Code Reviews| Index: sandbox/win/tests/integration_tests/hooking_win_proc.cc |
| diff --git a/sandbox/win/tests/integration_tests/hooking_win_proc.cc b/sandbox/win/tests/integration_tests/hooking_win_proc.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..92f8e5c318b81578c523f235ac4a3d3d2c9a450f |
| --- /dev/null |
| +++ b/sandbox/win/tests/integration_tests/hooking_win_proc.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
|
Will Harris
2016/04/04 00:15:09
no (c)
penny
2016/04/11 22:11:59
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <windows.h> |
| + |
| +namespace { |
| + |
| +const wchar_t* class_name = L"myWindowClass"; |
|
Will Harris
2016/04/04 00:15:09
if these are shared they should be in a .h file an
penny
2016/04/11 22:11:59
Done.
|
| +// WinProc event name |
| +const wchar_t* winproc_event = L"ChromeExtensionTestEvent"; |
| + |
| +} // namespace |
| + |
| +LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { |
| + // Injected keystroke via PostThreadMessage won't be dispatched to here. |
| + // Have to use SendMessage or SendInput to trigger the keyboard hook. |
| + switch (msg) { |
| + case WM_KEYDOWN: |
| + break; |
| + case WM_KEYUP: |
| + break; |
| + case WM_CLOSE: |
| + ::DestroyWindow(hwnd); |
| + break; |
| + case WM_DESTROY: |
| + ::PostQuitMessage(0); |
| + break; |
| + default: |
| + return ::DefWindowProc(hwnd, msg, wParam, lParam); |
| + } |
| + return 0; |
| +} |
| + |
| +int WINAPI WinMain(HINSTANCE hInstance, |
| + HINSTANCE hPrevInstance, |
| + LPSTR lpCmdLine, |
| + int nCmdShow) { |
| + WNDCLASSEX wc; |
| + HWND hwnd; |
| + MSG msg; |
| + |
| + // Our parent should have set up this event already. |
| + HANDLE event = ::OpenEventW(EVENT_MODIFY_STATE, FALSE, winproc_event); |
|
Will Harris
2016/04/04 00:15:09
scoped, unless a reason not to wait to link base?
penny
2016/04/11 22:11:59
I'd prefer not to pull base into the hook dll or t
|
| + if (event == NULL || event == INVALID_HANDLE_VALUE) |
| + return 1; |
| + |
| + // Step 1: Registering the Window Class. |
| + wc.cbSize = sizeof(WNDCLASSEX); |
| + wc.style = 0; |
| + wc.lpfnWndProc = WndProc; |
| + wc.cbClsExtra = 0; |
| + wc.cbWndExtra = 0; |
| + wc.hInstance = hInstance; |
| + wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); |
| + wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); |
| + wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); |
| + wc.lpszMenuName = NULL; |
| + wc.lpszClassName = class_name; |
| + wc.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION); |
| + |
| + if (!::RegisterClassEx(&wc)) |
| + return 1; |
| + |
| + // Step 2: Creating the Window. |
| + hwnd = |
| + ::CreateWindowExW(WS_EX_CLIENTEDGE, class_name, |
| + L"ChromeMitigationTests", WS_OVERLAPPEDWINDOW, |
|
Will Harris
2016/04/04 00:15:09
nit: formatting?
penny
2016/04/11 22:11:59
Done.
|
| + CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, |
| + hInstance, NULL); |
| + |
| + if (hwnd == NULL) |
| + return 1; |
| + |
| + ::ShowWindow(hwnd, nCmdShow); |
| + ::UpdateWindow(hwnd); |
| + |
| + // Step 3: Let 'em know we're up and running. |
| + ::SetEvent(event); |
| + |
| + // Step 4: The Message Loop |
| + // WM_QUIT results in a 0 value from GetMessageW, |
| + // breaking the loop. |
| + while (::GetMessageW(&msg, NULL, 0, 0) > 0) { |
| + ::TranslateMessage(&msg); |
| + ::DispatchMessageW(&msg); |
| + } |
| + |
| + ::SetEvent(event); |
| + ::CloseHandle(event); |
| + |
| + return msg.wParam; |
| +} |