1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef widget_windows_WinEventObserver_h
8 #define widget_windows_WinEventObserver_h
12 #include "mozilla/Maybe.h"
13 #include "nsISupportsImpl.h"
14 #include "nsTHashSet.h"
20 class WinEventObserver
{
21 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventObserver
)
23 virtual void OnWinEventProc(HWND aHwnd
, UINT aMsg
, WPARAM aWParam
,
25 virtual void Destroy();
28 virtual ~WinEventObserver();
30 bool mDestroyed
= false;
33 // Uses singleton window to observe events like display status and session
35 class WinEventHub final
{
36 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventHub
)
39 // Returns true if the singleton exists (or was created). Will return
40 // false if the singleton couldn't be created, in which case a call
41 // to Get() will return a nullptr. It is safe to call this function
44 static RefPtr
<WinEventHub
> Get() { return sInstance
; }
46 void AddObserver(WinEventObserver
* aObserver
);
47 void RemoveObserver(WinEventObserver
* aObserver
);
49 HWND
GetWnd() { return mHWnd
; }
55 static LRESULT CALLBACK
WinEventProc(HWND aHwnd
, UINT aMsg
, WPARAM aWParam
,
59 void ProcessWinEventProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
62 nsTHashSet
<nsRefPtrHashKey
<WinEventObserver
>> mObservers
;
64 static StaticRefPtr
<WinEventHub
> sInstance
;
67 class DisplayStatusListener
{
69 virtual void OnDisplayStateChanged(bool aDisplayOn
) = 0;
72 // Observe Display on/off event
73 class DisplayStatusObserver final
: public WinEventObserver
{
75 static already_AddRefed
<DisplayStatusObserver
> Create(
76 DisplayStatusListener
* aListener
);
79 explicit DisplayStatusObserver(DisplayStatusListener
* aListener
);
80 virtual ~DisplayStatusObserver();
81 void OnWinEventProc(HWND aHwnd
, UINT aMsg
, WPARAM aWParam
,
82 LPARAM aLParam
) override
;
84 DisplayStatusListener
* mListener
;
86 HPOWERNOTIFY mDisplayStatusHandle
= nullptr;
89 class SessionChangeListener
{
91 virtual void OnSessionChange(WPARAM aStatusCode
,
92 Maybe
<bool> aIsCurrentSession
) = 0;
95 // Observe session lock/unlock event
96 class SessionChangeObserver
: public WinEventObserver
{
98 static already_AddRefed
<SessionChangeObserver
> Create(
99 SessionChangeListener
* aListener
);
102 explicit SessionChangeObserver(SessionChangeListener
* aListener
);
103 virtual ~SessionChangeObserver();
106 void OnWinEventProc(HWND aHwnd
, UINT aMsg
, WPARAM aWParam
,
107 LPARAM aLParam
) override
;
109 SessionChangeListener
* mListener
;
112 } // namespace widget
113 } // namespace mozilla
115 #endif // widget_windows_WinEventObserver_h