| 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/signin/core/browser/account_fetcher_service.h" | 5 #include "components/signin/core/browser/account_fetcher_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "components/pref_registry/pref_registry_syncable.h" | 12 #include "components/pref_registry/pref_registry_syncable.h" |
| 13 #include "components/signin/core/browser/account_info_fetcher.h" | 13 #include "components/signin/core/browser/account_info_fetcher.h" |
| 14 #include "components/signin/core/browser/account_seeding_tracker.h" |
| 14 #include "components/signin/core/browser/account_tracker_service.h" | 15 #include "components/signin/core/browser/account_tracker_service.h" |
| 15 #include "components/signin/core/browser/child_account_info_fetcher.h" | 16 #include "components/signin/core/browser/child_account_info_fetcher.h" |
| 16 #include "components/signin/core/browser/refresh_token_annotation_request.h" | 17 #include "components/signin/core/browser/refresh_token_annotation_request.h" |
| 17 #include "components/signin/core/browser/signin_client.h" | 18 #include "components/signin/core/browser/signin_client.h" |
| 18 #include "components/signin/core/common/signin_switches.h" | 19 #include "components/signin/core/common/signin_switches.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const base::TimeDelta kRefreshFromTokenServiceDelay = | 24 const base::TimeDelta kRefreshFromTokenServiceDelay = |
| (...skipping 24 matching lines...) Expand all Loading... |
| 48 "account_tracker_service_last_update"; | 49 "account_tracker_service_last_update"; |
| 49 | 50 |
| 50 // AccountFetcherService implementation | 51 // AccountFetcherService implementation |
| 51 AccountFetcherService::AccountFetcherService() | 52 AccountFetcherService::AccountFetcherService() |
| 52 : account_tracker_service_(nullptr), | 53 : account_tracker_service_(nullptr), |
| 53 token_service_(nullptr), | 54 token_service_(nullptr), |
| 54 signin_client_(nullptr), | 55 signin_client_(nullptr), |
| 55 invalidation_service_(nullptr), | 56 invalidation_service_(nullptr), |
| 56 network_fetches_enabled_(false), | 57 network_fetches_enabled_(false), |
| 57 shutdown_called_(false), | 58 shutdown_called_(false), |
| 58 child_info_request_(nullptr) {} | 59 child_info_request_(nullptr), |
| 60 account_seeding_tracker_(nullptr) {} |
| 59 | 61 |
| 60 AccountFetcherService::~AccountFetcherService() { | 62 AccountFetcherService::~AccountFetcherService() { |
| 61 DCHECK(shutdown_called_); | 63 DCHECK(shutdown_called_); |
| 62 } | 64 } |
| 63 | 65 |
| 64 // static | 66 // static |
| 65 void AccountFetcherService::RegisterPrefs( | 67 void AccountFetcherService::RegisterPrefs( |
| 66 user_prefs::PrefRegistrySyncable* user_prefs) { | 68 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 67 user_prefs->RegisterInt64Pref(kLastUpdatePref, 0); | 69 user_prefs->RegisterInt64Pref(kLastUpdatePref, 0); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void AccountFetcherService::Initialize( | 72 void AccountFetcherService::Initialize( |
| 71 SigninClient* signin_client, | 73 SigninClient* signin_client, |
| 72 OAuth2TokenService* token_service, | 74 OAuth2TokenService* token_service, |
| 73 AccountTrackerService* account_tracker_service, | 75 AccountTrackerService* account_tracker_service, |
| 74 invalidation::InvalidationService* invalidation_service) { | 76 invalidation::InvalidationService* invalidation_service) { |
| 75 DCHECK(signin_client); | 77 DCHECK(signin_client); |
| 76 DCHECK(!signin_client_); | 78 DCHECK(!signin_client_); |
| 77 signin_client_ = signin_client; | 79 signin_client_ = signin_client; |
| 78 invalidation_service_ = invalidation_service; | 80 invalidation_service_ = invalidation_service; |
| 79 DCHECK(account_tracker_service); | 81 DCHECK(account_tracker_service); |
| 80 DCHECK(!account_tracker_service_); | 82 DCHECK(!account_tracker_service_); |
| 81 account_tracker_service_ = account_tracker_service; | 83 account_tracker_service_ = account_tracker_service; |
| 84 account_seeding_tracker_.reset( |
| 85 new AccountSeedingTracker(account_tracker_service)); |
| 82 DCHECK(token_service); | 86 DCHECK(token_service); |
| 83 DCHECK(!token_service_); | 87 DCHECK(!token_service_); |
| 84 token_service_ = token_service; | 88 token_service_ = token_service; |
| 85 token_service_->AddObserver(this); | 89 token_service_->AddObserver(this); |
| 86 | 90 |
| 87 last_updated_ = base::Time::FromInternalValue( | 91 last_updated_ = base::Time::FromInternalValue( |
| 88 signin_client_->GetPrefs()->GetInt64(kLastUpdatePref)); | 92 signin_client_->GetPrefs()->GetInt64(kLastUpdatePref)); |
| 89 | 93 |
| 90 RefreshAllAccountInfo(true); | 94 RefreshAllAccountInfo(true); |
| 91 } | 95 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 115 | 119 |
| 116 bool AccountFetcherService::IsAllUserInfoFetched() const { | 120 bool AccountFetcherService::IsAllUserInfoFetched() const { |
| 117 return user_info_requests_.empty(); | 121 return user_info_requests_.empty(); |
| 118 } | 122 } |
| 119 | 123 |
| 120 void AccountFetcherService::FetchUserInfoBeforeSignin( | 124 void AccountFetcherService::FetchUserInfoBeforeSignin( |
| 121 const std::string& account_id) { | 125 const std::string& account_id) { |
| 122 RefreshAccountInfo(account_id, false); | 126 RefreshAccountInfo(account_id, false); |
| 123 } | 127 } |
| 124 | 128 |
| 129 #if defined(OS_ANDROID) |
| 130 void AccountFetcherService::OnAccountsSeeded() { |
| 131 std::vector<std::string> newly_seeded_acounts( |
| 132 account_seeding_tracker_->GetNewlySeededAccounts()); |
| 133 for (const std::string& account : newly_seeded_acounts) |
| 134 StartFetchingUserInfo(account); |
| 135 UpdateChildInfo(); |
| 136 } |
| 137 |
| 138 bool AccountFetcherService::AreAllAccountsSeeded() { |
| 139 return account_seeding_tracker_->AreAllAccountsSeeded(); |
| 140 } |
| 141 #endif |
| 142 |
| 125 void AccountFetcherService::RefreshAllAccountInfo(bool only_fetch_if_invalid) { | 143 void AccountFetcherService::RefreshAllAccountInfo(bool only_fetch_if_invalid) { |
| 126 std::vector<std::string> accounts = token_service_->GetAccounts(); | 144 std::vector<std::string> accounts = token_service_->GetAccounts(); |
| 127 for (std::vector<std::string>::const_iterator it = accounts.begin(); | 145 for (std::vector<std::string>::const_iterator it = accounts.begin(); |
| 128 it != accounts.end(); ++it) { | 146 it != accounts.end(); ++it) { |
| 129 RefreshAccountInfo(*it, only_fetch_if_invalid); | 147 RefreshAccountInfo(*it, only_fetch_if_invalid); |
| 130 } | 148 } |
| 131 } | 149 } |
| 132 | 150 |
| 133 // Child account status is refreshed through invalidations which are only | 151 // Child account status is refreshed through invalidations which are only |
| 134 // available for the primary account. Finding the primary account requires a | 152 // available for the primary account. Finding the primary account requires a |
| 135 // dependency on signin_manager which we get around by only allowing a single | 153 // dependency on signin_manager which we get around by only allowing a single |
| 136 // account. This is possible since we only support a single account to be a | 154 // account. This is possible since we only support a single account to be a |
| 137 // child anyway. | 155 // child anyway. |
| 138 void AccountFetcherService::UpdateChildInfo() { | 156 void AccountFetcherService::UpdateChildInfo() { |
| 139 DCHECK(CalledOnValidThread()); | 157 DCHECK(CalledOnValidThread()); |
| 140 std::vector<std::string> accounts = token_service_->GetAccounts(); | 158 std::vector<std::string> accounts = token_service_->GetAccounts(); |
| 141 if (accounts.size() == 1) { | 159 if (accounts.size() == 1) { |
| 142 const std::string& candidate = accounts[0]; | 160 const std::string& candidate = accounts[0]; |
| 143 if (candidate == child_request_account_id_) | 161 if (candidate == child_request_account_id_) |
| 144 return; | 162 return; |
| 145 if (!child_request_account_id_.empty()) | 163 if (!child_request_account_id_.empty()) |
| 146 ResetChildInfo(); | 164 ResetChildInfo(); |
| 147 if (!AccountSupportsUserInfo(candidate)) | 165 if (!AccountSupportsUserInfo(candidate)) |
| 148 return; | 166 return; |
| 167 if (!account_seeding_tracker_->IsAccountSeeded(candidate)) |
| 168 return; |
| 149 child_request_account_id_ = candidate; | 169 child_request_account_id_ = candidate; |
| 150 StartFetchingChildInfo(candidate); | 170 StartFetchingChildInfo(candidate); |
| 151 } else { | 171 } else { |
| 152 ResetChildInfo(); | 172 ResetChildInfo(); |
| 153 } | 173 } |
| 154 } | 174 } |
| 155 | 175 |
| 156 void AccountFetcherService::RefreshAllAccountsAndScheduleNext() { | 176 void AccountFetcherService::RefreshAllAccountsAndScheduleNext() { |
| 157 DCHECK(network_fetches_enabled_); | 177 DCHECK(network_fetches_enabled_); |
| 158 RefreshAllAccountInfo(false); | 178 RefreshAllAccountInfo(false); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 177 } | 197 } |
| 178 | 198 |
| 179 // Starts fetching user information. This is called periodically to refresh. | 199 // Starts fetching user information. This is called periodically to refresh. |
| 180 void AccountFetcherService::StartFetchingUserInfo( | 200 void AccountFetcherService::StartFetchingUserInfo( |
| 181 const std::string& account_id) { | 201 const std::string& account_id) { |
| 182 DCHECK(CalledOnValidThread()); | 202 DCHECK(CalledOnValidThread()); |
| 183 if (!network_fetches_enabled_) { | 203 if (!network_fetches_enabled_) { |
| 184 pending_user_info_fetches_.push_back(account_id); | 204 pending_user_info_fetches_.push_back(account_id); |
| 185 return; | 205 return; |
| 186 } | 206 } |
| 207 if (!account_seeding_tracker_->IsAccountSeeded(account_id)) |
| 208 return; |
| 187 | 209 |
| 188 if (!ContainsKey(user_info_requests_, account_id)) { | 210 if (!ContainsKey(user_info_requests_, account_id)) { |
| 189 DVLOG(1) << "StartFetching " << account_id; | 211 DVLOG(1) << "StartFetching " << account_id; |
| 190 scoped_ptr<AccountInfoFetcher> fetcher(new AccountInfoFetcher( | 212 scoped_ptr<AccountInfoFetcher> fetcher(new AccountInfoFetcher( |
| 191 token_service_, signin_client_->GetURLRequestContext(), this, | 213 token_service_, signin_client_->GetURLRequestContext(), this, |
| 192 account_id)); | 214 account_id)); |
| 193 user_info_requests_.set(account_id, fetcher.Pass()); | 215 user_info_requests_.set(account_id, fetcher.Pass()); |
| 194 user_info_requests_.get(account_id)->Start(); | 216 user_info_requests_.get(account_id)->Start(); |
| 195 } | 217 } |
| 196 } | 218 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 UpdateChildInfo(); | 335 UpdateChildInfo(); |
| 314 account_tracker_service_->StopTrackingAccount(account_id); | 336 account_tracker_service_->StopTrackingAccount(account_id); |
| 315 } | 337 } |
| 316 | 338 |
| 317 void AccountFetcherService::OnRefreshTokensLoaded() { | 339 void AccountFetcherService::OnRefreshTokensLoaded() { |
| 318 // OnRefreshTokenAvailable has been called for all accounts by this point. | 340 // OnRefreshTokenAvailable has been called for all accounts by this point. |
| 319 // Maybe remove this after further investigation. | 341 // Maybe remove this after further investigation. |
| 320 RefreshAllAccountInfo(true); | 342 RefreshAllAccountInfo(true); |
| 321 UpdateChildInfo(); | 343 UpdateChildInfo(); |
| 322 } | 344 } |
| OLD | NEW |