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> |
(...skipping 24 matching lines...) Expand all Loading... |
35 namespace chromeos { | 35 namespace chromeos { |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 // TODO(hidehiko): Share the constant between Chrome and ChromeOS. | 39 // TODO(hidehiko): Share the constant between Chrome and ChromeOS. |
40 constexpr char kArcLowDiskError[] = | 40 constexpr char kArcLowDiskError[] = |
41 "org.chromium.SessionManagerInterface.LowFreeDisk"; | 41 "org.chromium.SessionManagerInterface.LowFreeDisk"; |
42 | 42 |
43 constexpr char kStubPolicyFile[] = "stub_policy"; | 43 constexpr char kStubPolicyFile[] = "stub_policy"; |
44 constexpr char kStubDevicePolicyFile[] = "stub_device_policy"; | 44 constexpr char kStubDevicePolicyFile[] = "stub_device_policy"; |
| 45 constexpr char kStubStateKeysFile[] = "stub_state_keys"; |
45 | 46 |
46 // Returns a location for |file| that is specific to the given |cryptohome_id|. | 47 // Returns a location for |file| that is specific to the given |cryptohome_id|. |
47 // These paths will be relative to DIR_USER_POLICY_KEYS, and can be used only | 48 // These paths will be relative to DIR_USER_POLICY_KEYS, and can be used only |
48 // to store stub files. | 49 // to store stub files. |
49 base::FilePath GetUserFilePath(const cryptohome::Identification& cryptohome_id, | 50 base::FilePath GetUserFilePath(const cryptohome::Identification& cryptohome_id, |
50 const char* file) { | 51 const char* file) { |
51 base::FilePath keys_path; | 52 base::FilePath keys_path; |
52 if (!PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &keys_path)) | 53 if (!PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &keys_path)) |
53 return base::FilePath(); | 54 return base::FilePath(); |
54 const std::string sanitized = | 55 const std::string sanitized = |
(...skipping 12 matching lines...) Expand all 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 asynchronously read (or if missing create) state key stubs. |
| 79 std::vector<std::string> ReadCreateStateKeysStub(const base::FilePath& path) { |
| 80 std::string contents; |
| 81 if (base::PathExists(path)) { |
| 82 contents = GetFileContent(path); |
| 83 } else { |
| 84 // Create stub state keys on the fly. |
| 85 for (int i = 0; i < 5; ++i) { |
| 86 contents += crypto::SHA256HashString( |
| 87 base::IntToString(i) + |
| 88 base::Int64ToString(base::Time::Now().ToJavaTime())); |
| 89 } |
| 90 StoreFile(path, contents); |
| 91 } |
| 92 |
| 93 std::vector<std::string> state_keys; |
| 94 for (size_t i = 0; i < contents.size() / 32; ++i) { |
| 95 state_keys.push_back(contents.substr(i * 32, 32)); |
| 96 } |
| 97 return state_keys; |
| 98 } |
| 99 |
| 100 // Turn pass-by-value into pass-by-reference as expected by StateKeysCallback. |
| 101 void RunStateKeysCallbackStub(SessionManagerClient::StateKeysCallback callback, |
| 102 std::vector<std::string> state_keys) { |
| 103 callback.Run(state_keys); |
| 104 } |
| 105 |
77 } // namespace | 106 } // namespace |
78 | 107 |
79 // The SessionManagerClient implementation used in production. | 108 // The SessionManagerClient implementation used in production. |
80 class SessionManagerClientImpl : public SessionManagerClient { | 109 class SessionManagerClientImpl : public SessionManagerClient { |
81 public: | 110 public: |
82 SessionManagerClientImpl() | 111 SessionManagerClientImpl() |
83 : session_manager_proxy_(NULL), | 112 : session_manager_proxy_(NULL), |
84 screen_is_locked_(false), | 113 screen_is_locked_(false), |
85 weak_ptr_factory_(this) {} | 114 weak_ptr_factory_(this) {} |
86 | 115 |
(...skipping 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 StorePolicyForUser(cryptohome::Identification::FromString(account_id), | 1013 StorePolicyForUser(cryptohome::Identification::FromString(account_id), |
985 policy_blob, callback); | 1014 policy_blob, callback); |
986 } | 1015 } |
987 | 1016 |
988 bool SupportsRestartToApplyUserFlags() const override { return false; } | 1017 bool SupportsRestartToApplyUserFlags() const override { return false; } |
989 | 1018 |
990 void SetFlagsForUser(const cryptohome::Identification& cryptohome_id, | 1019 void SetFlagsForUser(const cryptohome::Identification& cryptohome_id, |
991 const std::vector<std::string>& flags) override {} | 1020 const std::vector<std::string>& flags) override {} |
992 | 1021 |
993 void GetServerBackedStateKeys(const StateKeysCallback& callback) override { | 1022 void GetServerBackedStateKeys(const StateKeysCallback& callback) override { |
994 std::vector<std::string> state_keys; | 1023 base::FilePath owner_key_path; |
995 for (int i = 0; i < 5; ++i) | 1024 CHECK(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)); |
996 state_keys.push_back(crypto::SHA256HashString(base::IntToString(i))); | 1025 const base::FilePath state_keys_path = |
997 | 1026 owner_key_path.DirName().AppendASCII(kStubStateKeysFile); |
998 if (!callback.is_null()) | 1027 base::PostTaskWithTraitsAndReplyWithResult( |
999 callback.Run(state_keys); | 1028 FROM_HERE, |
| 1029 base::TaskTraits() |
| 1030 .WithShutdownBehavior( |
| 1031 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 1032 .MayBlock(), |
| 1033 base::Bind(&ReadCreateStateKeysStub, state_keys_path), |
| 1034 base::Bind(&RunStateKeysCallbackStub, callback)); |
1000 } | 1035 } |
1001 | 1036 |
1002 void CheckArcAvailability(const ArcCallback& callback) override { | 1037 void CheckArcAvailability(const ArcCallback& callback) override { |
1003 callback.Run(false); | 1038 callback.Run(false); |
1004 } | 1039 } |
1005 | 1040 |
1006 void StartArcInstance(const cryptohome::Identification& cryptohome_id, | 1041 void StartArcInstance(const cryptohome::Identification& cryptohome_id, |
1007 bool disable_boot_completed_broadcast, | 1042 bool disable_boot_completed_broadcast, |
1008 const StartArcInstanceCallback& callback) override { | 1043 const StartArcInstanceCallback& callback) override { |
1009 callback.Run(StartArcInstanceResult::UNKNOWN_ERROR); | 1044 callback.Run(StartArcInstanceResult::UNKNOWN_ERROR); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1053 | 1088 |
1054 SessionManagerClient* SessionManagerClient::Create( | 1089 SessionManagerClient* SessionManagerClient::Create( |
1055 DBusClientImplementationType type) { | 1090 DBusClientImplementationType type) { |
1056 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 1091 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
1057 return new SessionManagerClientImpl(); | 1092 return new SessionManagerClientImpl(); |
1058 DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); | 1093 DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); |
1059 return new SessionManagerClientStubImpl(); | 1094 return new SessionManagerClientStubImpl(); |
1060 } | 1095 } |
1061 | 1096 |
1062 } // namespace chromeos | 1097 } // namespace chromeos |
OLD | NEW |