Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / power / PowerManager.cpp
blobef5bc6396a30ae8c9cf7a4ed1d643dd2ac563780
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"
9 #include "WakeLock.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"
18 #include "nsError.h"
19 #include "mozilla/dom/MozPowerManagerBinding.h"
20 #include "mozilla/Services.h"
22 namespace mozilla {
23 namespace dom {
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)
29 NS_INTERFACE_MAP_END
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);
42 nsresult
43 PowerManager::Init(nsIDOMWindow *aWindow)
45 mWindow = 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);
53 return NS_OK;
56 nsresult
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);
65 return NS_OK;
68 void
69 PowerManager::Reboot(ErrorResult& aRv)
71 nsCOMPtr<nsIPowerManagerService> pmService =
72 do_GetService(POWERMANAGERSERVICE_CONTRACTID);
73 if (pmService) {
74 pmService->Reboot();
75 } else {
76 aRv.Throw(NS_ERROR_UNEXPECTED);
80 void
81 PowerManager::FactoryReset(mozilla::dom::FactoryResetReason& aReason)
83 hal::FactoryReset(aReason);
86 void
87 PowerManager::PowerOff(ErrorResult& aRv)
89 nsCOMPtr<nsIPowerManagerService> pmService =
90 do_GetService(POWERMANAGERSERVICE_CONTRACTID);
91 if (pmService) {
92 pmService->PowerOff();
93 } else {
94 aRv.Throw(NS_ERROR_UNEXPECTED);
98 void
99 PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
101 if (!mListeners.Contains(aListener)) {
102 mListeners.AppendElement(aListener);
106 void
107 PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
109 mListeners.RemoveElement(aListener);
112 void
113 PowerManager::GetWakeLockState(const nsAString& aTopic,
114 nsAString& aState,
115 ErrorResult& aRv)
117 nsCOMPtr<nsIPowerManagerService> pmService =
118 do_GetService(POWERMANAGERSERVICE_CONTRACTID);
119 if (pmService) {
120 aRv = pmService->GetWakeLockState(aTopic, aState);
121 } else {
122 aRv.Throw(NS_ERROR_UNEXPECTED);
126 NS_IMETHODIMP
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
132 * cleanup the mess.
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);
142 return NS_OK;
145 bool
146 PowerManager::ScreenEnabled()
148 return hal::GetScreenEnabled();
151 void
152 PowerManager::SetScreenEnabled(bool aEnabled)
154 hal::SetScreenEnabled(aEnabled);
157 bool
158 PowerManager::KeyLightEnabled()
160 return hal::GetKeyLightEnabled();
163 void
164 PowerManager::SetKeyLightEnabled(bool aEnabled)
166 hal::SetKeyLightEnabled(aEnabled);
169 double
170 PowerManager::ScreenBrightness()
172 return hal::GetScreenBrightness();
175 void
176 PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv)
178 if (0 <= aBrightness && aBrightness <= 1) {
179 hal::SetScreenBrightness(aBrightness);
180 } else {
181 aRv.Throw(NS_ERROR_INVALID_ARG);
185 bool
186 PowerManager::CpuSleepAllowed()
188 return hal::GetCpuSleepAllowed();
191 void
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();
208 } // dom
209 } // mozilla