2011-11-02 08:01:32 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-11-02 08:01:32 -07:00
|
|
|
|
2011-11-09 00:57:57 -08:00
|
|
|
#include <limits>
|
2011-11-02 08:27:06 -07:00
|
|
|
#include "mozilla/Hal.h"
|
2011-11-02 08:01:32 -07:00
|
|
|
#include "BatteryManager.h"
|
|
|
|
#include "nsIDOMClassInfo.h"
|
|
|
|
#include "Constants.h"
|
2011-11-02 08:27:06 -07:00
|
|
|
#include "nsDOMEvent.h"
|
2011-11-03 16:31:32 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-02-07 18:53:33 -08:00
|
|
|
#include "nsDOMEventTargetHelper.h"
|
2011-11-02 08:27:06 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We have to use macros here because our leak analysis tool things we are
|
|
|
|
* leaking strings when we have |static const nsString|. Sad :(
|
|
|
|
*/
|
2011-11-09 00:57:57 -08:00
|
|
|
#define LEVELCHANGE_EVENT_NAME NS_LITERAL_STRING("levelchange")
|
|
|
|
#define CHARGINGCHANGE_EVENT_NAME NS_LITERAL_STRING("chargingchange")
|
|
|
|
#define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
|
2011-11-09 00:58:18 -08:00
|
|
|
#define CHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("chargingtimechange")
|
2011-11-02 08:01:32 -07:00
|
|
|
|
2012-07-05 03:05:09 -07:00
|
|
|
DOMCI_DATA(BatteryManager, mozilla::dom::battery::BatteryManager)
|
2011-11-02 08:01:32 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace battery {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BatteryManager,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2011-11-02 08:01:32 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BatteryManager,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2011-11-02 08:01:32 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BatteryManager)
|
2012-07-05 03:05:09 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMBatteryManager)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BatteryManager)
|
2012-02-07 18:53:33 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
2011-11-02 08:01:32 -07:00
|
|
|
|
2012-02-07 18:53:33 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper)
|
2011-11-02 08:01:32 -07:00
|
|
|
|
2012-08-30 20:45:16 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(BatteryManager, levelchange)
|
|
|
|
NS_IMPL_EVENT_HANDLER(BatteryManager, chargingchange)
|
|
|
|
NS_IMPL_EVENT_HANDLER(BatteryManager, chargingtimechange)
|
|
|
|
NS_IMPL_EVENT_HANDLER(BatteryManager, dischargingtimechange)
|
|
|
|
|
2011-11-02 08:01:32 -07:00
|
|
|
BatteryManager::BatteryManager()
|
|
|
|
: mLevel(kDefaultLevel)
|
|
|
|
, mCharging(kDefaultCharging)
|
2011-12-01 02:49:42 -08:00
|
|
|
, mRemainingTime(kDefaultRemainingTime)
|
2011-11-02 08:01:32 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-02 08:27:06 -07:00
|
|
|
void
|
2012-03-12 17:56:07 -07:00
|
|
|
BatteryManager::Init(nsPIDOMWindow *aWindow)
|
2011-11-02 08:27:06 -07:00
|
|
|
{
|
2012-03-12 17:56:07 -07:00
|
|
|
BindToOwner(aWindow);
|
2011-11-19 02:37:21 -08:00
|
|
|
|
2011-11-02 08:27:06 -07:00
|
|
|
hal::RegisterBatteryObserver(this);
|
|
|
|
|
2011-12-14 02:40:29 -08:00
|
|
|
hal::BatteryInformation batteryInfo;
|
|
|
|
hal::GetCurrentBatteryInformation(&batteryInfo);
|
2011-11-02 08:27:06 -07:00
|
|
|
|
2011-12-14 02:40:29 -08:00
|
|
|
UpdateFromBatteryInfo(batteryInfo);
|
2011-11-02 08:27:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BatteryManager::Shutdown()
|
|
|
|
{
|
|
|
|
hal::UnregisterBatteryObserver(this);
|
|
|
|
}
|
|
|
|
|
2011-11-02 08:01:32 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BatteryManager::GetCharging(bool* aCharging)
|
|
|
|
{
|
|
|
|
*aCharging = mCharging;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-09 00:56:52 -08:00
|
|
|
BatteryManager::GetLevel(double* aLevel)
|
2011-11-02 08:01:32 -07:00
|
|
|
{
|
|
|
|
*aLevel = mLevel;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-09 00:57:57 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BatteryManager::GetDischargingTime(double* aDischargingTime)
|
|
|
|
{
|
2011-11-09 00:58:18 -08:00
|
|
|
if (mCharging || mRemainingTime == kUnknownRemainingTime) {
|
2011-11-09 00:57:57 -08:00
|
|
|
*aDischargingTime = std::numeric_limits<double>::infinity();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-09 00:58:18 -08:00
|
|
|
*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;
|
2011-11-09 00:57:57 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-02 08:27:06 -07:00
|
|
|
nsresult
|
|
|
|
BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
|
2011-11-02 08:27:06 -07:00
|
|
|
nsresult rv = event->InitEvent(aEventName, false, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-19 01:11:16 -08:00
|
|
|
rv = event->SetTrusted(true);
|
2011-11-02 08:27:06 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
rv = DispatchEvent(event, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
|
|
|
|
{
|
|
|
|
mLevel = aBatteryInfo.level();
|
|
|
|
mCharging = aBatteryInfo.charging();
|
2011-11-09 00:58:59 -08:00
|
|
|
mRemainingTime = aBatteryInfo.remainingTime();
|
2011-12-01 02:49:42 -08:00
|
|
|
|
|
|
|
// 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!");
|
|
|
|
}
|
2011-11-02 08:27:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
|
|
|
|
{
|
2011-11-09 00:56:52 -08:00
|
|
|
double previousLevel = mLevel;
|
2011-11-02 08:27:06 -07:00
|
|
|
bool previousCharging = mCharging;
|
2011-11-09 00:58:18 -08:00
|
|
|
double previousRemainingTime = mRemainingTime;
|
2011-11-02 08:27:06 -07:00
|
|
|
|
|
|
|
UpdateFromBatteryInfo(aBatteryInfo);
|
|
|
|
|
|
|
|
if (previousCharging != mCharging) {
|
|
|
|
DispatchTrustedEventToSelf(CHARGINGCHANGE_EVENT_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousLevel != mLevel) {
|
|
|
|
DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
|
|
|
|
}
|
2011-11-09 00:57:57 -08:00
|
|
|
|
2011-11-09 00:58:18 -08:00
|
|
|
/*
|
|
|
|
* 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);
|
2011-11-09 00:57:57 -08:00
|
|
|
}
|
2011-11-02 08:27:06 -07:00
|
|
|
}
|
|
|
|
|
2011-11-03 16:31:32 -07:00
|
|
|
/* static */ bool
|
|
|
|
BatteryManager::HasSupport()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("dom.battery.enabled", true);
|
|
|
|
}
|
|
|
|
|
2011-11-02 08:01:32 -07:00
|
|
|
} // namespace battery
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|