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

Side by Side Diff: ui/gfx/win/singleton_hwnd.h

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
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 #ifndef UI_GFX_WIN_SINGLETON_HWND_H_ 5 #ifndef UI_GFX_WIN_SINGLETON_HWND_H_
6 #define UI_GFX_WIN_SINGLETON_HWND_H_ 6 #define UI_GFX_WIN_SINGLETON_HWND_H_
7 7
8 #include <windows.h> 8 #include <windows.h>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/callback.h"
12 #include "base/callback_forward.h" 13 #include "base/callback_forward.h"
13 #include "base/observer_list.h" 14 #include "base/observer_list.h"
14 #include "ui/gfx/win/window_impl.h" 15 #include "ui/gfx/win/window_impl.h"
15 16
16 template<typename T> struct DefaultSingletonTraits; 17 template<typename T> struct DefaultSingletonTraits;
17 18
18 namespace gfx { 19 namespace gfx {
19 20
20 // Singleton message-only HWND that allows interested clients to receive WM_* 21 // Singleton message-only HWND that allows interested clients to receive WM_*
21 // notifications. 22 // notifications.
22 class GFX_EXPORT SingletonHwnd : public WindowImpl { 23 class GFX_EXPORT SingletonHwnd : public WindowImpl {
23 public: 24 public:
24 static SingletonHwnd* GetInstance(); 25 // Singleton lifetime management is tricky. This observer handles the correct
26 // cleanup if either the SingletonHwnd or forwarded object is destroyed first.
27 // To use this class, make sure this object is bound to your object's lifetime
28 // and use SetWndProc to set your WndProc.
29 class GFX_EXPORT Observer {
sky 2015/04/24 20:58:23 Put this into its own header. (From the style guid
robliao 2015/04/24 21:49:03 Done.
30 public:
31 typedef base::Callback<void(HWND, UINT, WPARAM, LPARAM)> WndProc;
25 32
26 // Observer interface for receiving Windows WM_* notifications. 33 Observer();
27 class Observer { 34 ~Observer();
28 public: 35
29 virtual void OnWndProc(HWND hwnd, 36 void SetWndProc(const WndProc& wndProc);
sky 2015/04/24 20:58:23 wnd_proc
robliao 2015/04/24 21:49:03 Done.
30 UINT message, 37 void ClearWndProc();
31 WPARAM wparam, 38
32 LPARAM lparam) = 0; 39 private:
40 friend class SingletonHwnd;
41
42 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
43
44 WndProc wndProc_;
sky 2015/04/24 20:58:23 wm_proc_.
robliao 2015/04/24 21:49:03 Done.
45
46 DISALLOW_COPY_AND_ASSIGN(Observer);
33 }; 47 };
34 48
35 // Add/remove observer to receive WM_* notifications. 49 static SingletonHwnd* GetInstance();
36 void AddObserver(Observer* observer);
37 void RemoveObserver(Observer* observer);
38 50
39 // Windows callback for WM_* notifications. 51 // Windows callback for WM_* notifications.
40 BOOL ProcessWindowMessage(HWND window, 52 BOOL ProcessWindowMessage(HWND window,
41 UINT message, 53 UINT message,
42 WPARAM wparam, 54 WPARAM wparam,
43 LPARAM lparam, 55 LPARAM lparam,
44 LRESULT& result, 56 LRESULT& result,
45 DWORD msg_map_id) override; 57 DWORD msg_map_id) override;
46 58
47 private: 59 private:
48 friend struct DefaultSingletonTraits<SingletonHwnd>; 60 friend struct DefaultSingletonTraits<SingletonHwnd>;
49 61
50 SingletonHwnd(); 62 SingletonHwnd();
51 ~SingletonHwnd() override; 63 ~SingletonHwnd() override;
52 64
65 // Add/remove Observer to forward WM_* notifications.
66 void AddObserver(Observer* observer);
67 void RemoveObserver(Observer* observer);
68
53 // List of registered observers. 69 // List of registered observers.
54 ObserverList<Observer> observer_list_; 70 ObserverList<Observer> observer_list_;
55 71
56 DISALLOW_COPY_AND_ASSIGN(SingletonHwnd); 72 DISALLOW_COPY_AND_ASSIGN(SingletonHwnd);
57 }; 73 };
58 74
59 } // namespace gfx 75 } // namespace gfx
60 76
61 #endif // UI_GFX_WIN_SINGLETON_HWND_H_ 77 #endif // UI_GFX_WIN_SINGLETON_HWND_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698