16d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
26d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
36d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// found in the LICENSE file.
46d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
56d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "google_apis/gaia/account_tracker.h"
66d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/debug/trace_event.h"
86d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/logging.h"
96d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/stl_util.h"
106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)namespace gaia {
136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)AccountTracker::AccountTracker(
156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    IdentityProvider* identity_provider,
166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    net::URLRequestContextGetter* request_context_getter)
176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    : identity_provider_(identity_provider),
186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      request_context_getter_(request_context_getter),
196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      shutdown_called_(false) {
206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  identity_provider_->AddObserver(this);
216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  identity_provider_->GetTokenService()->AddObserver(this);
226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)AccountTracker::~AccountTracker() {
256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(shutdown_called_);
266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::Shutdown() {
296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  shutdown_called_ = true;
306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  STLDeleteValues(&user_info_requests_);
316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  identity_provider_->GetTokenService()->RemoveObserver(this);
326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  identity_provider_->RemoveObserver(this);
336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool AccountTracker::IsAllUserInfoFetched() const {
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return user_info_requests_.empty();
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::AddObserver(Observer* observer) {
406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  observer_list_.AddObserver(observer);
416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::RemoveObserver(Observer* observer) {
446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  observer_list_.RemoveObserver(observer);
456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)std::vector<AccountIds> AccountTracker::GetAccounts() const {
486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const std::string active_account_id =
496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      identity_provider_->GetActiveAccountId();
506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  std::vector<AccountIds> accounts;
516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  for (std::map<std::string, AccountState>::const_iterator it =
536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)           accounts_.begin();
546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       it != accounts_.end();
556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       ++it) {
566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const AccountState& state = it->second;
576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    if (it->first == active_account_id) {
606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      if (is_visible)
616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        accounts.insert(accounts.begin(), state.ids);
626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      else
636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        return std::vector<AccountIds>();
646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    } else if (is_visible) {
666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      accounts.push_back(state.ids);
676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    }
686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return accounts;
706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)AccountIds AccountTracker::FindAccountIdsByGaiaId(const std::string& gaia_id) {
736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  for (std::map<std::string, AccountState>::const_iterator it =
746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)           accounts_.begin();
756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       it != accounts_.end();
766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       ++it) {
776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const AccountState& state = it->second;
786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    if (state.ids.gaia == gaia_id) {
796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      return state.ids;
806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    }
816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return AccountIds();
846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT1("identity",
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               "AccountTracker::OnRefreshTokenAvailable",
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               "account_key",
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               account_id);
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Ignore refresh tokens if there is no active account ID at all.
936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (identity_provider_->GetActiveAccountId().empty())
946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return;
956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "AVAILABLE " << account_id;
976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  UpdateSignInState(account_id, true);
986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT1("identity",
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               "AccountTracker::OnRefreshTokenRevoked",
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               "account_key",
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               account_id);
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "REVOKED " << account_id;
1076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  UpdateSignInState(account_id, false);
1086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::OnActiveAccountLogin() {
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT0("identity", "AccountTracker::OnActiveAccountLogin");
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  std::vector<std::string> accounts =
1146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      identity_provider_->GetTokenService()->GetAccounts();
1156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
1176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  for (std::vector<std::string>::const_iterator it = accounts.begin();
1196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       it != accounts.end();
1206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       ++it) {
1216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    OnRefreshTokenAvailable(*it);
1226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
1236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::OnActiveAccountLogout() {
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT0("identity", "AccountTracker::OnActiveAccountLogout");
1276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "LOGOUT";
1286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  StopTrackingAllAccounts();
1296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
1326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  accounts_[ids.account_key].ids = ids;
1336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  accounts_[ids.account_key].is_signed_in = is_signed_in;
1346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
1366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)           << is_signed_in;
1376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (VLOG_IS_ON(1)) {
1396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    for (std::map<std::string, AccountState>::const_iterator it =
1406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)             accounts_.begin();
1416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)         it != accounts_.end();
1426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)         ++it) {
1436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      DVLOG(1) << it->first << ":" << it->second.is_signed_in;
1446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    }
1456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
1466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::NotifyAccountAdded(const AccountState& account) {
1496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(!account.ids.gaia.empty());
1506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  FOR_EACH_OBSERVER(
1516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      Observer, observer_list_, OnAccountAdded(account.ids));
1526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
1556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(!account.ids.gaia.empty());
1566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  FOR_EACH_OBSERVER(
1576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      Observer, observer_list_, OnAccountRemoved(account.ids));
1586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::NotifySignInChanged(const AccountState& account) {
1616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(!account.ids.gaia.empty());
1626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  FOR_EACH_OBSERVER(Observer,
1636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                    observer_list_,
1646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                    OnAccountSignInChanged(account.ids, account.is_signed_in));
1656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::UpdateSignInState(const std::string account_key,
1686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                                       bool is_signed_in) {
1696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  StartTrackingAccount(account_key);
1706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  AccountState& account = accounts_[account_key];
1716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool needs_gaia_id = account.ids.gaia.empty();
1726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool was_signed_in = account.is_signed_in;
1736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  account.is_signed_in = is_signed_in;
1746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (needs_gaia_id && is_signed_in)
1766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    StartFetchingUserInfo(account_key);
1776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (!needs_gaia_id && (was_signed_in != is_signed_in))
1796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    NotifySignInChanged(account);
1806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::StartTrackingAccount(const std::string account_key) {
1836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (!ContainsKey(accounts_, account_key)) {
1846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    DVLOG(1) << "StartTracking " << account_key;
1856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    AccountState account_state;
1866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    account_state.ids.account_key = account_key;
1876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    account_state.ids.email = account_key;
1886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    account_state.is_signed_in = false;
1896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    accounts_.insert(make_pair(account_key, account_state));
1906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
1916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
1926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::StopTrackingAccount(const std::string account_key) {
1946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "StopTracking " << account_key;
1956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (ContainsKey(accounts_, account_key)) {
1966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    AccountState& account = accounts_[account_key];
1976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    if (!account.ids.gaia.empty()) {
1986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      UpdateSignInState(account_key, false);
1996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      NotifyAccountRemoved(account);
2006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    }
2016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    accounts_.erase(account_key);
2026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
2036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (ContainsKey(user_info_requests_, account_key))
2056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    DeleteFetcher(user_info_requests_[account_key]);
2066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::StopTrackingAllAccounts() {
2096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  while (!accounts_.empty())
2106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    StopTrackingAccount(accounts_.begin()->first);
2116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::StartFetchingUserInfo(const std::string account_key) {
2146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (ContainsKey(user_info_requests_, account_key))
2156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    DeleteFetcher(user_info_requests_[account_key]);
2166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "StartFetching " << account_key;
2186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  AccountIdFetcher* fetcher =
2196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      new AccountIdFetcher(identity_provider_->GetTokenService(),
2206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                           request_context_getter_.get(),
2216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                           this,
2226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                           account_key);
2236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  user_info_requests_[account_key] = fetcher;
2246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  fetcher->Start();
2256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
2286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                                            const std::string& gaia_id) {
2296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const std::string& account_key = fetcher->account_key();
2306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(ContainsKey(accounts_, account_key));
2316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  AccountState& account = accounts_[account_key];
2326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  account.ids.gaia = gaia_id;
2346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  NotifyAccountAdded(account);
2356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (account.is_signed_in)
2376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    NotifySignInChanged(account);
2386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DeleteFetcher(fetcher);
2406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
2436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
2446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  std::string key = fetcher->account_key();
2456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DeleteFetcher(fetcher);
2466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  StopTrackingAccount(key);
2476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
2506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DVLOG(1) << "DeleteFetcher " << fetcher->account_key();
2516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const std::string& account_key = fetcher->account_key();
2526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(ContainsKey(user_info_requests_, account_key));
2536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK_EQ(fetcher, user_info_requests_[account_key]);
2546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  user_info_requests_.erase(account_key);
2556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  delete fetcher;
2566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)AccountIdFetcher::AccountIdFetcher(
2596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    OAuth2TokenService* token_service,
2606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    net::URLRequestContextGetter* request_context_getter,
2616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    AccountTracker* tracker,
2626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const std::string& account_key)
2636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    : OAuth2TokenService::Consumer("gaia_account_tracker"),
2646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      token_service_(token_service),
2656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      request_context_getter_(request_context_getter),
2666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      tracker_(tracker),
2676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      account_key_(account_key) {
2685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_BEGIN1(
2695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "identity", "AccountIdFetcher", this, "account_key", account_key);
2706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)AccountIdFetcher::~AccountIdFetcher() {
2735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_END0("identity", "AccountIdFetcher", this);
2745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountIdFetcher::Start() {
2776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  OAuth2TokenService::ScopeSet scopes;
2786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scopes.insert("https://www.googleapis.com/auth/userinfo.profile");
2796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  login_token_request_ = token_service_->StartRequest(
2806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      account_key_, scopes, this);
2816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountIdFetcher::OnGetTokenSuccess(
2846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const OAuth2TokenService::Request* request,
2856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const std::string& access_token,
2866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const base::Time& expiration_time) {
2875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_STEP_PAST0(
2885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "identity", "AccountIdFetcher", this, "OnGetTokenSuccess");
2896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK_EQ(request, login_token_request_.get());
2906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
2926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const int kMaxGetUserIdRetries = 3;
2946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
2956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
2966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountIdFetcher::OnGetTokenFailure(
2986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const OAuth2TokenService::Request* request,
2996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const GoogleServiceAuthError& error) {
3005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_STEP_PAST1("identity",
3015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "AccountIdFetcher",
3025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               this,
3035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "OnGetTokenFailure",
3045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "google_service_auth_error",
3055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               error.ToString());
3066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
3076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK_EQ(request, login_token_request_.get());
3086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  tracker_->OnUserInfoFetchFailure(this);
3096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
3106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
3116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
3125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_STEP_PAST1("identity",
3135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "AccountIdFetcher",
3145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               this,
3155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "OnGetUserIdResponse",
3165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "gaia_id",
3175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               gaia_id);
3186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  tracker_->OnUserInfoFetchSuccess(this, gaia_id);
3196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
3206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
3216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountIdFetcher::OnOAuthError() {
3225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_STEP_PAST0(
3235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "identity", "AccountIdFetcher", this, "OnOAuthError");
3246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  LOG(ERROR) << "OnOAuthError";
3256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  tracker_->OnUserInfoFetchFailure(this);
3266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
3276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
3286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void AccountIdFetcher::OnNetworkError(int response_code) {
3295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TRACE_EVENT_ASYNC_STEP_PAST1("identity",
3305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "AccountIdFetcher",
3315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               this,
3325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "OnNetworkError",
3335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               "response_code",
3345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               response_code);
3356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  LOG(ERROR) << "OnNetworkError " << response_code;
3366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  tracker_->OnUserInfoFetchFailure(this);
3376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
3386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
3396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}  // namespace gaia
340