Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/battery/BatteryManager.cpp
Line
Count
Source (jump to first uncovered line)
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 <cmath>
8
#include <limits>
9
#include "BatteryManager.h"
10
#include "Constants.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
#include "mozilla/Hal.h"
13
#include "mozilla/dom/BatteryManagerBinding.h"
14
#include "mozilla/Preferences.h"
15
#include "nsContentUtils.h"
16
#include "nsIDocument.h"
17
18
/**
19
 * We have to use macros here because our leak analysis tool things we are
20
 * leaking strings when we have |static const nsString|. Sad :(
21
 */
22
0
#define LEVELCHANGE_EVENT_NAME           NS_LITERAL_STRING("levelchange")
23
0
#define CHARGINGCHANGE_EVENT_NAME        NS_LITERAL_STRING("chargingchange")
24
0
#define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
25
0
#define CHARGINGTIMECHANGE_EVENT_NAME    NS_LITERAL_STRING("chargingtimechange")
26
27
namespace mozilla {
28
namespace dom {
29
namespace battery {
30
31
BatteryManager::BatteryManager(nsPIDOMWindowInner* aWindow)
32
  : DOMEventTargetHelper(aWindow)
33
  , mLevel(kDefaultLevel)
34
  , mCharging(kDefaultCharging)
35
  , mRemainingTime(kDefaultRemainingTime)
36
0
{
37
0
}
38
39
void
40
BatteryManager::Init()
41
0
{
42
0
  hal::RegisterBatteryObserver(this);
43
0
44
0
  hal::BatteryInformation batteryInfo;
45
0
  hal::GetCurrentBatteryInformation(&batteryInfo);
46
0
47
0
  UpdateFromBatteryInfo(batteryInfo);
48
0
}
49
50
void
51
BatteryManager::Shutdown()
52
0
{
53
0
  hal::UnregisterBatteryObserver(this);
54
0
}
55
56
JSObject*
57
BatteryManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
58
0
{
59
0
  return BatteryManager_Binding::Wrap(aCx, this, aGivenProto);
60
0
}
61
62
bool
63
BatteryManager::Charging() const
64
0
{
65
0
  MOZ_ASSERT(NS_IsMainThread());
66
0
  // For testing, unable to report the battery status information
67
0
  if (Preferences::GetBool("dom.battery.test.default", false)) {
68
0
    return true;
69
0
  }
70
0
  if (Preferences::GetBool("dom.battery.test.charging", false)) {
71
0
    return true;
72
0
  }
73
0
  if (Preferences::GetBool("dom.battery.test.discharging", false)) {
74
0
    return false;
75
0
  }
76
0
77
0
  return mCharging;
78
0
}
79
80
double
81
BatteryManager::DischargingTime() const
82
0
{
83
0
  MOZ_ASSERT(NS_IsMainThread());
84
0
  // For testing, unable to report the battery status information
85
0
  if (Preferences::GetBool("dom.battery.test.default", false)) {
86
0
    return std::numeric_limits<double>::infinity();
87
0
  }
88
0
  if (Preferences::GetBool("dom.battery.test.discharging", false)) {
89
0
    return 42.0;
90
0
  }
91
0
92
0
  if (Charging() || mRemainingTime == kUnknownRemainingTime) {
93
0
    return std::numeric_limits<double>::infinity();
94
0
  }
95
0
96
0
  return mRemainingTime;
97
0
}
98
99
double
100
BatteryManager::ChargingTime() const
101
0
{
102
0
  MOZ_ASSERT(NS_IsMainThread());
103
0
  // For testing, unable to report the battery status information
104
0
  if (Preferences::GetBool("dom.battery.test.default", false)) {
105
0
    return 0.0;
106
0
  }
107
0
  if (Preferences::GetBool("dom.battery.test.charging", false)) {
108
0
    return 42.0;
109
0
  }
110
0
111
0
  if (!Charging() || mRemainingTime == kUnknownRemainingTime) {
112
0
    return std::numeric_limits<double>::infinity();
113
0
  }
114
0
115
0
  return mRemainingTime;
116
0
}
117
118
double
119
BatteryManager::Level() const
120
0
{
121
0
  MOZ_ASSERT(NS_IsMainThread());
122
0
  // For testing, unable to report the battery status information
123
0
  if (Preferences::GetBool("dom.battery.test.default")) {
124
0
    return 1.0;
125
0
  }
126
0
127
0
  return mLevel;
128
0
}
129
130
void
131
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
132
0
{
133
0
  mLevel = aBatteryInfo.level();
134
0
135
0
  // Round to the nearest ten percent for non-chrome.
136
0
  nsIDocument* doc = GetOwner() ? GetOwner()->GetDoc() : nullptr;
137
0
138
0
  mCharging = aBatteryInfo.charging();
139
0
  mRemainingTime = aBatteryInfo.remainingTime();
140
0
141
0
  if (!nsContentUtils::IsChromeDoc(doc))
142
0
  {
143
0
    mLevel = lround(mLevel * 10.0) / 10.0;
144
0
    if (mLevel == 1.0) {
145
0
      mRemainingTime = mCharging ? kDefaultRemainingTime : kUnknownRemainingTime;
146
0
    } else if (mRemainingTime != kUnknownRemainingTime) {
147
0
      // Round the remaining time to a multiple of 15 minutes and never zero
148
0
      const double MINUTES_15 = 15.0 * 60.0;
149
0
      mRemainingTime = fmax(lround(mRemainingTime / MINUTES_15) * MINUTES_15,
150
0
                            MINUTES_15);
151
0
    }
152
0
  }
153
0
154
0
  // Add some guards to make sure the values are coherent.
155
0
  if (mLevel == 1.0 && mCharging == true &&
156
0
      mRemainingTime != kDefaultRemainingTime) {
157
0
    mRemainingTime = kDefaultRemainingTime;
158
0
    NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
159
0
             "should be 0. Please fix your backend!");
160
0
  }
161
0
}
162
163
void
164
BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
165
0
{
166
0
  double previousLevel = mLevel;
167
0
  bool previousCharging = mCharging;
168
0
  double previousRemainingTime = mRemainingTime;
169
0
170
0
  UpdateFromBatteryInfo(aBatteryInfo);
171
0
172
0
  if (previousCharging != mCharging) {
173
0
    DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
174
0
  }
175
0
176
0
  if (previousLevel != mLevel) {
177
0
    DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
178
0
  }
179
0
180
0
  /*
181
0
   * There are a few situations that could happen here:
182
0
   * 1. Charging state changed:
183
0
   *   a. Previous remaining time wasn't unkwonw, we have to fire an event for
184
0
   *      the change.
185
0
   *   b. New remaining time isn't unkwonw, we have to fire an event for it.
186
0
   * 2. Charging state didn't change but remainingTime did, we have to fire
187
0
   *    the event that correspond to the current charging state.
188
0
   */
189
0
  if (mCharging != previousCharging) {
190
0
    if (previousRemainingTime != kUnknownRemainingTime) {
191
0
      DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
192
0
                                            : DISCHARGINGTIMECHANGE_EVENT_NAME);
193
0
    }
194
0
    if (mRemainingTime != kUnknownRemainingTime) {
195
0
      DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
196
0
                                     : DISCHARGINGTIMECHANGE_EVENT_NAME);
197
0
    }
198
0
  } else if (previousRemainingTime != mRemainingTime) {
199
0
    DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
200
0
                                   : DISCHARGINGTIMECHANGE_EVENT_NAME);
201
0
  }
202
0
}
203
204
} // namespace battery
205
} // namespace dom
206
} // namespace mozilla