Home | History | Annotate | Download | only in power_monitor
      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/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"
     14 
     15 #if defined(OS_WIN)
     16 #include <windows.h>
     17 
     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
     21 #else
     22 #undef ENABLE_BATTERY_MONITORING
     23 #endif  // !OS_WIN
     24 
     25 #if defined(ENABLE_BATTERY_MONITORING)
     26 #include "base/timer/timer.h"
     27 #endif  // defined(ENABLE_BATTERY_MONITORING)
     28 
     29 #if defined(OS_IOS)
     30 #include <objc/runtime.h>
     31 #endif  // OS_IOS
     32 
     33 namespace base {
     34 
     35 // A class used to monitor the power state change and notify the observers about
     36 // the change event.
     37 class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
     38  public:
     39   PowerMonitorDeviceSource();
     40   virtual ~PowerMonitorDeviceSource();
     41 
     42 #if defined(OS_MACOSX)
     43   // Allocate system resources needed by the PowerMonitor class.
     44   //
     45   // This function must be called before instantiating an instance of the class
     46   // and before the Sandbox is initialized.
     47 #if !defined(OS_IOS)
     48   static void AllocateSystemIOPorts();
     49 #else
     50   static void AllocateSystemIOPorts() {}
     51 #endif  // OS_IOS
     52 #endif  // OS_MACOSX
     53 
     54  private:
     55 #if defined(OS_WIN)
     56   // Represents a message-only window for power message handling on Windows.
     57   // Only allow PowerMonitor to create it.
     58   class PowerMessageWindow {
     59    public:
     60     PowerMessageWindow();
     61     ~PowerMessageWindow();
     62 
     63    private:
     64     void ProcessWmPowerBroadcastMessage(int event_id);
     65     LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
     66                              WPARAM wparam, LPARAM lparam);
     67     static LRESULT CALLBACK WndProcThunk(HWND hwnd,
     68                                          UINT message,
     69                                          WPARAM wparam,
     70                                          LPARAM lparam);
     71     // Instance of the module containing the window procedure.
     72     HMODULE instance_;
     73     // A hidden message-only window.
     74     HWND message_hwnd_;
     75   };
     76 #endif  // OS_WIN
     77 
     78 #if defined(OS_MACOSX)
     79   void PlatformInit();
     80   void PlatformDestroy();
     81 #endif
     82 
     83   // Platform-specific method to check whether the system is currently
     84   // running on battery power.  Returns true if running on batteries,
     85   // false otherwise.
     86   virtual bool IsOnBatteryPowerImpl() OVERRIDE;
     87 
     88   // Checks the battery status and notifies observers if the battery
     89   // status has changed.
     90   void BatteryCheck();
     91 
     92 #if defined(OS_IOS)
     93   // Holds pointers to system event notification observers.
     94   std::vector<id> notification_observers_;
     95 #endif
     96 
     97 #if defined(ENABLE_BATTERY_MONITORING)
     98   base::OneShotTimer<PowerMonitorDeviceSource> delayed_battery_check_;
     99 #endif
    100 
    101 #if defined(OS_WIN)
    102   PowerMessageWindow power_message_window_;
    103 #endif
    104 
    105   DISALLOW_COPY_AND_ASSIGN(PowerMonitorDeviceSource);
    106 };
    107 
    108 }  // namespace base
    109 
    110 #endif  // BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_
    111