blob: 7e6c8fed4143a1bfaeba289dbd8d6d814492d167 [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
Darin Petkovca621542012-07-25 14:25:56 +02005#ifndef SHILL_POWER_MANAGER_H_
6#define SHILL_POWER_MANAGER_H_
Gary Morain43bc6272012-01-30 14:01:15 -08007
8// This class instantiates a PowerManagerProxy and distributes power events to
9// registered users. It also provides a means for calling methods on the
10// PowerManagerProxy.
11//
12// Usage:
13//
14// Registering for power state changes is done as follows:
15//
16// class Foo {
17// public:
18// void HandleStateChange(PowerManager::SuspendState new_state);
19// };
20// Foo foo;
21// PowerManager power_manager(ProxyFactory::GetInstance());
Eric Shienbrood3e20a232012-02-16 11:35:56 -050022// PowerManager::PowerStateCallback cb = Bind(&Foo::HandleStateChange, &foo);
Gary Morain43bc6272012-01-30 14:01:15 -080023// power_manager.AddStateChangeCallback("foo_key", cb);
24//
Eric Shienbrood3e20a232012-02-16 11:35:56 -050025// Note that depending on the definition of Foo, "&foo" may need to appear
26// inside an appropriate wrapper, such as base::Unretained.
27//
Gary Morain43bc6272012-01-30 14:01:15 -080028// Whenever the power state changes, foo.HandleStateChange() is called with the
Eric Shienbrood3e20a232012-02-16 11:35:56 -050029// new state passed in. To unregister:
Gary Morain43bc6272012-01-30 14:01:15 -080030//
31// power_manager.RemoveStateChangeCallback("foo_key");
Gary Morain43bc6272012-01-30 14:01:15 -080032
33#include <map>
34#include <string>
35
Eric Shienbrood3e20a232012-02-16 11:35:56 -050036#include <base/callback.h>
37#include <base/memory/scoped_ptr.h>
Gary Morain43bc6272012-01-30 14:01:15 -080038
39#include "shill/power_manager_proxy_interface.h"
40
41namespace shill {
42
43class ProxyFactory;
44
45class PowerManager : public PowerManagerProxyDelegate {
46 public:
47 typedef PowerManagerProxyDelegate::SuspendState SuspendState;
48
49 // Callbacks registered with the power manager are of this type. They take
50 // one argument, the new power state. The callback function or method should
51 // look like this:
52 //
53 // void HandlePowerStateChange(PowerStateCallbacks::SuspendState);
Eric Shienbrood3e20a232012-02-16 11:35:56 -050054 typedef base::Callback<void(SuspendState)> PowerStateCallback;
Gary Morain43bc6272012-01-30 14:01:15 -080055
Gary Morain52fabab2012-08-22 09:27:31 -070056 // This callback is called prior to a suspend event. When it is OK for the
57 // system to suspend, this callback should call SuspendActionComplete(),
58 // passing it the sequence number passed to this callback.
59 typedef base::Callback<void(uint32)> SuspendDelayCallback;
60
Gary Morain43bc6272012-01-30 14:01:15 -080061 // |proxy_factory| creates the PowerManagerProxy. Usually this is
62 // ProxyFactory::GetInstance(). Use a fake for testing.
63 explicit PowerManager(ProxyFactory *proxy_factory);
64 virtual ~PowerManager();
65
Darin Petkovca621542012-07-25 14:25:56 +020066 SuspendState power_state() const { return power_state_; }
67
Gary Morain43bc6272012-01-30 14:01:15 -080068 // Methods inherited from PowerManagerProxyDelegate.
69 virtual void OnSuspendDelay(uint32 sequence_number);
70 virtual void OnPowerStateChanged(SuspendState new_power_state);
71
72 // TODO(gmorain): Add descriptive comment.
73 void RegisterSuspendDelay(const uint32_t &delay_ms);
74
75 // Registers a power state change callback with the power manager. When a
76 // power state change occurs, this callback will be called with the new power
Eric Shienbrood3e20a232012-02-16 11:35:56 -050077 // state as its argument. |key| must be unique.
Gary Morainac1bdb42012-02-16 17:42:29 -080078 virtual void AddStateChangeCallback(const std::string &key,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050079 const PowerStateCallback &callback);
Gary Morain43bc6272012-01-30 14:01:15 -080080
Gary Morain52fabab2012-08-22 09:27:31 -070081 // Registers a suspend delay callback with the power manager. This callback
82 // will be called prior to a suspend event by the amount specified in the most
83 // recent call to RegisterSuspendDelay(). |key| must be unique.
84 virtual void AddSuspendDelayCallback(const std::string &key,
85 const SuspendDelayCallback &callback);
86
87 // Unregisters a power state change callback identified by |key|. The
88 // callback must have been previously registered and not yet removed.
Gary Morainac1bdb42012-02-16 17:42:29 -080089 virtual void RemoveStateChangeCallback(const std::string &key);
Gary Morain43bc6272012-01-30 14:01:15 -080090
Gary Morain52fabab2012-08-22 09:27:31 -070091 // Unregisters a suspend delay callback identified by |key|. The callback
92 // must have been previously registered and not yet removed.
93 virtual void RemoveSuspendDelayCallback(const std::string &key);
Gary Morain43bc6272012-01-30 14:01:15 -080094
95 private:
Darin Petkovca621542012-07-25 14:25:56 +020096 friend class ManagerTest;
97
Eric Shienbrood3e20a232012-02-16 11:35:56 -050098 typedef std::map<const std::string, PowerStateCallback>
Gary Morain43bc6272012-01-30 14:01:15 -080099 StateChangeCallbackMap;
100
Gary Morain52fabab2012-08-22 09:27:31 -0700101 typedef std::map<const std::string, SuspendDelayCallback>
102 SuspendDelayCallbackMap;
103
104 template<class Callback>
105 void AddCallback(const std::string &key, const Callback &callback,
106 std::map<const std::string, Callback> *callback_map);
107
108 template<class Callback>
109 void RemoveCallback(const std::string &key,
110 std::map<const std::string, Callback> *callback_map);
111
112 template<class Param, class Callback>
113 void OnEvent(const Param &param,
114 std::map<const std::string, Callback> *callback_map) const;
115
116
Gary Morain43bc6272012-01-30 14:01:15 -0800117 // The power manager proxy created by this class. It dispatches the inherited
118 // delegate methods of this object when changes in the power state occur.
119 const scoped_ptr<PowerManagerProxyInterface> power_manager_proxy_;
120 StateChangeCallbackMap state_change_callbacks_;
Gary Morain52fabab2012-08-22 09:27:31 -0700121 SuspendDelayCallbackMap suspend_delay_callbacks_;
Gary Morain43bc6272012-01-30 14:01:15 -0800122
Darin Petkovca621542012-07-25 14:25:56 +0200123 SuspendState power_state_;
124
Gary Morain43bc6272012-01-30 14:01:15 -0800125 DISALLOW_COPY_AND_ASSIGN(PowerManager);
126};
127
128} // namespace shill
129
Darin Petkovca621542012-07-25 14:25:56 +0200130#endif // SHILL_POWER_MANAGER_H_