blob: 82700187d4f81dae7e528de7f54b26cbb7434bf3 [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>
Darin Petkov3ec55342012-09-28 14:04:44 +020037#include <base/cancelable_callback.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050038#include <base/memory/scoped_ptr.h>
Gary Morain43bc6272012-01-30 14:01:15 -080039
40#include "shill/power_manager_proxy_interface.h"
41
42namespace shill {
43
Darin Petkov3ec55342012-09-28 14:04:44 +020044class EventDispatcher;
Gary Morain43bc6272012-01-30 14:01:15 -080045class ProxyFactory;
46
47class PowerManager : public PowerManagerProxyDelegate {
48 public:
49 typedef PowerManagerProxyDelegate::SuspendState SuspendState;
50
51 // Callbacks registered with the power manager are of this type. They take
52 // one argument, the new power state. The callback function or method should
53 // look like this:
54 //
55 // void HandlePowerStateChange(PowerStateCallbacks::SuspendState);
Eric Shienbrood3e20a232012-02-16 11:35:56 -050056 typedef base::Callback<void(SuspendState)> PowerStateCallback;
Gary Morain43bc6272012-01-30 14:01:15 -080057
Gary Morain52fabab2012-08-22 09:27:31 -070058 // This callback is called prior to a suspend event. When it is OK for the
59 // system to suspend, this callback should call SuspendActionComplete(),
60 // passing it the sequence number passed to this callback.
61 typedef base::Callback<void(uint32)> SuspendDelayCallback;
62
Darin Petkov3ec55342012-09-28 14:04:44 +020063 static const int kSuspendTimeoutMilliseconds;
64
Gary Morain43bc6272012-01-30 14:01:15 -080065 // |proxy_factory| creates the PowerManagerProxy. Usually this is
66 // ProxyFactory::GetInstance(). Use a fake for testing.
Darin Petkov3ec55342012-09-28 14:04:44 +020067 PowerManager(EventDispatcher *dispatcher,
68 ProxyFactory *proxy_factory);
Gary Morain43bc6272012-01-30 14:01:15 -080069 virtual ~PowerManager();
70
Darin Petkovca621542012-07-25 14:25:56 +020071 SuspendState power_state() const { return power_state_; }
72
Gary Morain43bc6272012-01-30 14:01:15 -080073 // Methods inherited from PowerManagerProxyDelegate.
74 virtual void OnSuspendDelay(uint32 sequence_number);
75 virtual void OnPowerStateChanged(SuspendState new_power_state);
76
Darin Petkov3ec55342012-09-28 14:04:44 +020077 // See corresponding methods in PowerManagerProxyInterface.
78 virtual void RegisterSuspendDelay(uint32 delay_ms);
79 virtual void UnregisterSuspendDelay();
80 virtual void SuspendReady(uint32 suspend_ready);
Gary Morain43bc6272012-01-30 14:01:15 -080081
82 // Registers a power state change callback with the power manager. When a
83 // power state change occurs, this callback will be called with the new power
Eric Shienbrood3e20a232012-02-16 11:35:56 -050084 // state as its argument. |key| must be unique.
Gary Morainac1bdb42012-02-16 17:42:29 -080085 virtual void AddStateChangeCallback(const std::string &key,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050086 const PowerStateCallback &callback);
Gary Morain43bc6272012-01-30 14:01:15 -080087
Gary Morain52fabab2012-08-22 09:27:31 -070088 // Registers a suspend delay callback with the power manager. This callback
89 // will be called prior to a suspend event by the amount specified in the most
90 // recent call to RegisterSuspendDelay(). |key| must be unique.
91 virtual void AddSuspendDelayCallback(const std::string &key,
92 const SuspendDelayCallback &callback);
93
94 // Unregisters a power state change callback identified by |key|. The
95 // callback must have been previously registered and not yet removed.
Gary Morainac1bdb42012-02-16 17:42:29 -080096 virtual void RemoveStateChangeCallback(const std::string &key);
Gary Morain43bc6272012-01-30 14:01:15 -080097
Gary Morain52fabab2012-08-22 09:27:31 -070098 // Unregisters a suspend delay callback identified by |key|. The callback
99 // must have been previously registered and not yet removed.
100 virtual void RemoveSuspendDelayCallback(const std::string &key);
Gary Morain43bc6272012-01-30 14:01:15 -0800101
102 private:
Darin Petkovca621542012-07-25 14:25:56 +0200103 friend class ManagerTest;
Darin Petkov3ec55342012-09-28 14:04:44 +0200104 friend class PowerManagerTest;
Darin Petkovca621542012-07-25 14:25:56 +0200105
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500106 typedef std::map<const std::string, PowerStateCallback>
Gary Morain43bc6272012-01-30 14:01:15 -0800107 StateChangeCallbackMap;
108
Gary Morain52fabab2012-08-22 09:27:31 -0700109 typedef std::map<const std::string, SuspendDelayCallback>
110 SuspendDelayCallbackMap;
111
Darin Petkov3ec55342012-09-28 14:04:44 +0200112 void OnSuspendTimeout();
113
Gary Morain52fabab2012-08-22 09:27:31 -0700114 template<class Callback>
115 void AddCallback(const std::string &key, const Callback &callback,
116 std::map<const std::string, Callback> *callback_map);
117
118 template<class Callback>
119 void RemoveCallback(const std::string &key,
120 std::map<const std::string, Callback> *callback_map);
121
122 template<class Param, class Callback>
123 void OnEvent(const Param &param,
124 std::map<const std::string, Callback> *callback_map) const;
125
Darin Petkov3ec55342012-09-28 14:04:44 +0200126 EventDispatcher *dispatcher_;
Gary Morain52fabab2012-08-22 09:27:31 -0700127
Gary Morain43bc6272012-01-30 14:01:15 -0800128 // The power manager proxy created by this class. It dispatches the inherited
129 // delegate methods of this object when changes in the power state occur.
130 const scoped_ptr<PowerManagerProxyInterface> power_manager_proxy_;
131 StateChangeCallbackMap state_change_callbacks_;
Gary Morain52fabab2012-08-22 09:27:31 -0700132 SuspendDelayCallbackMap suspend_delay_callbacks_;
Darin Petkov3ec55342012-09-28 14:04:44 +0200133 base::CancelableClosure suspend_timeout_;
Gary Morain43bc6272012-01-30 14:01:15 -0800134
Darin Petkovca621542012-07-25 14:25:56 +0200135 SuspendState power_state_;
136
Gary Morain43bc6272012-01-30 14:01:15 -0800137 DISALLOW_COPY_AND_ASSIGN(PowerManager);
138};
139
140} // namespace shill
141
Darin Petkovca621542012-07-25 14:25:56 +0200142#endif // SHILL_POWER_MANAGER_H_