OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/session_manager_client.h" | 5 #include "chromeos/dbus/session_manager_client.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/metrics/histogram_macros.h" | |
18 #include "base/path_service.h" | 19 #include "base/path_service.h" |
19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
21 #include "base/task_scheduler/post_task.h" | 22 #include "base/task_scheduler/post_task.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
23 #include "chromeos/chromeos_paths.h" | 24 #include "chromeos/chromeos_paths.h" |
24 #include "chromeos/cryptohome/cryptohome_parameters.h" | 25 #include "chromeos/cryptohome/cryptohome_parameters.h" |
25 #include "chromeos/dbus/blocking_method_caller.h" | 26 #include "chromeos/dbus/blocking_method_caller.h" |
26 #include "chromeos/dbus/cryptohome_client.h" | 27 #include "chromeos/dbus/cryptohome_client.h" |
27 #include "components/policy/proto/device_management_backend.pb.h" | 28 #include "components/policy/proto/device_management_backend.pb.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 // Helper to write a file in a background thread. | 68 // Helper to write a file in a background thread. |
68 void StoreFile(const base::FilePath& path, const std::string& data) { | 69 void StoreFile(const base::FilePath& path, const std::string& data) { |
69 const int size = static_cast<int>(data.size()); | 70 const int size = static_cast<int>(data.size()); |
70 if (path.empty() || | 71 if (path.empty() || |
71 !base::CreateDirectory(path.DirName()) || | 72 !base::CreateDirectory(path.DirName()) || |
72 base::WriteFile(path, data.data(), size) != size) { | 73 base::WriteFile(path, data.data(), size) != size) { |
73 LOG(WARNING) << "Failed to write to " << path.value(); | 74 LOG(WARNING) << "Failed to write to " << path.value(); |
74 } | 75 } |
75 } | 76 } |
76 | 77 |
78 // Helper to notify the callback with SUCCESS, to be used by the stub. | |
79 void NotifyOnRetrievePolicySuccess( | |
80 const SessionManagerClient::RetrievePolicyCallback& callback, | |
81 const std::string& protobuf) { | |
82 callback.Run(protobuf, SessionManagerClient::SUCCESS); | |
83 } | |
84 | |
77 } // namespace | 85 } // namespace |
78 | 86 |
79 // The SessionManagerClient implementation used in production. | 87 // The SessionManagerClient implementation used in production. |
80 class SessionManagerClientImpl : public SessionManagerClient { | 88 class SessionManagerClientImpl : public SessionManagerClient { |
81 public: | 89 public: |
82 SessionManagerClientImpl() | 90 SessionManagerClientImpl() |
83 : session_manager_proxy_(NULL), | 91 : session_manager_proxy_(NULL), |
84 screen_is_locked_(false), | 92 screen_is_locked_(false), |
85 weak_ptr_factory_(this) {} | 93 weak_ptr_factory_(this) {} |
86 | 94 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 203 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
196 base::Bind(&SessionManagerClientImpl::OnRetrieveActiveSessions, | 204 base::Bind(&SessionManagerClientImpl::OnRetrieveActiveSessions, |
197 weak_ptr_factory_.GetWeakPtr(), | 205 weak_ptr_factory_.GetWeakPtr(), |
198 login_manager::kSessionManagerRetrieveActiveSessions, | 206 login_manager::kSessionManagerRetrieveActiveSessions, |
199 callback)); | 207 callback)); |
200 } | 208 } |
201 | 209 |
202 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override { | 210 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override { |
203 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | 211 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, |
204 login_manager::kSessionManagerRetrievePolicy); | 212 login_manager::kSessionManagerRetrievePolicy); |
205 session_manager_proxy_->CallMethod( | 213 session_manager_proxy_->CallMethodWithErrorCallback( |
206 &method_call, | 214 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
207 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 215 base::Bind(&SessionManagerClientImpl::OnRetrievePolicySuccess, |
208 base::Bind(&SessionManagerClientImpl::OnRetrievePolicy, | |
209 weak_ptr_factory_.GetWeakPtr(), | 216 weak_ptr_factory_.GetWeakPtr(), |
210 login_manager::kSessionManagerRetrievePolicy, | 217 login_manager::kSessionManagerRetrievePolicy, callback), |
211 callback)); | 218 base::Bind(&SessionManagerClientImpl::OnRetrievePolicyError, |
219 weak_ptr_factory_.GetWeakPtr(), callback)); | |
212 } | 220 } |
213 | 221 |
214 std::string BlockingRetrieveDevicePolicy() override { | 222 std::string BlockingRetrieveDevicePolicy() override { |
215 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | 223 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, |
216 login_manager::kSessionManagerRetrievePolicy); | 224 login_manager::kSessionManagerRetrievePolicy); |
217 std::unique_ptr<dbus::Response> response = | 225 std::unique_ptr<dbus::Response> response = |
218 blocking_method_caller_->CallMethodAndBlock(&method_call); | 226 blocking_method_caller_->CallMethodAndBlock(&method_call); |
219 std::string policy; | 227 std::string policy; |
220 ExtractString(login_manager::kSessionManagerRetrievePolicy, response.get(), | 228 ExtractString(login_manager::kSessionManagerRetrievePolicy, response.get(), |
221 &policy); | 229 &policy); |
(...skipping 21 matching lines...) Expand all Loading... | |
243 response.get(), | 251 response.get(), |
244 &policy); | 252 &policy); |
245 return policy; | 253 return policy; |
246 } | 254 } |
247 | 255 |
248 void RetrieveDeviceLocalAccountPolicy( | 256 void RetrieveDeviceLocalAccountPolicy( |
249 const std::string& account_name, | 257 const std::string& account_name, |
250 const RetrievePolicyCallback& callback) override { | 258 const RetrievePolicyCallback& callback) override { |
251 CallRetrievePolicyByUsername( | 259 CallRetrievePolicyByUsername( |
252 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy, | 260 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy, |
253 account_name, | 261 account_name, callback); |
254 callback); | |
255 } | 262 } |
256 | 263 |
257 std::string BlockingRetrieveDeviceLocalAccountPolicy( | 264 std::string BlockingRetrieveDeviceLocalAccountPolicy( |
258 const std::string& account_name) override { | 265 const std::string& account_name) override { |
259 dbus::MethodCall method_call( | 266 dbus::MethodCall method_call( |
260 login_manager::kSessionManagerInterface, | 267 login_manager::kSessionManagerInterface, |
261 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy); | 268 login_manager::kSessionManagerRetrieveDeviceLocalAccountPolicy); |
262 dbus::MessageWriter writer(&method_call); | 269 dbus::MessageWriter writer(&method_call); |
263 writer.AppendString(account_name); | 270 writer.AppendString(account_name); |
264 std::unique_ptr<dbus::Response> response = | 271 std::unique_ptr<dbus::Response> response = |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
488 } | 495 } |
489 | 496 |
490 // Helper for RetrieveDeviceLocalAccountPolicy and RetrievePolicyForUser. | 497 // Helper for RetrieveDeviceLocalAccountPolicy and RetrievePolicyForUser. |
491 void CallRetrievePolicyByUsername(const std::string& method_name, | 498 void CallRetrievePolicyByUsername(const std::string& method_name, |
492 const std::string& account_id, | 499 const std::string& account_id, |
493 const RetrievePolicyCallback& callback) { | 500 const RetrievePolicyCallback& callback) { |
494 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | 501 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, |
495 method_name); | 502 method_name); |
496 dbus::MessageWriter writer(&method_call); | 503 dbus::MessageWriter writer(&method_call); |
497 writer.AppendString(account_id); | 504 writer.AppendString(account_id); |
498 session_manager_proxy_->CallMethod( | 505 session_manager_proxy_->CallMethodWithErrorCallback( |
499 &method_call, | 506 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
500 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 507 base::Bind(&SessionManagerClientImpl::OnRetrievePolicySuccess, |
501 base::Bind( | 508 weak_ptr_factory_.GetWeakPtr(), method_name, callback), |
502 &SessionManagerClientImpl::OnRetrievePolicy, | 509 base::Bind(&SessionManagerClientImpl::OnRetrievePolicyError, |
503 weak_ptr_factory_.GetWeakPtr(), | 510 weak_ptr_factory_.GetWeakPtr(), callback)); |
504 method_name, | |
505 callback)); | |
506 } | 511 } |
507 | 512 |
508 void CallStorePolicyByUsername(const std::string& method_name, | 513 void CallStorePolicyByUsername(const std::string& method_name, |
509 const std::string& account_id, | 514 const std::string& account_id, |
510 const std::string& policy_blob, | 515 const std::string& policy_blob, |
511 const StorePolicyCallback& callback) { | 516 const StorePolicyCallback& callback) { |
512 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | 517 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, |
513 method_name); | 518 method_name); |
514 dbus::MessageWriter writer(&method_call); | 519 dbus::MessageWriter writer(&method_call); |
515 writer.AppendString(account_id); | 520 writer.AppendString(account_id); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
607 size_t length = 0; | 612 size_t length = 0; |
608 if (!reader.PopArrayOfBytes(&values, &length)) { | 613 if (!reader.PopArrayOfBytes(&values, &length)) { |
609 LOG(ERROR) << "Invalid response: " << response->ToString(); | 614 LOG(ERROR) << "Invalid response: " << response->ToString(); |
610 return; | 615 return; |
611 } | 616 } |
612 // static_cast does not work due to signedness. | 617 // static_cast does not work due to signedness. |
613 extracted->assign(reinterpret_cast<const char*>(values), length); | 618 extracted->assign(reinterpret_cast<const char*>(values), length); |
614 } | 619 } |
615 | 620 |
616 // Called when kSessionManagerRetrievePolicy or | 621 // Called when kSessionManagerRetrievePolicy or |
617 // kSessionManagerRetrievePolicyForUser method is complete. | 622 // kSessionManagerRetrievePolicyForUser method is complete. |
emaxx
2017/04/18 15:03:17
nit: Maybe change "complete" to "successfully comp
igorcov
2017/04/20 14:52:29
Done.
| |
618 void OnRetrievePolicy(const std::string& method_name, | 623 void OnRetrievePolicySuccess(const std::string& method_name, |
619 const RetrievePolicyCallback& callback, | 624 const RetrievePolicyCallback& callback, |
620 dbus::Response* response) { | 625 dbus::Response* response) { |
621 std::string serialized_proto; | 626 std::string serialized_proto; |
622 ExtractString(method_name, response, &serialized_proto); | 627 ExtractString(method_name, response, &serialized_proto); |
623 callback.Run(serialized_proto); | 628 callback.Run(serialized_proto, SUCCESS); |
629 | |
630 UMA_HISTOGRAM_ENUMERATION("Enterprise.RetrievePolicyResponse", SUCCESS, | |
631 RETRIEVE_POLICY_RESPONSE_TYPE_COUNT); | |
632 } | |
633 | |
634 void OnRetrievePolicyError(const RetrievePolicyCallback& callback, | |
emaxx
2017/04/18 15:03:17
nit: Please add a comment for this method.
igorcov
2017/04/20 14:52:29
Done.
| |
635 dbus::ErrorResponse* response) { | |
636 RetrievePolicyResponseType response_type = OTHER_ERROR; | |
637 if (response->GetErrorName() == | |
638 login_manager::dbus_error::kSessionDoesNotExist) { | |
639 response_type = SESSION_DOES_NOT_EXIST; | |
640 } | |
641 callback.Run(std::string(), response_type); | |
642 | |
643 UMA_HISTOGRAM_ENUMERATION("Enterprise.RetrievePolicyResponse", | |
644 response_type, | |
645 RETRIEVE_POLICY_RESPONSE_TYPE_COUNT); | |
624 } | 646 } |
625 | 647 |
626 // Called when kSessionManagerStorePolicy or kSessionManagerStorePolicyForUser | 648 // Called when kSessionManagerStorePolicy or kSessionManagerStorePolicyForUser |
627 // method is complete. | 649 // method is complete. |
628 void OnStorePolicy(const std::string& method_name, | 650 void OnStorePolicy(const std::string& method_name, |
629 const StorePolicyCallback& callback, | 651 const StorePolicyCallback& callback, |
630 dbus::Response* response) { | 652 dbus::Response* response) { |
631 bool success = false; | 653 bool success = false; |
632 if (!response) { | 654 if (!response) { |
633 LOG(ERROR) << "Failed to call " << method_name; | 655 LOG(ERROR) << "Failed to call " << method_name; |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
854 void NotifyLockScreenDismissed() override { | 876 void NotifyLockScreenDismissed() override { |
855 screen_is_locked_ = false; | 877 screen_is_locked_ = false; |
856 for (auto& observer : observers_) | 878 for (auto& observer : observers_) |
857 observer.ScreenIsUnlocked(); | 879 observer.ScreenIsUnlocked(); |
858 } | 880 } |
859 void RetrieveActiveSessions(const ActiveSessionsCallback& callback) override { | 881 void RetrieveActiveSessions(const ActiveSessionsCallback& callback) override { |
860 } | 882 } |
861 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override { | 883 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override { |
862 base::FilePath owner_key_path; | 884 base::FilePath owner_key_path; |
863 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) { | 885 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) { |
864 callback.Run(""); | 886 callback.Run("", SUCCESS); |
865 return; | 887 return; |
866 } | 888 } |
867 base::FilePath device_policy_path = | 889 base::FilePath device_policy_path = |
868 owner_key_path.DirName().AppendASCII(kStubDevicePolicyFile); | 890 owner_key_path.DirName().AppendASCII(kStubDevicePolicyFile); |
869 base::PostTaskWithTraitsAndReplyWithResult( | 891 base::PostTaskWithTraitsAndReplyWithResult( |
870 FROM_HERE, base::TaskTraits() | 892 FROM_HERE, |
871 .WithShutdownBehavior( | 893 base::TaskTraits() |
872 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | 894 .WithShutdownBehavior( |
873 .MayBlock(), | 895 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
874 base::Bind(&GetFileContent, device_policy_path), callback); | 896 .MayBlock(), |
897 base::Bind(&GetFileContent, device_policy_path), | |
898 base::Bind(&NotifyOnRetrievePolicySuccess, callback)); | |
875 } | 899 } |
876 std::string BlockingRetrieveDevicePolicy() override { | 900 std::string BlockingRetrieveDevicePolicy() override { |
877 base::FilePath owner_key_path; | 901 base::FilePath owner_key_path; |
878 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) { | 902 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) { |
879 return ""; | 903 return ""; |
880 } | 904 } |
881 base::FilePath device_policy_path = | 905 base::FilePath device_policy_path = |
882 owner_key_path.DirName().AppendASCII(kStubDevicePolicyFile); | 906 owner_key_path.DirName().AppendASCII(kStubDevicePolicyFile); |
883 return GetFileContent(device_policy_path); | 907 return GetFileContent(device_policy_path); |
884 } | 908 } |
885 void RetrievePolicyForUser(const cryptohome::Identification& cryptohome_id, | 909 void RetrievePolicyForUser(const cryptohome::Identification& cryptohome_id, |
886 const RetrievePolicyCallback& callback) override { | 910 const RetrievePolicyCallback& callback) override { |
887 base::PostTaskWithTraitsAndReplyWithResult( | 911 base::PostTaskWithTraitsAndReplyWithResult( |
888 FROM_HERE, | 912 FROM_HERE, |
889 base::TaskTraits() | 913 base::TaskTraits() |
890 .WithShutdownBehavior( | 914 .WithShutdownBehavior( |
891 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | 915 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
892 .MayBlock(), | 916 .MayBlock(), |
893 base::Bind(&GetFileContent, | 917 base::Bind(&GetFileContent, |
894 GetUserFilePath(cryptohome_id, kStubPolicyFile)), | 918 GetUserFilePath(cryptohome_id, kStubPolicyFile)), |
895 callback); | 919 base::Bind(&NotifyOnRetrievePolicySuccess, callback)); |
896 } | 920 } |
897 std::string BlockingRetrievePolicyForUser( | 921 std::string BlockingRetrievePolicyForUser( |
898 const cryptohome::Identification& cryptohome_id) override { | 922 const cryptohome::Identification& cryptohome_id) override { |
899 return GetFileContent(GetUserFilePath(cryptohome_id, kStubPolicyFile)); | 923 return GetFileContent(GetUserFilePath(cryptohome_id, kStubPolicyFile)); |
900 } | 924 } |
901 void RetrieveDeviceLocalAccountPolicy( | 925 void RetrieveDeviceLocalAccountPolicy( |
902 const std::string& account_id, | 926 const std::string& account_id, |
903 const RetrievePolicyCallback& callback) override { | 927 const RetrievePolicyCallback& callback) override { |
904 RetrievePolicyForUser(cryptohome::Identification::FromString(account_id), | 928 RetrievePolicyForUser(cryptohome::Identification::FromString(account_id), |
905 callback); | 929 callback); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1053 | 1077 |
1054 SessionManagerClient* SessionManagerClient::Create( | 1078 SessionManagerClient* SessionManagerClient::Create( |
1055 DBusClientImplementationType type) { | 1079 DBusClientImplementationType type) { |
1056 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 1080 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
1057 return new SessionManagerClientImpl(); | 1081 return new SessionManagerClientImpl(); |
1058 DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); | 1082 DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); |
1059 return new SessionManagerClientStubImpl(); | 1083 return new SessionManagerClientStubImpl(); |
1060 } | 1084 } |
1061 | 1085 |
1062 } // namespace chromeos | 1086 } // namespace chromeos |
OLD | NEW |