Source code

Revision control

Copy as Markdown

Other Tools

/* -*- 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 */
#ifndef widget_windows_WinEventObserver_h
#define widget_windows_WinEventObserver_h
#include <windows.h>
#include "mozilla/Maybe.h"
#include "nsISupportsImpl.h"
#include "nsTHashSet.h"
namespace mozilla {
namespace widget {
class WinEventObserver {
virtual void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam,
LPARAM aLParam) {}
virtual void Destroy();
virtual ~WinEventObserver();
bool mDestroyed = false;
// Uses singleton window to observe events like display status and session
// change.
class WinEventHub final {
// 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<WinEventHub> Get() { return sInstance; }
void AddObserver(WinEventObserver* aObserver);
void RemoveObserver(WinEventObserver* aObserver);
HWND GetWnd() { return mHWnd; }
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<nsRefPtrHashKey<WinEventObserver>> mObservers;
static StaticRefPtr<WinEventHub> sInstance;
class DisplayStatusListener {
virtual void OnDisplayStateChanged(bool aDisplayOn) = 0;
// Observe Display on/off event
class DisplayStatusObserver final : public WinEventObserver {
static already_AddRefed<DisplayStatusObserver> Create(
DisplayStatusListener* aListener);
explicit DisplayStatusObserver(DisplayStatusListener* aListener);
virtual ~DisplayStatusObserver();
void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam,
LPARAM aLParam) override;
DisplayStatusListener* mListener;
HPOWERNOTIFY mDisplayStatusHandle = nullptr;
class SessionChangeListener {
virtual void OnSessionChange(WPARAM aStatusCode,
Maybe<bool> aIsCurrentSession) = 0;
// Observe session lock/unlock event
class SessionChangeObserver : public WinEventObserver {
static already_AddRefed<SessionChangeObserver> Create(
SessionChangeListener* aListener);
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