| Index: ui/base/win/singleton_hwnd.cc
|
| ===================================================================
|
| --- ui/base/win/singleton_hwnd.cc (revision 0)
|
| +++ ui/base/win/singleton_hwnd.cc (revision 0)
|
| @@ -0,0 +1,84 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ui/base/win/singleton_hwnd.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| +#include "base/logging.h"
|
| +#include "base/win/wrapped_window_proc.h"
|
| +#include "ui/base/win/hwnd_util.h"
|
| +
|
| +namespace {
|
| +
|
| +// Windows class name to use for the listener window.
|
| +const wchar_t kWindowClassName[] = L"SingletonHwnd";
|
| +
|
| +// Windows callback for listening for WM_* messages.
|
| +LRESULT CALLBACK ListenerWindowProc(HWND hwnd,
|
| + UINT message,
|
| + WPARAM wparam,
|
| + LPARAM lparam) {
|
| + ui::SingletonHwnd::GetInstance()->OnWndProc(hwnd, message, wparam, lparam);
|
| + return ::DefWindowProc(hwnd, message, wparam, lparam);
|
| +}
|
| +
|
| +// Creates a listener window to receive WM_* messages.
|
| +HWND CreateListenerWindow() {
|
| + HINSTANCE hinst = 0;
|
| + if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
| + reinterpret_cast<char*>(&ListenerWindowProc),
|
| + &hinst)) {
|
| + NOTREACHED();
|
| + }
|
| +
|
| + WNDCLASSEX wc = {0};
|
| + wc.cbSize = sizeof(wc);
|
| + wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>;
|
| + wc.hInstance = hinst;
|
| + wc.lpszClassName = kWindowClassName;
|
| + ATOM clazz = ::RegisterClassEx(&wc);
|
| + DCHECK(clazz);
|
| +
|
| + return ::CreateWindow(MAKEINTATOM(clazz), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
|
| + hinst, 0);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace ui {
|
| +
|
| +// static
|
| +SingletonHwnd* SingletonHwnd::GetInstance() {
|
| + return Singleton<SingletonHwnd>::get();
|
| +}
|
| +
|
| +void SingletonHwnd::RegisterListener(WndProcCallback& callback) {
|
| + if (!listener_window_) {
|
| + listener_window_ = CreateListenerWindow();
|
| + ui::CheckWindowCreated(listener_window_);
|
| + }
|
| + listeners_.push_back(callback);
|
| +}
|
| +
|
| +void SingletonHwnd::OnWndProc(HWND hwnd,
|
| + UINT message,
|
| + WPARAM wparam,
|
| + LPARAM lparam) {
|
| + for (size_t i = 0; i < listeners_.size(); ++i)
|
| + listeners_[i].Run(hwnd, message, wparam, lparam);
|
| +}
|
| +
|
| +SingletonHwnd::SingletonHwnd()
|
| + : listener_window_(NULL) {
|
| +}
|
| +
|
| +SingletonHwnd::~SingletonHwnd() {
|
| + if (listener_window_) {
|
| + ::DestroyWindow(listener_window_);
|
| + ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL));
|
| + }
|
| +}
|
| +
|
| +} // namespace ui
|
|
|