1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include <limits>
39 : #include "mozilla/Hal.h"
40 : #include "BatteryManager.h"
41 : #include "nsIDOMClassInfo.h"
42 : #include "Constants.h"
43 : #include "nsDOMEvent.h"
44 : #include "mozilla/Preferences.h"
45 : #include "nsDOMEventTargetHelper.h"
46 :
47 : /**
48 : * We have to use macros here because our leak analysis tool things we are
49 : * leaking strings when we have |static const nsString|. Sad :(
50 : */
51 : #define LEVELCHANGE_EVENT_NAME NS_LITERAL_STRING("levelchange")
52 : #define CHARGINGCHANGE_EVENT_NAME NS_LITERAL_STRING("chargingchange")
53 : #define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
54 : #define CHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("chargingtimechange")
55 :
56 : DOMCI_DATA(MozBatteryManager, mozilla::dom::battery::BatteryManager)
57 :
58 : namespace mozilla {
59 : namespace dom {
60 : namespace battery {
61 :
62 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager)
63 :
64 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BatteryManager,
65 : nsDOMEventTargetHelper)
66 0 : NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(levelchange)
67 0 : NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(chargingchange)
68 0 : NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(chargingtimechange)
69 0 : NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(dischargingtimechange)
70 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
71 :
72 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BatteryManager,
73 : nsDOMEventTargetHelper)
74 0 : NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(levelchange)
75 0 : NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(chargingchange)
76 0 : NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(chargingtimechange)
77 0 : NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(dischargingtimechange)
78 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
79 :
80 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BatteryManager)
81 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMozBatteryManager)
82 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozBatteryManager)
83 0 : NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
84 :
85 0 : NS_IMPL_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetHelper)
86 0 : NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper)
87 :
88 0 : BatteryManager::BatteryManager()
89 : : mLevel(kDefaultLevel)
90 : , mCharging(kDefaultCharging)
91 0 : , mRemainingTime(kDefaultRemainingTime)
92 : {
93 0 : }
94 :
95 : void
96 0 : BatteryManager::Init(nsPIDOMWindow *aWindow)
97 : {
98 0 : BindToOwner(aWindow);
99 :
100 0 : hal::RegisterBatteryObserver(this);
101 :
102 0 : hal::BatteryInformation batteryInfo;
103 0 : hal::GetCurrentBatteryInformation(&batteryInfo);
104 :
105 0 : UpdateFromBatteryInfo(batteryInfo);
106 0 : }
107 :
108 : void
109 0 : BatteryManager::Shutdown()
110 : {
111 0 : hal::UnregisterBatteryObserver(this);
112 0 : }
113 :
114 : NS_IMETHODIMP
115 0 : BatteryManager::GetCharging(bool* aCharging)
116 : {
117 0 : *aCharging = mCharging;
118 :
119 0 : return NS_OK;
120 : }
121 :
122 : NS_IMETHODIMP
123 0 : BatteryManager::GetLevel(double* aLevel)
124 : {
125 0 : *aLevel = mLevel;
126 :
127 0 : return NS_OK;
128 : }
129 :
130 : NS_IMETHODIMP
131 0 : BatteryManager::GetDischargingTime(double* aDischargingTime)
132 : {
133 0 : if (mCharging || mRemainingTime == kUnknownRemainingTime) {
134 0 : *aDischargingTime = std::numeric_limits<double>::infinity();
135 0 : return NS_OK;
136 : }
137 :
138 0 : *aDischargingTime = mRemainingTime;
139 :
140 0 : return NS_OK;
141 : }
142 :
143 : NS_IMETHODIMP
144 0 : BatteryManager::GetChargingTime(double* aChargingTime)
145 : {
146 0 : if (!mCharging || mRemainingTime == kUnknownRemainingTime) {
147 0 : *aChargingTime = std::numeric_limits<double>::infinity();
148 0 : return NS_OK;
149 : }
150 :
151 0 : *aChargingTime = mRemainingTime;
152 :
153 0 : return NS_OK;
154 : }
155 :
156 0 : NS_IMPL_EVENT_HANDLER(BatteryManager, levelchange)
157 0 : NS_IMPL_EVENT_HANDLER(BatteryManager, chargingchange)
158 0 : NS_IMPL_EVENT_HANDLER(BatteryManager, chargingtimechange)
159 0 : NS_IMPL_EVENT_HANDLER(BatteryManager, dischargingtimechange)
160 :
161 : nsresult
162 0 : BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)
163 : {
164 0 : nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
165 0 : nsresult rv = event->InitEvent(aEventName, false, false);
166 0 : NS_ENSURE_SUCCESS(rv, rv);
167 :
168 0 : rv = event->SetTrusted(true);
169 0 : NS_ENSURE_SUCCESS(rv, rv);
170 :
171 : bool dummy;
172 0 : rv = DispatchEvent(event, &dummy);
173 0 : NS_ENSURE_SUCCESS(rv, rv);
174 :
175 0 : return NS_OK;
176 : }
177 :
178 : void
179 0 : BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
180 : {
181 0 : mLevel = aBatteryInfo.level();
182 0 : mCharging = aBatteryInfo.charging();
183 0 : mRemainingTime = aBatteryInfo.remainingTime();
184 :
185 : // Add some guards to make sure the values are coherent.
186 0 : if (mLevel == 1.0 && mCharging == true &&
187 : mRemainingTime != kDefaultRemainingTime) {
188 0 : mRemainingTime = kDefaultRemainingTime;
189 : NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
190 0 : "should be 0. Please fix your backend!");
191 : }
192 0 : }
193 :
194 : void
195 0 : BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
196 : {
197 0 : double previousLevel = mLevel;
198 0 : bool previousCharging = mCharging;
199 0 : double previousRemainingTime = mRemainingTime;
200 :
201 0 : UpdateFromBatteryInfo(aBatteryInfo);
202 :
203 0 : if (previousCharging != mCharging) {
204 0 : DispatchTrustedEventToSelf(CHARGINGCHANGE_EVENT_NAME);
205 : }
206 :
207 0 : if (previousLevel != mLevel) {
208 0 : DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
209 : }
210 :
211 : /*
212 : * There are a few situations that could happen here:
213 : * 1. Charging state changed:
214 : * a. Previous remaining time wasn't unkwonw, we have to fire an event for
215 : * the change.
216 : * b. New remaining time isn't unkwonw, we have to fire an event for it.
217 : * 2. Charging state didn't change but remainingTime did, we have to fire
218 : * the event that correspond to the current charging state.
219 : */
220 0 : if (mCharging != previousCharging) {
221 0 : if (previousRemainingTime != kUnknownRemainingTime) {
222 0 : DispatchTrustedEventToSelf(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
223 0 : : DISCHARGINGTIMECHANGE_EVENT_NAME);
224 : }
225 0 : if (mRemainingTime != kUnknownRemainingTime) {
226 0 : DispatchTrustedEventToSelf(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
227 0 : : DISCHARGINGTIMECHANGE_EVENT_NAME);
228 : }
229 0 : } else if (previousRemainingTime != mRemainingTime) {
230 0 : DispatchTrustedEventToSelf(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
231 0 : : DISCHARGINGTIMECHANGE_EVENT_NAME);
232 : }
233 0 : }
234 :
235 : /* static */ bool
236 306 : BatteryManager::HasSupport()
237 : {
238 306 : return Preferences::GetBool("dom.battery.enabled", true);
239 : }
240 :
241 : } // namespace battery
242 : } // namespace dom
243 4392 : } // namespace mozilla
|