Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chromeos / power / power_data_collector.h
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 CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
6 #define CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
7
8 #include <deque>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/time/time.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/power_manager_client.h"
16
17 namespace power_manager {
18 class PowerSupplyProperties;
19 }
20
21 namespace chromeos {
22
23 // A class which starts collecting power metrics, like the battery charge, as
24 // soon as it is initialized via Initialize().
25 //
26 // This class is implemented as a global singleton, initialized after
27 // DBusThreadManager which it depends on.
28 class CHROMEOS_EXPORT PowerDataCollector : public PowerManagerClient::Observer {
29  public:
30   struct PowerSupplySnapshot {
31     PowerSupplySnapshot();
32
33     // Time when the snapshot was captured.
34     base::TimeTicks time;
35
36     // True if connected to external power at the time of the snapshot.
37     bool external_power;
38
39     // The battery charge as a percentage of full charge in range [0.0, 100.00].
40     double battery_percent;
41
42     // The battery discharge rate in W. Positive if the battery is being
43     // discharged and negative if it's being charged.
44     double battery_discharge_rate;
45   };
46
47   const std::deque<PowerSupplySnapshot>& power_supply_data() const {
48     return power_supply_data_;
49   }
50
51   // Can be called only after DBusThreadManager is initialized.
52   static void Initialize();
53
54   // Can be called only if initialized via Initialize, and before
55   // DBusThreadManager is destroyed.
56   static void Shutdown();
57
58   // Returns the global instance of PowerDataCollector.
59   static PowerDataCollector* Get();
60
61   // PowerManagerClient::Observer implementation:
62   virtual void PowerChanged(
63       const power_manager::PowerSupplyProperties& prop) OVERRIDE;
64
65   // Only those power data samples which fall within the last
66   // |kSampleTimeLimitSec| are stored in memory.
67   static const int kSampleTimeLimitSec;
68
69  private:
70   FRIEND_TEST_ALL_PREFIXES(PowerDataCollectorTest, AddSnapshot);
71
72   PowerDataCollector();
73
74   virtual ~PowerDataCollector();
75
76   // Adds a snapshot to |power_supply_data_|.
77   // It dumps snapshots |kSampleTimeLimitSec| or more older than |snapshot|.
78   void AddSnapshot(const PowerSupplySnapshot& snapshot);
79
80   std::deque<PowerSupplySnapshot> power_supply_data_;
81
82   DISALLOW_COPY_AND_ASSIGN(PowerDataCollector);
83 };
84
85 }  // namespace chromeos
86
87 #endif  // CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_