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

Cleanup: remove IdentityAccountTracker (replaced by gaia::AccountTracker)

This change replaces the IdentityAccountTracker with
gaia::AccountTracker, which mostly does the same thing. The one
functional consequence of this change is that the Identity API no
longer keeps track of login token errors on its own. Instead it
relies on the underlying token services to report errors to the
user.

BUG=364203

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283942 0039d316-1c4b-4281-b951-d872f2087c98
parent f4e0bd2c
// Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_
#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_
#include <map>
#include <string>
#include <vector>
#include "base/observer_list.h"
#include "components/signin/core/browser/signin_error_controller.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "google_apis/gaia/gaia_oauth_client.h"
#include "google_apis/gaia/oauth2_token_service.h"
class GoogleServiceAuthError;
class Profile;
namespace extensions {
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 SigninErrorController::AuthStatusProvider,
public SigninManagerBase::Observer {
public:
explicit AccountTracker(Profile* profile);
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 ReportAuthError(const std::string& account_key,
const GoogleServiceAuthError& error);
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;
std::string FindAccountKeyByGaiaId(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);
// AuthStatusProvider implementation.
virtual std::string GetAccountId() const OVERRIDE;
virtual std::string GetUsername() const OVERRIDE;
virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE;
// SigninManagerBase::Observer implementation.
virtual void GoogleSigninSucceeded(const std::string& username,
const std::string& password) OVERRIDE;
virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
// Sets the state of an account. Does not fire notifications.
void SetAccountStateForTest(AccountIds ids, bool is_signed_in);
private:
struct AccountState {
AccountIds ids;
bool is_signed_in;
};
const std::string signin_manager_account_id() const;
void NotifyAccountAdded(const AccountState& account);
void NotifyAccountRemoved(const AccountState& account);
void NotifySignInChanged(const AccountState& account);
void ClearAuthError(const std::string& account_key);
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);
Profile* profile_;
std::map<std::string, AccountIdFetcher*> user_info_requests_;
std::map<std::string, AccountState> accounts_;
std::map<std::string, GoogleServiceAuthError> account_errors_;
ObserverList<Observer> observer_list_;
};
class AccountIdFetcher : public OAuth2TokenService::Consumer,
public gaia::GaiaOAuthClient::Delegate {
public:
AccountIdFetcher(Profile* profile,
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:
Profile* profile_;
AccountTracker* tracker_;
const std::string account_key_;
scoped_ptr<OAuth2TokenService::Request> login_token_request_;
scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h" #include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "chrome/common/extensions/api/identity.h" #include "chrome/common/extensions/api/identity.h"
#include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
...@@ -130,7 +131,15 @@ const base::Time& IdentityTokenCacheValue::expiration_time() const { ...@@ -130,7 +131,15 @@ const base::Time& IdentityTokenCacheValue::expiration_time() const {
IdentityAPI::IdentityAPI(content::BrowserContext* context) IdentityAPI::IdentityAPI(content::BrowserContext* context)
: browser_context_(context), : browser_context_(context),
account_tracker_(Profile::FromBrowserContext(context)) { profile_identity_provider_(
SigninManagerFactory::GetForProfile(
Profile::FromBrowserContext(context)),
ProfileOAuth2TokenServiceFactory::GetForProfile(
Profile::FromBrowserContext(context)),
LoginUIServiceFactory::GetForProfile(
Profile::FromBrowserContext(context))),
account_tracker_(&profile_identity_provider_,
g_browser_process->system_request_context()) {
account_tracker_.AddObserver(this); account_tracker_.AddObserver(this);
} }
...@@ -173,11 +182,11 @@ const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { ...@@ -173,11 +182,11 @@ const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() {
std::vector<std::string> IdentityAPI::GetAccounts() const { std::vector<std::string> IdentityAPI::GetAccounts() const {
const std::string primary_account_id = GetPrimaryAccountId(browser_context_); const std::string primary_account_id = GetPrimaryAccountId(browser_context_);
const std::vector<AccountIds> ids = account_tracker_.GetAccounts(); const std::vector<gaia::AccountIds> ids = account_tracker_.GetAccounts();
std::vector<std::string> gaia_ids; std::vector<std::string> gaia_ids;
if (switches::IsExtensionsMultiAccount()) { if (switches::IsExtensionsMultiAccount()) {
for (std::vector<AccountIds>::const_iterator it = ids.begin(); for (std::vector<gaia::AccountIds>::const_iterator it = ids.begin();
it != ids.end(); it != ids.end();
++it) { ++it) {
gaia_ids.push_back(it->gaia); gaia_ids.push_back(it->gaia);
...@@ -190,16 +199,7 @@ std::vector<std::string> IdentityAPI::GetAccounts() const { ...@@ -190,16 +199,7 @@ std::vector<std::string> IdentityAPI::GetAccounts() const {
} }
std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) { std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) {
return account_tracker_.FindAccountKeyByGaiaId(gaia_id); return account_tracker_.FindAccountIdsByGaiaId(gaia_id).account_key;
}
void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) {
account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_),
error);
}
GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const {
return account_tracker_.GetAuthStatus();
} }
void IdentityAPI::Shutdown() { void IdentityAPI::Shutdown() {
...@@ -216,11 +216,13 @@ BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { ...@@ -216,11 +216,13 @@ BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() {
return g_factory.Pointer(); return g_factory.Pointer();
} }
void IdentityAPI::OnAccountAdded(const AccountIds& ids) {} void IdentityAPI::OnAccountAdded(const gaia::AccountIds& ids) {
}
void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {} void IdentityAPI::OnAccountRemoved(const gaia::AccountIds& ids) {
}
void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids, void IdentityAPI::OnAccountSignInChanged(const gaia::AccountIds& ids,
bool is_signed_in) { bool is_signed_in) {
api::identity::AccountInfo account_info; api::identity::AccountInfo account_info;
account_info.id = ids.gaia; account_info.id = ids.gaia;
...@@ -242,7 +244,8 @@ void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) { ...@@ -242,7 +244,8 @@ void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) {
shutdown_observer_list_.RemoveObserver(observer); shutdown_observer_list_.RemoveObserver(observer);
} }
void IdentityAPI::SetAccountStateForTest(AccountIds ids, bool is_signed_in) { void IdentityAPI::SetAccountStateForTest(gaia::AccountIds ids,
bool is_signed_in) {
account_tracker_.SetAccountStateForTest(ids, is_signed_in); account_tracker_.SetAccountStateForTest(ids, is_signed_in);
} }
...@@ -533,9 +536,7 @@ void IdentityGetAuthTokenFunction::OnMintTokenFailure( ...@@ -533,9 +536,7 @@ void IdentityGetAuthTokenFunction::OnMintTokenFailure(
case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
case GoogleServiceAuthError::ACCOUNT_DELETED: case GoogleServiceAuthError::ACCOUNT_DELETED:
case GoogleServiceAuthError::ACCOUNT_DISABLED: case GoogleServiceAuthError::ACCOUNT_DISABLED:
extensions::IdentityAPI::GetFactoryInstance() // TODO(courage): flush ticket and retry once
->Get(GetProfile())
->ReportAuthError(error);
if (should_prompt_for_signin_) { if (should_prompt_for_signin_) {
// Display a login prompt and try again (once). // Display a login prompt and try again (once).
StartSigninFlow(); StartSigninFlow();
......
...@@ -14,15 +14,15 @@ ...@@ -14,15 +14,15 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "chrome/browser/extensions/api/identity/account_tracker.h"
#include "chrome/browser/extensions/api/identity/extension_token_key.h" #include "chrome/browser/extensions/api/identity/extension_token_key.h"
#include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h"
#include "chrome/browser/extensions/api/identity/identity_mint_queue.h" #include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
#include "chrome/browser/extensions/api/identity/identity_signin_flow.h" #include "chrome/browser/extensions/api/identity/identity_signin_flow.h"
#include "chrome/browser/extensions/api/identity/web_auth_flow.h" #include "chrome/browser/extensions/api/identity/web_auth_flow.h"
#include "chrome/browser/extensions/chrome_extension_function.h" #include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/signin/signin_global_error.h" #include "chrome/browser/signin/profile_identity_provider.h"
#include "extensions/browser/browser_context_keyed_api_factory.h" #include "extensions/browser/browser_context_keyed_api_factory.h"
#include "google_apis/gaia/account_tracker.h"
#include "google_apis/gaia/oauth2_mint_token_flow.h" #include "google_apis/gaia/oauth2_mint_token_flow.h"
#include "google_apis/gaia/oauth2_token_service.h" #include "google_apis/gaia/oauth2_token_service.h"
...@@ -83,7 +83,7 @@ class IdentityTokenCacheValue { ...@@ -83,7 +83,7 @@ class IdentityTokenCacheValue {
}; };
class IdentityAPI : public BrowserContextKeyedAPI, class IdentityAPI : public BrowserContextKeyedAPI,
public AccountTracker::Observer { public gaia::AccountTracker::Observer {
public: public:
typedef std::map<ExtensionTokenKey, IdentityTokenCacheValue> CachedTokens; typedef std::map<ExtensionTokenKey, IdentityTokenCacheValue> CachedTokens;
...@@ -112,24 +112,20 @@ class IdentityAPI : public BrowserContextKeyedAPI, ...@@ -112,24 +112,20 @@ class IdentityAPI : public BrowserContextKeyedAPI,
std::vector<std::string> GetAccounts() const; std::vector<std::string> GetAccounts() const;
std::string FindAccountKeyByGaiaId(const std::string& gaia_id); std::string FindAccountKeyByGaiaId(const std::string& gaia_id);
// Global error reporting.
void ReportAuthError(const GoogleServiceAuthError& error);
GoogleServiceAuthError GetAuthStatusForTest() const;
// BrowserContextKeyedAPI implementation. // BrowserContextKeyedAPI implementation.
virtual void Shutdown() OVERRIDE; virtual void Shutdown() OVERRIDE;
static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance();
// AccountTracker::Observer implementation: // gaia::AccountTracker::Observer implementation:
virtual void OnAccountAdded(const AccountIds& ids) OVERRIDE; virtual void OnAccountAdded(const gaia::AccountIds& ids) OVERRIDE;
virtual void OnAccountRemoved(const AccountIds& ids) OVERRIDE; virtual void OnAccountRemoved(const gaia::AccountIds& ids) OVERRIDE;
virtual void OnAccountSignInChanged(const AccountIds& ids, virtual void OnAccountSignInChanged(const gaia::AccountIds& ids,
bool is_signed_in) OVERRIDE; bool is_signed_in) OVERRIDE;
void AddShutdownObserver(ShutdownObserver* observer); void AddShutdownObserver(ShutdownObserver* observer);
void RemoveShutdownObserver(ShutdownObserver* observer); void RemoveShutdownObserver(ShutdownObserver* observer);
void SetAccountStateForTest(AccountIds ids, bool is_signed_in); void SetAccountStateForTest(gaia::AccountIds ids, bool is_signed_in);
private: private:
friend class BrowserContextKeyedAPIFactory<IdentityAPI>; friend class BrowserContextKeyedAPIFactory<IdentityAPI>;
...@@ -141,7 +137,8 @@ class IdentityAPI : public BrowserContextKeyedAPI, ...@@ -141,7 +137,8 @@ class IdentityAPI : public BrowserContextKeyedAPI,
content::BrowserContext* browser_context_; content::BrowserContext* browser_context_;
IdentityMintRequestQueue mint_queue_; IdentityMintRequestQueue mint_queue_;
CachedTokens token_cache_; CachedTokens token_cache_;
AccountTracker account_tracker_; ProfileIdentityProvider profile_identity_provider_;
gaia::AccountTracker account_tracker_;
ObserverList<ShutdownObserver> shutdown_observer_list_; ObserverList<ShutdownObserver> shutdown_observer_list_;
}; };
......
...@@ -447,8 +447,8 @@ class MockQueuedMintRequest : public IdentityMintRequestQueue::Request { ...@@ -447,8 +447,8 @@ class MockQueuedMintRequest : public IdentityMintRequestQueue::Request {
MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType)); MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType));
}; };
AccountIds CreateIds(std::string email, std::string obfid) { gaia::AccountIds CreateIds(std::string email, std::string obfid) {
AccountIds ids; gaia::AccountIds ids;
ids.account_key = email; ids.account_key = email;
ids.email = email; ids.email = email;
ids.gaia = obfid; ids.gaia = obfid;
...@@ -462,7 +462,7 @@ class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest { ...@@ -462,7 +462,7 @@ class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest {
} }
protected: protected:
void SetAccountState(AccountIds ids, bool is_signed_in) { void SetAccountState(gaia::AccountIds ids, bool is_signed_in) {
IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
ids, is_signed_in); ids, is_signed_in);
} }
...@@ -707,7 +707,7 @@ class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest { ...@@ -707,7 +707,7 @@ class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest {
#endif #endif
} }
void SetAccountState(AccountIds ids, bool is_signed_in) { void SetAccountState(gaia::AccountIds ids, bool is_signed_in) {
IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
ids, is_signed_in); ids, is_signed_in);
} }
...@@ -889,9 +889,6 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ...@@ -889,9 +889,6 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
EXPECT_FALSE(func->login_ui_shown()); EXPECT_FALSE(func->login_ui_shown());
EXPECT_FALSE(func->scope_ui_shown()); EXPECT_FALSE(func->scope_ui_shown());
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
id_api()->GetAuthStatusForTest().state());
} }
IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
...@@ -907,9 +904,6 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ...@@ -907,9 +904,6 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
EXPECT_FALSE(func->login_ui_shown()); EXPECT_FALSE(func->login_ui_shown());
EXPECT_FALSE(func->scope_ui_shown()); EXPECT_FALSE(func->scope_ui_shown());
EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
id_api()->GetAuthStatusForTest());
} }
IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
......
...@@ -576,8 +576,6 @@ ...@@ -576,8 +576,6 @@
'browser/extensions/api/hotword_private/hotword_private_api.h', 'browser/extensions/api/hotword_private/hotword_private_api.h',
'browser/extensions/api/i18n/i18n_api.cc', 'browser/extensions/api/i18n/i18n_api.cc',
'browser/extensions/api/i18n/i18n_api.h', 'browser/extensions/api/i18n/i18n_api.h',
'browser/extensions/api/identity/account_tracker.cc',
'browser/extensions/api/identity/account_tracker.h',
'browser/extensions/api/identity/extension_token_key.cc', 'browser/extensions/api/identity/extension_token_key.cc',
'browser/extensions/api/identity/extension_token_key.h', 'browser/extensions/api/identity/extension_token_key.h',
'browser/extensions/api/identity/gaia_web_auth_flow.cc', 'browser/extensions/api/identity/gaia_web_auth_flow.cc',
......
...@@ -923,7 +923,6 @@ ...@@ -923,7 +923,6 @@
'browser/extensions/api/extension_action/extension_action_prefs_unittest.cc', 'browser/extensions/api/extension_action/extension_action_prefs_unittest.cc',
'browser/extensions/api/file_handlers/mime_util_unittest.cc', 'browser/extensions/api/file_handlers/mime_util_unittest.cc',
'browser/extensions/api/file_system/file_system_api_unittest.cc', 'browser/extensions/api/file_system/file_system_api_unittest.cc',
'browser/extensions/api/identity/account_tracker_unittest.cc',
'browser/extensions/api/identity/extension_token_key_unittest.cc', 'browser/extensions/api/identity/extension_token_key_unittest.cc',
'browser/extensions/api/identity/gaia_web_auth_flow_unittest.cc', 'browser/extensions/api/identity/gaia_web_auth_flow_unittest.cc',
'browser/extensions/api/identity/identity_mint_queue_unittest.cc', 'browser/extensions/api/identity/identity_mint_queue_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