Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #include "ui/base/win/singleton_hwnd.h" | |
| 6 | |
| 7 #include <vector> | |
|
tfarina
2012/02/08 20:34:55
nit: I'd remove this include since you already nee
asvitkine_google
2012/02/08 21:04:57
Done.
| |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/singleton.h" | |
|
tfarina
2012/02/08 20:34:55
also in the header file.
asvitkine_google
2012/02/08 21:04:57
Done.
| |
| 13 #include "base/win/wrapped_window_proc.h" | |
| 14 #include "ui/base/win/hwnd_util.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Windows class name to use for the listener window. | |
| 19 const wchar_t kWindowClassName[] = L"SingletonHwnd"; | |
| 20 | |
| 21 // Windows callback for listening for WM_* messages. | |
| 22 LRESULT CALLBACK ListenerWindowProc(HWND hwnd, | |
| 23 UINT message, | |
| 24 WPARAM wparam, | |
| 25 LPARAM lparam) { | |
| 26 gfx::SingletonHwnd::GetInstance()->OnWndProc(hwnd, message, wparam, lparam); | |
| 27 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
| 28 } | |
| 29 | |
| 30 // Creates a listener window to receive WM_* messages. | |
| 31 HWND CreateListenerWindow() { | |
| 32 HINSTANCE hinst = 0; | |
| 33 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
| 34 reinterpret_cast<char*>(&ListenerWindowProc), | |
| 35 &hinst)) { | |
| 36 NOTREACHED(); | |
| 37 } | |
| 38 | |
| 39 WNDCLASSEX wc = {0}; | |
| 40 wc.cbSize = sizeof(wc); | |
| 41 wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>; | |
| 42 wc.hInstance = hinst; | |
| 43 wc.lpszClassName = kWindowClassName; | |
| 44 ATOM clazz = ::RegisterClassEx(&wc); | |
| 45 DCHECK(clazz); | |
| 46 | |
| 47 return ::CreateWindow(MAKEINTATOM(clazz), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, | |
| 48 hinst, 0); | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 namespace gfx { | |
| 54 | |
| 55 // static | |
| 56 SingletonHwnd* SingletonHwnd::GetInstance() { | |
| 57 return Singleton<SingletonHwnd>::get(); | |
| 58 } | |
| 59 | |
| 60 void SingletonHwnd::RegisterListener(WndProcCallback& callback) { | |
| 61 if (!listener_window_) { | |
| 62 listener_window_ = CreateListenerWindow(); | |
| 63 ui::CheckWindowCreated(listener_window_); | |
| 64 } | |
| 65 listeners_.push_back(callback); | |
| 66 } | |
| 67 | |
| 68 void SingletonHwnd::OnWndProc(HWND hwnd, | |
| 69 UINT message, | |
| 70 WPARAM wparam, | |
| 71 LPARAM lparam) { | |
| 72 for (size_t i = 0; i < listeners_.size(); ++i) | |
| 73 listeners_[i].Run(hwnd, message, wparam, lparam); | |
| 74 } | |
| 75 | |
| 76 SingletonHwnd::SingletonHwnd() | |
| 77 : listener_window_(NULL) { | |
| 78 } | |
| 79 | |
| 80 SingletonHwnd::~SingletonHwnd() { | |
| 81 if (listener_window_) { | |
| 82 ::DestroyWindow(listener_window_); | |
| 83 ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL)); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 } // namespace gfx | |
| OLD | NEW |