OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/power_save_blocker_impl.h" | 5 #include "content/browser/power_save_blocker_impl.h" |
6 | 6 |
7 #include <X11/Xlib.h> | 7 #include <X11/Xlib.h> |
8 #include <X11/extensions/dpms.h> | 8 #include <X11/extensions/dpms.h> |
9 // Xlib #defines Status, but we can't have that for some of our headers. | 9 // Xlib #defines Status, but we can't have that for some of our headers. |
10 #ifdef Status | 10 #ifdef Status |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 82 |
83 // Selects an appropriate D-Bus API to use for this object. Must be called on | 83 // Selects an appropriate D-Bus API to use for this object. Must be called on |
84 // the UI thread. Checks enqueue_apply_ once an API has been selected, and | 84 // the UI thread. Checks enqueue_apply_ once an API has been selected, and |
85 // enqueues a call back to ApplyBlock() if it is true. See the comments for | 85 // enqueues a call back to ApplyBlock() if it is true. See the comments for |
86 // enqueue_apply_ below. | 86 // enqueue_apply_ below. |
87 void InitOnUIThread(); | 87 void InitOnUIThread(); |
88 | 88 |
89 // Apply or remove the power save block, respectively. These methods should be | 89 // Apply or remove the power save block, respectively. These methods should be |
90 // called once each, on the same thread, per instance. They block waiting for | 90 // called once each, on the same thread, per instance. They block waiting for |
91 // the action to complete (with a timeout); the thread must thus allow I/O. | 91 // the action to complete (with a timeout); the thread must thus allow I/O. |
92 void ApplyBlock(DBusAPI api); | 92 void ApplyBlock(); |
93 void RemoveBlock(DBusAPI api); | 93 void RemoveBlock(); |
94 | 94 |
95 // Asynchronous callback functions for ApplyBlock and RemoveBlock. | 95 // Asynchronous callback functions for ApplyBlock and RemoveBlock. |
96 // Functions do not receive ownership of |response|. | 96 // Functions do not receive ownership of |response|. |
97 void ApplyBlockFinished(DBusAPI api, dbus::Response* response); | 97 void ApplyBlockFinished(dbus::Response* response); |
98 void RemoveBlockFinished(dbus::Response* response); | 98 void RemoveBlockFinished(dbus::Response* response); |
99 | 99 |
100 // If DPMS (the power saving system in X11) is not enabled, then we don't want | 100 // If DPMS (the power saving system in X11) is not enabled, then we don't want |
101 // to try to disable power saving, since on some desktop environments that may | 101 // to try to disable power saving, since on some desktop environments that may |
102 // enable DPMS with very poor default settings (e.g. turning off the display | 102 // enable DPMS with very poor default settings (e.g. turning off the display |
103 // after only 1 second). Must be called on the UI thread. | 103 // after only 1 second). Must be called on the UI thread. |
104 static bool DPMSEnabled(); | 104 static bool DPMSEnabled(); |
105 | 105 |
106 // Returns an appropriate D-Bus API to use based on the desktop environment. | 106 // Returns an appropriate D-Bus API to use based on the desktop environment. |
107 // Must be called on the UI thread, as it may call DPMSEnabled() above. | 107 // Must be called on the UI thread, as it may call DPMSEnabled() above. |
(...skipping 17 matching lines...) Expand all Loading... |
125 bool unblock_inflight_; | 125 bool unblock_inflight_; |
126 // Indicates that RemoveBlock() is called before ApplyBlock() has finished. | 126 // Indicates that RemoveBlock() is called before ApplyBlock() has finished. |
127 // If it's true, then the RemoveBlock() call will be processed immediately | 127 // If it's true, then the RemoveBlock() call will be processed immediately |
128 // after ApplyBlock() has finished. | 128 // after ApplyBlock() has finished. |
129 bool enqueue_unblock_; | 129 bool enqueue_unblock_; |
130 | 130 |
131 scoped_refptr<dbus::Bus> bus_; | 131 scoped_refptr<dbus::Bus> bus_; |
132 | 132 |
133 // The cookie that identifies our inhibit request, | 133 // The cookie that identifies our inhibit request, |
134 // or 0 if there is no active inhibit request. | 134 // or 0 if there is no active inhibit request. |
135 uint32 inhibit_cookie_; | 135 uint32_t inhibit_cookie_; |
136 | 136 |
137 DISALLOW_COPY_AND_ASSIGN(Delegate); | 137 DISALLOW_COPY_AND_ASSIGN(Delegate); |
138 }; | 138 }; |
139 | 139 |
140 PowerSaveBlockerImpl::Delegate::Delegate(PowerSaveBlockerType type, | 140 PowerSaveBlockerImpl::Delegate::Delegate(PowerSaveBlockerType type, |
141 const std::string& description) | 141 const std::string& description) |
142 : type_(type), | 142 : type_(type), |
143 description_(description), | 143 description_(description), |
144 api_(NO_API), | 144 api_(NO_API), |
145 enqueue_apply_(false), | 145 enqueue_apply_(false), |
(...skipping 15 matching lines...) Expand all Loading... |
161 | 161 |
162 void PowerSaveBlockerImpl::Delegate::CleanUp() { | 162 void PowerSaveBlockerImpl::Delegate::CleanUp() { |
163 base::AutoLock lock(lock_); | 163 base::AutoLock lock(lock_); |
164 if (enqueue_apply_) { | 164 if (enqueue_apply_) { |
165 // If a call to ApplyBlock() has not yet been enqueued because we are still | 165 // If a call to ApplyBlock() has not yet been enqueued because we are still |
166 // initializing on the UI thread, then just cancel it. We don't need to | 166 // initializing on the UI thread, then just cancel it. We don't need to |
167 // remove the block because we haven't even applied it yet. | 167 // remove the block because we haven't even applied it yet. |
168 enqueue_apply_ = false; | 168 enqueue_apply_ = false; |
169 } else if (api_ != NO_API) { | 169 } else if (api_ != NO_API) { |
170 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 170 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
171 base::Bind(&Delegate::RemoveBlock, this, api_)); | 171 base::Bind(&Delegate::RemoveBlock, this)); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 void PowerSaveBlockerImpl::Delegate::InitOnUIThread() { | 175 void PowerSaveBlockerImpl::Delegate::InitOnUIThread() { |
176 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
177 base::AutoLock lock(lock_); | 177 base::AutoLock lock(lock_); |
178 api_ = SelectAPI(); | 178 api_ = SelectAPI(); |
179 if (enqueue_apply_ && api_ != NO_API) { | 179 if (enqueue_apply_ && api_ != NO_API) { |
180 // The thread we use here becomes the origin and D-Bus thread for the D-Bus | 180 // The thread we use here becomes the origin and D-Bus thread for the D-Bus |
181 // library, so we need to use the same thread above for RemoveBlock(). It | 181 // library, so we need to use the same thread above for RemoveBlock(). It |
182 // must be a thread that allows I/O operations, so we use the FILE thread. | 182 // must be a thread that allows I/O operations, so we use the FILE thread. |
183 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 183 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
184 base::Bind(&Delegate::ApplyBlock, this, api_)); | 184 base::Bind(&Delegate::ApplyBlock, this)); |
185 } | 185 } |
186 enqueue_apply_ = false; | 186 enqueue_apply_ = false; |
187 } | 187 } |
188 | 188 |
189 void PowerSaveBlockerImpl::Delegate::ApplyBlock(DBusAPI api) { | 189 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
190 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 190 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
191 DCHECK(!bus_); // ApplyBlock() should only be called once. | 191 DCHECK(!bus_); // ApplyBlock() should only be called once. |
192 DCHECK(!block_inflight_); | 192 DCHECK(!block_inflight_); |
193 | 193 |
194 dbus::Bus::Options options; | 194 dbus::Bus::Options options; |
195 options.bus_type = dbus::Bus::SESSION; | 195 options.bus_type = dbus::Bus::SESSION; |
196 options.connection_type = dbus::Bus::PRIVATE; | 196 options.connection_type = dbus::Bus::PRIVATE; |
197 bus_ = new dbus::Bus(options); | 197 bus_ = new dbus::Bus(options); |
198 | 198 |
199 scoped_refptr<dbus::ObjectProxy> object_proxy; | 199 scoped_refptr<dbus::ObjectProxy> object_proxy; |
200 scoped_ptr<dbus::MethodCall> method_call; | 200 scoped_ptr<dbus::MethodCall> method_call; |
201 scoped_ptr<dbus::MessageWriter> message_writer; | 201 scoped_ptr<dbus::MessageWriter> message_writer; |
202 | 202 |
203 switch (api) { | 203 switch (api_) { |
204 case NO_API: | 204 case NO_API: |
205 NOTREACHED(); // We should never call this method with this value. | 205 NOTREACHED(); // We should never call this method with this value. |
206 return; | 206 return; |
207 case GNOME_API: | 207 case GNOME_API: |
208 object_proxy = bus_->GetObjectProxy( | 208 object_proxy = bus_->GetObjectProxy( |
209 kGnomeAPIServiceName, | 209 kGnomeAPIServiceName, |
210 dbus::ObjectPath(kGnomeAPIObjectPath)); | 210 dbus::ObjectPath(kGnomeAPIObjectPath)); |
211 method_call.reset( | 211 method_call.reset( |
212 new dbus::MethodCall(kGnomeAPIInterfaceName, "Inhibit")); | 212 new dbus::MethodCall(kGnomeAPIInterfaceName, "Inhibit")); |
213 message_writer.reset(new dbus::MessageWriter(method_call.get())); | 213 message_writer.reset(new dbus::MessageWriter(method_call.get())); |
214 // The arguments of the method are: | 214 // The arguments of the method are: |
215 // app_id: The application identifier | 215 // app_id: The application identifier |
216 // toplevel_xid: The toplevel X window identifier | 216 // toplevel_xid: The toplevel X window identifier |
217 // reason: The reason for the inhibit | 217 // reason: The reason for the inhibit |
218 // flags: Flags that spefify what should be inhibited | 218 // flags: Flags that spefify what should be inhibited |
219 message_writer->AppendString( | 219 message_writer->AppendString( |
220 base::CommandLine::ForCurrentProcess()->GetProgram().value()); | 220 base::CommandLine::ForCurrentProcess()->GetProgram().value()); |
221 message_writer->AppendUint32(0); // should be toplevel_xid | 221 message_writer->AppendUint32(0); // should be toplevel_xid |
222 message_writer->AppendString(description_); | 222 message_writer->AppendString(description_); |
223 { | 223 { |
224 uint32 flags = 0; | 224 uint32_t flags = 0; |
225 switch (type_) { | 225 switch (type_) { |
226 case kPowerSaveBlockPreventDisplaySleep: | 226 case kPowerSaveBlockPreventDisplaySleep: |
227 flags |= INHIBIT_MARK_SESSION_IDLE; | 227 flags |= INHIBIT_MARK_SESSION_IDLE; |
228 flags |= INHIBIT_SUSPEND_SESSION; | 228 flags |= INHIBIT_SUSPEND_SESSION; |
229 break; | 229 break; |
230 case kPowerSaveBlockPreventAppSuspension: | 230 case kPowerSaveBlockPreventAppSuspension: |
231 flags |= INHIBIT_SUSPEND_SESSION; | 231 flags |= INHIBIT_SUSPEND_SESSION; |
232 break; | 232 break; |
233 } | 233 } |
234 message_writer->AppendUint32(flags); | 234 message_writer->AppendUint32(flags); |
(...skipping 11 matching lines...) Expand all Loading... |
246 // reason: The reason for the inhibit | 246 // reason: The reason for the inhibit |
247 message_writer->AppendString( | 247 message_writer->AppendString( |
248 base::CommandLine::ForCurrentProcess()->GetProgram().value()); | 248 base::CommandLine::ForCurrentProcess()->GetProgram().value()); |
249 message_writer->AppendString(description_); | 249 message_writer->AppendString(description_); |
250 break; | 250 break; |
251 } | 251 } |
252 | 252 |
253 block_inflight_ = true; | 253 block_inflight_ = true; |
254 object_proxy->CallMethod( | 254 object_proxy->CallMethod( |
255 method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 255 method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
256 base::Bind(&PowerSaveBlockerImpl::Delegate::ApplyBlockFinished, this, | 256 base::Bind(&PowerSaveBlockerImpl::Delegate::ApplyBlockFinished, this)); |
257 api)); | |
258 } | 257 } |
259 | 258 |
260 void PowerSaveBlockerImpl::Delegate::ApplyBlockFinished( | 259 void PowerSaveBlockerImpl::Delegate::ApplyBlockFinished( |
261 DBusAPI api, | |
262 dbus::Response* response) { | 260 dbus::Response* response) { |
263 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 261 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
264 DCHECK(bus_); | 262 DCHECK(bus_); |
265 DCHECK(block_inflight_); | 263 DCHECK(block_inflight_); |
266 block_inflight_ = false; | 264 block_inflight_ = false; |
267 | 265 |
268 if (response) { | 266 if (response) { |
269 // The method returns an inhibit_cookie, used to uniquely identify | 267 // The method returns an inhibit_cookie, used to uniquely identify |
270 // this request. It should be used as an argument to Uninhibit() | 268 // this request. It should be used as an argument to Uninhibit() |
271 // in order to remove the request. | 269 // in order to remove the request. |
272 dbus::MessageReader message_reader(response); | 270 dbus::MessageReader message_reader(response); |
273 if (!message_reader.PopUint32(&inhibit_cookie_)) | 271 if (!message_reader.PopUint32(&inhibit_cookie_)) |
274 LOG(ERROR) << "Invalid Inhibit() response: " << response->ToString(); | 272 LOG(ERROR) << "Invalid Inhibit() response: " << response->ToString(); |
275 } else { | 273 } else { |
276 LOG(ERROR) << "No response to Inhibit() request!"; | 274 LOG(ERROR) << "No response to Inhibit() request!"; |
277 } | 275 } |
278 | 276 |
279 if (enqueue_unblock_) { | 277 if (enqueue_unblock_) { |
280 enqueue_unblock_ = false; | 278 enqueue_unblock_ = false; |
281 // RemoveBlock() was called while the Inhibit operation was in flight, | 279 // RemoveBlock() was called while the Inhibit operation was in flight, |
282 // so go ahead and remove the block now. | 280 // so go ahead and remove the block now. |
283 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 281 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
284 base::Bind(&Delegate::RemoveBlock, this, api_)); | 282 base::Bind(&Delegate::RemoveBlock, this)); |
285 } | 283 } |
286 } | 284 } |
287 | 285 |
288 void PowerSaveBlockerImpl::Delegate::RemoveBlock(DBusAPI api) { | 286 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
289 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 287 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
290 DCHECK(bus_); // RemoveBlock() should only be called once. | 288 DCHECK(bus_); // RemoveBlock() should only be called once. |
291 DCHECK(!unblock_inflight_); | 289 DCHECK(!unblock_inflight_); |
292 | 290 |
293 if (block_inflight_) { | 291 if (block_inflight_) { |
294 DCHECK(!enqueue_unblock_); | 292 DCHECK(!enqueue_unblock_); |
295 // Can't call RemoveBlock until ApplyBlock's async operation has | 293 // Can't call RemoveBlock until ApplyBlock's async operation has |
296 // finished. Enqueue it for execution once ApplyBlock is done. | 294 // finished. Enqueue it for execution once ApplyBlock is done. |
297 enqueue_unblock_ = true; | 295 enqueue_unblock_ = true; |
298 return; | 296 return; |
299 } | 297 } |
300 | 298 |
301 scoped_refptr<dbus::ObjectProxy> object_proxy; | 299 scoped_refptr<dbus::ObjectProxy> object_proxy; |
302 scoped_ptr<dbus::MethodCall> method_call; | 300 scoped_ptr<dbus::MethodCall> method_call; |
303 | 301 |
304 switch (api) { | 302 switch (api_) { |
305 case NO_API: | 303 case NO_API: |
306 NOTREACHED(); // We should never call this method with this value. | 304 NOTREACHED(); // We should never call this method with this value. |
307 return; | 305 return; |
308 case GNOME_API: | 306 case GNOME_API: |
309 object_proxy = bus_->GetObjectProxy( | 307 object_proxy = bus_->GetObjectProxy( |
310 kGnomeAPIServiceName, | 308 kGnomeAPIServiceName, |
311 dbus::ObjectPath(kGnomeAPIObjectPath)); | 309 dbus::ObjectPath(kGnomeAPIObjectPath)); |
312 method_call.reset( | 310 method_call.reset( |
313 new dbus::MethodCall(kGnomeAPIInterfaceName, "Uninhibit")); | 311 new dbus::MethodCall(kGnomeAPIInterfaceName, "Uninhibit")); |
314 break; | 312 break; |
(...skipping 20 matching lines...) Expand all Loading... |
335 DCHECK(bus_); | 333 DCHECK(bus_); |
336 unblock_inflight_ = false; | 334 unblock_inflight_ = false; |
337 | 335 |
338 if (!response) | 336 if (!response) |
339 LOG(ERROR) << "No response to Uninhibit() request!"; | 337 LOG(ERROR) << "No response to Uninhibit() request!"; |
340 // We don't care about checking the result. We assume it works; we can't | 338 // We don't care about checking the result. We assume it works; we can't |
341 // really do anything about it anyway if it fails. | 339 // really do anything about it anyway if it fails. |
342 inhibit_cookie_ = 0; | 340 inhibit_cookie_ = 0; |
343 | 341 |
344 bus_->ShutdownAndBlock(); | 342 bus_->ShutdownAndBlock(); |
345 bus_ = NULL; | 343 bus_ = nullptr; |
346 } | 344 } |
347 | 345 |
348 // static | 346 // static |
349 bool PowerSaveBlockerImpl::Delegate::DPMSEnabled() { | 347 bool PowerSaveBlockerImpl::Delegate::DPMSEnabled() { |
| 348 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
350 XDisplay* display = gfx::GetXDisplay(); | 349 XDisplay* display = gfx::GetXDisplay(); |
351 BOOL enabled = false; | 350 BOOL enabled = false; |
352 int dummy; | 351 int dummy; |
353 if (DPMSQueryExtension(display, &dummy, &dummy) && DPMSCapable(display)) { | 352 if (DPMSQueryExtension(display, &dummy, &dummy) && DPMSCapable(display)) { |
354 CARD16 state; | 353 CARD16 state; |
355 DPMSInfo(display, &state, &enabled); | 354 DPMSInfo(display, &state, &enabled); |
356 } | 355 } |
357 return enabled; | 356 return enabled; |
358 } | 357 } |
359 | 358 |
360 // static | 359 // static |
361 DBusAPI PowerSaveBlockerImpl::Delegate::SelectAPI() { | 360 DBusAPI PowerSaveBlockerImpl::Delegate::SelectAPI() { |
| 361 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
362 scoped_ptr<base::Environment> env(base::Environment::Create()); | 362 scoped_ptr<base::Environment> env(base::Environment::Create()); |
363 switch (base::nix::GetDesktopEnvironment(env.get())) { | 363 switch (base::nix::GetDesktopEnvironment(env.get())) { |
364 case base::nix::DESKTOP_ENVIRONMENT_GNOME: | 364 case base::nix::DESKTOP_ENVIRONMENT_GNOME: |
365 case base::nix::DESKTOP_ENVIRONMENT_UNITY: | 365 case base::nix::DESKTOP_ENVIRONMENT_UNITY: |
366 if (DPMSEnabled()) | 366 if (DPMSEnabled()) |
367 return GNOME_API; | 367 return GNOME_API; |
368 break; | 368 break; |
369 case base::nix::DESKTOP_ENVIRONMENT_XFCE: | 369 case base::nix::DESKTOP_ENVIRONMENT_XFCE: |
370 case base::nix::DESKTOP_ENVIRONMENT_KDE4: | 370 case base::nix::DESKTOP_ENVIRONMENT_KDE4: |
371 case base::nix::DESKTOP_ENVIRONMENT_KDE5: | 371 case base::nix::DESKTOP_ENVIRONMENT_KDE5: |
(...skipping 13 matching lines...) Expand all Loading... |
385 const std::string& description) | 385 const std::string& description) |
386 : delegate_(new Delegate(type, description)) { | 386 : delegate_(new Delegate(type, description)) { |
387 delegate_->Init(); | 387 delegate_->Init(); |
388 } | 388 } |
389 | 389 |
390 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 390 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
391 delegate_->CleanUp(); | 391 delegate_->CleanUp(); |
392 } | 392 } |
393 | 393 |
394 } // namespace content | 394 } // namespace content |
OLD | NEW |