OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/ref_counted.h" |
| 10 #include "base/scoped_ptr.h" |
| 11 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/chromeos/cros/cros_library.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/profile.h" |
| 16 #include "chrome/browser/profile_manager.h" |
| 17 #include "chrome/common/net/gaia/gaia_authenticator2.h" |
| 18 #include "chrome/common/net/gaia/gaia_constants.h" |
| 19 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
| 20 #include "net/base/load_flags.h" |
| 21 #include "net/base/net_errors.h" |
| 22 #include "net/url_request/url_request_status.h" |
| 23 #include "third_party/libjingle/source/talk/base/urlencode.h" |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 // static |
| 28 const int OnlineAttempt::kClientLoginTimeoutMs = 10000; |
| 29 |
| 30 OnlineAttempt::OnlineAttempt(AuthAttemptState* current_attempt, |
| 31 AuthAttemptStateResolver* callback) |
| 32 : attempt_(current_attempt), |
| 33 resolver_(callback), |
| 34 fetch_canceler_(NULL), |
| 35 try_again_(true) { |
| 36 CHECK(chromeos::CrosLibrary::Get()->EnsureLoaded()); |
| 37 } |
| 38 |
| 39 OnlineAttempt::~OnlineAttempt() {} |
| 40 |
| 41 void OnlineAttempt::Initiate(Profile* profile) { |
| 42 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 43 gaia_authenticator_.reset( |
| 44 new GaiaAuthenticator2(this, |
| 45 GaiaConstants::kChromeOSSource, |
| 46 profile->GetRequestContext())); |
| 47 TryClientLogin(); |
| 48 } |
| 49 |
| 50 void OnlineAttempt::OnClientLoginSuccess( |
| 51 const GaiaAuthConsumer::ClientLoginResult& credentials) { |
| 52 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 53 LOG(INFO) << "Online login successful!"; |
| 54 if (fetch_canceler_) { |
| 55 fetch_canceler_->Cancel(); |
| 56 fetch_canceler_ = NULL; |
| 57 } |
| 58 |
| 59 TriggerResolve(credentials, LoginFailure::None()); |
| 60 } |
| 61 |
| 62 void OnlineAttempt::OnClientLoginFailure( |
| 63 const GoogleServiceAuthError& error) { |
| 64 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 65 if (fetch_canceler_) { |
| 66 fetch_canceler_->Cancel(); |
| 67 fetch_canceler_ = NULL; |
| 68 } |
| 69 if (error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { |
| 70 if (try_again_) { |
| 71 try_again_ = false; |
| 72 LOG(ERROR) << "Login attempt canceled!?!? Trying again."; |
| 73 TryClientLogin(); |
| 74 return; |
| 75 } |
| 76 LOG(ERROR) << "Login attempt canceled again? Already retried..."; |
| 77 } |
| 78 |
| 79 if (error.state() == GoogleServiceAuthError::TWO_FACTOR) { |
| 80 LOG(WARNING) << "Two factor authenticated. Sync will not work."; |
| 81 TriggerResolve(GaiaAuthConsumer::ClientLoginResult(), |
| 82 LoginFailure::None()); |
| 83 |
| 84 return; |
| 85 } |
| 86 |
| 87 TriggerResolve(GaiaAuthConsumer::ClientLoginResult(), |
| 88 LoginFailure::FromNetworkAuthFailure(error)); |
| 89 } |
| 90 |
| 91 void OnlineAttempt::TryClientLogin() { |
| 92 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 93 fetch_canceler_ = NewRunnableMethod(this, &OnlineAttempt::CancelClientLogin); |
| 94 gaia_authenticator_->StartClientLogin(attempt_->username, |
| 95 attempt_->password, |
| 96 GaiaConstants::kContactsService, |
| 97 attempt_->login_token, |
| 98 attempt_->login_captcha); |
| 99 ChromeThread::PostDelayedTask(ChromeThread::IO, FROM_HERE, |
| 100 fetch_canceler_, |
| 101 kClientLoginTimeoutMs); |
| 102 } |
| 103 |
| 104 void OnlineAttempt::CancelClientLogin() { |
| 105 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 106 if (gaia_authenticator_->HasPendingFetch()) { |
| 107 LOG(WARNING) << "Canceling ClientLogin attempt."; |
| 108 gaia_authenticator_->CancelRequest(); |
| 109 fetch_canceler_ = NULL; |
| 110 |
| 111 TriggerResolve(GaiaAuthConsumer::ClientLoginResult(), |
| 112 LoginFailure(LoginFailure::LOGIN_TIMED_OUT)); |
| 113 } |
| 114 } |
| 115 |
| 116 void OnlineAttempt::TriggerResolve( |
| 117 const GaiaAuthConsumer::ClientLoginResult& credentials, |
| 118 const LoginFailure& outcome) { |
| 119 attempt_->RecordOnlineLoginStatus(credentials, outcome); |
| 120 gaia_authenticator_.reset(NULL); |
| 121 resolver_->Resolve(); |
| 122 } |
| 123 |
| 124 } // namespace chromeos |
OLD | NEW |