Chromium Code Reviews| 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/oauth2_policy_fetcher.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/policy/browser_policy_connector.h" | |
| 11 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/profiles/profile_manager.h" | |
|
Joao da Silva
2013/01/11 16:45:07
These 2 not needed
zel
2013/01/11 19:51:16
Done.
| |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
| 16 #include "google_apis/gaia/gaia_constants.h" | |
| 17 #include "google_apis/gaia/gaia_urls.h" | |
| 18 #include "google_apis/gaia/google_service_auth_error.h" | |
| 19 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Max retry count for token fetching requests. | |
| 28 const int kMaxRequestAttemptCount = 5; | |
| 29 | |
| 30 // OAuth token request retry delay in milliseconds. | |
| 31 const int kRequestRestartDelay = 3000; | |
| 32 | |
| 33 const char kServiceScopeChromeOSDeviceManagement[] = | |
| 34 "https://www.googleapis.com/auth/chromeosdevicemanagement"; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 OAuth2PolicyFetcher::OAuth2PolicyFetcher( | |
| 39 net::URLRequestContextGetter* auth_context_getter, | |
| 40 net::URLRequestContextGetter* system_context_getter) | |
| 41 : refresh_token_fetcher_( | |
| 42 new GaiaAuthFetcher(this, | |
| 43 GaiaConstants::kChromeSource, | |
| 44 auth_context_getter)), | |
| 45 access_token_fetcher_( | |
| 46 new OAuth2AccessTokenFetcher(this, system_context_getter)), | |
| 47 retry_count_(0), | |
| 48 failed_(false) { | |
| 49 } | |
| 50 | |
| 51 OAuth2PolicyFetcher::OAuth2PolicyFetcher( | |
| 52 net::URLRequestContextGetter* system_context_getter, | |
| 53 const std::string& oauth2_refresh_token) | |
| 54 : access_token_fetcher_( | |
| 55 new OAuth2AccessTokenFetcher(this, system_context_getter)), | |
| 56 oauth2_refresh_token_(oauth2_refresh_token), | |
| 57 retry_count_(0), | |
| 58 failed_(false) { | |
| 59 } | |
| 60 | |
| 61 OAuth2PolicyFetcher::~OAuth2PolicyFetcher() { | |
| 62 } | |
| 63 | |
| 64 void OAuth2PolicyFetcher::Start() { | |
| 65 retry_count_ = 0; | |
| 66 if (oauth2_refresh_token_.empty()) { | |
| 67 StartFetchingRefreshToken(); | |
| 68 } else { | |
| 69 StartFetchingAccessToken(); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void OAuth2PolicyFetcher::StartFetchingRefreshToken() { | |
| 74 DCHECK(refresh_token_fetcher_.get()); | |
| 75 refresh_token_fetcher_->StartCookieForOAuthLoginTokenExchange(EmptyString()); | |
| 76 } | |
| 77 | |
| 78 void OAuth2PolicyFetcher::StartFetchingAccessToken() { | |
| 79 std::vector<std::string> scopes; | |
|
Joao da Silva
2013/01/11 16:45:07
#include <vector>
zel
2013/01/11 19:51:16
Done.
| |
| 80 scopes.push_back(kServiceScopeChromeOSDeviceManagement); | |
| 81 access_token_fetcher_->Start( | |
| 82 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | |
| 83 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | |
| 84 oauth2_refresh_token_, | |
| 85 scopes); | |
| 86 } | |
| 87 | |
| 88 void OAuth2PolicyFetcher::OnClientOAuthSuccess( | |
| 89 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { | |
| 90 oauth2_tokens_ = oauth2_tokens; | |
| 91 oauth2_refresh_token_ = oauth2_tokens.refresh_token; | |
| 92 retry_count_ = 0; | |
| 93 StartFetchingAccessToken(); | |
| 94 } | |
| 95 | |
| 96 void OAuth2PolicyFetcher::OnClientOAuthFailure( | |
| 97 const GoogleServiceAuthError& error) { | |
| 98 RetryOnError(error, | |
| 99 base::Bind(&OAuth2PolicyFetcher::StartFetchingRefreshToken, | |
| 100 AsWeakPtr())); | |
| 101 } | |
| 102 | |
| 103 void OAuth2PolicyFetcher::OnGetTokenSuccess( | |
| 104 const std::string& access_token, | |
| 105 const base::Time& expiration_time) { | |
| 106 VLOG(1) << "Got OAuth access token for device management service"; | |
| 107 SetPolicyToken(access_token); | |
| 108 } | |
| 109 | |
| 110 void OAuth2PolicyFetcher::OnGetTokenFailure( | |
| 111 const GoogleServiceAuthError& error) { | |
| 112 RetryOnError(error, | |
| 113 base::Bind(&OAuth2PolicyFetcher::StartFetchingAccessToken, | |
| 114 AsWeakPtr())); | |
| 115 } | |
| 116 | |
| 117 void OAuth2PolicyFetcher::RetryOnError(const GoogleServiceAuthError& error, | |
| 118 const base::Closure& task) { | |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 120 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || | |
| 121 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || | |
| 122 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && | |
| 123 retry_count_ < kMaxRequestAttemptCount) { | |
| 124 retry_count_++; | |
| 125 BrowserThread::PostDelayedTask( | |
| 126 BrowserThread::UI, FROM_HERE, task, | |
| 127 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | |
| 128 return; | |
| 129 } | |
| 130 LOG(WARNING) << "Unrecoverable error or retry count max reached."; | |
| 131 SetPolicyToken(EmptyString()); | |
| 132 failed_ = true; | |
| 133 } | |
| 134 | |
| 135 void OAuth2PolicyFetcher::SetPolicyToken(const std::string& token) { | |
| 136 policy_token_ = token; | |
| 137 | |
| 138 policy::BrowserPolicyConnector* browser_policy_connector = | |
| 139 g_browser_process->browser_policy_connector(); | |
| 140 browser_policy_connector->RegisterForUserPolicy(token); | |
| 141 | |
| 142 policy::UserCloudPolicyManagerChromeOS* cloud_policy_manager = | |
| 143 browser_policy_connector->GetUserCloudPolicyManager(); | |
| 144 if (cloud_policy_manager) { | |
| 145 if (token.empty()) | |
| 146 cloud_policy_manager->CancelWaitForPolicyFetch(); | |
| 147 else | |
| 148 cloud_policy_manager->RegisterClient(token); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 } // namespace chromeos | |
| OLD | NEW |