• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_
6 #define BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_
7 
8 #include "base/base_export.h"
9 #include "base/macros.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"
14 #include "build/build_config.h"
15 
16 #if defined(OS_WIN)
17 #include <windows.h>
18 
19 // Windows HiRes timers drain the battery faster so we need to know the battery
20 // status.  This isn't true for other platforms.
21 #define ENABLE_BATTERY_MONITORING 1
22 #else
23 #undef ENABLE_BATTERY_MONITORING
24 #endif  // !OS_WIN
25 
26 #if defined(ENABLE_BATTERY_MONITORING)
27 #include "base/timer/timer.h"
28 #endif  // defined(ENABLE_BATTERY_MONITORING)
29 
30 #if defined(OS_IOS)
31 #include <objc/runtime.h>
32 #endif  // OS_IOS
33 
34 namespace base {
35 
36 // A class used to monitor the power state change and notify the observers about
37 // the change event.
38 class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
39  public:
40   PowerMonitorDeviceSource();
41   ~PowerMonitorDeviceSource() override;
42 
43 #if defined(OS_MACOSX)
44   // Allocate system resources needed by the PowerMonitor class.
45   //
46   // This function must be called before instantiating an instance of the class
47   // and before the Sandbox is initialized.
48 #if !defined(OS_IOS)
49   static void AllocateSystemIOPorts();
50 #else
AllocateSystemIOPorts()51   static void AllocateSystemIOPorts() {}
52 #endif  // OS_IOS
53 #endif  // OS_MACOSX
54 
55 #if defined(OS_CHROMEOS)
56   // On Chrome OS, Chrome receives power-related events from powerd, the system
57   // power daemon, via D-Bus signals received on the UI thread. base can't
58   // directly depend on that code, so this class instead exposes static methods
59   // so that events can be passed in.
60   static void SetPowerSource(bool on_battery);
61   static void HandleSystemSuspending();
62   static void HandleSystemResumed();
63 #endif
64 
65  private:
66 #if defined(OS_WIN)
67   // Represents a message-only window for power message handling on Windows.
68   // Only allow PowerMonitor to create it.
69   class PowerMessageWindow {
70    public:
71     PowerMessageWindow();
72     ~PowerMessageWindow();
73 
74    private:
75     static LRESULT CALLBACK WndProcThunk(HWND hwnd,
76                                          UINT message,
77                                          WPARAM wparam,
78                                          LPARAM lparam);
79     // Instance of the module containing the window procedure.
80     HMODULE instance_;
81     // A hidden message-only window.
82     HWND message_hwnd_;
83   };
84 #endif  // OS_WIN
85 
86 #if defined(OS_MACOSX)
87   void PlatformInit();
88   void PlatformDestroy();
89 #endif
90 
91   // Platform-specific method to check whether the system is currently
92   // running on battery power.  Returns true if running on batteries,
93   // false otherwise.
94   bool IsOnBatteryPowerImpl() override;
95 
96   // Checks the battery status and notifies observers if the battery
97   // status has changed.
98   void BatteryCheck();
99 
100 #if defined(OS_IOS)
101   // Holds pointers to system event notification observers.
102   std::vector<id> notification_observers_;
103 #endif
104 
105 #if defined(ENABLE_BATTERY_MONITORING)
106   base::OneShotTimer delayed_battery_check_;
107 #endif
108 
109 #if defined(OS_WIN)
110   PowerMessageWindow power_message_window_;
111 #endif
112 
113   DISALLOW_COPY_AND_ASSIGN(PowerMonitorDeviceSource);
114 };
115 
116 }  // namespace base
117 
118 #endif  // BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_
119