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

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

Issue 7891021: Use stub impl when libcros fails to load (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix power manager stub impl Created 9 years, 3 months 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 | Annotate | Revision Log
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 "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/observer_list.h" 10 #include "base/observer_list.h"
(...skipping 20 matching lines...) Expand all
31 power_status_connection_ = NULL; 31 power_status_connection_ = NULL;
32 } 32 }
33 if (resume_status_connection_) { 33 if (resume_status_connection_) {
34 chromeos::DisconnectResume(resume_status_connection_); 34 chromeos::DisconnectResume(resume_status_connection_);
35 resume_status_connection_ = NULL; 35 resume_status_connection_ = NULL;
36 } 36 }
37 } 37 }
38 38
39 // Begin PowerLibrary implementation. 39 // Begin PowerLibrary implementation.
40 virtual void Init() OVERRIDE { 40 virtual void Init() OVERRIDE {
41 if (CrosLibrary::Get()->EnsureLoaded()) { 41 DCHECK(CrosLibrary::Get()->libcros_loaded());
42 power_status_connection_ = 42 power_status_connection_ =
43 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this); 43 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this);
44 resume_status_connection_ = 44 resume_status_connection_ =
45 chromeos::MonitorResume(&SystemResumedHandler, this); 45 chromeos::MonitorResume(&SystemResumedHandler, this);
46 }
47 } 46 }
48 47
49 virtual void AddObserver(Observer* observer) OVERRIDE { 48 virtual void AddObserver(Observer* observer) OVERRIDE {
50 observers_.AddObserver(observer); 49 observers_.AddObserver(observer);
51 } 50 }
52 51
53 virtual void RemoveObserver(Observer* observer) OVERRIDE { 52 virtual void RemoveObserver(Observer* observer) OVERRIDE {
54 observers_.RemoveObserver(observer); 53 observers_.RemoveObserver(observer);
55 } 54 }
56 55
(...skipping 21 matching lines...) Expand all
78 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); 77 return base::TimeDelta::FromSeconds(status_.battery_time_to_full);
79 } 78 }
80 79
81 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 80 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
82 // TODO(sidor): Examine if it's really a good idea to use void* as a second 81 // TODO(sidor): Examine if it's really a good idea to use void* as a second
83 // argument. 82 // argument.
84 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); 83 chromeos::GetIdleTime(&GetIdleTimeCallback, callback);
85 } 84 }
86 85
87 virtual void EnableScreenLock(bool enable) OVERRIDE { 86 virtual void EnableScreenLock(bool enable) OVERRIDE {
88 if (!CrosLibrary::Get()->EnsureLoaded())
89 return;
90
91 // Make sure we run on FILE thread becuase chromeos::EnableScreenLock 87 // Make sure we run on FILE thread becuase chromeos::EnableScreenLock
92 // would write power manager config file to disk. 88 // would write power manager config file to disk.
93 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { 89 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
94 BrowserThread::PostTask( 90 BrowserThread::PostTask(
95 BrowserThread::FILE, FROM_HERE, 91 BrowserThread::FILE, FROM_HERE,
96 NewRunnableMethod(this, &PowerLibraryImpl::EnableScreenLock, enable)); 92 NewRunnableMethod(this, &PowerLibraryImpl::EnableScreenLock, enable));
97 return; 93 return;
98 } 94 }
99 95
100 chromeos::EnableScreenLock(enable); 96 chromeos::EnableScreenLock(enable);
101 } 97 }
102 98
103 virtual void RequestRestart() OVERRIDE { 99 virtual void RequestRestart() OVERRIDE {
104 if (CrosLibrary::Get()->EnsureLoaded()) 100 chromeos::RequestRestart();
105 chromeos::RequestRestart();
106 } 101 }
107 102
108 virtual void RequestShutdown() OVERRIDE { 103 virtual void RequestShutdown() OVERRIDE {
109 if (CrosLibrary::Get()->EnsureLoaded()) 104 chromeos::RequestShutdown();
110 chromeos::RequestShutdown();
111 } 105 }
106
107 virtual void RequestStatusUpdate() OVERRIDE {
108 PowerInformation information;
109 chromeos::RetrievePowerInformation(&information);
110 UpdatePowerStatus(information.power_status);
111 }
112
112 // End PowerLibrary implementation. 113 // End PowerLibrary implementation.
113 114
114 private: 115 private:
115 static void GetIdleTimeCallback(void* object, 116 static void GetIdleTimeCallback(void* object,
116 int64_t time_idle_ms, 117 int64_t time_idle_ms,
117 bool success) { 118 bool success) {
118 DCHECK(object); 119 DCHECK(object);
119 CalculateIdleTimeCallback* notify = 120 CalculateIdleTimeCallback* notify =
120 static_cast<CalculateIdleTimeCallback*>(object); 121 static_cast<CalculateIdleTimeCallback*>(object);
121 if (success) { 122 if (success) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 184
184 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); 185 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl);
185 }; 186 };
186 187
187 // The stub implementation runs the battery up and down, pausing at the 188 // The stub implementation runs the battery up and down, pausing at the
188 // fully charged and fully depleted states. 189 // fully charged and fully depleted states.
189 class PowerLibraryStubImpl : public PowerLibrary { 190 class PowerLibraryStubImpl : public PowerLibrary {
190 public: 191 public:
191 PowerLibraryStubImpl() 192 PowerLibraryStubImpl()
192 : discharging_(true), 193 : discharging_(true),
193 battery_percentage_(20), 194 battery_percentage_(80),
satorux1 2011/09/16 20:05:55 This seems to be unrelated to the refactoring. Wha
stevenjb 2011/09/16 21:44:24 Will do.
194 pause_count_(0) { 195 pause_count_(0) {
195 timer_.Start(
196 FROM_HERE,
197 base::TimeDelta::FromMilliseconds(100),
198 this,
199 &PowerLibraryStubImpl::Update);
200 } 196 }
201 197
202 virtual ~PowerLibraryStubImpl() {} 198 virtual ~PowerLibraryStubImpl() {}
203 199
204 // Begin PowerLibrary implementation. 200 // Begin PowerLibrary implementation.
205 virtual void Init() OVERRIDE {} 201 virtual void Init() OVERRIDE {}
206 virtual void AddObserver(Observer* observer) OVERRIDE { 202 virtual void AddObserver(Observer* observer) OVERRIDE {
207 observers_.AddObserver(observer); 203 observers_.AddObserver(observer);
208 } 204 }
209 205
(...skipping 28 matching lines...) Expand all
238 if (battery_percentage_ == 100) 234 if (battery_percentage_ == 100)
239 return base::TimeDelta::FromSeconds(1); 235 return base::TimeDelta::FromSeconds(1);
240 else 236 else
241 return base::TimeDelta::FromHours(3) - battery_time_to_empty(); 237 return base::TimeDelta::FromHours(3) - battery_time_to_empty();
242 } 238 }
243 239
244 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 240 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
245 callback->Run(0); 241 callback->Run(0);
246 delete callback; 242 delete callback;
247 } 243 }
244
248 virtual void EnableScreenLock(bool enable) OVERRIDE {} 245 virtual void EnableScreenLock(bool enable) OVERRIDE {}
246
249 virtual void RequestRestart() OVERRIDE {} 247 virtual void RequestRestart() OVERRIDE {}
248
250 virtual void RequestShutdown() OVERRIDE {} 249 virtual void RequestShutdown() OVERRIDE {}
250
251 virtual void RequestStatusUpdate() OVERRIDE {
252 if (!timer_.IsRunning()) {
253 timer_.Start(
254 FROM_HERE,
255 base::TimeDelta::FromMilliseconds(100),
256 this,
257 &PowerLibraryStubImpl::Update);
258 } else {
259 timer_.Stop();
260 }
261 }
262
251 // End PowerLibrary implementation. 263 // End PowerLibrary implementation.
252 264
253 private: 265 private:
254 void Update() { 266 void Update() {
255 // We pause at 0 and 100% so that it's easier to check those conditions. 267 // We pause at 0 and 100% so that it's easier to check those conditions.
256 if (pause_count_ > 1) { 268 if (pause_count_ > 1) {
257 pause_count_--; 269 pause_count_--;
258 return; 270 return;
259 } 271 }
260 272
(...skipping 26 matching lines...) Expand all
287 impl = new PowerLibraryImpl(); 299 impl = new PowerLibraryImpl();
288 impl->Init(); 300 impl->Init();
289 return impl; 301 return impl;
290 } 302 }
291 303
292 } // namespace chromeos 304 } // namespace chromeos
293 305
294 // Allows InvokeLater without adding refcounting. This class is a Singleton and 306 // Allows InvokeLater without adding refcounting. This class is a Singleton and
295 // won't be deleted until it's last InvokeLater is run. 307 // won't be deleted until it's last InvokeLater is run.
296 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); 308 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698