| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chromeos/login/auth/online_attempt.h" | 5 #include "chromeos/login/auth/online_attempt.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "chromeos/login/auth/auth_attempt_state.h" | 15 #include "chromeos/login/auth/auth_attempt_state.h" |
| 14 #include "chromeos/login/auth/auth_attempt_state_resolver.h" | 16 #include "chromeos/login/auth/auth_attempt_state_resolver.h" |
| 15 #include "chromeos/login/auth/key.h" | 17 #include "chromeos/login/auth/key.h" |
| 16 #include "chromeos/login/auth/user_context.h" | 18 #include "chromeos/login/auth/user_context.h" |
| 17 #include "components/user_manager/user_type.h" | 19 #include "components/user_manager/user_type.h" |
| 18 #include "content/public/browser/browser_context.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "google_apis/gaia/gaia_auth_consumer.h" | 20 #include "google_apis/gaia/gaia_auth_consumer.h" |
| 21 #include "google_apis/gaia/gaia_auth_fetcher.h" | 21 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 22 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 25 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | |
| 28 | |
| 29 namespace chromeos { | 27 namespace chromeos { |
| 30 | 28 |
| 31 // static | 29 // static |
| 32 const int OnlineAttempt::kClientLoginTimeoutMs = 10000; | 30 const int OnlineAttempt::kClientLoginTimeoutMs = 10000; |
| 33 | 31 |
| 34 OnlineAttempt::OnlineAttempt(AuthAttemptState* current_attempt, | 32 OnlineAttempt::OnlineAttempt(AuthAttemptState* current_attempt, |
| 35 AuthAttemptStateResolver* callback) | 33 AuthAttemptStateResolver* callback) |
| 36 : attempt_(current_attempt), | 34 : message_loop_(base::MessageLoopProxy::current()), |
| 35 attempt_(current_attempt), |
| 37 resolver_(callback), | 36 resolver_(callback), |
| 38 weak_factory_(this), | 37 weak_factory_(this), |
| 39 try_again_(true) { | 38 try_again_(true) { |
| 40 DCHECK(attempt_->user_type == user_manager::USER_TYPE_REGULAR); | 39 DCHECK(attempt_->user_type == user_manager::USER_TYPE_REGULAR); |
| 41 } | 40 } |
| 42 | 41 |
| 43 OnlineAttempt::~OnlineAttempt() { | 42 OnlineAttempt::~OnlineAttempt() { |
| 44 // Just to be sure. | 43 // Just to be sure. |
| 45 if (client_fetcher_.get()) | 44 if (client_fetcher_.get()) |
| 46 client_fetcher_->CancelRequest(); | 45 client_fetcher_->CancelRequest(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 void OnlineAttempt::Initiate(content::BrowserContext* auth_context) { | 48 void OnlineAttempt::Initiate(net::URLRequestContextGetter* request_context) { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 49 client_fetcher_.reset(new GaiaAuthFetcher( |
| 51 client_fetcher_.reset( | 50 this, GaiaConstants::kChromeOSSource, request_context)); |
| 52 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, | 51 message_loop_->PostTask( |
| 53 auth_context->GetRequestContext())); | 52 FROM_HERE, |
| 54 BrowserThread::PostTask( | |
| 55 BrowserThread::UI, FROM_HERE, | |
| 56 base::Bind(&OnlineAttempt::TryClientLogin, weak_factory_.GetWeakPtr())); | 53 base::Bind(&OnlineAttempt::TryClientLogin, weak_factory_.GetWeakPtr())); |
| 57 } | 54 } |
| 58 | 55 |
| 59 void OnlineAttempt::OnClientLoginSuccess( | 56 void OnlineAttempt::OnClientLoginSuccess( |
| 60 const GaiaAuthConsumer::ClientLoginResult& unused) { | 57 const GaiaAuthConsumer::ClientLoginResult& unused) { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 62 VLOG(1) << "Online login successful!"; | 58 VLOG(1) << "Online login successful!"; |
| 63 | 59 |
| 64 weak_factory_.InvalidateWeakPtrs(); | 60 weak_factory_.InvalidateWeakPtrs(); |
| 65 | 61 |
| 66 if (attempt_->hosted_policy() == GaiaAuthFetcher::HostedAccountsAllowed && | 62 if (attempt_->hosted_policy() == GaiaAuthFetcher::HostedAccountsAllowed && |
| 67 attempt_->is_first_time_user()) { | 63 attempt_->is_first_time_user()) { |
| 68 // First time user, and we don't know if the account is HOSTED or not. | 64 // First time user, and we don't know if the account is HOSTED or not. |
| 69 // Since we don't allow HOSTED accounts to log in, we need to try | 65 // Since we don't allow HOSTED accounts to log in, we need to try |
| 70 // again, without allowing HOSTED accounts. | 66 // again, without allowing HOSTED accounts. |
| 71 // | 67 // |
| 72 // NOTE: we used to do this in the opposite order, so that we'd only | 68 // NOTE: we used to do this in the opposite order, so that we'd only |
| 73 // try the HOSTED pathway if GOOGLE-only failed. This breaks CAPTCHA | 69 // try the HOSTED pathway if GOOGLE-only failed. This breaks CAPTCHA |
| 74 // handling, though. | 70 // handling, though. |
| 75 attempt_->DisableHosted(); | 71 attempt_->DisableHosted(); |
| 76 TryClientLogin(); | 72 TryClientLogin(); |
| 77 return; | 73 return; |
| 78 } | 74 } |
| 79 TriggerResolve(AuthFailure::AuthFailureNone()); | 75 TriggerResolve(AuthFailure::AuthFailureNone()); |
| 80 } | 76 } |
| 81 | 77 |
| 82 void OnlineAttempt::OnClientLoginFailure( | 78 void OnlineAttempt::OnClientLoginFailure(const GoogleServiceAuthError& error) { |
| 83 const GoogleServiceAuthError& error) { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 85 | |
| 86 weak_factory_.InvalidateWeakPtrs(); | 79 weak_factory_.InvalidateWeakPtrs(); |
| 87 | 80 |
| 88 if (error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { | 81 if (error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { |
| 89 if (try_again_) { | 82 if (try_again_) { |
| 90 try_again_ = false; | 83 try_again_ = false; |
| 91 // TODO(cmasone): add UMA tracking for this to see if we can remove it. | 84 // TODO(cmasone): add UMA tracking for this to see if we can remove it. |
| 92 LOG(ERROR) << "Login attempt canceled!?!? Trying again."; | 85 LOG(ERROR) << "Login attempt canceled!?!? Trying again."; |
| 93 TryClientLogin(); | 86 TryClientLogin(); |
| 94 return; | 87 return; |
| 95 } | 88 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 112 LOG(WARNING) << "Two factor authenticated. Sync will not work."; | 105 LOG(WARNING) << "Two factor authenticated. Sync will not work."; |
| 113 TriggerResolve(AuthFailure::AuthFailureNone()); | 106 TriggerResolve(AuthFailure::AuthFailureNone()); |
| 114 | 107 |
| 115 return; | 108 return; |
| 116 } | 109 } |
| 117 VLOG(2) << "ClientLogin attempt failed with " << error.state(); | 110 VLOG(2) << "ClientLogin attempt failed with " << error.state(); |
| 118 TriggerResolve(AuthFailure::FromNetworkAuthFailure(error)); | 111 TriggerResolve(AuthFailure::FromNetworkAuthFailure(error)); |
| 119 } | 112 } |
| 120 | 113 |
| 121 void OnlineAttempt::TryClientLogin() { | 114 void OnlineAttempt::TryClientLogin() { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 message_loop_->PostDelayedTask( |
| 123 | 116 FROM_HERE, |
| 124 BrowserThread::PostDelayedTask( | |
| 125 BrowserThread::UI, FROM_HERE, | |
| 126 base::Bind(&OnlineAttempt::CancelClientLogin, weak_factory_.GetWeakPtr()), | 117 base::Bind(&OnlineAttempt::CancelClientLogin, weak_factory_.GetWeakPtr()), |
| 127 base::TimeDelta::FromMilliseconds(kClientLoginTimeoutMs)); | 118 base::TimeDelta::FromMilliseconds(kClientLoginTimeoutMs)); |
| 128 | 119 |
| 129 client_fetcher_->StartClientLogin( | 120 client_fetcher_->StartClientLogin( |
| 130 attempt_->user_context.GetUserID(), | 121 attempt_->user_context.GetUserID(), |
| 131 attempt_->user_context.GetKey()->GetSecret(), | 122 attempt_->user_context.GetKey()->GetSecret(), |
| 132 GaiaConstants::kSyncService, | 123 GaiaConstants::kSyncService, |
| 133 attempt_->login_token, | 124 attempt_->login_token, |
| 134 attempt_->login_captcha, | 125 attempt_->login_captcha, |
| 135 attempt_->hosted_policy()); | 126 attempt_->hosted_policy()); |
| 136 } | 127 } |
| 137 | 128 |
| 138 bool OnlineAttempt::HasPendingFetch() { | 129 bool OnlineAttempt::HasPendingFetch() { |
| 139 return client_fetcher_->HasPendingFetch(); | 130 return client_fetcher_->HasPendingFetch(); |
| 140 } | 131 } |
| 141 | 132 |
| 142 void OnlineAttempt::CancelRequest() { | 133 void OnlineAttempt::CancelRequest() { |
| 143 weak_factory_.InvalidateWeakPtrs(); | 134 weak_factory_.InvalidateWeakPtrs(); |
| 144 } | 135 } |
| 145 | 136 |
| 146 void OnlineAttempt::CancelClientLogin() { | 137 void OnlineAttempt::CancelClientLogin() { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 148 if (HasPendingFetch()) { | 138 if (HasPendingFetch()) { |
| 149 LOG(WARNING) << "Canceling ClientLogin attempt."; | 139 LOG(WARNING) << "Canceling ClientLogin attempt."; |
| 150 CancelRequest(); | 140 CancelRequest(); |
| 151 | 141 |
| 152 TriggerResolve(AuthFailure(AuthFailure::LOGIN_TIMED_OUT)); | 142 TriggerResolve(AuthFailure(AuthFailure::LOGIN_TIMED_OUT)); |
| 153 } | 143 } |
| 154 } | 144 } |
| 155 | 145 |
| 156 void OnlineAttempt::TriggerResolve(const AuthFailure& outcome) { | 146 void OnlineAttempt::TriggerResolve(const AuthFailure& outcome) { |
| 157 attempt_->RecordOnlineLoginStatus(outcome); | 147 attempt_->RecordOnlineLoginStatus(outcome); |
| 158 client_fetcher_.reset(NULL); | 148 client_fetcher_.reset(NULL); |
| 159 resolver_->Resolve(); | 149 resolver_->Resolve(); |
| 160 } | 150 } |
| 161 | 151 |
| 162 } // namespace chromeos | 152 } // namespace chromeos |
| OLD | NEW |