summaryrefslogtreecommitdiffstats
path: root/widget/windows/WinEventObserver.h
blob: 6a267871dd6074fa9a715ed94d8a9d0556c350a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 <windows.h>

#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<WinEventHub> 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<nsRefPtrHashKey<WinEventObserver>> mObservers;

  static StaticRefPtr<WinEventHub> sInstance;
};

class DisplayStatusListener {
 public:
  virtual void OnDisplayStateChanged(bool aDisplayOn) = 0;
};

// Observe Display on/off event
class DisplayStatusObserver final : public WinEventObserver {
 public:
  static already_AddRefed<DisplayStatusObserver> 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<bool> aIsCurrentSession) = 0;
};

// Observe session lock/unlock event
class SessionChangeObserver : public WinEventObserver {
 public:
  static already_AddRefed<SessionChangeObserver> 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