Chromium Code Reviews| 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 "google_apis/drive/base_requests.h" | 5 #include "google_apis/drive/base_requests.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | |
| 15 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 18 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
| 19 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/values.h" | 22 #include "base/values.h" |
| 21 #include "google_apis/drive/drive_api_parser.h" | 23 #include "google_apis/drive/drive_api_parser.h" |
| 22 #include "google_apis/drive/request_sender.h" | 24 #include "google_apis/drive/request_sender.h" |
| 23 #include "google_apis/drive/request_util.h" | 25 #include "google_apis/drive/request_util.h" |
| 24 #include "google_apis/drive/task_util.h" | 26 #include "google_apis/drive/task_util.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 | 77 |
| 76 // Footer for whole multipart message. | 78 // Footer for whole multipart message. |
| 77 const char kMultipartFooterFormat[] = "--%s--"; | 79 const char kMultipartFooterFormat[] = "--%s--"; |
| 78 | 80 |
| 79 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on | 81 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on |
| 80 // the calling thread when finished with either success or failure. | 82 // the calling thread when finished with either success or failure. |
| 81 // The callback must not be null. | 83 // The callback must not be null. |
| 82 void ParseJsonOnBlockingPool( | 84 void ParseJsonOnBlockingPool( |
| 83 base::TaskRunner* blocking_task_runner, | 85 base::TaskRunner* blocking_task_runner, |
| 84 const std::string& json, | 86 const std::string& json, |
| 85 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) { | 87 const base::Callback<void(std::unique_ptr<base::Value> value)>& callback) { |
| 86 base::PostTaskAndReplyWithResult( | 88 base::PostTaskAndReplyWithResult( |
| 87 blocking_task_runner, | 89 blocking_task_runner, |
| 88 FROM_HERE, | 90 FROM_HERE, |
| 89 base::Bind(&google_apis::ParseJson, json), | 91 base::Bind(&google_apis::ParseJson, json), |
| 90 callback); | 92 callback); |
| 91 } | 93 } |
| 92 | 94 |
| 93 // Returns response headers as a string. Returns a warning message if | 95 // Returns response headers as a string. Returns a warning message if |
| 94 // |url_fetcher| does not contain a valid response. Used only for debugging. | 96 // |url_fetcher| does not contain a valid response. Used only for debugging. |
| 95 std::string GetResponseHeadersAsString(const URLFetcher* url_fetcher) { | 97 std::string GetResponseHeadersAsString(const URLFetcher* url_fetcher) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 DVLOG(1) << error_body; | 146 DVLOG(1) << error_body; |
| 145 const char kErrorKey[] = "error"; | 147 const char kErrorKey[] = "error"; |
| 146 const char kErrorErrorsKey[] = "errors"; | 148 const char kErrorErrorsKey[] = "errors"; |
| 147 const char kErrorReasonKey[] = "reason"; | 149 const char kErrorReasonKey[] = "reason"; |
| 148 const char kErrorMessageKey[] = "message"; | 150 const char kErrorMessageKey[] = "message"; |
| 149 const char kErrorReasonRateLimitExceeded[] = "rateLimitExceeded"; | 151 const char kErrorReasonRateLimitExceeded[] = "rateLimitExceeded"; |
| 150 const char kErrorReasonUserRateLimitExceeded[] = "userRateLimitExceeded"; | 152 const char kErrorReasonUserRateLimitExceeded[] = "userRateLimitExceeded"; |
| 151 const char kErrorReasonQuotaExceeded[] = "quotaExceeded"; | 153 const char kErrorReasonQuotaExceeded[] = "quotaExceeded"; |
| 152 const char kErrorReasonResponseTooLarge[] = "responseTooLarge"; | 154 const char kErrorReasonResponseTooLarge[] = "responseTooLarge"; |
| 153 | 155 |
| 154 scoped_ptr<const base::Value> value(google_apis::ParseJson(error_body)); | 156 std::unique_ptr<const base::Value> value(google_apis::ParseJson(error_body)); |
| 155 const base::DictionaryValue* dictionary = NULL; | 157 const base::DictionaryValue* dictionary = NULL; |
| 156 const base::DictionaryValue* error = NULL; | 158 const base::DictionaryValue* error = NULL; |
| 157 if (value && | 159 if (value && |
| 158 value->GetAsDictionary(&dictionary) && | 160 value->GetAsDictionary(&dictionary) && |
| 159 dictionary->GetDictionaryWithoutPathExpansion(kErrorKey, &error)) { | 161 dictionary->GetDictionaryWithoutPathExpansion(kErrorKey, &error)) { |
| 160 // Get error message. | 162 // Get error message. |
| 161 std::string message; | 163 std::string message; |
| 162 error->GetStringWithoutPathExpansion(kErrorMessageKey, &message); | 164 error->GetStringWithoutPathExpansion(kErrorMessageKey, &message); |
| 163 DLOG(ERROR) << "code: " << code << ", message: " << message; | 165 DLOG(ERROR) << "code: " << code << ", message: " << message; |
| 164 | 166 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 180 } | 182 } |
| 181 } | 183 } |
| 182 | 184 |
| 183 return code; | 185 return code; |
| 184 } | 186 } |
| 185 | 187 |
| 186 } // namespace | 188 } // namespace |
| 187 | 189 |
| 188 namespace google_apis { | 190 namespace google_apis { |
| 189 | 191 |
| 190 scoped_ptr<base::Value> ParseJson(const std::string& json) { | 192 std::unique_ptr<base::Value> ParseJson(const std::string& json) { |
| 191 int error_code = -1; | 193 int error_code = -1; |
| 192 std::string error_message; | 194 std::string error_message; |
| 193 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( | 195 std::unique_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( |
| 194 json, base::JSON_PARSE_RFC, &error_code, &error_message); | 196 json, base::JSON_PARSE_RFC, &error_code, &error_message); |
| 195 | 197 |
| 196 if (!value.get()) { | 198 if (!value.get()) { |
| 197 std::string trimmed_json; | 199 std::string trimmed_json; |
| 198 if (json.size() < 80) { | 200 if (json.size() < 80) { |
| 199 trimmed_json = json; | 201 trimmed_json = json; |
| 200 } else { | 202 } else { |
| 201 // Take the first 50 and the last 10 bytes. | 203 // Take the first 50 and the last 10 bytes. |
| 202 trimmed_json = base::StringPrintf( | 204 trimmed_json = base::StringPrintf( |
| 203 "%s [%s bytes] %s", | 205 "%s [%s bytes] %s", |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 return file_writer_->Initialize(callback); | 286 return file_writer_->Initialize(callback); |
| 285 | 287 |
| 286 data_.clear(); | 288 data_.clear(); |
| 287 return net::OK; | 289 return net::OK; |
| 288 } | 290 } |
| 289 | 291 |
| 290 int ResponseWriter::Write(net::IOBuffer* buffer, | 292 int ResponseWriter::Write(net::IOBuffer* buffer, |
| 291 int num_bytes, | 293 int num_bytes, |
| 292 const net::CompletionCallback& callback) { | 294 const net::CompletionCallback& callback) { |
| 293 if (!get_content_callback_.is_null()) { | 295 if (!get_content_callback_.is_null()) { |
| 294 get_content_callback_.Run( | 296 get_content_callback_.Run(HTTP_SUCCESS, base::WrapUnique(new std::string( |
| 295 HTTP_SUCCESS, | 297 buffer->data(), num_bytes))); |
|
Roger Tawa OOO till Jul 10th
2016/04/08 15:03:33
Line formatting does not seem right here.
dcheng
2016/04/08 17:27:26
This is technically compliant to the style guide:
| |
| 296 make_scoped_ptr(new std::string(buffer->data(), num_bytes))); | |
| 297 } | 298 } |
| 298 | 299 |
| 299 if (file_writer_) { | 300 if (file_writer_) { |
| 300 const int result = file_writer_->Write( | 301 const int result = file_writer_->Write( |
| 301 buffer, num_bytes, | 302 buffer, num_bytes, |
| 302 base::Bind(&ResponseWriter::DidWrite, | 303 base::Bind(&ResponseWriter::DidWrite, |
| 303 weak_ptr_factory_.GetWeakPtr(), | 304 weak_ptr_factory_.GetWeakPtr(), |
| 304 make_scoped_refptr(buffer), callback)); | 305 make_scoped_refptr(buffer), callback)); |
| 305 if (result != net::ERR_IO_PENDING) | 306 if (result != net::ERR_IO_PENDING) |
| 306 DidWrite(buffer, net::CompletionCallback(), result); | 307 DidWrite(buffer, net::CompletionCallback(), result); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 410 | 411 |
| 411 base::FilePath output_file_path; | 412 base::FilePath output_file_path; |
| 412 GetContentCallback get_content_callback; | 413 GetContentCallback get_content_callback; |
| 413 GetOutputFilePath(&output_file_path, &get_content_callback); | 414 GetOutputFilePath(&output_file_path, &get_content_callback); |
| 414 if (!get_content_callback.is_null()) | 415 if (!get_content_callback.is_null()) |
| 415 get_content_callback = CreateRelayCallback(get_content_callback); | 416 get_content_callback = CreateRelayCallback(get_content_callback); |
| 416 response_writer_ = new ResponseWriter(blocking_task_runner(), | 417 response_writer_ = new ResponseWriter(blocking_task_runner(), |
| 417 output_file_path, | 418 output_file_path, |
| 418 get_content_callback); | 419 get_content_callback); |
| 419 url_fetcher_->SaveResponseWithWriter( | 420 url_fetcher_->SaveResponseWithWriter( |
| 420 scoped_ptr<net::URLFetcherResponseWriter>(response_writer_)); | 421 std::unique_ptr<net::URLFetcherResponseWriter>(response_writer_)); |
| 421 | 422 |
| 422 // Add request headers. | 423 // Add request headers. |
| 423 // Note that SetExtraRequestHeaders clears the current headers and sets it | 424 // Note that SetExtraRequestHeaders clears the current headers and sets it |
| 424 // to the passed-in headers, so calling it for each header will result in | 425 // to the passed-in headers, so calling it for each header will result in |
| 425 // only the last header being set in request headers. | 426 // only the last header being set in request headers. |
| 426 if (!custom_user_agent.empty()) | 427 if (!custom_user_agent.empty()) |
| 427 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); | 428 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); |
| 428 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); | 429 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); |
| 429 url_fetcher_->AddExtraRequestHeader( | 430 url_fetcher_->AddExtraRequestHeader( |
| 430 base::StringPrintf(kAuthorizationHeaderFormat, access_token.data())); | 431 base::StringPrintf(kAuthorizationHeaderFormat, access_token.data())); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 696 // ranges exclusively (i.e., end_position_received should be one passed | 697 // ranges exclusively (i.e., end_position_received should be one passed |
| 697 // the last valid index). So "+ 1" is added. | 698 // the last valid index). So "+ 1" is added. |
| 698 start_position_received = ranges[0].first_byte_position(); | 699 start_position_received = ranges[0].first_byte_position(); |
| 699 end_position_received = ranges[0].last_byte_position() + 1; | 700 end_position_received = ranges[0].last_byte_position() + 1; |
| 700 } | 701 } |
| 701 } | 702 } |
| 702 // The Range header has the received data range, so the start position | 703 // The Range header has the received data range, so the start position |
| 703 // should be always 0. | 704 // should be always 0. |
| 704 DCHECK_EQ(start_position_received, 0); | 705 DCHECK_EQ(start_position_received, 0); |
| 705 | 706 |
| 706 OnRangeRequestComplete(UploadRangeResponse(code, | 707 OnRangeRequestComplete(UploadRangeResponse(code, start_position_received, |
| 707 start_position_received, | |
| 708 end_position_received), | 708 end_position_received), |
| 709 scoped_ptr<base::Value>()); | 709 std::unique_ptr<base::Value>()); |
| 710 | 710 |
| 711 OnProcessURLFetchResultsComplete(); | 711 OnProcessURLFetchResultsComplete(); |
| 712 } else if (code == HTTP_CREATED || code == HTTP_SUCCESS) { | 712 } else if (code == HTTP_CREATED || code == HTTP_SUCCESS) { |
| 713 // The upload is successfully done. Parse the response which should be | 713 // The upload is successfully done. Parse the response which should be |
| 714 // the entry's metadata. | 714 // the entry's metadata. |
| 715 ParseJsonOnBlockingPool(blocking_task_runner(), | 715 ParseJsonOnBlockingPool(blocking_task_runner(), |
| 716 response_writer()->data(), | 716 response_writer()->data(), |
| 717 base::Bind(&UploadRangeRequestBase::OnDataParsed, | 717 base::Bind(&UploadRangeRequestBase::OnDataParsed, |
| 718 weak_ptr_factory_.GetWeakPtr(), | 718 weak_ptr_factory_.GetWeakPtr(), |
| 719 code)); | 719 code)); |
| 720 } else { | 720 } else { |
| 721 // Failed to upload. Run callbacks to notify the error. | 721 // Failed to upload. Run callbacks to notify the error. |
| 722 OnRangeRequestComplete( | 722 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), |
| 723 UploadRangeResponse(code, -1, -1), scoped_ptr<base::Value>()); | 723 std::unique_ptr<base::Value>()); |
| 724 OnProcessURLFetchResultsComplete(); | 724 OnProcessURLFetchResultsComplete(); |
| 725 } | 725 } |
| 726 } | 726 } |
| 727 | 727 |
| 728 void UploadRangeRequestBase::OnDataParsed(DriveApiErrorCode code, | 728 void UploadRangeRequestBase::OnDataParsed(DriveApiErrorCode code, |
| 729 scoped_ptr<base::Value> value) { | 729 std::unique_ptr<base::Value> value) { |
| 730 DCHECK(CalledOnValidThread()); | 730 DCHECK(CalledOnValidThread()); |
| 731 DCHECK(code == HTTP_CREATED || code == HTTP_SUCCESS); | 731 DCHECK(code == HTTP_CREATED || code == HTTP_SUCCESS); |
| 732 | 732 |
| 733 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), std::move(value)); | 733 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), std::move(value)); |
| 734 OnProcessURLFetchResultsComplete(); | 734 OnProcessURLFetchResultsComplete(); |
| 735 } | 735 } |
| 736 | 736 |
| 737 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( | 737 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( |
| 738 DriveApiErrorCode code) { | 738 DriveApiErrorCode code) { |
| 739 OnRangeRequestComplete( | 739 OnRangeRequestComplete(UploadRangeResponse(code, 0, 0), |
| 740 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); | 740 std::unique_ptr<base::Value>()); |
| 741 } | 741 } |
| 742 | 742 |
| 743 //========================== ResumeUploadRequestBase ========================= | 743 //========================== ResumeUploadRequestBase ========================= |
| 744 | 744 |
| 745 ResumeUploadRequestBase::ResumeUploadRequestBase( | 745 ResumeUploadRequestBase::ResumeUploadRequestBase( |
| 746 RequestSender* sender, | 746 RequestSender* sender, |
| 747 const GURL& upload_location, | 747 const GURL& upload_location, |
| 748 int64_t start_position, | 748 int64_t start_position, |
| 749 int64_t end_position, | 749 int64_t end_position, |
| 750 int64_t content_length, | 750 int64_t content_length, |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 916 base::Bind(&MultipartUploadRequestBase::OnDataParsed, | 916 base::Bind(&MultipartUploadRequestBase::OnDataParsed, |
| 917 weak_ptr_factory_.GetWeakPtr(), code, | 917 weak_ptr_factory_.GetWeakPtr(), code, |
| 918 notify_complete_callback)); | 918 notify_complete_callback)); |
| 919 } else { | 919 } else { |
| 920 NotifyError(MapJsonError(code, body)); | 920 NotifyError(MapJsonError(code, body)); |
| 921 notify_complete_callback.Run(); | 921 notify_complete_callback.Run(); |
| 922 } | 922 } |
| 923 } | 923 } |
| 924 | 924 |
| 925 void MultipartUploadRequestBase::NotifyError(DriveApiErrorCode code) { | 925 void MultipartUploadRequestBase::NotifyError(DriveApiErrorCode code) { |
| 926 callback_.Run(code, scoped_ptr<FileResource>()); | 926 callback_.Run(code, std::unique_ptr<FileResource>()); |
| 927 } | 927 } |
| 928 | 928 |
| 929 void MultipartUploadRequestBase::NotifyUploadProgress( | 929 void MultipartUploadRequestBase::NotifyUploadProgress( |
| 930 const net::URLFetcher* source, | 930 const net::URLFetcher* source, |
| 931 int64_t current, | 931 int64_t current, |
| 932 int64_t total) { | 932 int64_t total) { |
| 933 if (!progress_callback_.is_null()) | 933 if (!progress_callback_.is_null()) |
| 934 progress_callback_.Run(current, total); | 934 progress_callback_.Run(current, total); |
| 935 } | 935 } |
| 936 | 936 |
| 937 void MultipartUploadRequestBase::OnDataParsed( | 937 void MultipartUploadRequestBase::OnDataParsed( |
| 938 DriveApiErrorCode code, | 938 DriveApiErrorCode code, |
| 939 const base::Closure& notify_complete_callback, | 939 const base::Closure& notify_complete_callback, |
| 940 scoped_ptr<base::Value> value) { | 940 std::unique_ptr<base::Value> value) { |
| 941 DCHECK(thread_checker_.CalledOnValidThread()); | 941 DCHECK(thread_checker_.CalledOnValidThread()); |
| 942 if (value) | 942 if (value) |
| 943 callback_.Run(code, google_apis::FileResource::CreateFrom(*value)); | 943 callback_.Run(code, google_apis::FileResource::CreateFrom(*value)); |
| 944 else | 944 else |
| 945 NotifyError(DRIVE_PARSE_ERROR); | 945 NotifyError(DRIVE_PARSE_ERROR); |
| 946 notify_complete_callback.Run(); | 946 notify_complete_callback.Run(); |
| 947 } | 947 } |
| 948 | 948 |
| 949 //============================ DownloadFileRequestBase ========================= | 949 //============================ DownloadFileRequestBase ========================= |
| 950 | 950 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1002 download_action_callback_.Run(code, temp_file); | 1002 download_action_callback_.Run(code, temp_file); |
| 1003 OnProcessURLFetchResultsComplete(); | 1003 OnProcessURLFetchResultsComplete(); |
| 1004 } | 1004 } |
| 1005 | 1005 |
| 1006 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 1006 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
| 1007 DriveApiErrorCode code) { | 1007 DriveApiErrorCode code) { |
| 1008 download_action_callback_.Run(code, base::FilePath()); | 1008 download_action_callback_.Run(code, base::FilePath()); |
| 1009 } | 1009 } |
| 1010 | 1010 |
| 1011 } // namespace google_apis | 1011 } // namespace google_apis |
| OLD | NEW |