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:01:32 -07:00
|
|
|
#include "BatteryManager.h"
|
|
|
|
#include "Constants.h"
|
2014-03-31 23:13:50 -07:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "mozilla/Hal.h"
|
2013-02-05 04:54:49 -08:00
|
|
|
#include "mozilla/dom/BatteryManagerBinding.h"
|
2014-03-31 23:13:50 -07:00
|
|
|
#include "nsIDOMClassInfo.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
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace battery {
|
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
BatteryManager::BatteryManager(nsPIDOMWindow* aWindow)
|
2014-03-31 23:13:50 -07:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2014-01-06 18:53:23 -08:00
|
|
|
, mLevel(kDefaultLevel)
|
2011-11-02 08:01:32 -07:00
|
|
|
, 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
|
2014-01-06 18:53:23 -08:00
|
|
|
BatteryManager::Init()
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:54:49 -08:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
BatteryManager::WrapObject(JSContext* aCx)
|
2011-11-02 08:01:32 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return BatteryManagerBinding::Wrap(aCx, this);
|
2011-11-02 08:01:32 -07:00
|
|
|
}
|
|
|
|
|
2013-02-05 04:54:49 -08:00
|
|
|
double
|
|
|
|
BatteryManager::DischargingTime() const
|
2011-11-09 00:57:57 -08:00
|
|
|
{
|
2011-11-09 00:58:18 -08:00
|
|
|
if (mCharging || mRemainingTime == kUnknownRemainingTime) {
|
2013-02-05 04:54:49 -08:00
|
|
|
return std::numeric_limits<double>::infinity();
|
2011-11-09 00:57:57 -08:00
|
|
|
}
|
|
|
|
|
2013-02-05 04:54:49 -08:00
|
|
|
return mRemainingTime;
|
2011-11-09 00:58:18 -08:00
|
|
|
}
|
|
|
|
|
2013-02-05 04:54:49 -08:00
|
|
|
double
|
|
|
|
BatteryManager::ChargingTime() const
|
2011-11-09 00:58:18 -08:00
|
|
|
{
|
|
|
|
if (!mCharging || mRemainingTime == kUnknownRemainingTime) {
|
2013-02-05 04:54:49 -08:00
|
|
|
return std::numeric_limits<double>::infinity();
|
2011-11-09 00:58:18 -08:00
|
|
|
}
|
|
|
|
|
2013-02-05 04:54:49 -08:00
|
|
|
return mRemainingTime;
|
2011-11-09 00:57:57 -08:00
|
|
|
}
|
|
|
|
|
2011-11-02 08:27:06 -07:00
|
|
|
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) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
|
2011-11-02 08:27:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (previousLevel != mLevel) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
|
2011-11-02 08:27:06 -07:00
|
|
|
}
|
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) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
|
|
|
|
: DISCHARGINGTIMECHANGE_EVENT_NAME);
|
2011-11-09 00:58:18 -08:00
|
|
|
}
|
|
|
|
if (mRemainingTime != kUnknownRemainingTime) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
|
|
|
|
: DISCHARGINGTIMECHANGE_EVENT_NAME);
|
2011-11-09 00:58:18 -08:00
|
|
|
}
|
|
|
|
} else if (previousRemainingTime != mRemainingTime) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(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-02 08:01:32 -07:00
|
|
|
} // namespace battery
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|