| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/login/online_attempt.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.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" | |
| 25 | |
| 26 using content::BrowserThread; | |
| 27 | |
| 28 namespace chromeos { | |
| 29 | |
| 30 // static | |
| 31 const int OnlineAttempt::kClientLoginTimeoutMs = 10000; | |
| 32 | |
| 33 OnlineAttempt::OnlineAttempt(AuthAttemptState* current_attempt, | |
| 34 AuthAttemptStateResolver* callback) | |
| 35 : attempt_(current_attempt), | |
| 36 resolver_(callback), | |
| 37 weak_factory_(this), | |
| 38 try_again_(true) { | |
| 39 DCHECK(attempt_->user_type == User::USER_TYPE_REGULAR); | |
| 40 } | |
| 41 | |
| 42 OnlineAttempt::~OnlineAttempt() { | |
| 43 // Just to be sure. | |
| 44 if (client_fetcher_.get()) | |
| 45 client_fetcher_->CancelRequest(); | |
| 46 } | |
| 47 | |
| 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())); | |
| 56 } | |
| 57 | |
| 58 void OnlineAttempt::OnClientLoginSuccess( | |
| 59 const GaiaAuthConsumer::ClientLoginResult& unused) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 61 VLOG(1) << "Online login successful!"; | |
| 62 | |
| 63 weak_factory_.InvalidateWeakPtrs(); | |
| 64 | |
| 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. | |
| 70 // | |
| 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 | |
| 73 // handling, though. | |
| 74 attempt_->DisableHosted(); | |
| 75 TryClientLogin(); | |
| 76 return; | |
| 77 } | |
| 78 TriggerResolve(LoginFailure::LoginFailureNone()); | |
| 79 } | |
| 80 | |
| 81 void OnlineAttempt::OnClientLoginFailure( | |
| 82 const GoogleServiceAuthError& error) { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 84 | |
| 85 weak_factory_.InvalidateWeakPtrs(); | |
| 86 | |
| 87 if (error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { | |
| 88 if (try_again_) { | |
| 89 try_again_ = false; | |
| 90 // TODO(cmasone): add UMA tracking for this to see if we can remove it. | |
| 91 LOG(ERROR) << "Login attempt canceled!?!? Trying again."; | |
| 92 TryClientLogin(); | |
| 93 return; | |
| 94 } | |
| 95 LOG(ERROR) << "Login attempt canceled again? Already retried..."; | |
| 96 } | |
| 97 | |
| 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))); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 if (error.state() == GoogleServiceAuthError::TWO_FACTOR) { | |
| 112 LOG(WARNING) << "Two factor authenticated. Sync will not work."; | |
| 113 TriggerResolve(LoginFailure::LoginFailureNone()); | |
| 114 | |
| 115 return; | |
| 116 } | |
| 117 VLOG(2) << "ClientLogin attempt failed with " << error.state(); | |
| 118 TriggerResolve(LoginFailure::FromNetworkAuthFailure(error)); | |
| 119 } | |
| 120 | |
| 121 void OnlineAttempt::TryClientLogin() { | |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 123 | |
| 124 BrowserThread::PostDelayedTask( | |
| 125 BrowserThread::UI, FROM_HERE, | |
| 126 base::Bind(&OnlineAttempt::CancelClientLogin, weak_factory_.GetWeakPtr()), | |
| 127 base::TimeDelta::FromMilliseconds(kClientLoginTimeoutMs)); | |
| 128 | |
| 129 client_fetcher_->StartClientLogin( | |
| 130 attempt_->user_context.GetUserID(), | |
| 131 attempt_->user_context.GetPassword(), | |
| 132 GaiaConstants::kSyncService, | |
| 133 attempt_->login_token, | |
| 134 attempt_->login_captcha, | |
| 135 attempt_->hosted_policy()); | |
| 136 } | |
| 137 | |
| 138 bool OnlineAttempt::HasPendingFetch() { | |
| 139 return client_fetcher_->HasPendingFetch(); | |
| 140 } | |
| 141 | |
| 142 void OnlineAttempt::CancelRequest() { | |
| 143 weak_factory_.InvalidateWeakPtrs(); | |
| 144 } | |
| 145 | |
| 146 void OnlineAttempt::CancelClientLogin() { | |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 148 if (HasPendingFetch()) { | |
| 149 LOG(WARNING) << "Canceling ClientLogin attempt."; | |
| 150 CancelRequest(); | |
| 151 | |
| 152 TriggerResolve(LoginFailure(LoginFailure::LOGIN_TIMED_OUT)); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void OnlineAttempt::TriggerResolve( | |
| 157 const LoginFailure& outcome) { | |
| 158 attempt_->RecordOnlineLoginStatus(outcome); | |
| 159 client_fetcher_.reset(NULL); | |
| 160 resolver_->Resolve(); | |
| 161 } | |
| 162 | |
| 163 } // namespace chromeos | |
| OLD | NEW |