power_monitor.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef BASE_POWER_MONITOR_POWER_MONITOR_H_
  5. #define BASE_POWER_MONITOR_POWER_MONITOR_H_
  6. #include "base/base_export.h"
  7. #include "base/macros.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/no_destructor.h"
  10. #include "base/observer_list_threadsafe.h"
  11. #include "base/power_monitor/power_observer.h"
  12. #include "build/build_config.h"
  13. namespace base {
  14. class PowerMonitorSource;
  15. // A class used to monitor the power state change and notify the observers about
  16. // the change event. The threading model of this class is as follows:
  17. // Once initialized, it is threadsafe. However, the client must ensure that
  18. // initialization happens before any other methods are invoked, including
  19. // IsInitialized(). IsInitialized() exists only as a convenience for detection
  20. // of test contexts where the PowerMonitor global is never created.
  21. class BASE_EXPORT PowerMonitor {
  22. public:
  23. // Initializes global PowerMonitor state. Takes ownership of |source|, which
  24. // will be leaked on process teardown. May only be called once. Not threadsafe
  25. // - no other PowerMonitor methods may be called on any thread while calling
  26. // Initialize(). |source| must not be nullptr.
  27. static void Initialize(std::unique_ptr<PowerMonitorSource> source);
  28. // Returns true if Initialize() has been called. Safe to call on any thread,
  29. // but must not be called while Initialize() or ShutdownForTesting() is being
  30. // invoked.
  31. static bool IsInitialized();
  32. // Add and remove an observer.
  33. // Can be called from any thread. |observer| is notified on the sequence
  34. // from which it was registered.
  35. // Must not be called from within a notification callback.
  36. //
  37. // It is safe to add observers before the PowerMonitor is initialized. It is
  38. // safe to call RemoveObserver with a PowerObserver that was not added as an
  39. // observer.
  40. static void AddObserver(PowerObserver* observer);
  41. static void RemoveObserver(PowerObserver* observer);
  42. // Is the computer currently on battery power. May only be called if the
  43. // PowerMonitor has been initialized.
  44. static bool IsOnBatteryPower();
  45. // Is the computer currently in suspend mode. Safe to call on any thread. Safe
  46. // to call even if the PowerMonitor hasn't been initialized. When called
  47. // before initialisation, the process is assumed to not be suspended no matter
  48. // what is the real power state.
  49. static bool IsProcessSuspended();
  50. // Read the current DeviceThermalState if known. Can be called on any thread.
  51. // May only be called if the PowerMonitor has been initialized.
  52. static PowerObserver::DeviceThermalState GetCurrentThermalState();
  53. #if defined(OS_ANDROID)
  54. // Read and return the current remaining battery capacity (microampere-hours).
  55. // Only supported with a device power source (i.e. not in child processes in
  56. // Chrome) and on devices with Android >= Lollipop as well as a power supply
  57. // that supports this counter. Returns 0 if unsupported.
  58. static int GetRemainingBatteryCapacity();
  59. #endif // defined(OS_ANDROID)
  60. // Uninitializes the PowerMonitor. Should be called at the end of any unit
  61. // test that mocks out the PowerMonitor, to avoid affecting subsequent tests.
  62. // There must be no live PowerObservers when invoked. Safe to call even if the
  63. // PowerMonitor hasn't been initialized.
  64. static void ShutdownForTesting();
  65. private:
  66. friend class PowerMonitorSource;
  67. friend class base::NoDestructor<PowerMonitor>;
  68. PowerMonitor();
  69. ~PowerMonitor();
  70. static PowerMonitorSource* Source();
  71. static void NotifyPowerStateChange(bool battery_in_use);
  72. static void NotifySuspend();
  73. static void NotifyResume();
  74. static void NotifyThermalStateChange(
  75. PowerObserver::DeviceThermalState new_state);
  76. static PowerMonitor* GetInstance();
  77. scoped_refptr<ObserverListThreadSafe<PowerObserver>> observers_;
  78. std::unique_ptr<PowerMonitorSource> source_;
  79. DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
  80. };
  81. } // namespace base
  82. #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_