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 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 5 | #ifndef SHILL_POWER_MANAGER_H_ |
| 6 | #define SHILL_POWER_MANAGER_H_ |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 7 | |
| 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 22 | // PowerManager::PowerStateCallback cb = Bind(&Foo::HandleStateChange, &foo); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 23 | // power_manager.AddStateChangeCallback("foo_key", cb); |
| 24 | // |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 25 | // Note that depending on the definition of Foo, "&foo" may need to appear |
| 26 | // inside an appropriate wrapper, such as base::Unretained. |
| 27 | // |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 28 | // Whenever the power state changes, foo.HandleStateChange() is called with the |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 29 | // new state passed in. To unregister: |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 30 | // |
| 31 | // power_manager.RemoveStateChangeCallback("foo_key"); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 32 | |
| 33 | #include <map> |
| 34 | #include <string> |
| 35 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 36 | #include <base/callback.h> |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 37 | #include <base/cancelable_callback.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 38 | #include <base/memory/scoped_ptr.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 39 | |
| 40 | #include "shill/power_manager_proxy_interface.h" |
| 41 | |
| 42 | namespace shill { |
| 43 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 44 | class EventDispatcher; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 45 | class ProxyFactory; |
| 46 | |
| 47 | class 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 56 | typedef base::Callback<void(SuspendState)> PowerStateCallback; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 57 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 58 | // This callback is called prior to a suspend event. When it is OK for the |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 59 | // system to suspend, this callback should call ReportSuspendReadiness(), |
| 60 | // passing it the suspend ID passed to this callback. |
| 61 | typedef base::Callback<void(int)> SuspendDelayCallback; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 62 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 63 | static const int kSuspendTimeoutMilliseconds; |
| 64 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 65 | // |proxy_factory| creates the PowerManagerProxy. Usually this is |
| 66 | // ProxyFactory::GetInstance(). Use a fake for testing. |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 67 | PowerManager(EventDispatcher *dispatcher, |
| 68 | ProxyFactory *proxy_factory); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 69 | virtual ~PowerManager(); |
| 70 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 71 | SuspendState power_state() const { return power_state_; } |
| 72 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 73 | // Methods inherited from PowerManagerProxyDelegate. |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 74 | virtual void OnSuspendImminent(int suspend_id); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 75 | virtual void OnPowerStateChanged(SuspendState new_power_state); |
| 76 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 77 | // See corresponding methods in PowerManagerProxyInterface. |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 78 | virtual bool RegisterSuspendDelay(base::TimeDelta timeout, int *delay_id_out); |
| 79 | virtual bool UnregisterSuspendDelay(int delay_id); |
| 80 | virtual bool ReportSuspendReadiness(int delay_id, int suspend_id); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 81 | |
| 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 84 | // state as its argument. |key| must be unique. |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 85 | virtual void AddStateChangeCallback(const std::string &key, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 86 | const PowerStateCallback &callback); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 87 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 88 | // 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 Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 96 | virtual void RemoveStateChangeCallback(const std::string &key); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 97 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 98 | // 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 Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 101 | |
| 102 | private: |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 103 | friend class ManagerTest; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 104 | friend class PowerManagerTest; |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 105 | friend class ServiceTest; |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 106 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 107 | typedef std::map<const std::string, PowerStateCallback> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 108 | StateChangeCallbackMap; |
| 109 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 110 | typedef std::map<const std::string, SuspendDelayCallback> |
| 111 | SuspendDelayCallbackMap; |
| 112 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 113 | void OnSuspendTimeout(); |
| 114 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 115 | template<class Callback> |
| 116 | void AddCallback(const std::string &key, const Callback &callback, |
| 117 | std::map<const std::string, Callback> *callback_map); |
| 118 | |
| 119 | template<class Callback> |
| 120 | void RemoveCallback(const std::string &key, |
| 121 | std::map<const std::string, Callback> *callback_map); |
| 122 | |
| 123 | template<class Param, class Callback> |
| 124 | void OnEvent(const Param ¶m, |
| 125 | std::map<const std::string, Callback> *callback_map) const; |
| 126 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 127 | EventDispatcher *dispatcher_; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 128 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 129 | // The power manager proxy created by this class. It dispatches the inherited |
| 130 | // delegate methods of this object when changes in the power state occur. |
| 131 | const scoped_ptr<PowerManagerProxyInterface> power_manager_proxy_; |
| 132 | StateChangeCallbackMap state_change_callbacks_; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 133 | SuspendDelayCallbackMap suspend_delay_callbacks_; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 134 | base::CancelableClosure suspend_timeout_; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 135 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 136 | SuspendState power_state_; |
| 137 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(PowerManager); |
| 139 | }; |
| 140 | |
| 141 | } // namespace shill |
| 142 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 143 | #endif // SHILL_POWER_MANAGER_H_ |