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" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 15 | #include "shill/scope_logger.h" |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 16 | |
| 17 | using std::string; |
| 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | PowerManager::PowerManager(ProxyFactory *proxy_factory) |
| 22 | : power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)) { |
| 23 | } |
| 24 | |
| 25 | PowerManager::~PowerManager() { |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void PowerManager::AddStateChangeCallback(const string &key, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 29 | const PowerStateCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 30 | SLOG(Power, 2) << __func__ << " key " << key; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 31 | if (ContainsKey(state_change_callbacks_, key)) { |
| 32 | LOG(DFATAL) << "Inserting duplicate key " << key; |
| 33 | LOG(INFO) << "Removing previous callback for key " << key; |
| 34 | RemoveStateChangeCallback(key); |
| 35 | } |
| 36 | state_change_callbacks_[key] = callback; |
| 37 | } |
| 38 | |
| 39 | void PowerManager::RemoveStateChangeCallback(const string &key) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 40 | SLOG(Power, 2) << __func__ << " key " << key; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 41 | DCHECK(ContainsKey(state_change_callbacks_, key)) << "Removing unknown key " |
| 42 | << key; |
| 43 | StateChangeCallbackMap::iterator it = state_change_callbacks_.find(key); |
| 44 | if (it != state_change_callbacks_.end()) { |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 45 | state_change_callbacks_.erase(it); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void PowerManager::OnSuspendDelay(uint32 /* sequence_number */) { |
| 50 | // TODO(gmorain): Do something. |
| 51 | } |
| 52 | |
| 53 | void PowerManager::OnPowerStateChanged(SuspendState new_power_state) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 54 | SLOG(Power, 2) << __func__ << " new_power_state " << new_power_state; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 55 | for (StateChangeCallbackMap::const_iterator it = |
| 56 | state_change_callbacks_.begin(); |
| 57 | it != state_change_callbacks_.end(); ++it) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 58 | it->second.Run(new_power_state); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) { |
| 63 | // TODO(gmorain): Dispatch this to the proxy. |
| 64 | } |
| 65 | |
| 66 | } // namespace shill |