Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 1 | // 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/stl_util.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 12 | |
| 13 | #include "shill/power_manager_proxy_interface.h" |
| 14 | #include "shill/proxy_factory.h" |
| 15 | |
| 16 | using std::string; |
| 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | PowerManager::PowerManager(ProxyFactory *proxy_factory) |
| 21 | : power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)) { |
| 22 | } |
| 23 | |
| 24 | PowerManager::~PowerManager() { |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void PowerManager::AddStateChangeCallback(const string &key, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 28 | const PowerStateCallback &callback) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 29 | VLOG(2) << __func__ << " key " << key; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 30 | 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 | |
| 38 | void PowerManager::RemoveStateChangeCallback(const string &key) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 39 | VLOG(2) << __func__ << " key " << key; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 40 | 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 Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 44 | state_change_callbacks_.erase(it); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void PowerManager::OnSuspendDelay(uint32 /* sequence_number */) { |
| 49 | // TODO(gmorain): Do something. |
| 50 | } |
| 51 | |
| 52 | void PowerManager::OnPowerStateChanged(SuspendState new_power_state) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 53 | VLOG(2) << __func__ << " new_power_state " << new_power_state; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 54 | for (StateChangeCallbackMap::const_iterator it = |
| 55 | state_change_callbacks_.begin(); |
| 56 | it != state_change_callbacks_.end(); ++it) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 57 | it->second.Run(new_power_state); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) { |
| 62 | // TODO(gmorain): Dispatch this to the proxy. |
| 63 | } |
| 64 | |
| 65 | } // namespace shill |