Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Unified Diff: ui/gfx/win/singleton_hwnd.cc

Issue 1092183005: Fix Up SingletonHwnd Observer Lifetime Issues (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« ui/gfx/win/singleton_hwnd.h ('K') | « ui/gfx/win/singleton_hwnd.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« ui/gfx/win/singleton_hwnd.h ('K') | « ui/gfx/win/singleton_hwnd.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698