James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | // Provides a way to handle exceptions that happen while a WindowProc is |
| 6 | // running. The behavior of exceptions generated inside a WindowProc is OS |
| 7 | // dependent, but it is possible that the OS just ignores the exception and |
| 8 | // continues execution, which leads to unpredictable behavior for Chrome. |
| 9 | |
| 10 | #ifndef BASE_WIN_WRAPPED_WINDOW_PROC_H_ |
| 11 | #define BASE_WIN_WRAPPED_WINDOW_PROC_H_ |
| 12 | |
| 13 | #include <windows.h> |
| 14 | |
| 15 | #include "base/base_export.h" |
| 16 | #include "base/strings/string16.h" |
| 17 | |
| 18 | namespace base { |
| 19 | namespace win { |
| 20 | |
| 21 | // An exception filter for a WindowProc. The return value determines how the |
| 22 | // exception should be handled, following standard SEH rules. However, the |
| 23 | // expected behavior for this function is to not return, instead of returning |
| 24 | // EXCEPTION_EXECUTE_HANDLER or similar, given that in general we are not |
| 25 | // prepared to handle exceptions. |
| 26 | typedef int (__cdecl *WinProcExceptionFilter)(EXCEPTION_POINTERS* info); |
| 27 | |
| 28 | // Sets the filter to deal with exceptions inside a WindowProc. Returns the old |
| 29 | // exception filter, if any. |
| 30 | // This function should be called before any window is created. |
| 31 | BASE_EXPORT WinProcExceptionFilter SetWinProcExceptionFilter( |
| 32 | WinProcExceptionFilter filter); |
| 33 | |
| 34 | // Calls the registered exception filter. |
| 35 | BASE_EXPORT int CallExceptionFilter(EXCEPTION_POINTERS* info); |
| 36 | |
| 37 | // Initializes the WNDCLASSEX structure |*class_out| to be passed to |
| 38 | // RegisterClassEx() making sure that it is associated with the module |
| 39 | // containing the window procedure. |
| 40 | BASE_EXPORT void InitializeWindowClass( |
| 41 | const char16* class_name, |
| 42 | WNDPROC window_proc, |
| 43 | UINT style, |
| 44 | int class_extra, |
| 45 | int window_extra, |
| 46 | HCURSOR cursor, |
| 47 | HBRUSH background, |
| 48 | const char16* menu_name, |
| 49 | HICON large_icon, |
| 50 | HICON small_icon, |
| 51 | WNDCLASSEX* class_out); |
| 52 | |
| 53 | // Wrapper that supplies a standard exception frame for the provided WindowProc. |
| 54 | // The normal usage is something like this: |
| 55 | // |
| 56 | // LRESULT CALLBACK MyWinProc(HWND hwnd, UINT message, |
| 57 | // WPARAM wparam, LPARAM lparam) { |
| 58 | // // Do Something. |
| 59 | // } |
| 60 | // |
| 61 | // ... |
| 62 | // |
| 63 | // WNDCLASSEX wc = {0}; |
| 64 | // wc.lpfnWndProc = WrappedWindowProc<MyWinProc>; |
| 65 | // wc.lpszClassName = class_name; |
| 66 | // ... |
| 67 | // RegisterClassEx(&wc); |
| 68 | // |
| 69 | // CreateWindowW(class_name, window_name, ... |
| 70 | // |
| 71 | template <WNDPROC proc> |
| 72 | LRESULT CALLBACK WrappedWindowProc(HWND hwnd, UINT message, |
| 73 | WPARAM wparam, LPARAM lparam) { |
| 74 | LRESULT rv = 0; |
| 75 | __try { |
| 76 | rv = proc(hwnd, message, wparam, lparam); |
| 77 | } __except(CallExceptionFilter(GetExceptionInformation())) { |
| 78 | } |
| 79 | return rv; |
| 80 | } |
| 81 | |
| 82 | } // namespace win |
| 83 | } // namespace base |
| 84 | |
| 85 | #endif // BASE_WIN_WRAPPED_WINDOW_PROC_H_ |