2011-11-02 19:01:32 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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 19:01:32 +04:00
|
|
|
|
2011-11-09 12:57:57 +04:00
|
|
|
#include <limits>
|
2011-11-02 19:01:32 +04:00
|
|
|
#include "BatteryManager.h"
|
|
|
|
#include "Constants.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "mozilla/Hal.h"
|
2013-02-05 16:54:49 +04:00
|
|
|
#include "mozilla/dom/BatteryManagerBinding.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "nsIDOMClassInfo.h"
|
2011-11-02 19:27:06 +04: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 12:57:57 +04: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 12:58:18 +04:00
|
|
|
#define CHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("chargingtimechange")
|
2011-11-02 19:01:32 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace battery {
|
|
|
|
|
2014-01-07 06:53:23 +04:00
|
|
|
BatteryManager::BatteryManager(nsPIDOMWindow* aWindow)
|
2014-04-01 10:13:50 +04:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2014-01-07 06:53:23 +04:00
|
|
|
, mLevel(kDefaultLevel)
|
2011-11-02 19:01:32 +04:00
|
|
|
, mCharging(kDefaultCharging)
|
2011-12-01 14:49:42 +04:00
|
|
|
, mRemainingTime(kDefaultRemainingTime)
|
2011-11-02 19:01:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:27:06 +04:00
|
|
|
void
|
2014-01-07 06:53:23 +04:00
|
|
|
BatteryManager::Init()
|
2011-11-02 19:27:06 +04:00
|
|
|
{
|
|
|
|
hal::RegisterBatteryObserver(this);
|
|
|
|
|
2011-12-14 14:40:29 +04:00
|
|
|
hal::BatteryInformation batteryInfo;
|
|
|
|
hal::GetCurrentBatteryInformation(&batteryInfo);
|
2011-11-02 19:27:06 +04:00
|
|
|
|
2011-12-14 14:40:29 +04:00
|
|
|
UpdateFromBatteryInfo(batteryInfo);
|
2011-11-02 19:27:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BatteryManager::Shutdown()
|
|
|
|
{
|
|
|
|
hal::UnregisterBatteryObserver(this);
|
|
|
|
}
|
|
|
|
|
2013-02-05 16:54:49 +04:00
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
BatteryManager::WrapObject(JSContext* aCx)
|
2011-11-02 19:01:32 +04: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-09 02:27:17 +04:00
|
|
|
return BatteryManagerBinding::Wrap(aCx, this);
|
2011-11-02 19:01:32 +04:00
|
|
|
}
|
|
|
|
|
2013-02-05 16:54:49 +04:00
|
|
|
double
|
|
|
|
BatteryManager::DischargingTime() const
|
2011-11-09 12:57:57 +04:00
|
|
|
{
|
2011-11-09 12:58:18 +04:00
|
|
|
if (mCharging || mRemainingTime == kUnknownRemainingTime) {
|
2013-02-05 16:54:49 +04:00
|
|
|
return std::numeric_limits<double>::infinity();
|
2011-11-09 12:57:57 +04:00
|
|
|
}
|
|
|
|
|
2013-02-05 16:54:49 +04:00
|
|
|
return mRemainingTime;
|
2011-11-09 12:58:18 +04:00
|
|
|
}
|
|
|
|
|
2013-02-05 16:54:49 +04:00
|
|
|
double
|
|
|
|
BatteryManager::ChargingTime() const
|
2011-11-09 12:58:18 +04:00
|
|
|
{
|
|
|
|
if (!mCharging || mRemainingTime == kUnknownRemainingTime) {
|
2013-02-05 16:54:49 +04:00
|
|
|
return std::numeric_limits<double>::infinity();
|
2011-11-09 12:58:18 +04:00
|
|
|
}
|
|
|
|
|
2013-02-05 16:54:49 +04:00
|
|
|
return mRemainingTime;
|
2011-11-09 12:57:57 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 19:27:06 +04:00
|
|
|
void
|
|
|
|
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
|
|
|
|
{
|
|
|
|
mLevel = aBatteryInfo.level();
|
|
|
|
mCharging = aBatteryInfo.charging();
|
2011-11-09 12:58:59 +04:00
|
|
|
mRemainingTime = aBatteryInfo.remainingTime();
|
2011-12-01 14:49:42 +04: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 19:27:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
|
|
|
|
{
|
2011-11-09 12:56:52 +04:00
|
|
|
double previousLevel = mLevel;
|
2011-11-02 19:27:06 +04:00
|
|
|
bool previousCharging = mCharging;
|
2011-11-09 12:58:18 +04:00
|
|
|
double previousRemainingTime = mRemainingTime;
|
2011-11-02 19:27:06 +04:00
|
|
|
|
|
|
|
UpdateFromBatteryInfo(aBatteryInfo);
|
|
|
|
|
|
|
|
if (previousCharging != mCharging) {
|
2012-09-28 00:11:31 +04:00
|
|
|
DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
|
2011-11-02 19:27:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (previousLevel != mLevel) {
|
2012-09-28 00:11:31 +04:00
|
|
|
DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
|
2011-11-02 19:27:06 +04:00
|
|
|
}
|
2011-11-09 12:57:57 +04:00
|
|
|
|
2011-11-09 12:58:18 +04: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-28 00:11:31 +04:00
|
|
|
DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
|
|
|
|
: DISCHARGINGTIMECHANGE_EVENT_NAME);
|
2011-11-09 12:58:18 +04:00
|
|
|
}
|
|
|
|
if (mRemainingTime != kUnknownRemainingTime) {
|
2012-09-28 00:11:31 +04:00
|
|
|
DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
|
|
|
|
: DISCHARGINGTIMECHANGE_EVENT_NAME);
|
2011-11-09 12:58:18 +04:00
|
|
|
}
|
|
|
|
} else if (previousRemainingTime != mRemainingTime) {
|
2012-09-28 00:11:31 +04:00
|
|
|
DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
|
|
|
|
: DISCHARGINGTIMECHANGE_EVENT_NAME);
|
2011-11-09 12:57:57 +04:00
|
|
|
}
|
2011-11-02 19:27:06 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 19:01:32 +04:00
|
|
|
} // namespace battery
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|