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 #include "mozilla/dom/ContentParent.h"
8 #include "mozilla/Hal.h"
9 #include "mozilla/HalWakeLock.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/ModuleUtils.h"
12 #include "mozilla/Preferences.h"
13 #include "nsIDOMWakeLockListener.h"
14 #include "PowerManagerService.h"
24 namespace mozilla::dom::power
{
28 NS_IMPL_ISUPPORTS(PowerManagerService
, nsIPowerManagerService
)
31 StaticRefPtr
<PowerManagerService
> PowerManagerService::sSingleton
;
34 already_AddRefed
<PowerManagerService
> PowerManagerService::GetInstance() {
36 sSingleton
= new PowerManagerService();
38 ClearOnShutdown(&sSingleton
);
41 RefPtr
<PowerManagerService
> service
= sSingleton
.get();
42 return service
.forget();
45 void PowerManagerService::Init() { RegisterWakeLockObserver(this); }
47 PowerManagerService::~PowerManagerService() {
48 UnregisterWakeLockObserver(this);
51 void PowerManagerService::ComputeWakeLockState(
52 const WakeLockInformation
& aWakeLockInfo
, nsAString
& aState
) {
53 WakeLockState state
= hal::ComputeWakeLockState(aWakeLockInfo
.numLocks(),
54 aWakeLockInfo
.numHidden());
56 case WAKE_LOCK_STATE_UNLOCKED
:
57 aState
.AssignLiteral("unlocked");
59 case WAKE_LOCK_STATE_HIDDEN
:
60 aState
.AssignLiteral("locked-background");
62 case WAKE_LOCK_STATE_VISIBLE
:
63 aState
.AssignLiteral("locked-foreground");
68 void PowerManagerService::Notify(const WakeLockInformation
& aWakeLockInfo
) {
70 ComputeWakeLockState(aWakeLockInfo
, state
);
73 * Copy the listeners list before we walk through the callbacks
74 * because the callbacks may install new listeners. We expect no
75 * more than one listener per window, so it shouldn't be too long.
77 const CopyableAutoTArray
<nsCOMPtr
<nsIDOMMozWakeLockListener
>, 2> listeners
=
80 for (uint32_t i
= 0; i
< listeners
.Length(); ++i
) {
81 listeners
[i
]->Callback(aWakeLockInfo
.topic(), state
);
86 PowerManagerService::AddWakeLockListener(nsIDOMMozWakeLockListener
* aListener
) {
87 if (mWakeLockListeners
.Contains(aListener
)) return NS_OK
;
89 mWakeLockListeners
.AppendElement(aListener
);
94 PowerManagerService::RemoveWakeLockListener(
95 nsIDOMMozWakeLockListener
* aListener
) {
96 mWakeLockListeners
.RemoveElement(aListener
);
101 PowerManagerService::GetWakeLockState(const nsAString
& aTopic
,
103 WakeLockInformation info
;
104 GetWakeLockInfo(aTopic
, &info
);
106 ComputeWakeLockState(info
, aState
);
111 already_AddRefed
<WakeLock
> PowerManagerService::NewWakeLock(
112 const nsAString
& aTopic
, nsPIDOMWindowInner
* aWindow
,
113 mozilla::ErrorResult
& aRv
) {
114 RefPtr
<WakeLock
> wakelock
= new WakeLock();
115 aRv
= wakelock
->Init(aTopic
, aWindow
);
120 return wakelock
.forget();
124 PowerManagerService::NewWakeLock(const nsAString
& aTopic
,
125 mozIDOMWindow
* aWindow
,
126 nsIWakeLock
** aWakeLock
) {
127 mozilla::ErrorResult rv
;
128 RefPtr
<WakeLock
> wakelock
=
129 NewWakeLock(aTopic
, nsPIDOMWindowInner::From(aWindow
), rv
);
131 return rv
.StealNSResult();
134 wakelock
.forget(aWakeLock
);
138 } // namespace mozilla::dom::power