Index: device/battery/battery_status_manager_mac.cc |
diff --git a/content/browser/battery_status/battery_status_manager_mac.cc b/device/battery/battery_status_manager_mac.cc |
similarity index 74% |
rename from content/browser/battery_status/battery_status_manager_mac.cc |
rename to device/battery/battery_status_manager_mac.cc |
index 74ac272bb564680875edf2440195353e1ff12bf3..dc92c9cc9a93262312c51628dd823c67799d5f94 100644 |
--- a/content/browser/battery_status/battery_status_manager_mac.cc |
+++ b/device/battery/battery_status_manager_mac.cc |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "content/browser/battery_status/battery_status_manager.h" |
+#include "device/battery/battery_status_manager.h" |
#include <CoreFoundation/CoreFoundation.h> |
#include <IOKit/ps/IOPowerSources.h> |
@@ -14,10 +14,8 @@ |
#include "base/memory/ref_counted.h" |
#include "base/metrics/histogram.h" |
#include "base/time/time.h" |
-#include "content/public/browser/browser_thread.h" |
-#include "third_party/WebKit/public/platform/WebBatteryStatus.h" |
-namespace content { |
+namespace device { |
namespace { |
@@ -59,8 +57,7 @@ void UpdateNumberBatteriesHistogram(int count) { |
"BatteryStatus.NumberBatteriesMac", count, 1, 5, 6); |
} |
-void FetchBatteryStatus(CFDictionaryRef description, |
- blink::WebBatteryStatus& status) { |
+void FetchBatteryStatus(CFDictionaryRef description, BatteryStatus* status) { |
CFStringRef current_state = |
base::mac::GetValueFromDictionary<CFStringRef>(description, |
CFSTR(kIOPSPowerSourceStateKey)); |
@@ -72,7 +69,7 @@ void FetchBatteryStatus(CFDictionaryRef description, |
bool is_charged = |
GetValueAsBoolean(description, CFSTR(kIOPSIsChargedKey), false); |
- status.charging = !on_battery_power || is_charging; |
+ status->charging = !on_battery_power || is_charging; |
SInt64 current_capacity = |
GetValueAsSInt64(description, CFSTR(kIOPSCurrentCapacityKey), -1); |
@@ -83,7 +80,7 @@ void FetchBatteryStatus(CFDictionaryRef description, |
// which is 1. |
if (current_capacity != -1 && max_capacity != -1 && |
current_capacity <= max_capacity && max_capacity != 0) { |
- status.level = current_capacity / static_cast<double>(max_capacity); |
+ status->level = current_capacity / static_cast<double>(max_capacity); |
} |
if (is_charging) { |
@@ -92,7 +89,7 @@ void FetchBatteryStatus(CFDictionaryRef description, |
// Battery is charging: set the charging time if it's available, otherwise |
// set to +infinity. |
- status.chargingTime = charging_time != -1 |
+ status->charging_time = charging_time != -1 |
? base::TimeDelta::FromMinutes(charging_time).InSeconds() |
: std::numeric_limits<double>::infinity(); |
} else { |
@@ -100,7 +97,7 @@ void FetchBatteryStatus(CFDictionaryRef description, |
// Set chargingTime to +infinity if the battery is not charged. Otherwise |
// leave the default value, which is 0. |
if (!is_charged) |
- status.chargingTime = std::numeric_limits<double>::infinity(); |
+ status->charging_time = std::numeric_limits<double>::infinity(); |
// Set dischargingTime if it's available and valid, i.e. when on battery |
// power. Otherwise leave the default value, which is +infinity. |
@@ -108,15 +105,15 @@ void FetchBatteryStatus(CFDictionaryRef description, |
SInt64 discharging_time = |
GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1); |
if (discharging_time != -1) { |
- status.dischargingTime = |
+ status->discharging_time = |
base::TimeDelta::FromMinutes(discharging_time).InSeconds(); |
} |
} |
} |
} |
-std::vector<blink::WebBatteryStatus> GetInternalBatteriesStates() { |
- std::vector<blink::WebBatteryStatus> internal_sources; |
+std::vector<BatteryStatus> GetInternalBatteriesStates() { |
+ std::vector<BatteryStatus> internal_sources; |
base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); |
base::ScopedCFTypeRef<CFArrayRef> power_sources_list( |
@@ -140,8 +137,8 @@ std::vector<blink::WebBatteryStatus> GetInternalBatteriesStates() { |
GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); |
if (internal_source && source_present) { |
- blink::WebBatteryStatus status; |
- FetchBatteryStatus(description, status); |
+ BatteryStatus status; |
+ FetchBatteryStatus(description, &status); |
internal_sources.push_back(status); |
} |
} |
@@ -152,10 +149,10 @@ std::vector<blink::WebBatteryStatus> GetInternalBatteriesStates() { |
void OnBatteryStatusChanged(const BatteryCallback& callback) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- std::vector<blink::WebBatteryStatus> batteries(GetInternalBatteriesStates()); |
+ std::vector<BatteryStatus> batteries(GetInternalBatteriesStates()); |
if (batteries.empty()) { |
- callback.Run(blink::WebBatteryStatus()); |
+ callback.Run(BatteryStatus()); |
return; |
} |
@@ -173,58 +170,26 @@ class BatteryStatusObserver |
: callback_(callback) {} |
void Start() { |
- // Need to start on a thread with UI-type message loop for |
- // |notifier_run_loop_| to receive callbacks. |
- if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
- StartOnUI(); |
- } else { |
- BrowserThread::PostTask( |
- BrowserThread::UI, |
- FROM_HERE, |
- base::Bind(&BatteryStatusObserver::StartOnUI, this)); |
- } |
- } |
- |
- void Stop() { |
- if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
- StopOnUI(); |
- } else { |
- BrowserThread::PostTask( |
- BrowserThread::UI, |
- FROM_HERE, |
- base::Bind(&BatteryStatusObserver::StopOnUI, this)); |
- } |
- } |
- |
- private: |
- friend class base::RefCountedThreadSafe<BatteryStatusObserver>; |
- virtual ~BatteryStatusObserver() { DCHECK(!notifier_run_loop_source_); } |
- |
- void StartOnUI() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- |
if (notifier_run_loop_source_) |
return; |
notifier_run_loop_source_.reset( |
- IOPSNotificationCreateRunLoopSource(OnBatteryStatusChangedUI, |
+ IOPSNotificationCreateRunLoopSource(CallOnBatteryStatusChanged, |
static_cast<void*>(&callback_))); |
if (!notifier_run_loop_source_) { |
LOG(ERROR) << "Failed to create battery status notification run loop"; |
// Make sure to execute to callback with the default values. |
- callback_.Run(blink::WebBatteryStatus()); |
+ callback_.Run(BatteryStatus()); |
return; |
} |
- OnBatteryStatusChangedUI(static_cast<void*>(&callback_)); |
+ CallOnBatteryStatusChanged(static_cast<void*>(&callback_)); |
CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, |
kCFRunLoopDefaultMode); |
UpdateNumberBatteriesHistogram(GetInternalBatteriesStates().size()); |
} |
- void StopOnUI() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- |
+ void Stop() { |
if (!notifier_run_loop_source_) |
return; |
@@ -233,14 +198,12 @@ class BatteryStatusObserver |
notifier_run_loop_source_.reset(); |
} |
- static void OnBatteryStatusChangedUI(void* callback) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- // Offload fetching of values and callback execution to the IO thread. |
- BrowserThread::PostTask( |
- BrowserThread::IO, |
- FROM_HERE, |
- base::Bind(&OnBatteryStatusChanged, |
- *static_cast<BatteryCallback*>(callback))); |
+ private: |
+ friend class base::RefCountedThreadSafe<BatteryStatusObserver>; |
+ virtual ~BatteryStatusObserver() { DCHECK(!notifier_run_loop_source_); } |
+ |
+ static void CallOnBatteryStatusChanged(void* callback) { |
+ OnBatteryStatusChanged(*static_cast<BatteryCallback*>(callback)); |
} |
BatteryCallback callback_; |
@@ -260,13 +223,11 @@ class BatteryStatusManagerMac : public BatteryStatusManager { |
// BatteryStatusManager: |
virtual bool StartListeningBatteryChange() OVERRIDE { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
notifier_->Start(); |
return true; |
} |
virtual void StopListeningBatteryChange() OVERRIDE { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
notifier_->Stop(); |
} |
@@ -285,4 +246,4 @@ scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
new BatteryStatusManagerMac(callback)); |
} |
-} // namespace content |
+} // namespace device |