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