blob: 73cc7dd854e5289eaf0cd4ee43a5d834592438f7 [file] [log] [blame]
Gary Morain43bc6272012-01-30 14:01:15 -08001// Copyright (c) 2012 The Chromium OS 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 "shill/power_manager.h"
6
7#include <map>
8#include <string>
9
Eric Shienbrood3e20a232012-02-16 11:35:56 -050010#include <base/stl_util.h>
Gary Morain43bc6272012-01-30 14:01:15 -080011
Christopher Wileyb691efd2012-08-09 13:51:51 -070012#include "shill/logging.h"
Gary Morain43bc6272012-01-30 14:01:15 -080013#include "shill/power_manager_proxy_interface.h"
14#include "shill/proxy_factory.h"
15
16using std::string;
17
18namespace shill {
19
20PowerManager::PowerManager(ProxyFactory *proxy_factory)
Darin Petkovca621542012-07-25 14:25:56 +020021 : power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)),
22 power_state_(kUnknown) {}
Gary Morain43bc6272012-01-30 14:01:15 -080023
24PowerManager::~PowerManager() {
Gary Morain43bc6272012-01-30 14:01:15 -080025}
26
27void PowerManager::AddStateChangeCallback(const string &key,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050028 const PowerStateCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070029 SLOG(Power, 2) << __func__ << " key " << key;
Gary Morain43bc6272012-01-30 14:01:15 -080030 if (ContainsKey(state_change_callbacks_, key)) {
31 LOG(DFATAL) << "Inserting duplicate key " << key;
32 LOG(INFO) << "Removing previous callback for key " << key;
33 RemoveStateChangeCallback(key);
34 }
35 state_change_callbacks_[key] = callback;
36}
37
38void PowerManager::RemoveStateChangeCallback(const string &key) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070039 SLOG(Power, 2) << __func__ << " key " << key;
Gary Morain43bc6272012-01-30 14:01:15 -080040 DCHECK(ContainsKey(state_change_callbacks_, key)) << "Removing unknown key "
41 << key;
42 StateChangeCallbackMap::iterator it = state_change_callbacks_.find(key);
43 if (it != state_change_callbacks_.end()) {
Gary Morain43bc6272012-01-30 14:01:15 -080044 state_change_callbacks_.erase(it);
45 }
46}
47
48void PowerManager::OnSuspendDelay(uint32 /* sequence_number */) {
49 // TODO(gmorain): Do something.
50}
51
52void PowerManager::OnPowerStateChanged(SuspendState new_power_state) {
Darin Petkovca621542012-07-25 14:25:56 +020053 LOG(INFO) << "Power state changed: "
54 << power_state_ << "->" << new_power_state;
55 power_state_ = new_power_state;
Gary Morain43bc6272012-01-30 14:01:15 -080056 for (StateChangeCallbackMap::const_iterator it =
57 state_change_callbacks_.begin();
58 it != state_change_callbacks_.end(); ++it) {
Eric Shienbrood3e20a232012-02-16 11:35:56 -050059 it->second.Run(new_power_state);
Gary Morain43bc6272012-01-30 14:01:15 -080060 }
61}
62
63void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) {
64 // TODO(gmorain): Dispatch this to the proxy.
65}
66
67} // namespace shill