Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/policy/upload_job_impl.h" | 5 #include "chrome/browser/chromeos/policy/upload_job_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Format for bearer tokens in HTTP requests to access OAuth 2.0 protected | 24 // Format for bearer tokens in HTTP requests to access OAuth 2.0 protected |
| 25 // resources. | 25 // resources. |
| 26 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | 26 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
| 27 | 27 |
| 28 // Value the "Content-Type" field will be set to in the POST request. | 28 // Value the "Content-Type" field will be set to in the POST request. |
| 29 const char kUploadContentType[] = "multipart/form-data"; | 29 const char kUploadContentType[] = "multipart/form-data"; |
| 30 | 30 |
| 31 // Number of upload retries. | 31 // Number of upload tries. |
|
Andrew T Wilson (Slow)
2016/04/08 15:42:56
nit: tries -> attempts
Marton Hunyady
2016/04/08 16:47:46
Done.
| |
| 32 const int kMaxRetries = 1; | 32 const int kMaxRetries = 3; |
| 33 | 33 |
| 34 // Max size of MIME boundary according to RFC 1341, section 7.2.1. | 34 // Max size of MIME boundary according to RFC 1341, section 7.2.1. |
| 35 const size_t kMaxMimeBoundarySize = 70; | 35 const size_t kMaxMimeBoundarySize = 70; |
| 36 | 36 |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 UploadJobImpl::Delegate::~Delegate() { | 39 UploadJobImpl::Delegate::~Delegate() { |
| 40 } | 40 } |
| 41 | 41 |
| 42 UploadJobImpl::MimeBoundaryGenerator::~MimeBoundaryGenerator() { | 42 UploadJobImpl::MimeBoundaryGenerator::~MimeBoundaryGenerator() { |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 const OAuth2TokenService::Request* request, | 291 const OAuth2TokenService::Request* request, |
| 292 const GoogleServiceAuthError& error) { | 292 const GoogleServiceAuthError& error) { |
| 293 DCHECK_EQ(ACQUIRING_TOKEN, state_); | 293 DCHECK_EQ(ACQUIRING_TOKEN, state_); |
| 294 DCHECK_EQ(access_token_request_.get(), request); | 294 DCHECK_EQ(access_token_request_.get(), request); |
| 295 access_token_request_.reset(); | 295 access_token_request_.reset(); |
| 296 LOG(ERROR) << "Token request failed: " << error.ToString(); | 296 LOG(ERROR) << "Token request failed: " << error.ToString(); |
| 297 state_ = ERROR; | 297 state_ = ERROR; |
| 298 delegate_->OnFailure(AUTHENTICATION_ERROR); | 298 delegate_->OnFailure(AUTHENTICATION_ERROR); |
| 299 } | 299 } |
| 300 | 300 |
| 301 void UploadJobImpl::HandleError(ErrorCode errorCode) { | |
| 302 retry_++; | |
| 303 upload_fetcher_.reset(); | |
| 304 if (retry_ >= kMaxRetries) { | |
| 305 // Maximum number of tries reached, failure | |
| 306 access_token_.clear(); | |
| 307 post_data_.reset(); | |
| 308 state_ = ERROR; | |
| 309 delegate_->OnFailure(errorCode); | |
| 310 } else { | |
| 311 if (errorCode == AUTHENTICATION_ERROR) { | |
| 312 // Request new token and retry | |
|
Andrew T Wilson (Slow)
2016/04/08 15:42:55
nit: if your comment is a full sentence, you shoul
Marton Hunyady
2016/04/08 16:47:46
Done.
| |
| 313 OAuth2TokenService::ScopeSet scope_set; | |
| 314 scope_set.insert(GaiaConstants::kDeviceManagementServiceOAuth); | |
| 315 token_service_->InvalidateAccessToken(account_id_, scope_set, | |
| 316 access_token_); | |
| 317 access_token_.clear(); | |
| 318 RequestAccessToken(); | |
| 319 } else { | |
| 320 // Retry without a new token | |
| 321 state_ = ACQUIRING_TOKEN; | |
| 322 StartUpload(access_token_); | |
| 323 } | |
| 324 } | |
| 325 } | |
| 326 | |
| 301 void UploadJobImpl::OnURLFetchComplete(const net::URLFetcher* source) { | 327 void UploadJobImpl::OnURLFetchComplete(const net::URLFetcher* source) { |
| 302 DCHECK_EQ(upload_fetcher_.get(), source); | 328 DCHECK_EQ(upload_fetcher_.get(), source); |
| 329 DCHECK_EQ(UPLOADING, state_); | |
| 303 const net::URLRequestStatus& status = source->GetStatus(); | 330 const net::URLRequestStatus& status = source->GetStatus(); |
| 304 if (!status.is_success()) { | 331 if (!status.is_success()) { |
| 305 LOG(ERROR) << "URLRequestStatus error " << status.error(); | 332 LOG(ERROR) << "URLRequestStatus error " << status.error(); |
| 306 upload_fetcher_.reset(); | 333 HandleError(NETWORK_ERROR); |
| 307 state_ = ERROR; | 334 } else { |
| 308 post_data_.reset(); | 335 const int response_code = source->GetResponseCode(); |
| 309 delegate_->OnFailure(NETWORK_ERROR); | 336 if (response_code == net::HTTP_OK) { |
| 310 return; | 337 // Successful uploading |
| 311 } | |
| 312 | |
| 313 const int response_code = source->GetResponseCode(); | |
| 314 const bool success = response_code == net::HTTP_OK; | |
| 315 if (!success) | |
| 316 LOG(ERROR) << "POST request failed with HTTP status code " << response_code; | |
| 317 | |
| 318 if (response_code == net::HTTP_UNAUTHORIZED) { | |
| 319 if (retry_ >= kMaxRetries) { | |
| 320 upload_fetcher_.reset(); | 338 upload_fetcher_.reset(); |
| 339 access_token_.clear(); | |
| 340 post_data_.reset(); | |
| 341 state_ = SUCCESS; | |
| 342 delegate_->OnSuccess(); | |
| 343 } else if (response_code == net::HTTP_UNAUTHORIZED) { | |
| 321 LOG(ERROR) << "Unauthorized request."; | 344 LOG(ERROR) << "Unauthorized request."; |
| 322 state_ = ERROR; | 345 HandleError(AUTHENTICATION_ERROR); |
| 323 post_data_.reset(); | 346 } else { |
| 324 delegate_->OnFailure(AUTHENTICATION_ERROR); | 347 LOG(ERROR) << "POST request failed with HTTP status code " |
| 325 return; | 348 << response_code; |
| 349 HandleError(SERVER_ERROR); | |
| 326 } | 350 } |
| 327 retry_++; | |
| 328 upload_fetcher_.reset(); | |
| 329 OAuth2TokenService::ScopeSet scope_set; | |
| 330 scope_set.insert(GaiaConstants::kDeviceManagementServiceOAuth); | |
| 331 token_service_->InvalidateAccessToken(account_id_, scope_set, | |
| 332 access_token_); | |
| 333 access_token_.clear(); | |
| 334 RequestAccessToken(); | |
| 335 return; | |
| 336 } | |
| 337 | |
| 338 upload_fetcher_.reset(); | |
| 339 access_token_.clear(); | |
| 340 upload_fetcher_.reset(); | |
| 341 post_data_.reset(); | |
| 342 if (success) { | |
| 343 state_ = SUCCESS; | |
| 344 delegate_->OnSuccess(); | |
| 345 } else { | |
| 346 state_ = ERROR; | |
| 347 delegate_->OnFailure(SERVER_ERROR); | |
| 348 } | 351 } |
| 349 } | 352 } |
| 350 | 353 |
| 351 } // namespace policy | 354 } // namespace policy |
| OLD | NEW |