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.
5 #ifndef CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
6 #define CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
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"
17 namespace power_manager {
18 class PowerSupplyProperties;
23 // A class which starts collecting power metrics, like the battery charge, as
24 // soon as it is initialized via Initialize().
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 {
30 struct PowerSupplySnapshot {
31 PowerSupplySnapshot();
33 // Time when the snapshot was captured.
36 // True if connected to external power at the time of the snapshot.
39 // The battery charge as a percentage of full charge in range [0.0, 100.00].
40 double battery_percent;
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;
47 const std::deque<PowerSupplySnapshot>& power_supply_data() const {
48 return power_supply_data_;
51 // Can be called only after DBusThreadManager is initialized.
52 static void Initialize();
54 // Can be called only if initialized via Initialize, and before
55 // DBusThreadManager is destroyed.
56 static void Shutdown();
58 // Returns the global instance of PowerDataCollector.
59 static PowerDataCollector* Get();
61 // PowerManagerClient::Observer implementation:
62 virtual void PowerChanged(
63 const power_manager::PowerSupplyProperties& prop) OVERRIDE;
65 // Only those power data samples which fall within the last
66 // |kSampleTimeLimitSec| are stored in memory.
67 static const int kSampleTimeLimitSec;
70 FRIEND_TEST_ALL_PREFIXES(PowerDataCollectorTest, AddSnapshot);
74 virtual ~PowerDataCollector();
76 // Adds a snapshot to |power_supply_data_|.
77 // It dumps snapshots |kSampleTimeLimitSec| or more older than |snapshot|.
78 void AddSnapshot(const PowerSupplySnapshot& snapshot);
80 std::deque<PowerSupplySnapshot> power_supply_data_;
82 DISALLOW_COPY_AND_ASSIGN(PowerDataCollector);
85 } // namespace chromeos
87 #endif // CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_