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/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kAccountKeyPath[] = "account_id"; | 27 const char kAccountKeyPath[] = "account_id"; |
28 const char kAccountEmailPath[] = "email"; | 28 const char kAccountEmailPath[] = "email"; |
29 const char kAccountGaiaPath[] = "gaia"; | 29 const char kAccountGaiaPath[] = "gaia"; |
30 const char kAccountHostedDomainPath[] = "hd"; | 30 const char kAccountHostedDomainPath[] = "hd"; |
31 const char kAccountFullNamePath[] = "full_name"; | 31 const char kAccountFullNamePath[] = "full_name"; |
32 const char kAccountGivenNamePath[] = "given_name"; | 32 const char kAccountGivenNamePath[] = "given_name"; |
33 const char kAccountLocalePath[] = "locale"; | 33 const char kAccountLocalePath[] = "locale"; |
| 34 const char kAccountPictureURLPath[] = "picture_url"; |
34 | 35 |
35 const base::TimeDelta kRefreshFromTokenServiceDelay = | 36 const base::TimeDelta kRefreshFromTokenServiceDelay = |
36 base::TimeDelta::FromHours(24); | 37 base::TimeDelta::FromHours(24); |
37 | 38 |
38 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 39 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
39 // IsRefreshTokenDeviceIdExperimentEnabled is called from | 40 // IsRefreshTokenDeviceIdExperimentEnabled is called from |
40 // SendRefreshTokenAnnotationRequest only on desktop platforms. | 41 // SendRefreshTokenAnnotationRequest only on desktop platforms. |
41 bool IsRefreshTokenDeviceIdExperimentEnabled() { | 42 bool IsRefreshTokenDeviceIdExperimentEnabled() { |
42 const std::string group_name = | 43 const std::string group_name = |
43 base::FieldTrialList::FindFullName("RefreshTokenDeviceId"); | 44 base::FieldTrialList::FindFullName("RefreshTokenDeviceId"); |
44 return group_name == "Enabled"; | 45 return group_name == "Enabled"; |
45 } | 46 } |
46 #endif | 47 #endif |
47 } | 48 } |
48 | 49 |
49 // This must be a string which can never be a valid domain. | 50 // This must be a string which can never be a valid domain. |
50 const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; | 51 const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; |
51 | 52 |
| 53 // This must be a string which can never be a valid picture URL. |
| 54 const char AccountTrackerService::kNoPictureURLFound[] = "NO_PICTURE_URL"; |
| 55 |
52 class AccountInfoFetcher : public OAuth2TokenService::Consumer, | 56 class AccountInfoFetcher : public OAuth2TokenService::Consumer, |
53 public gaia::GaiaOAuthClient::Delegate { | 57 public gaia::GaiaOAuthClient::Delegate { |
54 public: | 58 public: |
55 AccountInfoFetcher(OAuth2TokenService* token_service, | 59 AccountInfoFetcher(OAuth2TokenService* token_service, |
56 net::URLRequestContextGetter* request_context_getter, | 60 net::URLRequestContextGetter* request_context_getter, |
57 AccountTrackerService* service, | 61 AccountTrackerService* service, |
58 const std::string& account_id); | 62 const std::string& account_id); |
59 ~AccountInfoFetcher() override; | 63 ~AccountInfoFetcher() override; |
60 | 64 |
61 const std::string& account_id() { return account_id_; } | 65 const std::string& account_id() { return account_id_; } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 "OnNetworkError", | 169 "OnNetworkError", |
166 "response_code", | 170 "response_code", |
167 response_code); | 171 response_code); |
168 LOG(ERROR) << "OnNetworkError " << response_code; | 172 LOG(ERROR) << "OnNetworkError " << response_code; |
169 service_->OnUserInfoFetchFailure(this); | 173 service_->OnUserInfoFetchFailure(this); |
170 } | 174 } |
171 | 175 |
172 AccountTrackerService::AccountInfo::AccountInfo() {} | 176 AccountTrackerService::AccountInfo::AccountInfo() {} |
173 AccountTrackerService::AccountInfo::~AccountInfo() {} | 177 AccountTrackerService::AccountInfo::~AccountInfo() {} |
174 | 178 |
175 bool AccountTrackerService::AccountInfo::IsValid() { | 179 bool AccountTrackerService::AccountInfo::IsValid() const { |
176 return !account_id.empty() && !email.empty() && !gaia.empty() && | 180 return !account_id.empty() && !email.empty() && !gaia.empty() && |
177 !hosted_domain.empty() && !full_name.empty() && !given_name.empty() && | 181 !hosted_domain.empty() && !full_name.empty() && !given_name.empty() && |
178 !locale.empty(); | 182 !locale.empty() && !picture_url.empty(); |
179 } | 183 } |
180 | 184 |
181 | 185 |
182 const char AccountTrackerService::kAccountInfoPref[] = "account_info"; | 186 const char AccountTrackerService::kAccountInfoPref[] = "account_info"; |
183 const char AccountTrackerService::kAccountTrackerServiceLastUpdate[] = | 187 const char AccountTrackerService::kAccountTrackerServiceLastUpdate[] = |
184 "account_tracker_service_last_update"; | 188 "account_tracker_service_last_update"; |
185 | 189 |
186 AccountTrackerService::AccountTrackerService() | 190 AccountTrackerService::AccountTrackerService() |
187 : token_service_(NULL), | 191 : token_service_(NULL), |
188 signin_client_(NULL), | 192 signin_client_(NULL), |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 if (user_info->GetString("hd", &hosted_domain) && !hosted_domain.empty()) { | 458 if (user_info->GetString("hd", &hosted_domain) && !hosted_domain.empty()) { |
455 state.info.hosted_domain = hosted_domain; | 459 state.info.hosted_domain = hosted_domain; |
456 } else { | 460 } else { |
457 state.info.hosted_domain = kNoHostedDomainFound; | 461 state.info.hosted_domain = kNoHostedDomainFound; |
458 } | 462 } |
459 | 463 |
460 user_info->GetString("name", &state.info.full_name); | 464 user_info->GetString("name", &state.info.full_name); |
461 user_info->GetString("given_name", &state.info.given_name); | 465 user_info->GetString("given_name", &state.info.given_name); |
462 user_info->GetString("locale", &state.info.locale); | 466 user_info->GetString("locale", &state.info.locale); |
463 | 467 |
| 468 std::string picture_url; |
| 469 if(user_info->GetString("picture", &picture_url)) { |
| 470 state.info.picture_url = picture_url; |
| 471 } else { |
| 472 state.info.picture_url = kNoPictureURLFound; |
| 473 } |
| 474 |
464 NotifyAccountUpdated(state); | 475 NotifyAccountUpdated(state); |
465 SaveToPrefs(state); | 476 SaveToPrefs(state); |
466 } | 477 } |
467 } | 478 } |
468 | 479 |
469 void AccountTrackerService::OnUserInfoFetchSuccess( | 480 void AccountTrackerService::OnUserInfoFetchSuccess( |
470 AccountInfoFetcher* fetcher, | 481 AccountInfoFetcher* fetcher, |
471 const base::DictionaryValue* user_info) { | 482 const base::DictionaryValue* user_info) { |
472 const std::string& account_id = fetcher->account_id(); | 483 const std::string& account_id = fetcher->account_id(); |
473 DCHECK(ContainsKey(accounts_, account_id)); | 484 DCHECK(ContainsKey(accounts_, account_id)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 if (dict->GetString(kAccountEmailPath, &value)) | 520 if (dict->GetString(kAccountEmailPath, &value)) |
510 state.info.email = base::UTF16ToUTF8(value); | 521 state.info.email = base::UTF16ToUTF8(value); |
511 if (dict->GetString(kAccountHostedDomainPath, &value)) | 522 if (dict->GetString(kAccountHostedDomainPath, &value)) |
512 state.info.hosted_domain = base::UTF16ToUTF8(value); | 523 state.info.hosted_domain = base::UTF16ToUTF8(value); |
513 if (dict->GetString(kAccountFullNamePath, &value)) | 524 if (dict->GetString(kAccountFullNamePath, &value)) |
514 state.info.full_name = base::UTF16ToUTF8(value); | 525 state.info.full_name = base::UTF16ToUTF8(value); |
515 if (dict->GetString(kAccountGivenNamePath, &value)) | 526 if (dict->GetString(kAccountGivenNamePath, &value)) |
516 state.info.given_name = base::UTF16ToUTF8(value); | 527 state.info.given_name = base::UTF16ToUTF8(value); |
517 if (dict->GetString(kAccountLocalePath, &value)) | 528 if (dict->GetString(kAccountLocalePath, &value)) |
518 state.info.locale = base::UTF16ToUTF8(value); | 529 state.info.locale = base::UTF16ToUTF8(value); |
| 530 if (dict->GetString(kAccountPictureURLPath, &value)) |
| 531 state.info.picture_url = base::UTF16ToUTF8(value); |
519 if (state.info.IsValid()) | 532 if (state.info.IsValid()) |
520 NotifyAccountUpdated(state); | 533 NotifyAccountUpdated(state); |
521 } | 534 } |
522 } | 535 } |
523 } | 536 } |
524 last_updated_ = base::Time::FromInternalValue( | 537 last_updated_ = base::Time::FromInternalValue( |
525 signin_client_->GetPrefs()->GetInt64(kAccountTrackerServiceLastUpdate)); | 538 signin_client_->GetPrefs()->GetInt64(kAccountTrackerServiceLastUpdate)); |
526 } | 539 } |
527 | 540 |
528 void AccountTrackerService::SaveToPrefs(const AccountState& state) { | 541 void AccountTrackerService::SaveToPrefs(const AccountState& state) { |
(...skipping 16 matching lines...) Expand all Loading... |
545 update->Append(dict); // |update| takes ownership. | 558 update->Append(dict); // |update| takes ownership. |
546 dict->SetString(kAccountKeyPath, account_id_16); | 559 dict->SetString(kAccountKeyPath, account_id_16); |
547 } | 560 } |
548 | 561 |
549 dict->SetString(kAccountEmailPath, state.info.email); | 562 dict->SetString(kAccountEmailPath, state.info.email); |
550 dict->SetString(kAccountGaiaPath, state.info.gaia); | 563 dict->SetString(kAccountGaiaPath, state.info.gaia); |
551 dict->SetString(kAccountHostedDomainPath, state.info.hosted_domain); | 564 dict->SetString(kAccountHostedDomainPath, state.info.hosted_domain); |
552 dict->SetString(kAccountFullNamePath, state.info.full_name); | 565 dict->SetString(kAccountFullNamePath, state.info.full_name); |
553 dict->SetString(kAccountGivenNamePath, state.info.given_name); | 566 dict->SetString(kAccountGivenNamePath, state.info.given_name); |
554 dict->SetString(kAccountLocalePath, state.info.locale); | 567 dict->SetString(kAccountLocalePath, state.info.locale); |
| 568 dict->SetString(kAccountPictureURLPath, state.info.picture_url); |
555 } | 569 } |
556 | 570 |
557 void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { | 571 void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { |
558 if (!signin_client_->GetPrefs()) | 572 if (!signin_client_->GetPrefs()) |
559 return; | 573 return; |
560 | 574 |
561 base::string16 account_id_16 = base::UTF8ToUTF16(state.info.account_id); | 575 base::string16 account_id_16 = base::UTF8ToUTF16(state.info.account_id); |
562 ListPrefUpdate update(signin_client_->GetPrefs(), kAccountInfoPref); | 576 ListPrefUpdate update(signin_client_->GetPrefs(), kAccountInfoPref); |
563 for(size_t i = 0; i < update->GetSize(); ++i) { | 577 for(size_t i = 0; i < update->GetSize(); ++i) { |
564 base::DictionaryValue* dict = NULL; | 578 base::DictionaryValue* dict = NULL; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 state.info.email = email; | 686 state.info.email = email; |
673 SaveToPrefs(state); | 687 SaveToPrefs(state); |
674 | 688 |
675 DVLOG(1) << "AccountTrackerService::SeedAccountInfo" | 689 DVLOG(1) << "AccountTrackerService::SeedAccountInfo" |
676 << " account_id=" << account_id | 690 << " account_id=" << account_id |
677 << " gaia_id=" << gaia | 691 << " gaia_id=" << gaia |
678 << " email=" << email; | 692 << " email=" << email; |
679 | 693 |
680 return account_id; | 694 return account_id; |
681 } | 695 } |
| 696 |
| 697 void AccountTrackerService::SeedAccountInfo( |
| 698 AccountTrackerService::AccountInfo info) { |
| 699 info.account_id = PickAccountIdForAccount(info.gaia, info.email); |
| 700 if (info.hosted_domain.empty()) { |
| 701 info.hosted_domain = kNoHostedDomainFound; |
| 702 } |
| 703 |
| 704 if(info.IsValid()) { |
| 705 if(!ContainsKey(accounts_, info.account_id)) { |
| 706 SeedAccountInfo(info.gaia, info.email); |
| 707 } |
| 708 |
| 709 AccountState& state = accounts_[info.account_id]; |
| 710 state.info = info; |
| 711 NotifyAccountUpdated(state); |
| 712 SaveToPrefs(state); |
| 713 } |
| 714 } |
OLD | NEW |