1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/PowerManager.h"
8 #include "mozilla/Hal.h"
10 #include "nsDOMClassInfoID.h"
11 #include "nsIDOMWakeLockListener.h"
12 #include "nsIDocument.h"
13 #include "nsIPermissionManager.h"
14 #include "nsIPowerManagerService.h"
15 #include "nsIPrincipal.h"
16 #include "nsPIDOMWindow.h"
17 #include "nsServiceManagerUtils.h"
19 #include "mozilla/dom/MozPowerManagerBinding.h"
20 #include "mozilla/Services.h"
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager
)
26 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
27 NS_INTERFACE_MAP_ENTRY(nsISupports
)
28 NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener
)
31 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PowerManager
, mListeners
, mWindow
)
33 NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager
)
34 NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager
)
36 /* virtual */ JSObject
*
37 PowerManager::WrapObject(JSContext
* aCx
)
39 return MozPowerManagerBinding::Wrap(aCx
, this);
43 PowerManager::Init(nsIDOMWindow
*aWindow
)
47 nsCOMPtr
<nsIPowerManagerService
> pmService
=
48 do_GetService(POWERMANAGERSERVICE_CONTRACTID
);
49 NS_ENSURE_STATE(pmService
);
51 // Add ourself to the global notification list.
52 pmService
->AddWakeLockListener(this);
57 PowerManager::Shutdown()
59 nsCOMPtr
<nsIPowerManagerService
> pmService
=
60 do_GetService(POWERMANAGERSERVICE_CONTRACTID
);
61 NS_ENSURE_STATE(pmService
);
63 // Remove ourself from the global notification list.
64 pmService
->RemoveWakeLockListener(this);
69 PowerManager::Reboot(ErrorResult
& aRv
)
71 nsCOMPtr
<nsIPowerManagerService
> pmService
=
72 do_GetService(POWERMANAGERSERVICE_CONTRACTID
);
76 aRv
.Throw(NS_ERROR_UNEXPECTED
);
81 PowerManager::FactoryReset(mozilla::dom::FactoryResetReason
& aReason
)
83 hal::FactoryReset(aReason
);
87 PowerManager::PowerOff(ErrorResult
& aRv
)
89 nsCOMPtr
<nsIPowerManagerService
> pmService
=
90 do_GetService(POWERMANAGERSERVICE_CONTRACTID
);
92 pmService
->PowerOff();
94 aRv
.Throw(NS_ERROR_UNEXPECTED
);
99 PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener
*aListener
)
101 if (!mListeners
.Contains(aListener
)) {
102 mListeners
.AppendElement(aListener
);
107 PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener
*aListener
)
109 mListeners
.RemoveElement(aListener
);
113 PowerManager::GetWakeLockState(const nsAString
& aTopic
,
117 nsCOMPtr
<nsIPowerManagerService
> pmService
=
118 do_GetService(POWERMANAGERSERVICE_CONTRACTID
);
120 aRv
= pmService
->GetWakeLockState(aTopic
, aState
);
122 aRv
.Throw(NS_ERROR_UNEXPECTED
);
127 PowerManager::Callback(const nsAString
&aTopic
, const nsAString
&aState
)
130 * We maintain a local listener list instead of using the global
131 * list so that when the window is destroyed we don't have to
133 * Copy the listeners list before we walk through the callbacks
134 * because the callbacks may install new listeners. We expect no
135 * more than one listener per window, so it shouldn't be too long.
137 nsAutoTArray
<nsCOMPtr
<nsIDOMMozWakeLockListener
>, 2> listeners(mListeners
);
138 for (uint32_t i
= 0; i
< listeners
.Length(); ++i
) {
139 listeners
[i
]->Callback(aTopic
, aState
);
146 PowerManager::ScreenEnabled()
148 return hal::GetScreenEnabled();
152 PowerManager::SetScreenEnabled(bool aEnabled
)
154 hal::SetScreenEnabled(aEnabled
);
158 PowerManager::KeyLightEnabled()
160 return hal::GetKeyLightEnabled();
164 PowerManager::SetKeyLightEnabled(bool aEnabled
)
166 hal::SetKeyLightEnabled(aEnabled
);
170 PowerManager::ScreenBrightness()
172 return hal::GetScreenBrightness();
176 PowerManager::SetScreenBrightness(double aBrightness
, ErrorResult
& aRv
)
178 if (0 <= aBrightness
&& aBrightness
<= 1) {
179 hal::SetScreenBrightness(aBrightness
);
181 aRv
.Throw(NS_ERROR_INVALID_ARG
);
186 PowerManager::CpuSleepAllowed()
188 return hal::GetCpuSleepAllowed();
192 PowerManager::SetCpuSleepAllowed(bool aAllowed
)
194 hal::SetCpuSleepAllowed(aAllowed
);
197 already_AddRefed
<PowerManager
>
198 PowerManager::CreateInstance(nsPIDOMWindow
* aWindow
)
200 nsRefPtr
<PowerManager
> powerManager
= new PowerManager();
201 if (NS_FAILED(powerManager
->Init(aWindow
))) {
202 powerManager
= nullptr;
205 return powerManager
.forget();