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