• 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_H_
6 #define BASE_POWER_MONITOR_POWER_MONITOR_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_observer.h"
13 
14 namespace base {
15 
16 class PowerMonitorSource;
17 
18 // A class used to monitor the power state change and notify the observers about
19 // the change event.
20 class BASE_EXPORT PowerMonitor {
21  public:
22   // Takes ownership of |source|.
23   explicit PowerMonitor(std::unique_ptr<PowerMonitorSource> source);
24   ~PowerMonitor();
25 
26   // Get the process-wide PowerMonitor (if not present, returns NULL).
27   static PowerMonitor* Get();
28 
29   // Add and remove an observer.
30   // Can be called from any thread. |observer| is notified on the sequence
31   // from which it was registered.
32   // Must not be called from within a notification callback.
33   void AddObserver(PowerObserver* observer);
34   void RemoveObserver(PowerObserver* observer);
35 
36   // Is the computer currently on battery power.
37   bool IsOnBatteryPower();
38 
39  private:
40   friend class PowerMonitorSource;
41 
42   PowerMonitorSource* Source();
43 
44   void NotifyPowerStateChange(bool battery_in_use);
45   void NotifySuspend();
46   void NotifyResume();
47 
48   scoped_refptr<ObserverListThreadSafe<PowerObserver>> observers_;
49   std::unique_ptr<PowerMonitorSource> source_;
50 
51   DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
52 };
53 
54 }  // namespace base
55 
56 #endif  // BASE_POWER_MONITOR_POWER_MONITOR_H_
57