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); |
| 45 } |
| 46 |
| 47 void GoogleAutoLoginHelper::CancelAll() { |
| 48 gaia_auth_fetcher_.reset(); |
| 49 uber_token_fetcher_.reset(); |
| 50 accounts_.clear(); |
33 } | 51 } |
34 | 52 |
35 void GoogleAutoLoginHelper::OnUbertokenSuccess(const std::string& uber_token) { | 53 void GoogleAutoLoginHelper::OnUbertokenSuccess(const std::string& uber_token) { |
36 VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess"; | 54 VLOG(1) << "GoogleAutoLoginHelper::OnUbertokenSuccess" |
37 gaia_auth_fetcher_.reset(CreateNewGaiaAuthFetcher()); | 55 << " account=" << accounts_.front(); |
| 56 gaia_auth_fetcher_.reset( |
| 57 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource, |
| 58 profile_->GetRequestContext())); |
38 gaia_auth_fetcher_->StartMergeSession(uber_token); | 59 gaia_auth_fetcher_->StartMergeSession(uber_token); |
39 } | 60 } |
40 | 61 |
41 void GoogleAutoLoginHelper::OnUbertokenFailure( | 62 void GoogleAutoLoginHelper::OnUbertokenFailure( |
42 const GoogleServiceAuthError& error) { | 63 const GoogleServiceAuthError& error) { |
43 VLOG(1) << "Failed to retrieve ubertoken, error: " << error.ToString(); | 64 VLOG(1) << "Failed to retrieve ubertoken" |
44 if (base::MessageLoop::current()) { | 65 << " account=" << accounts_.front() |
45 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 66 << " error=" << error.ToString(); |
46 } else { | 67 const std::string account_id = accounts_.front(); |
47 delete this; | 68 MergeNextAccount(); |
48 } | 69 SignalComplete(account_id, error); |
49 } | 70 } |
50 | 71 |
51 void GoogleAutoLoginHelper::OnMergeSessionSuccess(const std::string& data) { | 72 void GoogleAutoLoginHelper::OnMergeSessionSuccess(const std::string& data) { |
52 DVLOG(1) << "MergeSession successful." << data; | 73 DVLOG(1) << "MergeSession successful account=" << accounts_.front(); |
53 if (accounts_.empty()) { | 74 const std::string account_id = accounts_.front(); |
54 if (base::MessageLoop::current()) { | 75 MergeNextAccount(); |
55 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 76 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 } | 77 } |
65 | 78 |
66 void GoogleAutoLoginHelper::OnMergeSessionFailure( | 79 void GoogleAutoLoginHelper::OnMergeSessionFailure( |
67 const GoogleServiceAuthError& error) { | 80 const GoogleServiceAuthError& error) { |
68 VLOG(1) << "Failed MergeSession request, error: " << error.ToString(); | 81 VLOG(1) << "Failed MergeSession" |
69 // TODO(acleung): Depending on the return error we should probably | 82 << " account=" << accounts_.front() |
70 // take different actions instead of just throw our hands up. | 83 << " error=" << error.ToString(); |
| 84 const std::string account_id = accounts_.front(); |
| 85 MergeNextAccount(); |
| 86 SignalComplete(account_id, error); |
| 87 } |
71 | 88 |
72 // Clearning pending accounts for now. | 89 void GoogleAutoLoginHelper::StartFetching() { |
73 accounts_.clear(); | 90 uber_token_fetcher_.reset(new UbertokenFetcher(profile_, this)); |
| 91 uber_token_fetcher_->StartFetchingToken(accounts_.front()); |
| 92 } |
74 | 93 |
75 if (base::MessageLoop::current()) { | 94 void GoogleAutoLoginHelper::SignalComplete( |
76 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 95 const std::string& account_id, |
| 96 const GoogleServiceAuthError& error) { |
| 97 // Its possible for the observer to delete |this| object. Don't access |
| 98 // access any members after this calling the observer. This method should |
| 99 // be the last call in any other method. |
| 100 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 101 MergeSessionCompleted(account_id, error)); |
| 102 } |
| 103 |
| 104 void GoogleAutoLoginHelper::MergeNextAccount() { |
| 105 gaia_auth_fetcher_.reset(); |
| 106 accounts_.pop_front(); |
| 107 if (accounts_.empty()) { |
| 108 uber_token_fetcher_.reset(); |
77 } else { | 109 } else { |
78 delete this; | 110 StartFetching(); |
79 } | 111 } |
80 } | 112 } |
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 |