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" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 const std::string& additional_headers) { | 43 const std::string& additional_headers) { |
44 StartRequestHelper(url, | 44 StartRequestHelper(url, |
45 URLFetcher::POST, | 45 URLFetcher::POST, |
46 delegate, | 46 delegate, |
47 max_retries, | 47 max_retries, |
48 post_data_mime_type, | 48 post_data_mime_type, |
49 post_data, | 49 post_data, |
50 additional_headers); | 50 additional_headers); |
51 } | 51 } |
52 | 52 |
53 void CloudPrintURLFetcher::OnURLFetchComplete(const URLFetcher* source) { | 53 void CloudPrintURLFetcher::OnURLFetchComplete( |
54 VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << source->url() | 54 const content::URLFetcher* source) { |
55 << ", response code: " << source->response_code(); | 55 VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << source->GetUrl() |
| 56 << ", response code: " << source->GetResponseCode(); |
56 // Make sure we stay alive through the body of this function. | 57 // Make sure we stay alive through the body of this function. |
57 scoped_refptr<CloudPrintURLFetcher> keep_alive(this); | 58 scoped_refptr<CloudPrintURLFetcher> keep_alive(this); |
58 std::string data; | 59 std::string data; |
59 source->GetResponseAsString(&data); | 60 source->GetResponseAsString(&data); |
60 ResponseAction action = delegate_->HandleRawResponse( | 61 ResponseAction action = delegate_->HandleRawResponse( |
61 source, | 62 source, |
62 source->url(), | 63 source->GetUrl(), |
63 source->status(), | 64 source->GetStatus(), |
64 source->response_code(), | 65 source->GetResponseCode(), |
65 source->cookies(), | 66 source->GetCookies(), |
66 data); | 67 data); |
67 if (action == CONTINUE_PROCESSING) { | 68 if (action == CONTINUE_PROCESSING) { |
68 // If we are not using an OAuth token, and we got an auth error, we are | 69 // If we are not using an OAuth token, and we got an auth error, we are |
69 // done. Else, the token may have been refreshed. Let us try again. | 70 // done. Else, the token may have been refreshed. Let us try again. |
70 if ((RC_FORBIDDEN == source->response_code()) && | 71 if ((RC_FORBIDDEN == source->GetResponseCode()) && |
71 (!CloudPrintTokenStore::current() || | 72 (!CloudPrintTokenStore::current() || |
72 !CloudPrintTokenStore::current()->token_is_oauth())) { | 73 !CloudPrintTokenStore::current()->token_is_oauth())) { |
73 delegate_->OnRequestAuthError(); | 74 delegate_->OnRequestAuthError(); |
74 return; | 75 return; |
75 } | 76 } |
76 // We need to retry on all network errors. | 77 // We need to retry on all network errors. |
77 if (!source->status().is_success() || (source->response_code() != 200)) | 78 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) |
78 action = RETRY_REQUEST; | 79 action = RETRY_REQUEST; |
79 else | 80 else |
80 action = delegate_->HandleRawData(source, source->url(), data); | 81 action = delegate_->HandleRawData(source, source->GetUrl(), data); |
81 | 82 |
82 if (action == CONTINUE_PROCESSING) { | 83 if (action == CONTINUE_PROCESSING) { |
83 // If the delegate is not interested in handling the raw response data, | 84 // If the delegate is not interested in handling the raw response data, |
84 // we assume that a JSON response is expected. If we do not get a JSON | 85 // we assume that a JSON response is expected. If we do not get a JSON |
85 // response, we will retry (to handle the case where we got redirected | 86 // response, we will retry (to handle the case where we got redirected |
86 // to a non-cloudprint-server URL eg. for authentication). | 87 // to a non-cloudprint-server URL eg. for authentication). |
87 bool succeeded = false; | 88 bool succeeded = false; |
88 DictionaryValue* response_dict = NULL; | 89 DictionaryValue* response_dict = NULL; |
89 CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict); | 90 CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict); |
90 if (response_dict) | 91 if (response_dict) |
91 action = delegate_->HandleJSONData(source, | 92 action = delegate_->HandleJSONData(source, |
92 source->url(), | 93 source->GetUrl(), |
93 response_dict, | 94 response_dict, |
94 succeeded); | 95 succeeded); |
95 else | 96 else |
96 action = RETRY_REQUEST; | 97 action = RETRY_REQUEST; |
97 } | 98 } |
98 } | 99 } |
99 // Retry the request if needed. | 100 // Retry the request if needed. |
100 if (action == RETRY_REQUEST) { | 101 if (action == RETRY_REQUEST) { |
101 // Explicitly call ReceivedContentWasMalformed() to ensure the current | 102 // Explicitly call ReceivedContentWasMalformed() to ensure the current |
102 // request gets counted as a failure for calculation of the back-off | 103 // request gets counted as a failure for calculation of the back-off |
103 // period. If it was already a failure by status code, this call will | 104 // period. If it was already a failure by status code, this call will |
104 // be ignored. | 105 // be ignored. |
105 request_->ReceivedContentWasMalformed(); | 106 request_->ReceivedContentWasMalformed(); |
106 | 107 |
107 ++num_retries_; | 108 ++num_retries_; |
108 if ((-1 != source->max_retries()) && | 109 if ((-1 != source->GetMaxRetries()) && |
109 (num_retries_ > source->max_retries())) { | 110 (num_retries_ > source->GetMaxRetries())) { |
110 // Retry limit reached. Give up. | 111 // Retry limit reached. Give up. |
111 delegate_->OnRequestGiveUp(); | 112 delegate_->OnRequestGiveUp(); |
112 } else { | 113 } else { |
113 // Either no retry limit specified or retry limit has not yet been | 114 // Either no retry limit specified or retry limit has not yet been |
114 // reached. Try again. Set up the request headers again because the token | 115 // reached. Try again. Set up the request headers again because the token |
115 // may have changed. | 116 // may have changed. |
116 SetupRequestHeaders(); | 117 SetupRequestHeaders(); |
117 request_->StartWithRequestContextGetter(GetRequestContextGetter()); | 118 request_->StartWithRequestContextGetter(GetRequestContextGetter()); |
118 } | 119 } |
119 } | 120 } |
120 } | 121 } |
121 | 122 |
122 void CloudPrintURLFetcher::StartRequestHelper( | 123 void CloudPrintURLFetcher::StartRequestHelper( |
123 const GURL& url, | 124 const GURL& url, |
124 URLFetcher::RequestType request_type, | 125 URLFetcher::RequestType request_type, |
125 Delegate* delegate, | 126 Delegate* delegate, |
126 int max_retries, | 127 int max_retries, |
127 const std::string& post_data_mime_type, | 128 const std::string& post_data_mime_type, |
128 const std::string& post_data, | 129 const std::string& post_data, |
129 const std::string& additional_headers) { | 130 const std::string& additional_headers) { |
130 DCHECK(delegate); | 131 DCHECK(delegate); |
131 // Persist the additional headers in case we need to retry the request. | 132 // Persist the additional headers in case we need to retry the request. |
132 additional_headers_ = additional_headers; | 133 additional_headers_ = additional_headers; |
133 request_.reset(new URLFetcher(url, request_type, this)); | 134 request_.reset(new URLFetcher(url, request_type, this)); |
134 request_->set_request_context(GetRequestContextGetter()); | 135 request_->SetRequestContext(GetRequestContextGetter()); |
135 // Since we implement our own retry logic, disable the retry in URLFetcher. | 136 // Since we implement our own retry logic, disable the retry in URLFetcher. |
136 request_->set_automatically_retry_on_5xx(false); | 137 request_->SetAutomaticallyRetryOn5xx(false); |
137 request_->set_max_retries(max_retries); | 138 request_->SetMaxRetries(max_retries); |
138 SetupRequestHeaders(); | 139 SetupRequestHeaders(); |
139 delegate_ = delegate; | 140 delegate_ = delegate; |
140 if (request_type == URLFetcher::POST) { | 141 if (request_type == URLFetcher::POST) { |
141 request_->set_upload_data(post_data_mime_type, post_data); | 142 request_->SetUploadData(post_data_mime_type, post_data); |
142 } | 143 } |
143 | 144 |
144 request_->Start(); | 145 request_->Start(); |
145 } | 146 } |
146 | 147 |
147 void CloudPrintURLFetcher::SetupRequestHeaders() { | 148 void CloudPrintURLFetcher::SetupRequestHeaders() { |
148 std::string headers; | 149 std::string headers; |
149 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); | 150 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); |
150 if (token_store) { | 151 if (token_store) { |
151 headers = token_store->token_is_oauth() ? | 152 headers = token_store->token_is_oauth() ? |
152 "Authorization: OAuth " : "Authorization: GoogleLogin auth="; | 153 "Authorization: OAuth " : "Authorization: GoogleLogin auth="; |
153 headers += token_store->token(); | 154 headers += token_store->token(); |
154 headers += "\r\n"; | 155 headers += "\r\n"; |
155 } | 156 } |
156 headers += kChromeCloudPrintProxyHeader; | 157 headers += kChromeCloudPrintProxyHeader; |
157 if (!additional_headers_.empty()) { | 158 if (!additional_headers_.empty()) { |
158 headers += "\r\n"; | 159 headers += "\r\n"; |
159 headers += additional_headers_; | 160 headers += additional_headers_; |
160 } | 161 } |
161 request_->set_extra_request_headers(headers); | 162 request_->SetExtraRequestHeaders(headers); |
162 } | 163 } |
163 | 164 |
164 CloudPrintURLFetcher::~CloudPrintURLFetcher() {} | 165 CloudPrintURLFetcher::~CloudPrintURLFetcher() {} |
165 | 166 |
166 net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { | 167 net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { |
167 ServiceURLRequestContextGetter* getter = | 168 ServiceURLRequestContextGetter* getter = |
168 g_service_process->GetServiceURLRequestContextGetter(); | 169 g_service_process->GetServiceURLRequestContextGetter(); |
169 // Now set up the user agent for cloudprint. | 170 // Now set up the user agent for cloudprint. |
170 std::string user_agent = getter->user_agent(); | 171 std::string user_agent = getter->user_agent(); |
171 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); | 172 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); |
172 getter->set_user_agent(user_agent); | 173 getter->set_user_agent(user_agent); |
173 return getter; | 174 return getter; |
174 } | 175 } |
OLD | NEW |