forked from mozilla-b2g/gecko-b2g
/
BatteryManager.cpp
259 lines (223 loc) · 7.42 KB
/
BatteryManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <cmath>
#include <limits>
#include "BatteryManager.h"
#include "Constants.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/Hal.h"
#include "mozilla/dom/BatteryManagerBinding.h"
#include "mozilla/Preferences.h"
#include "nsContentUtils.h"
#include "nsIDOMClassInfo.h"
#include "nsIDocument.h"
/**
* We have to use macros here because our leak analysis tool things we are
* leaking strings when we have |static const nsString|. Sad :(
*/
#define LEVELCHANGE_EVENT_NAME NS_LITERAL_STRING("levelchange")
#define CHARGINGCHANGE_EVENT_NAME NS_LITERAL_STRING("chargingchange")
#define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
#define CHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("chargingtimechange")
#define BATTERYHEALTHCHANGE_EVENT_NAME NS_LITERAL_STRING("batteryhealthchange")
namespace mozilla {
namespace dom {
namespace battery {
BatteryManager::BatteryManager(nsPIDOMWindowInner* aWindow)
: DOMEventTargetHelper(aWindow)
, mLevel(kDefaultLevel)
, mCharging(kDefaultCharging)
, mRemainingTime(kDefaultRemainingTime)
, mHealth(kDefaultHealth)
{
}
void
BatteryManager::Init()
{
hal::RegisterBatteryObserver(this);
hal::BatteryInformation batteryInfo;
hal::GetCurrentBatteryInformation(&batteryInfo);
UpdateFromBatteryInfo(batteryInfo);
}
void
BatteryManager::Shutdown()
{
hal::UnregisterBatteryObserver(this);
}
JSObject*
BatteryManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return BatteryManagerBinding::Wrap(aCx, this, aGivenProto);
}
bool
BatteryManager::Charging() const
{
MOZ_ASSERT(NS_IsMainThread());
// For testing, unable to report the battery status information
if (Preferences::GetBool("dom.battery.test.default", false)) {
return true;
}
if (Preferences::GetBool("dom.battery.test.charging", false)) {
return true;
}
if (Preferences::GetBool("dom.battery.test.discharging", false)) {
return false;
}
return mCharging;
}
double
BatteryManager::DischargingTime() const
{
MOZ_ASSERT(NS_IsMainThread());
// For testing, unable to report the battery status information
if (Preferences::GetBool("dom.battery.test.default", false)) {
return std::numeric_limits<double>::infinity();
}
if (Preferences::GetBool("dom.battery.test.discharging", false)) {
return 42.0;
}
if (Charging() || mRemainingTime == kUnknownRemainingTime) {
return std::numeric_limits<double>::infinity();
}
return mRemainingTime;
}
double
BatteryManager::ChargingTime() const
{
MOZ_ASSERT(NS_IsMainThread());
// For testing, unable to report the battery status information
if (Preferences::GetBool("dom.battery.test.default", false)) {
return 0.0;
}
if (Preferences::GetBool("dom.battery.test.charging", false)) {
return 42.0;
}
if (!Charging() || mRemainingTime == kUnknownRemainingTime) {
return std::numeric_limits<double>::infinity();
}
return mRemainingTime;
}
double
BatteryManager::Level() const
{
MOZ_ASSERT(NS_IsMainThread());
// For testing, unable to report the battery status information
if (Preferences::GetBool("dom.battery.test.default")) {
return 1.0;
} else if (Preferences::GetBool("dom.battery.test.dummy_battery_level", false)) {
return 0.7;
}
return mLevel;
}
double
BatteryManager::Temperature() const
{
MOZ_ASSERT(NS_IsMainThread());
if (Preferences::GetBool("dom.battery.test.dummy_thermal_status", false)) {
return kDefaultTemperature;
}
return hal::GetBatteryTemperature();
}
BatteryHealth
BatteryManager::Health() const
{
MOZ_ASSERT(NS_IsMainThread());
if (Preferences::GetBool("dom.battery.test.dummy_thermal_status", false)) {
return BatteryHealth::Good;
}
return mHealth;
}
bool
BatteryManager::Present() const
{
MOZ_ASSERT(NS_IsMainThread());
return hal::IsBatteryPresent();
}
void
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
{
mLevel = aBatteryInfo.level();
// Round to the nearest ten percent for non-chrome and non-certified apps
nsIDocument* doc = GetOwner() ? GetOwner()->GetDoc() : nullptr;
uint16_t status = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
if (doc) {
doc->NodePrincipal()->GetAppStatus(&status);
}
mCharging = aBatteryInfo.charging();
mRemainingTime = aBatteryInfo.remainingTime();
mHealth = aBatteryInfo.health();
if (!nsContentUtils::IsChromeDoc(doc) &&
status != nsIPrincipal::APP_STATUS_CERTIFIED)
{
mLevel = lround(mLevel * 10.0) / 10.0;
if (mLevel == 1.0) {
mRemainingTime = mCharging ? kDefaultRemainingTime : kUnknownRemainingTime;
}
}
// Add some guards to make sure the values are coherent.
if (mLevel == 1.0 && mCharging == true &&
mRemainingTime != kDefaultRemainingTime) {
mRemainingTime = kDefaultRemainingTime;
NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
"should be 0. Please fix your backend!");
}
}
void
BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
{
double previousLevel = mLevel;
bool previousCharging = mCharging;
double previousRemainingTime = mRemainingTime;
BatteryHealth previousHealth = mHealth;
UpdateFromBatteryInfo(aBatteryInfo);
if (previousCharging != mCharging) {
DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
}
/*
* Don't fire levelchange event if dom.battery.test.dummy_battery_level is
* true.
*/
if (!Preferences::GetBool("dom.battery.test.dummy_battery_level", false) &&
previousLevel != mLevel) {
DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
}
/*
* Don't fire batteryhealthchange event if
* dom.battery.test.dummy_thermal_status is true.
*/
if (!Preferences::GetBool("dom.battery.test.dummy_thermal_status", false) &&
previousHealth != mHealth) {
DispatchTrustedEvent(BATTERYHEALTHCHANGE_EVENT_NAME);
}
/*
* There are a few situations that could happen here:
* 1. Charging state changed:
* a. Previous remaining time wasn't unkwonw, we have to fire an event for
* the change.
* b. New remaining time isn't unkwonw, we have to fire an event for it.
* 2. Charging state didn't change but remainingTime did, we have to fire
* the event that correspond to the current charging state.
* 3. If dom.battery.test.dummy_battery_level is true, won't dispatch event
* regarding estimated charging/discharging time.
*/
if (!Preferences::GetBool("dom.battery.test.dummy_battery_level", false) &&
mCharging != previousCharging) {
if (previousRemainingTime != kUnknownRemainingTime) {
DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
: DISCHARGINGTIMECHANGE_EVENT_NAME);
}
if (mRemainingTime != kUnknownRemainingTime) {
DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
: DISCHARGINGTIMECHANGE_EVENT_NAME);
}
} else if (previousRemainingTime != mRemainingTime) {
DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
: DISCHARGINGTIMECHANGE_EVENT_NAME);
}
}
} // namespace battery
} // namespace dom
} // namespace mozilla