1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_
6 #define BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/observer_list_threadsafe.h"
12 #include "base/power_monitor/power_monitor_source.h"
13 #include "base/power_monitor/power_observer.h"
18 // Windows HiRes timers drain the battery faster so we need to know the battery
19 // status. This isn't true for other platforms.
20 #define ENABLE_BATTERY_MONITORING 1
22 #undef ENABLE_BATTERY_MONITORING
25 #if defined(ENABLE_BATTERY_MONITORING)
26 #include "base/timer/timer.h"
27 #endif // defined(ENABLE_BATTERY_MONITORING)
30 #include <objc/runtime.h>
35 // A class used to monitor the power state change and notify the observers about
37 class BASE_EXPORT PowerMonitorDeviceSource
: public PowerMonitorSource
{
39 PowerMonitorDeviceSource();
40 ~PowerMonitorDeviceSource() override
;
42 #if defined(OS_MACOSX)
43 // Allocate system resources needed by the PowerMonitor class.
45 // This function must be called before instantiating an instance of the class
46 // and before the Sandbox is initialized.
48 static void AllocateSystemIOPorts();
50 static void AllocateSystemIOPorts() {}
54 #if defined(OS_CHROMEOS)
55 // On Chrome OS, Chrome receives power-related events from powerd, the system
56 // power daemon, via D-Bus signals received on the UI thread. base can't
57 // directly depend on that code, so this class instead exposes static methods
58 // so that events can be passed in.
59 static void SetPowerSource(bool on_battery
);
60 static void HandleSystemSuspending();
61 static void HandleSystemResumed();
66 // Represents a message-only window for power message handling on Windows.
67 // Only allow PowerMonitor to create it.
68 class PowerMessageWindow
{
71 ~PowerMessageWindow();
74 static LRESULT CALLBACK
WndProcThunk(HWND hwnd
,
78 // Instance of the module containing the window procedure.
80 // A hidden message-only window.
85 #if defined(OS_MACOSX)
87 void PlatformDestroy();
90 // Platform-specific method to check whether the system is currently
91 // running on battery power. Returns true if running on batteries,
93 bool IsOnBatteryPowerImpl() override
;
95 // Checks the battery status and notifies observers if the battery
96 // status has changed.
100 // Holds pointers to system event notification observers.
101 std::vector
<id
> notification_observers_
;
104 #if defined(ENABLE_BATTERY_MONITORING)
105 base::OneShotTimer
<PowerMonitorDeviceSource
> delayed_battery_check_
;
109 PowerMessageWindow power_message_window_
;
112 DISALLOW_COPY_AND_ASSIGN(PowerMonitorDeviceSource
);
117 #endif // BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_