blob: 21d06df0906da0c7e84c4105f37709635600f35c [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>
11#include <base/stl_util-inl.h>
12
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() {
25 STLDeleteValues(&state_change_callbacks_);
26}
27
28void PowerManager::AddStateChangeCallback(const string &key,
29 PowerStateCallback *callback) {
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
38void PowerManager::RemoveStateChangeCallback(const string &key) {
39 DCHECK(ContainsKey(state_change_callbacks_, key)) << "Removing unknown key "
40 << key;
41 StateChangeCallbackMap::iterator it = state_change_callbacks_.find(key);
42 if (it != state_change_callbacks_.end()) {
43 delete it->second;
44 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) {
53 for (StateChangeCallbackMap::const_iterator it =
54 state_change_callbacks_.begin();
55 it != state_change_callbacks_.end(); ++it) {
56 it->second->Run(new_power_state);
57 }
58}
59
60void PowerManager::RegisterSuspendDelay(const uint32_t & /* delay_ms */) {
61 // TODO(gmorain): Dispatch this to the proxy.
62}
63
64} // namespace shill