1 // Copyright (c) 2012 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/chromeos/login/online_attempt.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/chromeos/login/auth_attempt_state.h"
14 #include "chrome/browser/chromeos/login/auth_attempt_state_resolver.h"
15 #include "chrome/browser/chromeos/login/user.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "google_apis/gaia/gaia_auth_consumer.h"
20 #include "google_apis/gaia/gaia_auth_fetcher.h"
21 #include "google_apis/gaia/gaia_constants.h"
22 #include "net/base/load_flags.h"
23 #include "net/base/net_errors.h"
24 #include "net/url_request/url_request_status.h"
26 using content::BrowserThread
;
31 const int OnlineAttempt::kClientLoginTimeoutMs
= 10000;
33 OnlineAttempt::OnlineAttempt(AuthAttemptState
* current_attempt
,
34 AuthAttemptStateResolver
* callback
)
35 : attempt_(current_attempt
),
39 DCHECK(attempt_
->user_type
== User::USER_TYPE_REGULAR
);
42 OnlineAttempt::~OnlineAttempt() {
44 if (client_fetcher_
.get())
45 client_fetcher_
->CancelRequest();
48 void OnlineAttempt::Initiate(Profile
* auth_profile
) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
50 client_fetcher_
.reset(
51 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource
,
52 auth_profile
->GetRequestContext()));
53 BrowserThread::PostTask(
54 BrowserThread::UI
, FROM_HERE
,
55 base::Bind(&OnlineAttempt::TryClientLogin
, weak_factory_
.GetWeakPtr()));
58 void OnlineAttempt::OnClientLoginSuccess(
59 const GaiaAuthConsumer::ClientLoginResult
& unused
) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
61 VLOG(1) << "Online login successful!";
63 weak_factory_
.InvalidateWeakPtrs();
65 if (attempt_
->hosted_policy() == GaiaAuthFetcher::HostedAccountsAllowed
&&
66 attempt_
->is_first_time_user()) {
67 // First time user, and we don't know if the account is HOSTED or not.
68 // Since we don't allow HOSTED accounts to log in, we need to try
69 // again, without allowing HOSTED accounts.
71 // NOTE: we used to do this in the opposite order, so that we'd only
72 // try the HOSTED pathway if GOOGLE-only failed. This breaks CAPTCHA
74 attempt_
->DisableHosted();
78 TriggerResolve(LoginFailure::LoginFailureNone());
81 void OnlineAttempt::OnClientLoginFailure(
82 const GoogleServiceAuthError
& error
) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
85 weak_factory_
.InvalidateWeakPtrs();
87 if (error
.state() == GoogleServiceAuthError::REQUEST_CANCELED
) {
90 // TODO(cmasone): add UMA tracking for this to see if we can remove it.
91 LOG(ERROR
) << "Login attempt canceled!?!? Trying again.";
95 LOG(ERROR
) << "Login attempt canceled again? Already retried...";
98 if (error
.state() == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
&&
99 attempt_
->is_first_time_user() &&
100 attempt_
->hosted_policy() != GaiaAuthFetcher::HostedAccountsAllowed
) {
101 // This was a first-time login, we already tried allowing HOSTED accounts
102 // and succeeded. That we've failed with INVALID_GAIA_CREDENTIALS now
103 // indicates that the account is HOSTED.
104 LOG(WARNING
) << "Rejecting valid HOSTED account.";
105 TriggerResolve(LoginFailure::FromNetworkAuthFailure(
106 GoogleServiceAuthError(
107 GoogleServiceAuthError::HOSTED_NOT_ALLOWED
)));
111 if (error
.state() == GoogleServiceAuthError::TWO_FACTOR
) {
112 LOG(WARNING
) << "Two factor authenticated. Sync will not work.";
113 TriggerResolve(LoginFailure::LoginFailureNone());
117 VLOG(2) << "ClientLogin attempt failed with " << error
.state();
118 TriggerResolve(LoginFailure::FromNetworkAuthFailure(error
));
121 void OnlineAttempt::TryClientLogin() {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
124 BrowserThread::PostDelayedTask(
125 BrowserThread::UI
, FROM_HERE
,
126 base::Bind(&OnlineAttempt::CancelClientLogin
, weak_factory_
.GetWeakPtr()),
127 base::TimeDelta::FromMilliseconds(kClientLoginTimeoutMs
));
129 client_fetcher_
->StartClientLogin(
130 attempt_
->user_context
.username
,
131 attempt_
->user_context
.password
,
132 GaiaConstants::kSyncService
,
133 attempt_
->login_token
,
134 attempt_
->login_captcha
,
135 attempt_
->hosted_policy());
138 bool OnlineAttempt::HasPendingFetch() {
139 return client_fetcher_
->HasPendingFetch();
142 void OnlineAttempt::CancelRequest() {
143 weak_factory_
.InvalidateWeakPtrs();
146 void OnlineAttempt::CancelClientLogin() {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
148 if (HasPendingFetch()) {
149 LOG(WARNING
) << "Canceling ClientLogin attempt.";
152 TriggerResolve(LoginFailure(LoginFailure::LOGIN_TIMED_OUT
));
156 void OnlineAttempt::TriggerResolve(
157 const LoginFailure
& outcome
) {
158 attempt_
->RecordOnlineLoginStatus(outcome
);
159 client_fetcher_
.reset(NULL
);
160 resolver_
->Resolve();
163 } // namespace chromeos