Bug 793418 - OS X Battery Backend: Report time to charge when available. r=mounir
authorReuben Morais <reuben.morais@gmail.com>
Tue, 25 Sep 2012 15:32:02 +0100
changeset 114886 d40d6c7a82fe751d1b999be80dcd396d42ce7934
parent 114885 4af7c8a2159f184b1674c97410ed2065a902e6d4
child 114887 a49366805f7d341fb1d139c31d6af434a6886123
push id239
push userakeybl@mozilla.com
push dateThu, 03 Jan 2013 21:54:43 +0000
treeherdermozilla-release@3a7b66445659 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmounir
bugs793418
milestone18.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 793418 - OS X Battery Backend: Report time to charge when available. r=mounir
hal/cocoa/CocoaBattery.cpp
--- a/hal/cocoa/CocoaBattery.cpp
+++ b/hal/cocoa/CocoaBattery.cpp
@@ -230,26 +230,16 @@ MacPowerInformationService::HandleChange
   // Usually there's only 1 available, depending on current power source.
   for (CFIndex i = 0; i < ::CFArrayGetCount(list); ++i) {
     CFTypeRef source = ::CFArrayGetValueAtIndex(list, i);
     CFDictionaryRef currPowerSourceDesc = ::IOPSGetPowerSourceDescription(data, source);
     if (!currPowerSourceDesc) {
       continue;
     }
 
-    if (sIOPSGetTimeRemainingEstimate) {
-      // See if we can get a time estimate.
-      CFTimeInterval estimate = sIOPSGetTimeRemainingEstimate();
-      if (estimate == kIOPSTimeRemainingUnlimited || estimate == kIOPSTimeRemainingUnknown) {
-        remainingTime = kUnknownRemainingTime;
-      } else {
-        remainingTime = estimate;
-      }
-    }
-
     // Get a battery level estimate. This key is required.
     int currentCapacity = 0;
     const void* cfRef = ::CFDictionaryGetValue(currPowerSourceDesc, CFSTR(kIOPSCurrentCapacityKey));
     ::CFNumberGetValue((CFNumberRef)cfRef, kCFNumberSInt32Type, &currentCapacity);
 
     // This key is also required.
     int maxCapacity = 0;
     cfRef = ::CFDictionaryGetValue(currPowerSourceDesc, CFSTR(kIOPSMaxCapacityKey));
@@ -259,16 +249,41 @@ MacPowerInformationService::HandleChange
       level = static_cast<double>(currentCapacity)/static_cast<double>(maxCapacity);
     }
 
     // Find out if we're charging.
     // This key is optional, we fallback to kDefaultCharging if the current power
     // source doesn't have that info.
     if(::CFDictionaryGetValueIfPresent(currPowerSourceDesc, CFSTR(kIOPSIsChargingKey), &cfRef)) {
       charging = ::CFBooleanGetValue((CFBooleanRef)cfRef);
+
+      // Get an estimate of how long it's going to take until we're fully charged.
+      // This key is optional.
+      if (charging) {
+        // Default value that will be changed if we happen to find the actual
+        // remaining time.
+        remainingTime = level == 1.0 ? kDefaultRemainingTime : kUnknownRemainingTime;
+
+        if (::CFDictionaryGetValueIfPresent(currPowerSourceDesc,
+                CFSTR(kIOPSTimeToFullChargeKey), &cfRef)) {
+          int timeToCharge;
+          ::CFNumberGetValue((CFNumberRef)cfRef, kCFNumberIntType, &timeToCharge);
+          if (timeToCharge != kIOPSTimeRemainingUnknown) {
+            remainingTime = timeToCharge*60;
+          }
+        }
+      } else if (sIOPSGetTimeRemainingEstimate) { // not charging
+        // See if we can get a time estimate.
+        CFTimeInterval estimate = sIOPSGetTimeRemainingEstimate();
+        if (estimate == kIOPSTimeRemainingUnlimited || estimate == kIOPSTimeRemainingUnknown) {
+          remainingTime = kUnknownRemainingTime;
+        } else {
+          remainingTime = estimate;
+        }
+      }
     }
 
     break;
   }
 
   bool isNewData = level != power->mLevel || charging != power->mCharging ||
                    remainingTime != power->mRemainingTime;