From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- widget/windows/WinEventObserver.h | 115 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 widget/windows/WinEventObserver.h (limited to 'widget/windows/WinEventObserver.h') diff --git a/widget/windows/WinEventObserver.h b/widget/windows/WinEventObserver.h new file mode 100644 index 0000000000..6a267871dd --- /dev/null +++ b/widget/windows/WinEventObserver.h @@ -0,0 +1,115 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef widget_windows_WinEventObserver_h +#define widget_windows_WinEventObserver_h + +#include + +#include "mozilla/Maybe.h" +#include "nsISupportsImpl.h" +#include "nsTHashSet.h" + +namespace mozilla { + +namespace widget { + +class WinEventObserver { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventObserver) + public: + virtual void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam) {} + virtual void Destroy(); + + protected: + virtual ~WinEventObserver(); + + bool mDestroyed = false; +}; + +// Uses singleton window to observe events like display status and session +// change. +class WinEventHub final { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventHub) + + public: + // Returns true if the singleton exists (or was created). Will return + // false if the singleton couldn't be created, in which case a call + // to Get() will return a nullptr. It is safe to call this function + // repeatedly. + static bool Ensure(); + static RefPtr Get() { return sInstance; } + + void AddObserver(WinEventObserver* aObserver); + void RemoveObserver(WinEventObserver* aObserver); + + HWND GetWnd() { return mHWnd; } + + private: + WinEventHub(); + ~WinEventHub(); + + static LRESULT CALLBACK WinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam); + + bool Initialize(); + void ProcessWinEventProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); + + HWND mHWnd = nullptr; + nsTHashSet> mObservers; + + static StaticRefPtr sInstance; +}; + +class DisplayStatusListener { + public: + virtual void OnDisplayStateChanged(bool aDisplayOn) = 0; +}; + +// Observe Display on/off event +class DisplayStatusObserver final : public WinEventObserver { + public: + static already_AddRefed Create( + DisplayStatusListener* aListener); + + private: + explicit DisplayStatusObserver(DisplayStatusListener* aListener); + virtual ~DisplayStatusObserver(); + void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam) override; + + DisplayStatusListener* mListener; + + HPOWERNOTIFY mDisplayStatusHandle = nullptr; +}; + +class SessionChangeListener { + public: + virtual void OnSessionChange(WPARAM aStatusCode, + Maybe aIsCurrentSession) = 0; +}; + +// Observe session lock/unlock event +class SessionChangeObserver : public WinEventObserver { + public: + static already_AddRefed Create( + SessionChangeListener* aListener); + + private: + explicit SessionChangeObserver(SessionChangeListener* aListener); + virtual ~SessionChangeObserver(); + + void Initialize(); + void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam) override; + + SessionChangeListener* mListener; +}; + +} // namespace widget +} // namespace mozilla + +#endif // widget_windows_WinEventObserver_h -- cgit v1.2.3