Index: ui/gfx/win/singleton_hwnd.cc |
diff --git a/ui/gfx/win/singleton_hwnd.cc b/ui/gfx/win/singleton_hwnd.cc |
index fc4d11f2d5e0d8146d9a3fa5c0f666e2e3c86676..2db1668fe7831a32aa948fa7ae4e3dbf483b8dfd 100644 |
--- a/ui/gfx/win/singleton_hwnd.cc |
+++ b/ui/gfx/win/singleton_hwnd.cc |
@@ -9,19 +9,44 @@ |
namespace gfx { |
-// static |
-SingletonHwnd* SingletonHwnd::GetInstance() { |
- return Singleton<SingletonHwnd>::get(); |
+// SingletonHwnd::Observer |
+SingletonHwnd::Observer::Observer() {} |
+ |
+SingletonHwnd::Observer::~Observer() { |
+ ClearWndProc(); |
} |
-void SingletonHwnd::AddObserver(Observer* observer) { |
- observer_list_.AddObserver(observer); |
+void SingletonHwnd::Observer::SetWndProc(const WndProc& wndProc) { |
+ if (wndProc_.is_null()) |
sky
2015/04/24 20:58:23
Is there a reason you can't add observer in the co
robliao
2015/04/24 21:49:03
A critical part of SingletonHwndObserver is that i
sky
2015/04/27 15:11:28
I would prefer a scoped_ptr. It's make the observe
robliao
2015/04/27 22:45:46
Done.
|
+ SingletonHwnd::GetInstance()->AddObserver(this); |
+ |
+ wndProc_ = wndProc; |
+ |
+ if (wndProc_.is_null()) |
+ SingletonHwnd::GetInstance()->RemoveObserver(this); |
} |
-void SingletonHwnd::RemoveObserver(Observer* observer) { |
- if (!hwnd()) |
+void SingletonHwnd::Observer::ClearWndProc() { |
+ SetWndProc(WndProc()); |
+} |
+ |
+void SingletonHwnd::Observer::OnWndProc(HWND hwnd, |
+ UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam) { |
+ if (wndProc_.is_null()) |
return; |
- observer_list_.RemoveObserver(observer); |
+ |
+ wndProc_.Run(hwnd, message, wparam, lparam); |
+ |
+ if (message == WM_NCDESTROY) { |
+ ClearWndProc(); |
+ } |
+} |
+ |
+// static |
+SingletonHwnd* SingletonHwnd::GetInstance() { |
+ return Singleton<SingletonHwnd>::get(); |
} |
BOOL SingletonHwnd::ProcessWindowMessage(HWND window, |
@@ -47,6 +72,18 @@ SingletonHwnd::SingletonHwnd() { |
} |
SingletonHwnd::~SingletonHwnd() { |
+ // WindowImpl will clean up the hwnd value on WM_NCDESTROY. |
+ DestroyWindow(hwnd()); |
+ DCHECK(!observer_list_.might_have_observers()); |
+} |
+ |
+void SingletonHwnd::AddObserver(Observer* observer) { |
+ DCHECK(hwnd()); |
+ observer_list_.AddObserver(observer); |
+} |
+ |
+void SingletonHwnd::RemoveObserver(Observer* observer) { |
+ observer_list_.RemoveObserver(observer); |
} |
} // namespace gfx |