| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/signin/google_auto_login_helper.h" | 5 #include "chrome/browser/signin/google_auto_login_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "content/public/browser/notification_service.h" |
| 9 #include "google_apis/gaia/gaia_auth_fetcher.h" | 13 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 10 #include "google_apis/gaia/gaia_constants.h" | 14 #include "google_apis/gaia/gaia_constants.h" |
| 11 | 15 |
| 12 GoogleAutoLoginHelper::GoogleAutoLoginHelper(Profile* profile) | 16 GoogleAutoLoginHelper::GoogleAutoLoginHelper(Profile* profile, |
| 13 : profile_(profile) {} | 17 Observer* observer) |
| 18 : profile_(profile) { |
| 19 if (observer) |
| 20 AddObserver(observer); |
| 21 } |
| 14 | 22 |
| 15 GoogleAutoLoginHelper::~GoogleAutoLoginHelper() { | 23 GoogleAutoLoginHelper::~GoogleAutoLoginHelper() { |
| 16 DCHECK(accounts_.empty()); | 24 DCHECK(accounts_.empty()); |
| 17 } | 25 } |
| 18 | 26 |
| 19 void GoogleAutoLoginHelper::LogIn() { | 27 void GoogleAutoLoginHelper::LogIn() { |
| 20 // This is the code path for non-mirror world. | 28 ProfileOAuth2TokenService* token_service = |
| 21 uber_token_fetcher_.reset(CreateNewUbertokenFetcher()); | 29 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 22 uber_token_fetcher_->StartFetchingToken(); | 30 LogIn(token_service->GetPrimaryAccountId()); |
| 23 DCHECK(accounts_.empty()); | |
| 24 } | 31 } |
| 25 | 32 |
| 26 void GoogleAutoLoginHelper::LogIn(const std::string& account_id) { | 33 void GoogleAutoLoginHelper::LogIn(const std::string& account_id) { |
| 27 if (uber_token_fetcher_.get()) { | 34 accounts_.push_back(account_id); |
| 28 accounts_.push_back(account_id); | 35 if (accounts_.size() == 1) |
| 29 } else { | 36 StartFetching(); |
| 30 uber_token_fetcher_.reset(CreateNewUbertokenFetcher()); | 37 } |
| 31 uber_token_fetcher_->StartFetchingToken(account_id); | 38 |
| 32 } | 39 void GoogleAutoLoginHelper::AddObserver(Observer* observer) { |
| 40 observer_list_.AddObserver(observer); |
| 41 } |
| 42 |
| 43 void GoogleAutoLoginHelper::RemoveObserver(Observer* observer) { |
| 44 observer_list_.RemoveObserver(observer); |
| 33 } | 45 } |
| 34 | 46 |
| 35 void GoogleAutoLoginHelper::OnUbertokenSuccess(const std::string& uber_token) { | 47 void GoogleAutoLoginHelper::OnUbertokenSuccess(const std::string& uber_token) { |
| 36 VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess"; | 48 VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess" |
| 37 gaia_auth_fetcher_.reset(CreateNewGaiaAuthFetcher()); | 49 << " account=" << accounts_.front(); |
| 50 gaia_auth_fetcher_.reset( |
| 51 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource, |
| 52 profile_->GetRequestContext())); |
| 38 gaia_auth_fetcher_->StartMergeSession(uber_token); | 53 gaia_auth_fetcher_->StartMergeSession(uber_token); |
| 39 } | 54 } |
| 40 | 55 |
| 41 void GoogleAutoLoginHelper::OnUbertokenFailure( | 56 void GoogleAutoLoginHelper::OnUbertokenFailure( |
| 42 const GoogleServiceAuthError& error) { | 57 const GoogleServiceAuthError& error) { |
| 43 VLOG(1) << "Failed to retrieve ubertoken, error: " << error.ToString(); | 58 VLOG(1) << "Failed to retrieve ubertoken" |
| 44 if (base::MessageLoop::current()) { | 59 << " account=" << accounts_.front() |
| 45 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 60 << " error=" << error.ToString(); |
| 46 } else { | 61 const std::string account_id = accounts_.front(); |
| 47 delete this; | 62 MergeNextAccount(); |
| 48 } | 63 SignalComplete(account_id, error); |
| 49 } | 64 } |
| 50 | 65 |
| 51 void GoogleAutoLoginHelper::OnMergeSessionSuccess(const std::string& data) { | 66 void GoogleAutoLoginHelper::OnMergeSessionSuccess(const std::string& data) { |
| 52 DVLOG(1) << "MergeSession successful." << data; | 67 DVLOG(1) << "MergeSession successful account=" << accounts_.front(); |
| 53 if (accounts_.empty()) { | 68 const std::string account_id = accounts_.front(); |
| 54 if (base::MessageLoop::current()) { | 69 MergeNextAccount(); |
| 55 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 70 SignalComplete(account_id, GoogleServiceAuthError::AuthErrorNone()); |
| 56 } else { | |
| 57 delete this; | |
| 58 } | |
| 59 } else { | |
| 60 uber_token_fetcher_.reset(CreateNewUbertokenFetcher()); | |
| 61 uber_token_fetcher_->StartFetchingToken(accounts_.front()); | |
| 62 accounts_.pop_front(); | |
| 63 } | |
| 64 } | 71 } |
| 65 | 72 |
| 66 void GoogleAutoLoginHelper::OnMergeSessionFailure( | 73 void GoogleAutoLoginHelper::OnMergeSessionFailure( |
| 67 const GoogleServiceAuthError& error) { | 74 const GoogleServiceAuthError& error) { |
| 68 VLOG(1) << "Failed MergeSession request, error: " << error.ToString(); | 75 VLOG(1) << "Failed MergeSession" |
| 69 // TODO(acleung): Depending on the return error we should probably | 76 << " account=" << accounts_.front() |
| 70 // take different actions instead of just throw our hands up. | 77 << " error=" << error.ToString(); |
| 78 const std::string account_id = accounts_.front(); |
| 79 MergeNextAccount(); |
| 80 SignalComplete(account_id, error); |
| 81 } |
| 71 | 82 |
| 72 // Clearning pending accounts for now. | 83 void GoogleAutoLoginHelper::StartFetching() { |
| 73 accounts_.clear(); | 84 uber_token_fetcher_.reset(new UbertokenFetcher(profile_, this)); |
| 85 uber_token_fetcher_->StartFetchingToken(accounts_.front()); |
| 86 } |
| 74 | 87 |
| 75 if (base::MessageLoop::current()) { | 88 void GoogleAutoLoginHelper::SignalComplete( |
| 76 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 89 const std::string& account_id, |
| 90 const GoogleServiceAuthError& error) { |
| 91 // Its possible for the observer to delete |this| object. Don't access |
| 92 // access any members after this calling the observer. This method should |
| 93 // be the last call in any other method. |
| 94 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 95 MergeSessionCompleted(account_id, error)); |
| 96 } |
| 97 |
| 98 void GoogleAutoLoginHelper::MergeNextAccount() { |
| 99 gaia_auth_fetcher_.reset(); |
| 100 accounts_.pop_front(); |
| 101 if (accounts_.empty()) { |
| 102 uber_token_fetcher_.reset(); |
| 77 } else { | 103 } else { |
| 78 delete this; | 104 StartFetching(); |
| 79 } | 105 } |
| 80 } | 106 } |
| 81 | |
| 82 UbertokenFetcher* GoogleAutoLoginHelper::CreateNewUbertokenFetcher() { | |
| 83 return new UbertokenFetcher(profile_, this); | |
| 84 } | |
| 85 | |
| 86 GaiaAuthFetcher* GoogleAutoLoginHelper::CreateNewGaiaAuthFetcher() { | |
| 87 return new GaiaAuthFetcher( | |
| 88 this, GaiaConstants::kChromeSource, profile_->GetRequestContext()); | |
| 89 } | |
| 90 | |
| OLD | NEW |