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
3 // found in the LICENSE file.
5 #include "chrome/browser/signin/google_auto_login_helper.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chrome_notification_types.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"
13 #include "google_apis/gaia/gaia_auth_fetcher.h"
14 #include "google_apis/gaia/gaia_constants.h"
16 GoogleAutoLoginHelper::GoogleAutoLoginHelper(Profile
* profile
,
20 AddObserver(observer
);
23 GoogleAutoLoginHelper::~GoogleAutoLoginHelper() {
24 DCHECK(accounts_
.empty());
27 void GoogleAutoLoginHelper::LogIn(const std::string
& account_id
) {
28 accounts_
.push_back(account_id
);
29 if (accounts_
.size() == 1)
33 void GoogleAutoLoginHelper::AddObserver(Observer
* observer
) {
34 observer_list_
.AddObserver(observer
);
37 void GoogleAutoLoginHelper::RemoveObserver(Observer
* observer
) {
38 observer_list_
.RemoveObserver(observer
);
41 void GoogleAutoLoginHelper::CancelAll() {
42 gaia_auth_fetcher_
.reset();
43 uber_token_fetcher_
.reset();
47 void GoogleAutoLoginHelper::OnUbertokenSuccess(const std::string
& uber_token
) {
48 VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess"
49 << " account=" << accounts_
.front();
50 gaia_auth_fetcher_
.reset(
51 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource
,
52 profile_
->GetRequestContext()));
53 gaia_auth_fetcher_
->StartMergeSession(uber_token
);
56 void GoogleAutoLoginHelper::OnUbertokenFailure(
57 const GoogleServiceAuthError
& error
) {
58 VLOG(1) << "Failed to retrieve ubertoken"
59 << " account=" << accounts_
.front()
60 << " error=" << error
.ToString();
61 const std::string account_id
= accounts_
.front();
63 SignalComplete(account_id
, error
);
66 void GoogleAutoLoginHelper::OnMergeSessionSuccess(const std::string
& data
) {
67 DVLOG(1) << "MergeSession successful account=" << accounts_
.front();
68 const std::string account_id
= accounts_
.front();
70 SignalComplete(account_id
, GoogleServiceAuthError::AuthErrorNone());
73 void GoogleAutoLoginHelper::OnMergeSessionFailure(
74 const GoogleServiceAuthError
& error
) {
75 VLOG(1) << "Failed MergeSession"
76 << " account=" << accounts_
.front()
77 << " error=" << error
.ToString();
78 const std::string account_id
= accounts_
.front();
80 SignalComplete(account_id
, error
);
83 void GoogleAutoLoginHelper::StartFetching() {
84 uber_token_fetcher_
.reset(new UbertokenFetcher(
85 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
),
87 profile_
->GetRequestContext()));
88 uber_token_fetcher_
->StartFetchingToken(accounts_
.front());
91 void GoogleAutoLoginHelper::SignalComplete(
92 const std::string
& account_id
,
93 const GoogleServiceAuthError
& error
) {
94 // Its possible for the observer to delete |this| object. Don't access
95 // access any members after this calling the observer. This method should
96 // be the last call in any other method.
97 FOR_EACH_OBSERVER(Observer
, observer_list_
,
98 MergeSessionCompleted(account_id
, error
));
101 void GoogleAutoLoginHelper::MergeNextAccount() {
102 gaia_auth_fetcher_
.reset();
103 accounts_
.pop_front();
104 if (accounts_
.empty()) {
105 uber_token_fetcher_
.reset();