Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: chrome/browser/chromeos/cros/power_library.cc

Issue 8271024: chromeos: Add power supply info reading capability (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Various comment fixes, rearranged initialization order Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/cros/power_library.h" 5 #include "chrome/browser/chromeos/cros/power_library.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/observer_list.h" 13 #include "base/observer_list.h"
14 #include "base/time.h"
15 #include "base/timer.h"
16 #include "chrome/browser/chromeos/cros/cros_library.h" 14 #include "chrome/browser/chromeos/cros/cros_library.h"
17 #include "content/browser/browser_thread.h" 15 #include "content/public/browser_thread.h"
18 #include "third_party/cros/chromeos_power.h" 16 #include "third_party/cros/chromeos_power.h"
19 #include "third_party/cros/chromeos_resume.h" 17 #include "third_party/cros/chromeos_resume.h"
20 18
21 namespace chromeos { 19 namespace chromeos {
22 20
23 class PowerLibraryImpl : public PowerLibrary { 21 class PowerLibraryImpl : public PowerLibrary {
24 public: 22 public:
25 PowerLibraryImpl() 23 PowerLibraryImpl()
26 : power_status_connection_(NULL), 24 : resume_status_connection_(NULL) {
27 resume_status_connection_(NULL) {
28 } 25 }
29 26
30 virtual ~PowerLibraryImpl() { 27 virtual ~PowerLibraryImpl() {
31 if (power_status_connection_) {
32 chromeos::DisconnectPowerStatus(power_status_connection_);
33 power_status_connection_ = NULL;
34 }
35 if (resume_status_connection_) { 28 if (resume_status_connection_) {
36 chromeos::DisconnectResume(resume_status_connection_); 29 chromeos::DisconnectResume(resume_status_connection_);
37 resume_status_connection_ = NULL; 30 resume_status_connection_ = NULL;
38 } 31 }
39 } 32 }
40 33
41 // Begin PowerLibrary implementation. 34 // Begin PowerLibrary implementation.
42 virtual void Init() OVERRIDE { 35 virtual void Init() OVERRIDE {
43 DCHECK(CrosLibrary::Get()->libcros_loaded()); 36 DCHECK(CrosLibrary::Get()->libcros_loaded());
44 power_status_connection_ =
45 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this);
46 resume_status_connection_ = 37 resume_status_connection_ =
47 chromeos::MonitorResume(&SystemResumedHandler, this); 38 chromeos::MonitorResume(&SystemResumedHandler, this);
48 } 39 }
49 40
50 virtual void AddObserver(Observer* observer) OVERRIDE { 41 virtual void AddObserver(Observer* observer) OVERRIDE {
51 observers_.AddObserver(observer); 42 observers_.AddObserver(observer);
52 } 43 }
53 44
54 virtual void RemoveObserver(Observer* observer) OVERRIDE { 45 virtual void RemoveObserver(Observer* observer) OVERRIDE {
55 observers_.RemoveObserver(observer); 46 observers_.RemoveObserver(observer);
56 } 47 }
57 48
58 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 49 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
59 // TODO(sidor): Examine if it's really a good idea to use void* as a second 50 // TODO(sidor): Examine if it's really a good idea to use void* as a second
60 // argument. 51 // argument.
61 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); 52 chromeos::GetIdleTime(&GetIdleTimeCallback, callback);
62 } 53 }
63 54
64 virtual void EnableScreenLock(bool enable) OVERRIDE { 55 virtual void EnableScreenLock(bool enable) OVERRIDE {
65 // Called when the screen preference is changed, which should always 56 // Make sure we run on FILE thread because chromeos::EnableScreenLock
66 // run on UI thread.
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 // Post the task to FILE thread as chromeos::EnableScreenLock
69 // would write power manager config file to disk. 57 // would write power manager config file to disk.
70 BrowserThread::PostTask( 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
71 BrowserThread::FILE, FROM_HERE, 59
72 base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); 60 chromeos::EnableScreenLock(enable);
73 } 61 }
74 62
75 virtual void RequestRestart() OVERRIDE { 63 virtual void RequestRestart() OVERRIDE {
76 chromeos::RequestRestart(); 64 chromeos::RequestRestart();
77 } 65 }
78 66
79 virtual void RequestShutdown() OVERRIDE { 67 virtual void RequestShutdown() OVERRIDE {
80 chromeos::RequestShutdown(); 68 chromeos::RequestShutdown();
81 } 69 }
82 70
83 virtual void RequestStatusUpdate() OVERRIDE { 71 virtual void RequestStatusUpdate() OVERRIDE {
84 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; 72 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated;
85 // we should add a mechanism to immediately request an update, probably 73 // we should add a mechanism to immediately request an update, probably
86 // when we migrate the DBus code from libcros to here. 74 // when we migrate the DBus code from libcros to here.
87 } 75 }
88
89 // End PowerLibrary implementation. 76 // End PowerLibrary implementation.
90 77
91 private: 78 private:
92 static void DoEnableScreenLock(bool enable) { 79 static void DoEnableScreenLock(bool enable) {
93 chromeos::EnableScreenLock(enable); 80 chromeos::EnableScreenLock(enable);
94 } 81 }
95 82
96 static void GetIdleTimeCallback(void* object, 83 static void GetIdleTimeCallback(void* object,
97 int64_t time_idle_ms, 84 int64_t time_idle_ms,
98 bool success) { 85 bool success) {
99 DCHECK(object); 86 DCHECK(object);
100 CalculateIdleTimeCallback* notify = 87 CalculateIdleTimeCallback* notify =
101 static_cast<CalculateIdleTimeCallback*>(object); 88 static_cast<CalculateIdleTimeCallback*>(object);
102 if (success) { 89 if (success) {
103 notify->Run(time_idle_ms/1000); 90 notify->Run(time_idle_ms/1000);
104 } else { 91 } else {
105 LOG(ERROR) << "Power manager failed to calculate idle time."; 92 LOG(ERROR) << "Power manager failed to calculate idle time.";
106 notify->Run(-1); 93 notify->Run(-1);
107 } 94 }
108 delete notify; 95 delete notify;
109 } 96 }
110 97
111 static void PowerStatusChangedHandler(
112 void* object, const chromeos::PowerStatus& power_status) {
113 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros
114 // will be removed and this will not be necessary.
115 PowerSupplyStatus status;
116 status.line_power_on = power_status.line_power_on;
117 status.battery_is_present = power_status.battery_is_present;
118 status.battery_is_full =
119 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED);
120 status.battery_seconds_to_empty = power_status.battery_time_to_empty;
121 status.battery_seconds_to_full = power_status.battery_time_to_full;
122 status.battery_percentage = power_status.battery_percentage;
123
124 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object);
125 power->UpdatePowerStatus(status);
126 }
127
128 static void SystemResumedHandler(void* object) { 98 static void SystemResumedHandler(void* object) {
129 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); 99 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object);
130 power->SystemResumed(); 100 power->SystemResumed();
131 } 101 }
132 102
133 void UpdatePowerStatus(const PowerSupplyStatus& status) {
134 // Called from PowerStatusChangedHandler, a libcros callback which
135 // should always run on UI thread.
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
137
138 DVLOG(1) << "Power line_power_on = " << status.line_power_on
139 << " percentage = " << status.battery_percentage
140 << " seconds_to_empty = " << status.battery_seconds_to_empty
141 << " seconds_to_full = " << status.battery_seconds_to_full;
142 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
143 }
144
145 void SystemResumed() { 103 void SystemResumed() {
146 // Called from SystemResumedHandler, a libcros callback which should 104 // Called from SystemResumedHandler, a libcros callback which should
147 // always run on UI thread. 105 // always run on UI thread.
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
107
149 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); 108 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed());
150 } 109 }
151 110
152 ObserverList<Observer> observers_; 111 ObserverList<Observer> observers_;
153 112
154 // A reference to the power battery API, to allow callbacks when the battery
155 // status changes.
156 chromeos::PowerStatusConnection power_status_connection_;
157
158 // A reference to the resume alerts. 113 // A reference to the resume alerts.
159 chromeos::ResumeConnection resume_status_connection_; 114 chromeos::ResumeConnection resume_status_connection_;
160 115
161 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); 116 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl);
162 }; 117 };
163 118
164 // The stub implementation runs the battery up and down, pausing at the 119 // The stub implementation runs the battery up and down, pausing at the
165 // fully charged and fully depleted states. 120 // fully charged and fully depleted states.
166 class PowerLibraryStubImpl : public PowerLibrary { 121 class PowerLibraryStubImpl : public PowerLibrary {
167 public: 122 public:
168 PowerLibraryStubImpl() 123 PowerLibraryStubImpl() {
169 : discharging_(true),
170 battery_percentage_(80),
171 pause_count_(0) {
172 } 124 }
173 125
174 virtual ~PowerLibraryStubImpl() {} 126 virtual ~PowerLibraryStubImpl() {}
175 127
176 // Begin PowerLibrary implementation. 128 // Begin PowerLibrary implementation.
177 virtual void Init() OVERRIDE {} 129 virtual void Init() OVERRIDE {}
178 virtual void AddObserver(Observer* observer) OVERRIDE { 130 virtual void AddObserver(Observer* observer) OVERRIDE {
179 observers_.AddObserver(observer); 131 observers_.AddObserver(observer);
180 } 132 }
181 133
182 virtual void RemoveObserver(Observer* observer) OVERRIDE { 134 virtual void RemoveObserver(Observer* observer) OVERRIDE {
183 observers_.RemoveObserver(observer); 135 observers_.RemoveObserver(observer);
184 } 136 }
185 137
186 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 138 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
187 callback->Run(0); 139 callback->Run(0);
188 delete callback; 140 delete callback;
189 } 141 }
190 142
191 virtual void EnableScreenLock(bool enable) OVERRIDE {} 143 virtual void EnableScreenLock(bool enable) OVERRIDE {}
192 144
193 virtual void RequestRestart() OVERRIDE {} 145 virtual void RequestRestart() OVERRIDE {}
194 146
195 virtual void RequestShutdown() OVERRIDE {} 147 virtual void RequestShutdown() OVERRIDE {}
196 148
197 virtual void RequestStatusUpdate() OVERRIDE {
198 if (!timer_.IsRunning()) {
199 timer_.Start(
200 FROM_HERE,
201 base::TimeDelta::FromMilliseconds(100),
202 this,
203 &PowerLibraryStubImpl::Update);
204 } else {
205 timer_.Stop();
206 }
207 }
208
209 // End PowerLibrary implementation. 149 // End PowerLibrary implementation.
210
211 private: 150 private:
212 void Update() {
213 // We pause at 0 and 100% so that it's easier to check those conditions.
214 if (pause_count_ > 1) {
215 pause_count_--;
216 return;
217 }
218
219 if (battery_percentage_ == 0 || battery_percentage_ == 100) {
220 if (pause_count_) {
221 pause_count_ = 0;
222 discharging_ = !discharging_;
223 } else {
224 pause_count_ = 20;
225 return;
226 }
227 }
228 battery_percentage_ += (discharging_ ? -1 : 1);
229
230 PowerSupplyStatus status;
231 status.line_power_on = !discharging_;
232 status.battery_is_present = true;
233 status.battery_percentage = battery_percentage_;
234 status.battery_seconds_to_empty =
235 std::max(1, battery_percentage_ * 180 / 100);
236 status.battery_seconds_to_full =
237 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty);
238
239 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
240 }
241
242 bool discharging_;
243 int battery_percentage_;
244 int pause_count_;
245 ObserverList<Observer> observers_; 151 ObserverList<Observer> observers_;
246 base::RepeatingTimer<PowerLibraryStubImpl> timer_;
247 }; 152 };
248 153
249 // static 154 // static
250 PowerLibrary* PowerLibrary::GetImpl(bool stub) { 155 PowerLibrary* PowerLibrary::GetImpl(bool stub) {
251 PowerLibrary* impl; 156 PowerLibrary* impl;
252 if (stub) 157 if (stub)
253 impl = new PowerLibraryStubImpl(); 158 impl = new PowerLibraryStubImpl();
254 else 159 else
255 impl = new PowerLibraryImpl(); 160 impl = new PowerLibraryImpl();
256 impl->Init(); 161 impl->Init();
257 return impl; 162 return impl;
258 } 163 }
259 164
260 } // namespace chromeos 165 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698