diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-01-04 14:17:57 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-01-05 10:05:06 +0000 |
commit | 39d357e3248f80abea0159765ff39554affb40db (patch) | |
tree | aba0e6bfb76de0244bba0f5fdbd64b830dd6e621 /chromium/base/power_monitor | |
parent | 87778abf5a1f89266f37d1321b92a21851d8244d (diff) |
BASELINE: Update Chromium to 55.0.2883.105
And updates ninja to 1.7.2
Change-Id: I20d43c737f82764d857ada9a55586901b18b9243
Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
Diffstat (limited to 'chromium/base/power_monitor')
9 files changed, 96 insertions, 59 deletions
diff --git a/chromium/base/power_monitor/power_monitor_device_source.cc b/chromium/base/power_monitor/power_monitor_device_source.cc index f54b7ba3c83..d7060c2a228 100644 --- a/chromium/base/power_monitor/power_monitor_device_source.cc +++ b/chromium/base/power_monitor/power_monitor_device_source.cc @@ -4,27 +4,23 @@ #include "base/power_monitor/power_monitor_device_source.h" -#include "base/time/time.h" -#include "build/build_config.h" +#include "base/threading/thread_task_runner_handle.h" namespace base { -#if defined(ENABLE_BATTERY_MONITORING) -// The amount of time (in ms) to wait before running the initial -// battery check. -static int kDelayedBatteryCheckMs = 10 * 1000; -#endif // defined(ENABLE_BATTERY_MONITORING) - PowerMonitorDeviceSource::PowerMonitorDeviceSource() { - DCHECK(MessageLoop::current()); -#if defined(ENABLE_BATTERY_MONITORING) - delayed_battery_check_.Start(FROM_HERE, - base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, - &PowerMonitorDeviceSource::BatteryCheck); -#endif // defined(ENABLE_BATTERY_MONITORING) + DCHECK(ThreadTaskRunnerHandle::IsSet()); + #if defined(OS_MACOSX) PlatformInit(); #endif + +#if defined(OS_WIN) || defined(OS_MACOSX) + // Provide the correct battery status if possible. Others platforms, such as + // Android and ChromeOS, will update their status once their backends are + // actually initialized. + SetInitialOnBatteryPowerState(IsOnBatteryPowerImpl()); +#endif } PowerMonitorDeviceSource::~PowerMonitorDeviceSource() { @@ -33,8 +29,4 @@ PowerMonitorDeviceSource::~PowerMonitorDeviceSource() { #endif } -void PowerMonitorDeviceSource::BatteryCheck() { - ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); -} - } // namespace base diff --git a/chromium/base/power_monitor/power_monitor_device_source.h b/chromium/base/power_monitor/power_monitor_device_source.h index 2dabac8865e..69237cd6434 100644 --- a/chromium/base/power_monitor/power_monitor_device_source.h +++ b/chromium/base/power_monitor/power_monitor_device_source.h @@ -15,18 +15,8 @@ #if defined(OS_WIN) #include <windows.h> - -// Windows HiRes timers drain the battery faster so we need to know the battery -// status. This isn't true for other platforms. -#define ENABLE_BATTERY_MONITORING 1 -#else -#undef ENABLE_BATTERY_MONITORING #endif // !OS_WIN -#if defined(ENABLE_BATTERY_MONITORING) -#include "base/timer/timer.h" -#endif // defined(ENABLE_BATTERY_MONITORING) - #if defined(OS_IOS) #include <objc/runtime.h> #endif // OS_IOS @@ -93,19 +83,11 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource { // false otherwise. bool IsOnBatteryPowerImpl() override; - // Checks the battery status and notifies observers if the battery - // status has changed. - void BatteryCheck(); - #if defined(OS_IOS) // Holds pointers to system event notification observers. std::vector<id> notification_observers_; #endif -#if defined(ENABLE_BATTERY_MONITORING) - base::OneShotTimer delayed_battery_check_; -#endif - #if defined(OS_WIN) PowerMessageWindow power_message_window_; #endif diff --git a/chromium/base/power_monitor/power_monitor_device_source_android.cc b/chromium/base/power_monitor/power_monitor_device_source_android.cc index 9671c30761e..fd5635c1913 100644 --- a/chromium/base/power_monitor/power_monitor_device_source_android.cc +++ b/chromium/base/power_monitor/power_monitor_device_source_android.cc @@ -18,18 +18,16 @@ void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) { namespace android { -// Native implementation of PowerMonitor.java. +// Native implementation of PowerMonitor.java. Note: This will be invoked by +// PowerMonitor.java shortly after startup to set the correct initial value for +// "is on battery power." void OnBatteryChargingChanged(JNIEnv* env, const JavaParamRef<jclass>& clazz) { ProcessPowerEventHelper(PowerMonitorSource::POWER_STATE_EVENT); } -void OnMainActivityResumed(JNIEnv* env, const JavaParamRef<jclass>& clazz) { - ProcessPowerEventHelper(PowerMonitorSource::RESUME_EVENT); -} - -void OnMainActivitySuspended(JNIEnv* env, const JavaParamRef<jclass>& clazz) { - ProcessPowerEventHelper(PowerMonitorSource::SUSPEND_EVENT); -} +// Note: Android does not have the concept of suspend / resume as it's known by +// other platforms. Thus we do not send Suspend/Resume notifications. See +// http://crbug.com/644515 } // namespace android diff --git a/chromium/base/power_monitor/power_monitor_device_source_ios.mm b/chromium/base/power_monitor/power_monitor_device_source_ios.mm index dc12f1c2952..3e86b2e246c 100644 --- a/chromium/base/power_monitor/power_monitor_device_source_ios.mm +++ b/chromium/base/power_monitor/power_monitor_device_source_ios.mm @@ -8,6 +8,11 @@ namespace base { +bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() { + NOTIMPLEMENTED(); + return false; +} + void PowerMonitorDeviceSource::PlatformInit() { NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; id foreground = diff --git a/chromium/base/power_monitor/power_monitor_device_source_mac.mm b/chromium/base/power_monitor/power_monitor_device_source_mac.mm index 8c48117e220..be2b8b94743 100644 --- a/chromium/base/power_monitor/power_monitor_device_source_mac.mm +++ b/chromium/base/power_monitor/power_monitor_device_source_mac.mm @@ -7,10 +7,14 @@ #include "base/power_monitor/power_monitor_device_source.h" +#include "base/mac/foundation_util.h" +#include "base/mac/scoped_cftyperef.h" #include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor_source.h" #include <IOKit/IOMessage.h> +#include <IOKit/ps/IOPSKeys.h> +#include <IOKit/ps/IOPowerSources.h> #include <IOKit/pwr_mgt/IOPMLib.h> namespace base { @@ -19,11 +23,44 @@ void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) { PowerMonitorSource::ProcessPowerEvent(event); } +bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() { + base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); + base::ScopedCFTypeRef<CFArrayRef> power_sources_list( + IOPSCopyPowerSourcesList(info)); + + const CFIndex count = CFArrayGetCount(power_sources_list); + for (CFIndex i = 0; i < count; ++i) { + const CFDictionaryRef description = IOPSGetPowerSourceDescription( + info, CFArrayGetValueAtIndex(power_sources_list, i)); + if (!description) + continue; + + CFStringRef current_state = base::mac::GetValueFromDictionary<CFStringRef>( + description, CFSTR(kIOPSPowerSourceStateKey)); + + if (!current_state) + continue; + + // We only report "on battery power" if no source is on AC power. + if (CFStringCompare(current_state, CFSTR(kIOPSBatteryPowerValue), 0) != + kCFCompareEqualTo) { + return false; + } + } + + return true; +} + namespace { io_connect_t g_system_power_io_port = 0; IONotificationPortRef g_notification_port_ref = 0; io_object_t g_notifier_object = 0; +CFRunLoopSourceRef g_battery_status_ref = 0; + +void BatteryEventCallback(void*) { + ProcessPowerEventHelper(PowerMonitorSource::POWER_STATE_EVENT); +} void SystemPowerEventCallback(void*, io_service_t service, @@ -73,11 +110,16 @@ void PowerMonitorDeviceSource::PlatformInit() { if (g_system_power_io_port == 0) return; - // Add the notification port to the application runloop - CFRunLoopAddSource( - CFRunLoopGetCurrent(), - IONotificationPortGetRunLoopSource(g_notification_port_ref), - kCFRunLoopCommonModes); + // Add the notification port and battery monitor to the application runloop + CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource( + g_notification_port_ref), + kCFRunLoopCommonModes); + + base::ScopedCFTypeRef<CFRunLoopSourceRef> battery_status_ref( + IOPSNotificationCreateRunLoopSource(BatteryEventCallback, nullptr)); + CFRunLoopAddSource(CFRunLoopGetCurrent(), battery_status_ref, + kCFRunLoopDefaultMode); + g_battery_status_ref = battery_status_ref.release(); } void PowerMonitorDeviceSource::PlatformDestroy() { @@ -91,6 +133,12 @@ void PowerMonitorDeviceSource::PlatformDestroy() { IONotificationPortGetRunLoopSource(g_notification_port_ref), kCFRunLoopCommonModes); + base::ScopedCFTypeRef<CFRunLoopSourceRef> battery_status_ref( + g_battery_status_ref); + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), g_battery_status_ref, + kCFRunLoopDefaultMode); + g_battery_status_ref = 0; + // Deregister for system sleep notifications IODeregisterForSystemPower(&g_notifier_object); diff --git a/chromium/base/power_monitor/power_monitor_device_source_win.cc b/chromium/base/power_monitor/power_monitor_device_source_win.cc index b8b16e1d344..e74be50e2a2 100644 --- a/chromium/base/power_monitor/power_monitor_device_source_win.cc +++ b/chromium/base/power_monitor/power_monitor_device_source_win.cc @@ -2,8 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor_device_source.h" + +#include "base/message_loop/message_loop.h" +#include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor_source.h" #include "base/win/wrapped_window_proc.h" diff --git a/chromium/base/power_monitor/power_monitor_source.cc b/chromium/base/power_monitor/power_monitor_source.cc index 6868cb19e33..5d27a0e5a07 100644 --- a/chromium/base/power_monitor/power_monitor_source.cc +++ b/chromium/base/power_monitor/power_monitor_source.cc @@ -5,16 +5,12 @@ #include "base/power_monitor/power_monitor_source.h" #include "base/power_monitor/power_monitor.h" +#include "build/build_config.h" namespace base { -PowerMonitorSource::PowerMonitorSource() - : on_battery_power_(false), - suspended_(false) { -} - -PowerMonitorSource::~PowerMonitorSource() { -} +PowerMonitorSource::PowerMonitorSource() {} +PowerMonitorSource::~PowerMonitorSource() {} bool PowerMonitorSource::IsOnBatteryPower() { AutoLock auto_lock(battery_lock_); @@ -63,4 +59,11 @@ void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { } } +void PowerMonitorSource::SetInitialOnBatteryPowerState(bool on_battery_power) { + // Must only be called before a monitor exists, otherwise the caller should + // have just used a normal ProcessPowerEvent(POWER_STATE_EVENT) call. + DCHECK(!PowerMonitor::Get()); + on_battery_power_ = on_battery_power; +} + } // namespace base diff --git a/chromium/base/power_monitor/power_monitor_source.h b/chromium/base/power_monitor/power_monitor_source.h index e63f4f82bf9..b69cbf8317d 100644 --- a/chromium/base/power_monitor/power_monitor_source.h +++ b/chromium/base/power_monitor/power_monitor_source.h @@ -49,9 +49,14 @@ class BASE_EXPORT PowerMonitorSource { // false otherwise. virtual bool IsOnBatteryPowerImpl() = 0; + // Sets the initial state for |on_battery_power_|, which defaults to false + // since not all implementations can provide the value at construction. May + // only be called before a base::PowerMonitor has been created. + void SetInitialOnBatteryPowerState(bool on_battery_power); + private: - bool on_battery_power_; - bool suspended_; + bool on_battery_power_ = false; + bool suspended_ = false; // This lock guards access to on_battery_power_, to ensure that // IsOnBatteryPower can be called from any thread. diff --git a/chromium/base/power_monitor/power_monitor_unittest.cc b/chromium/base/power_monitor/power_monitor_unittest.cc index 0c4e7cb9030..a162763a83f 100644 --- a/chromium/base/power_monitor/power_monitor_unittest.cc +++ b/chromium/base/power_monitor/power_monitor_unittest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/macros.h" +#include "base/message_loop/message_loop.h" #include "base/power_monitor/power_monitor.h" #include "base/test/power_monitor_test_base.h" #include "testing/gtest/include/gtest/gtest.h" @@ -22,6 +23,7 @@ class PowerMonitorTest : public testing::Test { PowerMonitor* monitor() { return power_monitor_.get(); } private: + base::MessageLoop message_loop_; PowerMonitorTestSource* power_monitor_source_; std::unique_ptr<PowerMonitor> power_monitor_; |