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

Side by Side 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 unified diff | Download patch
« ui/gfx/win/singleton_hwnd.h ('K') | « ui/gfx/win/singleton_hwnd.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/win/singleton_hwnd.h" 5 #include "ui/gfx/win/singleton_hwnd.h"
6 6
7 #include "base/memory/singleton.h" 7 #include "base/memory/singleton.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 9
10 namespace gfx { 10 namespace gfx {
11 11
12 // SingletonHwnd::Observer
13 SingletonHwnd::Observer::Observer() {}
14
15 SingletonHwnd::Observer::~Observer() {
16 ClearWndProc();
17 }
18
19 void SingletonHwnd::Observer::SetWndProc(const WndProc& wndProc) {
20 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.
21 SingletonHwnd::GetInstance()->AddObserver(this);
22
23 wndProc_ = wndProc;
24
25 if (wndProc_.is_null())
26 SingletonHwnd::GetInstance()->RemoveObserver(this);
27 }
28
29 void SingletonHwnd::Observer::ClearWndProc() {
30 SetWndProc(WndProc());
31 }
32
33 void SingletonHwnd::Observer::OnWndProc(HWND hwnd,
34 UINT message,
35 WPARAM wparam,
36 LPARAM lparam) {
37 if (wndProc_.is_null())
38 return;
39
40 wndProc_.Run(hwnd, message, wparam, lparam);
41
42 if (message == WM_NCDESTROY) {
43 ClearWndProc();
44 }
45 }
46
12 // static 47 // static
13 SingletonHwnd* SingletonHwnd::GetInstance() { 48 SingletonHwnd* SingletonHwnd::GetInstance() {
14 return Singleton<SingletonHwnd>::get(); 49 return Singleton<SingletonHwnd>::get();
15 } 50 }
16 51
17 void SingletonHwnd::AddObserver(Observer* observer) {
18 observer_list_.AddObserver(observer);
19 }
20
21 void SingletonHwnd::RemoveObserver(Observer* observer) {
22 if (!hwnd())
23 return;
24 observer_list_.RemoveObserver(observer);
25 }
26
27 BOOL SingletonHwnd::ProcessWindowMessage(HWND window, 52 BOOL SingletonHwnd::ProcessWindowMessage(HWND window,
28 UINT message, 53 UINT message,
29 WPARAM wparam, 54 WPARAM wparam,
30 LPARAM lparam, 55 LPARAM lparam,
31 LRESULT& result, 56 LRESULT& result,
32 DWORD msg_map_id) { 57 DWORD msg_map_id) {
33 FOR_EACH_OBSERVER(Observer, 58 FOR_EACH_OBSERVER(Observer,
34 observer_list_, 59 observer_list_,
35 OnWndProc(window, message, wparam, lparam)); 60 OnWndProc(window, message, wparam, lparam));
36 return false; 61 return false;
37 } 62 }
38 63
39 SingletonHwnd::SingletonHwnd() { 64 SingletonHwnd::SingletonHwnd() {
40 if (!base::MessageLoopForUI::IsCurrent()) { 65 if (!base::MessageLoopForUI::IsCurrent()) {
41 // Creating this window in (e.g.) a renderer inhibits shutdown on 66 // Creating this window in (e.g.) a renderer inhibits shutdown on
42 // Windows. See http://crbug.com/230122 and http://crbug.com/236039. 67 // Windows. See http://crbug.com/230122 and http://crbug.com/236039.
43 DLOG(ERROR) << "Cannot create windows on non-UI thread!"; 68 DLOG(ERROR) << "Cannot create windows on non-UI thread!";
44 return; 69 return;
45 } 70 }
46 WindowImpl::Init(NULL, Rect()); 71 WindowImpl::Init(NULL, Rect());
47 } 72 }
48 73
49 SingletonHwnd::~SingletonHwnd() { 74 SingletonHwnd::~SingletonHwnd() {
75 // WindowImpl will clean up the hwnd value on WM_NCDESTROY.
76 DestroyWindow(hwnd());
77 DCHECK(!observer_list_.might_have_observers());
78 }
79
80 void SingletonHwnd::AddObserver(Observer* observer) {
81 DCHECK(hwnd());
82 observer_list_.AddObserver(observer);
83 }
84
85 void SingletonHwnd::RemoveObserver(Observer* observer) {
86 observer_list_.RemoveObserver(observer);
50 } 87 }
51 88
52 } // namespace gfx 89 } // namespace gfx
OLDNEW
« 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