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 #include "chrome/browser/chromeos/power/power_data_collector.h" 6 7 #include "base/logging.h" 8 #include "chromeos/dbus/dbus_thread_manager.h" 9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" 10 11 namespace chromeos { 12 13 namespace { 14 15 // The global PowerDataCollector instance. 16 PowerDataCollector* g_power_data_collector = NULL; 17 18 } // namespace 19 20 const int PowerDataCollector::kSampleTimeLimitSec = 24 * 60 * 60; 21 22 // static Initialize()23void 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); 29 } 30 InitializeForTesting()31void PowerDataCollector::InitializeForTesting() { 32 CHECK(DBusThreadManager::Get()); 33 CHECK(g_power_data_collector == NULL); 34 g_power_data_collector = new PowerDataCollector(false); 35 } 36 37 // static Shutdown()38void 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; 43 } 44 45 // static Get()46PowerDataCollector* PowerDataCollector::Get() { 47 CHECK(g_power_data_collector); 48 return g_power_data_collector; 49 } 50 PowerChanged(const power_manager::PowerSupplyProperties & prop)51void 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); 60 } 61 SuspendDone(const base::TimeDelta & sleep_duration)62void 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); 67 } 68 PowerDataCollector(const bool start_cpu_data_collector)69PowerDataCollector::PowerDataCollector(const bool start_cpu_data_collector) { 70 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); 71 if (start_cpu_data_collector) 72 cpu_data_collector_.Start(); 73 } 74 ~PowerDataCollector()75PowerDataCollector::~PowerDataCollector() { 76 DBusThreadManager* dbus_manager = DBusThreadManager::Get(); 77 CHECK(dbus_manager); 78 dbus_manager->GetPowerManagerClient()->RemoveObserver(this); 79 } 80 PowerSupplySample()81PowerDataCollector::PowerSupplySample::PowerSupplySample() 82 : external_power(false), 83 battery_percent(0.0), 84 battery_discharge_rate(0.0) { 85 } 86 SystemResumedSample()87PowerDataCollector::SystemResumedSample::SystemResumedSample() { 88 } 89 90 } // namespace chromeos 91