blob: 2fea585e0adaeb61f5084204b4ac56dd12aa16ff [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.
Gary Morain43bc6272012-01-30 14:01:15 -080011
12#include <map>
13#include <string>
14
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015#include <base/callback.h>
Darin Petkov3ec55342012-09-28 14:04:44 +020016#include <base/cancelable_callback.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050017#include <base/memory/scoped_ptr.h>
Gary Morain43bc6272012-01-30 14:01:15 -080018
19#include "shill/power_manager_proxy_interface.h"
20
21namespace shill {
22
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070023class DBusManager;
24class DBusNameWatcher;
Darin Petkov3ec55342012-09-28 14:04:44 +020025class EventDispatcher;
Gary Morain43bc6272012-01-30 14:01:15 -080026class ProxyFactory;
27
Ben Chan16d3acb2014-05-13 22:08:14 -070028// 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 Morain43bc6272012-01-30 14:01:15 -080031class PowerManager : public PowerManagerProxyDelegate {
32 public:
Daniel Eratfac09532014-04-17 20:25:59 -070033 // This callback is called prior to a suspend attempt. When it is OK for the
Daniel Erat0818cca2012-12-14 10:16:21 -080034 // system to suspend, this callback should call ReportSuspendReadiness(),
Daniel Eratfac09532014-04-17 20:25:59 -070035 // 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 Morain52fabab2012-08-22 09:27:31 -070043
Darin Petkov3ec55342012-09-28 14:04:44 +020044 static const int kSuspendTimeoutMilliseconds;
45
Gary Morain43bc6272012-01-30 14:01:15 -080046 // |proxy_factory| creates the PowerManagerProxy. Usually this is
47 // ProxyFactory::GetInstance(). Use a fake for testing.
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070048 // Note: |Start| should be called to initialize this object before using it.
Ben Chan16d3acb2014-05-13 22:08:14 -070049 PowerManager(EventDispatcher *dispatcher, ProxyFactory *proxy_factory);
Gary Morain43bc6272012-01-30 14:01:15 -080050 virtual ~PowerManager();
51
Daniel Eratfac09532014-04-17 20:25:59 -070052 bool suspending() const { return suspending_; }
53
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070054 // Requires a |DBusManager| that has been |Start|'ed.
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -070055 virtual void Start(DBusManager *dbus_manager);
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070056
Daniel Eratfac09532014-04-17 20:25:59 -070057 // 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 Petkovca621542012-07-25 14:25:56 +020076
Gary Morain43bc6272012-01-30 14:01:15 -080077 // Methods inherited from PowerManagerProxyDelegate.
Daniel Erat0818cca2012-12-14 10:16:21 -080078 virtual void OnSuspendImminent(int suspend_id);
Daniel Eratfac09532014-04-17 20:25:59 -070079 virtual void OnSuspendDone(int suspend_id);
Gary Morain43bc6272012-01-30 14:01:15 -080080
81 private:
Darin Petkovca621542012-07-25 14:25:56 +020082 friend class ManagerTest;
Darin Petkov3ec55342012-09-28 14:04:44 +020083 friend class PowerManagerTest;
Darin Petkovcb0b5662012-12-13 09:59:44 +010084 friend class ServiceTest;
Darin Petkovca621542012-07-25 14:25:56 +020085
Daniel Eratfac09532014-04-17 20:25:59 -070086 // Information about a suspend delay added via AddSuspendDelay().
87 struct SuspendDelay {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070088 std::string key;
89 std::string description;
90 base::TimeDelta timeout;
Daniel Eratfac09532014-04-17 20:25:59 -070091 SuspendImminentCallback imminent_callback;
92 SuspendDoneCallback done_callback;
93 int delay_id;
94 };
Gary Morain43bc6272012-01-30 14:01:15 -080095
Daniel Eratfac09532014-04-17 20:25:59 -070096 // Keyed by the |key| argument to AddSuspendDelay().
97 typedef std::map<const std::string, SuspendDelay> SuspendDelayMap;
Gary Morain52fabab2012-08-22 09:27:31 -070098
Daniel Eratfac09532014-04-17 20:25:59 -070099 // Run by |suspend_timeout_| to manually invoke OnSuspendDone() if the signal
100 // never arrives.
101 void OnSuspendTimeout(int suspend_id);
Gary Morain52fabab2012-08-22 09:27:31 -0700102
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700103 // 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 Petkov3ec55342012-09-28 14:04:44 +0200109 EventDispatcher *dispatcher_;
Gary Morain52fabab2012-08-22 09:27:31 -0700110
Gary Morain43bc6272012-01-30 14:01:15 -0800111 // 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 Prabhud9c786c2014-04-23 17:37:28 -0700114 scoped_ptr<DBusNameWatcher> power_manager_name_watcher_;
Daniel Eratfac09532014-04-17 20:25:59 -0700115 SuspendDelayMap suspend_delays_;
Darin Petkov3ec55342012-09-28 14:04:44 +0200116 base::CancelableClosure suspend_timeout_;
Gary Morain43bc6272012-01-30 14:01:15 -0800117
Daniel Eratfac09532014-04-17 20:25:59 -0700118 // Set to true by OnSuspendImminent() and to false by OnSuspendDone().
119 bool suspending_;
Darin Petkovca621542012-07-25 14:25:56 +0200120
Gary Morain43bc6272012-01-30 14:01:15 -0800121 DISALLOW_COPY_AND_ASSIGN(PowerManager);
122};
123
124} // namespace shill
125
Darin Petkovca621542012-07-25 14:25:56 +0200126#endif // SHILL_POWER_MANAGER_H_