Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / windows / WinEventObserver.h
blob6a267871dd6074fa9a715ed94d8a9d0556c350a5
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
10 #include <windows.h>
12 #include "mozilla/Maybe.h"
13 #include "nsISupportsImpl.h"
14 #include "nsTHashSet.h"
16 namespace mozilla {
18 namespace widget {
20 class WinEventObserver {
21 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventObserver)
22 public:
23 virtual void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam,
24 LPARAM aLParam) {}
25 virtual void Destroy();
27 protected:
28 virtual ~WinEventObserver();
30 bool mDestroyed = false;
33 // Uses singleton window to observe events like display status and session
34 // change.
35 class WinEventHub final {
36 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventHub)
38 public:
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
42 // repeatedly.
43 static bool Ensure();
44 static RefPtr<WinEventHub> Get() { return sInstance; }
46 void AddObserver(WinEventObserver* aObserver);
47 void RemoveObserver(WinEventObserver* aObserver);
49 HWND GetWnd() { return mHWnd; }
51 private:
52 WinEventHub();
53 ~WinEventHub();
55 static LRESULT CALLBACK WinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam,
56 LPARAM aLParam);
58 bool Initialize();
59 void ProcessWinEventProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
61 HWND mHWnd = nullptr;
62 nsTHashSet<nsRefPtrHashKey<WinEventObserver>> mObservers;
64 static StaticRefPtr<WinEventHub> sInstance;
67 class DisplayStatusListener {
68 public:
69 virtual void OnDisplayStateChanged(bool aDisplayOn) = 0;
72 // Observe Display on/off event
73 class DisplayStatusObserver final : public WinEventObserver {
74 public:
75 static already_AddRefed<DisplayStatusObserver> Create(
76 DisplayStatusListener* aListener);
78 private:
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 {
90 public:
91 virtual void OnSessionChange(WPARAM aStatusCode,
92 Maybe<bool> aIsCurrentSession) = 0;
95 // Observe session lock/unlock event
96 class SessionChangeObserver : public WinEventObserver {
97 public:
98 static already_AddRefed<SessionChangeObserver> Create(
99 SessionChangeListener* aListener);
101 private:
102 explicit SessionChangeObserver(SessionChangeListener* aListener);
103 virtual ~SessionChangeObserver();
105 void Initialize();
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