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
3 // found in the LICENSE file.
5 #include "google_apis/gaia/account_tracker.h"
7 #include "base/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "base/stl_util.h"
11 #include "net/url_request/url_request_context_getter.h"
15 AccountTracker::AccountTracker(
16 IdentityProvider* identity_provider,
17 net::URLRequestContextGetter* request_context_getter)
18 : identity_provider_(identity_provider),
19 request_context_getter_(request_context_getter),
20 shutdown_called_(false) {
21 identity_provider_->AddObserver(this);
22 identity_provider_->GetTokenService()->AddObserver(this);
25 AccountTracker::~AccountTracker() {
26 DCHECK(shutdown_called_);
29 void AccountTracker::Shutdown() {
30 shutdown_called_ = true;
31 STLDeleteValues(&user_info_requests_);
32 identity_provider_->GetTokenService()->RemoveObserver(this);
33 identity_provider_->RemoveObserver(this);
36 bool AccountTracker::IsAllUserInfoFetched() const {
37 return user_info_requests_.empty();
40 void AccountTracker::AddObserver(Observer* observer) {
41 observer_list_.AddObserver(observer);
44 void AccountTracker::RemoveObserver(Observer* observer) {
45 observer_list_.RemoveObserver(observer);
48 std::vector<AccountIds> AccountTracker::GetAccounts() const {
49 const std::string active_account_id =
50 identity_provider_->GetActiveAccountId();
51 std::vector<AccountIds> accounts;
53 for (std::map<std::string, AccountState>::const_iterator it =
55 it != accounts_.end();
57 const AccountState& state = it->second;
58 bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
60 if (it->first == active_account_id) {
62 accounts.insert(accounts.begin(), state.ids);
64 return std::vector<AccountIds>();
66 } else if (is_visible) {
67 accounts.push_back(state.ids);
73 AccountIds AccountTracker::FindAccountIdsByGaiaId(const std::string& gaia_id) {
74 for (std::map<std::string, AccountState>::const_iterator it =
76 it != accounts_.end();
78 const AccountState& state = it->second;
79 if (state.ids.gaia == gaia_id) {
87 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
88 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
89 tracked_objects::ScopedTracker tracking_profile(
90 FROM_HERE_WITH_EXPLICIT_FUNCTION(
91 "422460 AccountTracker::OnRefreshTokenAvailable"));
93 TRACE_EVENT1("identity",
94 "AccountTracker::OnRefreshTokenAvailable",
98 // Ignore refresh tokens if there is no active account ID at all.
99 if (identity_provider_->GetActiveAccountId().empty())
102 DVLOG(1) << "AVAILABLE " << account_id;
103 UpdateSignInState(account_id, true);
106 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
107 TRACE_EVENT1("identity",
108 "AccountTracker::OnRefreshTokenRevoked",
112 DVLOG(1) << "REVOKED " << account_id;
113 UpdateSignInState(account_id, false);
116 void AccountTracker::OnActiveAccountLogin() {
117 TRACE_EVENT0("identity", "AccountTracker::OnActiveAccountLogin");
119 std::vector<std::string> accounts =
120 identity_provider_->GetTokenService()->GetAccounts();
122 DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
124 for (std::vector<std::string>::const_iterator it = accounts.begin();
125 it != accounts.end();
127 OnRefreshTokenAvailable(*it);
131 void AccountTracker::OnActiveAccountLogout() {
132 TRACE_EVENT0("identity", "AccountTracker::OnActiveAccountLogout");
133 DVLOG(1) << "LOGOUT";
134 StopTrackingAllAccounts();
137 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
138 accounts_[ids.account_key].ids = ids;
139 accounts_[ids.account_key].is_signed_in = is_signed_in;
141 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
145 for (std::map<std::string, AccountState>::const_iterator it =
147 it != accounts_.end();
149 DVLOG(1) << it->first << ":" << it->second.is_signed_in;
154 void AccountTracker::NotifyAccountAdded(const AccountState& account) {
155 DCHECK(!account.ids.gaia.empty());
157 Observer, observer_list_, OnAccountAdded(account.ids));
160 void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
161 DCHECK(!account.ids.gaia.empty());
163 Observer, observer_list_, OnAccountRemoved(account.ids));
166 void AccountTracker::NotifySignInChanged(const AccountState& account) {
167 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
168 tracked_objects::ScopedTracker tracking_profile(
169 FROM_HERE_WITH_EXPLICIT_FUNCTION(
170 "422460 AccountTracker::NotifySignInChanged"));
172 DCHECK(!account.ids.gaia.empty());
173 FOR_EACH_OBSERVER(Observer,
175 OnAccountSignInChanged(account.ids, account.is_signed_in));
178 void AccountTracker::UpdateSignInState(const std::string account_key,
180 tracked_objects::ScopedTracker tracking_profile(
181 FROM_HERE_WITH_EXPLICIT_FUNCTION(
182 "422460 AccountTracker::UpdateSignInState"));
184 StartTrackingAccount(account_key);
185 AccountState& account = accounts_[account_key];
186 bool needs_gaia_id = account.ids.gaia.empty();
187 bool was_signed_in = account.is_signed_in;
188 account.is_signed_in = is_signed_in;
190 if (needs_gaia_id && is_signed_in)
191 StartFetchingUserInfo(account_key);
193 if (!needs_gaia_id && (was_signed_in != is_signed_in))
194 NotifySignInChanged(account);
197 void AccountTracker::StartTrackingAccount(const std::string account_key) {
198 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
199 tracked_objects::ScopedTracker tracking_profile(
200 FROM_HERE_WITH_EXPLICIT_FUNCTION(
201 "422460 AccountTracker::StartTrackingAccount"));
203 if (!ContainsKey(accounts_, account_key)) {
204 DVLOG(1) << "StartTracking " << account_key;
205 AccountState account_state;
206 account_state.ids.account_key = account_key;
207 account_state.ids.email = account_key;
208 account_state.is_signed_in = false;
209 accounts_.insert(make_pair(account_key, account_state));
213 void AccountTracker::StopTrackingAccount(const std::string account_key) {
214 DVLOG(1) << "StopTracking " << account_key;
215 if (ContainsKey(accounts_, account_key)) {
216 AccountState& account = accounts_[account_key];
217 if (!account.ids.gaia.empty()) {
218 UpdateSignInState(account_key, false);
219 NotifyAccountRemoved(account);
221 accounts_.erase(account_key);
224 if (ContainsKey(user_info_requests_, account_key))
225 DeleteFetcher(user_info_requests_[account_key]);
228 void AccountTracker::StopTrackingAllAccounts() {
229 while (!accounts_.empty())
230 StopTrackingAccount(accounts_.begin()->first);
233 void AccountTracker::StartFetchingUserInfo(const std::string account_key) {
234 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
235 tracked_objects::ScopedTracker tracking_profile(
236 FROM_HERE_WITH_EXPLICIT_FUNCTION(
237 "422460 AccountTracker::StartFetchingUserInfo"));
239 if (ContainsKey(user_info_requests_, account_key)) {
240 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
241 tracked_objects::ScopedTracker tracking_profile1(
242 FROM_HERE_WITH_EXPLICIT_FUNCTION(
243 "422460 AccountTracker::StartFetchingUserInfo 1"));
245 DeleteFetcher(user_info_requests_[account_key]);
248 DVLOG(1) << "StartFetching " << account_key;
249 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
250 tracked_objects::ScopedTracker tracking_profile2(
251 FROM_HERE_WITH_EXPLICIT_FUNCTION(
252 "422460 AccountTracker::StartFetchingUserInfo 2"));
254 AccountIdFetcher* fetcher =
255 new AccountIdFetcher(identity_provider_->GetTokenService(),
256 request_context_getter_.get(),
259 user_info_requests_[account_key] = fetcher;
261 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
262 tracked_objects::ScopedTracker tracking_profile3(
263 FROM_HERE_WITH_EXPLICIT_FUNCTION(
264 "422460 AccountTracker::StartFetchingUserInfo 3"));
269 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
270 const std::string& gaia_id) {
271 const std::string& account_key = fetcher->account_key();
272 DCHECK(ContainsKey(accounts_, account_key));
273 AccountState& account = accounts_[account_key];
275 account.ids.gaia = gaia_id;
276 NotifyAccountAdded(account);
278 if (account.is_signed_in)
279 NotifySignInChanged(account);
281 DeleteFetcher(fetcher);
284 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
285 LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
286 std::string key = fetcher->account_key();
287 DeleteFetcher(fetcher);
288 StopTrackingAccount(key);
291 void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
292 DVLOG(1) << "DeleteFetcher " << fetcher->account_key();
293 const std::string& account_key = fetcher->account_key();
294 DCHECK(ContainsKey(user_info_requests_, account_key));
295 DCHECK_EQ(fetcher, user_info_requests_[account_key]);
296 user_info_requests_.erase(account_key);
300 AccountIdFetcher::AccountIdFetcher(
301 OAuth2TokenService* token_service,
302 net::URLRequestContextGetter* request_context_getter,
303 AccountTracker* tracker,
304 const std::string& account_key)
305 : OAuth2TokenService::Consumer("gaia_account_tracker"),
306 token_service_(token_service),
307 request_context_getter_(request_context_getter),
309 account_key_(account_key) {
310 TRACE_EVENT_ASYNC_BEGIN1(
311 "identity", "AccountIdFetcher", this, "account_key", account_key);
314 AccountIdFetcher::~AccountIdFetcher() {
315 TRACE_EVENT_ASYNC_END0("identity", "AccountIdFetcher", this);
318 void AccountIdFetcher::Start() {
319 OAuth2TokenService::ScopeSet scopes;
320 scopes.insert("https://www.googleapis.com/auth/userinfo.profile");
321 login_token_request_ = token_service_->StartRequest(
322 account_key_, scopes, this);
325 void AccountIdFetcher::OnGetTokenSuccess(
326 const OAuth2TokenService::Request* request,
327 const std::string& access_token,
328 const base::Time& expiration_time) {
329 TRACE_EVENT_ASYNC_STEP_PAST0(
330 "identity", "AccountIdFetcher", this, "OnGetTokenSuccess");
331 DCHECK_EQ(request, login_token_request_.get());
333 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
335 const int kMaxGetUserIdRetries = 3;
336 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
339 void AccountIdFetcher::OnGetTokenFailure(
340 const OAuth2TokenService::Request* request,
341 const GoogleServiceAuthError& error) {
342 TRACE_EVENT_ASYNC_STEP_PAST1("identity",
346 "google_service_auth_error",
348 LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
349 DCHECK_EQ(request, login_token_request_.get());
350 tracker_->OnUserInfoFetchFailure(this);
353 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
354 TRACE_EVENT_ASYNC_STEP_PAST1("identity",
357 "OnGetUserIdResponse",
360 tracker_->OnUserInfoFetchSuccess(this, gaia_id);
363 void AccountIdFetcher::OnOAuthError() {
364 TRACE_EVENT_ASYNC_STEP_PAST0(
365 "identity", "AccountIdFetcher", this, "OnOAuthError");
366 LOG(ERROR) << "OnOAuthError";
367 tracker_->OnUserInfoFetchFailure(this);
370 void AccountIdFetcher::OnNetworkError(int response_code) {
371 TRACE_EVENT_ASYNC_STEP_PAST1("identity",
377 LOG(ERROR) << "OnNetworkError " << response_code;
378 tracker_->OnUserInfoFetchFailure(this);