OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/google_apis/base_requests.h" | 5 #include "chrome/browser/google_apis/base_requests.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/location.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
10 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
11 #include "base/threading/sequenced_worker_pool.h" | |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/google_apis/request_sender.h" | 13 #include "chrome/browser/google_apis/request_sender.h" |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
16 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
17 #include "net/http/http_byte_range.h" | 16 #include "net/http/http_byte_range.h" |
18 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
19 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
20 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
21 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
22 | 21 |
23 using content::BrowserThread; | |
24 using net::URLFetcher; | 22 using net::URLFetcher; |
25 | 23 |
26 namespace { | 24 namespace { |
27 | 25 |
28 // Template for optional OAuth2 authorization HTTP header. | 26 // Template for optional OAuth2 authorization HTTP header. |
29 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | 27 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
30 // Template for GData API version HTTP header. | 28 // Template for GData API version HTTP header. |
31 const char kGDataVersionHeader[] = "GData-Version: 3.0"; | 29 const char kGDataVersionHeader[] = "GData-Version: 3.0"; |
32 | 30 |
33 // Maximum number of attempts for re-authentication per request. | 31 // Maximum number of attempts for re-authentication per request. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } else { | 71 } else { |
74 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); | 72 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); |
75 } | 73 } |
76 return headers; | 74 return headers; |
77 } | 75 } |
78 | 76 |
79 } // namespace | 77 } // namespace |
80 | 78 |
81 namespace google_apis { | 79 namespace google_apis { |
82 | 80 |
83 void ParseJson(const std::string& json, const ParseJsonCallback& callback) { | 81 void ParseJson(base::TaskRunner* blocking_task_runner, |
| 82 const std::string& json, |
| 83 const ParseJsonCallback& callback) { |
84 base::PostTaskAndReplyWithResult( | 84 base::PostTaskAndReplyWithResult( |
85 BrowserThread::GetBlockingPool(), | 85 blocking_task_runner, |
86 FROM_HERE, | 86 FROM_HERE, |
87 base::Bind(&ParseJsonOnBlockingPool, json), | 87 base::Bind(&ParseJsonOnBlockingPool, json), |
88 callback); | 88 callback); |
89 } | 89 } |
90 | 90 |
91 //============================ UrlFetchRequestBase =========================== | 91 //============================ UrlFetchRequestBase =========================== |
92 | 92 |
93 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) | 93 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) |
94 : re_authenticate_count_(0), | 94 : re_authenticate_count_(0), |
95 sender_(sender), | 95 sender_(sender), |
(...skipping 28 matching lines...) Expand all Loading... |
124 url_fetcher_->SetRequestContext(sender_->url_request_context_getter()); | 124 url_fetcher_->SetRequestContext(sender_->url_request_context_getter()); |
125 // Always set flags to neither send nor save cookies. | 125 // Always set flags to neither send nor save cookies. |
126 url_fetcher_->SetLoadFlags( | 126 url_fetcher_->SetLoadFlags( |
127 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | 127 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | |
128 net::LOAD_DISABLE_CACHE); | 128 net::LOAD_DISABLE_CACHE); |
129 | 129 |
130 base::FilePath output_file_path; | 130 base::FilePath output_file_path; |
131 if (GetOutputFilePath(&output_file_path)) { | 131 if (GetOutputFilePath(&output_file_path)) { |
132 url_fetcher_->SaveResponseToFileAtPath( | 132 url_fetcher_->SaveResponseToFileAtPath( |
133 output_file_path, | 133 output_file_path, |
134 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 134 blocking_task_runner()); |
135 } | 135 } |
136 | 136 |
137 // Add request headers. | 137 // Add request headers. |
138 // Note that SetExtraRequestHeaders clears the current headers and sets it | 138 // Note that SetExtraRequestHeaders clears the current headers and sets it |
139 // to the passed-in headers, so calling it for each header will result in | 139 // to the passed-in headers, so calling it for each header will result in |
140 // only the last header being set in request headers. | 140 // only the last header being set in request headers. |
141 if (!custom_user_agent.empty()) | 141 if (!custom_user_agent.empty()) |
142 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); | 142 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); |
143 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); | 143 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); |
144 url_fetcher_->AddExtraRequestHeader( | 144 url_fetcher_->AddExtraRequestHeader( |
(...skipping 13 matching lines...) Expand all Loading... |
158 base::FilePath local_file_path; | 158 base::FilePath local_file_path; |
159 int64 range_offset = 0; | 159 int64 range_offset = 0; |
160 int64 range_length = 0; | 160 int64 range_length = 0; |
161 if (GetContentFile(&local_file_path, &range_offset, &range_length, | 161 if (GetContentFile(&local_file_path, &range_offset, &range_length, |
162 &upload_content_type)) { | 162 &upload_content_type)) { |
163 url_fetcher_->SetUploadFilePath( | 163 url_fetcher_->SetUploadFilePath( |
164 upload_content_type, | 164 upload_content_type, |
165 local_file_path, | 165 local_file_path, |
166 range_offset, | 166 range_offset, |
167 range_length, | 167 range_length, |
168 BrowserThread::GetBlockingPool()); | 168 blocking_task_runner()); |
169 } else { | 169 } else { |
170 // Even if there is no content data, UrlFetcher requires to set empty | 170 // Even if there is no content data, UrlFetcher requires to set empty |
171 // upload data string for POST, PUT and PATCH methods, explicitly. | 171 // upload data string for POST, PUT and PATCH methods, explicitly. |
172 // It is because that most requests of those methods have non-empty | 172 // It is because that most requests of those methods have non-empty |
173 // body, and UrlFetcher checks whether it is actually not forgotten. | 173 // body, and UrlFetcher checks whether it is actually not forgotten. |
174 if (request_type == URLFetcher::POST || | 174 if (request_type == URLFetcher::POST || |
175 request_type == URLFetcher::PUT || | 175 request_type == URLFetcher::PUT || |
176 request_type == URLFetcher::PATCH) { | 176 request_type == URLFetcher::PATCH) { |
177 // Set empty upload content-type and upload content, so that | 177 // Set empty upload content-type and upload content, so that |
178 // the request will have no "Content-type: " header and no content. | 178 // the request will have no "Content-type: " header and no content. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 code = GDATA_OTHER_ERROR; | 226 code = GDATA_OTHER_ERROR; |
227 } | 227 } |
228 } | 228 } |
229 return code; | 229 return code; |
230 } | 230 } |
231 | 231 |
232 bool UrlFetchRequestBase::CalledOnValidThread() { | 232 bool UrlFetchRequestBase::CalledOnValidThread() { |
233 return thread_checker_.CalledOnValidThread(); | 233 return thread_checker_.CalledOnValidThread(); |
234 } | 234 } |
235 | 235 |
| 236 base::TaskRunner* UrlFetchRequestBase::blocking_task_runner() const { |
| 237 return sender_->blocking_task_runner(); |
| 238 } |
| 239 |
236 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { | 240 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { |
237 sender_->RequestFinished(this); | 241 sender_->RequestFinished(this); |
238 } | 242 } |
239 | 243 |
240 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { | 244 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { |
241 GDataErrorCode code = GetErrorCode(source); | 245 GDataErrorCode code = GetErrorCode(source); |
242 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); | 246 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); |
243 | 247 |
244 if (code == HTTP_UNAUTHORIZED) { | 248 if (code == HTTP_UNAUTHORIZED) { |
245 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { | 249 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 } | 305 } |
302 | 306 |
303 GetDataRequest::~GetDataRequest() {} | 307 GetDataRequest::~GetDataRequest() {} |
304 | 308 |
305 void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code, | 309 void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code, |
306 const std::string& data) { | 310 const std::string& data) { |
307 DCHECK(CalledOnValidThread()); | 311 DCHECK(CalledOnValidThread()); |
308 | 312 |
309 VLOG(1) << "JSON received from " << GetURL().spec() << ": " | 313 VLOG(1) << "JSON received from " << GetURL().spec() << ": " |
310 << data.size() << " bytes"; | 314 << data.size() << " bytes"; |
311 ParseJson(data, | 315 ParseJson(blocking_task_runner(), |
| 316 data, |
312 base::Bind(&GetDataRequest::OnDataParsed, | 317 base::Bind(&GetDataRequest::OnDataParsed, |
313 weak_ptr_factory_.GetWeakPtr(), | 318 weak_ptr_factory_.GetWeakPtr(), |
314 fetch_error_code)); | 319 fetch_error_code)); |
315 } | 320 } |
316 | 321 |
317 void GetDataRequest::ProcessURLFetchResults(const URLFetcher* source) { | 322 void GetDataRequest::ProcessURLFetchResults(const URLFetcher* source) { |
318 std::string data; | 323 std::string data; |
319 source->GetResponseAsString(&data); | 324 source->GetResponseAsString(&data); |
320 scoped_ptr<base::Value> root_value; | 325 scoped_ptr<base::Value> root_value; |
321 GDataErrorCode fetch_error_code = GetErrorCode(source); | 326 GDataErrorCode fetch_error_code = GetErrorCode(source); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 start_position_received, | 488 start_position_received, |
484 end_position_received), | 489 end_position_received), |
485 scoped_ptr<base::Value>()); | 490 scoped_ptr<base::Value>()); |
486 | 491 |
487 OnProcessURLFetchResultsComplete(true); | 492 OnProcessURLFetchResultsComplete(true); |
488 } else { | 493 } else { |
489 // There might be explanation of unexpected error code in response. | 494 // There might be explanation of unexpected error code in response. |
490 std::string response_content; | 495 std::string response_content; |
491 source->GetResponseAsString(&response_content); | 496 source->GetResponseAsString(&response_content); |
492 | 497 |
493 ParseJson(response_content, | 498 ParseJson(blocking_task_runner(), |
| 499 response_content, |
494 base::Bind(&UploadRangeRequestBase::OnDataParsed, | 500 base::Bind(&UploadRangeRequestBase::OnDataParsed, |
495 weak_ptr_factory_.GetWeakPtr(), | 501 weak_ptr_factory_.GetWeakPtr(), |
496 code)); | 502 code)); |
497 } | 503 } |
498 } | 504 } |
499 | 505 |
500 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, | 506 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, |
501 scoped_ptr<base::Value> value) { | 507 scoped_ptr<base::Value> value) { |
502 DCHECK(CalledOnValidThread()); | 508 DCHECK(CalledOnValidThread()); |
503 | 509 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 download_action_callback_.Run(code, temp_file); | 675 download_action_callback_.Run(code, temp_file); |
670 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 676 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
671 } | 677 } |
672 | 678 |
673 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 679 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
674 GDataErrorCode code) { | 680 GDataErrorCode code) { |
675 download_action_callback_.Run(code, base::FilePath()); | 681 download_action_callback_.Run(code, base::FilePath()); |
676 } | 682 } |
677 | 683 |
678 } // namespace google_apis | 684 } // namespace google_apis |
OLD | NEW |