blob: 1afbb484114b92f0f3a961abe014fbf2a0d554a9 [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
10#include <base/logging.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050011#include <base/stl_util.h>
Gary Morain43bc6272012-01-30 14:01:15 -080012
13#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)
21 : power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)) {
22}
23
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) {
mukesh agrawalbf14e942012-03-02 14:36:34 -080029 VLOG(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) {
mukesh agrawalbf14e942012-03-02 14:36:34 -080039 VLOG(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) {
mukesh agrawalbf14e942012-03-02 14:36:34 -080053 VLOG(2) << __func__ << " new_power_state " << new_power_state;
Gary Morain43bc6272012-01-30 14:01:15 -080054 for (StateChangeCallbackMap::const_iterator it =
55 state_change_callbacks_.begin();
56 it != state_change_callbacks_.end(); ++it) {
Eric Shienbrood3e20a232012-02-16 11:35:56 -050057 it->second.Run(new_power_state);
Gary Morain43bc6272012-01-30 14:01:15 -080058 }
59}
60
61void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) {
62 // TODO(gmorain): Dispatch this to the proxy.
63}
64
65} // namespace shill