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 #include "chrome/browser/chromeos/power/power_data_collector.h"
7 #include "base/logging.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
15 // The global PowerDataCollector instance.
16 PowerDataCollector
* g_power_data_collector
= NULL
;
20 const int PowerDataCollector::kSampleTimeLimitSec
= 24 * 60 * 60;
23 void PowerDataCollector::Initialize() {
24 // Check that power data collector is initialized only after the
25 // DBusThreadManager is initialized.
26 CHECK(DBusThreadManager::Get());
27 CHECK(g_power_data_collector
== NULL
);
28 g_power_data_collector
= new PowerDataCollector(true);
31 void PowerDataCollector::InitializeForTesting() {
32 CHECK(DBusThreadManager::Get());
33 CHECK(g_power_data_collector
== NULL
);
34 g_power_data_collector
= new PowerDataCollector(false);
38 void PowerDataCollector::Shutdown() {
39 // Shutdown only if initialized.
40 CHECK(g_power_data_collector
);
41 delete g_power_data_collector
;
42 g_power_data_collector
= NULL
;
46 PowerDataCollector
* PowerDataCollector::Get() {
47 CHECK(g_power_data_collector
);
48 return g_power_data_collector
;
51 void PowerDataCollector::PowerChanged(
52 const power_manager::PowerSupplyProperties
& prop
) {
53 PowerSupplySample sample
;
54 sample
.time
= base::Time::Now();
55 sample
.external_power
= (prop
.external_power() !=
56 power_manager::PowerSupplyProperties::DISCONNECTED
);
57 sample
.battery_percent
= prop
.battery_percent();
58 sample
.battery_discharge_rate
= prop
.battery_discharge_rate();
59 AddSample(&power_supply_data_
, sample
);
62 void PowerDataCollector::SuspendDone(const base::TimeDelta
& sleep_duration
) {
63 SystemResumedSample sample
;
64 sample
.time
= base::Time::Now();
65 sample
.sleep_duration
= sleep_duration
;
66 AddSample(&system_resumed_data_
, sample
);
69 PowerDataCollector::PowerDataCollector(const bool start_cpu_data_collector
) {
70 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
71 if (start_cpu_data_collector
)
72 cpu_data_collector_
.Start();
75 PowerDataCollector::~PowerDataCollector() {
76 DBusThreadManager
* dbus_manager
= DBusThreadManager::Get();
78 dbus_manager
->GetPowerManagerClient()->RemoveObserver(this);
81 PowerDataCollector::PowerSupplySample::PowerSupplySample()
82 : external_power(false),
84 battery_discharge_rate(0.0) {
87 PowerDataCollector::SystemResumedSample::SystemResumedSample() {
90 } // namespace chromeos