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 "base/memory/singleton.h" |
| 8 #include "base/process_util.h" |
| 9 #include "base/win/wrapped_window_proc.h" |
| 10 #include "ui/base/win/hwnd_util.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Windows class name to use for the listener window. |
| 15 const wchar_t kWindowClassName[] = L"Chrome_SingletonHwnd"; |
| 16 |
| 17 // Windows callback for listening for WM_* messages. |
| 18 LRESULT CALLBACK ListenerWindowProc(HWND hwnd, |
| 19 UINT message, |
| 20 WPARAM wparam, |
| 21 LPARAM lparam) { |
| 22 ui::SingletonHwnd::GetInstance()->OnWndProc(hwnd, message, wparam, lparam); |
| 23 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 24 } |
| 25 |
| 26 // Creates a listener window to receive WM_* messages. |
| 27 HWND CreateListenerWindow() { |
| 28 WNDCLASSEX wc = {0}; |
| 29 wc.cbSize = sizeof(wc); |
| 30 wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>; |
| 31 wc.hInstance = base::GetModuleFromAddress(&ListenerWindowProc); |
| 32 wc.lpszClassName = kWindowClassName; |
| 33 ATOM window_class = ::RegisterClassEx(&wc); |
| 34 DCHECK(window_class); |
| 35 |
| 36 return ::CreateWindow(MAKEINTATOM(window_class), 0, 0, 0, 0, 0, 0, 0, 0, |
| 37 wc.hInstance, 0); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 namespace ui { |
| 43 |
| 44 // static |
| 45 SingletonHwnd* SingletonHwnd::GetInstance() { |
| 46 return Singleton<SingletonHwnd>::get(); |
| 47 } |
| 48 |
| 49 void SingletonHwnd::AddObserver(Observer* observer) { |
| 50 if (!listener_window_) { |
| 51 listener_window_ = CreateListenerWindow(); |
| 52 ui::CheckWindowCreated(listener_window_); |
| 53 } |
| 54 observer_list_.AddObserver(observer); |
| 55 } |
| 56 |
| 57 void SingletonHwnd::RemoveObserver(Observer* observer) { |
| 58 observer_list_.RemoveObserver(observer); |
| 59 } |
| 60 |
| 61 void SingletonHwnd::OnWndProc(HWND hwnd, |
| 62 UINT message, |
| 63 WPARAM wparam, |
| 64 LPARAM lparam) { |
| 65 FOR_EACH_OBSERVER(Observer, |
| 66 observer_list_, |
| 67 OnWndProc(hwnd, message, wparam, lparam)); |
| 68 } |
| 69 |
| 70 SingletonHwnd::SingletonHwnd() |
| 71 : listener_window_(NULL) { |
| 72 } |
| 73 |
| 74 SingletonHwnd::~SingletonHwnd() { |
| 75 if (listener_window_) { |
| 76 ::DestroyWindow(listener_window_); |
| 77 ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL)); |
| 78 } |
| 79 } |
| 80 |
| 81 } // namespace ui |
OLD | NEW |