root/chrome/browser/chromeos/power/power_data_collector.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. Initialize
  2. InitializeForTesting
  3. Shutdown
  4. Get
  5. PowerChanged
  6. SystemResumed
  7. battery_discharge_rate

// Copyright 2014 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 "chrome/browser/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(true);
}

void PowerDataCollector::InitializeForTesting() {
  CHECK(DBusThreadManager::Get());
  CHECK(g_power_data_collector == NULL);
  g_power_data_collector = new PowerDataCollector(false);
}

// 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(const bool start_cpu_data_collector) {
  DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
  if (start_cpu_data_collector)
    cpu_data_collector_.Start();
}

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

/* [<][>][^][v][top][bottom][index][help] */