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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 10 | #include <base/stl_util.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 11 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 12 | #include "shill/logging.h" |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 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) |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 21 | : power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)), |
| 22 | power_state_(kUnknown) {} |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 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) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 29 | SLOG(Power, 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) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 39 | SLOG(Power, 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) { |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 53 | LOG(INFO) << "Power state changed: " |
| 54 | << power_state_ << "->" << new_power_state; |
| 55 | power_state_ = new_power_state; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 56 | for (StateChangeCallbackMap::const_iterator it = |
| 57 | state_change_callbacks_.begin(); |
| 58 | it != state_change_callbacks_.end(); ++it) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 59 | it->second.Run(new_power_state); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) { |
| 64 | // TODO(gmorain): Dispatch this to the proxy. |
| 65 | } |
| 66 | |
| 67 | } // namespace shill |