Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / power / power_data_collector.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
6 #define CHROME_BROWSER_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/time/time.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/power_manager_client.h"
15
16 namespace power_manager {
17 class PowerSupplyProperties;
18 }
19
20 namespace chromeos {
21
22 // A class which starts collecting power metrics, like the battery charge, as
23 // soon as it is initialized via Initialize().
24 //
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 {
28  public:
29   struct PowerSupplySample {
30     PowerSupplySample();
31
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
34     // suspended.
35     base::Time time;
36
37     // True if connected to external power at the time of the sample.
38     bool external_power;
39
40     // The battery charge as a percentage of full charge in range [0.0, 100.00].
41     double battery_percent;
42
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;
46   };
47
48   struct SystemResumedSample {
49     SystemResumedSample();
50
51     // Time when the system resumed.
52     base::Time time;
53
54     // The duration for which the system was in sleep/suspend state.
55     base::TimeDelta sleep_duration;
56   };
57
58   const std::deque<PowerSupplySample>& power_supply_data() const {
59     return power_supply_data_;
60   }
61
62   const std::deque<SystemResumedSample>& system_resumed_data() const {
63     return system_resumed_data_;
64   }
65
66   // Can be called only after DBusThreadManager is initialized.
67   static void Initialize();
68
69   // Can be called only if initialized via Initialize, and before
70   // DBusThreadManager is destroyed.
71   static void Shutdown();
72
73   // Returns the global instance of PowerDataCollector.
74   static PowerDataCollector* Get();
75
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;
80
81   // Only those power data samples which fall within the last
82   // |kSampleTimeLimitSec| are stored in memory.
83   static const int kSampleTimeLimitSec;
84
85  private:
86   PowerDataCollector();
87
88   virtual ~PowerDataCollector();
89
90   std::deque<PowerSupplySample> power_supply_data_;
91   std::deque<SystemResumedSample> system_resumed_data_;
92
93   DISALLOW_COPY_AND_ASSIGN(PowerDataCollector);
94 };
95
96 // Adds |sample| to |sample_deque|.
97 // It dumps samples |PowerDataCollector::kSampleTimeLimitSec| or more older than
98 // |sample|.
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();
106     } else {
107       break;
108     }
109   }
110   sample_queue->push_back(sample);
111 }
112
113 }  // namespace chromeos
114
115 #endif  // CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_