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

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

Issue 1092183005: Fix Up SingletonHwnd Observer Lifetime Issues (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert to scoped_ptr 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
Index: ui/gfx/win/singleton_hwnd_observer.cc
diff --git a/ui/gfx/win/singleton_hwnd_observer.cc b/ui/gfx/win/singleton_hwnd_observer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3b3c3097c4d0b725be40fd5d4da6ebf15fc1026c
--- /dev/null
+++ b/ui/gfx/win/singleton_hwnd_observer.cc
@@ -0,0 +1,42 @@
+// Copyright (c) 2015 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/gfx/win/singleton_hwnd_observer.h"
+
+#include "ui/gfx/win/singleton_hwnd.h"
+
+namespace gfx {
+
+SingletonHwndObserver::SingletonHwndObserver(const WndProc& wnd_proc) :
+ wnd_proc_(wnd_proc) {
+ DCHECK(!wnd_proc.is_null());
+ SingletonHwnd::GetInstance()->AddObserver(this);
+}
+
+SingletonHwndObserver::~SingletonHwndObserver() {
+ ClearWndProc();
sky 2015/04/28 13:20:34 Why do you need the ClearWndProc? Won't evertyhing
robliao 2015/04/28 14:11:55 We need to unregister from the SingletonHwnd in tw
sky 2015/04/28 16:45:46 I see it. You're taking is_null() to know when you
+}
+
+void SingletonHwndObserver::ClearWndProc() {
+ if (!wnd_proc_.is_null())
+ SingletonHwnd::GetInstance()->RemoveObserver(this);
+
+ wnd_proc_.Reset();
+}
+
+void SingletonHwndObserver::OnWndProc(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam) {
+ if (wnd_proc_.is_null())
sky 2015/04/28 16:45:46 Shouldn't this be a DCHECK?
robliao 2015/04/28 22:14:10 Actually, even a DCHECK isn't necessary, as it wou
+ return;
+
+ wnd_proc_.Run(hwnd, message, wparam, lparam);
+
+ if (message == WM_NCDESTROY) {
sky 2015/04/28 13:20:34 nit: no {}
robliao 2015/04/28 14:11:55 Done.
+ ClearWndProc();
+ }
+}
+
+} // namespace gfx
« ui/gfx/win/singleton_hwnd_observer.h ('K') | « ui/gfx/win/singleton_hwnd_observer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698