Bug 699744 - Add dischargingTime attribute in BatteryManager. r=sicking
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 09 Nov 2011 09:57:57 +0100
changeset 80078 a6bdf740fa192b6dd283dc2b0e93332705e7c4dc
parent 80077 090456585da8b7f0ddb2d1a0672952917d147979
child 80079 42f490a7566a279fbfaf56818b7e436ec57a0a2a
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
bugs699744
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 699744 - Add dischargingTime attribute in BatteryManager. r=sicking
dom/battery/BatteryManager.cpp
dom/battery/BatteryManager.h
dom/battery/Constants.h
dom/battery/nsIDOMBatteryManager.idl
dom/battery/test/test_battery_basics.html
--- a/dom/battery/BatteryManager.cpp
+++ b/dom/battery/BatteryManager.cpp
@@ -30,61 +30,66 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#include <limits>
 #include "mozilla/Hal.h"
 #include "BatteryManager.h"
 #include "nsIDOMClassInfo.h"
 #include "Constants.h"
 #include "nsDOMEvent.h"
 #include "mozilla/Preferences.h"
 
 /**
  * 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 LEVELCHANGE_EVENT_NAME           NS_LITERAL_STRING("levelchange")
+#define CHARGINGCHANGE_EVENT_NAME        NS_LITERAL_STRING("chargingchange")
+#define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
 
 DOMCI_DATA(BatteryManager, mozilla::dom::battery::BatteryManager)
 
 namespace mozilla {
 namespace dom {
 namespace battery {
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager)
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BatteryManager,
                                                   nsDOMEventTargetHelper)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLevelChangeListener)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnChargingChangeListener)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnDischargingTimeChangeListener)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BatteryManager,
                                                 nsDOMEventTargetHelper)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLevelChangeListener)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnChargingChangeListener)
+  NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnDischargingTimeChangeListener)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BatteryManager)
   NS_INTERFACE_MAP_ENTRY(nsIDOMBatteryManager)
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BatteryManager)
 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)
 {
 }
 
 BatteryManager::~BatteryManager()
 {
   if (mListenerManager) {
     mListenerManager->Disconnect();
   }
@@ -121,16 +126,29 @@ NS_IMETHODIMP
 BatteryManager::GetLevel(double* aLevel)
 {
   *aLevel = mLevel;
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
+BatteryManager::GetDischargingTime(double* aDischargingTime)
+{
+  if (mDischargingTime == kUnknownRemainingTime) {
+    *aDischargingTime = std::numeric_limits<double>::infinity();
+    return NS_OK;
+  }
+
+  *aDischargingTime = mDischargingTime;
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 BatteryManager::GetOnlevelchange(nsIDOMEventListener** aOnlevelchange)
 {
   return GetInnerEventListener(mOnLevelChangeListener, aOnlevelchange);
 }
 
 NS_IMETHODIMP
 BatteryManager::SetOnlevelchange(nsIDOMEventListener* aOnlevelchange)
 {
@@ -146,16 +164,31 @@ BatteryManager::GetOnchargingchange(nsID
 
 NS_IMETHODIMP
 BatteryManager::SetOnchargingchange(nsIDOMEventListener* aOnchargingchange)
 {
   return RemoveAddEventListener(CHARGINGCHANGE_EVENT_NAME,
                                 mOnChargingChangeListener, aOnchargingchange);
 }
 
+NS_IMETHODIMP
+BatteryManager::GetOndischargingtimechange(nsIDOMEventListener** aOndischargingtimechange)
+{
+  return GetInnerEventListener(mOnDischargingTimeChangeListener,
+                               aOndischargingtimechange);
+}
+
+NS_IMETHODIMP
+BatteryManager::SetOndischargingtimechange(nsIDOMEventListener* aOndischargingtimechange)
+{
+  return RemoveAddEventListener(DISCHARGINGTIMECHANGE_EVENT_NAME,
+                                mOnDischargingTimeChangeListener,
+                                aOndischargingtimechange);
+}
+
 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);
@@ -175,26 +208,31 @@ BatteryManager::UpdateFromBatteryInfo(co
   mCharging = aBatteryInfo.charging();
 }
 
 void
 BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
 {
   double previousLevel = mLevel;
   bool previousCharging = mCharging;
+  double previousDischargingTime = mDischargingTime;
 
   UpdateFromBatteryInfo(aBatteryInfo);
 
   if (previousCharging != mCharging) {
     DispatchTrustedEventToSelf(CHARGINGCHANGE_EVENT_NAME);
   }
 
   if (previousLevel != mLevel) {
     DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
   }
+
+  if (previousDischargingTime != mDischargingTime) {
+    DispatchTrustedEventToSelf(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,18 +90,20 @@ 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;
 
   nsRefPtr<nsDOMEventListenerWrapper> mOnLevelChangeListener;
   nsRefPtr<nsDOMEventListenerWrapper> mOnChargingChangeListener;
+  nsRefPtr<nsDOMEventListenerWrapper> mOnDischargingTimeChangeListener;
 };
 
 } // namespace battery
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_battery_BatteryManager_h
--- a/dom/battery/Constants.h
+++ b/dom/battery/Constants.h
@@ -41,16 +41,17 @@
 /**
  * A set of constants that might need to be used by battery backends.
  * It's not part of BatteryManager.h to prevent those backends to include it.
  */
 namespace mozilla {
 namespace dom {
 namespace battery {
 
-  static const double kDefaultLevel    = 1.0;
-  static const bool   kDefaultCharging = true;
+  static const double kDefaultLevel         = 1.0;
+  static const bool   kDefaultCharging      = true;
+  static const double kUnknownRemainingTime = -1;
 
 } // namespace battery
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_battery_Constants_h__
--- a/dom/battery/nsIDOMBatteryManager.idl
+++ b/dom/battery/nsIDOMBatteryManager.idl
@@ -33,17 +33,19 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsIDOMEventTarget.idl"
 
 interface nsIDOMEventListener;
 
-[scriptable, function, uuid(67eb3507-a920-49a1-9ee6-5a32754c1d70)]
+[scriptable, function, uuid(78959ad6-0250-4180-bfbb-a9601513c601)]
 interface nsIDOMBatteryManager : nsIDOMEventTarget
 {
   readonly attribute double     level;
   readonly attribute boolean    charging;
+  readonly attribute double     dischargingTime;
 
   attribute nsIDOMEventListener onlevelchange;
   attribute nsIDOMEventListener onchargingchange;
+  attribute nsIDOMEventListener ondischargingtimechange;
 };
--- a/dom/battery/test/test_battery_basics.html
+++ b/dom/battery/test/test_battery_basics.html
@@ -14,13 +14,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");
 
 </script>
 </pre>
 </body>
 </html>