| 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const char kAccountKeyPath[] = "account_id"; | 30 const char kAccountKeyPath[] = "account_id"; |
| 31 const char kAccountEmailPath[] = "email"; | 31 const char kAccountEmailPath[] = "email"; |
| 32 const char kAccountGaiaPath[] = "gaia"; | 32 const char kAccountGaiaPath[] = "gaia"; |
| 33 const char kAccountHostedDomainPath[] = "hd"; | 33 const char kAccountHostedDomainPath[] = "hd"; |
| 34 const char kAccountFullNamePath[] = "full_name"; | 34 const char kAccountFullNamePath[] = "full_name"; |
| 35 const char kAccountGivenNamePath[] = "given_name"; | 35 const char kAccountGivenNamePath[] = "given_name"; |
| 36 const char kAccountLocalePath[] = "locale"; | 36 const char kAccountLocalePath[] = "locale"; |
| 37 const char kAccountPictureURLPath[] = "picture_url"; |
| 37 const char kAccountServiceFlagsPath[] = "service_flags"; | 38 const char kAccountServiceFlagsPath[] = "service_flags"; |
| 38 | 39 |
| 39 const base::TimeDelta kRefreshFromTokenServiceDelay = | 40 const base::TimeDelta kRefreshFromTokenServiceDelay = |
| 40 base::TimeDelta::FromHours(24); | 41 base::TimeDelta::FromHours(24); |
| 41 | 42 |
| 42 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 43 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 43 // IsRefreshTokenDeviceIdExperimentEnabled is called from | 44 // IsRefreshTokenDeviceIdExperimentEnabled is called from |
| 44 // SendRefreshTokenAnnotationRequest only on desktop platforms. | 45 // SendRefreshTokenAnnotationRequest only on desktop platforms. |
| 45 bool IsRefreshTokenDeviceIdExperimentEnabled() { | 46 bool IsRefreshTokenDeviceIdExperimentEnabled() { |
| 46 const std::string group_name = | 47 const std::string group_name = |
| 47 base::FieldTrialList::FindFullName("RefreshTokenDeviceId"); | 48 base::FieldTrialList::FindFullName("RefreshTokenDeviceId"); |
| 48 return group_name == "Enabled"; | 49 return group_name == "Enabled"; |
| 49 } | 50 } |
| 50 #endif | 51 #endif |
| 51 } | 52 } |
| 52 | 53 |
| 53 // This must be a string which can never be a valid domain. | 54 // This must be a string which can never be a valid domain. |
| 54 const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; | 55 const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; |
| 55 | 56 |
| 57 // This must be a string which can never be a valid picture URL. |
| 58 const char AccountTrackerService::kNoPictureURLFound[] = "NO_PICTURE_URL"; |
| 59 |
| 56 class AccountInfoFetcher : public OAuth2TokenService::Consumer, | 60 class AccountInfoFetcher : public OAuth2TokenService::Consumer, |
| 57 public gaia::GaiaOAuthClient::Delegate, | 61 public gaia::GaiaOAuthClient::Delegate, |
| 58 public GaiaAuthConsumer { | 62 public GaiaAuthConsumer { |
| 59 public: | 63 public: |
| 60 AccountInfoFetcher(OAuth2TokenService* token_service, | 64 AccountInfoFetcher(OAuth2TokenService* token_service, |
| 61 net::URLRequestContextGetter* request_context_getter, | 65 net::URLRequestContextGetter* request_context_getter, |
| 62 AccountTrackerService* service, | 66 AccountTrackerService* service, |
| 63 const std::string& account_id); | 67 const std::string& account_id); |
| 64 ~AccountInfoFetcher() override; | 68 ~AccountInfoFetcher() override; |
| 65 | 69 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 "OnNetworkError", | 227 "OnNetworkError", |
| 224 "response_code", | 228 "response_code", |
| 225 response_code); | 229 response_code); |
| 226 LOG(ERROR) << "OnNetworkError " << response_code; | 230 LOG(ERROR) << "OnNetworkError " << response_code; |
| 227 service_->OnUserInfoFetchFailure(this); | 231 service_->OnUserInfoFetchFailure(this); |
| 228 } | 232 } |
| 229 | 233 |
| 230 AccountTrackerService::AccountInfo::AccountInfo() {} | 234 AccountTrackerService::AccountInfo::AccountInfo() {} |
| 231 AccountTrackerService::AccountInfo::~AccountInfo() {} | 235 AccountTrackerService::AccountInfo::~AccountInfo() {} |
| 232 | 236 |
| 233 bool AccountTrackerService::AccountInfo::IsValid() { | 237 bool AccountTrackerService::AccountInfo::IsValid() const { |
| 234 return !account_id.empty() && !email.empty() && !gaia.empty() && | 238 return !account_id.empty() && !email.empty() && !gaia.empty() && |
| 235 !hosted_domain.empty() && !full_name.empty() && !given_name.empty() && | 239 !hosted_domain.empty() && !full_name.empty() && !given_name.empty() && |
| 236 !locale.empty(); | 240 !locale.empty() && !picture_url.empty(); |
| 237 } | 241 } |
| 238 | 242 |
| 239 | 243 |
| 240 const char AccountTrackerService::kAccountInfoPref[] = "account_info"; | 244 const char AccountTrackerService::kAccountInfoPref[] = "account_info"; |
| 241 const char AccountTrackerService::kAccountTrackerServiceLastUpdate[] = | 245 const char AccountTrackerService::kAccountTrackerServiceLastUpdate[] = |
| 242 "account_tracker_service_last_update"; | 246 "account_tracker_service_last_update"; |
| 243 | 247 |
| 244 AccountTrackerService::AccountTrackerService() | 248 AccountTrackerService::AccountTrackerService() |
| 245 : token_service_(NULL), | 249 : token_service_(NULL), |
| 246 signin_client_(NULL), | 250 signin_client_(NULL), |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 if (user_info->GetString("hd", &hosted_domain) && !hosted_domain.empty()) { | 517 if (user_info->GetString("hd", &hosted_domain) && !hosted_domain.empty()) { |
| 514 state.info.hosted_domain = hosted_domain; | 518 state.info.hosted_domain = hosted_domain; |
| 515 } else { | 519 } else { |
| 516 state.info.hosted_domain = kNoHostedDomainFound; | 520 state.info.hosted_domain = kNoHostedDomainFound; |
| 517 } | 521 } |
| 518 | 522 |
| 519 user_info->GetString("name", &state.info.full_name); | 523 user_info->GetString("name", &state.info.full_name); |
| 520 user_info->GetString("given_name", &state.info.given_name); | 524 user_info->GetString("given_name", &state.info.given_name); |
| 521 user_info->GetString("locale", &state.info.locale); | 525 user_info->GetString("locale", &state.info.locale); |
| 522 | 526 |
| 527 std::string picture_url; |
| 528 if(user_info->GetString("picture", &picture_url)) { |
| 529 state.info.picture_url = picture_url; |
| 530 } else { |
| 531 state.info.picture_url = kNoPictureURLFound; |
| 532 } |
| 533 |
| 523 state.info.service_flags = *service_flags; | 534 state.info.service_flags = *service_flags; |
| 524 | 535 |
| 525 NotifyAccountUpdated(state); | 536 NotifyAccountUpdated(state); |
| 526 SaveToPrefs(state); | 537 SaveToPrefs(state); |
| 527 } | 538 } |
| 528 } | 539 } |
| 529 | 540 |
| 530 void AccountTrackerService::OnUserInfoFetchSuccess( | 541 void AccountTrackerService::OnUserInfoFetchSuccess( |
| 531 AccountInfoFetcher* fetcher, | 542 AccountInfoFetcher* fetcher, |
| 532 const base::DictionaryValue* user_info, | 543 const base::DictionaryValue* user_info, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 if (dict->GetString(kAccountEmailPath, &value)) | 582 if (dict->GetString(kAccountEmailPath, &value)) |
| 572 state.info.email = base::UTF16ToUTF8(value); | 583 state.info.email = base::UTF16ToUTF8(value); |
| 573 if (dict->GetString(kAccountHostedDomainPath, &value)) | 584 if (dict->GetString(kAccountHostedDomainPath, &value)) |
| 574 state.info.hosted_domain = base::UTF16ToUTF8(value); | 585 state.info.hosted_domain = base::UTF16ToUTF8(value); |
| 575 if (dict->GetString(kAccountFullNamePath, &value)) | 586 if (dict->GetString(kAccountFullNamePath, &value)) |
| 576 state.info.full_name = base::UTF16ToUTF8(value); | 587 state.info.full_name = base::UTF16ToUTF8(value); |
| 577 if (dict->GetString(kAccountGivenNamePath, &value)) | 588 if (dict->GetString(kAccountGivenNamePath, &value)) |
| 578 state.info.given_name = base::UTF16ToUTF8(value); | 589 state.info.given_name = base::UTF16ToUTF8(value); |
| 579 if (dict->GetString(kAccountLocalePath, &value)) | 590 if (dict->GetString(kAccountLocalePath, &value)) |
| 580 state.info.locale = base::UTF16ToUTF8(value); | 591 state.info.locale = base::UTF16ToUTF8(value); |
| 592 if (dict->GetString(kAccountPictureURLPath, &value)) |
| 593 state.info.picture_url = base::UTF16ToUTF8(value); |
| 581 | 594 |
| 582 const base::ListValue* service_flags_list; | 595 const base::ListValue* service_flags_list; |
| 583 if (dict->GetList(kAccountServiceFlagsPath, &service_flags_list)) { | 596 if (dict->GetList(kAccountServiceFlagsPath, &service_flags_list)) { |
| 584 std::string flag; | 597 std::string flag; |
| 585 for(base::Value* flag: *service_flags_list) { | 598 for(base::Value* flag: *service_flags_list) { |
| 586 std::string flag_string; | 599 std::string flag_string; |
| 587 if(flag->GetAsString(&flag_string)) { | 600 if(flag->GetAsString(&flag_string)) { |
| 588 state.info.service_flags.push_back(flag_string); | 601 state.info.service_flags.push_back(flag_string); |
| 589 } | 602 } |
| 590 } | 603 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 619 update->Append(dict); // |update| takes ownership. | 632 update->Append(dict); // |update| takes ownership. |
| 620 dict->SetString(kAccountKeyPath, account_id_16); | 633 dict->SetString(kAccountKeyPath, account_id_16); |
| 621 } | 634 } |
| 622 | 635 |
| 623 dict->SetString(kAccountEmailPath, state.info.email); | 636 dict->SetString(kAccountEmailPath, state.info.email); |
| 624 dict->SetString(kAccountGaiaPath, state.info.gaia); | 637 dict->SetString(kAccountGaiaPath, state.info.gaia); |
| 625 dict->SetString(kAccountHostedDomainPath, state.info.hosted_domain); | 638 dict->SetString(kAccountHostedDomainPath, state.info.hosted_domain); |
| 626 dict->SetString(kAccountFullNamePath, state.info.full_name); | 639 dict->SetString(kAccountFullNamePath, state.info.full_name); |
| 627 dict->SetString(kAccountGivenNamePath, state.info.given_name); | 640 dict->SetString(kAccountGivenNamePath, state.info.given_name); |
| 628 dict->SetString(kAccountLocalePath, state.info.locale); | 641 dict->SetString(kAccountLocalePath, state.info.locale); |
| 642 dict->SetString(kAccountPictureURLPath, state.info.picture_url); |
| 629 | 643 |
| 630 scoped_ptr<base::ListValue> service_flags_list; | 644 scoped_ptr<base::ListValue> service_flags_list; |
| 631 service_flags_list.reset(new base::ListValue); | 645 service_flags_list.reset(new base::ListValue); |
| 632 service_flags_list->AppendStrings(state.info.service_flags); | 646 service_flags_list->AppendStrings(state.info.service_flags); |
| 633 | 647 |
| 634 dict->Set(kAccountServiceFlagsPath, service_flags_list.Pass()); | 648 dict->Set(kAccountServiceFlagsPath, service_flags_list.Pass()); |
| 635 } | 649 } |
| 636 | 650 |
| 637 void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { | 651 void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { |
| 638 if (!signin_client_->GetPrefs()) | 652 if (!signin_client_->GetPrefs()) |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 state.info.email = email; | 766 state.info.email = email; |
| 753 SaveToPrefs(state); | 767 SaveToPrefs(state); |
| 754 | 768 |
| 755 DVLOG(1) << "AccountTrackerService::SeedAccountInfo" | 769 DVLOG(1) << "AccountTrackerService::SeedAccountInfo" |
| 756 << " account_id=" << account_id | 770 << " account_id=" << account_id |
| 757 << " gaia_id=" << gaia | 771 << " gaia_id=" << gaia |
| 758 << " email=" << email; | 772 << " email=" << email; |
| 759 | 773 |
| 760 return account_id; | 774 return account_id; |
| 761 } | 775 } |
| 776 |
| 777 void AccountTrackerService::SeedAccountInfo( |
| 778 AccountTrackerService::AccountInfo info) { |
| 779 info.account_id = PickAccountIdForAccount(info.gaia, info.email); |
| 780 if (info.hosted_domain.empty()) { |
| 781 info.hosted_domain = kNoHostedDomainFound; |
| 782 } |
| 783 |
| 784 if(info.IsValid()) { |
| 785 if(!ContainsKey(accounts_, info.account_id)) { |
| 786 SeedAccountInfo(info.gaia, info.email); |
| 787 } |
| 788 |
| 789 AccountState& state = accounts_[info.account_id]; |
| 790 state.info = info; |
| 791 NotifyAccountUpdated(state); |
| 792 SaveToPrefs(state); |
| 793 } |
| 794 } |
| OLD | NEW |