blob: d8e3176bd7881fc268d096d46fd64a8dff143c99 [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"
Ben Chanfad4a0b2012-04-18 15:49:59 -070015#include "shill/scope_logger.h"
Gary Morain43bc6272012-01-30 14:01:15 -080016
17using std::string;
18
19namespace shill {
20
21PowerManager::PowerManager(ProxyFactory *proxy_factory)
22 : power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)) {
23}
24
25PowerManager::~PowerManager() {
Gary Morain43bc6272012-01-30 14:01:15 -080026}
27
28void PowerManager::AddStateChangeCallback(const string &key,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050029 const PowerStateCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070030 SLOG(Power, 2) << __func__ << " key " << key;
Gary Morain43bc6272012-01-30 14:01:15 -080031 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
39void PowerManager::RemoveStateChangeCallback(const string &key) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070040 SLOG(Power, 2) << __func__ << " key " << key;
Gary Morain43bc6272012-01-30 14:01:15 -080041 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 Morain43bc6272012-01-30 14:01:15 -080045 state_change_callbacks_.erase(it);
46 }
47}
48
49void PowerManager::OnSuspendDelay(uint32 /* sequence_number */) {
50 // TODO(gmorain): Do something.
51}
52
53void PowerManager::OnPowerStateChanged(SuspendState new_power_state) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070054 SLOG(Power, 2) << __func__ << " new_power_state " << new_power_state;
Gary Morain43bc6272012-01-30 14:01:15 -080055 for (StateChangeCallbackMap::const_iterator it =
56 state_change_callbacks_.begin();
57 it != state_change_callbacks_.end(); ++it) {
Eric Shienbrood3e20a232012-02-16 11:35:56 -050058 it->second.Run(new_power_state);
Gary Morain43bc6272012-01-30 14:01:15 -080059 }
60}
61
62void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) {
63 // TODO(gmorain): Dispatch this to the proxy.
64}
65
66} // namespace shill