Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / session_manager_client.cc
blobf2c36d5ffaf1a36adb38d321b27da06f9659d55a
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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/session_manager_client.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/string_util.h"
10 #include "dbus/bus.h"
11 #include "dbus/message.h"
12 #include "dbus/object_path.h"
13 #include "dbus/object_proxy.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
16 namespace chromeos {
18 // The SessionManagerClient implementation used in production.
19 class SessionManagerClientImpl : public SessionManagerClient {
20 public:
21 explicit SessionManagerClientImpl(dbus::Bus* bus)
22 : session_manager_proxy_(NULL),
23 weak_ptr_factory_(this) {
24 session_manager_proxy_ = bus->GetObjectProxy(
25 login_manager::kSessionManagerServiceName,
26 dbus::ObjectPath(login_manager::kSessionManagerServicePath));
28 // Signals emitted on Chromium's interface. Many of these ought to be
29 // method calls instead.
30 session_manager_proxy_->ConnectToSignal(
31 chromium::kChromiumInterface,
32 chromium::kOwnerKeySetSignal,
33 base::Bind(&SessionManagerClientImpl::OwnerKeySetReceived,
34 weak_ptr_factory_.GetWeakPtr()),
35 base::Bind(&SessionManagerClientImpl::SignalConnected,
36 weak_ptr_factory_.GetWeakPtr()));
37 session_manager_proxy_->ConnectToSignal(
38 chromium::kChromiumInterface,
39 chromium::kPropertyChangeCompleteSignal,
40 base::Bind(&SessionManagerClientImpl::PropertyChangeCompleteReceived,
41 weak_ptr_factory_.GetWeakPtr()),
42 base::Bind(&SessionManagerClientImpl::SignalConnected,
43 weak_ptr_factory_.GetWeakPtr()));
44 session_manager_proxy_->ConnectToSignal(
45 chromium::kChromiumInterface,
46 chromium::kLockScreenSignal,
47 base::Bind(&SessionManagerClientImpl::ScreenLockReceived,
48 weak_ptr_factory_.GetWeakPtr()),
49 base::Bind(&SessionManagerClientImpl::SignalConnected,
50 weak_ptr_factory_.GetWeakPtr()));
51 session_manager_proxy_->ConnectToSignal(
52 chromium::kChromiumInterface,
53 chromium::kUnlockScreenSignal,
54 base::Bind(&SessionManagerClientImpl::ScreenUnlockReceived,
55 weak_ptr_factory_.GetWeakPtr()),
56 base::Bind(&SessionManagerClientImpl::SignalConnected,
57 weak_ptr_factory_.GetWeakPtr()));
58 session_manager_proxy_->ConnectToSignal(
59 chromium::kChromiumInterface,
60 chromium::kLivenessRequestedSignal,
61 base::Bind(&SessionManagerClientImpl::LivenessRequestedReceived,
62 weak_ptr_factory_.GetWeakPtr()),
63 base::Bind(&SessionManagerClientImpl::SignalConnected,
64 weak_ptr_factory_.GetWeakPtr()));
66 // Signals emitted on the session manager's interface.
67 session_manager_proxy_->ConnectToSignal(
68 login_manager::kSessionManagerInterface,
69 login_manager::kScreenIsLockedSignal,
70 base::Bind(&SessionManagerClientImpl::ScreenIsLockedReceived,
71 weak_ptr_factory_.GetWeakPtr()),
72 base::Bind(&SessionManagerClientImpl::SignalConnected,
73 weak_ptr_factory_.GetWeakPtr()));
74 session_manager_proxy_->ConnectToSignal(
75 login_manager::kSessionManagerInterface,
76 login_manager::kScreenIsUnlockedSignal,
77 base::Bind(&SessionManagerClientImpl::ScreenIsUnlockedReceived,
78 weak_ptr_factory_.GetWeakPtr()),
79 base::Bind(&SessionManagerClientImpl::SignalConnected,
80 weak_ptr_factory_.GetWeakPtr()));
83 virtual ~SessionManagerClientImpl() {
86 // SessionManagerClient overrides:
87 virtual void AddObserver(Observer* observer) OVERRIDE {
88 observers_.AddObserver(observer);
91 virtual void RemoveObserver(Observer* observer) OVERRIDE {
92 observers_.RemoveObserver(observer);
95 virtual bool HasObserver(Observer* observer) OVERRIDE {
96 return observers_.HasObserver(observer);
99 virtual void EmitLoginPromptReady() OVERRIDE {
100 SimpleMethodCallToSessionManager(
101 login_manager::kSessionManagerEmitLoginPromptReady);
104 virtual void EmitLoginPromptVisible() OVERRIDE {
105 SimpleMethodCallToSessionManager(
106 login_manager::kSessionManagerEmitLoginPromptVisible);
109 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {
110 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
111 login_manager::kSessionManagerRestartJob);
112 dbus::MessageWriter writer(&method_call);
113 writer.AppendInt32(pid);
114 writer.AppendString(command_line);
115 session_manager_proxy_->CallMethod(
116 &method_call,
117 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
118 base::Bind(&SessionManagerClientImpl::OnRestartJob,
119 weak_ptr_factory_.GetWeakPtr()));
122 virtual void RestartEntd() OVERRIDE {
123 SimpleMethodCallToSessionManager(login_manager::kSessionManagerRestartEntd);
126 virtual void StartSession(const std::string& user_email) OVERRIDE {
127 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
128 login_manager::kSessionManagerStartSession);
129 dbus::MessageWriter writer(&method_call);
130 writer.AppendString(user_email);
131 writer.AppendString(""); // Unique ID is deprecated
132 session_manager_proxy_->CallMethod(
133 &method_call,
134 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
135 base::Bind(&SessionManagerClientImpl::OnStartSession,
136 weak_ptr_factory_.GetWeakPtr()));
139 virtual void StopSession() OVERRIDE {
140 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
141 login_manager::kSessionManagerStopSession);
142 dbus::MessageWriter writer(&method_call);
143 writer.AppendString(""); // Unique ID is deprecated
144 session_manager_proxy_->CallMethod(
145 &method_call,
146 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
147 base::Bind(&SessionManagerClientImpl::OnStopSession,
148 weak_ptr_factory_.GetWeakPtr()));
151 virtual void StartDeviceWipe() OVERRIDE {
152 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
153 login_manager::kSessionManagerStartDeviceWipe);
154 session_manager_proxy_->CallMethod(
155 &method_call,
156 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
157 base::Bind(&SessionManagerClientImpl::OnDeviceWipe,
158 weak_ptr_factory_.GetWeakPtr()));
161 virtual void RequestLockScreen() OVERRIDE {
162 SimpleMethodCallToSessionManager(login_manager::kSessionManagerLockScreen);
165 virtual void NotifyLockScreenShown() OVERRIDE {
166 SimpleMethodCallToSessionManager(
167 login_manager::kSessionManagerHandleLockScreenShown);
170 virtual void RequestUnlockScreen() OVERRIDE {
171 SimpleMethodCallToSessionManager(
172 login_manager::kSessionManagerUnlockScreen);
175 virtual void NotifyLockScreenDismissed() OVERRIDE {
176 SimpleMethodCallToSessionManager(
177 login_manager::kSessionManagerHandleLockScreenDismissed);
180 virtual void RetrieveDevicePolicy(
181 const RetrievePolicyCallback& callback) OVERRIDE {
182 CallRetrievePolicy(login_manager::kSessionManagerRetrievePolicy,
183 callback);
186 virtual void RetrieveUserPolicy(
187 const RetrievePolicyCallback& callback) OVERRIDE {
188 CallRetrievePolicy(login_manager::kSessionManagerRetrieveUserPolicy,
189 callback);
192 virtual void RetrieveDeviceLocalAccountPolicy(
193 const std::string& account_name,
194 const RetrievePolicyCallback& callback) OVERRIDE {
195 dbus::MethodCall method_call(
196 login_manager::kSessionManagerInterface,
197 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy);
198 dbus::MessageWriter writer(&method_call);
199 writer.AppendString(account_name);
200 session_manager_proxy_->CallMethod(
201 &method_call,
202 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
203 base::Bind(
204 &SessionManagerClientImpl::OnRetrievePolicy,
205 weak_ptr_factory_.GetWeakPtr(),
206 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy,
207 callback));
210 virtual void StoreDevicePolicy(const std::string& policy_blob,
211 const StorePolicyCallback& callback) OVERRIDE {
212 CallStorePolicy(login_manager::kSessionManagerStorePolicy,
213 policy_blob, callback);
216 virtual void StoreUserPolicy(const std::string& policy_blob,
217 const StorePolicyCallback& callback) OVERRIDE {
218 CallStorePolicy(login_manager::kSessionManagerStoreUserPolicy,
219 policy_blob, callback);
222 virtual void StoreDeviceLocalAccountPolicy(
223 const std::string& account_name,
224 const std::string& policy_blob,
225 const StorePolicyCallback& callback) OVERRIDE {
226 dbus::MethodCall method_call(
227 login_manager::kSessionManagerInterface,
228 login_manager::kSessionManagerStoreDeviceLocalAccountPolicy);
229 dbus::MessageWriter writer(&method_call);
230 writer.AppendString(account_name);
231 // static_cast does not work due to signedness.
232 writer.AppendArrayOfBytes(
233 reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size());
234 session_manager_proxy_->CallMethod(
235 &method_call,
236 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
237 base::Bind(
238 &SessionManagerClientImpl::OnStorePolicy,
239 weak_ptr_factory_.GetWeakPtr(),
240 login_manager::kSessionManagerStoreDeviceLocalAccountPolicy,
241 callback));
244 private:
245 // Makes a method call to the session manager with no arguments and no
246 // response.
247 void SimpleMethodCallToSessionManager(const std::string& method_name) {
248 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
249 method_name);
250 session_manager_proxy_->CallMethod(
251 &method_call,
252 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
253 dbus::ObjectProxy::EmptyResponseCallback());
256 // Helper for Retrieve{User,Device}Policy.
257 virtual void CallRetrievePolicy(const std::string& method_name,
258 const RetrievePolicyCallback& callback) {
259 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
260 method_name);
261 session_manager_proxy_->CallMethod(
262 &method_call,
263 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
264 base::Bind(&SessionManagerClientImpl::OnRetrievePolicy,
265 weak_ptr_factory_.GetWeakPtr(),
266 method_name,
267 callback));
270 // Helper for Store{User,Device}Policy.
271 virtual void CallStorePolicy(const std::string& method_name,
272 const std::string& policy_blob,
273 const StorePolicyCallback& callback) {
274 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
275 method_name);
276 dbus::MessageWriter writer(&method_call);
277 // static_cast does not work due to signedness.
278 writer.AppendArrayOfBytes(
279 reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size());
280 session_manager_proxy_->CallMethod(
281 &method_call,
282 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
283 base::Bind(&SessionManagerClientImpl::OnStorePolicy,
284 weak_ptr_factory_.GetWeakPtr(),
285 method_name,
286 callback));
289 // Called when kSessionManagerRestartJob method is complete.
290 void OnRestartJob(dbus::Response* response) {
291 LOG_IF(ERROR, !response)
292 << "Failed to call "
293 << login_manager::kSessionManagerRestartJob;
296 // Called when kSessionManagerStartSession method is complete.
297 void OnStartSession(dbus::Response* response) {
298 LOG_IF(ERROR, !response)
299 << "Failed to call "
300 << login_manager::kSessionManagerStartSession;
303 // Called when kSessionManagerStopSession method is complete.
304 void OnStopSession(dbus::Response* response) {
305 LOG_IF(ERROR, !response)
306 << "Failed to call "
307 << login_manager::kSessionManagerStopSession;
310 // Called when kSessionManagerStopSession method is complete.
311 void OnDeviceWipe(dbus::Response* response) {
312 LOG_IF(ERROR, !response)
313 << "Failed to call "
314 << login_manager::kSessionManagerStartDeviceWipe;
317 // Called when kSessionManagerRetrievePolicy or
318 // kSessionManagerRetrieveUserPolicy method is complete.
319 void OnRetrievePolicy(const std::string& method_name,
320 const RetrievePolicyCallback& callback,
321 dbus::Response* response) {
322 if (!response) {
323 LOG(ERROR) << "Failed to call " << method_name;
324 callback.Run("");
325 return;
327 dbus::MessageReader reader(response);
328 uint8* values = NULL;
329 size_t length = 0;
330 if (!reader.PopArrayOfBytes(&values, &length)) {
331 LOG(ERROR) << "Invalid response: " << response->ToString();
332 callback.Run("");
333 return;
335 // static_cast does not work due to signedness.
336 std::string serialized_proto(reinterpret_cast<char*>(values), length);
337 callback.Run(serialized_proto);
340 // Called when kSessionManagerStorePolicy or kSessionManagerStoreUserPolicy
341 // method is complete.
342 void OnStorePolicy(const std::string& method_name,
343 const StorePolicyCallback& callback,
344 dbus::Response* response) {
345 bool success = false;
346 if (!response) {
347 LOG(ERROR) << "Failed to call " << method_name;
348 } else {
349 dbus::MessageReader reader(response);
350 if (!reader.PopBool(&success))
351 LOG(ERROR) << "Invalid response: " << response->ToString();
353 callback.Run(success);
356 // Called when the owner key set signal is received.
357 void OwnerKeySetReceived(dbus::Signal* signal) {
358 dbus::MessageReader reader(signal);
359 std::string result_string;
360 if (!reader.PopString(&result_string)) {
361 LOG(ERROR) << "Invalid signal: " << signal->ToString();
362 return;
364 const bool success = StartsWithASCII(result_string, "success", false);
365 FOR_EACH_OBSERVER(Observer, observers_, OwnerKeySet(success));
368 // Called when the property change complete signal is received.
369 void PropertyChangeCompleteReceived(dbus::Signal* signal) {
370 dbus::MessageReader reader(signal);
371 std::string result_string;
372 if (!reader.PopString(&result_string)) {
373 LOG(ERROR) << "Invalid signal: " << signal->ToString();
374 return;
376 const bool success = StartsWithASCII(result_string, "success", false);
377 FOR_EACH_OBSERVER(Observer, observers_, PropertyChangeComplete(success));
380 void ScreenLockReceived(dbus::Signal* signal) {
381 FOR_EACH_OBSERVER(Observer, observers_, LockScreen());
384 void ScreenUnlockReceived(dbus::Signal* signal) {
385 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen());
388 void LivenessRequestedReceived(dbus::Signal* signal) {
389 SimpleMethodCallToSessionManager(
390 login_manager::kSessionManagerHandleLivenessConfirmed);
393 void ScreenIsLockedReceived(dbus::Signal* signal) {
394 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsLocked());
397 void ScreenIsUnlockedReceived(dbus::Signal* signal) {
398 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsUnlocked());
401 // Called when the object is connected to the signal.
402 void SignalConnected(const std::string& interface_name,
403 const std::string& signal_name,
404 bool success) {
405 LOG_IF(ERROR, !success) << "Failed to connect to " << signal_name;
408 dbus::ObjectProxy* session_manager_proxy_;
409 ObserverList<Observer> observers_;
411 // Note: This should remain the last member so it'll be destroyed and
412 // invalidate its weak pointers before any other members are destroyed.
413 base::WeakPtrFactory<SessionManagerClientImpl> weak_ptr_factory_;
415 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientImpl);
418 // The SessionManagerClient implementation used on Linux desktop,
419 // which does nothing.
420 class SessionManagerClientStubImpl : public SessionManagerClient {
421 public:
422 SessionManagerClientStubImpl() {}
423 virtual ~SessionManagerClientStubImpl() {}
425 // SessionManagerClient overrides.
426 virtual void AddObserver(Observer* observer) OVERRIDE {
427 observers_.AddObserver(observer);
429 virtual void RemoveObserver(Observer* observer) OVERRIDE {
430 observers_.RemoveObserver(observer);
432 virtual bool HasObserver(Observer* observer) OVERRIDE {
433 return observers_.HasObserver(observer);
435 virtual void EmitLoginPromptReady() OVERRIDE {}
436 virtual void EmitLoginPromptVisible() OVERRIDE {}
437 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {}
438 virtual void RestartEntd() OVERRIDE {}
439 virtual void StartSession(const std::string& user_email) OVERRIDE {}
440 virtual void StopSession() OVERRIDE {}
441 virtual void StartDeviceWipe() OVERRIDE {}
442 virtual void RequestLockScreen() OVERRIDE {
443 FOR_EACH_OBSERVER(Observer, observers_, LockScreen());
445 virtual void NotifyLockScreenShown() OVERRIDE {
446 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsLocked());
448 virtual void RequestUnlockScreen() OVERRIDE {
449 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen());
451 virtual void NotifyLockScreenDismissed() OVERRIDE {
452 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsUnlocked());
454 virtual void RetrieveDevicePolicy(
455 const RetrievePolicyCallback& callback) OVERRIDE {
456 callback.Run(device_policy_);
458 virtual void RetrieveUserPolicy(
459 const RetrievePolicyCallback& callback) OVERRIDE {
460 callback.Run(user_policy_);
462 virtual void RetrieveDeviceLocalAccountPolicy(
463 const std::string& account_name,
464 const RetrievePolicyCallback& callback) OVERRIDE {
465 callback.Run("");
467 virtual void StoreDevicePolicy(const std::string& policy_blob,
468 const StorePolicyCallback& callback) OVERRIDE {
469 device_policy_ = policy_blob;
470 callback.Run(true);
472 virtual void StoreUserPolicy(const std::string& policy_blob,
473 const StorePolicyCallback& callback) OVERRIDE {
474 user_policy_ = policy_blob;
475 callback.Run(true);
477 virtual void StoreDeviceLocalAccountPolicy(
478 const std::string& account_name,
479 const std::string& policy_blob,
480 const StorePolicyCallback& callback) OVERRIDE {
481 callback.Run(true);
484 private:
485 ObserverList<Observer> observers_;
486 std::string device_policy_;
487 std::string user_policy_;
489 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientStubImpl);
492 SessionManagerClient::SessionManagerClient() {
495 SessionManagerClient::~SessionManagerClient() {
498 SessionManagerClient* SessionManagerClient::Create(
499 DBusClientImplementationType type,
500 dbus::Bus* bus) {
501 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
502 return new SessionManagerClientImpl(bus);
503 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
504 return new SessionManagerClientStubImpl();
507 } // namespace chromeos