Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Unified Diff: google_apis/gaia/account_tracker.cc

Issue 336253002: Add IdentityProvider-based AccountTracker to google_apis (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add pointer ownership comment Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « google_apis/gaia/account_tracker.h ('k') | google_apis/gaia/account_tracker_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: google_apis/gaia/account_tracker.cc
diff --git a/chrome/browser/extensions/api/identity/account_tracker.cc b/google_apis/gaia/account_tracker.cc
similarity index 60%
copy from chrome/browser/extensions/api/identity/account_tracker.cc
copy to google_apis/gaia/account_tracker.cc
index a93f505034a040ea673be1debff96b0d005881c4..9cfe6c9e27883a9dbb4413dea7320eee81fa9da6 100644
--- a/chrome/browser/extensions/api/identity/account_tracker.cc
+++ b/google_apis/gaia/account_tracker.cc
@@ -2,47 +2,33 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/extensions/api/identity/account_tracker.h"
+#include "google_apis/gaia/account_tracker.h"
#include "base/logging.h"
#include "base/stl_util.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
-#include "chrome/browser/signin/signin_manager_factory.h"
-#include "components/signin/core/browser/profile_oauth2_token_service.h"
-#include "components/signin/core/browser/signin_manager.h"
-#include "content/public/browser/notification_details.h"
-#include "extensions/browser/extension_system.h"
-
-namespace extensions {
-
-AccountTracker::AccountTracker(Profile* profile) : profile_(profile) {
- ProfileOAuth2TokenService* service =
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
- service->AddObserver(this);
- service->signin_error_controller()->AddProvider(this);
- SigninManagerFactory::GetForProfile(profile_)->AddObserver(this);
-}
+#include "net/url_request/url_request_context_getter.h"
-AccountTracker::~AccountTracker() {}
+namespace gaia {
-void AccountTracker::ReportAuthError(const std::string& account_id,
- const GoogleServiceAuthError& error) {
- account_errors_.insert(make_pair(account_id, error));
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
- signin_error_controller()->AuthStatusChanged();
- UpdateSignInState(account_id, false);
+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_);
- SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this);
- ProfileOAuth2TokenService* service =
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
- service->signin_error_controller()->RemoveProvider(this);
- service->RemoveObserver(this);
+ identity_provider_->GetTokenService()->RemoveObserver(this);
+ identity_provider_->RemoveObserver(this);
}
void AccountTracker::AddObserver(Observer* observer) {
@@ -54,7 +40,8 @@ void AccountTracker::RemoveObserver(Observer* observer) {
}
std::vector<AccountIds> AccountTracker::GetAccounts() const {
- const std::string primary_account_id = signin_manager_account_id();
+ const std::string active_account_id =
+ identity_provider_->GetActiveAccountId();
std::vector<AccountIds> accounts;
for (std::map<std::string, AccountState>::const_iterator it =
@@ -64,7 +51,7 @@ std::vector<AccountIds> AccountTracker::GetAccounts() const {
const AccountState& state = it->second;
bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
- if (it->first == primary_account_id) {
+ if (it->first == active_account_id) {
if (is_visible)
accounts.insert(accounts.begin(), state.ids);
else
@@ -77,27 +64,26 @@ std::vector<AccountIds> AccountTracker::GetAccounts() const {
return accounts;
}
-std::string AccountTracker::FindAccountKeyByGaiaId(const std::string& gaia_id) {
+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.account_key;
+ return state.ids;
}
}
- return std::string();
+ return AccountIds();
}
void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
- // Ignore refresh tokens if there is no primary account ID at all.
- if (signin_manager_account_id().empty())
+ // Ignore refresh tokens if there is no active account ID at all.
+ if (identity_provider_->GetActiveAccountId().empty())
return;
DVLOG(1) << "AVAILABLE " << account_id;
- ClearAuthError(account_id);
UpdateSignInState(account_id, true);
}
@@ -106,10 +92,11 @@ void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
UpdateSignInState(account_id, false);
}
-void AccountTracker::GoogleSigninSucceeded(const std::string& username,
- const std::string& password) {
+void AccountTracker::OnActiveAccountLogin() {
std::vector<std::string> accounts =
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->GetAccounts();
+ identity_provider_->GetTokenService()->GetAccounts();
+
+ DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
for (std::vector<std::string>::const_iterator it = accounts.begin();
it != accounts.end();
@@ -118,13 +105,9 @@ void AccountTracker::GoogleSigninSucceeded(const std::string& username,
}
}
-void AccountTracker::GoogleSignedOut(const std::string& username) {
- if (username == signin_manager_account_id() ||
- signin_manager_account_id().empty()) {
- StopTrackingAllAccounts();
- } else {
- StopTrackingAccount(username);
- }
+void AccountTracker::OnActiveAccountLogout() {
+ DVLOG(1) << "LOGOUT";
+ StopTrackingAllAccounts();
}
void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
@@ -144,11 +127,6 @@ void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
}
}
-const std::string AccountTracker::signin_manager_account_id() const {
- return SigninManagerFactory::GetForProfile(profile_)
- ->GetAuthenticatedAccountId();
-}
-
void AccountTracker::NotifyAccountAdded(const AccountState& account) {
DCHECK(!account.ids.gaia.empty());
FOR_EACH_OBSERVER(
@@ -168,13 +146,7 @@ void AccountTracker::NotifySignInChanged(const AccountState& account) {
OnAccountSignInChanged(account.ids, account.is_signed_in));
}
-void AccountTracker::ClearAuthError(const std::string& account_key) {
- account_errors_.erase(account_key);
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
- signin_error_controller()->AuthStatusChanged();
-}
-
-void AccountTracker::UpdateSignInState(const std::string& account_key,
+void AccountTracker::UpdateSignInState(const std::string account_key,
bool is_signed_in) {
StartTrackingAccount(account_key);
AccountState& account = accounts_[account_key];
@@ -189,7 +161,7 @@ void AccountTracker::UpdateSignInState(const std::string& account_key,
NotifySignInChanged(account);
}
-void AccountTracker::StartTrackingAccount(const std::string& account_key) {
+void AccountTracker::StartTrackingAccount(const std::string account_key) {
if (!ContainsKey(accounts_, account_key)) {
DVLOG(1) << "StartTracking " << account_key;
AccountState account_state;
@@ -200,7 +172,8 @@ void AccountTracker::StartTrackingAccount(const std::string& account_key) {
}
}
-void AccountTracker::StopTrackingAccount(const std::string& account_key) {
+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()) {
@@ -210,8 +183,6 @@ void AccountTracker::StopTrackingAccount(const std::string& account_key) {
accounts_.erase(account_key);
}
- ClearAuthError(account_key);
-
if (ContainsKey(user_info_requests_, account_key))
DeleteFetcher(user_info_requests_[account_key]);
}
@@ -221,13 +192,16 @@ void AccountTracker::StopTrackingAllAccounts() {
StopTrackingAccount(accounts_.begin()->first);
}
-void AccountTracker::StartFetchingUserInfo(const std::string& account_key) {
+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(profile_, this, account_key);
+ new AccountIdFetcher(identity_provider_->GetTokenService(),
+ request_context_getter_.get(),
+ this,
+ account_key);
user_info_requests_[account_key] = fetcher;
fetcher->Start();
}
@@ -254,32 +228,8 @@ void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
StopTrackingAccount(key);
}
-std::string AccountTracker::GetAccountId() const {
- if (account_errors_.size() == 0)
- return std::string();
- else
- return account_errors_.begin()->first;
-}
-
-std::string AccountTracker::GetUsername() const {
- std::string id = GetAccountId();
- if (!id.empty()) {
- std::map<std::string, AccountState>::const_iterator it =
- accounts_.find(id);
- if (it != accounts_.end())
- return it->second.ids.email;
- }
- return std::string();
-}
-
-GoogleServiceAuthError AccountTracker::GetAuthStatus() const {
- if (account_errors_.size() == 0)
- return GoogleServiceAuthError::AuthErrorNone();
- else
- return account_errors_.begin()->second;
-}
-
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]);
@@ -287,20 +237,22 @@ void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
delete fetcher;
}
-AccountIdFetcher::AccountIdFetcher(Profile* profile,
- AccountTracker* tracker,
- const std::string& account_key)
- : OAuth2TokenService::Consumer("extensions_account_tracker"),
- profile_(profile),
+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) {}
+ account_key_(account_key) {
+}
AccountIdFetcher::~AccountIdFetcher() {}
void AccountIdFetcher::Start() {
- ProfileOAuth2TokenService* service =
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
- login_token_request_ = service->StartRequest(
+ login_token_request_ = token_service_->StartRequest(
account_key_, OAuth2TokenService::ScopeSet(), this);
}
@@ -310,8 +262,7 @@ void AccountIdFetcher::OnGetTokenSuccess(
const base::Time& expiration_time) {
DCHECK_EQ(request, login_token_request_.get());
- gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(
- g_browser_process->system_request_context()));
+ gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
const int kMaxGetUserIdRetries = 3;
gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
@@ -339,4 +290,4 @@ void AccountIdFetcher::OnNetworkError(int response_code) {
tracker_->OnUserInfoFetchFailure(this);
}
-} // namespace extensions
+} // namespace gaia
« no previous file with comments | « google_apis/gaia/account_tracker.h ('k') | google_apis/gaia/account_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698