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 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
59 tracked_objects::ScopedTracker
tracking_profile(
60 FROM_HERE_WITH_EXPLICIT_FUNCTION(
61 "440919 EndSessionWatcherWindow::WndProcThunk"));
63 EndSessionWatcherWindow
* msg_wnd
=
64 reinterpret_cast<EndSessionWatcherWindow
*>(
65 ::GetWindowLongPtr(hwnd
, GWLP_USERDATA
));
67 return msg_wnd
->WndProc(hwnd
, message
, wparam
, lparam
);
69 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
72 LRESULT
EndSessionWatcherWindow::WndProc(HWND hwnd
,
78 case WM_QUERYENDSESSION
:
79 return OnEndSessionMessage(message
, wparam
, lparam
);
84 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
87 } // namespace browser_watcher