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> |
| 37 | #include <base/memory/scoped_ptr.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 38 | |
| 39 | #include "shill/power_manager_proxy_interface.h" |
| 40 | |
| 41 | namespace shill { |
| 42 | |
| 43 | class ProxyFactory; |
| 44 | |
| 45 | class 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 54 | typedef base::Callback<void(SuspendState)> PowerStateCallback; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 55 | |
| 56 | // |proxy_factory| creates the PowerManagerProxy. Usually this is |
| 57 | // ProxyFactory::GetInstance(). Use a fake for testing. |
| 58 | explicit PowerManager(ProxyFactory *proxy_factory); |
| 59 | virtual ~PowerManager(); |
| 60 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 61 | SuspendState power_state() const { return power_state_; } |
| 62 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 63 | // Methods inherited from PowerManagerProxyDelegate. |
| 64 | virtual void OnSuspendDelay(uint32 sequence_number); |
| 65 | virtual void OnPowerStateChanged(SuspendState new_power_state); |
| 66 | |
| 67 | // TODO(gmorain): Add descriptive comment. |
| 68 | void RegisterSuspendDelay(const uint32_t &delay_ms); |
| 69 | |
| 70 | // Registers a power state change callback with the power manager. When a |
| 71 | // 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] | 72 | // state as its argument. |key| must be unique. |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 73 | virtual void AddStateChangeCallback(const std::string &key, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 74 | const PowerStateCallback &callback); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 75 | |
| 76 | // Unregisters a callback identified by |key|. The callback must have been |
| 77 | // previously registered and not yet removed. The callback is deleted. |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 78 | virtual void RemoveStateChangeCallback(const std::string &key); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 79 | |
| 80 | // TODO(gmorain): Add registration for the OnSuspendDelay event. |
| 81 | |
| 82 | private: |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 83 | friend class ManagerTest; |
| 84 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 85 | typedef std::map<const std::string, PowerStateCallback> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 86 | StateChangeCallbackMap; |
| 87 | |
| 88 | // The power manager proxy created by this class. It dispatches the inherited |
| 89 | // delegate methods of this object when changes in the power state occur. |
| 90 | const scoped_ptr<PowerManagerProxyInterface> power_manager_proxy_; |
| 91 | StateChangeCallbackMap state_change_callbacks_; |
| 92 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 93 | SuspendState power_state_; |
| 94 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(PowerManager); |
| 96 | }; |
| 97 | |
| 98 | } // namespace shill |
| 99 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 100 | #endif // SHILL_POWER_MANAGER_H_ |