Chromium Code Reviews| Index: chrome/browser/chromeos/login/login_manager.cc |
| diff --git a/chrome/browser/chromeos/login/login_manager.cc b/chrome/browser/chromeos/login/login_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4587c712a75e06242f4b0965f6bfccf166316206 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/login_manager.cc |
| @@ -0,0 +1,125 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
|
xiyuan
2013/01/07 23:07:55
nit: insert an empty line
zel
2013/01/08 02:05:41
Done.
|
| +#include "chrome/browser/chromeos/login/login_manager.h" |
| + |
| +#include "base/metrics/histogram.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/signin/token_service.h" |
| +#include "chrome/browser/signin/token_service_factory.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "google_apis/gaia/gaia_auth_fetcher.h" |
| +#include "google_apis/gaia/gaia_constants.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +// OAuth token verification max retry count. |
| +const int kMaxCookieRecoveryAttemptCount = 5; |
| +// OAuth token verification retry delay in milliseconds. |
| +const int kCookieRecoveryRestartDelay = 10000; |
| + |
| +} // namespace |
| + |
| + |
| +namespace chromeos { |
| + |
| +LoginManager::LoginManager(Delegate* delegate, |
| + Profile* user_profile) |
| + : delegate_(delegate), |
| + user_profile_(user_profile), |
| + loading_reported_(false), |
| + restore_attempt_count_(0) { |
| + TokenService* token_service = |
| + TokenServiceFactory::GetForProfile(user_profile_); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, |
| + content::Source<TokenService>(token_service)); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| + content::Source<TokenService>(token_service)); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
| + content::Source<TokenService>(token_service)); |
| +} |
| + |
| +void LoginManager::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) OVERRIDE { |
| + TokenService* token_service = |
| + TokenServiceFactory::GetForProfile(user_profile_); |
| + switch (type) { |
| + case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { |
| + refresh_token_ = token_service->GetOAuth2LoginRefreshToken(); |
| + ReportTokenLoaded(); |
| + // Have we started restoring GAIA auth cookies yet? |
| + if (!refresh_token_.empty() && !login_verifier_.get()) |
| + RestoreSessionCookies(); |
| + |
| + break; |
| + } |
| + case chrome::NOTIFICATION_TOKEN_REQUEST_FAILED: { |
| + // TODO(zelidrag): Figure out how to recover from transient errors with |
| + // TokenService class - similar to what we do in RetryOnError() here. |
| + TokenService::TokenAvailableDetails* token_details = |
| + content::Details<TokenService::TokenAvailableDetails>( |
| + details).ptr(); |
| + if (token_details->service() == |
| + GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
| + delegate_->OnOAuth2RefreshTokenFetchFailed(user_profile_, token_details->service()); |
|
xiyuan
2013/01/07 23:07:55
nit: 80 cols
zel
2013/01/08 02:05:41
Done.
|
| + } |
| + break; |
| + } |
| + case chrome::NOTIFICATION_TOKEN_AVAILABLE: { |
| + TokenService::TokenAvailableDetails* token_details = |
| + content::Details<TokenService::TokenAvailableDetails>( |
| + details).ptr(); |
| + if (token_details->service() == |
| + GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
| + DCHECK(!login_verifier_.get()); |
| + refresh_token_ = token_details->token(); |
| + RestoreSessionCookies(); |
| + } |
| + |
| + break; |
| + } |
| + default: |
| + NOTREACHED(); |
| + break; |
| + } |
| +} |
| + |
| +void LoginManager::RestoreSessionCookies() { |
| + LOG(WARNING) << "Fetched refresh token!"; |
|
xiyuan
2013/01/07 23:07:55
nit: VLOG(1) instead of LOG(WARNING)?
|
| + DCHECK(!refresh_token_.empty()); |
| + if (!login_verifier_.get()) { |
| + login_verifier_.reset( |
| + new OAuth2LoginVerifier(this, |
| + g_browser_process->system_request_context(), |
| + user_profile_)); |
| + } |
| + login_verifier_->VerifyRefreshToken(refresh_token_); |
| +} |
| + |
| +void LoginManager::OnOAuth2LoginVerifierSuccess( |
| + const std::string& sid, const std::string& lsid, const std::string& auth) { |
|
xiyuan
2013/01/07 23:07:55
nit: put the args into their own lines.
zel
2013/01/08 02:05:41
Done.
|
| + delegate_->OnCookiesRestoreSuccess(user_profile_); |
| +} |
| + |
| +void LoginManager::OnOAuth2LoginVerifierFaulure() { |
| + delegate_->OnCookiesRestoreFailure(user_profile_); |
| +} |
| + |
| +void LoginManager::ReportTokenLoaded() { |
| + LOG(WARNING) << "Got OAuth2 refresh token!"; |
|
xiyuan
2013/01/07 23:07:55
nit: VLOG(1) instead of LOG(WARNING)?
|
| + DCHECK(!loading_reported_); |
| + loading_reported_ = true; |
| + delegate_->OnOAuth2RefreshTokenLoaded(user_profile_, refresh_token_); |
| +} |
| + |
| +} // namespace chromeos |