Commit 0d02c33d authored by courage@chromium.org's avatar courage@chromium.org

Add IdentityProvider-based AccountTracker to google_apis

This is a re-implementation of extensions::AccountTracker, built on
IdentityProvider instead of SigninManager and
ProfileOAuth2TokenService. Removing the dependency on chrome/browser
will allow GCM to use this code.

BUG=374988

Review URL: https://codereview.chromium.org/336253002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278476 0039d316-1c4b-4281-b951-d872f2087c98
parent 07d78a4e
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/gaia/account_tracker.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "net/url_request/url_request_context_getter.h"
namespace gaia {
AccountTracker::AccountTracker(
IdentityProvider* identity_provider,
net::URLRequestContextGetter* request_context_getter)
: identity_provider_(identity_provider),
request_context_getter_(request_context_getter),
shutdown_called_(false) {
identity_provider_->AddObserver(this);
identity_provider_->GetTokenService()->AddObserver(this);
}
AccountTracker::~AccountTracker() {
DCHECK(shutdown_called_);
}
void AccountTracker::Shutdown() {
shutdown_called_ = true;
STLDeleteValues(&user_info_requests_);
identity_provider_->GetTokenService()->RemoveObserver(this);
identity_provider_->RemoveObserver(this);
}
void AccountTracker::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void AccountTracker::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
std::vector<AccountIds> AccountTracker::GetAccounts() const {
const std::string active_account_id =
identity_provider_->GetActiveAccountId();
std::vector<AccountIds> accounts;
for (std::map<std::string, AccountState>::const_iterator it =
accounts_.begin();
it != accounts_.end();
++it) {
const AccountState& state = it->second;
bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
if (it->first == active_account_id) {
if (is_visible)
accounts.insert(accounts.begin(), state.ids);
else
return std::vector<AccountIds>();
} else if (is_visible) {
accounts.push_back(state.ids);
}
}
return accounts;
}
AccountIds AccountTracker::FindAccountIdsByGaiaId(const std::string& gaia_id) {
for (std::map<std::string, AccountState>::const_iterator it =
accounts_.begin();
it != accounts_.end();
++it) {
const AccountState& state = it->second;
if (state.ids.gaia == gaia_id) {
return state.ids;
}
}
return AccountIds();
}
void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
// Ignore refresh tokens if there is no active account ID at all.
if (identity_provider_->GetActiveAccountId().empty())
return;
DVLOG(1) << "AVAILABLE " << account_id;
UpdateSignInState(account_id, true);
}
void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
DVLOG(1) << "REVOKED " << account_id;
UpdateSignInState(account_id, false);
}
void AccountTracker::OnActiveAccountLogin() {
std::vector<std::string> accounts =
identity_provider_->GetTokenService()->GetAccounts();
DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
for (std::vector<std::string>::const_iterator it = accounts.begin();
it != accounts.end();
++it) {
OnRefreshTokenAvailable(*it);
}
}
void AccountTracker::OnActiveAccountLogout() {
DVLOG(1) << "LOGOUT";
StopTrackingAllAccounts();
}
void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
accounts_[ids.account_key].ids = ids;
accounts_[ids.account_key].is_signed_in = is_signed_in;
DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
<< is_signed_in;
if (VLOG_IS_ON(1)) {
for (std::map<std::string, AccountState>::const_iterator it =
accounts_.begin();
it != accounts_.end();
++it) {
DVLOG(1) << it->first << ":" << it->second.is_signed_in;
}
}
}
void AccountTracker::NotifyAccountAdded(const AccountState& account) {
DCHECK(!account.ids.gaia.empty());
FOR_EACH_OBSERVER(
Observer, observer_list_, OnAccountAdded(account.ids));
}
void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
DCHECK(!account.ids.gaia.empty());
FOR_EACH_OBSERVER(
Observer, observer_list_, OnAccountRemoved(account.ids));
}
void AccountTracker::NotifySignInChanged(const AccountState& account) {
DCHECK(!account.ids.gaia.empty());
FOR_EACH_OBSERVER(Observer,
observer_list_,
OnAccountSignInChanged(account.ids, account.is_signed_in));
}
void AccountTracker::UpdateSignInState(const std::string account_key,
bool is_signed_in) {
StartTrackingAccount(account_key);
AccountState& account = accounts_[account_key];
bool needs_gaia_id = account.ids.gaia.empty();
bool was_signed_in = account.is_signed_in;
account.is_signed_in = is_signed_in;
if (needs_gaia_id && is_signed_in)
StartFetchingUserInfo(account_key);
if (!needs_gaia_id && (was_signed_in != is_signed_in))
NotifySignInChanged(account);
}
void AccountTracker::StartTrackingAccount(const std::string account_key) {
if (!ContainsKey(accounts_, account_key)) {
DVLOG(1) << "StartTracking " << account_key;
AccountState account_state;
account_state.ids.account_key = account_key;
account_state.ids.email = account_key;
account_state.is_signed_in = false;
accounts_.insert(make_pair(account_key, account_state));
}
}
void AccountTracker::StopTrackingAccount(const std::string account_key) {
DVLOG(1) << "StopTracking " << account_key;
if (ContainsKey(accounts_, account_key)) {
AccountState& account = accounts_[account_key];
if (!account.ids.gaia.empty()) {
UpdateSignInState(account_key, false);
NotifyAccountRemoved(account);
}
accounts_.erase(account_key);
}
if (ContainsKey(user_info_requests_, account_key))
DeleteFetcher(user_info_requests_[account_key]);
}
void AccountTracker::StopTrackingAllAccounts() {
while (!accounts_.empty())
StopTrackingAccount(accounts_.begin()->first);
}
void AccountTracker::StartFetchingUserInfo(const std::string account_key) {
if (ContainsKey(user_info_requests_, account_key))
DeleteFetcher(user_info_requests_[account_key]);
DVLOG(1) << "StartFetching " << account_key;
AccountIdFetcher* fetcher =
new AccountIdFetcher(identity_provider_->GetTokenService(),
request_context_getter_.get(),
this,
account_key);
user_info_requests_[account_key] = fetcher;
fetcher->Start();
}
void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
const std::string& gaia_id) {
const std::string& account_key = fetcher->account_key();
DCHECK(ContainsKey(accounts_, account_key));
AccountState& account = accounts_[account_key];
account.ids.gaia = gaia_id;
NotifyAccountAdded(account);
if (account.is_signed_in)
NotifySignInChanged(account);
DeleteFetcher(fetcher);
}
void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
std::string key = fetcher->account_key();
DeleteFetcher(fetcher);
StopTrackingAccount(key);
}
void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
DVLOG(1) << "DeleteFetcher " << fetcher->account_key();
const std::string& account_key = fetcher->account_key();
DCHECK(ContainsKey(user_info_requests_, account_key));
DCHECK_EQ(fetcher, user_info_requests_[account_key]);
user_info_requests_.erase(account_key);
delete fetcher;
}
AccountIdFetcher::AccountIdFetcher(
OAuth2TokenService* token_service,
net::URLRequestContextGetter* request_context_getter,
AccountTracker* tracker,
const std::string& account_key)
: OAuth2TokenService::Consumer("gaia_account_tracker"),
token_service_(token_service),
request_context_getter_(request_context_getter),
tracker_(tracker),
account_key_(account_key) {
}
AccountIdFetcher::~AccountIdFetcher() {}
void AccountIdFetcher::Start() {
login_token_request_ = token_service_->StartRequest(
account_key_, OAuth2TokenService::ScopeSet(), this);
}
void AccountIdFetcher::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
DCHECK_EQ(request, login_token_request_.get());
gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
const int kMaxGetUserIdRetries = 3;
gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
}
void AccountIdFetcher::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
DCHECK_EQ(request, login_token_request_.get());
tracker_->OnUserInfoFetchFailure(this);
}
void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
tracker_->OnUserInfoFetchSuccess(this, gaia_id);
}
void AccountIdFetcher::OnOAuthError() {
LOG(ERROR) << "OnOAuthError";
tracker_->OnUserInfoFetchFailure(this);
}
void AccountIdFetcher::OnNetworkError(int response_code) {
LOG(ERROR) << "OnNetworkError " << response_code;
tracker_->OnUserInfoFetchFailure(this);
}
} // namespace gaia
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_
#define GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_
#include <map>
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "google_apis/gaia/gaia_oauth_client.h"
#include "google_apis/gaia/identity_provider.h"
#include "google_apis/gaia/oauth2_token_service.h"
class GoogleServiceAuthError;
namespace net {
class URLRequestContextGetter;
}
namespace gaia {
struct AccountIds {
std::string account_key; // The account ID used by OAuth2TokenService.
std::string gaia;
std::string email;
};
class AccountIdFetcher;
// The AccountTracker keeps track of what accounts exist on the
// profile and the state of their credentials. The tracker fetches the
// gaia ID of each account it knows about.
//
// The AccountTracker maintains these invariants:
// 1. Events are only fired after the gaia ID has been fetched.
// 2. Add/Remove and SignIn/SignOut pairs are always generated in order.
// 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove.
// 4. If there is no primary account, there are no other accounts.
class AccountTracker : public OAuth2TokenService::Observer,
public IdentityProvider::Observer {
public:
AccountTracker(IdentityProvider* identity_provider,
net::URLRequestContextGetter* request_context_getter);
virtual ~AccountTracker();
class Observer {
public:
virtual void OnAccountAdded(const AccountIds& ids) = 0;
virtual void OnAccountRemoved(const AccountIds& ids) = 0;
virtual void OnAccountSignInChanged(const AccountIds& ids,
bool is_signed_in) = 0;
};
void Shutdown();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Returns the list of accounts that are signed in, and for which gaia IDs
// have been fetched. The primary account for the profile will be first
// in the vector. Additional accounts will be in order of their gaia IDs.
std::vector<AccountIds> GetAccounts() const;
AccountIds FindAccountIdsByGaiaId(const std::string& gaia_id);
// OAuth2TokenService::Observer implementation.
virtual void OnRefreshTokenAvailable(const std::string& account_key) OVERRIDE;
virtual void OnRefreshTokenRevoked(const std::string& account_key) OVERRIDE;
void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
const std::string& gaia_id);
void OnUserInfoFetchFailure(AccountIdFetcher* fetcher);
// IdentityProvider::Observer implementation.
virtual void OnActiveAccountLogin() OVERRIDE;
virtual void OnActiveAccountLogout() OVERRIDE;
// Sets the state of an account. Does not fire notifications.
void SetAccountStateForTest(AccountIds ids, bool is_signed_in);
IdentityProvider* identity_provider() { return identity_provider_; }
private:
struct AccountState {
AccountIds ids;
bool is_signed_in;
};
void NotifyAccountAdded(const AccountState& account);
void NotifyAccountRemoved(const AccountState& account);
void NotifySignInChanged(const AccountState& account);
void UpdateSignInState(const std::string account_key, bool is_signed_in);
void StartTrackingAccount(const std::string account_key);
void StopTrackingAccount(const std::string account_key);
void StopTrackingAllAccounts();
void StartFetchingUserInfo(const std::string account_key);
void DeleteFetcher(AccountIdFetcher* fetcher);
IdentityProvider* identity_provider_; // Not owned.
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
std::map<std::string, AccountIdFetcher*> user_info_requests_;
std::map<std::string, AccountState> accounts_;
ObserverList<Observer> observer_list_;
bool shutdown_called_;
};
class AccountIdFetcher : public OAuth2TokenService::Consumer,
public gaia::GaiaOAuthClient::Delegate {
public:
AccountIdFetcher(OAuth2TokenService* token_service,
net::URLRequestContextGetter* request_context_getter,
AccountTracker* tracker,
const std::string& account_key);
virtual ~AccountIdFetcher();
const std::string& account_key() { return account_key_; }
void Start();
// OAuth2TokenService::Consumer implementation.
virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) OVERRIDE;
virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) OVERRIDE;
// gaia::GaiaOAuthClient::Delegate implementation.
virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE;
virtual void OnOAuthError() OVERRIDE;
virtual void OnNetworkError(int response_code) OVERRIDE;
private:
OAuth2TokenService* token_service_;
net::URLRequestContextGetter* request_context_getter_;
AccountTracker* tracker_;
const std::string account_key_;
scoped_ptr<OAuth2TokenService::Request> login_token_request_;
scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
};
} // namespace extensions
#endif // GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_
This diff is collapsed.
......@@ -4,11 +4,22 @@
#include "google_apis/gaia/fake_oauth2_token_service.h"
FakeOAuth2TokenService::FakeOAuth2TokenService() : request_context_(NULL) {}
FakeOAuth2TokenService::PendingRequest::PendingRequest() {
}
FakeOAuth2TokenService::PendingRequest::~PendingRequest() {
}
FakeOAuth2TokenService::FakeOAuth2TokenService() : request_context_(NULL) {
}
FakeOAuth2TokenService::~FakeOAuth2TokenService() {
}
std::vector<std::string> FakeOAuth2TokenService::GetAccounts() {
return std::vector<std::string>(account_ids_.begin(), account_ids_.end());
}
void FakeOAuth2TokenService::FetchOAuth2Token(
RequestImpl* request,
const std::string& account_id,
......@@ -16,6 +27,13 @@ void FakeOAuth2TokenService::FetchOAuth2Token(
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes) {
PendingRequest pending_request;
pending_request.account_id = account_id;
pending_request.client_id = client_id;
pending_request.client_secret = client_secret;
pending_request.scopes = scopes;
pending_request.request = request->AsWeakPtr();
pending_requests_.push_back(pending_request);
}
void FakeOAuth2TokenService::InvalidateOAuth2Token(
......@@ -36,8 +54,30 @@ bool FakeOAuth2TokenService::RefreshTokenIsAvailable(
void FakeOAuth2TokenService::AddAccount(const std::string& account_id) {
account_ids_.insert(account_id);
FireRefreshTokenAvailable(account_id);
}
void FakeOAuth2TokenService::RemoveAccount(const std::string& account_id) {
account_ids_.erase(account_id);
FireRefreshTokenRevoked(account_id);
}
void FakeOAuth2TokenService::IssueAllTokensForAccount(
const std::string& account_id,
const std::string& access_token,
const base::Time& expiration) {
// Walk the requests and notify the callbacks.
for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
it != pending_requests_.end(); ++it) {
if (it->request && (account_id == it->account_id)) {
it->request->InformConsumer(
GoogleServiceAuthError::AuthErrorNone(), access_token, expiration);
}
}
}
OAuth2AccessTokenFetcher* FakeOAuth2TokenService::CreateAccessTokenFetcher(
const std::string& account_id,
net::URLRequestContextGetter* getter,
......
......@@ -9,6 +9,7 @@
#include <string>
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "google_apis/gaia/oauth2_token_service.h"
namespace net {
......@@ -21,7 +22,15 @@ class FakeOAuth2TokenService : public OAuth2TokenService {
FakeOAuth2TokenService();
virtual ~FakeOAuth2TokenService();
virtual std::vector<std::string> GetAccounts() OVERRIDE;
void AddAccount(const std::string& account_id);
void RemoveAccount(const std::string& account_id);
// Helper routines to issue tokens for pending requests.
void IssueAllTokensForAccount(const std::string& account_id,
const std::string& access_token,
const base::Time& expiration);
void set_request_context(net::URLRequestContextGetter* request_context) {
request_context_ = request_context;
......@@ -45,6 +54,17 @@ class FakeOAuth2TokenService : public OAuth2TokenService {
OVERRIDE;
private:
struct PendingRequest {
PendingRequest();
~PendingRequest();
std::string account_id;
std::string client_id;
std::string client_secret;
ScopeSet scopes;
base::WeakPtr<RequestImpl> request;
};
// OAuth2TokenService overrides.
virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
......@@ -54,6 +74,8 @@ class FakeOAuth2TokenService : public OAuth2TokenService {
OAuth2AccessTokenConsumer* consumer) OVERRIDE;
std::set<std::string> account_ids_;
std::vector<PendingRequest> pending_requests_;
net::URLRequestContextGetter* request_context_; // weak
DISALLOW_COPY_AND_ASSIGN(FakeOAuth2TokenService);
......
......@@ -95,6 +95,8 @@
'drive/task_util.h',
'drive/time_util.cc',
'drive/time_util.h',
'gaia/account_tracker.cc',
'gaia/account_tracker.h',
'gaia/gaia_auth_consumer.cc',
'gaia/gaia_auth_consumer.h',
'gaia/gaia_auth_fetcher.cc',
......@@ -168,6 +170,7 @@
'drive/request_sender_unittest.cc',
'drive/request_util_unittest.cc',
'drive/time_util_unittest.cc',
'gaia/account_tracker_unittest.cc',
'gaia/gaia_auth_fetcher_unittest.cc',
'gaia/gaia_auth_util_unittest.cc',
'gaia/gaia_oauth_client_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment