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