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/service/cloud_print/cloud_print_url_fetcher.h" | 5 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h" |
6 | 6 |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.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/service/cloud_print/cloud_print_consts.h" | 10 #include "chrome/service/cloud_print/cloud_print_consts.h" |
11 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 11 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
12 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 12 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
13 #include "chrome/service/net/service_url_request_context.h" | 13 #include "chrome/service/net/service_url_request_context.h" |
14 #include "chrome/service/service_process.h" | 14 #include "chrome/service/service_process.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
17 | 17 |
| 18 const int kHTTPUnsupportedMediaType = 415; |
| 19 |
18 CloudPrintURLFetcher::CloudPrintURLFetcher() | 20 CloudPrintURLFetcher::CloudPrintURLFetcher() |
19 : delegate_(NULL), | 21 : delegate_(NULL), |
20 num_retries_(0) { | 22 num_retries_(0) { |
21 } | 23 } |
22 | 24 |
| 25 bool CloudPrintURLFetcher::IsSameRequest(const URLFetcher* source) { |
| 26 return (request_.get() == source); |
| 27 } |
| 28 |
23 void CloudPrintURLFetcher::StartGetRequest( | 29 void CloudPrintURLFetcher::StartGetRequest( |
24 const GURL& url, | 30 const GURL& url, |
25 Delegate* delegate, | 31 Delegate* delegate, |
26 int max_retries, | 32 int max_retries, |
27 const std::string& additional_headers) { | 33 const std::string& additional_headers) { |
28 StartRequestHelper(url, | 34 StartRequestHelper(url, |
29 URLFetcher::GET, | 35 URLFetcher::GET, |
30 delegate, | 36 delegate, |
31 max_retries, | 37 max_retries, |
32 std::string(), | 38 std::string(), |
(...skipping 28 matching lines...) Expand all Loading... |
61 VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << url | 67 VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << url |
62 << ", response code: " << response_code; | 68 << ", response code: " << response_code; |
63 // Make sure we stay alive through the body of this function. | 69 // Make sure we stay alive through the body of this function. |
64 scoped_refptr<CloudPrintURLFetcher> keep_alive(this); | 70 scoped_refptr<CloudPrintURLFetcher> keep_alive(this); |
65 ResponseAction action = delegate_->HandleRawResponse(source, | 71 ResponseAction action = delegate_->HandleRawResponse(source, |
66 url, | 72 url, |
67 status, | 73 status, |
68 response_code, | 74 response_code, |
69 cookies, | 75 cookies, |
70 data); | 76 data); |
| 77 |
| 78 // If we get auth error, notify delegate and check if it wants to procceed. |
| 79 if (action == CONTINUE_PROCESSING && RC_FORBIDDEN == response_code) { |
| 80 action = delegate_->OnRequestAuthError(); |
| 81 } |
| 82 |
71 if (action == CONTINUE_PROCESSING) { | 83 if (action == CONTINUE_PROCESSING) { |
72 // If we are not using an OAuth token, and we got an auth error, we are | |
73 // done. Else, the token may have been refreshed. Let us try again. | |
74 if ((RC_FORBIDDEN == response_code) && | |
75 (!CloudPrintTokenStore::current() || | |
76 !CloudPrintTokenStore::current()->token_is_oauth())) { | |
77 delegate_->OnRequestAuthError(); | |
78 return; | |
79 } | |
80 // We need to retry on all network errors. | 84 // We need to retry on all network errors. |
81 if (!status.is_success() || (response_code != 200)) | 85 if (!status.is_success() || (response_code != 200)) |
82 action = RETRY_REQUEST; | 86 action = RETRY_REQUEST; |
83 else | 87 else |
84 action = delegate_->HandleRawData(source, url, data); | 88 action = delegate_->HandleRawData(source, url, data); |
85 | 89 |
86 if (action == CONTINUE_PROCESSING) { | 90 if (action == CONTINUE_PROCESSING) { |
87 // If the delegate is not interested in handling the raw response data, | 91 // If the delegate is not interested in handling the raw response data, |
88 // we assume that a JSON response is expected. If we do not get a JSON | 92 // we assume that a JSON response is expected. If we do not get a JSON |
89 // response, we will retry (to handle the case where we got redirected | 93 // response, we will retry (to handle the case where we got redirected |
(...skipping 11 matching lines...) Expand all Loading... |
101 } | 105 } |
102 } | 106 } |
103 // Retry the request if needed. | 107 // Retry the request if needed. |
104 if (action == RETRY_REQUEST) { | 108 if (action == RETRY_REQUEST) { |
105 // Explicitly call ReceivedContentWasMalformed() to ensure the current | 109 // Explicitly call ReceivedContentWasMalformed() to ensure the current |
106 // request gets counted as a failure for calculation of the back-off | 110 // request gets counted as a failure for calculation of the back-off |
107 // period. If it was already a failure by status code, this call will | 111 // period. If it was already a failure by status code, this call will |
108 // be ignored. | 112 // be ignored. |
109 request_->ReceivedContentWasMalformed(); | 113 request_->ReceivedContentWasMalformed(); |
110 | 114 |
| 115 // If we receive error code from the server "Media Type Not Supported", |
| 116 // there is no reasons to retry, request will never succeed. |
| 117 // In that case we should call OnRequestGiveUp() right away. |
| 118 if (response_code == kHTTPUnsupportedMediaType) |
| 119 num_retries_ = source->max_retries(); |
| 120 |
111 ++num_retries_; | 121 ++num_retries_; |
112 if ((-1 != source->max_retries()) && | 122 if ((-1 != source->max_retries()) && |
113 (num_retries_ > source->max_retries())) { | 123 (num_retries_ > source->max_retries())) { |
114 // Retry limit reached. Give up. | 124 // Retry limit reached. Give up. |
115 delegate_->OnRequestGiveUp(); | 125 delegate_->OnRequestGiveUp(); |
116 } else { | 126 } else { |
117 // Either no retry limit specified or retry limit has not yet been | 127 // Either no retry limit specified or retry limit has not yet been |
118 // reached. Try again. Set up the request headers again because the token | 128 // reached. Try again. Set up the request headers again because the token |
119 // may have changed. | 129 // may have changed. |
120 SetupRequestHeaders(); | 130 SetupRequestHeaders(); |
(...skipping 11 matching lines...) Expand all Loading... |
132 const std::string& post_data, | 142 const std::string& post_data, |
133 const std::string& additional_headers) { | 143 const std::string& additional_headers) { |
134 DCHECK(delegate); | 144 DCHECK(delegate); |
135 // Persist the additional headers in case we need to retry the request. | 145 // Persist the additional headers in case we need to retry the request. |
136 additional_headers_ = additional_headers; | 146 additional_headers_ = additional_headers; |
137 request_.reset(new URLFetcher(url, request_type, this)); | 147 request_.reset(new URLFetcher(url, request_type, this)); |
138 request_->set_request_context(GetRequestContextGetter()); | 148 request_->set_request_context(GetRequestContextGetter()); |
139 // Since we implement our own retry logic, disable the retry in URLFetcher. | 149 // Since we implement our own retry logic, disable the retry in URLFetcher. |
140 request_->set_automatically_retry_on_5xx(false); | 150 request_->set_automatically_retry_on_5xx(false); |
141 request_->set_max_retries(max_retries); | 151 request_->set_max_retries(max_retries); |
| 152 delegate_ = delegate; |
142 SetupRequestHeaders(); | 153 SetupRequestHeaders(); |
143 delegate_ = delegate; | |
144 if (request_type == URLFetcher::POST) { | 154 if (request_type == URLFetcher::POST) { |
145 request_->set_upload_data(post_data_mime_type, post_data); | 155 request_->set_upload_data(post_data_mime_type, post_data); |
146 } | 156 } |
147 | 157 |
148 request_->Start(); | 158 request_->Start(); |
149 } | 159 } |
150 | 160 |
151 void CloudPrintURLFetcher::SetupRequestHeaders() { | 161 void CloudPrintURLFetcher::SetupRequestHeaders() { |
152 std::string headers; | 162 std::string headers = delegate_->GetAuthHeader(); |
153 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); | 163 if (!headers.empty()) |
154 if (token_store) { | |
155 headers = token_store->token_is_oauth() ? | |
156 "Authorization: OAuth " : "Authorization: GoogleLogin auth="; | |
157 headers += token_store->token(); | |
158 headers += "\r\n"; | 164 headers += "\r\n"; |
159 } | |
160 headers += kChromeCloudPrintProxyHeader; | 165 headers += kChromeCloudPrintProxyHeader; |
161 if (!additional_headers_.empty()) { | 166 if (!additional_headers_.empty()) { |
162 headers += "\r\n"; | 167 headers += "\r\n"; |
163 headers += additional_headers_; | 168 headers += additional_headers_; |
164 } | 169 } |
165 request_->set_extra_request_headers(headers); | 170 request_->set_extra_request_headers(headers); |
166 } | 171 } |
167 | 172 |
168 CloudPrintURLFetcher::~CloudPrintURLFetcher() {} | 173 CloudPrintURLFetcher::~CloudPrintURLFetcher() {} |
169 | 174 |
170 net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { | 175 net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { |
171 ServiceURLRequestContextGetter* getter = | 176 ServiceURLRequestContextGetter* getter = |
172 g_service_process->GetServiceURLRequestContextGetter(); | 177 g_service_process->GetServiceURLRequestContextGetter(); |
173 // Now set up the user agent for cloudprint. | 178 // Now set up the user agent for cloudprint. |
174 std::string user_agent = getter->user_agent(); | 179 std::string user_agent = getter->user_agent(); |
175 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); | 180 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); |
176 getter->set_user_agent(user_agent); | 181 getter->set_user_agent(user_agent); |
177 return getter; | 182 return getter; |
178 } | 183 } |
OLD | NEW |