| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/net/gaia/gaia_oauth_client.h" | 5 #include "chrome/common/net/gaia/gaia_oauth_client.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/net/http_return.h" | 10 #include "chrome/common/net/http_return.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, | 38 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, |
| 39 const std::string& auth_code, | 39 const std::string& auth_code, |
| 40 int max_retries, | 40 int max_retries, |
| 41 GaiaOAuthClient::Delegate* delegate); | 41 GaiaOAuthClient::Delegate* delegate); |
| 42 void RefreshToken(const OAuthClientInfo& oauth_client_info, | 42 void RefreshToken(const OAuthClientInfo& oauth_client_info, |
| 43 const std::string& refresh_token, | 43 const std::string& refresh_token, |
| 44 int max_retries, | 44 int max_retries, |
| 45 GaiaOAuthClient::Delegate* delegate); | 45 GaiaOAuthClient::Delegate* delegate); |
| 46 | 46 |
| 47 // content::URLFetcherDelegate implementation. | 47 // content::URLFetcherDelegate implementation. |
| 48 virtual void OnURLFetchComplete(const URLFetcher* source); | 48 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 void MakeGaiaRequest(std::string post_body, | 51 void MakeGaiaRequest(std::string post_body, |
| 52 int max_retries, | 52 int max_retries, |
| 53 GaiaOAuthClient::Delegate* delegate); | 53 GaiaOAuthClient::Delegate* delegate); |
| 54 void HandleResponse(const URLFetcher* source, bool* should_retry_request); | 54 void HandleResponse(const content::URLFetcher* source, |
| 55 bool* should_retry_request); |
| 55 | 56 |
| 56 GURL gaia_url_; | 57 GURL gaia_url_; |
| 57 int num_retries_; | 58 int num_retries_; |
| 58 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 59 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 59 GaiaOAuthClient::Delegate* delegate_; | 60 GaiaOAuthClient::Delegate* delegate_; |
| 60 scoped_ptr<URLFetcher> request_; | 61 scoped_ptr<content::URLFetcher> request_; |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 void GaiaOAuthClient::Core::GetTokensFromAuthCode( | 64 void GaiaOAuthClient::Core::GetTokensFromAuthCode( |
| 64 const OAuthClientInfo& oauth_client_info, | 65 const OAuthClientInfo& oauth_client_info, |
| 65 const std::string& auth_code, | 66 const std::string& auth_code, |
| 66 int max_retries, | 67 int max_retries, |
| 67 GaiaOAuthClient::Delegate* delegate) { | 68 GaiaOAuthClient::Delegate* delegate) { |
| 68 std::string post_body = | 69 std::string post_body = |
| 69 "code=" + EscapeUrlEncodedData(auth_code, true) + | 70 "code=" + EscapeUrlEncodedData(auth_code, true) + |
| 70 "&client_id=" + EscapeUrlEncodedData(oauth_client_info.client_id, true) + | 71 "&client_id=" + EscapeUrlEncodedData(oauth_client_info.client_id, true) + |
| (...skipping 18 matching lines...) Expand all Loading... |
| 89 } | 90 } |
| 90 | 91 |
| 91 void GaiaOAuthClient::Core::MakeGaiaRequest( | 92 void GaiaOAuthClient::Core::MakeGaiaRequest( |
| 92 std::string post_body, | 93 std::string post_body, |
| 93 int max_retries, | 94 int max_retries, |
| 94 GaiaOAuthClient::Delegate* delegate) { | 95 GaiaOAuthClient::Delegate* delegate) { |
| 95 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; | 96 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; |
| 96 delegate_ = delegate; | 97 delegate_ = delegate; |
| 97 num_retries_ = 0; | 98 num_retries_ = 0; |
| 98 request_.reset(URLFetcher::Create(0, gaia_url_, URLFetcher::POST, this)); | 99 request_.reset(URLFetcher::Create(0, gaia_url_, URLFetcher::POST, this)); |
| 99 request_->set_request_context(request_context_getter_); | 100 request_->SetRequestContext(request_context_getter_); |
| 100 request_->set_upload_data("application/x-www-form-urlencoded", post_body); | 101 request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
| 101 request_->set_max_retries(max_retries); | 102 request_->SetMaxRetries(max_retries); |
| 102 request_->Start(); | 103 request_->Start(); |
| 103 } | 104 } |
| 104 | 105 |
| 105 // URLFetcher::Delegate implementation. | 106 // URLFetcher::Delegate implementation. |
| 106 void GaiaOAuthClient::Core::OnURLFetchComplete(const URLFetcher* source) { | 107 void GaiaOAuthClient::Core::OnURLFetchComplete( |
| 108 const content::URLFetcher* source) { |
| 107 bool should_retry = false; | 109 bool should_retry = false; |
| 108 HandleResponse(source, &should_retry); | 110 HandleResponse(source, &should_retry); |
| 109 if (should_retry) { | 111 if (should_retry) { |
| 110 // Explicitly call ReceivedContentWasMalformed() to ensure the current | 112 // Explicitly call ReceivedContentWasMalformed() to ensure the current |
| 111 // request gets counted as a failure for calculation of the back-off | 113 // request gets counted as a failure for calculation of the back-off |
| 112 // period. If it was already a failure by status code, this call will | 114 // period. If it was already a failure by status code, this call will |
| 113 // be ignored. | 115 // be ignored. |
| 114 request_->ReceivedContentWasMalformed(); | 116 request_->ReceivedContentWasMalformed(); |
| 115 num_retries_++; | 117 num_retries_++; |
| 116 // We must set our request_context_getter_ again because | 118 // We must set our request_context_getter_ again because |
| 117 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... | 119 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... |
| 118 request_->set_request_context(request_context_getter_); | 120 request_->SetRequestContext(request_context_getter_); |
| 119 request_->Start(); | 121 request_->Start(); |
| 120 } else { | 122 } else { |
| 121 request_.reset(); | 123 request_.reset(); |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 | 126 |
| 125 void GaiaOAuthClient::Core::HandleResponse( | 127 void GaiaOAuthClient::Core::HandleResponse( |
| 126 const URLFetcher* source, | 128 const content::URLFetcher* source, |
| 127 bool* should_retry_request) { | 129 bool* should_retry_request) { |
| 128 *should_retry_request = false; | 130 *should_retry_request = false; |
| 129 // RC_BAD_REQUEST means the arguments are invalid. No point retrying. We are | 131 // RC_BAD_REQUEST means the arguments are invalid. No point retrying. We are |
| 130 // done here. | 132 // done here. |
| 131 if (source->response_code() == RC_BAD_REQUEST) { | 133 if (source->GetResponseCode() == RC_BAD_REQUEST) { |
| 132 delegate_->OnOAuthError(); | 134 delegate_->OnOAuthError(); |
| 133 return; | 135 return; |
| 134 } | 136 } |
| 135 std::string access_token; | 137 std::string access_token; |
| 136 std::string refresh_token; | 138 std::string refresh_token; |
| 137 int expires_in_seconds = 0; | 139 int expires_in_seconds = 0; |
| 138 if (source->response_code() == RC_REQUEST_OK) { | 140 if (source->GetResponseCode() == RC_REQUEST_OK) { |
| 139 std::string data; | 141 std::string data; |
| 140 source->GetResponseAsString(&data); | 142 source->GetResponseAsString(&data); |
| 141 scoped_ptr<Value> message_value(base::JSONReader::Read(data, false)); | 143 scoped_ptr<Value> message_value(base::JSONReader::Read(data, false)); |
| 142 if (message_value.get() && | 144 if (message_value.get() && |
| 143 message_value->IsType(Value::TYPE_DICTIONARY)) { | 145 message_value->IsType(Value::TYPE_DICTIONARY)) { |
| 144 scoped_ptr<DictionaryValue> response_dict( | 146 scoped_ptr<DictionaryValue> response_dict( |
| 145 static_cast<DictionaryValue*>(message_value.release())); | 147 static_cast<DictionaryValue*>(message_value.release())); |
| 146 response_dict->GetString(kAccessTokenValue, &access_token); | 148 response_dict->GetString(kAccessTokenValue, &access_token); |
| 147 response_dict->GetString(kRefreshTokenValue, &refresh_token); | 149 response_dict->GetString(kRefreshTokenValue, &refresh_token); |
| 148 response_dict->GetInteger(kExpiresInValue, &expires_in_seconds); | 150 response_dict->GetInteger(kExpiresInValue, &expires_in_seconds); |
| 149 } | 151 } |
| 150 } | 152 } |
| 151 if (access_token.empty()) { | 153 if (access_token.empty()) { |
| 152 // If we don't have an access token yet and the the error was not | 154 // If we don't have an access token yet and the the error was not |
| 153 // RC_BAD_REQUEST, we may need to retry. | 155 // RC_BAD_REQUEST, we may need to retry. |
| 154 if ((-1 != source->max_retries()) && | 156 if ((-1 != source->GetMaxRetries()) && |
| 155 (num_retries_ > source->max_retries())) { | 157 (num_retries_ > source->GetMaxRetries())) { |
| 156 // Retry limit reached. Give up. | 158 // Retry limit reached. Give up. |
| 157 delegate_->OnNetworkError(source->response_code()); | 159 delegate_->OnNetworkError(source->GetResponseCode()); |
| 158 } else { | 160 } else { |
| 159 *should_retry_request = true; | 161 *should_retry_request = true; |
| 160 } | 162 } |
| 161 } else if (refresh_token.empty()) { | 163 } else if (refresh_token.empty()) { |
| 162 // If we only have an access token, then this was a refresh request. | 164 // If we only have an access token, then this was a refresh request. |
| 163 delegate_->OnRefreshTokenResponse(access_token, expires_in_seconds); | 165 delegate_->OnRefreshTokenResponse(access_token, expires_in_seconds); |
| 164 } else { | 166 } else { |
| 165 delegate_->OnGetTokensResponse(refresh_token, | 167 delegate_->OnGetTokensResponse(refresh_token, |
| 166 access_token, | 168 access_token, |
| 167 expires_in_seconds); | 169 expires_in_seconds); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 192 const std::string& refresh_token, | 194 const std::string& refresh_token, |
| 193 int max_retries, | 195 int max_retries, |
| 194 Delegate* delegate) { | 196 Delegate* delegate) { |
| 195 return core_->RefreshToken(oauth_client_info, | 197 return core_->RefreshToken(oauth_client_info, |
| 196 refresh_token, | 198 refresh_token, |
| 197 max_retries, | 199 max_retries, |
| 198 delegate); | 200 delegate); |
| 199 } | 201 } |
| 200 | 202 |
| 201 } // namespace gaia | 203 } // namespace gaia |
| OLD | NEW |