| 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_tracker_service.h" | 14 #include "components/signin/core/browser/account_tracker_service.h" |
| 15 #include "components/signin/core/browser/child_account_info_fetcher.h" | 15 #include "components/signin/core/browser/child_account_info_fetcher.h" |
| 16 #include "components/signin/core/browser/refresh_token_annotation_request.h" | 16 #include "components/signin/core/browser/refresh_token_annotation_request.h" |
| 17 #include "components/signin/core/browser/signin_client.h" | 17 #include "components/signin/core/browser/signin_client.h" |
| 18 #include "components/signin/core/common/signin_switches.h" | 18 #include "components/signin/core/common/signin_switches.h" |
| 19 #include "google_apis/gaia/oauth2_token_service_delegate.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 = |
| 24 base::TimeDelta::FromHours(24); | 25 base::TimeDelta::FromHours(24); |
| 25 | 26 |
| 26 bool AccountSupportsUserInfo(const std::string& account_id) { | 27 bool AccountSupportsUserInfo(const std::string& account_id) { |
| 27 // Supervised users use a specially scoped token which when used for general | 28 // Supervised users use a specially scoped token which when used for general |
| 28 // purposes causes the token service to raise spurious auth errors. | 29 // purposes causes the token service to raise spurious auth errors. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // child_info_request_ is an invalidation handler and needs to be | 96 // child_info_request_ is an invalidation handler and needs to be |
| 96 // unregistered during the lifetime of the invalidation service. | 97 // unregistered during the lifetime of the invalidation service. |
| 97 child_info_request_.reset(); | 98 child_info_request_.reset(); |
| 98 shutdown_called_ = true; | 99 shutdown_called_ = true; |
| 99 } | 100 } |
| 100 | 101 |
| 101 void AccountFetcherService::EnableNetworkFetches() { | 102 void AccountFetcherService::EnableNetworkFetches() { |
| 102 DCHECK(CalledOnValidThread()); | 103 DCHECK(CalledOnValidThread()); |
| 103 DCHECK(!network_fetches_enabled_); | 104 DCHECK(!network_fetches_enabled_); |
| 104 network_fetches_enabled_ = true; | 105 network_fetches_enabled_ = true; |
| 105 // If there are accounts in |pending_user_info_fetches_|, they were deemed | 106 FetchPendingAccountInfo(); |
| 106 // invalid after being loaded from prefs and need to be fetched now instead of | |
| 107 // waiting after the timer. | |
| 108 for (const std::string& account_id : pending_user_info_fetches_) | |
| 109 StartFetchingUserInfo(account_id); | |
| 110 pending_user_info_fetches_.clear(); | |
| 111 | 107 |
| 112 // Now that network fetches are enabled, schedule the next refresh. | 108 // Now that network fetches are enabled, schedule the next refresh. |
| 113 ScheduleNextRefresh(); | 109 ScheduleNextRefresh(); |
| 114 } | 110 } |
| 115 | 111 |
| 116 bool AccountFetcherService::IsAllUserInfoFetched() const { | 112 bool AccountFetcherService::IsAllUserInfoFetched() const { |
| 117 return user_info_requests_.empty(); | 113 return user_info_requests_.empty(); |
| 118 } | 114 } |
| 119 | 115 |
| 120 void AccountFetcherService::FetchUserInfoBeforeSignin( | 116 void AccountFetcherService::FetchUserInfoBeforeSignin( |
| 121 const std::string& account_id) { | 117 const std::string& account_id) { |
| 122 RefreshAccountInfo(account_id, false); | 118 RefreshAccountInfo(account_id, false); |
| 123 } | 119 } |
| 124 | 120 |
| 121 #if defined(OS_ANDROID) |
| 122 void AccountFetcherService::OnAccountIdNameMapSeeded() { |
| 123 DCHECK(CalledOnValidThread()); |
| 124 DCHECK(token_service_->GetDelegate()->NeedsAccountIdNameMap()); |
| 125 FetchPendingAccountInfo(); |
| 126 } |
| 127 #endif |
| 128 |
| 125 void AccountFetcherService::RefreshAllAccountInfo(bool only_fetch_if_invalid) { | 129 void AccountFetcherService::RefreshAllAccountInfo(bool only_fetch_if_invalid) { |
| 126 std::vector<std::string> accounts = token_service_->GetAccounts(); | 130 std::vector<std::string> accounts = token_service_->GetAccounts(); |
| 127 for (std::vector<std::string>::const_iterator it = accounts.begin(); | 131 for (std::vector<std::string>::const_iterator it = accounts.begin(); |
| 128 it != accounts.end(); ++it) { | 132 it != accounts.end(); ++it) { |
| 129 RefreshAccountInfo(*it, only_fetch_if_invalid); | 133 RefreshAccountInfo(*it, only_fetch_if_invalid); |
| 130 } | 134 } |
| 131 } | 135 } |
| 132 | 136 |
| 133 // Child account status is refreshed through invalidations which are only | 137 // Child account status is refreshed through invalidations which are only |
| 134 // available for the primary account. Finding the primary account requires a | 138 // 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 | 139 // 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 | 140 // account. This is possible since we only support a single account to be a |
| 137 // child anyway. | 141 // child anyway. |
| 138 void AccountFetcherService::UpdateChildInfo() { | 142 void AccountFetcherService::UpdateChildInfo() { |
| 139 DCHECK(CalledOnValidThread()); | 143 DCHECK(CalledOnValidThread()); |
| 140 std::vector<std::string> accounts = token_service_->GetAccounts(); | 144 std::vector<std::string> accounts = token_service_->GetAccounts(); |
| 141 if (accounts.size() == 1) { | 145 if (accounts.size() == 1) { |
| 142 const std::string& candidate = accounts[0]; | 146 const std::string& candidate = accounts[0]; |
| 143 if (candidate == child_request_account_id_) | 147 if (candidate == child_request_account_id_) |
| 144 return; | 148 return; |
| 145 if (!child_request_account_id_.empty()) | 149 if (!child_request_account_id_.empty()) |
| 146 ResetChildInfo(); | 150 ResetChildInfo(); |
| 147 if (!AccountSupportsUserInfo(candidate)) | 151 if (!AccountSupportsUserInfo(candidate)) |
| 148 return; | 152 return; |
| 153 if (DeferFetchingAccountInfo(candidate)) |
| 154 return; |
| 149 child_request_account_id_ = candidate; | 155 child_request_account_id_ = candidate; |
| 150 StartFetchingChildInfo(candidate); | 156 StartFetchingChildInfo(candidate); |
| 151 } else { | 157 } else { |
| 152 ResetChildInfo(); | 158 ResetChildInfo(); |
| 153 } | 159 } |
| 154 } | 160 } |
| 155 | 161 |
| 156 void AccountFetcherService::RefreshAllAccountsAndScheduleNext() { | 162 void AccountFetcherService::RefreshAllAccountsAndScheduleNext() { |
| 157 DCHECK(network_fetches_enabled_); | 163 DCHECK(network_fetches_enabled_); |
| 158 RefreshAllAccountInfo(false); | 164 RefreshAllAccountInfo(false); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 173 timer_.Start(FROM_HERE, kRefreshFromTokenServiceDelay - time_since_update, | 179 timer_.Start(FROM_HERE, kRefreshFromTokenServiceDelay - time_since_update, |
| 174 this, | 180 this, |
| 175 &AccountFetcherService::RefreshAllAccountsAndScheduleNext); | 181 &AccountFetcherService::RefreshAllAccountsAndScheduleNext); |
| 176 } | 182 } |
| 177 } | 183 } |
| 178 | 184 |
| 179 // Starts fetching user information. This is called periodically to refresh. | 185 // Starts fetching user information. This is called periodically to refresh. |
| 180 void AccountFetcherService::StartFetchingUserInfo( | 186 void AccountFetcherService::StartFetchingUserInfo( |
| 181 const std::string& account_id) { | 187 const std::string& account_id) { |
| 182 DCHECK(CalledOnValidThread()); | 188 DCHECK(CalledOnValidThread()); |
| 183 if (!network_fetches_enabled_) { | 189 if (DeferFetchingAccountInfo(account_id)) |
| 184 pending_user_info_fetches_.push_back(account_id); | |
| 185 return; | 190 return; |
| 186 } | |
| 187 | 191 |
| 188 if (!ContainsKey(user_info_requests_, account_id)) { | 192 if (!ContainsKey(user_info_requests_, account_id)) { |
| 189 DVLOG(1) << "StartFetching " << account_id; | 193 DVLOG(1) << "StartFetching " << account_id; |
| 190 scoped_ptr<AccountInfoFetcher> fetcher(new AccountInfoFetcher( | 194 scoped_ptr<AccountInfoFetcher> fetcher(new AccountInfoFetcher( |
| 191 token_service_, signin_client_->GetURLRequestContext(), this, | 195 token_service_, signin_client_->GetURLRequestContext(), this, |
| 192 account_id)); | 196 account_id)); |
| 193 user_info_requests_.set(account_id, fetcher.Pass()); | 197 user_info_requests_.set(account_id, fetcher.Pass()); |
| 194 user_info_requests_.get(account_id)->Start(); | 198 user_info_requests_.get(account_id)->Start(); |
| 195 } | 199 } |
| 196 } | 200 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 UpdateChildInfo(); | 317 UpdateChildInfo(); |
| 314 account_tracker_service_->StopTrackingAccount(account_id); | 318 account_tracker_service_->StopTrackingAccount(account_id); |
| 315 } | 319 } |
| 316 | 320 |
| 317 void AccountFetcherService::OnRefreshTokensLoaded() { | 321 void AccountFetcherService::OnRefreshTokensLoaded() { |
| 318 // OnRefreshTokenAvailable has been called for all accounts by this point. | 322 // OnRefreshTokenAvailable has been called for all accounts by this point. |
| 319 // Maybe remove this after further investigation. | 323 // Maybe remove this after further investigation. |
| 320 RefreshAllAccountInfo(true); | 324 RefreshAllAccountInfo(true); |
| 321 UpdateChildInfo(); | 325 UpdateChildInfo(); |
| 322 } | 326 } |
| 327 |
| 328 bool AccountFetcherService::DeferFetchingAccountInfo( |
| 329 const std::string& account_id) { |
| 330 // There are two reasons for an account to be in |pending_user_info_fetches_|, |
| 331 // either they were fetched before network fetch was enabled or the |
| 332 // Id <-> Name mapping was required and unavailable for the account. |
| 333 bool defer_fetch = |
| 334 !network_fetches_enabled_ || |
| 335 (token_service_->GetDelegate()->NeedsAccountIdNameMap() && |
| 336 !account_tracker_service_->HasIdNameMappingForAccount(account_id)); |
| 337 if (defer_fetch) |
| 338 pending_user_info_fetches_.insert(account_id); |
| 339 return defer_fetch; |
| 340 } |
| 341 |
| 342 void AccountFetcherService::FetchPendingAccountInfo() { |
| 343 // Since further failures will be added back, we create a new copy of the |
| 344 // accounts list before processing it. |
| 345 std::set<std::string> accounts_to_process; |
| 346 pending_user_info_fetches_.swap(accounts_to_process); |
| 347 for (const std::string& account : accounts_to_process) { |
| 348 if (account_tracker_service_->IsTrackingAccount(account)) |
| 349 StartFetchingUserInfo(account); |
| 350 } |
| 351 UpdateChildInfo(); |
| 352 } |
| OLD | NEW |