1 // Copyright 2014 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 CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/time/time.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/power_manager_client.h"
16 namespace power_manager {
17 class PowerSupplyProperties;
22 // A class which starts collecting power metrics, like the battery charge, as
23 // soon as it is initialized via Initialize().
25 // This class is implemented as a global singleton, initialized after
26 // DBusThreadManager which it depends on.
27 class CHROMEOS_EXPORT PowerDataCollector : public PowerManagerClient::Observer {
29 struct PowerSupplySample {
32 // Time when the sample was captured. We use base::Time instead of
33 // base::TimeTicks because the latter does not advance when the system is
37 // True if connected to external power at the time of the sample.
40 // The battery charge as a percentage of full charge in range [0.0, 100.00].
41 double battery_percent;
43 // The battery discharge rate in W. Positive if the battery is being
44 // discharged and negative if it's being charged.
45 double battery_discharge_rate;
48 struct SystemResumedSample {
49 SystemResumedSample();
51 // Time when the system resumed.
54 // The duration for which the system was in sleep/suspend state.
55 base::TimeDelta sleep_duration;
58 const std::deque<PowerSupplySample>& power_supply_data() const {
59 return power_supply_data_;
62 const std::deque<SystemResumedSample>& system_resumed_data() const {
63 return system_resumed_data_;
66 // Can be called only after DBusThreadManager is initialized.
67 static void Initialize();
69 // Can be called only if initialized via Initialize, and before
70 // DBusThreadManager is destroyed.
71 static void Shutdown();
73 // Returns the global instance of PowerDataCollector.
74 static PowerDataCollector* Get();
76 // PowerManagerClient::Observer implementation:
77 virtual void PowerChanged(
78 const power_manager::PowerSupplyProperties& prop) OVERRIDE;
79 virtual void SystemResumed(const base::TimeDelta& sleep_duration) OVERRIDE;
81 // Only those power data samples which fall within the last
82 // |kSampleTimeLimitSec| are stored in memory.
83 static const int kSampleTimeLimitSec;
88 virtual ~PowerDataCollector();
90 std::deque<PowerSupplySample> power_supply_data_;
91 std::deque<SystemResumedSample> system_resumed_data_;
93 DISALLOW_COPY_AND_ASSIGN(PowerDataCollector);
96 // Adds |sample| to |sample_deque|.
97 // It dumps samples |PowerDataCollector::kSampleTimeLimitSec| or more older than
99 template <typename SampleType>
100 void AddSample(std::deque<SampleType>* sample_queue, const SampleType& sample) {
101 while (!sample_queue->empty()) {
102 const SampleType& first = sample_queue->front();
103 if (sample.time - first.time >
104 base::TimeDelta::FromSeconds(PowerDataCollector::kSampleTimeLimitSec)) {
105 sample_queue->pop_front();
110 sample_queue->push_back(sample);
113 } // namespace chromeos
115 #endif // CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_