Chromium Code Reviews| Index: ui/gfx/win/singleton_hwnd.h |
| diff --git a/ui/gfx/win/singleton_hwnd.h b/ui/gfx/win/singleton_hwnd.h |
| index 5dbfe90d2171b7964c90c5cc947759961ef4313a..1e10af118223b57c82c8e5d70128b2fc08c17cc0 100644 |
| --- a/ui/gfx/win/singleton_hwnd.h |
| +++ b/ui/gfx/win/singleton_hwnd.h |
| @@ -9,6 +9,7 @@ |
| #include <vector> |
| #include "base/basictypes.h" |
| +#include "base/callback.h" |
| #include "base/callback_forward.h" |
| #include "base/observer_list.h" |
| #include "ui/gfx/win/window_impl.h" |
| @@ -21,20 +22,31 @@ namespace gfx { |
| // notifications. |
| class GFX_EXPORT SingletonHwnd : public WindowImpl { |
| public: |
| - static SingletonHwnd* GetInstance(); |
| - |
| - // Observer interface for receiving Windows WM_* notifications. |
| - class Observer { |
| + // Singleton lifetime management is tricky. This observer handles the correct |
| + // cleanup if either the SingletonHwnd or forwarded object is destroyed first. |
| + // To use this class, make sure this object is bound to your object's lifetime |
| + // and use SetWndProc to set your WndProc. |
| + 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.
|
| public: |
| - virtual void OnWndProc(HWND hwnd, |
| - UINT message, |
| - WPARAM wparam, |
| - LPARAM lparam) = 0; |
| + typedef base::Callback<void(HWND, UINT, WPARAM, LPARAM)> WndProc; |
| + |
| + Observer(); |
| + ~Observer(); |
| + |
| + void SetWndProc(const WndProc& wndProc); |
|
sky
2015/04/24 20:58:23
wnd_proc
robliao
2015/04/24 21:49:03
Done.
|
| + void ClearWndProc(); |
| + |
| + private: |
| + friend class SingletonHwnd; |
| + |
| + void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| + |
| + WndProc wndProc_; |
|
sky
2015/04/24 20:58:23
wm_proc_.
robliao
2015/04/24 21:49:03
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(Observer); |
| }; |
| - // Add/remove observer to receive WM_* notifications. |
| - void AddObserver(Observer* observer); |
| - void RemoveObserver(Observer* observer); |
| + static SingletonHwnd* GetInstance(); |
| // Windows callback for WM_* notifications. |
| BOOL ProcessWindowMessage(HWND window, |
| @@ -50,6 +62,10 @@ class GFX_EXPORT SingletonHwnd : public WindowImpl { |
| SingletonHwnd(); |
| ~SingletonHwnd() override; |
| + // Add/remove Observer to forward WM_* notifications. |
| + void AddObserver(Observer* observer); |
| + void RemoveObserver(Observer* observer); |
| + |
| // List of registered observers. |
| ObserverList<Observer> observer_list_; |