Bug 699743 - Add chargingTime attribute in BatteryManager. r=sicking

This commit is contained in:
Mounir Lamouri 2011-11-09 09:58:18 +01:00
parent e22b6dd831
commit 8b8a55e897
4 changed files with 64 additions and 8 deletions

View File

@ -50,6 +50,7 @@
#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")
DOMCI_DATA(BatteryManager, mozilla::dom::battery::BatteryManager)
@ -84,7 +85,7 @@ NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper)
BatteryManager::BatteryManager()
: mLevel(kDefaultLevel)
, mCharging(kDefaultCharging)
, mDischargingTime(kUnknownRemainingTime)
, mRemainingTime(kUnknownRemainingTime)
{
}
@ -133,12 +134,25 @@ BatteryManager::GetLevel(double* aLevel)
NS_IMETHODIMP
BatteryManager::GetDischargingTime(double* aDischargingTime)
{
if (mDischargingTime == kUnknownRemainingTime) {
if (mCharging || mRemainingTime == kUnknownRemainingTime) {
*aDischargingTime = std::numeric_limits<double>::infinity();
return NS_OK;
}
*aDischargingTime = mDischargingTime;
*aDischargingTime = mRemainingTime;
return NS_OK;
}
NS_IMETHODIMP
BatteryManager::GetChargingTime(double* aChargingTime)
{
if (!mCharging || mRemainingTime == kUnknownRemainingTime) {
*aChargingTime = std::numeric_limits<double>::infinity();
return NS_OK;
}
*aChargingTime = mRemainingTime;
return NS_OK;
}
@ -184,6 +198,21 @@ BatteryManager::SetOndischargingtimechange(nsIDOMEventListener* aOndischargingti
aOndischargingtimechange);
}
NS_IMETHODIMP
BatteryManager::GetOnchargingtimechange(nsIDOMEventListener** aOnchargingtimechange)
{
return GetInnerEventListener(mOnChargingTimeChangeListener,
aOnchargingtimechange);
}
NS_IMETHODIMP
BatteryManager::SetOnchargingtimechange(nsIDOMEventListener* aOnchargingtimechange)
{
return RemoveAddEventListener(CHARGINGTIMECHANGE_EVENT_NAME,
mOnChargingTimeChangeListener,
aOnchargingtimechange);
}
nsresult
BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)
{
@ -213,7 +242,7 @@ BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
{
double previousLevel = mLevel;
bool previousCharging = mCharging;
double previousDischargingTime = mDischargingTime;
double previousRemainingTime = mRemainingTime;
UpdateFromBatteryInfo(aBatteryInfo);
@ -225,8 +254,27 @@ BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
}
if (previousDischargingTime != mDischargingTime) {
DispatchTrustedEventToSelf(DISCHARGINGTIMECHANGE_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.
*/
if (mCharging != previousCharging) {
if (previousRemainingTime != kUnknownRemainingTime) {
DispatchTrustedEventToSelf(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
: DISCHARGINGTIMECHANGE_EVENT_NAME);
}
if (mRemainingTime != kUnknownRemainingTime) {
DispatchTrustedEventToSelf(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
: DISCHARGINGTIMECHANGE_EVENT_NAME);
}
} else if (previousRemainingTime != mRemainingTime) {
DispatchTrustedEventToSelf(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
: DISCHARGINGTIMECHANGE_EVENT_NAME);
}
}

View File

@ -95,11 +95,16 @@ private:
double mLevel;
bool mCharging;
double mDischargingTime;
/**
* Represents the discharging time or the charging time, dpending on the
* current battery status (charging or not).
*/
double mRemainingTime;
nsRefPtr<nsDOMEventListenerWrapper> mOnLevelChangeListener;
nsRefPtr<nsDOMEventListenerWrapper> mOnChargingChangeListener;
nsRefPtr<nsDOMEventListenerWrapper> mOnDischargingTimeChangeListener;
nsRefPtr<nsDOMEventListenerWrapper> mOnChargingTimeChangeListener;
};
} // namespace battery

View File

@ -38,14 +38,16 @@
interface nsIDOMEventListener;
[scriptable, function, uuid(78959ad6-0250-4180-bfbb-a9601513c601)]
[scriptable, function, uuid(6dcb803b-e968-4c02-88f5-049a3f2a2efb)]
interface nsIDOMBatteryManager : nsIDOMEventTarget
{
readonly attribute double level;
readonly attribute boolean charging;
readonly attribute double dischargingTime;
readonly attribute double chargingTime;
attribute nsIDOMEventListener onlevelchange;
attribute nsIDOMEventListener onchargingchange;
attribute nsIDOMEventListener ondischargingtimechange;
attribute nsIDOMEventListener onchargingtimechange;
};

View File

@ -20,6 +20,7 @@ var battery = navigator.mozBattery;
is(battery.level, 1.0, "Default battery level should be 1.0");
is(battery.charging, true, "Default charging value should be true");
is(battery.dischargingTime, Infinity, "Default dischargingTime should be Inifinity");
is(battery.chargingTime, Infinity, "Default chargingTime should be Inifinity");
</script>
</pre>