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

Side by Side Diff: content/browser/power_save_blocker_x11.cc

Issue 946643002: Use PowerSaveBlocker for audio and video on Chrome OS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: apply nits Created 5 years, 10 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
OLDNEW
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 "/org/freedesktop/PowerManagement/Inhibit"; 60 "/org/freedesktop/PowerManagement/Inhibit";
61 61
62 } // namespace 62 } // namespace
63 63
64 namespace content { 64 namespace content {
65 65
66 class PowerSaveBlockerImpl::Delegate 66 class PowerSaveBlockerImpl::Delegate
67 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { 67 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
68 public: 68 public:
69 // Picks an appropriate D-Bus API to use based on the desktop environment. 69 // Picks an appropriate D-Bus API to use based on the desktop environment.
70 Delegate(PowerSaveBlockerType type, const std::string& reason); 70 Delegate(PowerSaveBlockerType type, const std::string& description);
71 71
72 // Post a task to initialize the delegate on the UI thread, which will itself 72 // Post a task to initialize the delegate on the UI thread, which will itself
73 // then post a task to apply the power save block on the FILE thread. 73 // then post a task to apply the power save block on the FILE thread.
74 void Init(); 74 void Init();
75 75
76 // Post a task to remove the power save block on the FILE thread, unless it 76 // Post a task to remove the power save block on the FILE thread, unless it
77 // hasn't yet been applied, in which case we just prevent it from applying. 77 // hasn't yet been applied, in which case we just prevent it from applying.
78 void CleanUp(); 78 void CleanUp();
79 79
80 private: 80 private:
(...skipping 16 matching lines...) Expand all
97 // to try to disable power saving, since on some desktop environments that may 97 // to try to disable power saving, since on some desktop environments that may
98 // enable DPMS with very poor default settings (e.g. turning off the display 98 // enable DPMS with very poor default settings (e.g. turning off the display
99 // after only 1 second). Must be called on the UI thread. 99 // after only 1 second). Must be called on the UI thread.
100 static bool DPMSEnabled(); 100 static bool DPMSEnabled();
101 101
102 // Returns an appropriate D-Bus API to use based on the desktop environment. 102 // Returns an appropriate D-Bus API to use based on the desktop environment.
103 // Must be called on the UI thread, as it may call DPMSEnabled() above. 103 // Must be called on the UI thread, as it may call DPMSEnabled() above.
104 static DBusAPI SelectAPI(); 104 static DBusAPI SelectAPI();
105 105
106 const PowerSaveBlockerType type_; 106 const PowerSaveBlockerType type_;
107 const std::string reason_; 107 const std::string description_;
108 108
109 // Initially, we post a message to the UI thread to select an API. When it 109 // Initially, we post a message to the UI thread to select an API. When it
110 // finishes, it will post a message to the FILE thread to perform the actual 110 // finishes, it will post a message to the FILE thread to perform the actual
111 // application of the block, unless enqueue_apply_ is false. We set it to 111 // application of the block, unless enqueue_apply_ is false. We set it to
112 // false when we post that message, or when RemoveBlock() is called before 112 // false when we post that message, or when RemoveBlock() is called before
113 // ApplyBlock() has run. Both api_ and enqueue_apply_ are guarded by lock_. 113 // ApplyBlock() has run. Both api_ and enqueue_apply_ are guarded by lock_.
114 DBusAPI api_; 114 DBusAPI api_;
115 bool enqueue_apply_; 115 bool enqueue_apply_;
116 base::Lock lock_; 116 base::Lock lock_;
117 117
118 scoped_refptr<dbus::Bus> bus_; 118 scoped_refptr<dbus::Bus> bus_;
119 119
120 // The cookie that identifies our inhibit request, 120 // The cookie that identifies our inhibit request,
121 // or 0 if there is no active inhibit request. 121 // or 0 if there is no active inhibit request.
122 uint32 inhibit_cookie_; 122 uint32 inhibit_cookie_;
123 123
124 DISALLOW_COPY_AND_ASSIGN(Delegate); 124 DISALLOW_COPY_AND_ASSIGN(Delegate);
125 }; 125 };
126 126
127 PowerSaveBlockerImpl::Delegate::Delegate(PowerSaveBlockerType type, 127 PowerSaveBlockerImpl::Delegate::Delegate(PowerSaveBlockerType type,
128 const std::string& reason) 128 const std::string& description)
129 : type_(type), 129 : type_(type),
130 reason_(reason), 130 description_(description),
131 api_(NO_API), 131 api_(NO_API),
132 enqueue_apply_(false), 132 enqueue_apply_(false),
133 inhibit_cookie_(0) { 133 inhibit_cookie_(0) {
134 // We're on the client's thread here, so we don't allocate the dbus::Bus 134 // We're on the client's thread here, so we don't allocate the dbus::Bus
135 // object yet. We'll do it later in ApplyBlock(), on the FILE thread. 135 // object yet. We'll do it later in ApplyBlock(), on the FILE thread.
136 } 136 }
137 137
138 void PowerSaveBlockerImpl::Delegate::Init() { 138 void PowerSaveBlockerImpl::Delegate::Init() {
139 base::AutoLock lock(lock_); 139 base::AutoLock lock(lock_);
140 DCHECK(!enqueue_apply_); 140 DCHECK(!enqueue_apply_);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 new dbus::MethodCall(kGnomeAPIInterfaceName, "Inhibit")); 195 new dbus::MethodCall(kGnomeAPIInterfaceName, "Inhibit"));
196 message_writer.reset(new dbus::MessageWriter(method_call.get())); 196 message_writer.reset(new dbus::MessageWriter(method_call.get()));
197 // The arguments of the method are: 197 // The arguments of the method are:
198 // app_id: The application identifier 198 // app_id: The application identifier
199 // toplevel_xid: The toplevel X window identifier 199 // toplevel_xid: The toplevel X window identifier
200 // reason: The reason for the inhibit 200 // reason: The reason for the inhibit
201 // flags: Flags that spefify what should be inhibited 201 // flags: Flags that spefify what should be inhibited
202 message_writer->AppendString( 202 message_writer->AppendString(
203 base::CommandLine::ForCurrentProcess()->GetProgram().value()); 203 base::CommandLine::ForCurrentProcess()->GetProgram().value());
204 message_writer->AppendUint32(0); // should be toplevel_xid 204 message_writer->AppendUint32(0); // should be toplevel_xid
205 message_writer->AppendString(reason_); 205 message_writer->AppendString(description_);
206 { 206 {
207 uint32 flags = 0; 207 uint32 flags = 0;
208 switch (type_) { 208 switch (type_) {
209 case kPowerSaveBlockPreventDisplaySleep: 209 case kPowerSaveBlockPreventDisplaySleep:
210 flags |= INHIBIT_MARK_SESSION_IDLE; 210 flags |= INHIBIT_MARK_SESSION_IDLE;
211 flags |= INHIBIT_SUSPEND_SESSION; 211 flags |= INHIBIT_SUSPEND_SESSION;
212 break; 212 break;
213 case kPowerSaveBlockPreventAppSuspension: 213 case kPowerSaveBlockPreventAppSuspension:
214 flags |= INHIBIT_SUSPEND_SESSION; 214 flags |= INHIBIT_SUSPEND_SESSION;
215 break; 215 break;
216 } 216 }
217 message_writer->AppendUint32(flags); 217 message_writer->AppendUint32(flags);
218 } 218 }
219 break; 219 break;
220 case FREEDESKTOP_API: 220 case FREEDESKTOP_API:
221 object_proxy = bus_->GetObjectProxy( 221 object_proxy = bus_->GetObjectProxy(
222 kFreeDesktopAPIServiceName, 222 kFreeDesktopAPIServiceName,
223 dbus::ObjectPath(kFreeDesktopAPIObjectPath)); 223 dbus::ObjectPath(kFreeDesktopAPIObjectPath));
224 method_call.reset( 224 method_call.reset(
225 new dbus::MethodCall(kFreeDesktopAPIInterfaceName, "Inhibit")); 225 new dbus::MethodCall(kFreeDesktopAPIInterfaceName, "Inhibit"));
226 message_writer.reset(new dbus::MessageWriter(method_call.get())); 226 message_writer.reset(new dbus::MessageWriter(method_call.get()));
227 // The arguments of the method are: 227 // The arguments of the method are:
228 // app_id: The application identifier 228 // app_id: The application identifier
229 // reason: The reason for the inhibit 229 // reason: The reason for the inhibit
230 message_writer->AppendString( 230 message_writer->AppendString(
231 base::CommandLine::ForCurrentProcess()->GetProgram().value()); 231 base::CommandLine::ForCurrentProcess()->GetProgram().value());
232 message_writer->AppendString(reason_); 232 message_writer->AppendString(description_);
233 break; 233 break;
234 } 234 }
235 235
236 // We could do this method call asynchronously, but if we did, we'd need to 236 // We could do this method call asynchronously, but if we did, we'd need to
237 // handle the case where we want to cancel the block before we get a reply. 237 // handle the case where we want to cancel the block before we get a reply.
238 // We're on the FILE thread so it should be OK to block briefly here. 238 // We're on the FILE thread so it should be OK to block briefly here.
239 scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock( 239 scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
240 method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 240 method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
241 if (response) { 241 if (response) {
242 // The method returns an inhibit_cookie, used to uniquely identify 242 // The method returns an inhibit_cookie, used to uniquely identify
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return FREEDESKTOP_API; 318 return FREEDESKTOP_API;
319 break; 319 break;
320 case base::nix::DESKTOP_ENVIRONMENT_KDE3: 320 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
321 case base::nix::DESKTOP_ENVIRONMENT_OTHER: 321 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
322 // Not supported. 322 // Not supported.
323 break; 323 break;
324 } 324 }
325 return NO_API; 325 return NO_API;
326 } 326 }
327 327
328 PowerSaveBlockerImpl::PowerSaveBlockerImpl( 328 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
329 PowerSaveBlockerType type, const std::string& reason) 329 Reason reason,
330 : delegate_(new Delegate(type, reason)) { 330 const std::string& description)
331 : delegate_(new Delegate(type, description)) {
331 delegate_->Init(); 332 delegate_->Init();
332 } 333 }
333 334
334 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { 335 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
335 delegate_->CleanUp(); 336 delegate_->CleanUp();
336 } 337 }
337 338
338 } // namespace content 339 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/power_save_blocker_win.cc ('k') | content/browser/web_contents/web_contents_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698