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> ReadCreateStateKeys(const base::FilePath& path) { | |
Daniel Erat
2017/03/23 23:35:23
please update this function's name to make it clea
Thiemo Nagel
2017/04/11 15:29:32
Done.
(I really dislike the practice of keeping m
| |
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 std::vector<std::string> g_state_keys; | |
Daniel Erat
2017/03/23 23:35:23
i don't think that non-POD static variables are al
Thiemo Nagel
2017/04/11 15:29:32
Sorry for that momentary lapse of reason.
| |
101 | |
102 // This wrapper is required because StateKeysCallback takes a reference. This is | |
103 // thread-safe as long as all callers are on the same thread. | |
Daniel Erat
2017/03/23 23:35:23
can you just fix the callback to not take a refere
Thiemo Nagel
2017/04/11 15:29:32
It's not even necessary to store them anywhere. A
| |
104 void RunStateKeysCallback(SessionManagerClient::StateKeysCallback callback, | |
105 std::vector<std::string> state_keys) { | |
106 g_state_keys = state_keys; | |
107 callback.Run(g_state_keys); | |
108 } | |
109 | |
77 } // namespace | 110 } // namespace |
78 | 111 |
79 // The SessionManagerClient implementation used in production. | 112 // The SessionManagerClient implementation used in production. |
80 class SessionManagerClientImpl : public SessionManagerClient { | 113 class SessionManagerClientImpl : public SessionManagerClient { |
81 public: | 114 public: |
82 SessionManagerClientImpl() | 115 SessionManagerClientImpl() |
83 : session_manager_proxy_(NULL), | 116 : session_manager_proxy_(NULL), |
84 screen_is_locked_(false), | 117 screen_is_locked_(false), |
85 weak_ptr_factory_(this) {} | 118 weak_ptr_factory_(this) {} |
86 | 119 |
(...skipping 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
984 StorePolicyForUser(cryptohome::Identification::FromString(account_id), | 1017 StorePolicyForUser(cryptohome::Identification::FromString(account_id), |
985 policy_blob, callback); | 1018 policy_blob, callback); |
986 } | 1019 } |
987 | 1020 |
988 bool SupportsRestartToApplyUserFlags() const override { return false; } | 1021 bool SupportsRestartToApplyUserFlags() const override { return false; } |
989 | 1022 |
990 void SetFlagsForUser(const cryptohome::Identification& cryptohome_id, | 1023 void SetFlagsForUser(const cryptohome::Identification& cryptohome_id, |
991 const std::vector<std::string>& flags) override {} | 1024 const std::vector<std::string>& flags) override {} |
992 | 1025 |
993 void GetServerBackedStateKeys(const StateKeysCallback& callback) override { | 1026 void GetServerBackedStateKeys(const StateKeysCallback& callback) override { |
994 std::vector<std::string> state_keys; | 1027 base::FilePath owner_key_path; |
995 for (int i = 0; i < 5; ++i) | 1028 CHECK(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)); |
996 state_keys.push_back(crypto::SHA256HashString(base::IntToString(i))); | 1029 const base::FilePath state_keys_path = |
997 | 1030 owner_key_path.DirName().AppendASCII(kStubStateKeysFile); |
998 if (!callback.is_null()) | 1031 base::PostTaskWithTraitsAndReplyWithResult( |
999 callback.Run(state_keys); | 1032 FROM_HERE, |
1033 base::TaskTraits() | |
1034 .WithShutdownBehavior( | |
1035 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | |
1036 .MayBlock(), | |
1037 base::Bind(&ReadCreateStateKeys, state_keys_path), | |
1038 base::Bind(&RunStateKeysCallback, callback)); | |
1000 } | 1039 } |
1001 | 1040 |
1002 void CheckArcAvailability(const ArcCallback& callback) override { | 1041 void CheckArcAvailability(const ArcCallback& callback) override { |
1003 callback.Run(false); | 1042 callback.Run(false); |
1004 } | 1043 } |
1005 | 1044 |
1006 void StartArcInstance(const cryptohome::Identification& cryptohome_id, | 1045 void StartArcInstance(const cryptohome::Identification& cryptohome_id, |
1007 bool disable_boot_completed_broadcast, | 1046 bool disable_boot_completed_broadcast, |
1008 const StartArcInstanceCallback& callback) override { | 1047 const StartArcInstanceCallback& callback) override { |
1009 callback.Run(StartArcInstanceResult::UNKNOWN_ERROR); | 1048 callback.Run(StartArcInstanceResult::UNKNOWN_ERROR); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1053 | 1092 |
1054 SessionManagerClient* SessionManagerClient::Create( | 1093 SessionManagerClient* SessionManagerClient::Create( |
1055 DBusClientImplementationType type) { | 1094 DBusClientImplementationType type) { |
1056 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 1095 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
1057 return new SessionManagerClientImpl(); | 1096 return new SessionManagerClientImpl(); |
1058 DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); | 1097 DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); |
1059 return new SessionManagerClientStubImpl(); | 1098 return new SessionManagerClientStubImpl(); |
1060 } | 1099 } |
1061 | 1100 |
1062 } // namespace chromeos | 1101 } // namespace chromeos |
OLD | NEW |