bluetooth: Shutdown BluetoothAdapter before DBus on ChromeOS.
[chromium-blink-merge.git] / chromeos / dbus / session_manager_client.cc
blob825699b49510064b86210cea707fbff80e66ffea
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/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/task_runner_util.h"
16 #include "base/threading/worker_pool.h"
17 #include "chromeos/chromeos_paths.h"
18 #include "chromeos/dbus/blocking_method_caller.h"
19 #include "chromeos/dbus/cryptohome_client.h"
20 #include "crypto/sha2.h"
21 #include "dbus/bus.h"
22 #include "dbus/message.h"
23 #include "dbus/object_path.h"
24 #include "dbus/object_proxy.h"
25 #include "policy/proto/device_management_backend.pb.h"
26 #include "third_party/cros_system_api/dbus/service_constants.h"
28 namespace chromeos {
30 namespace {
32 // Returns a location for |file| that is specific to the given |username|.
33 // These paths will be relative to DIR_USER_POLICY_KEYS, and can be used only
34 // to store stub files.
35 base::FilePath GetUserFilePath(const std::string& username, const char* file) {
36 base::FilePath keys_path;
37 if (!PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &keys_path))
38 return base::FilePath();
39 const std::string sanitized =
40 CryptohomeClient::GetStubSanitizedUsername(username);
41 return keys_path.AppendASCII(sanitized).AppendASCII(file);
44 // Helper to asynchronously retrieve a file's content.
45 std::string GetFileContent(const base::FilePath& path) {
46 std::string result;
47 if (!path.empty())
48 base::ReadFileToString(path, &result);
49 return result;
52 // Helper to write a file in a background thread.
53 void StoreFile(const base::FilePath& path, const std::string& data) {
54 const int size = static_cast<int>(data.size());
55 if (path.empty() ||
56 !base::CreateDirectory(path.DirName()) ||
57 base::WriteFile(path, data.data(), size) != size) {
58 LOG(WARNING) << "Failed to write to " << path.value();
62 } // namespace
64 // The SessionManagerClient implementation used in production.
65 class SessionManagerClientImpl : public SessionManagerClient {
66 public:
67 SessionManagerClientImpl()
68 : session_manager_proxy_(NULL),
69 screen_is_locked_(false),
70 weak_ptr_factory_(this) {}
72 ~SessionManagerClientImpl() override {}
74 // SessionManagerClient overrides:
75 void SetStubDelegate(StubDelegate* delegate) override {
76 // Do nothing; this isn't a stub implementation.
79 void AddObserver(Observer* observer) override {
80 observers_.AddObserver(observer);
83 void RemoveObserver(Observer* observer) override {
84 observers_.RemoveObserver(observer);
87 bool HasObserver(const Observer* observer) const override {
88 return observers_.HasObserver(observer);
91 bool IsScreenLocked() const override { return screen_is_locked_; }
93 void EmitLoginPromptVisible() override {
94 SimpleMethodCallToSessionManager(
95 login_manager::kSessionManagerEmitLoginPromptVisible);
96 FOR_EACH_OBSERVER(Observer, observers_, EmitLoginPromptVisibleCalled());
99 void RestartJob(int pid, const std::string& command_line) override {
100 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
101 login_manager::kSessionManagerRestartJob);
102 dbus::MessageWriter writer(&method_call);
103 writer.AppendInt32(pid);
104 writer.AppendString(command_line);
105 session_manager_proxy_->CallMethod(
106 &method_call,
107 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
108 base::Bind(&SessionManagerClientImpl::OnRestartJob,
109 weak_ptr_factory_.GetWeakPtr()));
112 void StartSession(const std::string& user_email) override {
113 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
114 login_manager::kSessionManagerStartSession);
115 dbus::MessageWriter writer(&method_call);
116 writer.AppendString(user_email);
117 writer.AppendString(""); // Unique ID is deprecated
118 session_manager_proxy_->CallMethod(
119 &method_call,
120 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
121 base::Bind(&SessionManagerClientImpl::OnStartSession,
122 weak_ptr_factory_.GetWeakPtr()));
125 void StopSession() override {
126 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
127 login_manager::kSessionManagerStopSession);
128 dbus::MessageWriter writer(&method_call);
129 writer.AppendString(""); // Unique ID is deprecated
130 session_manager_proxy_->CallMethod(
131 &method_call,
132 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
133 base::Bind(&SessionManagerClientImpl::OnStopSession,
134 weak_ptr_factory_.GetWeakPtr()));
137 void StartDeviceWipe() override {
138 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
139 login_manager::kSessionManagerStartDeviceWipe);
140 session_manager_proxy_->CallMethod(
141 &method_call,
142 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
143 base::Bind(&SessionManagerClientImpl::OnDeviceWipe,
144 weak_ptr_factory_.GetWeakPtr()));
147 void RequestLockScreen() override {
148 SimpleMethodCallToSessionManager(login_manager::kSessionManagerLockScreen);
151 void NotifyLockScreenShown() override {
152 SimpleMethodCallToSessionManager(
153 login_manager::kSessionManagerHandleLockScreenShown);
156 void NotifyLockScreenDismissed() override {
157 SimpleMethodCallToSessionManager(
158 login_manager::kSessionManagerHandleLockScreenDismissed);
161 void NotifySupervisedUserCreationStarted() override {
162 SimpleMethodCallToSessionManager(
163 login_manager::kSessionManagerHandleSupervisedUserCreationStarting);
166 void NotifySupervisedUserCreationFinished() override {
167 SimpleMethodCallToSessionManager(
168 login_manager::kSessionManagerHandleSupervisedUserCreationFinished);
171 void RetrieveActiveSessions(const ActiveSessionsCallback& callback) override {
172 dbus::MethodCall method_call(
173 login_manager::kSessionManagerInterface,
174 login_manager::kSessionManagerRetrieveActiveSessions);
176 session_manager_proxy_->CallMethod(
177 &method_call,
178 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
179 base::Bind(&SessionManagerClientImpl::OnRetrieveActiveSessions,
180 weak_ptr_factory_.GetWeakPtr(),
181 login_manager::kSessionManagerRetrieveActiveSessions,
182 callback));
185 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override {
186 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
187 login_manager::kSessionManagerRetrievePolicy);
188 session_manager_proxy_->CallMethod(
189 &method_call,
190 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
191 base::Bind(&SessionManagerClientImpl::OnRetrievePolicy,
192 weak_ptr_factory_.GetWeakPtr(),
193 login_manager::kSessionManagerRetrievePolicy,
194 callback));
197 void RetrievePolicyForUser(const std::string& username,
198 const RetrievePolicyCallback& callback) override {
199 CallRetrievePolicyByUsername(
200 login_manager::kSessionManagerRetrievePolicyForUser,
201 username,
202 callback);
205 std::string BlockingRetrievePolicyForUser(
206 const std::string& username) override {
207 dbus::MethodCall method_call(
208 login_manager::kSessionManagerInterface,
209 login_manager::kSessionManagerRetrievePolicyForUser);
210 dbus::MessageWriter writer(&method_call);
211 writer.AppendString(username);
212 scoped_ptr<dbus::Response> response =
213 blocking_method_caller_->CallMethodAndBlock(&method_call);
214 std::string policy;
215 ExtractString(login_manager::kSessionManagerRetrievePolicyForUser,
216 response.get(),
217 &policy);
218 return policy;
221 void RetrieveDeviceLocalAccountPolicy(
222 const std::string& account_name,
223 const RetrievePolicyCallback& callback) override {
224 CallRetrievePolicyByUsername(
225 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy,
226 account_name,
227 callback);
230 void StoreDevicePolicy(const std::string& policy_blob,
231 const StorePolicyCallback& callback) override {
232 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
233 login_manager::kSessionManagerStorePolicy);
234 dbus::MessageWriter writer(&method_call);
235 // static_cast does not work due to signedness.
236 writer.AppendArrayOfBytes(
237 reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size());
238 session_manager_proxy_->CallMethod(
239 &method_call,
240 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
241 base::Bind(&SessionManagerClientImpl::OnStorePolicy,
242 weak_ptr_factory_.GetWeakPtr(),
243 login_manager::kSessionManagerStorePolicy,
244 callback));
247 void StorePolicyForUser(const std::string& username,
248 const std::string& policy_blob,
249 const StorePolicyCallback& callback) override {
250 CallStorePolicyByUsername(login_manager::kSessionManagerStorePolicyForUser,
251 username,
252 policy_blob,
253 callback);
256 void StoreDeviceLocalAccountPolicy(
257 const std::string& account_name,
258 const std::string& policy_blob,
259 const StorePolicyCallback& callback) override {
260 CallStorePolicyByUsername(
261 login_manager::kSessionManagerStoreDeviceLocalAccountPolicy,
262 account_name,
263 policy_blob,
264 callback);
267 void SetFlagsForUser(const std::string& username,
268 const std::vector<std::string>& flags) override {
269 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
270 login_manager::kSessionManagerSetFlagsForUser);
271 dbus::MessageWriter writer(&method_call);
272 writer.AppendString(username);
273 writer.AppendArrayOfStrings(flags);
274 session_manager_proxy_->CallMethod(
275 &method_call,
276 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
277 dbus::ObjectProxy::EmptyResponseCallback());
280 void GetServerBackedStateKeys(const StateKeysCallback& callback) override {
281 dbus::MethodCall method_call(
282 login_manager::kSessionManagerInterface,
283 login_manager::kSessionManagerGetServerBackedStateKeys);
285 session_manager_proxy_->CallMethod(
286 &method_call,
287 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
288 base::Bind(&SessionManagerClientImpl::OnGetServerBackedStateKeys,
289 weak_ptr_factory_.GetWeakPtr(),
290 callback));
293 protected:
294 void Init(dbus::Bus* bus) override {
295 session_manager_proxy_ = bus->GetObjectProxy(
296 login_manager::kSessionManagerServiceName,
297 dbus::ObjectPath(login_manager::kSessionManagerServicePath));
298 blocking_method_caller_.reset(
299 new BlockingMethodCaller(bus, session_manager_proxy_));
301 // Signals emitted on the session manager's interface.
302 session_manager_proxy_->ConnectToSignal(
303 login_manager::kSessionManagerInterface,
304 login_manager::kOwnerKeySetSignal,
305 base::Bind(&SessionManagerClientImpl::OwnerKeySetReceived,
306 weak_ptr_factory_.GetWeakPtr()),
307 base::Bind(&SessionManagerClientImpl::SignalConnected,
308 weak_ptr_factory_.GetWeakPtr()));
309 session_manager_proxy_->ConnectToSignal(
310 login_manager::kSessionManagerInterface,
311 login_manager::kPropertyChangeCompleteSignal,
312 base::Bind(&SessionManagerClientImpl::PropertyChangeCompleteReceived,
313 weak_ptr_factory_.GetWeakPtr()),
314 base::Bind(&SessionManagerClientImpl::SignalConnected,
315 weak_ptr_factory_.GetWeakPtr()));
316 session_manager_proxy_->ConnectToSignal(
317 login_manager::kSessionManagerInterface,
318 login_manager::kScreenIsLockedSignal,
319 base::Bind(&SessionManagerClientImpl::ScreenIsLockedReceived,
320 weak_ptr_factory_.GetWeakPtr()),
321 base::Bind(&SessionManagerClientImpl::SignalConnected,
322 weak_ptr_factory_.GetWeakPtr()));
323 session_manager_proxy_->ConnectToSignal(
324 login_manager::kSessionManagerInterface,
325 login_manager::kScreenIsUnlockedSignal,
326 base::Bind(&SessionManagerClientImpl::ScreenIsUnlockedReceived,
327 weak_ptr_factory_.GetWeakPtr()),
328 base::Bind(&SessionManagerClientImpl::SignalConnected,
329 weak_ptr_factory_.GetWeakPtr()));
332 private:
333 // Makes a method call to the session manager with no arguments and no
334 // response.
335 void SimpleMethodCallToSessionManager(const std::string& method_name) {
336 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
337 method_name);
338 session_manager_proxy_->CallMethod(
339 &method_call,
340 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
341 dbus::ObjectProxy::EmptyResponseCallback());
344 // Helper for RetrieveDeviceLocalAccountPolicy and RetrievePolicyForUser.
345 void CallRetrievePolicyByUsername(const std::string& method_name,
346 const std::string& username,
347 const RetrievePolicyCallback& callback) {
348 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
349 method_name);
350 dbus::MessageWriter writer(&method_call);
351 writer.AppendString(username);
352 session_manager_proxy_->CallMethod(
353 &method_call,
354 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
355 base::Bind(
356 &SessionManagerClientImpl::OnRetrievePolicy,
357 weak_ptr_factory_.GetWeakPtr(),
358 method_name,
359 callback));
362 void CallStorePolicyByUsername(const std::string& method_name,
363 const std::string& username,
364 const std::string& policy_blob,
365 const StorePolicyCallback& callback) {
366 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
367 method_name);
368 dbus::MessageWriter writer(&method_call);
369 writer.AppendString(username);
370 // static_cast does not work due to signedness.
371 writer.AppendArrayOfBytes(
372 reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size());
373 session_manager_proxy_->CallMethod(
374 &method_call,
375 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
376 base::Bind(
377 &SessionManagerClientImpl::OnStorePolicy,
378 weak_ptr_factory_.GetWeakPtr(),
379 method_name,
380 callback));
383 // Called when kSessionManagerRestartJob method is complete.
384 void OnRestartJob(dbus::Response* response) {
385 LOG_IF(ERROR, !response)
386 << "Failed to call "
387 << login_manager::kSessionManagerRestartJob;
390 // Called when kSessionManagerStartSession method is complete.
391 void OnStartSession(dbus::Response* response) {
392 LOG_IF(ERROR, !response)
393 << "Failed to call "
394 << login_manager::kSessionManagerStartSession;
397 // Called when kSessionManagerStopSession method is complete.
398 void OnStopSession(dbus::Response* response) {
399 LOG_IF(ERROR, !response)
400 << "Failed to call "
401 << login_manager::kSessionManagerStopSession;
404 // Called when kSessionManagerStopSession method is complete.
405 void OnDeviceWipe(dbus::Response* response) {
406 LOG_IF(ERROR, !response)
407 << "Failed to call "
408 << login_manager::kSessionManagerStartDeviceWipe;
411 // Called when kSessionManagerRetrieveActiveSessions method is complete.
412 void OnRetrieveActiveSessions(const std::string& method_name,
413 const ActiveSessionsCallback& callback,
414 dbus::Response* response) {
415 ActiveSessionsMap sessions;
416 bool success = false;
417 if (!response) {
418 LOG(ERROR) << "Failed to call " << method_name;
419 callback.Run(sessions, success);
420 return;
423 dbus::MessageReader reader(response);
424 dbus::MessageReader array_reader(NULL);
426 if (!reader.PopArray(&array_reader)) {
427 LOG(ERROR) << method_name << " response is incorrect: "
428 << response->ToString();
429 } else {
430 while (array_reader.HasMoreData()) {
431 dbus::MessageReader dict_entry_reader(NULL);
432 std::string key;
433 std::string value;
434 if (!array_reader.PopDictEntry(&dict_entry_reader) ||
435 !dict_entry_reader.PopString(&key) ||
436 !dict_entry_reader.PopString(&value)) {
437 LOG(ERROR) << method_name << " response is incorrect: "
438 << response->ToString();
439 } else {
440 sessions[key] = value;
443 success = true;
445 callback.Run(sessions, success);
448 void ExtractString(const std::string& method_name,
449 dbus::Response* response,
450 std::string* extracted) {
451 if (!response) {
452 LOG(ERROR) << "Failed to call " << method_name;
453 return;
455 dbus::MessageReader reader(response);
456 const uint8* values = NULL;
457 size_t length = 0;
458 if (!reader.PopArrayOfBytes(&values, &length)) {
459 LOG(ERROR) << "Invalid response: " << response->ToString();
460 return;
462 // static_cast does not work due to signedness.
463 extracted->assign(reinterpret_cast<const char*>(values), length);
466 // Called when kSessionManagerRetrievePolicy or
467 // kSessionManagerRetrievePolicyForUser method is complete.
468 void OnRetrievePolicy(const std::string& method_name,
469 const RetrievePolicyCallback& callback,
470 dbus::Response* response) {
471 std::string serialized_proto;
472 ExtractString(method_name, response, &serialized_proto);
473 callback.Run(serialized_proto);
476 // Called when kSessionManagerStorePolicy or kSessionManagerStorePolicyForUser
477 // method is complete.
478 void OnStorePolicy(const std::string& method_name,
479 const StorePolicyCallback& callback,
480 dbus::Response* response) {
481 bool success = false;
482 if (!response) {
483 LOG(ERROR) << "Failed to call " << method_name;
484 } else {
485 dbus::MessageReader reader(response);
486 if (!reader.PopBool(&success))
487 LOG(ERROR) << "Invalid response: " << response->ToString();
489 callback.Run(success);
492 // Called when the owner key set signal is received.
493 void OwnerKeySetReceived(dbus::Signal* signal) {
494 dbus::MessageReader reader(signal);
495 std::string result_string;
496 if (!reader.PopString(&result_string)) {
497 LOG(ERROR) << "Invalid signal: " << signal->ToString();
498 return;
500 const bool success = StartsWithASCII(result_string, "success", false);
501 FOR_EACH_OBSERVER(Observer, observers_, OwnerKeySet(success));
504 // Called when the property change complete signal is received.
505 void PropertyChangeCompleteReceived(dbus::Signal* signal) {
506 dbus::MessageReader reader(signal);
507 std::string result_string;
508 if (!reader.PopString(&result_string)) {
509 LOG(ERROR) << "Invalid signal: " << signal->ToString();
510 return;
512 const bool success = StartsWithASCII(result_string, "success", false);
513 FOR_EACH_OBSERVER(Observer, observers_, PropertyChangeComplete(success));
516 void ScreenIsLockedReceived(dbus::Signal* signal) {
517 screen_is_locked_ = true;
518 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsLocked());
521 void ScreenIsUnlockedReceived(dbus::Signal* signal) {
522 screen_is_locked_ = false;
523 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsUnlocked());
526 // Called when the object is connected to the signal.
527 void SignalConnected(const std::string& interface_name,
528 const std::string& signal_name,
529 bool success) {
530 LOG_IF(ERROR, !success) << "Failed to connect to " << signal_name;
533 // Called when kSessionManagerGetServerBackedStateKeys method is complete.
534 void OnGetServerBackedStateKeys(const StateKeysCallback& callback,
535 dbus::Response* response) {
536 std::vector<std::string> state_keys;
537 if (!response) {
538 LOG(ERROR) << "Failed to call "
539 << login_manager::kSessionManagerStartSession;
540 } else {
541 dbus::MessageReader reader(response);
542 dbus::MessageReader array_reader(NULL);
544 if (!reader.PopArray(&array_reader)) {
545 LOG(ERROR) << "Bad response: " << response->ToString();
546 } else {
547 while (array_reader.HasMoreData()) {
548 const uint8* data = NULL;
549 size_t size = 0;
550 if (!array_reader.PopArrayOfBytes(&data, &size)) {
551 LOG(ERROR) << "Bad response: " << response->ToString();
552 state_keys.clear();
553 break;
555 state_keys.push_back(
556 std::string(reinterpret_cast<const char*>(data), size));
561 if (!callback.is_null())
562 callback.Run(state_keys);
566 dbus::ObjectProxy* session_manager_proxy_;
567 scoped_ptr<BlockingMethodCaller> blocking_method_caller_;
568 ObserverList<Observer> observers_;
570 // Most recent screen-lock state received from session_manager.
571 bool screen_is_locked_;
573 // Note: This should remain the last member so it'll be destroyed and
574 // invalidate its weak pointers before any other members are destroyed.
575 base::WeakPtrFactory<SessionManagerClientImpl> weak_ptr_factory_;
577 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientImpl);
580 // The SessionManagerClient implementation used on Linux desktop,
581 // which does nothing.
582 class SessionManagerClientStubImpl : public SessionManagerClient {
583 public:
584 SessionManagerClientStubImpl() : delegate_(NULL), screen_is_locked_(false) {}
585 ~SessionManagerClientStubImpl() override {}
587 // SessionManagerClient overrides
588 void Init(dbus::Bus* bus) override {}
589 void SetStubDelegate(StubDelegate* delegate) override {
590 delegate_ = delegate;
592 void AddObserver(Observer* observer) override {
593 observers_.AddObserver(observer);
595 void RemoveObserver(Observer* observer) override {
596 observers_.RemoveObserver(observer);
598 bool HasObserver(const Observer* observer) const override {
599 return observers_.HasObserver(observer);
601 bool IsScreenLocked() const override { return screen_is_locked_; }
602 void EmitLoginPromptVisible() override {}
603 void RestartJob(int pid, const std::string& command_line) override {}
604 void StartSession(const std::string& user_email) override {}
605 void StopSession() override {}
606 void NotifySupervisedUserCreationStarted() override {}
607 void NotifySupervisedUserCreationFinished() override {}
608 void StartDeviceWipe() override {}
609 void RequestLockScreen() override {
610 if (delegate_)
611 delegate_->LockScreenForStub();
613 void NotifyLockScreenShown() override {
614 screen_is_locked_ = true;
615 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsLocked());
617 void NotifyLockScreenDismissed() override {
618 screen_is_locked_ = false;
619 FOR_EACH_OBSERVER(Observer, observers_, ScreenIsUnlocked());
621 void RetrieveActiveSessions(const ActiveSessionsCallback& callback) override {
623 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override {
624 base::FilePath owner_key_path;
625 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) {
626 callback.Run("");
627 return;
629 base::FilePath device_policy_path =
630 owner_key_path.DirName().AppendASCII("stub_device_policy");
631 base::PostTaskAndReplyWithResult(
632 base::WorkerPool::GetTaskRunner(false).get(),
633 FROM_HERE,
634 base::Bind(&GetFileContent, device_policy_path),
635 callback);
637 void RetrievePolicyForUser(const std::string& username,
638 const RetrievePolicyCallback& callback) override {
639 base::PostTaskAndReplyWithResult(
640 base::WorkerPool::GetTaskRunner(false).get(),
641 FROM_HERE,
642 base::Bind(&GetFileContent, GetUserFilePath(username, "stub_policy")),
643 callback);
645 std::string BlockingRetrievePolicyForUser(
646 const std::string& username) override {
647 return GetFileContent(GetUserFilePath(username, "stub_policy"));
649 void RetrieveDeviceLocalAccountPolicy(
650 const std::string& account_name,
651 const RetrievePolicyCallback& callback) override {
652 RetrievePolicyForUser(account_name, callback);
654 void StoreDevicePolicy(const std::string& policy_blob,
655 const StorePolicyCallback& callback) override {
656 enterprise_management::PolicyFetchResponse response;
657 base::FilePath owner_key_path;
658 if (!response.ParseFromString(policy_blob) ||
659 !PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) {
660 callback.Run(false);
661 return;
664 if (response.has_new_public_key()) {
665 base::WorkerPool::PostTask(
666 FROM_HERE,
667 base::Bind(&StoreFile, owner_key_path, response.new_public_key()),
668 false);
671 // Chrome will attempt to retrieve the device policy right after storing
672 // during enrollment, so make sure it's written before signaling
673 // completion.
674 // Note also that the owner key will be written before the device policy,
675 // if it was present in the blob.
676 base::FilePath device_policy_path =
677 owner_key_path.DirName().AppendASCII("stub_device_policy");
678 base::WorkerPool::PostTaskAndReply(
679 FROM_HERE,
680 base::Bind(&StoreFile, device_policy_path, policy_blob),
681 base::Bind(callback, true),
682 false);
684 void StorePolicyForUser(const std::string& username,
685 const std::string& policy_blob,
686 const StorePolicyCallback& callback) override {
687 // The session manager writes the user policy key to a well-known
688 // location. Do the same with the stub impl, so that user policy works and
689 // can be tested on desktop builds.
690 enterprise_management::PolicyFetchResponse response;
691 if (!response.ParseFromString(policy_blob)) {
692 callback.Run(false);
693 return;
696 if (response.has_new_public_key()) {
697 base::FilePath key_path = GetUserFilePath(username, "policy.pub");
698 base::WorkerPool::PostTask(
699 FROM_HERE,
700 base::Bind(&StoreFile, key_path, response.new_public_key()),
701 false);
704 // This file isn't read directly by Chrome, but is used by this class to
705 // reload the user policy across restarts.
706 base::FilePath stub_policy_path = GetUserFilePath(username, "stub_policy");
707 base::WorkerPool::PostTaskAndReply(
708 FROM_HERE,
709 base::Bind(&StoreFile, stub_policy_path, policy_blob),
710 base::Bind(callback, true),
711 false);
713 void StoreDeviceLocalAccountPolicy(
714 const std::string& account_name,
715 const std::string& policy_blob,
716 const StorePolicyCallback& callback) override {
717 StorePolicyForUser(account_name, policy_blob, callback);
719 void SetFlagsForUser(const std::string& username,
720 const std::vector<std::string>& flags) override {}
722 void GetServerBackedStateKeys(const StateKeysCallback& callback) override {
723 std::vector<std::string> state_keys;
724 for (int i = 0; i < 5; ++i)
725 state_keys.push_back(crypto::SHA256HashString(base::IntToString(i)));
727 if (!callback.is_null())
728 callback.Run(state_keys);
731 private:
732 StubDelegate* delegate_; // Weak pointer; may be NULL.
733 ObserverList<Observer> observers_;
734 std::string device_policy_;
735 bool screen_is_locked_;
737 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientStubImpl);
740 SessionManagerClient::SessionManagerClient() {
743 SessionManagerClient::~SessionManagerClient() {
746 SessionManagerClient* SessionManagerClient::Create(
747 DBusClientImplementationType type) {
748 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
749 return new SessionManagerClientImpl();
750 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
751 return new SessionManagerClientStubImpl();
754 } // namespace chromeos