1 // Copyright (c) 2015 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.
5 #include "components/browser_watcher/endsession_watcher_window_win.h"
7 #include "base/logging.h"
8 #include "base/profiler/scoped_tracker.h"
9 #include "base/win/wrapped_window_proc.h"
11 namespace browser_watcher
{
15 const wchar_t kWindowClassName
[] = L
"Chrome_BrowserWatcherWindow";
19 EndSessionWatcherWindow::EndSessionWatcherWindow(
20 const EndSessionMessageCallback
& on_end_session_message
) :
21 on_end_session_message_(on_end_session_message
) {
22 WNDCLASSEX window_class
= {0};
23 base::win::InitializeWindowClass(
25 &base::win::WrappedWindowProc
<EndSessionWatcherWindow::WndProcThunk
>,
26 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
,
28 instance_
= window_class
.hInstance
;
29 ATOM clazz
= ::RegisterClassEx(&window_class
);
32 // TODO(siggi): will a message window do here?
33 window_
= ::CreateWindow(kWindowClassName
,
34 0, 0, 0, 0, 0, 0, 0, 0, instance_
, 0);
35 ::SetWindowLongPtr(window_
, GWLP_USERDATA
, reinterpret_cast<LONG_PTR
>(this));
36 DCHECK_EQ(::GetWindowLongPtr(window_
, GWLP_USERDATA
),
37 reinterpret_cast<LONG_PTR
>(this));
40 EndSessionWatcherWindow::~EndSessionWatcherWindow() {
42 ::DestroyWindow(window_
);
43 ::UnregisterClass(kWindowClassName
, instance_
);
47 LRESULT
EndSessionWatcherWindow::OnEndSessionMessage(UINT message
,
50 on_end_session_message_
.Run(message
, lparam
);
54 LRESULT CALLBACK
EndSessionWatcherWindow::WndProcThunk(HWND hwnd
,
58 EndSessionWatcherWindow
* msg_wnd
=
59 reinterpret_cast<EndSessionWatcherWindow
*>(
60 ::GetWindowLongPtr(hwnd
, GWLP_USERDATA
));
62 return msg_wnd
->WndProc(hwnd
, message
, wparam
, lparam
);
64 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
67 LRESULT
EndSessionWatcherWindow::WndProc(HWND hwnd
,
73 case WM_QUERYENDSESSION
:
74 return OnEndSessionMessage(message
, wparam
, lparam
);
79 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
82 } // namespace browser_watcher