2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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
|
|
|
|
2015-08-10 23:20:37 +03:00
|
|
|
#include <cmath>
|
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"
|
2015-08-21 13:29:25 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "nsIDOMClassInfo.h"
|
2015-08-10 23:20:37 +03:00
|
|
|
#include "nsIDocument.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*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
BatteryManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2011-11-02 19:01:32 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return BatteryManagerBinding::Wrap(aCx, this, aGivenProto);
|
2011-11-02 19:01:32 +04:00
|
|
|
}
|
|
|
|
|
2015-08-21 13:29:25 +03:00
|
|
|
bool
|
|
|
|
BatteryManager::Charging() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// For testing, unable to report the battery status information
|
|
|
|
if (Preferences::GetBool("dom.battery.test.default", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Preferences::GetBool("dom.battery.test.charging", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Preferences::GetBool("dom.battery.test.discharging", false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCharging;
|
|
|
|
}
|
|
|
|
|
2013-02-05 16:54:49 +04:00
|
|
|
double
|
|
|
|
BatteryManager::DischargingTime() const
|
2011-11-09 12:57:57 +04:00
|
|
|
{
|
2015-08-21 13:29:25 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// For testing, unable to report the battery status information
|
|
|
|
if (Preferences::GetBool("dom.battery.test.default", false)) {
|
|
|
|
return std::numeric_limits<double>::infinity();
|
|
|
|
}
|
|
|
|
if (Preferences::GetBool("dom.battery.test.discharging", false)) {
|
|
|
|
return 42.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Charging() || 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
|
|
|
{
|
2015-08-21 13:29:25 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// For testing, unable to report the battery status information
|
|
|
|
if (Preferences::GetBool("dom.battery.test.default", false)) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
if (Preferences::GetBool("dom.battery.test.charging", false)) {
|
|
|
|
return 42.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Charging() || 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
|
|
|
}
|
|
|
|
|
2015-08-21 13:29:25 +03:00
|
|
|
double
|
|
|
|
BatteryManager::Level() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// For testing, unable to report the battery status information
|
|
|
|
if (Preferences::GetBool("dom.battery.test.default")) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLevel;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:27:06 +04:00
|
|
|
void
|
|
|
|
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
|
|
|
|
{
|
|
|
|
mLevel = aBatteryInfo.level();
|
2015-08-10 23:20:37 +03:00
|
|
|
|
|
|
|
// Round to the nearest ten percent for non-chrome and non-certified apps
|
|
|
|
nsIDocument* doc = GetOwner()->GetDoc();
|
|
|
|
uint16_t status = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
|
|
|
|
if (doc) {
|
|
|
|
doc->NodePrincipal()->GetAppStatus(&status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsContentUtils::IsChromeDoc(doc) &&
|
|
|
|
status != nsIPrincipal::APP_STATUS_CERTIFIED)
|
|
|
|
{
|
|
|
|
mLevel = lround(mLevel * 10.0) / 10.0;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:27:06 +04:00
|
|
|
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
|