OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/signin/core/browser/account_tracker_service.h" | 5 #include "components/signin/core/browser/account_tracker_service.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 ++it) { | 134 ++it) { |
135 const AccountState& state = it->second; | 135 const AccountState& state = it->second; |
136 if (gaia::AreEmailsSame(state.info.email, email)) | 136 if (gaia::AreEmailsSame(state.info.email, email)) |
137 return state.info; | 137 return state.info; |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 return AccountInfo(); | 141 return AccountInfo(); |
142 } | 142 } |
143 | 143 |
144 bool AccountTrackerService::IsTrackingAccount( | |
145 const std::string& account_id) const { | |
146 return ContainsKey(accounts_, account_id); | |
147 } | |
148 | |
149 bool AccountTrackerService::HasIdNameMappingForAccount( | |
150 const std::string& account_id) const { | |
151 AccountInfo account = GetAccountInfo(account_id); | |
152 return !account.email.empty() && !account.gaia.empty(); | |
gogerald1
2015/10/13 16:34:53
This is always true, if IsMigratable() is true and
nyquist
2015/10/15 19:33:45
I thought GetAccountInfo(...) returns an empty str
| |
153 } | |
154 | |
155 bool AccountTrackerService::HasIdNameMappingForAllAccounts() const { | |
156 for (const auto& account : accounts_) { | |
157 const AccountInfo& account_info = account.second.info; | |
158 if (account_info.email.empty() || account_info.gaia.empty()) | |
gogerald1
2015/10/13 16:34:53
Same as above comments
| |
159 return false; | |
160 } | |
161 return true; | |
162 } | |
163 | |
144 AccountTrackerService::AccountIdMigrationState | 164 AccountTrackerService::AccountIdMigrationState |
145 AccountTrackerService::GetMigrationState() const { | 165 AccountTrackerService::GetMigrationState() const { |
146 return GetMigrationState(signin_client_->GetPrefs()); | 166 return GetMigrationState(signin_client_->GetPrefs()); |
147 } | 167 } |
148 | 168 |
149 void AccountTrackerService::SetMigrationState(AccountIdMigrationState state) { | 169 void AccountTrackerService::SetMigrationState(AccountIdMigrationState state) { |
150 signin_client_->GetPrefs()->SetInteger(prefs::kAccountIdMigrationState, | 170 signin_client_->GetPrefs()->SetInteger(prefs::kAccountIdMigrationState, |
151 state); | 171 state); |
152 } | 172 } |
153 | 173 |
(...skipping 21 matching lines...) Expand all Loading... | |
175 } | 195 } |
176 | 196 |
177 void AccountTrackerService::NotifyAccountRemoved(const AccountState& state) { | 197 void AccountTrackerService::NotifyAccountRemoved(const AccountState& state) { |
178 DCHECK(!state.info.gaia.empty()); | 198 DCHECK(!state.info.gaia.empty()); |
179 FOR_EACH_OBSERVER( | 199 FOR_EACH_OBSERVER( |
180 Observer, observer_list_, OnAccountRemoved(state.info)); | 200 Observer, observer_list_, OnAccountRemoved(state.info)); |
181 } | 201 } |
182 | 202 |
183 void AccountTrackerService::StartTrackingAccount( | 203 void AccountTrackerService::StartTrackingAccount( |
184 const std::string& account_id) { | 204 const std::string& account_id) { |
185 if (!ContainsKey(accounts_, account_id)) { | 205 if (!IsTrackingAccount(account_id)) { |
186 DVLOG(1) << "StartTracking " << account_id; | 206 DVLOG(1) << "StartTracking " << account_id; |
187 AccountState state; | 207 AccountState state; |
188 state.info.account_id = account_id; | 208 state.info.account_id = account_id; |
189 state.info.is_child_account = false; | 209 state.info.is_child_account = false; |
190 accounts_.insert(make_pair(account_id, state)); | 210 accounts_.insert(make_pair(account_id, state)); |
191 } | 211 } |
192 } | 212 } |
193 | 213 |
194 void AccountTrackerService::StopTrackingAccount(const std::string& account_id) { | 214 void AccountTrackerService::StopTrackingAccount(const std::string& account_id) { |
195 DVLOG(1) << "StopTracking " << account_id; | 215 DVLOG(1) << "StopTracking " << account_id; |
196 if (ContainsKey(accounts_, account_id)) { | 216 if (IsTrackingAccount(account_id)) { |
197 AccountState& state = accounts_[account_id]; | 217 AccountState& state = accounts_[account_id]; |
198 RemoveFromPrefs(state); | 218 RemoveFromPrefs(state); |
199 if (!state.info.gaia.empty()) | 219 if (!state.info.gaia.empty()) |
200 NotifyAccountRemoved(state); | 220 NotifyAccountRemoved(state); |
201 | 221 |
202 accounts_.erase(account_id); | 222 accounts_.erase(account_id); |
203 } | 223 } |
204 } | 224 } |
205 | 225 |
206 void AccountTrackerService::SetAccountStateFromUserInfo( | 226 void AccountTrackerService::SetAccountStateFromUserInfo( |
207 const std::string& account_id, | 227 const std::string& account_id, |
208 const base::DictionaryValue* user_info) { | 228 const base::DictionaryValue* user_info) { |
209 DCHECK(ContainsKey(accounts_, account_id)); | 229 DCHECK(IsTrackingAccount(account_id)); |
210 AccountState& state = accounts_[account_id]; | 230 AccountState& state = accounts_[account_id]; |
211 | 231 |
212 std::string gaia_id; | 232 std::string gaia_id; |
213 std::string email; | 233 std::string email; |
214 if (user_info->GetString("id", &gaia_id) && | 234 if (user_info->GetString("id", &gaia_id) && |
215 user_info->GetString("email", &email)) { | 235 user_info->GetString("email", &email)) { |
216 state.info.gaia = gaia_id; | 236 state.info.gaia = gaia_id; |
217 state.info.email = email; | 237 state.info.email = email; |
218 | 238 |
219 std::string hosted_domain; | 239 std::string hosted_domain; |
(...skipping 14 matching lines...) Expand all Loading... | |
234 state.info.picture_url = kNoPictureURLFound; | 254 state.info.picture_url = kNoPictureURLFound; |
235 } | 255 } |
236 } | 256 } |
237 if (state.info.IsValid()) | 257 if (state.info.IsValid()) |
238 NotifyAccountUpdated(state); | 258 NotifyAccountUpdated(state); |
239 SaveToPrefs(state); | 259 SaveToPrefs(state); |
240 } | 260 } |
241 | 261 |
242 void AccountTrackerService::SetIsChildAccount(const std::string& account_id, | 262 void AccountTrackerService::SetIsChildAccount(const std::string& account_id, |
243 const bool& is_child_account) { | 263 const bool& is_child_account) { |
244 DCHECK(ContainsKey(accounts_, account_id)); | 264 DCHECK(IsTrackingAccount(account_id)); |
245 AccountState& state = accounts_[account_id]; | 265 AccountState& state = accounts_[account_id]; |
246 if (state.info.is_child_account == is_child_account) | 266 if (state.info.is_child_account == is_child_account) |
247 return; | 267 return; |
248 state.info.is_child_account = is_child_account; | 268 state.info.is_child_account = is_child_account; |
249 if (state.info.IsValid()) | 269 if (state.info.IsValid()) |
250 NotifyAccountUpdated(state); | 270 NotifyAccountUpdated(state); |
251 SaveToPrefs(state); | 271 SaveToPrefs(state); |
252 } | 272 } |
253 | 273 |
254 bool AccountTrackerService::IsMigratable() const { | 274 bool AccountTrackerService::IsMigratable() const { |
255 #if !defined(OS_CHROMEOS) | 275 #if !defined(OS_CHROMEOS) |
256 for (std::map<std::string, AccountState>::const_iterator it = | 276 return HasIdNameMappingForAllAccounts(); |
257 accounts_.begin(); | |
258 it != accounts_.end(); ++it) { | |
259 const AccountState& state = it->second; | |
260 if ((it->first).empty() || state.info.gaia.empty()) | |
261 return false; | |
262 } | |
263 return true; | |
264 #else | 277 #else |
265 return false; | 278 return false; |
266 #endif | 279 #endif |
267 } | 280 } |
268 | 281 |
269 void AccountTrackerService::MigrateToGaiaId() { | 282 void AccountTrackerService::MigrateToGaiaId() { |
270 std::set<std::string> to_remove; | 283 std::set<std::string> to_remove; |
271 std::map<std::string, AccountState> migrated_accounts; | 284 std::map<std::string, AccountState> migrated_accounts; |
272 for (std::map<std::string, AccountState>::const_iterator it = | 285 for (std::map<std::string, AccountState>::const_iterator it = |
273 accounts_.begin(); | 286 accounts_.begin(); |
274 it != accounts_.end(); ++it) { | 287 it != accounts_.end(); ++it) { |
275 const AccountState& state = it->second; | 288 const AccountState& state = it->second; |
276 std::string account_id = it->first; | 289 std::string account_id = it->first; |
277 if (account_id != state.info.gaia) { | 290 if (account_id != state.info.gaia) { |
278 std::string new_account_id = state.info.gaia; | 291 std::string new_account_id = state.info.gaia; |
279 if (!ContainsKey(accounts_, new_account_id)) { | 292 if (!IsTrackingAccount(new_account_id)) { |
280 AccountState new_state = state; | 293 AccountState new_state = state; |
281 new_state.info.account_id = new_account_id; | 294 new_state.info.account_id = new_account_id; |
282 migrated_accounts.insert(make_pair(new_account_id, new_state)); | 295 migrated_accounts.insert(make_pair(new_account_id, new_state)); |
283 SaveToPrefs(new_state); | 296 SaveToPrefs(new_state); |
284 } | 297 } |
285 to_remove.insert(account_id); | 298 to_remove.insert(account_id); |
286 } | 299 } |
287 } | 300 } |
288 | 301 |
289 // Remove any obsolete account. | 302 // Remove any obsolete account. |
290 for (auto account_id : to_remove) { | 303 for (auto account_id : to_remove) { |
291 if (ContainsKey(accounts_, account_id)) { | 304 if (IsTrackingAccount(account_id)) { |
292 AccountState& state = accounts_[account_id]; | 305 AccountState& state = accounts_[account_id]; |
293 RemoveFromPrefs(state); | 306 RemoveFromPrefs(state); |
294 accounts_.erase(account_id); | 307 accounts_.erase(account_id); |
295 } | 308 } |
296 } | 309 } |
297 | 310 |
298 for (std::map<std::string, AccountState>::const_iterator it = | 311 for (std::map<std::string, AccountState>::const_iterator it = |
299 migrated_accounts.begin(); | 312 migrated_accounts.begin(); |
300 it != migrated_accounts.end(); ++it) { | 313 it != migrated_accounts.end(); ++it) { |
301 accounts_.insert(*it); | 314 accounts_.insert(*it); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 return gaia; | 472 return gaia; |
460 default: | 473 default: |
461 NOTREACHED(); | 474 NOTREACHED(); |
462 return email; | 475 return email; |
463 } | 476 } |
464 } | 477 } |
465 | 478 |
466 std::string AccountTrackerService::SeedAccountInfo(const std::string& gaia, | 479 std::string AccountTrackerService::SeedAccountInfo(const std::string& gaia, |
467 const std::string& email) { | 480 const std::string& email) { |
468 const std::string account_id = PickAccountIdForAccount(gaia, email); | 481 const std::string account_id = PickAccountIdForAccount(gaia, email); |
469 const bool already_exists = ContainsKey(accounts_, account_id); | 482 const bool already_exists = IsTrackingAccount(account_id); |
470 StartTrackingAccount(account_id); | 483 StartTrackingAccount(account_id); |
471 AccountState& state = accounts_[account_id]; | 484 AccountState& state = accounts_[account_id]; |
472 DCHECK(!already_exists || state.info.gaia.empty() || state.info.gaia == gaia); | 485 DCHECK(!already_exists || state.info.gaia.empty() || state.info.gaia == gaia); |
473 state.info.gaia = gaia; | 486 state.info.gaia = gaia; |
474 state.info.email = email; | 487 state.info.email = email; |
475 SaveToPrefs(state); | 488 SaveToPrefs(state); |
476 | 489 |
477 DVLOG(1) << "AccountTrackerService::SeedAccountInfo" | 490 DVLOG(1) << "AccountTrackerService::SeedAccountInfo" |
478 << " account_id=" << account_id | 491 << " account_id=" << account_id |
479 << " gaia_id=" << gaia | 492 << " gaia_id=" << gaia |
480 << " email=" << email; | 493 << " email=" << email; |
481 | 494 |
482 return account_id; | 495 return account_id; |
483 } | 496 } |
484 | 497 |
485 void AccountTrackerService::SeedAccountInfo(AccountInfo info) { | 498 void AccountTrackerService::SeedAccountInfo(AccountInfo info) { |
486 info.account_id = PickAccountIdForAccount(info.gaia, info.email); | 499 info.account_id = PickAccountIdForAccount(info.gaia, info.email); |
487 if (info.hosted_domain.empty()) { | 500 if (info.hosted_domain.empty()) { |
488 info.hosted_domain = kNoHostedDomainFound; | 501 info.hosted_domain = kNoHostedDomainFound; |
489 } | 502 } |
490 | 503 |
491 if(info.IsValid()) { | 504 if(info.IsValid()) { |
492 if(!ContainsKey(accounts_, info.account_id)) { | 505 if (!IsTrackingAccount(info.account_id)) { |
493 SeedAccountInfo(info.gaia, info.email); | 506 SeedAccountInfo(info.gaia, info.email); |
494 } | 507 } |
495 | 508 |
496 AccountState& state = accounts_[info.account_id]; | 509 AccountState& state = accounts_[info.account_id]; |
497 state.info = info; | 510 state.info = info; |
498 NotifyAccountUpdated(state); | 511 NotifyAccountUpdated(state); |
499 SaveToPrefs(state); | 512 SaveToPrefs(state); |
500 } | 513 } |
501 } | 514 } |
502 | 515 |
503 void AccountTrackerService::RemoveAccount(const std::string& account_id) { | 516 void AccountTrackerService::RemoveAccount(const std::string& account_id) { |
504 StopTrackingAccount(account_id); | 517 StopTrackingAccount(account_id); |
505 } | 518 } |
OLD | NEW |