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

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: Allow null SingletonHwnd Hwnd Created 5 years, 7 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
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 #include "ui/gfx/win/singleton_hwnd_observer.h"
9 10
10 namespace gfx { 11 namespace gfx {
11 12
12 // static 13 // static
13 SingletonHwnd* SingletonHwnd::GetInstance() { 14 SingletonHwnd* SingletonHwnd::GetInstance() {
14 return Singleton<SingletonHwnd>::get(); 15 return Singleton<SingletonHwnd>::get();
15 } 16 }
16 17
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, 18 BOOL SingletonHwnd::ProcessWindowMessage(HWND window,
28 UINT message, 19 UINT message,
29 WPARAM wparam, 20 WPARAM wparam,
30 LPARAM lparam, 21 LPARAM lparam,
31 LRESULT& result, 22 LRESULT& result,
32 DWORD msg_map_id) { 23 DWORD msg_map_id) {
33 FOR_EACH_OBSERVER(Observer, 24 FOR_EACH_OBSERVER(SingletonHwndObserver,
34 observer_list_, 25 observer_list_,
35 OnWndProc(window, message, wparam, lparam)); 26 OnWndProc(window, message, wparam, lparam));
36 return false; 27 return false;
37 } 28 }
38 29
39 SingletonHwnd::SingletonHwnd() { 30 SingletonHwnd::SingletonHwnd() {
40 if (!base::MessageLoopForUI::IsCurrent()) { 31 if (!base::MessageLoopForUI::IsCurrent()) {
41 // Creating this window in (e.g.) a renderer inhibits shutdown on 32 // Creating this window in (e.g.) a renderer inhibits shutdown on
42 // Windows. See http://crbug.com/230122 and http://crbug.com/236039. 33 // Windows. See http://crbug.com/230122 and http://crbug.com/236039.
43 DLOG(ERROR) << "Cannot create windows on non-UI thread!"; 34 DLOG(ERROR) << "Cannot create windows on non-UI thread!";
44 return; 35 return;
45 } 36 }
46 WindowImpl::Init(NULL, Rect()); 37 WindowImpl::Init(NULL, Rect());
47 } 38 }
48 39
49 SingletonHwnd::~SingletonHwnd() { 40 SingletonHwnd::~SingletonHwnd() {
41 // WindowImpl will clean up the hwnd value on WM_NCDESTROY.
42 if (hwnd())
43 DestroyWindow(hwnd());
44
45 // Tell all of our current observers to clean themselves up.
46 FOR_EACH_OBSERVER(SingletonHwndObserver, observer_list_, ClearWndProc());
47 }
48
49 void SingletonHwnd::AddObserver(SingletonHwndObserver* observer) {
50 observer_list_.AddObserver(observer);
51 }
52
53 void SingletonHwnd::RemoveObserver(SingletonHwndObserver* observer) {
54 observer_list_.RemoveObserver(observer);
50 } 55 }
51 56
52 } // namespace gfx 57 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698