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
|
|
|
|
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*
|
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
|
|
|
}
|
|
|
|
|
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
|