Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/service/cloud_print/cloud_print_url_fetcher.h" | 5 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/common/net/http_return.h" | 9 #include "chrome/common/net/http_return.h" |
| 10 #include "chrome/common/net/url_fetcher_protect.h" | |
| 11 #include "chrome/service/cloud_print/cloud_print_consts.h" | 10 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 12 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 11 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 13 #include "chrome/service/net/service_url_request_context.h" | 12 #include "chrome/service/net/service_url_request_context.h" |
| 14 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 15 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
| 16 | 15 |
| 17 CloudPrintURLFetcher::CloudPrintURLFetcher() | 16 CloudPrintURLFetcher::CloudPrintURLFetcher() |
| 18 : delegate_(NULL), | 17 : delegate_(NULL), |
| 19 protect_entry_(NULL), | |
| 20 num_retries_(0) { | 18 num_retries_(0) { |
| 21 } | 19 } |
| 22 | 20 |
| 23 void CloudPrintURLFetcher::StartGetRequest(const GURL& url, | 21 void CloudPrintURLFetcher::StartGetRequest(const GURL& url, |
| 24 Delegate* delegate, | 22 Delegate* delegate, |
| 25 const std::string& auth_token, | 23 const std::string& auth_token, |
| 26 const std::string& retry_policy) { | 24 int max_retries) { |
| 27 StartRequestHelper(url, URLFetcher::GET, delegate, auth_token, retry_policy, | 25 StartRequestHelper(url, URLFetcher::GET, delegate, auth_token, max_retries, |
| 28 std::string(), std::string()); | 26 std::string(), std::string()); |
| 29 } | 27 } |
| 30 | 28 |
| 31 void CloudPrintURLFetcher::StartPostRequest( | 29 void CloudPrintURLFetcher::StartPostRequest( |
| 32 const GURL& url, | 30 const GURL& url, |
| 33 Delegate* delegate, | 31 Delegate* delegate, |
| 34 const std::string& auth_token, | 32 const std::string& auth_token, |
| 35 const std::string& retry_policy, | 33 int max_retries, |
| 36 const std::string& post_data_mime_type, | 34 const std::string& post_data_mime_type, |
| 37 const std::string& post_data) { | 35 const std::string& post_data) { |
| 38 StartRequestHelper(url, URLFetcher::POST, delegate, auth_token, retry_policy, | 36 StartRequestHelper(url, URLFetcher::POST, delegate, auth_token, max_retries, |
| 39 post_data_mime_type, post_data); | 37 post_data_mime_type, post_data); |
| 40 } | 38 } |
| 41 | 39 |
| 42 // URLFetcher::Delegate implementation. | 40 // URLFetcher::Delegate implementation. |
| 43 void CloudPrintURLFetcher::OnURLFetchComplete( | 41 void CloudPrintURLFetcher::OnURLFetchComplete( |
| 44 const URLFetcher* source, | 42 const URLFetcher* source, |
| 45 const GURL& url, | 43 const GURL& url, |
| 46 const URLRequestStatus& status, | 44 const URLRequestStatus& status, |
| 47 int response_code, | 45 int response_code, |
| 48 const ResponseCookies& cookies, | 46 const ResponseCookies& cookies, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 action = delegate_->HandleJSONData(source, | 79 action = delegate_->HandleJSONData(source, |
| 82 url, | 80 url, |
| 83 response_dict, | 81 response_dict, |
| 84 succeeded); | 82 succeeded); |
| 85 else | 83 else |
| 86 action = RETRY_REQUEST; | 84 action = RETRY_REQUEST; |
| 87 } | 85 } |
| 88 } | 86 } |
| 89 // Retry the request if needed. | 87 // Retry the request if needed. |
| 90 if (action == RETRY_REQUEST) { | 88 if (action == RETRY_REQUEST) { |
| 91 int64 back_off_time = | 89 // If the response code is greater than or equal to 500, then the back-off |
| 92 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::FAILURE); | 90 // period has been increased at the network level; otherwise, explicitly |
| 91 // call ReceivedContentWasMalformed() to count the current request as a | |
| 92 // failure and increase the back-off period. | |
| 93 if (response_code < 500) | |
| 94 request_->ReceivedContentWasMalformed(); | |
| 95 | |
| 93 ++num_retries_; | 96 ++num_retries_; |
| 94 int max_retries = protect_entry_->max_retries(); | 97 if ((-1 != source->max_retries()) && |
| 95 if ((-1 != max_retries) && (num_retries_ > max_retries)) { | 98 (num_retries_ > source->max_retries())) { |
| 96 // Retry limit reached. Give up. | 99 // Retry limit reached. Give up. |
| 97 delegate_->OnRequestGiveUp(); | 100 delegate_->OnRequestGiveUp(); |
| 98 } else { | 101 } else { |
| 99 // Either no retry limit specified or retry limit has not yet been | 102 // Either no retry limit specified or retry limit has not yet been |
| 100 // reached. Try again. | 103 // reached. Try again. |
| 101 MessageLoop::current()->PostDelayedTask( | 104 StartRequestNow(); |
|
sanjeevr
2010/11/22 19:09:26
We don't need StartRequestNow as a separate method
yzshen
2010/11/22 22:59:17
Done.
| |
| 102 FROM_HERE, | |
| 103 NewRunnableMethod(this, &CloudPrintURLFetcher::StartRequestNow), | |
| 104 back_off_time); | |
| 105 } | 105 } |
| 106 } else { | |
| 107 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SUCCESS); | |
| 108 } | 106 } |
| 109 } | 107 } |
| 110 | 108 |
| 111 void CloudPrintURLFetcher::StartRequestHelper( | 109 void CloudPrintURLFetcher::StartRequestHelper( |
| 112 const GURL& url, | 110 const GURL& url, |
| 113 URLFetcher::RequestType request_type, | 111 URLFetcher::RequestType request_type, |
| 114 Delegate* delegate, | 112 Delegate* delegate, |
| 115 const std::string& auth_token, | 113 const std::string& auth_token, |
| 116 const std::string& retry_policy, | 114 int max_retries, |
| 117 const std::string& post_data_mime_type, | 115 const std::string& post_data_mime_type, |
| 118 const std::string& post_data) { | 116 const std::string& post_data) { |
| 119 DCHECK(delegate); | 117 DCHECK(delegate); |
| 120 request_.reset(new URLFetcher(url, request_type, this)); | 118 request_.reset(new URLFetcher(url, request_type, this)); |
| 121 request_->set_request_context(GetRequestContextGetter()); | 119 request_->set_request_context(GetRequestContextGetter()); |
| 122 // Since we implement our own retry logic, disable the retry in URLFetcher. | 120 // Since we implement our own retry logic, disable the retry in URLFetcher. |
| 123 request_->set_automatically_retry_on_5xx(false); | 121 request_->set_automatically_retry_on_5xx(false); |
| 122 request_->set_max_retries(max_retries); | |
| 124 delegate_ = delegate; | 123 delegate_ = delegate; |
| 125 std::string headers = "Authorization: GoogleLogin auth="; | 124 std::string headers = "Authorization: GoogleLogin auth="; |
| 126 headers += auth_token; | 125 headers += auth_token; |
| 127 headers += "\r\n"; | 126 headers += "\r\n"; |
| 128 headers += kChromeCloudPrintProxyHeader; | 127 headers += kChromeCloudPrintProxyHeader; |
| 129 request_->set_extra_request_headers(headers); | 128 request_->set_extra_request_headers(headers); |
| 130 if (request_type == URLFetcher::POST) { | 129 if (request_type == URLFetcher::POST) { |
| 131 request_->set_upload_data(post_data_mime_type, post_data); | 130 request_->set_upload_data(post_data_mime_type, post_data); |
| 132 } | 131 } |
| 133 // Initialize the retry policy for this request. | 132 |
| 134 protect_entry_ = | 133 StartRequestNow(); |
| 135 URLFetcherProtectManager::GetInstance()->Register(retry_policy); | |
| 136 MessageLoop::current()->PostDelayedTask( | |
| 137 FROM_HERE, | |
| 138 NewRunnableMethod(this, &CloudPrintURLFetcher::StartRequestNow), | |
| 139 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SEND)); | |
| 140 } | 134 } |
| 141 | 135 |
| 142 void CloudPrintURLFetcher::StartRequestNow() { | 136 void CloudPrintURLFetcher::StartRequestNow() { |
| 143 request_->Start(); | 137 request_->Start(); |
| 144 } | 138 } |
| 145 | 139 |
| 146 URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { | 140 URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { |
| 147 ServiceURLRequestContextGetter* getter = | 141 ServiceURLRequestContextGetter* getter = |
| 148 new ServiceURLRequestContextGetter(); | 142 new ServiceURLRequestContextGetter(); |
| 149 // Now set up the user agent for cloudprint. | 143 // Now set up the user agent for cloudprint. |
| 150 std::string user_agent = getter->user_agent(); | 144 std::string user_agent = getter->user_agent(); |
| 151 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); | 145 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); |
| 152 getter->set_user_agent(user_agent); | 146 getter->set_user_agent(user_agent); |
| 153 return getter; | 147 return getter; |
| 154 } | 148 } |
| 155 | 149 |
| OLD | NEW |