session_manager_client.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#include "chromeos/dbus/session_manager_client.h"
6
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"
15
16namespace chromeos {
17
18// The SessionManagerClient implementation used in production.
19class SessionManagerClientImpl : public SessionManagerClient {
20 public:
21  explicit SessionManagerClientImpl(dbus::Bus* bus)
22      : session_manager_proxy_(NULL),
23        screen_locked_(false),
24        weak_ptr_factory_(this) {
25    session_manager_proxy_ = bus->GetObjectProxy(
26        login_manager::kSessionManagerServiceName,
27        dbus::ObjectPath(login_manager::kSessionManagerServicePath));
28
29    session_manager_proxy_->ConnectToSignal(
30        chromium::kChromiumInterface,
31        chromium::kOwnerKeySetSignal,
32        base::Bind(&SessionManagerClientImpl::OwnerKeySetReceived,
33                   weak_ptr_factory_.GetWeakPtr()),
34        base::Bind(&SessionManagerClientImpl::SignalConnected,
35                   weak_ptr_factory_.GetWeakPtr()));
36
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
45    session_manager_proxy_->ConnectToSignal(
46        chromium::kChromiumInterface,
47        chromium::kLockScreenSignal,
48        base::Bind(&SessionManagerClientImpl::ScreenLockReceived,
49                   weak_ptr_factory_.GetWeakPtr()),
50        base::Bind(&SessionManagerClientImpl::SignalConnected,
51                   weak_ptr_factory_.GetWeakPtr()));
52
53    session_manager_proxy_->ConnectToSignal(
54        chromium::kChromiumInterface,
55        chromium::kUnlockScreenSignal,
56        base::Bind(&SessionManagerClientImpl::ScreenUnlockReceived,
57                   weak_ptr_factory_.GetWeakPtr()),
58        base::Bind(&SessionManagerClientImpl::SignalConnected,
59                   weak_ptr_factory_.GetWeakPtr()));
60  }
61
62  virtual ~SessionManagerClientImpl() {
63  }
64
65  // SessionManagerClient overrides:
66  virtual void AddObserver(Observer* observer) OVERRIDE {
67    observers_.AddObserver(observer);
68  }
69
70  virtual void RemoveObserver(Observer* observer) OVERRIDE {
71    observers_.RemoveObserver(observer);
72  }
73
74  virtual bool HasObserver(Observer* observer) OVERRIDE {
75    return observers_.HasObserver(observer);
76  }
77
78  virtual void EmitLoginPromptReady() OVERRIDE {
79    SimpleMethodCallToSessionManager(
80        login_manager::kSessionManagerEmitLoginPromptReady);
81  }
82
83  virtual void EmitLoginPromptVisible() OVERRIDE {
84    SimpleMethodCallToSessionManager(
85        login_manager::kSessionManagerEmitLoginPromptVisible);
86  }
87
88  virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {
89    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
90                                 login_manager::kSessionManagerRestartJob);
91    dbus::MessageWriter writer(&method_call);
92    writer.AppendInt32(pid);
93    writer.AppendString(command_line);
94    session_manager_proxy_->CallMethod(
95        &method_call,
96        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
97        base::Bind(&SessionManagerClientImpl::OnRestartJob,
98                   weak_ptr_factory_.GetWeakPtr()));
99  }
100
101  virtual void RestartEntd() OVERRIDE {
102    SimpleMethodCallToSessionManager(login_manager::kSessionManagerRestartEntd);
103  }
104
105  virtual void StartSession(const std::string& user_email) OVERRIDE {
106    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
107                                 login_manager::kSessionManagerStartSession);
108    dbus::MessageWriter writer(&method_call);
109    writer.AppendString(user_email);
110    writer.AppendString("");  // Unique ID is deprecated
111    session_manager_proxy_->CallMethod(
112        &method_call,
113        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
114        base::Bind(&SessionManagerClientImpl::OnStartSession,
115                   weak_ptr_factory_.GetWeakPtr()));
116  }
117
118  virtual void StopSession() OVERRIDE {
119    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
120                                 login_manager::kSessionManagerStopSession);
121    dbus::MessageWriter writer(&method_call);
122    writer.AppendString("");  // Unique ID is deprecated
123    session_manager_proxy_->CallMethod(
124        &method_call,
125        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
126        base::Bind(&SessionManagerClientImpl::OnStopSession,
127                   weak_ptr_factory_.GetWeakPtr()));
128  }
129
130  virtual void StartDeviceWipe() OVERRIDE {
131    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
132                                 login_manager::kSessionManagerStartDeviceWipe);
133    session_manager_proxy_->CallMethod(
134        &method_call,
135        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
136        base::Bind(&SessionManagerClientImpl::OnDeviceWipe,
137                   weak_ptr_factory_.GetWeakPtr()));
138  }
139
140  virtual void RequestLockScreen() OVERRIDE {
141    SimpleMethodCallToSessionManager(login_manager::kSessionManagerLockScreen);
142  }
143
144  virtual void NotifyLockScreenShown() OVERRIDE {
145    SimpleMethodCallToSessionManager(
146        login_manager::kSessionManagerHandleLockScreenShown);
147  }
148
149  virtual void RequestUnlockScreen() OVERRIDE {
150    SimpleMethodCallToSessionManager(
151        login_manager::kSessionManagerUnlockScreen);
152  }
153
154  virtual void NotifyLockScreenDismissed() OVERRIDE {
155    SimpleMethodCallToSessionManager(
156        login_manager::kSessionManagerHandleLockScreenDismissed);
157  }
158
159  virtual bool GetIsScreenLocked() OVERRIDE {
160    return screen_locked_;
161  }
162
163  virtual void RetrieveDevicePolicy(
164      const RetrievePolicyCallback& callback) OVERRIDE {
165    CallRetrievePolicy(login_manager::kSessionManagerRetrievePolicy,
166                       callback);
167  }
168
169  virtual void RetrieveUserPolicy(
170      const RetrievePolicyCallback& callback) OVERRIDE {
171    CallRetrievePolicy(login_manager::kSessionManagerRetrieveUserPolicy,
172                       callback);
173  }
174
175  virtual void StoreDevicePolicy(const std::string& policy_blob,
176                                 const StorePolicyCallback& callback) OVERRIDE {
177    CallStorePolicy(login_manager::kSessionManagerStorePolicy,
178                    policy_blob, callback);
179  }
180
181  virtual void StoreUserPolicy(const std::string& policy_blob,
182                               const StorePolicyCallback& callback) OVERRIDE {
183    CallStorePolicy(login_manager::kSessionManagerStoreUserPolicy,
184                    policy_blob, callback);
185  }
186
187 private:
188  // Makes a method call to the session manager with no arguments and no
189  // response.
190  void SimpleMethodCallToSessionManager(const std::string& method_name) {
191    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
192                                 method_name);
193    session_manager_proxy_->CallMethod(
194        &method_call,
195        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
196        dbus::ObjectProxy::EmptyResponseCallback());
197  }
198
199  // Helper for Retrieve{User,Device}Policy.
200  virtual void CallRetrievePolicy(const std::string& method_name,
201                                  const RetrievePolicyCallback& callback) {
202    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
203                                 method_name);
204    session_manager_proxy_->CallMethod(
205        &method_call,
206        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
207        base::Bind(&SessionManagerClientImpl::OnRetrievePolicy,
208                   weak_ptr_factory_.GetWeakPtr(),
209                   method_name,
210                   callback));
211  }
212
213  // Helper for Store{User,Device}Policy.
214  virtual void CallStorePolicy(const std::string& method_name,
215                               const std::string& policy_blob,
216                               const StorePolicyCallback& callback) {
217    dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
218                                 method_name);
219    dbus::MessageWriter writer(&method_call);
220    // static_cast does not work due to signedness.
221    writer.AppendArrayOfBytes(
222        reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size());
223    session_manager_proxy_->CallMethod(
224        &method_call,
225        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
226        base::Bind(&SessionManagerClientImpl::OnStorePolicy,
227                   weak_ptr_factory_.GetWeakPtr(),
228                   method_name,
229                   callback));
230  }
231
232  // Called when kSessionManagerRestartJob method is complete.
233  void OnRestartJob(dbus::Response* response) {
234    LOG_IF(ERROR, !response)
235        << "Failed to call "
236        << login_manager::kSessionManagerRestartJob;
237  }
238
239  // Called when kSessionManagerStartSession method is complete.
240  void OnStartSession(dbus::Response* response) {
241    LOG_IF(ERROR, !response)
242        << "Failed to call "
243        << login_manager::kSessionManagerStartSession;
244  }
245
246  // Called when kSessionManagerStopSession method is complete.
247  void OnStopSession(dbus::Response* response) {
248    LOG_IF(ERROR, !response)
249        << "Failed to call "
250        << login_manager::kSessionManagerStopSession;
251  }
252
253  // Called when kSessionManagerStopSession method is complete.
254  void OnDeviceWipe(dbus::Response* response) {
255    LOG_IF(ERROR, !response)
256        << "Failed to call "
257        << login_manager::kSessionManagerStartDeviceWipe;
258  }
259
260  // Called when kSessionManagerRetrievePolicy or
261  // kSessionManagerRetrieveUserPolicy  method is complete.
262  void OnRetrievePolicy(const std::string& method_name,
263                        const RetrievePolicyCallback& callback,
264                        dbus::Response* response) {
265    if (!response) {
266      LOG(ERROR) << "Failed to call " << method_name;
267      callback.Run("");
268      return;
269    }
270    dbus::MessageReader reader(response);
271    uint8* values = NULL;
272    size_t length = 0;
273    if (!reader.PopArrayOfBytes(&values, &length)) {
274      LOG(ERROR) << "Invalid response: " << response->ToString();
275      callback.Run("");
276      return;
277    }
278    // static_cast does not work due to signedness.
279    std::string serialized_proto(reinterpret_cast<char*>(values), length);
280    callback.Run(serialized_proto);
281  }
282
283  // Called when kSessionManagerStorePolicy or kSessionManagerStoreUserPolicy
284  // method is complete.
285  void OnStorePolicy(const std::string& method_name,
286                     const StorePolicyCallback& callback,
287                     dbus::Response* response) {
288    bool success = false;
289    if (!response) {
290      LOG(ERROR) << "Failed to call " << method_name;
291    } else {
292      dbus::MessageReader reader(response);
293      if (!reader.PopBool(&success))
294        LOG(ERROR) << "Invalid response: " << response->ToString();
295    }
296    callback.Run(success);
297  }
298
299  // Called when the owner key set signal is received.
300  void OwnerKeySetReceived(dbus::Signal* signal) {
301    dbus::MessageReader reader(signal);
302    std::string result_string;
303    if (!reader.PopString(&result_string)) {
304      LOG(ERROR) << "Invalid signal: " << signal->ToString();
305      return;
306    }
307    const bool success = StartsWithASCII(result_string, "success", false);
308    FOR_EACH_OBSERVER(Observer, observers_, OwnerKeySet(success));
309  }
310
311  // Called when the property change complete signal is received.
312  void PropertyChangeCompleteReceived(dbus::Signal* signal) {
313    dbus::MessageReader reader(signal);
314    std::string result_string;
315    if (!reader.PopString(&result_string)) {
316      LOG(ERROR) << "Invalid signal: " << signal->ToString();
317      return;
318    }
319    const bool success = StartsWithASCII(result_string, "success", false);
320    FOR_EACH_OBSERVER(Observer, observers_, PropertyChangeComplete(success));
321  }
322
323  void ScreenLockReceived(dbus::Signal* signal) {
324    screen_locked_ = true;
325    FOR_EACH_OBSERVER(Observer, observers_, LockScreen());
326  }
327
328  void ScreenUnlockReceived(dbus::Signal* signal) {
329    screen_locked_ = false;
330    FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen());
331  }
332
333  // Called when the object is connected to the signal.
334  void SignalConnected(const std::string& interface_name,
335                       const std::string& signal_name,
336                       bool success) {
337    LOG_IF(ERROR, !success) << "Failed to connect to " << signal_name;
338  }
339
340  dbus::ObjectProxy* session_manager_proxy_;
341  ObserverList<Observer> observers_;
342  bool screen_locked_;
343
344  // Note: This should remain the last member so it'll be destroyed and
345  // invalidate its weak pointers before any other members are destroyed.
346  base::WeakPtrFactory<SessionManagerClientImpl> weak_ptr_factory_;
347
348  DISALLOW_COPY_AND_ASSIGN(SessionManagerClientImpl);
349};
350
351// The SessionManagerClient implementation used on Linux desktop,
352// which does nothing.
353class SessionManagerClientStubImpl : public SessionManagerClient {
354 public:
355  SessionManagerClientStubImpl() : screen_locked_(false) {}
356  virtual ~SessionManagerClientStubImpl() {}
357
358  // SessionManagerClient overrides.
359  virtual void AddObserver(Observer* observer) OVERRIDE {
360    observers_.AddObserver(observer);
361  }
362  virtual void RemoveObserver(Observer* observer) OVERRIDE {
363    observers_.RemoveObserver(observer);
364  }
365  virtual bool HasObserver(Observer* observer) OVERRIDE {
366    return observers_.HasObserver(observer);
367  }
368  virtual void EmitLoginPromptReady() OVERRIDE {}
369  virtual void EmitLoginPromptVisible() OVERRIDE {}
370  virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {}
371  virtual void RestartEntd() OVERRIDE {}
372  virtual void StartSession(const std::string& user_email) OVERRIDE {}
373  virtual void StopSession() OVERRIDE {}
374  virtual void StartDeviceWipe() OVERRIDE {}
375  virtual void RequestLockScreen() OVERRIDE {
376    screen_locked_ = true;
377    FOR_EACH_OBSERVER(Observer, observers_, LockScreen());
378  }
379  virtual void NotifyLockScreenShown() OVERRIDE {}
380  virtual void RequestUnlockScreen() OVERRIDE {
381    screen_locked_ = false;
382    FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen());
383  }
384  virtual void NotifyLockScreenDismissed() OVERRIDE {}
385  virtual bool GetIsScreenLocked() OVERRIDE { return screen_locked_; }
386  virtual void RetrieveDevicePolicy(
387      const RetrievePolicyCallback& callback) OVERRIDE {
388    callback.Run("");
389  }
390  virtual void RetrieveUserPolicy(
391      const RetrievePolicyCallback& callback) OVERRIDE {
392    callback.Run("");
393  }
394  virtual void StoreDevicePolicy(const std::string& policy_blob,
395                                 const StorePolicyCallback& callback) OVERRIDE {
396    callback.Run(true);
397  }
398  virtual void StoreUserPolicy(const std::string& policy_blob,
399                               const StorePolicyCallback& callback) OVERRIDE {
400    callback.Run(true);
401  }
402
403 private:
404  ObserverList<Observer> observers_;
405  bool screen_locked_;
406
407  DISALLOW_COPY_AND_ASSIGN(SessionManagerClientStubImpl);
408};
409
410SessionManagerClient::SessionManagerClient() {
411}
412
413SessionManagerClient::~SessionManagerClient() {
414}
415
416SessionManagerClient* SessionManagerClient::Create(
417    DBusClientImplementationType type,
418    dbus::Bus* bus) {
419  if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
420    return new SessionManagerClientImpl(bus);
421  DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
422  return new SessionManagerClientStubImpl();
423}
424
425}  // namespace chromeos
426