Backed out changeset 3fe07c50c854 (bug 946316) for bustage. a=backout
[gecko.git] / dom / battery / BatteryManager.cpp
blob2a1278b341dd47f969954f6039aed42ac7f1ca77
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 <limits>
7 #include "mozilla/Hal.h"
8 #include "BatteryManager.h"
9 #include "nsIDOMClassInfo.h"
10 #include "Constants.h"
11 #include "nsDOMEvent.h"
12 #include "mozilla/Preferences.h"
13 #include "nsDOMEventTargetHelper.h"
14 #include "mozilla/dom/BatteryManagerBinding.h"
16 /**
17 * We have to use macros here because our leak analysis tool things we are
18 * leaking strings when we have |static const nsString|. Sad :(
20 #define LEVELCHANGE_EVENT_NAME NS_LITERAL_STRING("levelchange")
21 #define CHARGINGCHANGE_EVENT_NAME NS_LITERAL_STRING("chargingchange")
22 #define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
23 #define CHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("chargingtimechange")
25 namespace mozilla {
26 namespace dom {
27 namespace battery {
29 BatteryManager::BatteryManager()
30 : mLevel(kDefaultLevel)
31 , mCharging(kDefaultCharging)
32 , mRemainingTime(kDefaultRemainingTime)
34 SetIsDOMBinding();
37 void
38 BatteryManager::Init(nsPIDOMWindow *aWindow)
40 BindToOwner(aWindow);
42 hal::RegisterBatteryObserver(this);
44 hal::BatteryInformation batteryInfo;
45 hal::GetCurrentBatteryInformation(&batteryInfo);
47 UpdateFromBatteryInfo(batteryInfo);
50 void
51 BatteryManager::Shutdown()
53 hal::UnregisterBatteryObserver(this);
56 JSObject*
57 BatteryManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
59 return BatteryManagerBinding::Wrap(aCx, aScope, this);
62 double
63 BatteryManager::DischargingTime() const
65 if (mCharging || mRemainingTime == kUnknownRemainingTime) {
66 return std::numeric_limits<double>::infinity();
69 return mRemainingTime;
72 double
73 BatteryManager::ChargingTime() const
75 if (!mCharging || mRemainingTime == kUnknownRemainingTime) {
76 return std::numeric_limits<double>::infinity();
79 return mRemainingTime;
82 void
83 BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
85 mLevel = aBatteryInfo.level();
86 mCharging = aBatteryInfo.charging();
87 mRemainingTime = aBatteryInfo.remainingTime();
89 // Add some guards to make sure the values are coherent.
90 if (mLevel == 1.0 && mCharging == true &&
91 mRemainingTime != kDefaultRemainingTime) {
92 mRemainingTime = kDefaultRemainingTime;
93 NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
94 "should be 0. Please fix your backend!");
98 void
99 BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
101 double previousLevel = mLevel;
102 bool previousCharging = mCharging;
103 double previousRemainingTime = mRemainingTime;
105 UpdateFromBatteryInfo(aBatteryInfo);
107 if (previousCharging != mCharging) {
108 DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
111 if (previousLevel != mLevel) {
112 DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
116 * There are a few situations that could happen here:
117 * 1. Charging state changed:
118 * a. Previous remaining time wasn't unkwonw, we have to fire an event for
119 * the change.
120 * b. New remaining time isn't unkwonw, we have to fire an event for it.
121 * 2. Charging state didn't change but remainingTime did, we have to fire
122 * the event that correspond to the current charging state.
124 if (mCharging != previousCharging) {
125 if (previousRemainingTime != kUnknownRemainingTime) {
126 DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
127 : DISCHARGINGTIMECHANGE_EVENT_NAME);
129 if (mRemainingTime != kUnknownRemainingTime) {
130 DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
131 : DISCHARGINGTIMECHANGE_EVENT_NAME);
133 } else if (previousRemainingTime != mRemainingTime) {
134 DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
135 : DISCHARGINGTIMECHANGE_EVENT_NAME);
139 /* static */ bool
140 BatteryManager::HasSupport()
142 return Preferences::GetBool("dom.battery.enabled", true);
145 } // namespace battery
146 } // namespace dom
147 } // namespace mozilla