Bug 699743 - Add chargingTime attribute in BatteryManager. r=sicking
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 09 Nov 2011 09:58:18 +0100
changeset 80079 42f490a7566a279fbfaf56818b7e436ec57a0a2a
parent 80078 a6bdf740fa192b6dd283dc2b0e93332705e7c4dc
child 80080 6918831fdd2f96d8a687f87fa98ddd5a7578d2ac
push id21462
push usermak77@bonardo.net
push dateThu, 10 Nov 2011 10:40:54 +0000
treeherdermozilla-central@9ce43912891b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs699743
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 699743 - Add chargingTime attribute in BatteryManager. r=sicking
dom/battery/BatteryManager.cpp
dom/battery/BatteryManager.h
dom/battery/nsIDOMBatteryManager.idl
dom/battery/test/test_battery_basics.html
--- a/dom/battery/BatteryManager.cpp
+++ b/dom/battery/BatteryManager.cpp
@@ -45,16 +45,17 @@
 
 /**
  * We have to use macros here because our leak analysis tool things we are
  * leaking strings when we have |static const nsString|. Sad :(
  */
 #define LEVELCHANGE_EVENT_NAME           NS_LITERAL_STRING("levelchange")
 #define CHARGINGCHANGE_EVENT_NAME        NS_LITERAL_STRING("chargingchange")
 #define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
+#define CHARGINGTIMECHANGE_EVENT_NAME    NS_LITERAL_STRING("chargingtimechange")
 
 DOMCI_DATA(BatteryManager, mozilla::dom::battery::BatteryManager)
 
 namespace mozilla {
 namespace dom {
 namespace battery {
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager)
@@ -79,17 +80,17 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper)
 
 BatteryManager::BatteryManager()
   : mLevel(kDefaultLevel)
   , mCharging(kDefaultCharging)
-  , mDischargingTime(kUnknownRemainingTime)
+  , mRemainingTime(kUnknownRemainingTime)
 {
 }
 
 BatteryManager::~BatteryManager()
 {
   if (mListenerManager) {
     mListenerManager->Disconnect();
   }
@@ -128,22 +129,35 @@ BatteryManager::GetLevel(double* aLevel)
   *aLevel = mLevel;
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BatteryManager::GetDischargingTime(double* aDischargingTime)
 {
-  if (mDischargingTime == kUnknownRemainingTime) {
+  if (mCharging || mRemainingTime == kUnknownRemainingTime) {
     *aDischargingTime = std::numeric_limits<double>::infinity();
     return NS_OK;
   }
 
-  *aDischargingTime = mDischargingTime;
+  *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;
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BatteryManager::GetOnlevelchange(nsIDOMEventListener** aOnlevelchange)
 {
   return GetInnerEventListener(mOnLevelChangeListener, aOnlevelchange);
@@ -179,16 +193,31 @@ BatteryManager::GetOndischargingtimechan
 NS_IMETHODIMP
 BatteryManager::SetOndischargingtimechange(nsIDOMEventListener* aOndischargingtimechange)
 {
   return RemoveAddEventListener(DISCHARGINGTIMECHANGE_EVENT_NAME,
                                 mOnDischargingTimeChangeListener,
                                 aOndischargingtimechange);
 }
 
+NS_IMETHODIMP
+BatteryManager::GetOnchargingtimechange(nsIDOMEventListener** aOnchargingtimechange)
+{
+  return GetInnerEventListener(mOnChargingTimeChangeListener,
+                               aOnchargingtimechange);
+}
+
+NS_IMETHODIMP
+BatteryManager::SetOnchargingtimechange(nsIDOMEventListener* aOnchargingtimechange)
+{
+  return RemoveAddEventListener(CHARGINGTIMECHANGE_EVENT_NAME,
+                                mOnChargingTimeChangeListener,
+                                aOnchargingtimechange);
+}
+
 nsresult
 BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)
 {
   nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
   nsresult rv = event->InitEvent(aEventName, false, false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = event->SetTrusted(PR_TRUE);
@@ -208,30 +237,49 @@ BatteryManager::UpdateFromBatteryInfo(co
   mCharging = aBatteryInfo.charging();
 }
 
 void
 BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
 {
   double previousLevel = mLevel;
   bool previousCharging = mCharging;
-  double previousDischargingTime = mDischargingTime;
+  double previousRemainingTime = mRemainingTime;
 
   UpdateFromBatteryInfo(aBatteryInfo);
 
   if (previousCharging != mCharging) {
     DispatchTrustedEventToSelf(CHARGINGCHANGE_EVENT_NAME);
   }
 
   if (previousLevel != mLevel) {
     DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
   }
 
-  if (previousDischargingTime != mDischargingTime) {
-    DispatchTrustedEventToSelf(DISCHARGINGTIMECHANGE_EVENT_NAME);
+  /*
+   * 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);
   }
 }
 
 /* static */ bool
 BatteryManager::HasSupport()
 {
   return Preferences::GetBool("dom.battery.enabled", true);
 }
--- a/dom/battery/BatteryManager.h
+++ b/dom/battery/BatteryManager.h
@@ -90,20 +90,25 @@ private:
   /**
    * Update the battery information stored in the battery manager object using
    * a battery information object.
    */
   void UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo);
 
   double mLevel;
   bool   mCharging;
-  double mDischargingTime;
+  /**
+   * Represents the discharging time or the charging time, dpending on the
+   * current battery status (charging or not).
+   */
+  double mRemainingTime;
 
   nsRefPtr<nsDOMEventListenerWrapper> mOnLevelChangeListener;
   nsRefPtr<nsDOMEventListenerWrapper> mOnChargingChangeListener;
   nsRefPtr<nsDOMEventListenerWrapper> mOnDischargingTimeChangeListener;
+  nsRefPtr<nsDOMEventListenerWrapper> mOnChargingTimeChangeListener;
 };
 
 } // namespace battery
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_battery_BatteryManager_h
--- a/dom/battery/nsIDOMBatteryManager.idl
+++ b/dom/battery/nsIDOMBatteryManager.idl
@@ -33,19 +33,21 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsIDOMEventTarget.idl"
 
 interface nsIDOMEventListener;
 
-[scriptable, function, uuid(78959ad6-0250-4180-bfbb-a9601513c601)]
+[scriptable, function, uuid(6dcb803b-e968-4c02-88f5-049a3f2a2efb)]
 interface nsIDOMBatteryManager : nsIDOMEventTarget
 {
   readonly attribute double     level;
   readonly attribute boolean    charging;
   readonly attribute double     dischargingTime;
+  readonly attribute double     chargingTime;
 
   attribute nsIDOMEventListener onlevelchange;
   attribute nsIDOMEventListener onchargingchange;
   attribute nsIDOMEventListener ondischargingtimechange;
+  attribute nsIDOMEventListener onchargingtimechange;
 };
--- a/dom/battery/test/test_battery_basics.html
+++ b/dom/battery/test/test_battery_basics.html
@@ -15,13 +15,14 @@
 /** Test for Battery API **/
 
 ok('mozBattery' in navigator, "navigator.mozBattery should exist");
 
 var battery = navigator.mozBattery;
 is(battery.level, 1.0, "Default battery level should be 1.0");
 is(battery.charging, true, "Default charging value should be true");
 is(battery.dischargingTime, Infinity, "Default dischargingTime should be Inifinity");
+is(battery.chargingTime, Infinity, "Default chargingTime should be Inifinity");
 
 </script>
 </pre>
 </body>
 </html>