| Index: components/signin/core/browser/account_reconcilor.cc
|
| diff --git a/chrome/browser/signin/account_reconcilor.cc b/components/signin/core/browser/account_reconcilor.cc
|
| similarity index 80%
|
| rename from chrome/browser/signin/account_reconcilor.cc
|
| rename to components/signin/core/browser/account_reconcilor.cc
|
| index 59a6d995bf5605155658b6d4994e1d81e4291a4a..7c6152932a10b0e9e7a5ded75d80192e1d26d289 100644
|
| --- a/chrome/browser/signin/account_reconcilor.cc
|
| +++ b/components/signin/core/browser/account_reconcilor.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "chrome/browser/signin/account_reconcilor.h"
|
| +#include "components/signin/core/browser/account_reconcilor.h"
|
|
|
| #include <algorithm>
|
|
|
| @@ -13,12 +13,9 @@
|
| #include "base/message_loop/message_loop_proxy.h"
|
| #include "base/strings/string_number_conversions.h"
|
| #include "base/time/time.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 "chrome/browser/signin/signin_oauth_helper.h"
|
| #include "components/signin/core/browser/profile_oauth2_token_service.h"
|
| #include "components/signin/core/browser/signin_client.h"
|
| +#include "components/signin/core/browser/signin_oauth_helper.h"
|
| #include "google_apis/gaia/gaia_auth_fetcher.h"
|
| #include "google_apis/gaia/gaia_auth_util.h"
|
| #include "google_apis/gaia/gaia_constants.h"
|
| @@ -59,8 +56,9 @@ AccountReconcilor::RefreshTokenFetcher::RefreshTokenFetcher(
|
| AccountReconcilor* reconcilor,
|
| const std::string& account_id,
|
| int session_index)
|
| - : SigninOAuthHelper(reconcilor->profile()->GetRequestContext(),
|
| - base::IntToString(session_index), this),
|
| + : SigninOAuthHelper(reconcilor->client()->GetURLRequestContext(),
|
| + base::IntToString(session_index),
|
| + this),
|
| reconcilor_(reconcilor),
|
| account_id_(account_id),
|
| session_index_(session_index) {
|
| @@ -69,12 +67,11 @@ AccountReconcilor::RefreshTokenFetcher::RefreshTokenFetcher(
|
| }
|
|
|
| void AccountReconcilor::RefreshTokenFetcher::OnSigninOAuthInformationAvailable(
|
| - const std::string& email,
|
| - const std::string& display_email,
|
| - const std::string& refresh_token) {
|
| + const std::string& email,
|
| + const std::string& display_email,
|
| + const std::string& refresh_token) {
|
| VLOG(1) << "RefreshTokenFetcher::OnSigninOAuthInformationAvailable:"
|
| - << " account=" << account_id_
|
| - << " email=" << email
|
| + << " account=" << account_id_ << " email=" << email
|
| << " displayEmail=" << display_email;
|
|
|
| // TODO(rogerta): because of the problem with email vs displayEmail and
|
| @@ -87,12 +84,10 @@ void AccountReconcilor::RefreshTokenFetcher::OnSigninOAuthInformationAvailable(
|
| void AccountReconcilor::RefreshTokenFetcher::OnSigninOAuthInformationFailure(
|
| const GoogleServiceAuthError& error) {
|
| VLOG(1) << "RefreshTokenFetcher::OnSigninOAuthInformationFailure:"
|
| - << " account=" << account_id_
|
| - << " session_index=" << session_index_;
|
| + << " account=" << account_id_ << " session_index=" << session_index_;
|
| reconcilor_->HandleRefreshTokenFetched(account_id_, std::string());
|
| }
|
|
|
| -
|
| bool AccountReconcilor::EmailLessFunc::operator()(const std::string& s1,
|
| const std::string& s2) const {
|
| return gaia::CanonicalizeEmail(s1) < gaia::CanonicalizeEmail(s2);
|
| @@ -128,7 +123,7 @@ AccountReconcilor::UserIdFetcher::UserIdFetcher(AccountReconcilor* reconcilor,
|
| : reconcilor_(reconcilor),
|
| account_id_(account_id),
|
| access_token_(access_token),
|
| - gaia_auth_client_(reconcilor_->profile()->GetRequestContext()) {
|
| + gaia_auth_client_(reconcilor_->client()->GetURLRequestContext()) {
|
| DCHECK(reconcilor_);
|
| DCHECK(!account_id_.empty());
|
|
|
| @@ -155,9 +150,8 @@ void AccountReconcilor::UserIdFetcher::OnOAuthError() {
|
| VLOG(1) << "AccountReconcilor::OnOAuthError: " << account_id_;
|
|
|
| // Invalidate the access token to force a refetch next time.
|
| - ProfileOAuth2TokenService* token_service =
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(reconcilor_->profile());
|
| - token_service->InvalidateToken(account_id_, GetScopes(), access_token_);
|
| + reconcilor_->token_service()->InvalidateToken(
|
| + account_id_, GetScopes(), access_token_);
|
|
|
| // HandleFailedAccountIdCheck() may delete |this|, so call it last.
|
| reconcilor_->HandleFailedAccountIdCheck(account_id_);
|
| @@ -173,14 +167,16 @@ void AccountReconcilor::UserIdFetcher::OnNetworkError(int response_code) {
|
| reconcilor_->HandleFailedAccountIdCheck(account_id_);
|
| }
|
|
|
| -AccountReconcilor::AccountReconcilor(Profile* profile, SigninClient* client)
|
| +AccountReconcilor::AccountReconcilor(ProfileOAuth2TokenService* token_service,
|
| + SigninManager* signin_manager,
|
| + SigninClient* client)
|
| : OAuth2TokenService::Consumer("account_reconcilor"),
|
| - profile_(profile),
|
| + token_service_(token_service),
|
| + signin_manager_(signin_manager),
|
| client_(client),
|
| - merge_session_helper_(
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
|
| - profile->GetRequestContext(),
|
| - this),
|
| + merge_session_helper_(token_service_,
|
| + client->GetURLRequestContext(),
|
| + this),
|
| registered_with_token_service_(false),
|
| is_reconcile_started_(false),
|
| are_gaia_accounts_set_(false),
|
| @@ -202,18 +198,16 @@ void AccountReconcilor::Initialize(bool start_reconcile_if_tokens_available) {
|
| VLOG(1) << "AccountReconcilor::Initialize";
|
| RegisterWithSigninManager();
|
|
|
| - // If this profile is not connected, the reconcilor should do nothing but
|
| - // wait for the connection.
|
| - if (IsProfileConnected()) {
|
| + // If this user is not signed in, the reconcilor should do nothing but
|
| + // wait for signin.
|
| + if (IsUserSignedIn()) {
|
| RegisterForCookieChanges();
|
| RegisterWithTokenService();
|
| StartPeriodicReconciliation();
|
|
|
| // Start a reconcile if the tokens are already loaded.
|
| - ProfileOAuth2TokenService* token_service =
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
|
| if (start_reconcile_if_tokens_available &&
|
| - token_service->GetAccounts().size() > 0) {
|
| + token_service_->GetAccounts().size() > 0) {
|
| StartReconcile();
|
| }
|
| }
|
| @@ -251,7 +245,7 @@ void AccountReconcilor::DeleteFetchers() {
|
|
|
| bool AccountReconcilor::AreAllRefreshTokensChecked() const {
|
| return chrome_accounts_.size() ==
|
| - (valid_chrome_accounts_.size() + invalid_chrome_accounts_.size());
|
| + (valid_chrome_accounts_.size() + invalid_chrome_accounts_.size());
|
| }
|
|
|
| void AccountReconcilor::RegisterForCookieChanges() {
|
| @@ -264,15 +258,11 @@ void AccountReconcilor::UnregisterForCookieChanges() {
|
| }
|
|
|
| void AccountReconcilor::RegisterWithSigninManager() {
|
| - SigninManagerBase* signin_manager =
|
| - SigninManagerFactory::GetForProfile(profile_);
|
| - signin_manager->AddObserver(this);
|
| + signin_manager_->AddObserver(this);
|
| }
|
|
|
| void AccountReconcilor::UnregisterWithSigninManager() {
|
| - SigninManagerBase* signin_manager =
|
| - SigninManagerFactory::GetForProfile(profile_);
|
| - signin_manager->RemoveObserver(this);
|
| + signin_manager_->RemoveObserver(this);
|
| }
|
|
|
| void AccountReconcilor::RegisterWithTokenService() {
|
| @@ -283,9 +273,7 @@ void AccountReconcilor::RegisterWithTokenService() {
|
| if (registered_with_token_service_)
|
| return;
|
|
|
| - ProfileOAuth2TokenService* token_service =
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
|
| - token_service->AddObserver(this);
|
| + token_service_->AddObserver(this);
|
| registered_with_token_service_ = true;
|
| }
|
|
|
| @@ -293,25 +281,21 @@ void AccountReconcilor::UnregisterWithTokenService() {
|
| if (!registered_with_token_service_)
|
| return;
|
|
|
| - ProfileOAuth2TokenService* token_service =
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
|
| - token_service->RemoveObserver(this);
|
| + token_service_->RemoveObserver(this);
|
| registered_with_token_service_ = false;
|
| }
|
|
|
| -bool AccountReconcilor::IsProfileConnected() {
|
| - return !SigninManagerFactory::GetForProfile(profile_)->
|
| - GetAuthenticatedUsername().empty();
|
| +bool AccountReconcilor::IsUserSignedIn() {
|
| + return !signin_manager_->GetAuthenticatedUsername().empty();
|
| }
|
|
|
| void AccountReconcilor::StartPeriodicReconciliation() {
|
| VLOG(1) << "AccountReconcilor::StartPeriodicReconciliation";
|
| // TODO(rogerta): pick appropriate thread and timeout value.
|
| - reconciliation_timer_.Start(
|
| - FROM_HERE,
|
| - base::TimeDelta::FromSeconds(300),
|
| - this,
|
| - &AccountReconcilor::PeriodicReconciliation);
|
| + reconciliation_timer_.Start(FROM_HERE,
|
| + base::TimeDelta::FromSeconds(300),
|
| + this,
|
| + &AccountReconcilor::PeriodicReconciliation);
|
| }
|
|
|
| void AccountReconcilor::StopPeriodicReconciliation() {
|
| @@ -345,8 +329,8 @@ void AccountReconcilor::OnRefreshTokenRevoked(const std::string& account_id) {
|
|
|
| void AccountReconcilor::OnRefreshTokensLoaded() {}
|
|
|
| -void AccountReconcilor::GoogleSigninSucceeded(
|
| - const std::string& username, const std::string& password) {
|
| +void AccountReconcilor::GoogleSigninSucceeded(const std::string& username,
|
| + const std::string& password) {
|
| VLOG(1) << "AccountReconcilor::GoogleSigninSucceeded: signed in";
|
| RegisterForCookieChanges();
|
| RegisterWithTokenService();
|
| @@ -367,10 +351,9 @@ void AccountReconcilor::PerformMergeAction(const std::string& account_id) {
|
|
|
| void AccountReconcilor::StartRemoveAction(const std::string& account_id) {
|
| VLOG(1) << "AccountReconcilor::StartRemoveAction: " << account_id;
|
| - GetAccountsFromCookie(
|
| - base::Bind(&AccountReconcilor::FinishRemoveAction,
|
| - base::Unretained(this),
|
| - account_id));
|
| + GetAccountsFromCookie(base::Bind(&AccountReconcilor::FinishRemoveAction,
|
| + base::Unretained(this),
|
| + account_id));
|
| }
|
|
|
| void AccountReconcilor::FinishRemoveAction(
|
| @@ -378,13 +361,14 @@ void AccountReconcilor::FinishRemoveAction(
|
| const GoogleServiceAuthError& error,
|
| const std::vector<std::pair<std::string, bool> >& accounts) {
|
| VLOG(1) << "AccountReconcilor::FinishRemoveAction:"
|
| - << " account=" << account_id
|
| - << " error=" << error.ToString();
|
| + << " account=" << account_id << " error=" << error.ToString();
|
| if (error.state() == GoogleServiceAuthError::NONE) {
|
| AbortReconcile();
|
| std::vector<std::string> accounts_only;
|
| for (std::vector<std::pair<std::string, bool> >::const_iterator i =
|
| - accounts.begin(); i != accounts.end(); ++i) {
|
| + accounts.begin();
|
| + i != accounts.end();
|
| + ++i) {
|
| accounts_only.push_back(i->first);
|
| }
|
| merge_session_helper_.LogOut(account_id, accounts_only);
|
| @@ -392,12 +376,10 @@ void AccountReconcilor::FinishRemoveAction(
|
| // Wait for the next ReconcileAction if there is an error.
|
| }
|
|
|
| -void AccountReconcilor::PerformAddToChromeAction(
|
| - const std::string& account_id,
|
| - int session_index) {
|
| +void AccountReconcilor::PerformAddToChromeAction(const std::string& account_id,
|
| + int session_index) {
|
| VLOG(1) << "AccountReconcilor::PerformAddToChromeAction:"
|
| - << " account=" << account_id
|
| - << " session_index=" << session_index;
|
| + << " account=" << account_id << " session_index=" << session_index;
|
|
|
| #if !defined(OS_ANDROID) && !defined(OS_IOS)
|
| refresh_token_fetchers_.push_back(
|
| @@ -411,7 +393,7 @@ void AccountReconcilor::PerformLogoutAllAccountsAction() {
|
| }
|
|
|
| void AccountReconcilor::StartReconcile() {
|
| - if (!IsProfileConnected() || is_reconcile_started_)
|
| + if (!IsUserSignedIn() || is_reconcile_started_)
|
| return;
|
|
|
| is_reconcile_started_ = true;
|
| @@ -421,7 +403,7 @@ void AccountReconcilor::StartReconcile() {
|
| gaia_accounts_.clear();
|
| GetAccountsFromCookie(base::Bind(
|
| &AccountReconcilor::ContinueReconcileActionAfterGetGaiaAccounts,
|
| - base::Unretained(this)));
|
| + base::Unretained(this)));
|
|
|
| // Reset state for validating oauth2 tokens.
|
| primary_account_.clear();
|
| @@ -439,8 +421,8 @@ void AccountReconcilor::GetAccountsFromCookie(
|
| get_gaia_accounts_callbacks_.push_back(callback);
|
| if (!gaia_fetcher_) {
|
| // There is no list account request in flight.
|
| - gaia_fetcher_.reset(new GaiaAuthFetcher(this, GaiaConstants::kChromeSource,
|
| - profile_->GetRequestContext()));
|
| + gaia_fetcher_.reset(new GaiaAuthFetcher(
|
| + this, GaiaConstants::kChromeSource, client_->GetURLRequestContext()));
|
| gaia_fetcher_->StartListAccounts();
|
| }
|
| }
|
| @@ -464,10 +446,10 @@ void AccountReconcilor::OnListAccountsSuccess(const std::string& data) {
|
| // There must be at least one callback waiting for result.
|
| DCHECK(!get_gaia_accounts_callbacks_.empty());
|
|
|
| - GoogleServiceAuthError error = !valid_json
|
| - ? GoogleServiceAuthError(
|
| - GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE)
|
| - : GoogleServiceAuthError::AuthErrorNone();
|
| + GoogleServiceAuthError error =
|
| + !valid_json ? GoogleServiceAuthError(
|
| + GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE)
|
| + : GoogleServiceAuthError::AuthErrorNone();
|
| get_gaia_accounts_callbacks_.front().Run(error, gaia_accounts);
|
| get_gaia_accounts_callbacks_.pop_front();
|
|
|
| @@ -491,8 +473,8 @@ void AccountReconcilor::OnListAccountsFailure(
|
|
|
| void AccountReconcilor::MayBeDoNextListAccounts() {
|
| if (!get_gaia_accounts_callbacks_.empty()) {
|
| - gaia_fetcher_.reset(new GaiaAuthFetcher(this, GaiaConstants::kChromeSource,
|
| - profile_->GetRequestContext()));
|
| + gaia_fetcher_.reset(new GaiaAuthFetcher(
|
| + this, GaiaConstants::kChromeSource, client_->GetURLRequestContext()));
|
| gaia_fetcher_->StartListAccounts();
|
| }
|
| }
|
| @@ -510,13 +492,10 @@ void AccountReconcilor::ContinueReconcileActionAfterGetGaiaAccounts(
|
| }
|
|
|
| void AccountReconcilor::ValidateAccountsFromTokenService() {
|
| - primary_account_ =
|
| - SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername();
|
| + primary_account_ = signin_manager_->GetAuthenticatedUsername();
|
| DCHECK(!primary_account_.empty());
|
|
|
| - ProfileOAuth2TokenService* token_service =
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
|
| - chrome_accounts_ = token_service->GetAccounts();
|
| + chrome_accounts_ = token_service_->GetAccounts();
|
| DCHECK_GT(chrome_accounts_.size(), 0u);
|
|
|
| VLOG(1) << "AccountReconcilor::ValidateAccountsFromTokenService: "
|
| @@ -529,9 +508,8 @@ void AccountReconcilor::ValidateAccountsFromTokenService() {
|
| const OAuth2TokenService::ScopeSet scopes =
|
| AccountReconcilor::UserIdFetcher::GetScopes();
|
| for (size_t i = 0; i < chrome_accounts_.size(); ++i) {
|
| - requests_[i] = token_service->StartRequest(chrome_accounts_[i],
|
| - scopes,
|
| - this);
|
| + requests_[i] =
|
| + token_service_->StartRequest(chrome_accounts_[i], scopes, this);
|
| }
|
|
|
| DCHECK_EQ(0u, user_id_fetchers_.size());
|
| @@ -554,8 +532,7 @@ void AccountReconcilor::OnGetTokenSuccess(
|
| VLOG(1) << "AccountReconcilor::OnGetTokenSuccess: valid " << account_id;
|
|
|
| DCHECK(!user_id_fetchers_[index]);
|
| - user_id_fetchers_[index] =
|
| - new UserIdFetcher(this, access_token, account_id);
|
| + user_id_fetchers_[index] = new UserIdFetcher(this, access_token, account_id);
|
| }
|
|
|
| void AccountReconcilor::OnGetTokenFailure(
|
| @@ -570,8 +547,7 @@ void AccountReconcilor::OnGetTokenFailure(
|
|
|
| const std::string& account_id = chrome_accounts_[index];
|
|
|
| - VLOG(1) << "AccountReconcilor::OnGetTokenFailure: invalid "
|
| - << account_id;
|
| + VLOG(1) << "AccountReconcilor::OnGetTokenFailure: invalid " << account_id;
|
| HandleFailedAccountIdCheck(account_id);
|
| }
|
|
|
| @@ -596,15 +572,16 @@ void AccountReconcilor::FinishReconcile() {
|
| for (size_t i = 0; i < gaia_accounts_.size(); ++i) {
|
| const std::string& gaia_account = gaia_accounts_[i].first;
|
| if (gaia_accounts_[i].second &&
|
| - valid_chrome_accounts_.find(gaia_account) ==
|
| - valid_chrome_accounts_.end()) {
|
| + valid_chrome_accounts_.find(gaia_account) ==
|
| + valid_chrome_accounts_.end()) {
|
| add_to_chrome_.push_back(std::make_pair(gaia_account, i));
|
| }
|
| }
|
|
|
| // Determine if we need to merge accounts from chrome into gaia cookie.
|
| for (EmailSet::const_iterator i = valid_chrome_accounts_.begin();
|
| - i != valid_chrome_accounts_.end(); ++i) {
|
| + i != valid_chrome_accounts_.end();
|
| + ++i) {
|
| bool add_to_cookie = true;
|
| for (size_t j = 0; j < gaia_accounts_.size(); ++j) {
|
| if (gaia::AreEmailsSame(gaia_accounts_[j].first, *i)) {
|
| @@ -623,7 +600,8 @@ void AccountReconcilor::FinishReconcile() {
|
| PerformLogoutAllAccountsAction();
|
| add_to_cookie_.push_back(primary_account_);
|
| for (EmailSet::const_iterator i = valid_chrome_accounts_.begin();
|
| - i != valid_chrome_accounts_.end(); ++i) {
|
| + i != valid_chrome_accounts_.end();
|
| + ++i) {
|
| if (*i != primary_account_)
|
| add_to_cookie_.push_back(*i);
|
| }
|
| @@ -637,8 +615,9 @@ void AccountReconcilor::FinishReconcile() {
|
| // For each account in the gaia cookie not known to chrome,
|
| // PerformAddToChromeAction.
|
| for (std::vector<std::pair<std::string, int> >::const_iterator i =
|
| - add_to_chrome_.begin();
|
| - i != add_to_chrome_.end(); ++i) {
|
| + add_to_chrome_.begin();
|
| + i != add_to_chrome_.end();
|
| + ++i) {
|
| PerformAddToChromeAction(i->first, i->second);
|
| }
|
|
|
| @@ -667,8 +646,7 @@ void AccountReconcilor::ScheduleStartReconcileIfChromeAccountsChanged() {
|
| // Start a reconcile as the token accounts have changed.
|
| VLOG(1) << "AccountReconcilor::StartReconcileIfChromeAccountsChanged";
|
| std::vector<std::string> reconciled_accounts(chrome_accounts_);
|
| - std::vector<std::string> new_chrome_accounts(
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->GetAccounts());
|
| + std::vector<std::string> new_chrome_accounts(token_service_->GetAccounts());
|
| std::sort(reconciled_accounts.begin(), reconciled_accounts.end());
|
| std::sort(new_chrome_accounts.begin(), new_chrome_accounts.end());
|
| if (reconciled_accounts != new_chrome_accounts) {
|
| @@ -686,7 +664,8 @@ void AccountReconcilor::MergeSessionCompleted(
|
|
|
| // Remove the account from the list that is being merged.
|
| for (std::vector<std::string>::iterator i = add_to_cookie_.begin();
|
| - i != add_to_cookie_.end(); ++i) {
|
| + i != add_to_cookie_.end();
|
| + ++i) {
|
| if (account_id == *i) {
|
| add_to_cookie_.erase(i);
|
| break;
|
| @@ -713,15 +692,14 @@ void AccountReconcilor::HandleRefreshTokenFetched(
|
| const std::string& account_id,
|
| const std::string& refresh_token) {
|
| if (!refresh_token.empty()) {
|
| - ProfileOAuth2TokenService* token_service =
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile());
|
| - token_service->UpdateCredentials(account_id, refresh_token);
|
| + token_service_->UpdateCredentials(account_id, refresh_token);
|
| }
|
|
|
| // Remove the account from the list that is being updated.
|
| for (std::vector<std::pair<std::string, int> >::iterator i =
|
| add_to_chrome_.begin();
|
| - i != add_to_chrome_.end(); ++i) {
|
| + i != add_to_chrome_.end();
|
| + ++i) {
|
| if (gaia::AreEmailsSame(account_id, i->first)) {
|
| add_to_chrome_.erase(i);
|
| break;
|
|
|