~oxide-developers/oxide/oxide.trunk

« back to all changes in this revision

Viewing changes to shared/browser/oxide_power_save_blocker.cc

  • Committer: Justin McPherson
  • Date: 2015-07-28 06:03:19 UTC
  • mfrom: (1159.2.6 1328494)
  • Revision ID: justin.mcpherson@canonical.com-20150728060319-6b72w4to1l8imc21
Request that the screensaver be inhibited (desktop)

This adds to the already existing code to do this by activating the desktop path.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
#include "shared/port/content/browser/power_save_blocker_oxide.h"
33
33
 
 
34
#include "oxide_browser_platform_integration.h"
34
35
#include "oxide_browser_platform_integration_observer.h"
35
36
#include "oxide_form_factor.h"
36
37
 
43
44
const char kUnityScreenInterface[] = "com.canonical.Unity.Screen";
44
45
const int kInvalidCookie = -1;
45
46
 
 
47
const char kFreeDesktopScreenSaverName[] = "org.freedesktop.ScreenSaver";
 
48
const char kFreeDesktopScreenSaverPath[] = "/org/freedesktop/ScreenSaver";
 
49
const char kFreeDestopScreenSaverInterface[] = "org.freedesktop.ScreenSaver";
 
50
 
 
51
const char kDefaultInhibitReason[] = "Active Application";
 
52
 
46
53
}
47
54
 
48
55
class PowerSaveBlocker : public content::PowerSaveBlockerOxideDelegate,
49
56
                         public BrowserPlatformIntegrationObserver {
50
57
 public:
51
 
  PowerSaveBlocker();
 
58
  PowerSaveBlocker(const std::string& description);
52
59
 
53
60
 private:
54
61
  virtual ~PowerSaveBlocker() {}
59
66
  void ApplyBlock();
60
67
  void RemoveBlock();
61
68
 
 
69
  void ApplyBlockUnityScreenService();
 
70
  void RemoveBlockUnityScreenService();
 
71
 
 
72
  void ApplyBlockFreedesktop();
 
73
  void RemoveBlockFreedesktop();
 
74
 
62
75
  // BrowserPlatformIntegrationObserver implementation
63
76
  void ApplicationStateChanged() final;
64
77
 
65
78
  oxide::FormFactor form_factor_;
66
79
  scoped_refptr<dbus::Bus> bus_;
67
 
  int cookie_;
 
80
  union {
 
81
    int32_t unity_cookie_;
 
82
    uint32_t freedesktop_cookie_;
 
83
  };
 
84
  std::string description_;
68
85
};
69
86
 
70
87
void PowerSaveBlocker::Init() {
89
106
 
90
107
  if (form_factor_ == oxide::FORM_FACTOR_PHONE ||
91
108
      form_factor_ == oxide::FORM_FACTOR_TABLET) {
92
 
    DCHECK(!bus_.get());
93
 
    dbus::Bus::Options options;
94
 
    options.bus_type = dbus::Bus::SYSTEM;
95
 
    options.connection_type = dbus::Bus::PRIVATE;
96
 
    bus_ = new dbus::Bus(options);
97
 
 
98
 
    scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
99
 
          kUnityScreenServiceName,
100
 
          dbus::ObjectPath(kUnityScreenPath));
101
 
    scoped_ptr<dbus::MethodCall> method_call;
102
 
    method_call.reset(
103
 
        new dbus::MethodCall(kUnityScreenInterface, "keepDisplayOn"));
104
 
    scoped_ptr<dbus::MessageWriter> message_writer;
105
 
    message_writer.reset(new dbus::MessageWriter(method_call.get()));
106
 
 
107
 
    scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
108
 
        method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
109
 
    if (response) {
110
 
      dbus::MessageReader message_reader(response.get());
111
 
      if (!message_reader.PopInt32(&cookie_)) {
112
 
        LOG(ERROR) << "Invalid response for screen blanking inhibition request: "
113
 
                   << response->ToString();
114
 
      }
115
 
    } else {
116
 
      LOG(ERROR) << "Failed to inhibit screen blanking";
117
 
    }
 
109
    ApplyBlockUnityScreenService();
118
110
  } else {
119
 
    NOTIMPLEMENTED();
 
111
    ApplyBlockFreedesktop();
120
112
  }
121
113
}
122
114
 
125
117
 
126
118
  if (form_factor_ == oxide::FORM_FACTOR_PHONE ||
127
119
      form_factor_ == oxide::FORM_FACTOR_TABLET) {
128
 
    if (cookie_ != kInvalidCookie) {
129
 
      DCHECK(bus_.get());
130
 
      scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
131
 
          kUnityScreenServiceName,
132
 
          dbus::ObjectPath(kUnityScreenPath));
133
 
      scoped_ptr<dbus::MethodCall> method_call;
134
 
      method_call.reset(
135
 
          new dbus::MethodCall(kUnityScreenInterface, "removeDisplayOnRequest"));
136
 
      dbus::MessageWriter message_writer(method_call.get());
137
 
      message_writer.AppendInt32(cookie_);
138
 
      object_proxy->CallMethodAndBlock(
139
 
          method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
140
 
      cookie_ = kInvalidCookie;
141
 
    }
142
 
 
143
 
    if (bus_.get()) {
144
 
      bus_->ShutdownAndBlock();
145
 
      bus_ = nullptr;
146
 
    }
147
 
  } else {
148
 
    NOTIMPLEMENTED();
 
120
    RemoveBlockUnityScreenService();
 
121
  } else {
 
122
    RemoveBlockFreedesktop();
 
123
  }
 
124
}
 
125
 
 
126
void PowerSaveBlocker::ApplyBlockUnityScreenService() {
 
127
  DCHECK(!bus_.get());
 
128
  dbus::Bus::Options options;
 
129
  options.bus_type = dbus::Bus::SYSTEM;
 
130
  options.connection_type = dbus::Bus::PRIVATE;
 
131
  bus_ = new dbus::Bus(options);
 
132
 
 
133
  scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
 
134
        kUnityScreenServiceName,
 
135
        dbus::ObjectPath(kUnityScreenPath));
 
136
  scoped_ptr<dbus::MethodCall> method_call;
 
137
  method_call.reset(
 
138
      new dbus::MethodCall(kUnityScreenInterface, "keepDisplayOn"));
 
139
  scoped_ptr<dbus::MessageWriter> message_writer;
 
140
  message_writer.reset(new dbus::MessageWriter(method_call.get()));
 
141
 
 
142
  scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
 
143
      method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
 
144
  if (response) {
 
145
    dbus::MessageReader message_reader(response.get());
 
146
    if (!message_reader.PopInt32(&unity_cookie_)) {
 
147
      LOG(ERROR) << "Invalid response for screen blanking inhibition request: "
 
148
                 << response->ToString();
 
149
    }
 
150
  } else {
 
151
    LOG(ERROR) << "Failed to inhibit screen blanking";
 
152
  }
 
153
}
 
154
 
 
155
void PowerSaveBlocker::RemoveBlockUnityScreenService() {
 
156
  if (unity_cookie_ != kInvalidCookie) {
 
157
    DCHECK(bus_.get());
 
158
    scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
 
159
        kUnityScreenServiceName,
 
160
        dbus::ObjectPath(kUnityScreenPath));
 
161
    scoped_ptr<dbus::MethodCall> method_call;
 
162
    method_call.reset(
 
163
        new dbus::MethodCall(kUnityScreenInterface, "removeDisplayOnRequest"));
 
164
    dbus::MessageWriter message_writer(method_call.get());
 
165
    message_writer.AppendInt32(unity_cookie_);
 
166
    object_proxy->CallMethodAndBlock(
 
167
        method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
 
168
    unity_cookie_ = kInvalidCookie;
 
169
  }
 
170
 
 
171
  if (bus_.get()) {
 
172
    bus_->ShutdownAndBlock();
 
173
    bus_ = nullptr;
 
174
  }
 
175
}
 
176
 
 
177
void PowerSaveBlocker::ApplyBlockFreedesktop() {
 
178
  std::string application_name =
 
179
      BrowserPlatformIntegration::GetInstance()->GetApplicationName();
 
180
  std::string description{kDefaultInhibitReason};
 
181
 
 
182
  if (!description_.empty()) {
 
183
    description = description_;
 
184
  }
 
185
 
 
186
  DCHECK(!bus_.get());
 
187
  dbus::Bus::Options options;
 
188
  options.bus_type = dbus::Bus::SESSION;
 
189
  options.connection_type = dbus::Bus::PRIVATE;
 
190
  bus_ = new dbus::Bus(options);
 
191
 
 
192
  scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
 
193
        kFreeDesktopScreenSaverName,
 
194
        dbus::ObjectPath(kFreeDesktopScreenSaverPath));
 
195
  scoped_ptr<dbus::MethodCall> method_call;
 
196
  method_call.reset(
 
197
      new dbus::MethodCall(kFreeDestopScreenSaverInterface, "Inhibit"));
 
198
  scoped_ptr<dbus::MessageWriter> message_writer;
 
199
  message_writer.reset(new dbus::MessageWriter(method_call.get()));
 
200
  message_writer->AppendString(application_name);
 
201
  message_writer->AppendString(description);
 
202
 
 
203
  scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
 
204
      method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
 
205
  if (response) {
 
206
    dbus::MessageReader message_reader(response.get());
 
207
    if (!message_reader.PopUint32(&freedesktop_cookie_)) {
 
208
      LOG(ERROR) << "Invalid response for screen blanking inhibition request: "
 
209
                 << response->ToString();
 
210
    }
 
211
  } else {
 
212
    LOG(ERROR) << "Failed to inhibit screen blanking";
 
213
  }
 
214
}
 
215
 
 
216
void PowerSaveBlocker::RemoveBlockFreedesktop() {
 
217
  if (freedesktop_cookie_ != uint32_t(kInvalidCookie)) {
 
218
    DCHECK(bus_.get());
 
219
    scoped_refptr<dbus::ObjectProxy> object_proxy = bus_->GetObjectProxy(
 
220
        kFreeDesktopScreenSaverName,
 
221
        dbus::ObjectPath(kFreeDesktopScreenSaverPath));
 
222
    scoped_ptr<dbus::MethodCall> method_call;
 
223
    method_call.reset(
 
224
        new dbus::MethodCall(kFreeDestopScreenSaverInterface, "UnInhibit"));
 
225
    dbus::MessageWriter message_writer(method_call.get());
 
226
    message_writer.AppendUint32(freedesktop_cookie_);
 
227
    object_proxy->CallMethodAndBlock(
 
228
        method_call.get(), dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
 
229
    freedesktop_cookie_ = kInvalidCookie;
 
230
  }
 
231
 
 
232
  if (bus_.get()) {
 
233
    bus_->ShutdownAndBlock();
 
234
    bus_ = nullptr;
149
235
  }
150
236
}
151
237
 
153
239
  BrowserPlatformIntegration::ApplicationState state =
154
240
      BrowserPlatformIntegration::GetInstance()->GetApplicationState();
155
241
  if (state != BrowserPlatformIntegration::APPLICATION_STATE_SUSPENDED &&
156
 
      cookie_ == kInvalidCookie) {
 
242
      unity_cookie_ == kInvalidCookie) {
157
243
    Init();
158
244
  } else if (state == BrowserPlatformIntegration::APPLICATION_STATE_SUSPENDED &&
159
 
             cookie_ != kInvalidCookie) {
 
245
             unity_cookie_ != kInvalidCookie) {
160
246
    CleanUp();
161
247
  }
162
248
}
163
249
 
164
 
PowerSaveBlocker::PowerSaveBlocker()
 
250
PowerSaveBlocker::PowerSaveBlocker(const std::string& description)
165
251
    : form_factor_(oxide::GetFormFactorHint())
166
 
    , cookie_(kInvalidCookie) {}
 
252
    , unity_cookie_(kInvalidCookie)
 
253
    , description_(description)
 
254
{}
167
255
 
168
256
content::PowerSaveBlockerOxideDelegate* CreatePowerSaveBlocker(
169
257
    content::PowerSaveBlocker::PowerSaveBlockerType type,
170
258
    content::PowerSaveBlocker::Reason reason,
171
259
    const std::string& description) {
172
 
  return new PowerSaveBlocker();
 
260
  return new PowerSaveBlocker(description);
173
261
}
174
262
 
175
263
} // namespace oxide