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. |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
| 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/callback.h> |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 16 | #include <base/cancelable_callback.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 17 | #include <base/memory/scoped_ptr.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 18 | |
| 19 | #include "shill/power_manager_proxy_interface.h" |
| 20 | |
| 21 | namespace shill { |
| 22 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 23 | class DBusManager; |
| 24 | class DBusNameWatcher; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 25 | class EventDispatcher; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 26 | class ProxyFactory; |
| 27 | |
Ben Chan | 16d3acb | 2014-05-13 22:08:14 -0700 | [diff] [blame] | 28 | // TODO(benchan): The current implementation supports multiple suspend delays, |
| 29 | // which seems unnecessary as shill only registers one. We should simplify the |
| 30 | // implementation (crbug.com/373348). |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 31 | class PowerManager : public PowerManagerProxyDelegate { |
| 32 | public: |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 33 | // This callback is called prior to a suspend attempt. When it is OK for the |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 34 | // system to suspend, this callback should call ReportSuspendReadiness(), |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 35 | // passing it the key passed to AddSuspendDelay() and the suspend ID passed to |
| 36 | // this callback. |
| 37 | typedef base::Callback<void(int)> SuspendImminentCallback; |
| 38 | |
| 39 | // This callback is called after the completion of a suspend attempt. The |
| 40 | // receiver should undo any pre-suspend work that was done by the |
| 41 | // SuspendImminentCallback. |
| 42 | typedef base::Callback<void(int)> SuspendDoneCallback; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 43 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 44 | static const int kSuspendTimeoutMilliseconds; |
| 45 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 46 | // |proxy_factory| creates the PowerManagerProxy. Usually this is |
| 47 | // ProxyFactory::GetInstance(). Use a fake for testing. |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 48 | // Note: |Start| should be called to initialize this object before using it. |
Ben Chan | 16d3acb | 2014-05-13 22:08:14 -0700 | [diff] [blame] | 49 | PowerManager(EventDispatcher *dispatcher, ProxyFactory *proxy_factory); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 50 | virtual ~PowerManager(); |
| 51 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 52 | bool suspending() const { return suspending_; } |
| 53 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 54 | // Requires a |DBusManager| that has been |Start|'ed. |
mukesh agrawal | 6eb4f2c | 2014-07-01 14:31:10 -0700 | [diff] [blame] | 55 | virtual void Start(DBusManager *dbus_manager); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 56 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 57 | // Registers a suspend delay with the power manager under the unique name |
| 58 | // |key|. See PowerManagerProxyInterface::RegisterSuspendDelay() for |
| 59 | // information about |description| and |timeout|. |imminent_callback| will be |
| 60 | // invoked when a suspend attempt is commenced and |done_callback| will be |
| 61 | // invoked when the attempt is completed. Returns false on failure. |
| 62 | virtual bool AddSuspendDelay(const std::string &key, |
| 63 | const std::string &description, |
| 64 | base::TimeDelta timeout, |
| 65 | const SuspendImminentCallback &immiment_callback, |
| 66 | const SuspendDoneCallback &done_callback); |
| 67 | |
| 68 | // Removes a suspend delay previously created via AddSuspendDelay(). Returns |
| 69 | // false on failure. |
| 70 | virtual bool RemoveSuspendDelay(const std::string &key); |
| 71 | |
| 72 | // Reports readiness for suspend attempt |suspend_id| on behalf of the suspend |
| 73 | // delay described by |key|, the value originally passed to AddSuspendDelay(). |
| 74 | // Returns false on failure. |
| 75 | virtual bool ReportSuspendReadiness(const std::string &key, int suspend_id); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 76 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 77 | // Methods inherited from PowerManagerProxyDelegate. |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 78 | virtual void OnSuspendImminent(int suspend_id); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 79 | virtual void OnSuspendDone(int suspend_id); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 82 | friend class ManagerTest; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 83 | friend class PowerManagerTest; |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 84 | friend class ServiceTest; |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 85 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 86 | // Information about a suspend delay added via AddSuspendDelay(). |
| 87 | struct SuspendDelay { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 88 | std::string key; |
| 89 | std::string description; |
| 90 | base::TimeDelta timeout; |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 91 | SuspendImminentCallback imminent_callback; |
| 92 | SuspendDoneCallback done_callback; |
| 93 | int delay_id; |
| 94 | }; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 95 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 96 | // Keyed by the |key| argument to AddSuspendDelay(). |
| 97 | typedef std::map<const std::string, SuspendDelay> SuspendDelayMap; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 98 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 99 | // Run by |suspend_timeout_| to manually invoke OnSuspendDone() if the signal |
| 100 | // never arrives. |
| 101 | void OnSuspendTimeout(int suspend_id); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 102 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 103 | // These functions track the power_manager daemon appearing/vanishing from the |
| 104 | // DBus connection. |
| 105 | void OnPowerManagerAppeared(const std::string &name, |
| 106 | const std::string &owner); |
| 107 | void OnPowerManagerVanished(const std::string &name); |
| 108 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 109 | EventDispatcher *dispatcher_; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 110 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 111 | // The power manager proxy created by this class. It dispatches the inherited |
| 112 | // delegate methods of this object when changes in the power state occur. |
| 113 | const scoped_ptr<PowerManagerProxyInterface> power_manager_proxy_; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 114 | scoped_ptr<DBusNameWatcher> power_manager_name_watcher_; |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 115 | SuspendDelayMap suspend_delays_; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 116 | base::CancelableClosure suspend_timeout_; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 117 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 118 | // Set to true by OnSuspendImminent() and to false by OnSuspendDone(). |
| 119 | bool suspending_; |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 120 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(PowerManager); |
| 122 | }; |
| 123 | |
| 124 | } // namespace shill |
| 125 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 126 | #endif // SHILL_POWER_MANAGER_H_ |