OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/user_manager/known_user.h" | 5 #include "components/user_manager/known_user.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // Key of GAPS cookie. | 39 // Key of GAPS cookie. |
40 const char kGAPSCookie[] = "gaps_cookie"; | 40 const char kGAPSCookie[] = "gaps_cookie"; |
41 | 41 |
42 // Key of the reason for re-auth. | 42 // Key of the reason for re-auth. |
43 const char kReauthReasonKey[] = "reauth_reason"; | 43 const char kReauthReasonKey[] = "reauth_reason"; |
44 | 44 |
45 // Key for the GaiaId migration status. | 45 // Key for the GaiaId migration status. |
46 const char kGaiaIdMigration[] = "gaia_id_migration"; | 46 const char kGaiaIdMigration[] = "gaia_id_migration"; |
47 | 47 |
48 PrefService* GetLocalState() { | 48 PrefService* GetLocalState() { |
49 UserManager* user_manager = UserManager::Get(); | 49 if (!UserManager::IsInitialized()) |
50 if (user_manager) | 50 return nullptr; |
51 return user_manager->GetLocalState(); | |
52 | 51 |
53 return nullptr; | 52 return UserManager::Get()->GetLocalState(); |
54 } | 53 } |
55 | 54 |
56 // Checks if values in |dict| correspond with |account_id| identity. | 55 // Checks if values in |dict| correspond with |account_id| identity. |
57 bool UserMatches(const AccountId& account_id, | 56 bool UserMatches(const AccountId& account_id, |
58 const base::DictionaryValue& dict) { | 57 const base::DictionaryValue& dict) { |
59 std::string value; | 58 std::string value; |
60 | 59 |
61 // TODO(alemate): update code once user id is really a struct. | 60 // TODO(alemate): update code once user id is really a struct. |
62 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value); | 61 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value); |
63 if (has_gaia_id && account_id.GetGaiaId() == value) | 62 if (has_gaia_id && account_id.GetGaiaId() == value) |
64 return true; | 63 return true; |
65 | 64 |
66 bool has_email = dict.GetString(kCanonicalEmail, &value); | 65 bool has_email = dict.GetString(kCanonicalEmail, &value); |
67 if (has_email && account_id.GetUserEmail() == value) | 66 if (has_email && account_id.GetUserEmail() == value) |
68 return true; | 67 return true; |
69 | 68 |
70 return false; | 69 return false; |
71 } | 70 } |
72 | 71 |
73 // Fills relevant |dict| values based on |account_id|. | 72 // Fills relevant |dict| values based on |account_id|. |
74 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { | 73 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { |
75 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); | 74 if (!account_id.GetUserEmail().empty()) |
| 75 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); |
| 76 |
| 77 if (!account_id.GetGaiaId().empty()) |
| 78 dict.SetString(kGAIAIdKey, account_id.GetGaiaId()); |
76 } | 79 } |
77 | 80 |
78 } // namespace | 81 } // namespace |
79 | 82 |
80 bool FindPrefs(const AccountId& account_id, | 83 bool FindPrefs(const AccountId& account_id, |
81 const base::DictionaryValue** out_value) { | 84 const base::DictionaryValue** out_value) { |
82 PrefService* local_state = GetLocalState(); | 85 PrefService* local_state = GetLocalState(); |
83 | 86 |
84 // Local State may not be initialized in tests. | 87 // Local State may not be initialized in tests. |
85 if (!local_state) | 88 if (!local_state) |
86 return false; | 89 return false; |
87 | 90 |
88 // UserManager is usually NULL in unit tests. | 91 // UserManager is usually NULL in unit tests. |
89 UserManager* user_manager = UserManager::Get(); | 92 if (UserManager::IsInitialized() && |
90 if (user_manager && | 93 UserManager::Get()->IsUserNonCryptohomeDataEphemeral(account_id)) |
91 user_manager->IsUserNonCryptohomeDataEphemeral(account_id)) | |
92 return false; | 94 return false; |
93 | 95 |
94 const base::ListValue* known_users = local_state->GetList(kKnownUsers); | 96 const base::ListValue* known_users = local_state->GetList(kKnownUsers); |
95 for (size_t i = 0; i < known_users->GetSize(); ++i) { | 97 for (size_t i = 0; i < known_users->GetSize(); ++i) { |
96 const base::DictionaryValue* element = nullptr; | 98 const base::DictionaryValue* element = nullptr; |
97 if (known_users->GetDictionary(i, &element)) { | 99 if (known_users->GetDictionary(i, &element)) { |
98 if (UserMatches(account_id, *element)) { | 100 if (UserMatches(account_id, *element)) { |
99 known_users->GetDictionary(i, out_value); | 101 known_users->GetDictionary(i, out_value); |
100 return true; | 102 return true; |
101 } | 103 } |
102 } | 104 } |
103 } | 105 } |
104 return false; | 106 return false; |
105 } | 107 } |
106 | 108 |
107 void UpdatePrefs(const AccountId& account_id, | 109 void UpdatePrefs(const AccountId& account_id, |
108 const base::DictionaryValue& values, | 110 const base::DictionaryValue& values, |
109 bool clear) { | 111 bool clear) { |
110 PrefService* local_state = GetLocalState(); | 112 PrefService* local_state = GetLocalState(); |
111 | 113 |
112 // Local State may not be initialized in tests. | 114 // Local State may not be initialized in tests. |
113 if (!local_state) | 115 if (!local_state) |
114 return; | 116 return; |
115 | 117 |
116 // UserManager is usually NULL in unit tests. | 118 // UserManager is usually NULL in unit tests. |
117 UserManager* user_manager = UserManager::Get(); | 119 if (UserManager::IsInitialized() && |
118 if (user_manager && | 120 UserManager::Get()->IsUserNonCryptohomeDataEphemeral(account_id)) |
119 user_manager->IsUserNonCryptohomeDataEphemeral(account_id)) | |
120 return; | 121 return; |
121 | 122 |
122 ListPrefUpdate update(local_state, kKnownUsers); | 123 ListPrefUpdate update(local_state, kKnownUsers); |
123 for (size_t i = 0; i < update->GetSize(); ++i) { | 124 for (size_t i = 0; i < update->GetSize(); ++i) { |
124 base::DictionaryValue* element = nullptr; | 125 base::DictionaryValue* element = nullptr; |
125 if (update->GetDictionary(i, &element)) { | 126 if (update->GetDictionary(i, &element)) { |
126 if (UserMatches(account_id, *element)) { | 127 if (UserMatches(account_id, *element)) { |
127 if (clear) | 128 if (clear) |
128 element->Clear(); | 129 element->Clear(); |
129 element->MergeDictionary(&values); | 130 element->MergeDictionary(&values); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 UpdatePrefs(account_id, dict, false); | 213 UpdatePrefs(account_id, dict, false); |
213 } | 214 } |
214 | 215 |
215 AccountId GetAccountId(const std::string& user_email, | 216 AccountId GetAccountId(const std::string& user_email, |
216 const std::string& gaia_id) { | 217 const std::string& gaia_id) { |
217 // In tests empty accounts are possible. | 218 // In tests empty accounts are possible. |
218 if (user_email.empty() && gaia_id.empty()) | 219 if (user_email.empty() && gaia_id.empty()) |
219 return EmptyAccountId(); | 220 return EmptyAccountId(); |
220 | 221 |
221 AccountId result(EmptyAccountId()); | 222 AccountId result(EmptyAccountId()); |
222 UserManager* user_manager = UserManager::Get(); | 223 // UserManager is usually NULL in unit tests. |
223 if (user_manager && | 224 if (UserManager::IsInitialized() && |
224 user_manager->GetPlatformKnownUserId(user_email, gaia_id, &result)) { | 225 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id, |
| 226 &result)) { |
225 return result; | 227 return result; |
226 } | 228 } |
227 | 229 |
228 // We can have several users with the same gaia_id but different e-mails. | 230 // We can have several users with the same gaia_id but different e-mails. |
229 // The opposite case is not possible. | 231 // The opposite case is not possible. |
230 std::string stored_gaia_id; | 232 std::string stored_gaia_id; |
231 const std::string sanitized_email = | 233 const std::string sanitized_email = |
232 user_email.empty() | 234 user_email.empty() |
233 ? std::string() | 235 ? std::string() |
234 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); | 236 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); |
(...skipping 14 matching lines...) Expand all Loading... |
249 if (!gaia_id.empty() && GetStringPref(AccountId::FromGaiaId(gaia_id), | 251 if (!gaia_id.empty() && GetStringPref(AccountId::FromGaiaId(gaia_id), |
250 kCanonicalEmail, &stored_email)) { | 252 kCanonicalEmail, &stored_email)) { |
251 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); | 253 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); |
252 } | 254 } |
253 | 255 |
254 return (gaia_id.empty() | 256 return (gaia_id.empty() |
255 ? AccountId::FromUserEmail(user_email) | 257 ? AccountId::FromUserEmail(user_email) |
256 : AccountId::FromUserEmailGaiaId(user_email, gaia_id)); | 258 : AccountId::FromUserEmailGaiaId(user_email, gaia_id)); |
257 } | 259 } |
258 | 260 |
| 261 std::vector<AccountId> GetKnownAccountIds() { |
| 262 std::vector<AccountId> result; |
| 263 PrefService* local_state = GetLocalState(); |
| 264 |
| 265 // Local State may not be initialized in tests. |
| 266 if (!local_state) |
| 267 return result; |
| 268 |
| 269 const base::ListValue* known_users = local_state->GetList(kKnownUsers); |
| 270 for (size_t i = 0; i < known_users->GetSize(); ++i) { |
| 271 const base::DictionaryValue* element = nullptr; |
| 272 if (known_users->GetDictionary(i, &element)) { |
| 273 std::string email; |
| 274 std::string gaia_id; |
| 275 const bool has_email = element->GetString(kCanonicalEmail, &email); |
| 276 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); |
| 277 if (has_email || has_gaia_id) |
| 278 result.push_back(AccountId::FromUserEmailGaiaId(email, gaia_id)); |
| 279 } |
| 280 } |
| 281 return result; |
| 282 } |
| 283 |
259 bool GetGaiaIdMigrationStatus(const AccountId& account_id, | 284 bool GetGaiaIdMigrationStatus(const AccountId& account_id, |
260 const std::string& subsystem) { | 285 const std::string& subsystem) { |
261 bool migrated = false; | 286 bool migrated = false; |
262 | 287 |
263 if (GetBooleanPref(account_id, | 288 if (GetBooleanPref(account_id, |
264 std::string(kGaiaIdMigration) + "." + subsystem, | 289 std::string(kGaiaIdMigration) + "." + subsystem, |
265 &migrated)) { | 290 &migrated)) { |
266 return migrated; | 291 return migrated; |
267 } | 292 } |
268 | 293 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 } | 374 } |
350 } | 375 } |
351 } | 376 } |
352 | 377 |
353 void RegisterPrefs(PrefRegistrySimple* registry) { | 378 void RegisterPrefs(PrefRegistrySimple* registry) { |
354 registry->RegisterListPref(kKnownUsers); | 379 registry->RegisterListPref(kKnownUsers); |
355 } | 380 } |
356 | 381 |
357 } // namespace known_user | 382 } // namespace known_user |
358 } // namespace user_manager | 383 } // namespace user_manager |
OLD | NEW |