Merge lp:~osomon/oxide/power-save-blocker into lp:~oxide-developers/oxide/oxide.trunk

Proposed by Olivier Tilloy
Status: Merged
Merged at revision: 612
Proposed branch: lp:~osomon/oxide/power-save-blocker
Merge into: lp:~oxide-developers/oxide/oxide.trunk
Diff against target: 197 lines (+164/-0)
3 files modified
shared/browser/oxide_power_save_blocker.cc (+161/-0)
shared/shared.gyp (+2/-0)
shared/shared.gypi (+1/-0)
To merge this branch: bzr merge lp:~osomon/oxide/power-save-blocker
Reviewer Review Type Date Requested Status
Chris Coulson Approve
Review via email: mp+222133@code.launchpad.net

Commit message

Implement the power save blocker interface using the powerd D-Bus API where available.

To post a comment you must log in.
Revision history for this message
Chris Coulson (chrisccoulson) wrote :

Thanks for this. This looks ok to commit as is. Would you mind also creating a bug to add support for this in Unity 7 desktop (I guess using gnome-session or whatever it is we're using now)

review: Approve
Revision history for this message
Olivier Tilloy (osomon) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'shared/browser/oxide_power_save_blocker.cc'
2--- shared/browser/oxide_power_save_blocker.cc 1970-01-01 00:00:00 +0000
3+++ shared/browser/oxide_power_save_blocker.cc 2014-06-05 08:47:07 +0000
4@@ -0,0 +1,161 @@
5+// vim:expandtab:shiftwidth=2:tabstop=2:
6+// Copyright (C) 2014 Canonical Ltd.
7+
8+// This library is free software; you can redistribute it and/or
9+// modify it under the terms of the GNU Lesser General Public
10+// License as published by the Free Software Foundation; either
11+// version 2.1 of the License, or (at your option) any later version.
12+
13+// This library is distributed in the hope that it will be useful,
14+// but WITHOUT ANY WARRANTY; without even the implied warranty of
15+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+// Lesser General Public License for more details.
17+
18+// You should have received a copy of the GNU Lesser General Public
19+// License along with this library; if not, write to the Free Software
20+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
22+// Largely inspired by the X11 implementation in
23+// content/browser/power_save_blocker_x11.cc
24+
25+#include "content/browser/power_save_blocker_impl.h"
26+
27+#include "base/logging.h"
28+#include "base/memory/ref_counted.h"
29+#include "base/memory/scoped_ptr.h"
30+#include "content/public/browser/browser_thread.h"
31+#include "dbus/bus.h"
32+#include "dbus/message.h"
33+#include "dbus/object_path.h"
34+#include "dbus/object_proxy.h"
35+
36+#include "oxide_form_factor.h"
37+
38+namespace content {
39+
40+namespace {
41+
42+const char kPowerdServiceName[] = "com.canonical.powerd";
43+const char kPowerdPath[] = "/com/canonical/powerd";
44+const char kPowerdInterface[] = "com.canonical.powerd";
45+
46+}
47+
48+class PowerSaveBlockerImpl::Delegate
49+ : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
50+ public:
51+ Delegate(PowerSaveBlockerType type, const std::string& reason);
52+
53+ void Init();
54+ void CleanUp();
55+
56+ private:
57+ friend class base::RefCountedThreadSafe<Delegate>;
58+ ~Delegate() {}
59+
60+ void ApplyBlock();
61+ void RemoveBlock();
62+
63+ PowerSaveBlockerType type_;
64+ std::string reason_;
65+ oxide::FormFactor form_factor_;
66+ scoped_refptr<dbus::Bus> bus_;
67+ std::string cookie_;
68+
69+ DISALLOW_COPY_AND_ASSIGN(Delegate);
70+};
71+
72+PowerSaveBlockerImpl::Delegate::Delegate(PowerSaveBlockerType type,
73+ const std::string& reason) :
74+ type_(type),
75+ reason_(reason),
76+ form_factor_(oxide::GetFormFactorHint()) {}
77+
78+void PowerSaveBlockerImpl::Delegate::Init() {
79+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
80+ base::Bind(&Delegate::ApplyBlock, this));
81+}
82+
83+void PowerSaveBlockerImpl::Delegate::CleanUp() {
84+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
85+ base::Bind(&Delegate::RemoveBlock, this));
86+}
87+
88+void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
89+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
90+
91+ if (form_factor_ == oxide::FORM_FACTOR_PHONE ||
92+ form_factor_ == oxide::FORM_FACTOR_TABLET) {
93+ DCHECK(!bus_.get());
94+ dbus::Bus::Options options;
95+ options.bus_type = dbus::Bus::SYSTEM;
96+ options.connection_type = dbus::Bus::PRIVATE;
97+ bus_ = new dbus::Bus(options);
98+
99+ scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
100+ kPowerdServiceName,
101+ dbus::ObjectPath(kPowerdPath));
102+ scoped_ptr<dbus::MethodCall> method_call;
103+ method_call.reset(
104+ new dbus::MethodCall(kPowerdInterface, "requestDisplayState"));
105+ scoped_ptr<dbus::MessageWriter> message_writer;
106+ message_writer.reset(new dbus::MessageWriter(method_call.get()));
107+ message_writer->AppendString("[oxide] " + reason_);
108+ message_writer->AppendInt32(1); // POWERD_DISPLAY_STATE_ON
109+ message_writer->AppendUint32(4); // POWERD_DISPLAY_FLAG_BRIGHT
110+
111+ scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
112+ method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
113+ if (response) {
114+ dbus::MessageReader message_reader(response.get());
115+ if (!message_reader.PopString(&cookie_)) {
116+ LOG(ERROR) << "Invalid response for screen blanking inhibition request: "
117+ << response->ToString();
118+ }
119+ } else {
120+ LOG(ERROR) << "Failed to inhibit screen blanking";
121+ }
122+ } else {
123+ NOTIMPLEMENTED();
124+ }
125+}
126+
127+void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
128+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
129+
130+ if (form_factor_ == oxide::FORM_FACTOR_PHONE ||
131+ form_factor_ == oxide::FORM_FACTOR_TABLET) {
132+ DCHECK(bus_.get());
133+
134+ if (!cookie_.empty()) {
135+ scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
136+ kPowerdServiceName,
137+ dbus::ObjectPath(kPowerdPath));
138+ scoped_ptr<dbus::MethodCall> method_call;
139+ method_call.reset(
140+ new dbus::MethodCall(kPowerdInterface, "clearDisplayState"));
141+ dbus::MessageWriter message_writer(method_call.get());
142+ message_writer.AppendString(cookie_);
143+ object_proxy->CallMethodAndBlock(
144+ method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
145+ cookie_.clear();
146+ }
147+
148+ bus_->ShutdownAndBlock();
149+ bus_ = NULL;
150+ } else {
151+ NOTIMPLEMENTED();
152+ }
153+}
154+
155+PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
156+ const std::string& reason)
157+ : delegate_(new Delegate(type, reason)) {
158+ delegate_->Init();
159+}
160+
161+PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
162+ delegate_->CleanUp();
163+}
164+
165+}
166
167=== modified file 'shared/shared.gyp'
168--- shared/shared.gyp 2014-06-02 21:32:41 +0000
169+++ shared/shared.gyp 2014-06-05 08:47:07 +0000
170@@ -176,6 +176,7 @@
171 'oxide_packed_resources',
172 'oxide_extra_resources',
173 '<(DEPTH)/base/base.gyp:base',
174+ '<(DEPTH)/build/linux/system.gyp:dbus',
175 '<(DEPTH)/build/linux/system.gyp:fontconfig',
176 '<(DEPTH)/content/content.gyp:content_app_both',
177 '<(DEPTH)/content/content.gyp:content_browser',
178@@ -249,6 +250,7 @@
179 'browser/oxide_off_the_record_browser_context_impl.h',
180 'browser/oxide_permission_request.cc',
181 'browser/oxide_permission_request.h',
182+ 'browser/oxide_power_save_blocker.cc',
183 'browser/oxide_render_widget_host_view.cc',
184 'browser/oxide_render_widget_host_view.h',
185 'browser/oxide_render_widget_host_view_factory.cc',
186
187=== modified file 'shared/shared.gypi'
188--- shared/shared.gypi 2014-06-02 12:23:00 +0000
189+++ shared/shared.gypi 2014-06-05 08:47:07 +0000
190@@ -20,6 +20,7 @@
191 ['_target_name=="content_browser"', {
192 'sources/': [
193 ['exclude', 'compositor_util\\.cc'],
194+ ['exclude', 'power_save_blocker_ozone\\.cc'],
195 ['exclude', 'render_widget_host_view_aura\\.cc'],
196 ['exclude', 'touch_editable_impl_aura\\.cc'],
197 ['exclude', 'web_contents_view_aura\\.cc'],

Subscribers

People subscribed via source and target branches