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/location.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.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 "chrome/browser/google_apis/task_util.h" |
| 15 #include "net/base/io_buffer.h" |
14 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
15 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
16 #include "net/http/http_byte_range.h" | 18 #include "net/http/http_byte_range.h" |
17 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
18 #include "net/http/http_util.h" | 20 #include "net/http/http_util.h" |
19 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
21 | 23 |
22 using net::URLFetcher; | 24 using net::URLFetcher; |
23 | 25 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 void ParseJson(base::TaskRunner* blocking_task_runner, | 94 void ParseJson(base::TaskRunner* blocking_task_runner, |
93 const std::string& json, | 95 const std::string& json, |
94 const ParseJsonCallback& callback) { | 96 const ParseJsonCallback& callback) { |
95 base::PostTaskAndReplyWithResult( | 97 base::PostTaskAndReplyWithResult( |
96 blocking_task_runner, | 98 blocking_task_runner, |
97 FROM_HERE, | 99 FROM_HERE, |
98 base::Bind(&ParseJsonOnBlockingPool, json), | 100 base::Bind(&ParseJsonOnBlockingPool, json), |
99 callback); | 101 callback); |
100 } | 102 } |
101 | 103 |
| 104 //=========================== ResponseWriter ================================== |
| 105 ResponseWriter::ResponseWriter(base::TaskRunner* file_task_runner, |
| 106 const base::FilePath& file_path, |
| 107 const GetContentCallback& get_content_callback) |
| 108 : get_content_callback_(get_content_callback) { |
| 109 if (!file_path.empty()) { |
| 110 file_writer_.reset( |
| 111 new net::URLFetcherFileWriter(file_task_runner, file_path)); |
| 112 } |
| 113 } |
| 114 |
| 115 ResponseWriter::~ResponseWriter() { |
| 116 } |
| 117 |
| 118 void ResponseWriter::DisownFile() { |
| 119 DCHECK(file_writer_); |
| 120 file_writer_->DisownFile(); |
| 121 } |
| 122 |
| 123 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { |
| 124 if (file_writer_) |
| 125 return file_writer_->Initialize(callback); |
| 126 |
| 127 data_.clear(); |
| 128 return net::OK; |
| 129 } |
| 130 |
| 131 int ResponseWriter::Write(net::IOBuffer* buffer, |
| 132 int num_bytes, |
| 133 const net::CompletionCallback& callback) { |
| 134 if (!get_content_callback_.is_null()) { |
| 135 get_content_callback_.Run( |
| 136 HTTP_SUCCESS, |
| 137 make_scoped_ptr(new std::string(buffer->data(), num_bytes))); |
| 138 } |
| 139 |
| 140 if (file_writer_) |
| 141 return file_writer_->Write(buffer, num_bytes, callback); |
| 142 |
| 143 data_.append(buffer->data(), num_bytes); |
| 144 return num_bytes; |
| 145 } |
| 146 |
| 147 int ResponseWriter::Finish(const net::CompletionCallback& callback) { |
| 148 if (file_writer_) |
| 149 return file_writer_->Finish(callback); |
| 150 |
| 151 return net::OK; |
| 152 } |
| 153 |
102 //============================ UrlFetchRequestBase =========================== | 154 //============================ UrlFetchRequestBase =========================== |
103 | 155 |
104 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) | 156 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) |
105 : re_authenticate_count_(0), | 157 : re_authenticate_count_(0), |
106 sender_(sender), | 158 sender_(sender), |
107 weak_ptr_factory_(this) { | 159 weak_ptr_factory_(this) { |
108 } | 160 } |
109 | 161 |
110 UrlFetchRequestBase::~UrlFetchRequestBase() {} | 162 UrlFetchRequestBase::~UrlFetchRequestBase() {} |
111 | 163 |
(...skipping 20 matching lines...) Expand all Loading... |
132 URLFetcher::RequestType request_type = GetRequestType(); | 184 URLFetcher::RequestType request_type = GetRequestType(); |
133 url_fetcher_.reset( | 185 url_fetcher_.reset( |
134 URLFetcher::Create(url, request_type, this)); | 186 URLFetcher::Create(url, request_type, this)); |
135 url_fetcher_->SetRequestContext(sender_->url_request_context_getter()); | 187 url_fetcher_->SetRequestContext(sender_->url_request_context_getter()); |
136 // Always set flags to neither send nor save cookies. | 188 // Always set flags to neither send nor save cookies. |
137 url_fetcher_->SetLoadFlags( | 189 url_fetcher_->SetLoadFlags( |
138 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | 190 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | |
139 net::LOAD_DISABLE_CACHE); | 191 net::LOAD_DISABLE_CACHE); |
140 | 192 |
141 base::FilePath output_file_path; | 193 base::FilePath output_file_path; |
142 if (GetOutputFilePath(&output_file_path)) { | 194 GetContentCallback get_content_callback; |
143 url_fetcher_->SaveResponseToFileAtPath( | 195 GetOutputFilePath(&output_file_path, &get_content_callback); |
144 output_file_path, | 196 if (!get_content_callback.is_null()) |
145 blocking_task_runner()); | 197 get_content_callback = CreateRelayCallback(get_content_callback); |
146 } | 198 response_writer_ = new ResponseWriter(blocking_task_runner(), |
| 199 output_file_path, |
| 200 get_content_callback); |
| 201 url_fetcher_->SaveResponseWithWriter( |
| 202 scoped_ptr<net::URLFetcherResponseWriter>(response_writer_)); |
147 | 203 |
148 // Add request headers. | 204 // Add request headers. |
149 // Note that SetExtraRequestHeaders clears the current headers and sets it | 205 // Note that SetExtraRequestHeaders clears the current headers and sets it |
150 // to the passed-in headers, so calling it for each header will result in | 206 // to the passed-in headers, so calling it for each header will result in |
151 // only the last header being set in request headers. | 207 // only the last header being set in request headers. |
152 if (!custom_user_agent.empty()) | 208 if (!custom_user_agent.empty()) |
153 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); | 209 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); |
154 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); | 210 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); |
155 url_fetcher_->AddExtraRequestHeader( | 211 url_fetcher_->AddExtraRequestHeader( |
156 base::StringPrintf(kAuthorizationHeaderFormat, access_token.data())); | 212 base::StringPrintf(kAuthorizationHeaderFormat, access_token.data())); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 return false; | 264 return false; |
209 } | 265 } |
210 | 266 |
211 bool UrlFetchRequestBase::GetContentFile(base::FilePath* local_file_path, | 267 bool UrlFetchRequestBase::GetContentFile(base::FilePath* local_file_path, |
212 int64* range_offset, | 268 int64* range_offset, |
213 int64* range_length, | 269 int64* range_length, |
214 std::string* upload_content_type) { | 270 std::string* upload_content_type) { |
215 return false; | 271 return false; |
216 } | 272 } |
217 | 273 |
218 bool UrlFetchRequestBase::GetOutputFilePath(base::FilePath* local_file_path) { | 274 void UrlFetchRequestBase::GetOutputFilePath( |
219 return false; | 275 base::FilePath* local_file_path, |
| 276 GetContentCallback* get_content_callback) { |
220 } | 277 } |
221 | 278 |
222 void UrlFetchRequestBase::Cancel() { | 279 void UrlFetchRequestBase::Cancel() { |
| 280 response_writer_ = NULL; |
223 url_fetcher_.reset(NULL); | 281 url_fetcher_.reset(NULL); |
224 RunCallbackOnPrematureFailure(GDATA_CANCELLED); | 282 RunCallbackOnPrematureFailure(GDATA_CANCELLED); |
225 sender_->RequestFinished(this); | 283 sender_->RequestFinished(this); |
226 } | 284 } |
227 | 285 |
228 // static | 286 // static |
229 GDataErrorCode UrlFetchRequestBase::GetErrorCode(const URLFetcher* source) { | 287 GDataErrorCode UrlFetchRequestBase::GetErrorCode(const URLFetcher* source) { |
230 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 288 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
231 if (!source->GetStatus().is_success()) { | 289 if (!source->GetStatus().is_success()) { |
232 switch (source->GetStatus().error()) { | 290 switch (source->GetStatus().error()) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 VLOG(1) << "JSON received from " << GetURL().spec() << ": " | 380 VLOG(1) << "JSON received from " << GetURL().spec() << ": " |
323 << data.size() << " bytes"; | 381 << data.size() << " bytes"; |
324 ParseJson(blocking_task_runner(), | 382 ParseJson(blocking_task_runner(), |
325 data, | 383 data, |
326 base::Bind(&GetDataRequest::OnDataParsed, | 384 base::Bind(&GetDataRequest::OnDataParsed, |
327 weak_ptr_factory_.GetWeakPtr(), | 385 weak_ptr_factory_.GetWeakPtr(), |
328 fetch_error_code)); | 386 fetch_error_code)); |
329 } | 387 } |
330 | 388 |
331 void GetDataRequest::ProcessURLFetchResults(const URLFetcher* source) { | 389 void GetDataRequest::ProcessURLFetchResults(const URLFetcher* source) { |
332 std::string data; | |
333 source->GetResponseAsString(&data); | |
334 scoped_ptr<base::Value> root_value; | |
335 GDataErrorCode fetch_error_code = GetErrorCode(source); | 390 GDataErrorCode fetch_error_code = GetErrorCode(source); |
336 | 391 |
337 switch (fetch_error_code) { | 392 switch (fetch_error_code) { |
338 case HTTP_SUCCESS: | 393 case HTTP_SUCCESS: |
339 case HTTP_CREATED: | 394 case HTTP_CREATED: |
340 ParseResponse(fetch_error_code, data); | 395 ParseResponse(fetch_error_code, response_writer()->data()); |
341 break; | 396 break; |
342 default: | 397 default: |
343 RunCallbackOnPrematureFailure(fetch_error_code); | 398 RunCallbackOnPrematureFailure(fetch_error_code); |
344 OnProcessURLFetchResultsComplete(); | 399 OnProcessURLFetchResultsComplete(); |
345 break; | 400 break; |
346 } | 401 } |
347 } | 402 } |
348 | 403 |
349 void GetDataRequest::RunCallbackOnPrematureFailure( | 404 void GetDataRequest::RunCallbackOnPrematureFailure( |
350 GDataErrorCode fetch_error_code) { | 405 GDataErrorCode fetch_error_code) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 538 |
484 OnRangeRequestComplete(UploadRangeResponse(code, | 539 OnRangeRequestComplete(UploadRangeResponse(code, |
485 start_position_received, | 540 start_position_received, |
486 end_position_received), | 541 end_position_received), |
487 scoped_ptr<base::Value>()); | 542 scoped_ptr<base::Value>()); |
488 | 543 |
489 OnProcessURLFetchResultsComplete(); | 544 OnProcessURLFetchResultsComplete(); |
490 } else if (code == HTTP_CREATED || code == HTTP_SUCCESS) { | 545 } else if (code == HTTP_CREATED || code == HTTP_SUCCESS) { |
491 // The upload is successfully done. Parse the response which should be | 546 // The upload is successfully done. Parse the response which should be |
492 // the entry's metadata. | 547 // the entry's metadata. |
493 std::string response_content; | |
494 source->GetResponseAsString(&response_content); | |
495 | |
496 ParseJson(blocking_task_runner(), | 548 ParseJson(blocking_task_runner(), |
497 response_content, | 549 response_writer()->data(), |
498 base::Bind(&UploadRangeRequestBase::OnDataParsed, | 550 base::Bind(&UploadRangeRequestBase::OnDataParsed, |
499 weak_ptr_factory_.GetWeakPtr(), | 551 weak_ptr_factory_.GetWeakPtr(), |
500 code)); | 552 code)); |
501 } else { | 553 } else { |
502 // Failed to upload. Run callbacks to notify the error. | 554 // Failed to upload. Run callbacks to notify the error. |
503 OnRangeRequestComplete( | 555 OnRangeRequestComplete( |
504 UploadRangeResponse(code, -1, -1), scoped_ptr<base::Value>()); | 556 UploadRangeResponse(code, -1, -1), scoped_ptr<base::Value>()); |
505 OnProcessURLFetchResultsComplete(); | 557 OnProcessURLFetchResultsComplete(); |
506 } | 558 } |
507 } | 559 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 // get_content_callback may be null. | 683 // get_content_callback may be null. |
632 } | 684 } |
633 | 685 |
634 DownloadFileRequestBase::~DownloadFileRequestBase() {} | 686 DownloadFileRequestBase::~DownloadFileRequestBase() {} |
635 | 687 |
636 // Overridden from UrlFetchRequestBase. | 688 // Overridden from UrlFetchRequestBase. |
637 GURL DownloadFileRequestBase::GetURL() const { | 689 GURL DownloadFileRequestBase::GetURL() const { |
638 return download_url_; | 690 return download_url_; |
639 } | 691 } |
640 | 692 |
641 bool DownloadFileRequestBase::GetOutputFilePath( | 693 void DownloadFileRequestBase::GetOutputFilePath( |
642 base::FilePath* local_file_path) { | 694 base::FilePath* local_file_path, |
| 695 GetContentCallback* get_content_callback) { |
643 // Configure so that the downloaded content is saved to |output_file_path_|. | 696 // Configure so that the downloaded content is saved to |output_file_path_|. |
644 *local_file_path = output_file_path_; | 697 *local_file_path = output_file_path_; |
645 return true; | 698 *get_content_callback = get_content_callback_; |
646 } | 699 } |
647 | 700 |
648 void DownloadFileRequestBase::OnURLFetchDownloadProgress( | 701 void DownloadFileRequestBase::OnURLFetchDownloadProgress( |
649 const URLFetcher* source, | 702 const URLFetcher* source, |
650 int64 current, | 703 int64 current, |
651 int64 total) { | 704 int64 total) { |
652 if (!progress_callback_.is_null()) | 705 if (!progress_callback_.is_null()) |
653 progress_callback_.Run(current, total); | 706 progress_callback_.Run(current, total); |
654 } | 707 } |
655 | 708 |
656 bool DownloadFileRequestBase::ShouldSendDownloadData() { | |
657 return !get_content_callback_.is_null(); | |
658 } | |
659 | |
660 void DownloadFileRequestBase::OnURLFetchDownloadData( | |
661 const URLFetcher* source, | |
662 scoped_ptr<std::string> download_data) { | |
663 if (!get_content_callback_.is_null()) | |
664 get_content_callback_.Run(HTTP_SUCCESS, download_data.Pass()); | |
665 } | |
666 | |
667 void DownloadFileRequestBase::ProcessURLFetchResults(const URLFetcher* source) { | 709 void DownloadFileRequestBase::ProcessURLFetchResults(const URLFetcher* source) { |
668 GDataErrorCode code = GetErrorCode(source); | 710 GDataErrorCode code = GetErrorCode(source); |
669 | 711 |
670 // Take over the ownership of the the downloaded temp file. | 712 // Take over the ownership of the the downloaded temp file. |
671 base::FilePath temp_file; | 713 base::FilePath temp_file; |
672 if (code == HTTP_SUCCESS && | 714 if (code == HTTP_SUCCESS) { |
673 !source->GetResponseAsFilePath(true, // take_ownership | 715 response_writer()->DisownFile(); |
674 &temp_file)) { | 716 temp_file = output_file_path_; |
675 code = GDATA_FILE_ERROR; | |
676 } | 717 } |
677 | 718 |
678 download_action_callback_.Run(code, temp_file); | 719 download_action_callback_.Run(code, temp_file); |
679 OnProcessURLFetchResultsComplete(); | 720 OnProcessURLFetchResultsComplete(); |
680 } | 721 } |
681 | 722 |
682 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 723 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
683 GDataErrorCode code) { | 724 GDataErrorCode code) { |
684 download_action_callback_.Run(code, base::FilePath()); | 725 download_action_callback_.Run(code, base::FilePath()); |
685 } | 726 } |
686 | 727 |
687 } // namespace google_apis | 728 } // namespace google_apis |
OLD | NEW |