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