summaryrefslogtreecommitdiffstats
path: root/chromeos/power/power_data_collector.cc
blob: 8d1811bcd95bb743191595684e8dfdbf4ab480b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/power/power_data_collector.h"

#include "base/logging.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"

namespace chromeos {

namespace {

// The global PowerDataCollector instance.
PowerDataCollector* g_power_data_collector = NULL;

}  // namespace

const int PowerDataCollector::kSampleTimeLimitSec = 24 * 60 * 60;

// static
void PowerDataCollector::Initialize() {
  // Check that power data collector is initialized only after the
  // DBusThreadManager is initialized.
  CHECK(DBusThreadManager::Get());
  CHECK(g_power_data_collector == NULL);
  g_power_data_collector = new PowerDataCollector();
}

// static
void PowerDataCollector::Shutdown() {
  // Shutdown only if initialized.
  CHECK(g_power_data_collector);
  delete g_power_data_collector;
  g_power_data_collector = NULL;
}

// static
PowerDataCollector* PowerDataCollector::Get() {
  CHECK(g_power_data_collector);
  return g_power_data_collector;
}

void PowerDataCollector::PowerChanged(
    const power_manager::PowerSupplyProperties& prop) {
  PowerSupplySample sample;
  sample.time = base::Time::Now();
  sample.external_power = (prop.external_power() !=
      power_manager::PowerSupplyProperties::DISCONNECTED);
  sample.battery_percent = prop.battery_percent();
  sample.battery_discharge_rate = prop.battery_discharge_rate();
  AddSample(&power_supply_data_, sample);
}

void PowerDataCollector::SystemResumed(const base::TimeDelta& sleep_duration) {
  SystemResumedSample sample;
  sample.time = base::Time::Now();
  sample.sleep_duration = sleep_duration;
  AddSample(&system_resumed_data_, sample);
}

PowerDataCollector::PowerDataCollector() {
  DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
}

PowerDataCollector::~PowerDataCollector() {
  DBusThreadManager* dbus_manager = DBusThreadManager::Get();
  CHECK(dbus_manager);
  dbus_manager->GetPowerManagerClient()->RemoveObserver(this);
}

PowerDataCollector::PowerSupplySample::PowerSupplySample()
    : external_power(false),
      battery_percent(0.0),
      battery_discharge_rate(0.0) {
}

PowerDataCollector::SystemResumedSample::SystemResumedSample() {
}

}  // namespace chromeos