| Index: chrome/browser/signin/google_auto_login_helper.cc
|
| diff --git a/chrome/browser/signin/google_auto_login_helper.cc b/chrome/browser/signin/google_auto_login_helper.cc
|
| index 1cf1cf2ec6ea914048a4a7ed6a04ee8cf87e14bf..ab0399aa75f8b6ddc77cfdb0d7ccddddf7f0dfcc 100644
|
| --- a/chrome/browser/signin/google_auto_login_helper.cc
|
| +++ b/chrome/browser/signin/google_auto_login_helper.cc
|
| @@ -5,86 +5,108 @@
|
| #include "chrome/browser/signin/google_auto_login_helper.h"
|
|
|
| #include "chrome/browser/browser_process.h"
|
| +#include "chrome/browser/chrome_notification_types.h"
|
| #include "chrome/browser/profiles/profile_manager.h"
|
| +#include "chrome/browser/signin/profile_oauth2_token_service.h"
|
| +#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
|
| +#include "content/public/browser/notification_service.h"
|
| #include "google_apis/gaia/gaia_auth_fetcher.h"
|
| #include "google_apis/gaia/gaia_constants.h"
|
|
|
| -GoogleAutoLoginHelper::GoogleAutoLoginHelper(Profile* profile)
|
| - : profile_(profile) {}
|
| +GoogleAutoLoginHelper::GoogleAutoLoginHelper(Profile* profile,
|
| + Observer* observer)
|
| + : profile_(profile) {
|
| + if (observer)
|
| + AddObserver(observer);
|
| +}
|
|
|
| GoogleAutoLoginHelper::~GoogleAutoLoginHelper() {
|
| DCHECK(accounts_.empty());
|
| }
|
|
|
| void GoogleAutoLoginHelper::LogIn() {
|
| - // This is the code path for non-mirror world.
|
| - uber_token_fetcher_.reset(CreateNewUbertokenFetcher());
|
| - uber_token_fetcher_->StartFetchingToken();
|
| - DCHECK(accounts_.empty());
|
| + ProfileOAuth2TokenService* token_service =
|
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
|
| + LogIn(token_service->GetPrimaryAccountId());
|
| }
|
|
|
| void GoogleAutoLoginHelper::LogIn(const std::string& account_id) {
|
| - if (uber_token_fetcher_.get()) {
|
| - accounts_.push_back(account_id);
|
| - } else {
|
| - uber_token_fetcher_.reset(CreateNewUbertokenFetcher());
|
| - uber_token_fetcher_->StartFetchingToken(account_id);
|
| - }
|
| + accounts_.push_back(account_id);
|
| + if (accounts_.size() == 1)
|
| + StartFetching();
|
| +}
|
| +
|
| +void GoogleAutoLoginHelper::AddObserver(Observer* observer) {
|
| + observer_list_.AddObserver(observer);
|
| +}
|
| +
|
| +void GoogleAutoLoginHelper::RemoveObserver(Observer* observer) {
|
| + observer_list_.RemoveObserver(observer);
|
| +}
|
| +
|
| +void GoogleAutoLoginHelper::CancelAll() {
|
| + gaia_auth_fetcher_.reset();
|
| + uber_token_fetcher_.reset();
|
| + accounts_.clear();
|
| }
|
|
|
| void GoogleAutoLoginHelper::OnUbertokenSuccess(const std::string& uber_token) {
|
| - VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess";
|
| - gaia_auth_fetcher_.reset(CreateNewGaiaAuthFetcher());
|
| + VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess"
|
| + << " account=" << accounts_.front();
|
| + gaia_auth_fetcher_.reset(
|
| + new GaiaAuthFetcher(this, GaiaConstants::kChromeSource,
|
| + profile_->GetRequestContext()));
|
| gaia_auth_fetcher_->StartMergeSession(uber_token);
|
| }
|
|
|
| void GoogleAutoLoginHelper::OnUbertokenFailure(
|
| const GoogleServiceAuthError& error) {
|
| - VLOG(1) << "Failed to retrieve ubertoken, error: " << error.ToString();
|
| - if (base::MessageLoop::current()) {
|
| - base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
|
| - } else {
|
| - delete this;
|
| - }
|
| + VLOG(1) << "Failed to retrieve ubertoken"
|
| + << " account=" << accounts_.front()
|
| + << " error=" << error.ToString();
|
| + const std::string account_id = accounts_.front();
|
| + MergeNextAccount();
|
| + SignalComplete(account_id, error);
|
| }
|
|
|
| void GoogleAutoLoginHelper::OnMergeSessionSuccess(const std::string& data) {
|
| - DVLOG(1) << "MergeSession successful." << data;
|
| - if (accounts_.empty()) {
|
| - if (base::MessageLoop::current()) {
|
| - base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
|
| - } else {
|
| - delete this;
|
| - }
|
| - } else {
|
| - uber_token_fetcher_.reset(CreateNewUbertokenFetcher());
|
| - uber_token_fetcher_->StartFetchingToken(accounts_.front());
|
| - accounts_.pop_front();
|
| - }
|
| + DVLOG(1) << "MergeSession successful account=" << accounts_.front();
|
| + const std::string account_id = accounts_.front();
|
| + MergeNextAccount();
|
| + SignalComplete(account_id, GoogleServiceAuthError::AuthErrorNone());
|
| }
|
|
|
| void GoogleAutoLoginHelper::OnMergeSessionFailure(
|
| const GoogleServiceAuthError& error) {
|
| - VLOG(1) << "Failed MergeSession request, error: " << error.ToString();
|
| - // TODO(acleung): Depending on the return error we should probably
|
| - // take different actions instead of just throw our hands up.
|
| -
|
| - // Clearning pending accounts for now.
|
| - accounts_.clear();
|
| -
|
| - if (base::MessageLoop::current()) {
|
| - base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
|
| - } else {
|
| - delete this;
|
| - }
|
| + VLOG(1) << "Failed MergeSession"
|
| + << " account=" << accounts_.front()
|
| + << " error=" << error.ToString();
|
| + const std::string account_id = accounts_.front();
|
| + MergeNextAccount();
|
| + SignalComplete(account_id, error);
|
| }
|
|
|
| -UbertokenFetcher* GoogleAutoLoginHelper::CreateNewUbertokenFetcher() {
|
| - return new UbertokenFetcher(profile_, this);
|
| +void GoogleAutoLoginHelper::StartFetching() {
|
| + uber_token_fetcher_.reset(new UbertokenFetcher(profile_, this));
|
| + uber_token_fetcher_->StartFetchingToken(accounts_.front());
|
| }
|
|
|
| -GaiaAuthFetcher* GoogleAutoLoginHelper::CreateNewGaiaAuthFetcher() {
|
| - return new GaiaAuthFetcher(
|
| - this, GaiaConstants::kChromeSource, profile_->GetRequestContext());
|
| +void GoogleAutoLoginHelper::SignalComplete(
|
| + const std::string& account_id,
|
| + const GoogleServiceAuthError& error) {
|
| + // Its possible for the observer to delete |this| object. Don't access
|
| + // access any members after this calling the observer. This method should
|
| + // be the last call in any other method.
|
| + FOR_EACH_OBSERVER(Observer, observer_list_,
|
| + MergeSessionCompleted(account_id, error));
|
| }
|
|
|
| +void GoogleAutoLoginHelper::MergeNextAccount() {
|
| + gaia_auth_fetcher_.reset();
|
| + accounts_.pop_front();
|
| + if (accounts_.empty()) {
|
| + uber_token_fetcher_.reset();
|
| + } else {
|
| + StartFetching();
|
| + }
|
| +}
|
|
|