Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/PowerManager.h"
8 :
9 : #include "mozilla/Hal.h"
10 : #include "WakeLock.h"
11 : #include "nsDOMClassInfoID.h"
12 : #include "nsIDOMWakeLockListener.h"
13 : #include "nsIDocument.h"
14 : #include "nsIPermissionManager.h"
15 : #include "nsIPowerManagerService.h"
16 : #include "nsIPrincipal.h"
17 : #include "nsPIDOMWindow.h"
18 : #include "nsServiceManagerUtils.h"
19 : #include "nsError.h"
20 : #include "mozilla/dom/MozPowerManagerBinding.h"
21 : #include "mozilla/Services.h"
22 :
23 : namespace mozilla {
24 : namespace dom {
25 :
26 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager)
27 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
28 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
29 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener)
30 0 : NS_INTERFACE_MAP_END
31 :
32 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PowerManager, mListeners, mWindow)
33 :
34 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager)
35 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager)
36 :
37 : /* virtual */ JSObject*
38 0 : PowerManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
39 : {
40 0 : return MozPowerManagerBinding::Wrap(aCx, this, aGivenProto);
41 : }
42 :
43 : nsresult
44 0 : PowerManager::Init(nsPIDOMWindowInner* aWindow)
45 : {
46 0 : mWindow = aWindow;
47 :
48 : nsCOMPtr<nsIPowerManagerService> pmService =
49 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
50 0 : NS_ENSURE_STATE(pmService);
51 :
52 : // Add ourself to the global notification list.
53 0 : pmService->AddWakeLockListener(this);
54 0 : return NS_OK;
55 : }
56 :
57 : nsresult
58 0 : PowerManager::Shutdown()
59 : {
60 : nsCOMPtr<nsIPowerManagerService> pmService =
61 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
62 0 : NS_ENSURE_STATE(pmService);
63 :
64 : // Remove ourself from the global notification list.
65 0 : pmService->RemoveWakeLockListener(this);
66 0 : return NS_OK;
67 : }
68 :
69 : void
70 0 : PowerManager::Reboot(ErrorResult& aRv)
71 : {
72 : nsCOMPtr<nsIPowerManagerService> pmService =
73 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
74 0 : if (pmService) {
75 0 : pmService->Reboot();
76 : } else {
77 0 : aRv.Throw(NS_ERROR_UNEXPECTED);
78 : }
79 0 : }
80 :
81 : void
82 0 : PowerManager::FactoryReset(mozilla::dom::FactoryResetReason& aReason)
83 : {
84 0 : hal::FactoryReset(aReason);
85 0 : }
86 :
87 : void
88 0 : PowerManager::PowerOff(ErrorResult& aRv)
89 : {
90 : nsCOMPtr<nsIPowerManagerService> pmService =
91 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
92 0 : if (pmService) {
93 0 : pmService->PowerOff();
94 : } else {
95 0 : aRv.Throw(NS_ERROR_UNEXPECTED);
96 : }
97 0 : }
98 :
99 : void
100 0 : PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
101 : {
102 0 : if (!mListeners.Contains(aListener)) {
103 0 : mListeners.AppendElement(aListener);
104 : }
105 0 : }
106 :
107 : void
108 0 : PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
109 : {
110 0 : mListeners.RemoveElement(aListener);
111 0 : }
112 :
113 : void
114 0 : PowerManager::GetWakeLockState(const nsAString& aTopic,
115 : nsAString& aState,
116 : ErrorResult& aRv)
117 : {
118 : nsCOMPtr<nsIPowerManagerService> pmService =
119 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
120 0 : if (pmService) {
121 0 : aRv = pmService->GetWakeLockState(aTopic, aState);
122 : } else {
123 0 : aRv.Throw(NS_ERROR_UNEXPECTED);
124 : }
125 0 : }
126 :
127 : NS_IMETHODIMP
128 0 : PowerManager::Callback(const nsAString &aTopic, const nsAString &aState)
129 : {
130 : /**
131 : * We maintain a local listener list instead of using the global
132 : * list so that when the window is destroyed we don't have to
133 : * cleanup the mess.
134 : * Copy the listeners list before we walk through the callbacks
135 : * because the callbacks may install new listeners. We expect no
136 : * more than one listener per window, so it shouldn't be too long.
137 : */
138 0 : AutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mListeners);
139 0 : for (uint32_t i = 0; i < listeners.Length(); ++i) {
140 0 : listeners[i]->Callback(aTopic, aState);
141 : }
142 :
143 0 : return NS_OK;
144 : }
145 :
146 : bool
147 0 : PowerManager::ScreenEnabled()
148 : {
149 0 : return hal::GetScreenEnabled();
150 : }
151 :
152 : void
153 0 : PowerManager::SetScreenEnabled(bool aEnabled)
154 : {
155 0 : hal::SetScreenEnabled(aEnabled);
156 0 : }
157 :
158 : bool
159 0 : PowerManager::KeyLightEnabled()
160 : {
161 0 : return hal::GetKeyLightEnabled();
162 : }
163 :
164 : void
165 0 : PowerManager::SetKeyLightEnabled(bool aEnabled)
166 : {
167 0 : hal::SetKeyLightEnabled(aEnabled);
168 0 : }
169 :
170 : double
171 0 : PowerManager::ScreenBrightness()
172 : {
173 0 : return hal::GetScreenBrightness();
174 : }
175 :
176 : void
177 0 : PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv)
178 : {
179 0 : if (0 <= aBrightness && aBrightness <= 1) {
180 0 : hal::SetScreenBrightness(aBrightness);
181 : } else {
182 0 : aRv.Throw(NS_ERROR_INVALID_ARG);
183 : }
184 0 : }
185 :
186 : bool
187 0 : PowerManager::CpuSleepAllowed()
188 : {
189 0 : return hal::GetCpuSleepAllowed();
190 : }
191 :
192 : void
193 0 : PowerManager::SetCpuSleepAllowed(bool aAllowed)
194 : {
195 0 : hal::SetCpuSleepAllowed(aAllowed);
196 0 : }
197 :
198 : already_AddRefed<PowerManager>
199 0 : PowerManager::CreateInstance(nsPIDOMWindowInner* aWindow)
200 : {
201 0 : RefPtr<PowerManager> powerManager = new PowerManager();
202 0 : if (NS_FAILED(powerManager->Init(aWindow))) {
203 0 : powerManager = nullptr;
204 : }
205 :
206 0 : return powerManager.forget();
207 : }
208 :
209 : } // namespace dom
210 : } // namespace mozilla
|